From 2766ea3af2ffc596ee5809b16f44f34d3499faf1 Mon Sep 17 00:00:00 2001 From: Steeve Chailloux Date: Thu, 12 Sep 2024 10:20:24 +0200 Subject: [PATCH] pass Host to OIDC settings from http request context Signed-off-by: Steeve Chailloux --- src/core/controllers/oidc.go | 10 ++++++++-- src/lib/config/userconfig.go | 10 ++++++++++ src/lib/context.go | 16 ++++++++++++++++ 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/src/core/controllers/oidc.go b/src/core/controllers/oidc.go index 295acb2cf8b..8adecc38939 100644 --- a/src/core/controllers/oidc.go +++ b/src/core/controllers/oidc.go @@ -26,6 +26,7 @@ import ( "github.com/goharbor/harbor/src/common/utils" ctluser "github.com/goharbor/harbor/src/controller/user" "github.com/goharbor/harbor/src/core/api" + "github.com/goharbor/harbor/src/lib" "github.com/goharbor/harbor/src/lib/config" "github.com/goharbor/harbor/src/lib/errors" "github.com/goharbor/harbor/src/lib/log" @@ -43,6 +44,11 @@ type OIDCController struct { api.BaseController } +// Context returns the context.Context from http.Request +func (oc *OIDCController) Context() context.Context { + return lib.WithHost(oc.Ctx.Request.Context(), oc.Ctx.Request.Host) +} + type onboardReq struct { Username string `json:"username"` } @@ -112,7 +118,7 @@ func (oc *OIDCController) Callback() { } } code := oc.Ctx.Request.URL.Query().Get("code") - ctx := oc.Ctx.Request.Context() + ctx := oc.Context() token, err := oidc.ExchangeToken(ctx, code) if err != nil { log.Errorf("Failed to exchange token, error: %v", err) @@ -276,7 +282,7 @@ func (oc *OIDCController) Onboard() { oc.SendInternalServerError(err) return } - ctx := oc.Ctx.Request.Context() + ctx := oc.Context() if user, onboarded := userOnboard(ctx, oc, d, username, tb); onboarded { user.OIDCUserMeta = nil if err := oc.DelSession(userInfoKey); err != nil { diff --git a/src/lib/config/userconfig.go b/src/lib/config/userconfig.go index 4012097c9e3..e78ff8fad5f 100644 --- a/src/lib/config/userconfig.go +++ b/src/lib/config/userconfig.go @@ -16,10 +16,13 @@ package config import ( "context" + "fmt" + "net/url" "strings" "github.com/goharbor/harbor/src/common" "github.com/goharbor/harbor/src/common/models" + "github.com/goharbor/harbor/src/lib" cfgModels "github.com/goharbor/harbor/src/lib/config/models" "github.com/goharbor/harbor/src/lib/errors" "github.com/goharbor/harbor/src/lib/log" @@ -162,6 +165,13 @@ func OIDCSetting(ctx context.Context) (*cfgModels.OIDCSetting, error) { } scopeStr := mgr.Get(ctx, common.OIDCScope).GetString() extEndpoint := strings.TrimSuffix(mgr.Get(context.Background(), common.ExtEndpoint).GetString(), "/") + if host := lib.GetHost(ctx); host != "" { + u, err := url.Parse(extEndpoint) + if err != nil { + return nil, err + } + extEndpoint = fmt.Sprintf("%s://%s", u.Scheme, host) + } scope := SplitAndTrim(scopeStr, ",") return &cfgModels.OIDCSetting{ Name: mgr.Get(ctx, common.OIDCName).GetString(), diff --git a/src/lib/context.go b/src/lib/context.go index 24ef00451c6..e994b5626f1 100644 --- a/src/lib/context.go +++ b/src/lib/context.go @@ -27,6 +27,7 @@ const ( contextKeyAuthMode contextKey = "authMode" contextKeyCarrySession contextKey = "carrySession" contextKeyRequestID contextKey = "X-Request-ID" + contextKeyHost contextKey = "host" ) // ArtifactInfo wraps the artifact info extracted from the request to "/v2/" @@ -128,3 +129,18 @@ func GetXRequestID(ctx context.Context) string { } return id } + +// WithHost returns a context with Host set +func WithHost(ctx context.Context, host string) context.Context { + return setToContext(ctx, contextKeyHost, host) +} + +// GetHost gets the Host from the context +func GetHost(ctx context.Context) string { + host := "" + value := getFromContext(ctx, contextKeyHost) + if value != nil { + host, _ = value.(string) + } + return host +}