Skip to content
Open
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
2 changes: 1 addition & 1 deletion api/grpc/mpi/v1/command_grpc.pb.go

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

2 changes: 1 addition & 1 deletion api/grpc/mpi/v1/files_grpc.pb.go

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

4 changes: 0 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,6 @@ github.com/foxboron/go-tpm-keyfiles v0.0.0-20251226215517-609e4778396f/go.mod h1
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbSClcnxKAGw=
github.com/fsnotify/fsnotify v1.9.0 h1:2Ml+OJNzbYCTzsxtv8vKSFD9PbJjmhYF14k/jKC7S9k=
github.com/fsnotify/fsnotify v1.9.0/go.mod h1:8jBTzvmWwFyi3Pb8djgCCO5IBqzKJ/Jwo8TRcHyHii0=
github.com/fsnotify/fsnotify v1.10.1 h1:b0/UzAf9yR5rhf3RPm9gf3ehBPpf0oZKIjtpKrx59Ho=
github.com/fsnotify/fsnotify v1.10.1/go.mod h1:TLheqan6HD6GBK6PrDWyDPBaEV8LspOxvPSjC+bVfgo=
github.com/fxamacker/cbor/v2 v2.9.1 h1:2rWm8B193Ll4VdjsJY28jxs70IdDsHRWgQYAI80+rMQ=
Expand Down Expand Up @@ -1083,8 +1081,6 @@ golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/mod v0.15.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/mod v0.35.0 h1:Ww1D637e6Pg+Zb2KrWfHQUnH2dQRLBQyAtpr/haaJeM=
golang.org/x/mod v0.35.0/go.mod h1:+GwiRhIInF8wPm+4AoT6L0FA1QWAad3OMdTRx4tFYlU=
golang.org/x/mod v0.36.0 h1:JJjpVx6myfUsUdAzZuOSTTmRE0PfZeNWzzvKrP7amb4=
golang.org/x/mod v0.36.0/go.mod h1:moc6ELqsWcOw5Ef3xVprK5ul/MvtVvkIXLziUOICjUQ=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
Expand Down
17 changes: 17 additions & 0 deletions internal/datasource/config/nginx_config_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,8 @@ func (ncp *NginxConfigParser) createNginxConfigContext(
}

rootDir := filepath.Dir(instance.GetInstanceRuntime().GetConfigPath())
ignoredAccessLog := make(map[string]struct{})
ignoredErrorLog := make(map[string]struct{})

for _, conf := range payload.Config {
slog.DebugContext(ctx, "Traversing NGINX config file", "config", conf)
Expand All @@ -274,16 +276,31 @@ func (ncp *NginxConfigParser) createNginxConfigContext(
case "log_format":
formatMap = ncp.formatMap(directive)
case "access_log":
if _, seen := ignoredAccessLog[directive.Args[0]]; seen {
slog.DebugContext(ctx, "Ignoring duplicated access_log entry:", "directive", directive.Args[0])

return nil
}

if !ncp.ignoreLog(directive.Args[0]) {
accessLog := ncp.accessLog(directive.Args[0], ncp.accessLogDirectiveFormat(directive),
formatMap)
nginxConfigContext.AccessLogs = ncp.addAccessLog(accessLog, nginxConfigContext.AccessLogs)
} else {
ignoredAccessLog[directive.Args[0]] = struct{}{}
}
case "error_log":
if _, seen := ignoredErrorLog[directive.Args[0]]; seen {
slog.DebugContext(ctx, "Ignoring duplicated error_log entry:", "directive", directive.Args[0])

return nil
}

if !ncp.ignoreLog(directive.Args[0]) {
errorLog := ncp.errorLog(directive.Args[0], ncp.errorLogDirectiveLevel(directive))
nginxConfigContext.ErrorLogs = append(nginxConfigContext.ErrorLogs, errorLog)
} else {
ignoredErrorLog[directive.Args[0]] = struct{}{}
slog.WarnContext(ctx, fmt.Sprintf("Currently error log outputs to %s. Log monitoring "+
"is disabled while applying a config; "+"log errors to file to enable error monitoring",
directive.Args[0]), "error_log", directive.Args[0])
Expand Down
69 changes: 69 additions & 0 deletions internal/datasource/config/nginx_config_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,17 @@ server {
allow 127.0.0.1;
deny all;
}
}
`
testConf27 = `http {
access_log /var/log/nginx/access.log;
access_log /var/log/nginx/access.log;

}`
testConf28 = `http {
error_log /var/log/nginx/error.log;
error_log /var/log/nginx/error.log;

}`
)

Expand Down Expand Up @@ -1495,6 +1506,64 @@ Reading: 0 Writing: 1 Waiting: 1
}
}

func TestNginxConfigParser_DuplicatedLogPaths(t *testing.T) {
ctx := context.Background()
dir := t.TempDir()

file := helpers.CreateFileWithErrorCheck(t, dir, "nginx-parse-config.conf")
defer helpers.RemoveFileWithErrorCheck(t, file.Name())

instance := protos.NginxOssInstance([]string{})
instance.InstanceRuntime.ConfigPath = file.Name()

tests := []struct {
name string
conf string
expectedLog string
excludedLogs []string
}{
{
name: "Test 1: Duplicate access log path is excluded once",
conf: testConf27,
expectedLog: "Ignoring duplicated access_log entry",
excludedLogs: []string{"/var/log/nginx/access.log"},
},
{
name: "Test 2: Duplicate error log path is excluded once",
conf: testConf28,
expectedLog: "Ignoring duplicated error_log entry",
excludedLogs: []string{"/var/log/nginx/error.log"},
},
{
name: "Test 3: Duplicate access log path not excluded is skipped",
conf: testConf27,
expectedLog: "Found duplicate access log, skipping",
excludedLogs: []string{},
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
logBuf := &bytes.Buffer{}
stub.StubLoggerWith(logBuf)

agentConfig := types.AgentConfig()
agentConfig.DataPlaneConfig.Nginx.ExcludeLogs = test.excludedLogs
agentConfig.AllowedDirectories = []string{dir}
nginxConfig := NewNginxConfigParser(agentConfig)

writeErr := os.WriteFile(file.Name(), []byte(test.conf), 0o600)
require.NoError(t, writeErr)

_, parseError := nginxConfig.Parse(ctx, instance)
require.NoError(t, parseError)

helpers.ValidateLog(t, test.expectedLog, logBuf)
logBuf.Reset()
})
}
}

func TestNginxConfigParser_ignoreLog(t *testing.T) {
tests := []struct {
name string
Expand Down
Loading