Skip to content

Commit c069df5

Browse files
committed
Review feedback
1 parent 28bcbe2 commit c069df5

File tree

4 files changed

+17
-14
lines changed

4 files changed

+17
-14
lines changed

internal/controller/nginx/config/policies/waf/generator.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import (
1010
"github.com/nginx/nginx-gateway-fabric/internal/framework/helpers"
1111
)
1212

13+
const appProtectBundleFolder = "/etc/app_protect/bundles"
14+
1315
var tmpl = template.Must(template.New("waf policy").Parse(wafTemplate))
1416

1517
const wafTemplate = `
@@ -63,7 +65,7 @@ func generate(pols []policies.Policy) policies.GenerateResultFiles {
6365
if wp.Spec.PolicySource != nil && wp.Spec.PolicySource.FileLocation != "" {
6466
fileLocation := wp.Spec.PolicySource.FileLocation
6567
bundleName := helpers.ToSafeFileName(fileLocation)
66-
bundlePath := fmt.Sprintf("%s/%s.tgz", "/etc/app_protect/bundles", bundleName)
68+
bundlePath := fmt.Sprintf("%s/%s.tgz", appProtectBundleFolder, bundleName)
6769
fields["BundlePath"] = bundlePath
6870
}
6971

@@ -79,7 +81,7 @@ func generate(pols []policies.Policy) policies.GenerateResultFiles {
7981

8082
if secLog.LogProfileBundle != nil && secLog.LogProfileBundle.FileLocation != "" {
8183
bundleName := helpers.ToSafeFileName(secLog.LogProfileBundle.FileLocation)
82-
bundlePath := fmt.Sprintf("%s/%s.tgz", "/etc/app_protect/bundles", bundleName)
84+
bundlePath := fmt.Sprintf("%s/%s.tgz", appProtectBundleFolder, bundleName)
8385
logEntry["LogProfileBundlePath"] = bundlePath
8486
}
8587

internal/controller/nginx/config/policies/waf/generator_test.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package waf_test
22

33
import (
4+
"fmt"
45
"testing"
56

67
. "github.com/onsi/gomega"
@@ -11,10 +12,16 @@ import (
1112
"github.com/nginx/nginx-gateway-fabric/internal/controller/nginx/config/http"
1213
"github.com/nginx/nginx-gateway-fabric/internal/controller/nginx/config/policies"
1314
"github.com/nginx/nginx-gateway-fabric/internal/controller/nginx/config/policies/waf"
15+
"github.com/nginx/nginx-gateway-fabric/internal/framework/helpers"
1416
)
1517

1618
func TestGenerate(t *testing.T) {
1719
t.Parallel()
20+
21+
apDirBase := "app_protect_policy_file \"/etc/app_protect/bundles"
22+
apFileDirective := fmt.Sprintf("%s/%s", apDirBase, helpers.ToSafeFileName("http://example.com/policy.tgz"))
23+
apSecLogBase := "app_protect_security_log \"/etc/app_protect/bundles"
24+
apSecLogDirective := fmt.Sprintf("%s/%s", apSecLogBase, helpers.ToSafeFileName("http://example.com/custom-log.tgz"))
1825
tests := []struct {
1926
name string
2027
policy policies.Policy
@@ -35,7 +42,7 @@ func TestGenerate(t *testing.T) {
3542
},
3643
expStrings: []string{
3744
"app_protect_enable on;",
38-
"app_protect_policy_file \"/etc/app_protect/bundles/",
45+
apFileDirective,
3946
},
4047
},
4148
{
@@ -64,7 +71,7 @@ func TestGenerate(t *testing.T) {
6471
},
6572
expStrings: []string{
6673
"app_protect_enable on;",
67-
"app_protect_policy_file \"/etc/app_protect/bundles/",
74+
apFileDirective,
6875
"app_protect_security_log_enable on;",
6976
"app_protect_security_log \"log_default\" stderr;",
7077
},
@@ -94,7 +101,7 @@ func TestGenerate(t *testing.T) {
94101
},
95102
expStrings: []string{
96103
"app_protect_security_log_enable on;",
97-
"app_protect_security_log \"/etc/app_protect/bundles/",
104+
apSecLogDirective,
98105
"/var/log/nginx/security.log;",
99106
},
100107
},
@@ -165,7 +172,7 @@ func TestGenerate(t *testing.T) {
165172
},
166173
expStrings: []string{
167174
"app_protect_enable on;",
168-
"app_protect_policy_file \"/etc/app_protect/bundles/",
175+
apFileDirective,
169176
"app_protect_security_log_enable on;",
170177
"app_protect_security_log \"log_all\" stderr;",
171178
"app_protect_security_log \"log_blocked\" /var/log/blocked.log;",

internal/controller/nginx/config/policies/waf/validator.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,7 @@ func (v *Validator) ValidateGlobalSettings(
6767
}
6868

6969
// Conflicts returns false as we don't allow merging for WAFPolicies.
70-
func (v Validator) Conflicts(polA, polB policies.Policy) bool {
71-
_ = helpers.MustCastObject[*ngfAPI.WAFPolicy](polA)
72-
_ = helpers.MustCastObject[*ngfAPI.WAFPolicy](polB)
70+
func (v Validator) Conflicts(_, _ policies.Policy) bool {
7371
return false
7472
}
7573

internal/controller/state/graph/policies.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -493,11 +493,7 @@ func fetchWAFPolicyBundleData(
493493
refPolicyBundles := make(map[WAFBundleKey]*WAFBundleData)
494494

495495
for policyKey, policy := range processedPolicies {
496-
if policyKey.GVK != wafPolicyGVK {
497-
continue
498-
}
499-
500-
if !policy.Valid {
496+
if policyKey.GVK != wafPolicyGVK || !policy.Valid {
501497
continue
502498
}
503499

0 commit comments

Comments
 (0)