Skip to content
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
21 changes: 21 additions & 0 deletions backend/options/oidc.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"crypto/tls"
"fmt"
"net"
"net/url"
"os"
"strings"

Expand Down Expand Up @@ -123,6 +124,7 @@ func (options *OIDC) Validate() error {
if options.CallbackURL == "" {
return fmt.Errorf("OIDC callback URL cannot be empty")
}

if options.CAFile != "" && options.TLSConfig != nil {
return fmt.Errorf("cannot use both CA file and embedded OIDC server")
}
Expand All @@ -131,6 +133,25 @@ func (options *OIDC) Validate() error {
return fmt.Errorf("invalid OIDC provider type: %s", options.Type)
}

issuerURL, err := url.Parse(options.IssuerURL)
Comment thread
cnvergence marked this conversation as resolved.
if err != nil {
return fmt.Errorf("--oidc-issuer-url must be a valid URL: %w", err)
}
if issuerURL.Scheme != "http" && issuerURL.Scheme != "https" {
return fmt.Errorf("--oidc-issuer-url must use http or https scheme, got: %s", issuerURL.Scheme)
}

callbackURL, err := url.Parse(options.CallbackURL)
if err != nil {
return fmt.Errorf("--oidc-callback-url must be a valid URL: %w", err)
}
if callbackURL.Scheme != "http" && callbackURL.Scheme != "https" {
return fmt.Errorf("--oidc-callback-url must use http or https scheme, got: %s", callbackURL.Scheme)
}
if !strings.HasSuffix(callbackURL.Path, "/api/callback") {
Comment thread
cnvergence marked this conversation as resolved.
return fmt.Errorf("--oidc-callback-url must end with '/api/callback', got path: %s", callbackURL.Path)
}

if options.Type == string(kubebindv1alpha2.OIDCProviderTypeEmbedded) && !strings.HasSuffix(options.IssuerURL, "/oidc") {
return fmt.Errorf("--oidc-issuer-url must end with '/oidc' when using embedded OIDC provider")
}
Expand Down
56 changes: 52 additions & 4 deletions backend/options/oidc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func TestOIDCValidate(t *testing.T) {
IssuerClientID: "test-client-id",
IssuerClientSecret: "test-client-secret",
IssuerURL: "http://localhost:8080/oidc",
CallbackURL: "http://localhost:8080/callback",
CallbackURL: "http://localhost:8080/api/callback",
},
wantErr: false,
},
Expand All @@ -49,7 +49,7 @@ func TestOIDCValidate(t *testing.T) {
IssuerClientID: "test-client-id",
IssuerClientSecret: "test-client-secret",
IssuerURL: "http://localhost:8080",
CallbackURL: "http://localhost:8080/callback",
CallbackURL: "http://localhost:8080/api/callback",
},
wantErr: true,
errMsg: "--oidc-issuer-url must end with '/oidc' when using embedded OIDC provider",
Expand All @@ -61,7 +61,7 @@ func TestOIDCValidate(t *testing.T) {
IssuerClientID: "test-client-id",
IssuerClientSecret: "test-client-secret",
IssuerURL: "http://localhost:8080/oidc/",
CallbackURL: "http://localhost:8080/callback",
CallbackURL: "http://localhost:8080/api/callback",
},
wantErr: true,
errMsg: "--oidc-issuer-url must end with '/oidc' when using embedded OIDC provider",
Expand All @@ -73,11 +73,59 @@ func TestOIDCValidate(t *testing.T) {
IssuerClientID: "test-client-id",
IssuerClientSecret: "test-client-secret",
IssuerURL: "http://localhost:8080",
CallbackURL: "http://localhost:8080/callback",
CallbackURL: "http://localhost:8080/api/callback",
AllowedGroups: []string{"admins"},
},
wantErr: false,
},
{
name: "malformed issuer URL",
options: &OIDC{
Type: string(kubebindv1alpha2.OIDCProviderTypeEmbedded),
IssuerClientID: "test-client-id",
IssuerClientSecret: "test-client-secret",
IssuerURL: "not-a-valid-url",
CallbackURL: "http://localhost:8080/api/callback",
},
wantErr: true,
errMsg: "--oidc-issuer-url must use http or https scheme, got: ",
},
{
name: "malformed callback URL",
options: &OIDC{
Type: string(kubebindv1alpha2.OIDCProviderTypeEmbedded),
IssuerClientID: "test-client-id",
IssuerClientSecret: "test-client-secret",
IssuerURL: "http://localhost:8080/oidc",
CallbackURL: "not-a-valid-url",
},
wantErr: true,
errMsg: "--oidc-callback-url must use http or https scheme, got: ",
},
{
name: "callback URL with invalid scheme",
options: &OIDC{
Type: string(kubebindv1alpha2.OIDCProviderTypeEmbedded),
IssuerClientID: "test-client-id",
IssuerClientSecret: "test-client-secret",
IssuerURL: "http://localhost:8080/oidc",
CallbackURL: "ftp://localhost:8080/api/callback",
},
wantErr: true,
errMsg: "--oidc-callback-url must use http or https scheme, got: ftp",
},
{
name: "callback URL with only /callback (missing /api prefix)",
options: &OIDC{
Type: string(kubebindv1alpha2.OIDCProviderTypeEmbedded),
IssuerClientID: "test-client-id",
IssuerClientSecret: "test-client-secret",
IssuerURL: "http://localhost:8080/oidc",
CallbackURL: "http://localhost:8080/callback",
},
wantErr: true,
errMsg: "--oidc-callback-url must end with '/api/callback', got path: /callback",
},
}

for _, tt := range tests {
Expand Down