From 2f7bbaab85007a008ef957ccec21dc18dd2cbcba Mon Sep 17 00:00:00 2001 From: Yordan Kinkov <yordan.kinkov@vereign.com> Date: Thu, 13 Oct 2022 21:41:41 +0300 Subject: [PATCH] Apply Authentication middleware to cache service --- cmd/cache/main.go | 10 ++++++++++ internal/config/config.go | 7 +++++++ 2 files changed, 17 insertions(+) diff --git a/cmd/cache/main.go b/cmd/cache/main.go index 37b7ec3..c81842f 100644 --- a/cmd/cache/main.go +++ b/cmd/cache/main.go @@ -28,6 +28,7 @@ import ( "gitlab.com/gaia-x/data-infrastructure-federation-services/tsa/cache/internal/service" "gitlab.com/gaia-x/data-infrastructure-federation-services/tsa/cache/internal/service/cache" "gitlab.com/gaia-x/data-infrastructure-federation-services/tsa/cache/internal/service/health" + "gitlab.com/gaia-x/data-infrastructure-federation-services/tsa/golib/auth" "gitlab.com/gaia-x/data-infrastructure-federation-services/tsa/golib/graceful" ) @@ -103,6 +104,15 @@ func main() { openapiServer = goaopenapisrv.New(openapiEndpoints, mux, dec, enc, nil, errFormatter, nil, nil) } + // Apply Authentication middleware if enabled + if cfg.Auth.Enabled { + m, err := auth.NewMiddleware(cfg.Auth.JwkURL, cfg.Auth.RefreshInterval, http.DefaultClient) + if err != nil { + log.Fatalf("failed to create authentication middleware: %v", err) + } + cacheServer.Use(m.Handler()) + } + // Configure the mux. goacachesrv.Mount(mux, cacheServer) goahealthsrv.Mount(mux, healthServer) diff --git a/internal/config/config.go b/internal/config/config.go index 2cb86d4..3d7b270 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -7,6 +7,7 @@ type Config struct { Redis redisConfig Nats natsConfig Metrics metricsConfig + Auth authConfig LogLevel string `envconfig:"LOG_LEVEL" default:"INFO"` } @@ -35,3 +36,9 @@ type natsConfig struct { type metricsConfig struct { Addr string `envconfig:"METRICS_ADDR" default:":2112"` } + +type authConfig struct { + Enabled bool `envconfig:"AUTH_ENABLED" default:"true"` + JwkURL string `envconfig:"AUTH_JWK_URL"` + RefreshInterval time.Duration `envconfig:"AUTH_REFRESH_INTERVAL" default:"1h"` +} -- GitLab