-
Notifications
You must be signed in to change notification settings - Fork 105
Expand file tree
/
Copy pathnginx-app-protect-instance-watcher.go
More file actions
300 lines (263 loc) · 9.55 KB
/
nginx-app-protect-instance-watcher.go
File metadata and controls
300 lines (263 loc) · 9.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
// Copyright (c) F5, Inc.
//
// This source code is licensed under the Apache License, Version 2.0 license found in the
// LICENSE file in the root directory of this source tree.
package instance
import (
"context"
"log/slog"
"os"
"strings"
"time"
"github.com/fsnotify/fsnotify"
mpi "github.com/nginx/agent/v3/api/grpc/mpi/v1"
"github.com/nginx/agent/v3/internal/config"
"github.com/nginx/agent/v3/internal/logger"
"github.com/nginx/agent/v3/pkg/id"
)
var (
versionFilePath = "/opt/app_protect/VERSION"
releaseFilePath = "/opt/app_protect/RELEASE"
attackSignatureVersionFilePath = "/opt/app_protect/var/update_files/signatures/version"
threatCampaignVersionFilePath = "/opt/app_protect/var/update_files/threat_campaigns/version"
enforcerEngineVersionFilePath = "/opt/app_protect/bd_config/enforcer.version"
versionFiles = []string{
versionFilePath,
releaseFilePath,
attackSignatureVersionFilePath,
threatCampaignVersionFilePath,
enforcerEngineVersionFilePath,
}
)
type NginxAppProtectInstanceWatcher struct {
agentConfig *config.Config
watcher *fsnotify.Watcher
instancesChannel chan<- InstanceUpdatesMessage
nginxAppProtectInstance *mpi.Instance
filesBeingWatched map[string]bool
version string
release string
attackSignatureVersion string
threatCampaignVersion string
enforcerEngineVersion string
}
func NewNginxAppProtectInstanceWatcher(agentConfig *config.Config) *NginxAppProtectInstanceWatcher {
return &NginxAppProtectInstanceWatcher{
agentConfig: agentConfig,
filesBeingWatched: make(map[string]bool),
}
}
func (w *NginxAppProtectInstanceWatcher) Watch(ctx context.Context, instancesChannel chan<- InstanceUpdatesMessage) {
monitoringFrequency := w.agentConfig.Watchers.InstanceWatcher.MonitoringFrequency
slog.DebugContext(
ctx,
"Starting NGINX App Protect instance watcher monitoring",
"monitoring_frequency", monitoringFrequency,
)
watcher, err := fsnotify.NewWatcher()
if err != nil {
slog.ErrorContext(ctx, "Failed to create NGINX App Protect instance watcher", "error", err)
return
}
w.watcher = watcher
w.instancesChannel = instancesChannel
w.watchVersionFiles(ctx)
instanceWatcherTicker := time.NewTicker(monitoringFrequency)
defer instanceWatcherTicker.Stop()
for {
select {
case <-ctx.Done():
closeError := w.watcher.Close()
if closeError != nil {
slog.ErrorContext(ctx, "Unable to close NGINX App Protect instance watcher", "error", closeError)
}
return
case <-instanceWatcherTicker.C:
// Need to keep watching directories in case NAP gets installed a while after NGINX Agent is started
w.watchVersionFiles(ctx)
w.checkForUpdates(ctx)
case event := <-w.watcher.Events:
w.handleEvent(ctx, event)
case watcherError := <-w.watcher.Errors:
slog.ErrorContext(ctx, "Unexpected error in NGINX App Protect instance watcher", "error", watcherError)
}
}
}
func (w *NginxAppProtectInstanceWatcher) watchVersionFiles(ctx context.Context) {
for _, versionFile := range versionFiles {
if !w.filesBeingWatched[versionFile] {
if _, fileOs := os.Stat(versionFile); fileOs != nil && os.IsNotExist(fileOs) {
w.filesBeingWatched[versionFile] = false
continue
}
w.addWatcher(ctx, versionFile)
w.filesBeingWatched[versionFile] = true
// On startup we need to read the files initially if they are discovered for the first time
w.readVersionFile(ctx, versionFile)
}
}
}
func (w *NginxAppProtectInstanceWatcher) addWatcher(ctx context.Context, versionFile string) {
if err := w.watcher.Add(versionFile); err != nil {
slog.ErrorContext(
ctx,
"Failed to add NGINX App Protect file watcher",
"file", versionFile, "error", err,
)
removeError := w.watcher.Remove(versionFile)
if removeError != nil {
slog.ErrorContext(
ctx,
"Failed to remove NGINX App Protect file watcher",
"file", versionFile, "error", removeError,
)
}
}
slog.DebugContext(ctx, "Added NGINX App Protect file watcher", "file", versionFile)
}
func (w *NginxAppProtectInstanceWatcher) readVersionFile(ctx context.Context, versionFile string) {
switch versionFile {
case versionFilePath:
w.version = w.readFile(ctx, versionFilePath)
case releaseFilePath:
w.release = w.readFile(ctx, releaseFilePath)
case threatCampaignVersionFilePath:
w.threatCampaignVersion = w.readFile(ctx, threatCampaignVersionFilePath)
case enforcerEngineVersionFilePath:
w.enforcerEngineVersion = w.readFile(ctx, enforcerEngineVersionFilePath)
case attackSignatureVersionFilePath:
w.attackSignatureVersion = w.readFile(ctx, attackSignatureVersionFilePath)
}
}
func (w *NginxAppProtectInstanceWatcher) handleEvent(ctx context.Context, event fsnotify.Event) {
switch {
case event.Has(fsnotify.Write), event.Has(fsnotify.Create):
w.handleFileUpdateEvent(ctx, event)
case event.Has(fsnotify.Remove), event.Has(fsnotify.Rename):
w.handleFileDeleteEvent(event)
}
}
func (w *NginxAppProtectInstanceWatcher) handleFileUpdateEvent(ctx context.Context, event fsnotify.Event) {
switch event.Name {
case versionFilePath:
w.version = w.readFile(ctx, event.Name)
case releaseFilePath:
w.release = w.readFile(ctx, event.Name)
case attackSignatureVersionFilePath:
w.attackSignatureVersion = w.readFile(ctx, event.Name)
case enforcerEngineVersionFilePath:
w.enforcerEngineVersion = w.readFile(ctx, event.Name)
case threatCampaignVersionFilePath:
w.threatCampaignVersion = w.readFile(ctx, event.Name)
}
}
func (w *NginxAppProtectInstanceWatcher) handleFileDeleteEvent(event fsnotify.Event) {
switch event.Name {
case versionFilePath:
w.version = ""
case releaseFilePath:
w.release = ""
case attackSignatureVersionFilePath:
w.attackSignatureVersion = ""
case enforcerEngineVersionFilePath:
w.enforcerEngineVersion = ""
case threatCampaignVersionFilePath:
w.threatCampaignVersion = ""
}
}
func (w *NginxAppProtectInstanceWatcher) checkForUpdates(ctx context.Context) {
// If a version file is discovered for the first time we treat that as a new instance
if w.isNewInstance() {
w.createInstance(ctx)
} else if w.nginxAppProtectInstance != nil {
// If a version file disappears then we assume that NGINX App Protect is uninstalled
if w.version == "" {
w.deleteInstance(ctx)
// If any version changes then we update the instance metadata
} else if w.haveVersionsChanged() {
w.updateInstance(ctx)
}
}
}
func (w *NginxAppProtectInstanceWatcher) isNewInstance() bool {
return w.nginxAppProtectInstance == nil && w.version != ""
}
func (w *NginxAppProtectInstanceWatcher) createInstance(ctx context.Context) {
w.nginxAppProtectInstance = &mpi.Instance{
InstanceMeta: &mpi.InstanceMeta{
InstanceId: id.Generate(versionFilePath),
InstanceType: mpi.InstanceMeta_INSTANCE_TYPE_NGINX_APP_PROTECT,
Version: w.version,
},
InstanceConfig: &mpi.InstanceConfig{},
InstanceRuntime: &mpi.InstanceRuntime{
ProcessId: 0,
BinaryPath: "",
ConfigPath: "",
Details: &mpi.InstanceRuntime_NginxAppProtectRuntimeInfo{
NginxAppProtectRuntimeInfo: &mpi.NGINXAppProtectRuntimeInfo{
Release: w.release,
AttackSignatureVersion: w.attackSignatureVersion,
ThreatCampaignVersion: w.threatCampaignVersion,
EnforcerEngineVersion: w.enforcerEngineVersion,
},
},
InstanceChildren: make([]*mpi.InstanceChild, 0),
},
}
slog.InfoContext(ctx, "Discovered a new NGINX App Protect instance")
w.instancesChannel <- InstanceUpdatesMessage{
CorrelationID: logger.CorrelationIDAttr(ctx),
InstanceUpdates: InstanceUpdates{
NewInstances: []*mpi.Instance{
w.nginxAppProtectInstance,
},
},
}
}
func (w *NginxAppProtectInstanceWatcher) deleteInstance(ctx context.Context) {
slog.InfoContext(ctx, "NGINX App Protect instance not longer exists")
w.instancesChannel <- InstanceUpdatesMessage{
CorrelationID: logger.CorrelationIDAttr(ctx),
InstanceUpdates: InstanceUpdates{
DeletedInstances: []*mpi.Instance{
w.nginxAppProtectInstance,
},
},
}
w.nginxAppProtectInstance = nil
}
func (w *NginxAppProtectInstanceWatcher) updateInstance(ctx context.Context) {
w.nginxAppProtectInstance.GetInstanceMeta().Version = w.version
runtimeInfo := w.nginxAppProtectInstance.GetInstanceRuntime().GetNginxAppProtectRuntimeInfo()
runtimeInfo.Release = w.release
runtimeInfo.AttackSignatureVersion = w.attackSignatureVersion
runtimeInfo.ThreatCampaignVersion = w.threatCampaignVersion
runtimeInfo.EnforcerEngineVersion = w.enforcerEngineVersion
slog.DebugContext(ctx, "NGINX App Protect instance updated")
w.instancesChannel <- InstanceUpdatesMessage{
CorrelationID: logger.CorrelationIDAttr(ctx),
InstanceUpdates: InstanceUpdates{
UpdatedInstances: []*mpi.Instance{
w.nginxAppProtectInstance,
},
},
}
}
func (w *NginxAppProtectInstanceWatcher) haveVersionsChanged() bool {
version := w.nginxAppProtectInstance.GetInstanceMeta().GetVersion()
runtimeInfo := w.nginxAppProtectInstance.GetInstanceRuntime().GetNginxAppProtectRuntimeInfo()
return version != w.version ||
runtimeInfo.GetRelease() != w.release ||
runtimeInfo.GetAttackSignatureVersion() != w.attackSignatureVersion ||
runtimeInfo.GetThreatCampaignVersion() != w.threatCampaignVersion ||
runtimeInfo.GetEnforcerEngineVersion() != w.enforcerEngineVersion
}
func (w *NginxAppProtectInstanceWatcher) readFile(ctx context.Context, filePath string) string {
contents, err := os.ReadFile(filePath)
if err != nil && !os.IsNotExist(err) {
slog.DebugContext(ctx, "Unable to read NGINX App Protect file", "file_path", filePath, "error", err)
return ""
}
return strings.TrimSuffix(string(contents), "\n")
}