Skip to content

Commit 32fbbc4

Browse files
committed
backend: allow OIDC cookies via flag
Add --oidc-use-cookie flag to support OIDC token cookies in non-in-cluster environments. Fixes #4481
1 parent d7d68c3 commit 32fbbc4

File tree

5 files changed

+12
-6
lines changed

5 files changed

+12
-6
lines changed

backend/cmd/headlamp.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,9 @@ func createHeadlampHandler(config *HeadlampConfig) http.Handler {
426426
go kubeconfig.LoadAndWatchFiles(config.KubeConfigStore, kubeConfigPath, kubeconfig.KubeConfig, skipFunc)
427427
}
428428

429+
// Initialize OIDC Cookie setting from flags
430+
config.OidcUseCookie = config.HeadlampCFG.OidcUseCookie
431+
429432
// In-cluster
430433
if config.UseInCluster {
431434
context, err := kubeconfig.GetInClusterContext(
@@ -1284,8 +1287,8 @@ func (c *HeadlampConfig) helmRouteReleaseHandler(
12841287
// Create a copy of the context to avoid modifying the cached context
12851288
context = context.Copy()
12861289

1287-
// If headlamp is running in cluster, use the token from the cookie for oidc auth
1288-
if c.UseInCluster && context.OidcConf != nil {
1290+
// If running in cluster or explicitly enabled via flag, use the token from the cookie for oidc auth
1291+
if (c.UseInCluster || c.OidcUseCookie) && context.OidcConf != nil {
12891292
setTokenFromCookie(r, clusterName)
12901293
}
12911294

backend/cmd/server.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ func buildHeadlampCFG(conf *config.Config, kubeConfigStore kubeconfig.ContextSto
100100
ProxyURLs: strings.Split(conf.ProxyURLs, ","),
101101
TLSCertPath: conf.TLSCertPath,
102102
TLSKeyPath: conf.TLSKeyPath,
103+
OidcUseCookie: conf.OidcUseCookie,
103104
}
104105
}
105106

backend/pkg/config/config.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ type Config struct {
6363
OidcValidatorIdpIssuerURL string `koanf:"oidc-validator-idp-issuer-url"`
6464
OidcScopes string `koanf:"oidc-scopes"`
6565
OidcUseAccessToken bool `koanf:"oidc-use-access-token"`
66+
OidcUseCookie bool `koanf:"oidc-use-cookie"`
6667
OidcSkipTLSVerify bool `koanf:"oidc-skip-tls-verify"`
6768
OidcCAFile string `koanf:"oidc-ca-file"`
6869
MeUsernamePath string `koanf:"me-username-path"`
@@ -86,10 +87,9 @@ type Config struct {
8687
}
8788

8889
func (c *Config) Validate() error {
89-
if !c.InCluster && (c.OidcClientID != "" || c.OidcClientSecret != "" || c.OidcIdpIssuerURL != "" ||
90+
if !c.InCluster && !c.OidcUseCookie && (c.OidcClientID != "" || c.OidcClientSecret != "" || c.OidcIdpIssuerURL != "" ||
9091
c.OidcValidatorClientID != "" || c.OidcValidatorIdpIssuerURL != "") {
91-
return errors.New(`oidc-client-id, oidc-client-secret, oidc-idp-issuer-url, oidc-validator-client-id,
92-
oidc-validator-idp-issuer-url, flags are only meant to be used in inCluster mode`)
92+
return errors.New("oidc-client-id, oidc-client-secret, oidc-idp-issuer-url, oidc-validator-client-id, oidc-validator-idp-issuer-url, flags are only meant to be used in inCluster mode or with --oidc-use-cookie")
9393
}
9494

9595
// OIDC TLS verification warning.
@@ -447,6 +447,7 @@ func addOIDCFlags(f *flag.FlagSet) {
447447
f.Bool("oidc-skip-tls-verify", false, "Skip TLS verification for OIDC")
448448
f.String("oidc-ca-file", "", "CA file for OIDC")
449449
f.Bool("oidc-use-access-token", false, "Setup oidc to pass through the access_token instead of the default id_token")
450+
f.Bool("oidc-use-cookie", false, "Enable OIDC cookie usage even when not running in-cluster")
450451
f.Bool("oidc-use-pkce", false, "Use PKCE (Proof Key for Code Exchange) for enhanced security in OIDC flow")
451452
f.String("me-username-path", DefaultMeUsernamePath,
452453
"Comma separated JMESPath expressions used to read username from the JWT payload")

backend/pkg/config/config_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ func TestParseErrors(t *testing.T) {
173173
{
174174
name: "oidc_settings_without_incluster",
175175
args: []string{"go run ./cmd", "-oidc-client-id=noClient"},
176-
errorContains: "are only meant to be used in inCluster mode",
176+
errorContains: "flags are only meant to be used in inCluster mode or with --oidc-use-cookie",
177177
},
178178
{
179179
name: "invalid_base_url",

backend/pkg/headlampconfig/headlampConfig.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,5 @@ type HeadlampCFG struct {
6262
ProxyURLs []string
6363
TLSCertPath string
6464
TLSKeyPath string
65+
OidcUseCookie bool
6566
}

0 commit comments

Comments
 (0)