Skip to content

Implement WAFPolicy controller #3532

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: feat/nap-waf
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 4 additions & 2 deletions internal/controller/nginx/config/policies/waf/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
"github.com/nginx/nginx-gateway-fabric/internal/framework/helpers"
)

const appProtectBundleFolder = "/etc/app_protect/bundles"

var tmpl = template.Must(template.New("waf policy").Parse(wafTemplate))

const wafTemplate = `
Expand Down Expand Up @@ -63,7 +65,7 @@
if wp.Spec.PolicySource != nil && wp.Spec.PolicySource.FileLocation != "" {
fileLocation := wp.Spec.PolicySource.FileLocation
bundleName := helpers.ToSafeFileName(fileLocation)
bundlePath := fmt.Sprintf("%s/%s.tgz", "/etc/app_protect/bundles", bundleName)
bundlePath := fmt.Sprintf("%s/%s.tgz", appProtectBundleFolder, bundleName)
fields["BundlePath"] = bundlePath
}

Expand All @@ -79,7 +81,7 @@

if secLog.LogProfileBundle != nil && secLog.LogProfileBundle.FileLocation != "" {
bundleName := helpers.ToSafeFileName(secLog.LogProfileBundle.FileLocation)
bundlePath := fmt.Sprintf("%s/%s.tgz", "/etc/app_protect/bundles", bundleName)
bundlePath := fmt.Sprintf("%s/%s.tgz", appProtectBundleFolder, bundleName)
logEntry["LogProfileBundlePath"] = bundlePath
}

Expand Down Expand Up @@ -109,13 +111,13 @@
if dest.File != nil {
return dest.File.Path
}
return "stderr"

Check warning on line 114 in internal/controller/nginx/config/policies/waf/generator.go

View check run for this annotation

Codecov / codecov/patch

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

Added line #L114 was not covered by tests
case ngfAPI.SecurityLogDestinationTypeSyslog:
if dest.Syslog != nil {
return fmt.Sprintf("syslog:server=%s", dest.Syslog.Server)
}
return "stderr"
default:
return "stderr"

Check warning on line 121 in internal/controller/nginx/config/policies/waf/generator.go

View check run for this annotation

Codecov / codecov/patch

internal/controller/nginx/config/policies/waf/generator.go#L119-L121

Added lines #L119 - L121 were not covered by tests
}
}
15 changes: 11 additions & 4 deletions internal/controller/nginx/config/policies/waf/generator_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package waf_test

import (
"fmt"
"testing"

. "github.com/onsi/gomega"
Expand All @@ -11,10 +12,16 @@ import (
"github.com/nginx/nginx-gateway-fabric/internal/controller/nginx/config/http"
"github.com/nginx/nginx-gateway-fabric/internal/controller/nginx/config/policies"
"github.com/nginx/nginx-gateway-fabric/internal/controller/nginx/config/policies/waf"
"github.com/nginx/nginx-gateway-fabric/internal/framework/helpers"
)

func TestGenerate(t *testing.T) {
t.Parallel()

apDirBase := "app_protect_policy_file \"/etc/app_protect/bundles"
apFileDirective := fmt.Sprintf("%s/%s", apDirBase, helpers.ToSafeFileName("http://example.com/policy.tgz"))
apSecLogBase := "app_protect_security_log \"/etc/app_protect/bundles"
apSecLogDirective := fmt.Sprintf("%s/%s", apSecLogBase, helpers.ToSafeFileName("http://example.com/custom-log.tgz"))
Comment on lines +21 to +24
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should these start with app not ap?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking AppProtect rather than AppProtect

tests := []struct {
name string
policy policies.Policy
Expand All @@ -35,7 +42,7 @@ func TestGenerate(t *testing.T) {
},
expStrings: []string{
"app_protect_enable on;",
"app_protect_policy_file \"/etc/app_protect/bundles/",
apFileDirective,
},
},
{
Expand Down Expand Up @@ -64,7 +71,7 @@ func TestGenerate(t *testing.T) {
},
expStrings: []string{
"app_protect_enable on;",
"app_protect_policy_file \"/etc/app_protect/bundles/",
apFileDirective,
"app_protect_security_log_enable on;",
"app_protect_security_log \"log_default\" stderr;",
},
Expand Down Expand Up @@ -94,7 +101,7 @@ func TestGenerate(t *testing.T) {
},
expStrings: []string{
"app_protect_security_log_enable on;",
"app_protect_security_log \"/etc/app_protect/bundles/",
apSecLogDirective,
"/var/log/nginx/security.log;",
},
},
Expand Down Expand Up @@ -165,7 +172,7 @@ func TestGenerate(t *testing.T) {
},
expStrings: []string{
"app_protect_enable on;",
"app_protect_policy_file \"/etc/app_protect/bundles/",
apFileDirective,
"app_protect_security_log_enable on;",
"app_protect_security_log \"log_all\" stderr;",
"app_protect_security_log \"log_blocked\" /var/log/blocked.log;",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,7 @@
}

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

Expand Down Expand Up @@ -123,8 +121,8 @@
}

if u.Scheme != "http" && u.Scheme != "https" {
return errors.New("scheme must be http or https")
}

Check warning on line 125 in internal/controller/nginx/config/policies/waf/validator.go

View check run for this annotation

Codecov / codecov/patch

internal/controller/nginx/config/policies/waf/validator.go#L124-L125

Added lines #L124 - L125 were not covered by tests

if u.Host == "" {
return errors.New("host cannot be empty")
Expand Down
6 changes: 1 addition & 5 deletions internal/controller/state/graph/policies.go
Original file line number Diff line number Diff line change
Expand Up @@ -484,8 +484,8 @@
) map[WAFBundleKey]*WAFBundleData {
// Use provided factory or default to real fetcher
createFetcher := func(opts ...fetch.Option) fetch.Fetcher {
return fetch.NewDefaultFetcher(opts...)
}

Check warning on line 488 in internal/controller/state/graph/policies.go

View check run for this annotation

Codecov / codecov/patch

internal/controller/state/graph/policies.go#L487-L488

Added lines #L487 - L488 were not covered by tests
if len(fetcherFactory) > 0 {
createFetcher = fetcherFactory[0]
}
Expand All @@ -493,17 +493,13 @@
refPolicyBundles := make(map[WAFBundleKey]*WAFBundleData)

for policyKey, policy := range processedPolicies {
if policyKey.GVK != wafPolicyGVK {
continue
}

if !policy.Valid {
if policyKey.GVK != wafPolicyGVK || !policy.Valid {
continue
}

wafPolicy, ok := policy.Source.(*ngfAPIv1alpha1.WAFPolicy)
if !ok {
continue

Check warning on line 502 in internal/controller/state/graph/policies.go

View check run for this annotation

Codecov / codecov/patch

internal/controller/state/graph/policies.go#L502

Added line #L502 was not covered by tests
}

if wafPolicy.Spec.PolicySource != nil && wafPolicy.Spec.PolicySource.FileLocation != "" {
Expand Down
Loading