Skip to content

Commit

Permalink
fix: wire up new config structs into web application bootstrap
Browse files Browse the repository at this point in the history
fixes #222
  • Loading branch information
shipperizer committed Mar 27, 2024
1 parent caa4f60 commit b1e0974
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 40 deletions.
62 changes: 31 additions & 31 deletions cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,21 +54,43 @@ func serve() {
}

logger := logging.NewLogger(specs.LogLevel, specs.LogFile)

monitor := prometheus.NewMonitor("identity-admin-ui", logger)
tracer := tracing.NewTracer(tracing.NewConfig(specs.TracingEnabled, specs.OtelGRPCEndpoint, specs.OtelHTTPEndpoint, logger))

hAdminClient := ih.NewClient(specs.HydraAdminURL, specs.Debug)
kAdminClient := ik.NewClient(specs.KratosAdminURL, specs.Debug)
kPublicClient := ik.NewClient(specs.KratosPublicURL, specs.Debug)
oPublicClient := io.NewClient(specs.OathkeeperPublicURL, specs.Debug)
extCfg := web.NewExternalClientsConfig(
ih.NewClient(specs.HydraAdminURL, specs.Debug),
ik.NewClient(specs.KratosAdminURL, specs.Debug),
ik.NewClient(specs.KratosPublicURL, specs.Debug),
io.NewClient(specs.OathkeeperPublicURL, specs.Debug),
openfga.NewClient(
openfga.NewConfig(
specs.ApiScheme,
specs.ApiHost,
specs.StoreId,
specs.ApiToken,
specs.ModelId,
specs.Debug,
tracer,
monitor,
logger,
),
),
// default to noop client for authorization
openfga.NewNoopClient(tracer, monitor, logger),
)

if specs.AuthorizationEnabled {
logger.Info("Authorization is enabled")
extCfg.SetAuthorizer(extCfg.OpenFGA())
}

k8sCoreV1, err := k8s.NewCoreV1Client("")

if err != nil {
panic(err)
}

