Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): swap out internal issuer for external issuer endpoint #1027

Merged
merged 3 commits into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion service/internal/auth/authn.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"crypto"
"crypto/sha256"
"encoding/base64"
"encoding/json"
"fmt"
"log/slog"
"net/http"
Expand Down Expand Up @@ -86,7 +87,7 @@ type Authentication struct {
}

// Creates new authN which is used to verify tokens for a set of given issuers
func NewAuthenticator(ctx context.Context, cfg Config, logr *logger.Logger) (*Authentication, error) {
func NewAuthenticator(ctx context.Context, cfg Config, logr *logger.Logger, wellknownRegistration func(namespace string, config any) error) (*Authentication, error) {
a := &Authentication{
enforceDPoP: cfg.EnforceDPoP,
logger: logr,
Expand All @@ -106,6 +107,13 @@ func NewAuthenticator(ctx context.Context, cfg Config, logr *logger.Logger) (*Au
return nil, err
}

// If the issuer is different from the one in the configuration, update the configuration
// This could happen if we are hitting an internal endpoint. Example we might point to https://keycloak.opentdf.svc/realms/opentdf
// but the external facing issuer is https://keycloak.opentdf.local/realms/opentdf
if oidcConfig.Issuer != cfg.Issuer {
cfg.Issuer = oidcConfig.Issuer
}

cacheInterval, err := time.ParseDuration(cfg.CacheRefresh)
if err != nil {
logr.ErrorContext(ctx, fmt.Sprintf("Invalid cache_refresh_interval [%s]", cfg.CacheRefresh), "err", err)
Expand Down Expand Up @@ -140,6 +148,27 @@ func NewAuthenticator(ctx context.Context, cfg Config, logr *logger.Logger) (*Au

a.oidcConfiguration = cfg.AuthNConfig

// Try an register oidc issuer to wellknown service but don't return an error if it fails
if err := wellknownRegistration("platform_issuer", cfg.Issuer); err != nil {
logr.Warn("failed to register platform issuer", slog.String("error", err.Error()))
}

var oidcConfigMap map[string]any

// Create a map of the oidc configuration
oidcConfigBytes, err := json.Marshal(oidcConfig)
if err != nil {
return nil, err
}

if err := json.Unmarshal(oidcConfigBytes, &oidcConfigMap); err != nil {
return nil, err
}

if err := wellknownRegistration("idp", oidcConfigMap); err != nil {
logr.Warn("failed to register platform idp information", slog.String("error", err.Error()))
}

return a, nil
}

Expand Down
7 changes: 5 additions & 2 deletions service/internal/auth/authn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func (s *AuthSuite) SetupTest() {
s.server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
if r.URL.Path == "/.well-known/openid-configuration" {
_, err := w.Write([]byte(fmt.Sprintf(`{"jwks_uri": "%s/jwks"}`, s.server.URL)))
_, err := w.Write([]byte(fmt.Sprintf(`{"issuer":"%s","jwks_uri": "%s/jwks"}`, s.server.URL, s.server.URL)))
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -173,6 +173,7 @@ func (s *AuthSuite) SetupTest() {
&logger.Logger{
Logger: slog.New(slog.Default().Handler()),
},
func(_ string, _ any) error { return nil },
)

s.Require().NoError(err)
Expand Down Expand Up @@ -603,7 +604,9 @@ func (s *AuthSuite) Test_Allowing_Auth_With_No_DPoP() {
config.AuthNConfig = authnConfig
auth, err := NewAuthenticator(context.Background(), config, &logger.Logger{
Logger: slog.New(slog.Default().Handler()),
})
},
func(_ string, _ any) error { return nil },
)

s.Require().NoError(err)

Expand Down
6 changes: 1 addition & 5 deletions service/internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ func NewOpenTDFServer(config Config, logr *logger.Logger) (*OpenTDFServer, error
context.Background(),
config.Auth,
logr,
config.WellKnownConfigRegister,
)
if err != nil {
return nil, fmt.Errorf("failed to create authentication interceptor: %w", err)
Expand All @@ -127,11 +128,6 @@ func NewOpenTDFServer(config Config, logr *logger.Logger) (*OpenTDFServer, error
logr.Warn("disabling authentication. this is deprecated and will be removed. if you are using an IdP without DPoP set `enforceDPoP = false`")
}

// Try an register oidc issuer to wellknown service but don't return an error if it fails
if err := config.WellKnownConfigRegister("platform_issuer", config.Auth.Issuer); err != nil {
logr.Warn("failed to register platform issuer", slog.String("error", err.Error()))
}

// Create grpc server and in process grpc server
grpcServer, err := newGrpcServer(config, authN)
if err != nil {
Expand Down
Loading