pomerium/config/helpers.go
Caleb Doxsey d5178f24a3
config: support multiple running services in addition to all-in-one mode (#5656)
## Summary
Currently you can either run a single service or all-in-one mode. This
PR adds support for running any 2 or 3 services together. For example
`services: authorize,proxy`.

I think I updated the conditions to make sense, but there's some risk
here as there may have been some assumptions in places I'm overlooking.

## Related issues
-
[ENG-2485](https://linear.app/pomerium/issue/ENG-2485/core-support-running-multiple-but-not-all-services)


## Checklist

- [x] reference any related issues
- [x] updated unit tests
- [x] add appropriate label (`enhancement`, `bug`, `breaking`,
`dependencies`, `ci`)
- [x] ready for review
2025-07-21 14:28:31 -06:00

122 lines
3 KiB
Go

package config
import (
"strings"
)
const (
// ServiceAll represents running all services in "all-in-one" mode
ServiceAll = "all"
// ServiceProxy represents running the proxy service component
ServiceProxy = "proxy"
// ServiceAuthorize represents running the authorize service component
ServiceAuthorize = "authorize"
// ServiceAuthenticate represents running the authenticate service component
ServiceAuthenticate = "authenticate"
// ServiceCache represents running the cache service component
ServiceCache = "cache"
// ServiceDataBroker represents running the databroker service component
ServiceDataBroker = "databroker"
// StoragePostgresName is the name of the Postgres storage backend
StoragePostgresName = "postgres"
// StorageInMemoryName is the name of the in-memory storage backend
StorageInMemoryName = "memory"
)
// IsAll checks to see if we should be running all services
func IsAll(services string) bool {
var isAuthenticate, isAuthorize, isDataBroker, isProxy bool
for _, svc := range splitServices(services) {
switch svc {
case ServiceAll:
isAuthenticate = true
isAuthorize = true
isDataBroker = true
isProxy = true
case ServiceAuthenticate:
isAuthenticate = true
case ServiceAuthorize:
isAuthorize = true
case ServiceCache, ServiceDataBroker:
isDataBroker = true
case ServiceProxy:
isProxy = true
}
}
return isAuthenticate && isAuthorize && isDataBroker && isProxy
}
// IsAuthenticate checks to see if we should be running the authenticate service
func IsAuthenticate(services string) bool {
for _, svc := range splitServices(services) {
switch svc {
case ServiceAll, ServiceAuthenticate:
return true
}
}
return false
}
// IsAuthorize checks to see if we should be running the authorize service
func IsAuthorize(services string) bool {
for _, svc := range splitServices(services) {
switch svc {
case ServiceAll, ServiceAuthorize:
return true
}
}
return false
}
// IsDataBroker checks to see if we should be running the databroker service
func IsDataBroker(services string) bool {
for _, svc := range splitServices(services) {
switch svc {
case ServiceAll, ServiceCache, ServiceDataBroker:
return true
}
}
return false
}
// IsProxy checks to see if we should be running the proxy service
func IsProxy(services string) bool {
for _, svc := range splitServices(services) {
switch svc {
case ServiceAll, ServiceProxy:
return true
}
}
return false
}
// IsValidService checks to see if a service is a valid service mode
func IsValidService(services string) bool {
svcs := splitServices(services)
for _, svc := range svcs {
switch svc {
case
ServiceAll,
ServiceAuthenticate,
ServiceAuthorize,
ServiceCache,
ServiceDataBroker,
ServiceProxy:
// valid
default:
return false
}
}
return len(svcs) > 0
}
func splitServices(raw string) []string {
var svcs []string
for _, s := range strings.Split(raw, ",") {
s = strings.TrimSpace(strings.ToLower(s))
if s != "" {
svcs = append(svcs, s)
}
}
return svcs
}