From 0283c8fa94760d99c10ea9f34807154f6ff25e11 Mon Sep 17 00:00:00 2001 From: Karen Santana Date: Mon, 25 May 2026 16:07:17 +0100 Subject: [PATCH 1/2] Fix logs being excluded multiple times --- api/grpc/mpi/v1/command_grpc.pb.go | 2 +- api/grpc/mpi/v1/files_grpc.pb.go | 2 +- .../datasource/config/nginx_config_parser.go | 17 +++++ .../config/nginx_config_parser_test.go | 69 +++++++++++++++++++ 4 files changed, 88 insertions(+), 2 deletions(-) diff --git a/api/grpc/mpi/v1/command_grpc.pb.go b/api/grpc/mpi/v1/command_grpc.pb.go index 2efba7b3c..fe7c79990 100644 --- a/api/grpc/mpi/v1/command_grpc.pb.go +++ b/api/grpc/mpi/v1/command_grpc.pb.go @@ -8,7 +8,7 @@ // Code generated by protoc-gen-go-grpc. DO NOT EDIT. // versions: -// - protoc-gen-go-grpc v1.6.1 +// - protoc-gen-go-grpc v1.6.2 // - protoc (unknown) // source: mpi/v1/command.proto diff --git a/api/grpc/mpi/v1/files_grpc.pb.go b/api/grpc/mpi/v1/files_grpc.pb.go index fec381a55..d5b8b3b5a 100644 --- a/api/grpc/mpi/v1/files_grpc.pb.go +++ b/api/grpc/mpi/v1/files_grpc.pb.go @@ -5,7 +5,7 @@ // Code generated by protoc-gen-go-grpc. DO NOT EDIT. // versions: -// - protoc-gen-go-grpc v1.6.1 +// - protoc-gen-go-grpc v1.6.2 // - protoc (unknown) // source: mpi/v1/files.proto diff --git a/internal/datasource/config/nginx_config_parser.go b/internal/datasource/config/nginx_config_parser.go index 19ed3a67c..643342872 100644 --- a/internal/datasource/config/nginx_config_parser.go +++ b/internal/datasource/config/nginx_config_parser.go @@ -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) @@ -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]) diff --git a/internal/datasource/config/nginx_config_parser_test.go b/internal/datasource/config/nginx_config_parser_test.go index 708531cc4..a97c1f240 100644 --- a/internal/datasource/config/nginx_config_parser_test.go +++ b/internal/datasource/config/nginx_config_parser_test.go @@ -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; + }` ) @@ -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 From 8b10e1b841fd4d2c43cd761e61702ab5374cafd7 Mon Sep 17 00:00:00 2001 From: Karen Santana Date: Mon, 25 May 2026 16:16:17 +0100 Subject: [PATCH 2/2] Add dependency changes --- go.sum | 4 ---- 1 file changed, 4 deletions(-) diff --git a/go.sum b/go.sum index 9a2bd05ad..43e59c9fd 100644 --- a/go.sum +++ b/go.sum @@ -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= @@ -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=