Skip to content

Commit 4e67e7d

Browse files
committed
fix: oid authentication endpoint was overriden by discovered value
Signed-off-by: Huabing Zhao <[email protected]>
1 parent 8a34627 commit 4e67e7d

File tree

4 files changed

+212
-7
lines changed

4 files changed

+212
-7
lines changed

internal/gatewayapi/securitypolicy.go

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1423,15 +1423,42 @@ func (t *Translator) buildOIDCProvider(policy *egv1a1.SecurityPolicy, resources
14231423
// Discover the token and authorization endpoints from the issuer's well-known url if not explicitly specified.
14241424
// EG assumes that the issuer url uses the same protocol and CA as the token endpoint.
14251425
// If we need to support different protocols or CAs, we need to add more fields to the OIDCProvider CRD.
1426-
if provider.TokenEndpoint == nil || provider.AuthorizationEndpoint == nil {
1426+
var (
1427+
userProvidedAuthorizationEndpoint = ptr.Deref(provider.AuthorizationEndpoint, "")
1428+
userProvidedTokenEndpoint = ptr.Deref(provider.TokenEndpoint, "")
1429+
userProvidedEndSessionEndpoint = ptr.Deref(provider.EndSessionEndpoint, "")
1430+
)
1431+
1432+
// Authorization endpoint and token endpoint are required fields.
1433+
// If either of them is not provided, we need to fetch them from the issuer's well-known url.
1434+
if userProvidedAuthorizationEndpoint == "" || userProvidedTokenEndpoint == "" {
1435+
// Fetch the endpoints from the issuer's well-known url.
14271436
discoveredConfig, err := t.fetchEndpointsFromIssuer(provider.Issuer, providerTLS)
14281437
if err != nil {
14291438
return nil, err
14301439
}
1431-
tokenEndpoint = discoveredConfig.TokenEndpoint
1432-
authorizationEndpoint = discoveredConfig.AuthorizationEndpoint
1433-
// endSessionEndpoint is optional, and we prioritize using the one provided in the well-known configuration.
1434-
if discoveredConfig.EndSessionEndpoint != nil && *discoveredConfig.EndSessionEndpoint != "" {
1440+
1441+
// Prioritize using the explicitly provided authorization endpoints if available.
1442+
// This allows users to add extra parameters to the authorization endpoint if needed.
1443+
if userProvidedAuthorizationEndpoint != "" {
1444+
authorizationEndpoint = userProvidedAuthorizationEndpoint
1445+
} else {
1446+
authorizationEndpoint = discoveredConfig.AuthorizationEndpoint
1447+
}
1448+
1449+
// Prioritize using the explicitly provided token endpoints if available.
1450+
// This may not be necessary, but we do it for consistency with authorization endpoint.
1451+
if userProvidedTokenEndpoint != "" {
1452+
tokenEndpoint = userProvidedTokenEndpoint
1453+
} else {
1454+
tokenEndpoint = discoveredConfig.TokenEndpoint
1455+
}
1456+
1457+
// Prioritize using the explicitly provided end session endpoints if available.
1458+
// This may not be necessary, but we do it for consistency with other endpoints.
1459+
if userProvidedEndSessionEndpoint != "" {
1460+
endSessionEndpoint = &userProvidedEndSessionEndpoint
1461+
} else {
14351462
endSessionEndpoint = discoveredConfig.EndSessionEndpoint
14361463
}
14371464
} else {

internal/gatewayapi/testdata/securitypolicy-with-oidc.in.yaml

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ secrets:
1414
data:
1515
client-secret: Y2xpZW50MTpzZWNyZXQK
1616
client-id: Y2xpZW50Mi5vYXV0aC5mb28uY29t
17+
- apiVersion: v1
18+
kind: Secret
19+
metadata:
20+
namespace: default
21+
name: client3-secret
22+
data:
23+
client-secret: Y2xpZW50MTpzZWNyZXQK
1724
- apiVersion: v1
1825
kind: Secret
1926
metadata:
@@ -75,6 +82,25 @@ httpRoutes:
7582
backendRefs:
7683
- name: service-1
7784
port: 8080
85+
- apiVersion: gateway.networking.k8s.io/v1
86+
kind: HTTPRoute
87+
metadata:
88+
namespace: default
89+
name: httproute-3
90+
spec:
91+
hostnames:
92+
- www.example.com
93+
parentRefs:
94+
- namespace: envoy-gateway
95+
name: gateway-1
96+
sectionName: http
97+
rules:
98+
- matches:
99+
- path:
100+
value: "/baz"
101+
backendRefs:
102+
- name: service-1
103+
port: 8080
78104
securityPolicies:
79105
- apiVersion: gateway.envoyproxy.io/v1alpha1
80106
kind: SecurityPolicy
@@ -130,3 +156,27 @@ securityPolicies:
130156
defaultRefreshTokenTTL: 48h
131157
cookieDomain: "example.com"
132158
disableTokenEncryption: true
159+
- apiVersion: gateway.envoyproxy.io/v1alpha1
160+
kind: SecurityPolicy
161+
metadata:
162+
namespace: default
163+
name: policy-for-http-route-3
164+
spec:
165+
targetRef:
166+
group: gateway.networking.k8s.io
167+
kind: HTTPRoute
168+
name: httproute-3
169+
oidc:
170+
provider:
171+
issuer: "https://accounts.google.com"
172+
authorizationEndpoint: "https://accounts.google.com/o/oauth2/v2/auth?foo=bar" # custom auth endpoint with query params, should be used as is
173+
clientID: "client1.apps.googleusercontent.com"
174+
clientSecret:
175+
name: "client3-secret"
176+
redirectURL: "https://www.example.com/bar/oauth2/callback"
177+
logoutPath: "/bar/logout"
178+
forwardAccessToken: true
179+
defaultTokenTTL: 30m
180+
refreshToken: true
181+
defaultRefreshTokenTTL: 24h
182+
csrfTokenTTL: 35m

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

Lines changed: 129 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ gateways:
1515
protocol: HTTP
1616
status:
1717
listeners:
18-
- attachedRoutes: 2
18+
- attachedRoutes: 3
1919
conditions:
2020
- lastTransitionTime: null
2121
message: Sending translated listener configuration to the data plane
@@ -113,6 +113,43 @@ httpRoutes:
113113
name: gateway-1
114114
namespace: envoy-gateway
115115
sectionName: http
116+
- apiVersion: gateway.networking.k8s.io/v1
117+
kind: HTTPRoute
118+
metadata:
119+
name: httproute-3
120+
namespace: default
121+
spec:
122+
hostnames:
123+
- www.example.com
124+
parentRefs:
125+
- name: gateway-1
126+
namespace: envoy-gateway
127+
sectionName: http
128+
rules:
129+
- backendRefs:
130+
- name: service-1
131+
port: 8080
132+
matches:
133+
- path:
134+
value: /baz
135+
status:
136+
parents:
137+
- conditions:
138+
- lastTransitionTime: null
139+
message: Route is accepted
140+
reason: Accepted
141+
status: "True"
142+
type: Accepted
143+
- lastTransitionTime: null
144+
message: Resolved all the Object references for the Route
145+
reason: ResolvedRefs
146+
status: "True"
147+
type: ResolvedRefs
148+
controllerName: gateway.envoyproxy.io/gatewayclass-controller
149+
parentRef:
150+
name: gateway-1
151+
namespace: envoy-gateway
152+
sectionName: http
116153
infraIR:
117154
envoy-gateway/gateway-1:
118155
proxy:
@@ -187,6 +224,47 @@ securityPolicies:
187224
status: "True"
188225
type: Accepted
189226
controllerName: gateway.envoyproxy.io/gatewayclass-controller
227+
- apiVersion: gateway.envoyproxy.io/v1alpha1
228+
kind: SecurityPolicy
229+
metadata:
230+
name: policy-for-http-route-3
231+
namespace: default
232+
spec:
233+
oidc:
234+
clientID: client1.apps.googleusercontent.com
235+
clientSecret:
236+
group: null
237+
kind: null
238+
name: client3-secret
239+
csrfTokenTTL: 35m
240+
defaultRefreshTokenTTL: 24h
241+
defaultTokenTTL: 30m
242+
forwardAccessToken: true
243+
logoutPath: /bar/logout
244+
provider:
245+
authorizationEndpoint: https://accounts.google.com/o/oauth2/v2/auth?foo=bar
246+
issuer: https://accounts.google.com
247+
redirectURL: https://www.example.com/bar/oauth2/callback
248+
refreshToken: true
249+
targetRef:
250+
group: gateway.networking.k8s.io
251+
kind: HTTPRoute
252+
name: httproute-3
253+
status:
254+
ancestors:
255+
- ancestorRef:
256+
group: gateway.networking.k8s.io
257+
kind: Gateway
258+
name: gateway-1
259+
namespace: envoy-gateway
260+
sectionName: http
261+
conditions:
262+
- lastTransitionTime: null
263+
message: Policy has been accepted.
264+
reason: Accepted
265+
status: "True"
266+
type: Accepted
267+
controllerName: gateway.envoyproxy.io/gatewayclass-controller
190268
- apiVersion: gateway.envoyproxy.io/v1alpha1
191269
kind: SecurityPolicy
192270
metadata:
@@ -228,7 +306,7 @@ securityPolicies:
228306
type: Accepted
229307
- lastTransitionTime: null
230308
message: 'This policy is being overridden by other securityPolicies for these
231-
routes: [default/httproute-1]'
309+
routes: [default/httproute-1 default/httproute-3]'
232310
reason: Overridden
233311
status: "True"
234312
type: Overridden
@@ -377,6 +455,55 @@ xdsIR:
377455
refreshToken: true
378456
scopes:
379457
- openid
458+
- destination:
459+
metadata:
460+
kind: HTTPRoute
461+
name: httproute-3
462+
namespace: default
463+
name: httproute/default/httproute-3/rule/0
464+
settings:
465+
- addressType: IP
466+
endpoints:
467+
- host: 7.7.7.7
468+
port: 8080
469+
metadata:
470+
name: service-1
471+
namespace: default
472+
sectionName: "8080"
473+
name: httproute/default/httproute-3/rule/0/backend/0
474+
protocol: HTTP
475+
weight: 1
476+
hostname: www.example.com
477+
isHTTP2: false
478+
metadata:
479+
kind: HTTPRoute
480+
name: httproute-3
481+
namespace: default
482+
name: httproute/default/httproute-3/rule/0/match/0/www_example_com
483+
pathMatch:
484+
distinct: false
485+
name: ""
486+
prefix: /baz
487+
security:
488+
oidc:
489+
clientID: client1.apps.googleusercontent.com
490+
clientSecret: '[redacted]'
491+
cookieSuffix: 811c9dc5
492+
csrfTokenTTL: 35m0s
493+
defaultRefreshTokenTTL: 24h0m0s
494+
defaultTokenTTL: 30m0s
495+
forwardAccessToken: true
496+
hmacSecret: '[redacted]'
497+
logoutPath: /bar/logout
498+
name: securitypolicy/default/policy-for-http-route-3
499+
provider:
500+
authorizationEndpoint: https://accounts.google.com/o/oauth2/v2/auth?foo=bar
501+
tokenEndpoint: https://oauth2.googleapis.com/token
502+
redirectPath: /bar/oauth2/callback
503+
redirectURL: https://www.example.com/bar/oauth2/callback
504+
refreshToken: true
505+
scopes:
506+
- openid
380507
readyListener:
381508
address: 0.0.0.0
382509
ipFamily: IPv4

release-notes/current.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ bug fixes: |
1616
- Fixed issue where reloading invalid envoy gateway configuration.
1717
- Fixed missing JWT provider configuration when JWT authentication is configured on multiple HTTP listeners sharing the same port.
1818
- Fixed issue where header modifier doesn't permit multiple values with commas.
19+
- Fixed configured OIDC authorization endpoint being overridden by discovered endpoints from issuer's well-known URL.
1920

2021
# Enhancements that improve performance.
2122
performance improvements: |

0 commit comments

Comments
 (0)