Skip to content

Commit 26fcdfa

Browse files
committed
fix port already in use
1 parent 814884c commit 26fcdfa

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

internal/datasource/config/nginx_config_parser.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ const (
4747

4848
type (
4949
NginxConfigParser struct {
50-
agentConfig *config.Config
50+
agentConfig *config.Config
51+
previousNAPSysLogServer string
5152
}
5253
)
5354

@@ -65,7 +66,8 @@ type (
6566

6667
func NewNginxConfigParser(agentConfig *config.Config) *NginxConfigParser {
6768
return &NginxConfigParser{
68-
agentConfig: agentConfig,
69+
agentConfig: agentConfig,
70+
previousNAPSysLogServer: "",
6971
}
7072
}
7173

@@ -176,7 +178,8 @@ func (ncp *NginxConfigParser) createNginxConfigContext(
176178
slog.Info("args", "", directive.Args)
177179
sysLogServers := ncp.findValidSysLogServers(directive.Args)
178180
if len(sysLogServers) == 0 {
179-
slog.WarnContext(ctx, "Could not find usable NAP syslog server, security violations will be unavailable")
181+
slog.WarnContext(ctx, "Could not find usable NAP syslog server, "+
182+
"security violations will be unavailable")
180183
}
181184
for i := range sysLogServers {
182185
sysLogServer := sysLogServers[i]
@@ -209,6 +212,7 @@ func (ncp *NginxConfigParser) createNginxConfigContext(
209212
syslogServer := ncp.parseSyslogDirective(ctx, napSyslogServersFound)
210213
if syslogServer != "" {
211214
nginxConfigContext.NAPSysLogServer = syslogServer
215+
ncp.previousNAPSysLogServer = syslogServer
212216
}
213217
}
214218

@@ -224,6 +228,12 @@ func (ncp *NginxConfigParser) createNginxConfigContext(
224228
}
225229

226230
func (ncp *NginxConfigParser) parseSyslogDirective(ctx context.Context, napSyslogServers map[string]bool) string {
231+
if ncp.previousNAPSysLogServer != "" {
232+
if _, ok := napSyslogServers[ncp.previousNAPSysLogServer]; ok {
233+
return ncp.previousNAPSysLogServer
234+
}
235+
}
236+
227237
for napSyslogServer := range napSyslogServers {
228238
ln, err := net.Listen("tcp", napSyslogServer)
229239
if err != nil {

internal/datasource/config/nginx_config_parser_test.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -601,11 +601,12 @@ func TestNginxConfigParser_SyslogServerParse(t *testing.T) {
601601
instance.InstanceRuntime.ConfigPath = file.Name()
602602

603603
tests := []struct {
604-
name string
605-
content string
606-
expectedLog string
607-
expectedSyslogServers string
608-
portInUse bool
604+
name string
605+
content string
606+
expectedLog string
607+
expectedSyslogServers string
608+
previousNAPSysLogServer string
609+
portInUse bool
609610
}{
610611
{
611612
name: "Test 1: Valid port",
@@ -639,6 +640,15 @@ func TestNginxConfigParser_SyslogServerParse(t *testing.T) {
639640
expectedLog: "Could not find usable NAP syslog server",
640641
portInUse: true,
641642
},
643+
{
644+
name: "Test 5: Server hasn't changed",
645+
expectedSyslogServers: "127.0.0.1:1515",
646+
content: testconfig.NginxConfigWithMultipleSysLogs(errorLog.Name(), accessLog.Name(),
647+
"my.domain.com:1517", "127.0.0.1:1515", "my.domain.com:1517"),
648+
expectedLog: "Found NAP syslog server",
649+
portInUse: true,
650+
previousNAPSysLogServer: "127.0.0.1:1515",
651+
},
642652
}
643653

644654
for _, test := range tests {
@@ -649,6 +659,7 @@ func TestNginxConfigParser_SyslogServerParse(t *testing.T) {
649659
agentConfig := types.AgentConfig()
650660
agentConfig.AllowedDirectories = []string{dir}
651661
nginxConfig := NewNginxConfigParser(agentConfig)
662+
nginxConfig.previousNAPSysLogServer = test.previousNAPSysLogServer
652663

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

0 commit comments

Comments
 (0)