Skip to content
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

Restrict NAP file/dir permissions #516

Merged
merged 5 commits into from
Dec 6, 2023
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
7 changes: 3 additions & 4 deletions src/extensions/nginx-app-protect/nap/nap.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,16 @@ import (
"strings"
"time"

log "github.com/sirupsen/logrus"

"github.com/nginx/agent/v2/src/core"

log "github.com/sirupsen/logrus"
)

const (
DefaultOptNAPDir = "/opt/app_protect"
DefaultNMSCompilerDir = "/opt/nms-nap-compiler"
compilerDirPrefix = "app_protect-"

dirPerm = 0o755
dirPerm = 0o750
)

var (
Expand Down
23 changes: 16 additions & 7 deletions src/extensions/nginx-app-protect/nap/nap_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ package nap
import (
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"

Expand All @@ -33,11 +34,11 @@ func UpdateMetadata(
data, err := os.ReadFile(appProtectWAFDetails.GetWafLocation())
if err != nil {
if !errors.Is(err, os.ErrNotExist) {
return err
return fmt.Errorf("failed to update metadata: %v", err)
}
} else {
if err := json.Unmarshal(data, &previousMeta); err != nil {
return err
return fmt.Errorf("failed to unmarshal current metadata: %v", err)
}
previousPrecompiledPublication = previousMeta.PrecompiledPublication
}
Expand All @@ -53,14 +54,16 @@ func UpdateMetadata(
policies, profiles := sdk.GetAppProtectPolicyAndSecurityLogFilesWithIgnoreDirectives(cfg, ignoreDirectives)

policyBundles := []*BundleMetadata{}
profileBundles := []*BundleMetadata{}

for _, policy := range policies {
bundle := &BundleMetadata{
Name: policy,
}
policyBundles = append(policyBundles, bundle)
}

profileBundles := []*BundleMetadata{}

for _, profile := range profiles {
bundle := &BundleMetadata{
Name: profile,
Expand All @@ -84,21 +87,27 @@ func UpdateMetadata(

m, err := json.Marshal(metadata)
if err != nil {
return err
return fmt.Errorf("failed to marshal metadata update: %v", err)
}

// Make dir if not exists
directory := filepath.Dir(appProtectWAFDetails.GetWafLocation())
_, err = os.Stat(directory)
if os.IsNotExist(err) {
err = os.MkdirAll(directory, 0o755)
err = os.MkdirAll(directory, 0o750)
if err != nil {
return err
return fmt.Errorf("failed to create directory for metadata update: %v", err)
}
}

log.Debugf("Writing NAP Metadata %s", m)
return os.WriteFile(appProtectWAFDetails.GetWafLocation(), m, 0o644)

err = os.WriteFile(appProtectWAFDetails.GetWafLocation(), m, 0o644)
if err != nil {
return fmt.Errorf("failed to write NAP Metadata update: %v", err)
}

return nil
}

// metadataAreEqual compares the metadata for equality
Expand Down
6 changes: 4 additions & 2 deletions src/extensions/nginx-app-protect/nap/nap_metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

"github.com/nginx/agent/sdk/v2"
"github.com/nginx/agent/sdk/v2/proto"

"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -170,9 +171,10 @@ func TestUpdateNapMetadata(t *testing.T) {
WafLocation: metadataFile,
PrecompiledPublication: tc.precompPub,
}
ignoreDirecitves := []string{}

err = UpdateMetadata(cfg, appProtectWAFDetails, ignoreDirecitves)
ignoreDirectives = []string{}

err = UpdateMetadata(cfg, appProtectWAFDetails, ignoreDirectives)
assert.NoError(t, err)

data, err := os.ReadFile(metadataFile)
Expand Down
4 changes: 2 additions & 2 deletions src/extensions/nginx-app-protect/nap/nap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import (
"os"
"testing"

testutils "github.com/nginx/agent/v2/test/utils"

"github.com/google/uuid"
"github.com/stretchr/testify/assert"

testutils "github.com/nginx/agent/v2/test/utils"
)

const (
Expand Down
2 changes: 1 addition & 1 deletion src/extensions/php_fpm_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ import (
agent_config "github.com/nginx/agent/sdk/v2/agent/config"
"github.com/nginx/agent/v2/src/core"
"github.com/nginx/agent/v2/src/core/config"

log "github.com/sirupsen/logrus"
)

const (
// Version
phpFpmMetricsExtensionPluginVersion = "v0.1.0"
)

Expand Down
1 change: 1 addition & 0 deletions src/extensions/php_fpm_metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/nginx/agent/v2/src/core/config"
"github.com/nginx/agent/v2/src/extensions"
tutils "github.com/nginx/agent/v2/test/utils"

"github.com/stretchr/testify/assert"
)

Expand Down
2 changes: 1 addition & 1 deletion src/extensions/prometheus-metrics/prometheus_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"strings"

"github.com/nginx/agent/sdk/v2/proto"

"github.com/nginx/agent/v2/src/core/metrics"

"github.com/prometheus/client_golang/prometheus"
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package prometheus_metrics
import (
"testing"

"github.com/nginx/agent/sdk/v2/proto"
"github.com/nginx/agent/v2/src/core/metrics"

"github.com/nginx/agent/sdk/v2/proto"
"github.com/prometheus/client_golang/prometheus"
"github.com/stretchr/testify/assert"
)
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.