Skip to content

Commit

Permalink
use hash as suffix
Browse files Browse the repository at this point in the history
Signed-off-by: huabing zhao <zhaohuabing@gmail.com>
  • Loading branch information
zhaohuabing committed Feb 21, 2024
1 parent eeea5c7 commit b295fb2
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 15 deletions.
14 changes: 13 additions & 1 deletion internal/gatewayapi/securitypolicy.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"encoding/json"
"errors"
"fmt"
"hash/fnv"
"net/http"
"net/netip"
"net/url"
Expand Down Expand Up @@ -494,6 +495,17 @@ func (t *Translator) buildOIDC(
logoutPath = *oidc.LogoutPath
}

nsName := types.NamespacedName{
Namespace: policy.GetNamespace(),
Name: policy.GetName(),
}
h := fnv.New32a()
_, err = h.Write([]byte(nsName.String()))
if err != nil {
return nil, fmt.Errorf("error generating oauth cookie suffix: %w", err)
}

Check warning on line 506 in internal/gatewayapi/securitypolicy.go

View check run for this annotation

Codecov / codecov/patch

internal/gatewayapi/securitypolicy.go#L505-L506

Added lines #L505 - L506 were not covered by tests
suffix := strconv.Itoa(int(h.Sum32()))

return &ir.OIDC{
Provider: *provider,
ClientID: oidc.ClientID,
Expand All @@ -502,7 +514,7 @@ func (t *Translator) buildOIDC(
RedirectURL: redirectURL,
RedirectPath: redirectPath,
LogoutPath: logoutPath,
CookieSuffix: fmt.Sprintf("%s-%s", policy.Namespace, policy.Name),
CookieSuffix: suffix,
}, nil
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ xdsIR:
oidc:
clientID: client2.oauth.foo.com
clientSecret: Y2xpZW50MTpzZWNyZXQK
cookieSuffix: default-policy-for-http-route
cookieSuffix: 1667669650
logoutPath: /foo/logout
provider:
authorizationEndpoint: https://oauth.foo.com/oauth2/v2/auth
Expand Down Expand Up @@ -262,7 +262,7 @@ xdsIR:
oidc:
clientID: client1.apps.googleusercontent.com
clientSecret: Y2xpZW50MTpzZWNyZXQK
cookieSuffix: envoy-gateway-policy-for-gateway-discover-endpoints
cookieSuffix: 2003913538
logoutPath: /bar/logout
provider:
authorizationEndpoint: https://accounts.google.com/o/oauth2/v2/auth
Expand Down
4 changes: 2 additions & 2 deletions internal/xds/translator/testdata/in/xds-ir/oidc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ http:
redirectURL: "https://www.example.com/foo/oauth2/callback"
redirectPath: "/foo/oauth2/callback"
logoutPath: "/foo/logout"
cookieSuffix: "default-security-policy-foo"
cookieSuffix: "1667669650"
- name: "second-route"
hostname: "*"
pathMatch:
Expand All @@ -55,4 +55,4 @@ http:
redirectURL: "https://www.example.com/bar/oauth2/callback"
redirectPath: "/bar/oauth2/callback"
logoutPath: "/bar/logout"
cookieSuffix: "default-security-policy-bar"
cookieSuffix: "2003913538"
20 changes: 10 additions & 10 deletions internal/xds/translator/testdata/out/xds-ir/oidc.listeners.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@
credentials:
clientId: client.oauth.foo.com
cookieNames:
bearerToken: BearerToken-default-security-policy-foo
idToken: IdToken-default-security-policy-foo
oauthExpires: OauthExpires-default-security-policy-foo
oauthHmac: OauthHMAC-default-security-policy-foo
refreshToken: RefreshToken-default-security-policy-foo
bearerToken: BearerToken-1667669650
idToken: IdToken-1667669650
oauthExpires: OauthExpires-1667669650
oauthHmac: OauthHMAC-1667669650
refreshToken: RefreshToken-1667669650
hmacSecret:
name: first-route/oauth2/hmac_secret
sdsConfig:
Expand Down Expand Up @@ -67,11 +67,11 @@
credentials:
clientId: client.oauth.bar.com
cookieNames:
bearerToken: BearerToken-default-security-policy-bar
idToken: IdToken-default-security-policy-bar
oauthExpires: OauthExpires-default-security-policy-bar
oauthHmac: OauthHMAC-default-security-policy-bar
refreshToken: RefreshToken-default-security-policy-bar
bearerToken: BearerToken-2003913538
idToken: IdToken-2003913538
oauthExpires: OauthExpires-2003913538
oauthHmac: OauthHMAC-2003913538
refreshToken: RefreshToken-2003913538
hmacSecret:
name: second-route/oauth2/hmac_secret
sdsConfig:
Expand Down

0 comments on commit b295fb2

Please sign in to comment.