Skip to content

Commit 67f9b5d

Browse files
authored
fix: error handling for fetching oidc config from well-known endpoint (#7301)
fix: empty openid config from well-known endpoint Signed-off-by: Huabing Zhao <[email protected]>
1 parent d6c886c commit 67f9b5d

File tree

2 files changed

+41
-5
lines changed

2 files changed

+41
-5
lines changed

internal/gatewayapi/securitypolicy.go

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1417,7 +1417,7 @@ func (t *Translator) buildOIDCProvider(policy *egv1a1.SecurityPolicy, resources
14171417
if provider.TokenEndpoint == nil || provider.AuthorizationEndpoint == nil {
14181418
discoveredConfig, err := fetchEndpointsFromIssuer(provider.Issuer, providerTLS)
14191419
if err != nil {
1420-
return nil, fmt.Errorf("error fetching endpoints from issuer: %w", err)
1420+
return nil, err
14211421
}
14221422
tokenEndpoint = discoveredConfig.TokenEndpoint
14231423
authorizationEndpoint = discoveredConfig.AuthorizationEndpoint
@@ -1493,6 +1493,16 @@ type OpenIDConfig struct {
14931493
EndSessionEndpoint *string `json:"end_session_endpoint,omitempty"`
14941494
}
14951495

1496+
func (o *OpenIDConfig) validate() error {
1497+
if o.TokenEndpoint == "" {
1498+
return errors.New("token_endpoint not found in OpenID configuration")
1499+
}
1500+
if o.AuthorizationEndpoint == "" {
1501+
return errors.New("authorization_endpoint not found in OpenID configuration")
1502+
}
1503+
return nil
1504+
}
1505+
14961506
func fetchEndpointsFromIssuer(issuerURL string, providerTLS *ir.TLSUpstreamConfig) (*OpenIDConfig, error) {
14971507
var (
14981508
tlsConfig *tls.Config
@@ -1516,21 +1526,47 @@ func fetchEndpointsFromIssuer(issuerURL string, providerTLS *ir.TLSUpstreamConfi
15161526
var config OpenIDConfig
15171527
if err = backoff.Retry(func() error {
15181528
resp, err := client.Get(fmt.Sprintf("%s/.well-known/openid-configuration", issuerURL))
1529+
// Retry on transport errors
15191530
if err != nil {
15201531
return err
15211532
}
1533+
15221534
defer resp.Body.Close()
1523-
if err = json.NewDecoder(resp.Body).Decode(&config); err != nil {
1524-
return err
1535+
switch {
1536+
// Retry on transient errors
1537+
case retryable(resp.StatusCode):
1538+
return fmt.Errorf("transient error fetching openid-configuration from issuer URL: %s, status code: %d", issuerURL, resp.StatusCode)
1539+
// Do not retry on client errors
1540+
case resp.StatusCode == http.StatusNotFound || resp.StatusCode == http.StatusBadRequest:
1541+
return &backoff.PermanentError{Err: fmt.Errorf("failed fetching openid-configuration from issuer URL: %s, status code: %d", issuerURL, resp.StatusCode)}
1542+
case resp.StatusCode == http.StatusOK:
1543+
// Do not retry if decoding fails
1544+
if err = json.NewDecoder(resp.Body).Decode(&config); err != nil {
1545+
return &backoff.PermanentError{Err: fmt.Errorf("error decoding openid-configuration response: %w", err)}
1546+
}
1547+
default:
1548+
// Do not retry on other status codes
1549+
return &backoff.PermanentError{Err: fmt.Errorf("unexpected status code %d when fetching openid-configuration from issuer URL: %s", resp.StatusCode, issuerURL)}
15251550
}
15261551
return nil
15271552
}, backoff.NewExponentialBackOff(backoff.WithMaxElapsedTime(5*time.Second))); err != nil {
15281553
return nil, err
15291554
}
15301555

1556+
if err = config.validate(); err != nil {
1557+
return nil, fmt.Errorf("invalid openid-configuration from issuer URL %s: %w", issuerURL, err)
1558+
}
1559+
15311560
return &config, nil
15321561
}
15331562

1563+
func retryable(code int) bool {
1564+
return code >= 500 &&
1565+
(code != http.StatusNotImplemented &&
1566+
code != http.StatusHTTPVersionNotSupported &&
1567+
code != http.StatusNetworkAuthenticationRequired)
1568+
}
1569+
15341570
// validateTokenEndpoint validates the token endpoint URL
15351571
func validateTokenEndpoint(tokenEndpoint string) error {
15361572
parsedURL, err := url.Parse(tokenEndpoint)

internal/gatewayapi/testdata/securitypolicy-with-oidc-invalid-issuer.out.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ securityPolicies:
8787
namespace: default
8888
conditions:
8989
- lastTransitionTime: null
90-
message: 'OIDC: error fetching endpoints from issuer: invalid character ''<''
91-
looking for beginning of value.'
90+
message: 'OIDC: failed fetching openid-configuration from issuer URL: https://httpbin.org/,
91+
status code: 404.'
9292
reason: Invalid
9393
status: "False"
9494
type: Accepted

0 commit comments

Comments
 (0)