Skip to content

Commit bade5eb

Browse files
zhaohuabingjukie
authored andcommitted
fix: oidc authentication endpoint was overwritten by discovered value (envoyproxy#7460)
fix: oid authentication endpoint was overriden by discovered value Signed-off-by: Huabing Zhao <[email protected]> Signed-off-by: Huabing (Robin) Zhao <[email protected]> Signed-off-by: jukie <[email protected]>
1 parent 525edeb commit bade5eb

File tree

3 files changed

+210
-7
lines changed

3 files changed

+210
-7
lines changed

internal/gatewayapi/securitypolicy.go

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1334,15 +1334,42 @@ func (t *Translator) buildOIDCProvider(policy *egv1a1.SecurityPolicy, resources
13341334
// Discover the token and authorization endpoints from the issuer's well-known url if not explicitly specified.
13351335
// EG assumes that the issuer url uses the same protocol and CA as the token endpoint.
13361336
// If we need to support different protocols or CAs, we need to add more fields to the OIDCProvider CRD.
1337-
if provider.TokenEndpoint == nil || provider.AuthorizationEndpoint == nil {
1337+
var (
1338+
userProvidedAuthorizationEndpoint = ptr.Deref(provider.AuthorizationEndpoint, "")
1339+
userProvidedTokenEndpoint = ptr.Deref(provider.TokenEndpoint, "")
1340+
userProvidedEndSessionEndpoint = ptr.Deref(provider.EndSessionEndpoint, "")
1341+
)
1342+
1343+
// Authorization endpoint and token endpoint are required fields.
1344+
// If either of them is not provided, we need to fetch them from the issuer's well-known url.
1345+
if userProvidedAuthorizationEndpoint == "" || userProvidedTokenEndpoint == "" {
1346+
// Fetch the endpoints from the issuer's well-known url.
13381347
discoveredConfig, err := fetchEndpointsFromIssuer(provider.Issuer, providerTLS)
13391348
if err != nil {
13401349
return nil, fmt.Errorf("error fetching endpoints from issuer: %w", err)
13411350
}
1342-
tokenEndpoint = discoveredConfig.TokenEndpoint
1343-
authorizationEndpoint = discoveredConfig.AuthorizationEndpoint
1344-
// endSessionEndpoint is optional, and we prioritize using the one provided in the well-known configuration.
1345-
if discoveredConfig.EndSessionEndpoint != nil && *discoveredConfig.EndSessionEndpoint != "" {
1351+
1352+
// Prioritize using the explicitly provided authorization endpoints if available.
1353+
// This allows users to add extra parameters to the authorization endpoint if needed.
1354+
if userProvidedAuthorizationEndpoint != "" {
1355+
authorizationEndpoint = userProvidedAuthorizationEndpoint
1356+
} else {
1357+
authorizationEndpoint = discoveredConfig.AuthorizationEndpoint
1358+
}
1359+
1360+
// Prioritize using the explicitly provided token endpoints if available.
1361+
// This may not be necessary, but we do it for consistency with authorization endpoint.
1362+
if userProvidedTokenEndpoint != "" {
1363+
tokenEndpoint = userProvidedTokenEndpoint
1364+
} else {
1365+
tokenEndpoint = discoveredConfig.TokenEndpoint
1366+
}
1367+
1368+
// Prioritize using the explicitly provided end session endpoints if available.
1369+
// This may not be necessary, but we do it for consistency with other endpoints.
1370+
if userProvidedEndSessionEndpoint != "" {
1371+
endSessionEndpoint = &userProvidedEndSessionEndpoint
1372+
} else {
13461373
endSessionEndpoint = discoveredConfig.EndSessionEndpoint
13471374
}
13481375
} else {

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

Lines changed: 49 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
@@ -128,3 +154,26 @@ securityPolicies:
128154
refreshToken: true
129155
defaultRefreshTokenTTL: 48h
130156
cookieDomain: "example.com"
157+
- apiVersion: gateway.envoyproxy.io/v1alpha1
158+
kind: SecurityPolicy
159+
metadata:
160+
namespace: default
161+
name: policy-for-http-route-3
162+
spec:
163+
targetRef:
164+
group: gateway.networking.k8s.io
165+
kind: HTTPRoute
166+
name: httproute-3
167+
oidc:
168+
provider:
169+
issuer: "https://accounts.google.com"
170+
authorizationEndpoint: "https://accounts.google.com/o/oauth2/v2/auth?foo=bar" # custom auth endpoint with query params, should be used as is
171+
clientID: "client1.apps.googleusercontent.com"
172+
clientSecret:
173+
name: "client3-secret"
174+
redirectURL: "https://www.example.com/bar/oauth2/callback"
175+
logoutPath: "/bar/logout"
176+
forwardAccessToken: true
177+
defaultTokenTTL: 30m
178+
refreshToken: true
179+
defaultRefreshTokenTTL: 24h

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

Lines changed: 129 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ gateways:
1616
protocol: HTTP
1717
status:
1818
listeners:
19-
- attachedRoutes: 2
19+
- attachedRoutes: 3
2020
conditions:
2121
- lastTransitionTime: null
2222
message: Sending translated listener configuration to the data plane
@@ -116,6 +116,44 @@ httpRoutes:
116116
name: gateway-1
117117
namespace: envoy-gateway
118118
sectionName: http
119+
- apiVersion: gateway.networking.k8s.io/v1
120+
kind: HTTPRoute
121+
metadata:
122+
creationTimestamp: null
123+
name: httproute-3
124+
namespace: default
125+
spec:
126+
hostnames:
127+
- www.example.com
128+
parentRefs:
129+
- name: gateway-1
130+
namespace: envoy-gateway
131+
sectionName: http
132+
rules:
133+
- backendRefs:
134+
- name: service-1
135+
port: 8080
136+
matches:
137+
- path:
138+
value: /baz
139+
status:
140+
parents:
141+
- conditions:
142+
- lastTransitionTime: null
143+
message: Route is accepted
144+
reason: Accepted
145+
status: "True"
146+
type: Accepted
147+
- lastTransitionTime: null
148+
message: Resolved all the Object references for the Route
149+
reason: ResolvedRefs
150+
status: "True"
151+
type: ResolvedRefs
152+
controllerName: gateway.envoyproxy.io/gatewayclass-controller
153+
parentRef:
154+
name: gateway-1
155+
namespace: envoy-gateway
156+
sectionName: http
119157
infraIR:
120158
envoy-gateway/gateway-1:
121159
proxy:
@@ -190,6 +228,47 @@ securityPolicies:
190228
status: "True"
191229
type: Accepted
192230
controllerName: gateway.envoyproxy.io/gatewayclass-controller
231+
- apiVersion: gateway.envoyproxy.io/v1alpha1
232+
kind: SecurityPolicy
233+
metadata:
234+
creationTimestamp: null
235+
name: policy-for-http-route-3
236+
namespace: default
237+
spec:
238+
oidc:
239+
clientID: client1.apps.googleusercontent.com
240+
clientSecret:
241+
group: null
242+
kind: null
243+
name: client3-secret
244+
defaultRefreshTokenTTL: 24h
245+
defaultTokenTTL: 30m
246+
forwardAccessToken: true
247+
logoutPath: /bar/logout
248+
provider:
249+
authorizationEndpoint: https://accounts.google.com/o/oauth2/v2/auth?foo=bar
250+
issuer: https://accounts.google.com
251+
redirectURL: https://www.example.com/bar/oauth2/callback
252+
refreshToken: true
253+
targetRef:
254+
group: gateway.networking.k8s.io
255+
kind: HTTPRoute
256+
name: httproute-3
257+
status:
258+
ancestors:
259+
- ancestorRef:
260+
group: gateway.networking.k8s.io
261+
kind: Gateway
262+
name: gateway-1
263+
namespace: envoy-gateway
264+
sectionName: http
265+
conditions:
266+
- lastTransitionTime: null
267+
message: Policy has been accepted.
268+
reason: Accepted
269+
status: "True"
270+
type: Accepted
271+
controllerName: gateway.envoyproxy.io/gatewayclass-controller
193272
- apiVersion: gateway.envoyproxy.io/v1alpha1
194273
kind: SecurityPolicy
195274
metadata:
@@ -231,7 +310,7 @@ securityPolicies:
231310
type: Accepted
232311
- lastTransitionTime: null
233312
message: 'This policy is being overridden by other securityPolicies for these
234-
routes: [default/httproute-1]'
313+
routes: [default/httproute-1 default/httproute-3]'
235314
reason: Overridden
236315
status: "True"
237316
type: Overridden
@@ -374,6 +453,54 @@ xdsIR:
374453
refreshToken: true
375454
scopes:
376455
- openid
456+
- destination:
457+
metadata:
458+
kind: HTTPRoute
459+
name: httproute-3
460+
namespace: default
461+
name: httproute/default/httproute-3/rule/0
462+
settings:
463+
- addressType: IP
464+
endpoints:
465+
- host: 7.7.7.7
466+
port: 8080
467+
metadata:
468+
name: service-1
469+
namespace: default
470+
sectionName: "8080"
471+
name: httproute/default/httproute-3/rule/0/backend/0
472+
protocol: HTTP
473+
weight: 1
474+
hostname: www.example.com
475+
isHTTP2: false
476+
metadata:
477+
kind: HTTPRoute
478+
name: httproute-3
479+
namespace: default
480+
name: httproute/default/httproute-3/rule/0/match/0/www_example_com
481+
pathMatch:
482+
distinct: false
483+
name: ""
484+
prefix: /baz
485+
security:
486+
oidc:
487+
clientID: client1.apps.googleusercontent.com
488+
clientSecret: '[redacted]'
489+
cookieSuffix: 811c9dc5
490+
defaultRefreshTokenTTL: 24h0m0s
491+
defaultTokenTTL: 30m0s
492+
forwardAccessToken: true
493+
hmacSecret: '[redacted]'
494+
logoutPath: /bar/logout
495+
name: securitypolicy/default/policy-for-http-route-3
496+
provider:
497+
authorizationEndpoint: https://accounts.google.com/o/oauth2/v2/auth?foo=bar
498+
tokenEndpoint: https://oauth2.googleapis.com/token
499+
redirectPath: /bar/oauth2/callback
500+
redirectURL: https://www.example.com/bar/oauth2/callback
501+
refreshToken: true
502+
scopes:
503+
- openid
377504
readyListener:
378505
address: 0.0.0.0
379506
ipFamily: IPv4

0 commit comments

Comments
 (0)