Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion internal/controller/trafficprotectionpolicy_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"context"
"encoding/json"
"fmt"
"slices"
"sort"
"strings"

Expand Down Expand Up @@ -1229,7 +1230,7 @@ func (r *TrafficProtectionPolicyReconciler) getCorazaDirectivesForTrafficProtect
secRuleEngine = "Off"
}

directives := r.Config.Gateway.Coraza.RouteBaseDirectives
directives := slices.Clone(r.Config.Gateway.Coraza.RouteBaseDirectives)

directives = append(directives, fmt.Sprintf("SecRuleEngine %s", secRuleEngine))

Expand Down
30 changes: 30 additions & 0 deletions internal/controller/trafficprotectionpolicy_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -992,6 +992,36 @@ func TestGetCorazaDirectivesForTrafficProtectionPolicy(t *testing.T) {
}
}

func TestGetCorazaDirectivesDoesNotAliasRouteBaseDirectives(t *testing.T) {
base := make([]string, 2, 8)
base[0] = "Include @crs-setup-conf"
base[1] = "Include @recommended-conf"

operatorConfig := config.NetworkServicesOperator{
Gateway: config.GatewayConfig{
Coraza: config.CorazaConfig{RouteBaseDirectives: base},
},
}
reconciler := &TrafficProtectionPolicyReconciler{Config: operatorConfig}

enforce := reconciler.getCorazaDirectivesForTrafficProtectionPolicy(&policyContext{ptr.To(
newTrafficProtectionPolicy("default", "tpp-enforce", func(tpp *networkingv1alpha.TrafficProtectionPolicy) {
tpp.Spec.Mode = networkingv1alpha.TrafficProtectionPolicyEnforce
}),
)})
assert.Contains(t, enforce, "SecRuleEngine On")

reconciler.getCorazaDirectivesForTrafficProtectionPolicy(&policyContext{ptr.To(
newTrafficProtectionPolicy("default", "tpp-observe"),
)})

assert.Contains(t, enforce, "SecRuleEngine On",
"second policy leaked into the first via a shared RouteBaseDirectives backing array")
assert.EqualValues(t, []string{"Include @crs-setup-conf", "Include @recommended-conf"},
reconciler.Config.Gateway.Coraza.RouteBaseDirectives,
"shared base directives were mutated")
}

// nolint:unparam
func newTrafficProtectionPolicy(
namespace,
Expand Down
Loading