Skip to content

Commit

Permalink
fix: Policies should apply only to gateways they were attached to whe…
Browse files Browse the repository at this point in the history
…n mergeGateways is true (#2671)

* When policies are attached to a gateway and mergeGateways is set to
true, don't apply policies to routes from other gateways.

Signed-off-by: Lior Okman <[email protected]>

* Updated the tests

Signed-off-by: Lior Okman <[email protected]>

* Update test to include all of the policy types

Signed-off-by: Lior Okman <[email protected]>

* Calculate the correct gatewayname as a prefix of the listener's name

Signed-off-by: Lior Okman <[email protected]>

* Make the linter happy

Signed-off-by: Lior Okman <[email protected]>

---------

Signed-off-by: Lior Okman <[email protected]>
  • Loading branch information
liorokman authored Feb 26, 2024
1 parent 338515a commit e1b2a71
Show file tree
Hide file tree
Showing 4 changed files with 507 additions and 1 deletion.
8 changes: 8 additions & 0 deletions internal/gatewayapi/backendtrafficpolicy.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,15 @@ func (t *Translator) translateBackendTrafficPolicyForGateway(policy *egv1a1.Back
// Should exist since we've validated this
ir := xdsIR[irKey]

policyTarget := irStringKey(
string(ptr.Deref(policy.Spec.TargetRef.Namespace, gwv1a2.Namespace(policy.Namespace))),
string(policy.Spec.TargetRef.Name),
)
for _, http := range ir.HTTP {
gatewayName := http.Name[0:strings.LastIndex(http.Name, "/")]
if t.MergeGateways && gatewayName != policyTarget {
continue
}
for _, r := range http.Routes {
// Apply if not already set
if r.RateLimit == nil {
Expand Down
11 changes: 10 additions & 1 deletion internal/gatewayapi/securitypolicy.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,8 @@ func (t *Translator) translateSecurityPolicyForGateway(
}
}

// Apply IR to all the routes within the specific Gateway
// Apply IR to all the routes within the specific Gateway that originated
// from the gateway to which this security policy was attached.
// If the feature is already set, then skip it, since it must have be
// set by a policy attaching to the route
//
Expand All @@ -370,7 +371,15 @@ func (t *Translator) translateSecurityPolicyForGateway(
// Should exist since we've validated this
ir := xdsIR[irKey]

policyTarget := irStringKey(
string(ptr.Deref(policy.Spec.TargetRef.Namespace, gwv1a2.Namespace(policy.Namespace))),
string(policy.Spec.TargetRef.Name),
)
for _, http := range ir.HTTP {
gatewayName := http.Name[0:strings.LastIndex(http.Name, "/")]
if t.MergeGateways && gatewayName != policyTarget {
continue
}
for _, r := range http.Routes {
// Apply if not already set
if r.CORS == nil {
Expand Down
132 changes: 132 additions & 0 deletions internal/gatewayapi/testdata/merge-with-isolated-policies.in.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
envoyproxy:
apiVersion: gateway.envoyproxy.io/v1alpha1
kind: EnvoyProxy
metadata:
namespace: envoy-gateway-system
name: test
spec:
mergeGateways: true
gateways:
- apiVersion: gateway.networking.k8s.io/v1beta1
kind: Gateway
metadata:
name: gateway-1
namespace: default
spec:
gatewayClassName: envoy-gateway-class
listeners:
- name: http
port: 80
protocol: HTTP
allowedRoutes:
namespaces:
from: Same
- apiVersion: gateway.networking.k8s.io/v1beta1
kind: Gateway
metadata:
name: gateway-2
namespace: default
spec:
gatewayClassName: envoy-gateway-class
listeners:
- name: http-2
port: 8888
protocol: HTTP
allowedRoutes:
namespaces:
from: Same
httpRoutes:
- apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
namespace: default
name: httproute-1
spec:
hostnames:
- gateway.envoyproxy.io
parentRefs:
- namespace: default
name: gateway-1
sectionName: http
rules:
- matches:
- path:
value: "/"
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: default
name: gateway-2
sectionName: http-2
rules:
- matches:
- path:
value: "/"
backendRefs:
- name: service-2
port: 8080
securityPolicies:
- apiVersion: gateway.envoyproxy.io/v1alpha1
kind: SecurityPolicy
metadata:
namespace: default
name: policy-for-route-2
spec:
targetRef:
group: gateway.networking.k8s.io
kind: Gateway
name: gateway-1
namespace: default
cors:
allowOrigins:
- "*"
allowMethods:
- GET
- POST
allowHeaders:
- "x-header-5"
- "x-header-6"
exposeHeaders:
- "x-header-7"
- "x-header-8"
maxAge: 2000s
clientTrafficPolicies:
- apiVersion: gateway.envoyproxy.io/v1alpha1
kind: ClientTrafficPolicy
metadata:
namespace: default
name: target-gateway
spec:
targetRef:
group: gateway.networking.k8s.io
kind: Gateway
name: gateway-2
namespace: default
timeout:
http:
requestReceivedTimeout: "5s"
backendTrafficPolicies:
- apiVersion: gateway.envoyproxy.io/v1alpha1
kind: BackendTrafficPolicy
metadata:
namespace: default
name: policy-for-gateway
spec:
targetRef:
group: gateway.networking.k8s.io
kind: Gateway
name: gateway-1
namespace: default
tcpKeepalive:
probes: 3
idleTime: 20m
interval: 60s
Loading

0 comments on commit e1b2a71

Please sign in to comment.