@@ -47,7 +47,8 @@ const (
47
47
48
48
type (
49
49
NginxConfigParser struct {
50
- agentConfig * config.Config
50
+ agentConfig * config.Config
51
+ previousNAPSysLogServer string
51
52
}
52
53
)
53
54
65
66
66
67
func NewNginxConfigParser (agentConfig * config.Config ) * NginxConfigParser {
67
68
return & NginxConfigParser {
68
- agentConfig : agentConfig ,
69
+ agentConfig : agentConfig ,
70
+ previousNAPSysLogServer : "" ,
69
71
}
70
72
}
71
73
@@ -107,6 +109,7 @@ func (ncp *NginxConfigParser) createNginxConfigContext(
107
109
payload * crossplane.Payload ,
108
110
) (* model.NginxConfigContext , error ) {
109
111
napSyslogServersFound := make (map [string ]bool )
112
+ napEnabled := false
110
113
111
114
nginxConfigContext := & model.NginxConfigContext {
112
115
InstanceID : instance .GetInstanceMeta ().GetInstanceId (),
@@ -173,19 +176,11 @@ func (ncp *NginxConfigParser) createNginxConfigContext(
173
176
}
174
177
case "app_protect_security_log" :
175
178
if len (directive .Args ) > 1 {
176
- syslogArg := directive .Args [1 ]
177
- re := regexp .MustCompile (`syslog:server=([\S]+)` )
178
- matches := re .FindStringSubmatch (syslogArg )
179
- if len (matches ) > 1 {
180
- syslogServer := matches [1 ]
181
- if ! napSyslogServersFound [syslogServer ] {
182
- nginxConfigContext .NAPSysLogServers = append (
183
- nginxConfigContext .NAPSysLogServers ,
184
- syslogServer ,
185
- )
186
- napSyslogServersFound [syslogServer ] = true
187
- slog .DebugContext (ctx , "Found NAP syslog server" , "address" , syslogServer )
188
- }
179
+ napEnabled = true
180
+ sysLogServer := ncp .findLocalSysLogServers (directive .Args [1 ])
181
+ if sysLogServer != "" && ! napSyslogServersFound [sysLogServer ] {
182
+ napSyslogServersFound [sysLogServer ] = true
183
+ slog .DebugContext (ctx , "Found NAP syslog server" , "address" , sysLogServer )
189
184
}
190
185
}
191
186
}
@@ -207,6 +202,17 @@ func (ncp *NginxConfigParser) createNginxConfigContext(
207
202
nginxConfigContext .PlusAPI = plusAPI
208
203
}
209
204
205
+ if len (napSyslogServersFound ) > 0 {
206
+ syslogServer := ncp .findAvailableSyslogServers (ctx , napSyslogServersFound )
207
+ if syslogServer != "" {
208
+ nginxConfigContext .NAPSysLogServer = syslogServer
209
+ ncp .previousNAPSysLogServer = syslogServer
210
+ }
211
+ } else if napEnabled {
212
+ slog .WarnContext (ctx , "Could not find available local NGINX App Protect syslog server. " +
213
+ "Security violations will not be collected." )
214
+ }
215
+
210
216
fileMeta , err := files .FileMeta (conf .File )
211
217
if err != nil {
212
218
slog .WarnContext (ctx , "Unable to get file metadata" , "file_name" , conf .File , "error" , err )
@@ -218,6 +224,49 @@ func (ncp *NginxConfigParser) createNginxConfigContext(
218
224
return nginxConfigContext , nil
219
225
}
220
226
227
+ func (ncp * NginxConfigParser ) findAvailableSyslogServers (ctx context.Context , napSyslogServers map [string ]bool ) string {
228
+ if ncp .previousNAPSysLogServer != "" {
229
+ if _ , ok := napSyslogServers [ncp .previousNAPSysLogServer ]; ok {
230
+ return ncp .previousNAPSysLogServer
231
+ }
232
+ }
233
+
234
+ for napSyslogServer := range napSyslogServers {
235
+ ln , err := net .Listen ("tcp" , napSyslogServer )
236
+ if err != nil {
237
+ slog .DebugContext (ctx , "NAP syslog server is not reachable" , "address" , napSyslogServer ,
238
+ "error" , err )
239
+
240
+ continue
241
+ }
242
+ ln .Close ()
243
+
244
+ slog .DebugContext (ctx , "Found valid NAP syslog server" , "address" , napSyslogServer )
245
+
246
+ return napSyslogServer
247
+ }
248
+
249
+ return ""
250
+ }
251
+
252
+ func (ncp * NginxConfigParser ) findLocalSysLogServers (sysLogServer string ) string {
253
+ re := regexp .MustCompile (`syslog:server=([\S]+)` )
254
+ matches := re .FindStringSubmatch (sysLogServer )
255
+ if len (matches ) > 1 {
256
+ host , _ , err := net .SplitHostPort (matches [1 ])
257
+ if err != nil {
258
+ return ""
259
+ }
260
+
261
+ ip := net .ParseIP (host )
262
+ if ip .IsLoopback () || strings .EqualFold (host , "localhost" ) {
263
+ return matches [1 ]
264
+ }
265
+ }
266
+
267
+ return ""
268
+ }
269
+
221
270
func (ncp * NginxConfigParser ) parseIncludeDirective (directive * crossplane.Directive ) string {
222
271
var include string
223
272
if filepath .IsAbs (directive .Args [0 ]) {
0 commit comments