// TODO @shipperizer standardize idp, schemas and rules configs
idpConfig := &idp.Config{
K8s: k8sCoreV1,
Name: specs.IDPConfigMapName,
Expand All @@ -77,30 +99,16 @@ func serve() {

schemasConfig := &schemas.Config{
K8s: k8sCoreV1,
Kratos: kPublicClient.IdentityApi(),
Kratos: extCfg.KratosPublic().IdentityApi(),
Name: specs.SchemasConfigMapName,
Namespace: specs.SchemasConfigMapNamespace,
}

rulesConfig := rules.NewConfig(specs.RulesConfigMapName, specs.RulesConfigFileName, specs.RulesConfigMapNamespace, k8sCoreV1, oPublicClient.ApiApi())

ofgaClient := openfga.NewClient(
openfga.NewConfig(
specs.ApiScheme,
specs.ApiHost,
specs.StoreId,
specs.ApiToken,
specs.ModelId,
specs.Debug,
tracer,
monitor,
logger,
),
)
rulesConfig := rules.NewConfig(specs.RulesConfigMapName, specs.RulesConfigFileName, specs.RulesConfigMapNamespace, k8sCoreV1, extCfg.OathkeeperPublic().ApiApi())

if specs.AuthorizationEnabled {
authorizer := authorization.NewAuthorizer(
ofgaClient,
extCfg.OpenFGA(),
tracer,
monitor,
logger,
Expand All @@ -111,15 +119,7 @@ func serve() {
}
}

var router http.Handler

if specs.AuthorizationEnabled {
logger.Info("Authorization is enabled")
router = web.NewRouter(idpConfig, schemasConfig, rulesConfig, hAdminClient, kAdminClient, ofgaClient, ofgaClient, tracer, monitor, logger)
} else {
logger.Info("Authorization is disabled, using noop authorizer")
router = web.NewRouter(idpConfig, schemasConfig, rulesConfig, hAdminClient, kAdminClient, ofgaClient, openfga.NewNoopClient(tracer, monitor, logger), tracer, monitor, logger)
}
router := web.NewRouter(idpConfig, schemasConfig, rulesConfig, extCfg, web.NewO11yConfig(tracer, monitor, logger))

logger.Infof("Starting server on port %v", specs.Port)

Expand Down
20 changes: 11 additions & 9 deletions pkg/web/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,8 @@ import (

chi "github.com/go-chi/chi/v5"
middleware "github.com/go-chi/chi/v5/middleware"
trace "go.opentelemetry.io/otel/trace"

"github.com/canonical/identity-platform-admin-ui/internal/authorization"
ih "github.com/canonical/identity-platform-admin-ui/internal/hydra"
ik "github.com/canonical/identity-platform-admin-ui/internal/kratos"
"github.com/canonical/identity-platform-admin-ui/internal/logging"
"github.com/canonical/identity-platform-admin-ui/internal/monitoring"
"github.com/canonical/identity-platform-admin-ui/internal/tracing"
Expand All @@ -28,9 +25,13 @@ import (
"github.com/canonical/identity-platform-admin-ui/pkg/status"
)

func NewRouter(idpConfig *idp.Config, schemasConfig *schemas.Config, rulesConfig *rules.Config, hydraClient *ih.Client, kratos *ik.Client, ofga OpenFGAClientInterface, authorizationClient OpenFGAClientInterface, tracer trace.Tracer, monitor monitoring.MonitorInterface, logger logging.LoggerInterface) http.Handler {
func NewRouter(idpConfig *idp.Config, schemasConfig *schemas.Config, rulesConfig *rules.Config, externalConfig ExternalClientsConfigInterface, ollyConfig O11yConfigInterface) http.Handler {
router := chi.NewMux()

logger := ollyConfig.Logger()
monitor := ollyConfig.Monitor()
tracer := ollyConfig.Tracer()

middlewares := make(chi.Middlewares, 0)
middlewares = append(
middlewares,
Expand All @@ -52,17 +53,17 @@ func NewRouter(idpConfig *idp.Config, schemasConfig *schemas.Config, rulesConfig
// apply authorization middleware using With to overcome issue with <id> URLParams not available
router = router.With(
authorization.NewMiddleware(
authorization.NewAuthorizer(authorizationClient, tracer, monitor, logger), monitor, logger).Authorize(),
authorization.NewAuthorizer(externalConfig.Authorizer(), tracer, monitor, logger), monitor, logger).Authorize(),
).(*chi.Mux)

status.NewAPI(tracer, monitor, logger).RegisterEndpoints(router)
metrics.NewAPI(logger).RegisterEndpoints(router)
identities.NewAPI(
identities.NewService(kratos.IdentityApi(), tracer, monitor, logger),
identities.NewService(externalConfig.KratosAdmin().IdentityApi(), tracer, monitor, logger),
logger,
).RegisterEndpoints(router)
clients.NewAPI(
clients.NewService(hydraClient, tracer, monitor, logger),
clients.NewService(externalConfig.HydraAdmin(), tracer, monitor, logger),
logger,
).RegisterEndpoints(router)
idp.NewAPI(
Expand All @@ -78,16 +79,17 @@ func NewRouter(idpConfig *idp.Config, schemasConfig *schemas.Config, rulesConfig
logger,
).RegisterEndpoints(router)
roles.NewAPI(
roles.NewService(ofga, tracer, monitor, logger),
roles.NewService(externalConfig.OpenFGA(), tracer, monitor, logger),
tracer,
monitor,
logger,
).RegisterEndpoints(router)
groups.NewAPI(
groups.NewService(ofga, tracer, monitor, logger),
groups.NewService(externalConfig.OpenFGA(), tracer, monitor, logger),
tracer,
monitor,
logger,
).RegisterEndpoints(router)

return tracing.NewMiddleware(monitor, logger).OpenTelemetry(router)
}

0 comments on commit b1e0974

Please sign in to comment.