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

pass Host to OIDC settings from http request context #21362

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
10 changes: 8 additions & 2 deletions src/core/controllers/oidc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"`
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down
10 changes: 10 additions & 0 deletions src/lib/config/userconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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(),
Expand Down
16 changes: 16 additions & 0 deletions src/lib/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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/"
Expand Down Expand Up @@ -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
}
Loading