Skip to content

Commit

Permalink
Change the Merge behavior to Replace for SecurityPolicy (envoyproxy#2885
Browse files Browse the repository at this point in the history
)

* Change the Merge behavior to Replace for SecurityPolicy

Signed-off-by: huabing zhao <[email protected]>

* add another http route

Signed-off-by: huabing zhao <[email protected]>

---------

Signed-off-by: huabing zhao <[email protected]>
  • Loading branch information
zhaohuabing authored and Xunzhuo committed Mar 13, 2024
1 parent 4c7c008 commit 01bea8b
Show file tree
Hide file tree
Showing 3 changed files with 451 additions and 1 deletion.
12 changes: 11 additions & 1 deletion internal/gatewayapi/securitypolicy.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,8 +407,18 @@ func (t *Translator) translateSecurityPolicyForGateway(
if t.MergeGateways && gatewayName != policyTarget {
continue
}
// A Policy targeting the most specific scope(xRoute) wins over a policy
// targeting a lesser specific scope(Gateway).
for _, r := range http.Routes {
// Apply if not already set
// If any of the features are already set, it means that a more specific
// policy(targeting xRoute) has already set it, so we skip it.
if r.CORS != nil ||
r.JWT != nil ||
r.OIDC != nil ||
r.BasicAuth != nil ||
r.ExtAuth != nil {
continue
}
if r.CORS == nil {
r.CORS = cors
}
Expand Down
117 changes: 117 additions & 0 deletions internal/gatewayapi/testdata/securitypolicy-override-replace.in.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
gateways:
- apiVersion: gateway.networking.k8s.io/v1
kind: Gateway
metadata:
namespace: envoy-gateway
name: gateway-1
spec:
gatewayClassName: envoy-gateway-class
listeners:
- name: http
protocol: HTTP
port: 80
allowedRoutes:
namespaces:
from: All
httpRoutes:
- apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
namespace: default
name: httproute-1
spec:
hostnames:
- gateway.envoyproxy.io
parentRefs:
- namespace: envoy-gateway
name: gateway-1
sectionName: http
rules:
- matches:
- path:
value: "/foo"
backendRefs:
- name: service-1
port: 8080
- apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
namespace: default
name: httproute-2
spec:
hostnames:
- gateway.envoyproxy.io
parentRefs:
- namespace: envoy-gateway
name: gateway-1
sectionName: http
rules:
- matches:
- path:
value: "/bar"
backendRefs:
- name: service-1
port: 8080
securityPolicies:
- apiVersion: gateway.envoyproxy.io/v1alpha1
kind: SecurityPolicy
metadata:
namespace: envoy-gateway
name: policy-for-gateway-1
spec:
targetRef:
group: gateway.networking.k8s.io
kind: Gateway
name: gateway-1
namespace: envoy-gateway
cors:
allowOrigins:
- "http://*.example.com"
- "http://foo.bar.com"
- "https://*"
allowMethods:
- GET
- POST
allowHeaders:
- "x-header-1"
- "x-header-2"
exposeHeaders:
- "x-header-3"
- "x-header-4"
maxAge: 1000s
jwt:
providers:
- name: example1
issuer: https://one.example.com
audiences:
- one.foo.com
remoteJWKS:
uri: https://one.example.com/jwt/public-key/jwks.json
claimToHeaders:
- header: one-route-example-key
claim: claim1
- apiVersion: gateway.envoyproxy.io/v1alpha1
kind: SecurityPolicy
metadata:
namespace: default
name: policy-for-route-1
spec:
targetRef:
group: gateway.networking.k8s.io
kind: HTTPRoute
name: httproute-1
namespace: default
cors:
allowOrigins:
- "https://*.test.com:8080"
- "https://www.test.org:8080"
allowMethods:
- GET
- POST
allowHeaders:
- "x-header-5"
- "x-header-6"
exposeHeaders:
- "x-header-7"
- "x-header-8"
maxAge: 2000s
Loading

0 comments on commit 01bea8b

Please sign in to comment.