diff --git a/src/extensions/nginx-app-protect/nap/nap_metadata.go b/src/extensions/nginx-app-protect/nap/nap_metadata.go index c9e6865839..dfcae82626 100644 --- a/src/extensions/nginx-app-protect/nap/nap_metadata.go +++ b/src/extensions/nginx-app-protect/nap/nap_metadata.go @@ -11,6 +11,7 @@ import ( "encoding/json" "errors" "os" + "path/filepath" "github.com/nginx/agent/sdk/v2" "github.com/nginx/agent/sdk/v2/proto" @@ -88,8 +89,18 @@ func UpdateMetadata( if err != nil { return err } - log.Debugf("Writing NAP Metadata %s", m) + // Make dir if not exists + directory := filepath.Dir(wafLocation) + _, err = os.Stat(directory) + if os.IsNotExist(err) { + err = os.MkdirAll(directory, 0755) + if err != nil { + return err + } + } + + log.Debugf("Writing NAP Metadata %s", m) return os.WriteFile(wafLocation, m, 0644) } diff --git a/src/extensions/nginx-app-protect/nap/nap_metadata_test.go b/src/extensions/nginx-app-protect/nap/nap_metadata_test.go index cea6f48e0e..2af0dd7163 100644 --- a/src/extensions/nginx-app-protect/nap/nap_metadata_test.go +++ b/src/extensions/nginx-app-protect/nap/nap_metadata_test.go @@ -19,7 +19,9 @@ import ( const ( configFile = "/tmp/testdata/nginx.conf" - metadataFile = "/tmp/testdata/app_protect_metadata.json" + basePath = "/tmp/testdata" + metadataPath = "/tmp/testdata/nms" + metadataFile = "/tmp/testdata/nms/app_protect_metadata.json" nginxID = "1" systemID = "2" @@ -105,6 +107,12 @@ func TestUpdateNapMetadata(t *testing.T) { precompPub bool expected string }{ + { + testName: "NoMetadataDir", + meta: "", + precompPub: false, + expected: expectedFalse, + }, { testName: "NoMetadataFile", meta: "", @@ -135,7 +143,14 @@ func TestUpdateNapMetadata(t *testing.T) { t.Run(tc.testName, func(t *testing.T) { defer tearDownTestDirectory() - if tc.testName != "NoMetadataFile" { + switch tc.testName { + case "NoMetadataDir": + err := os.MkdirAll(basePath, 0755) + assert.NoError(t, err) + case "NoMetadataFile": + err := os.MkdirAll(metadataPath, 0755) + assert.NoError(t, err) + default: err := setUpFile(metadataFile, []byte(tc.meta)) assert.NoError(t, err) } diff --git a/test/performance/vendor/github.com/nginx/agent/v2/src/extensions/nginx-app-protect/nap/nap_metadata.go b/test/performance/vendor/github.com/nginx/agent/v2/src/extensions/nginx-app-protect/nap/nap_metadata.go index c9e6865839..dfcae82626 100644 --- a/test/performance/vendor/github.com/nginx/agent/v2/src/extensions/nginx-app-protect/nap/nap_metadata.go +++ b/test/performance/vendor/github.com/nginx/agent/v2/src/extensions/nginx-app-protect/nap/nap_metadata.go @@ -11,6 +11,7 @@ import ( "encoding/json" "errors" "os" + "path/filepath" "github.com/nginx/agent/sdk/v2" "github.com/nginx/agent/sdk/v2/proto" @@ -88,8 +89,18 @@ func UpdateMetadata( if err != nil { return err } - log.Debugf("Writing NAP Metadata %s", m) + // Make dir if not exists + directory := filepath.Dir(wafLocation) + _, err = os.Stat(directory) + if os.IsNotExist(err) { + err = os.MkdirAll(directory, 0755) + if err != nil { + return err + } + } + + log.Debugf("Writing NAP Metadata %s", m) return os.WriteFile(wafLocation, m, 0644) }