Skip to content

Commit 1e8bd29

Browse files
authored
Fix include directive parsing to handle relative paths (#1174)
1 parent 6787152 commit 1e8bd29

File tree

4 files changed

+74
-13
lines changed

4 files changed

+74
-13
lines changed

internal/datasource/config/nginx_config_parser.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ func (ncp *NginxConfigParser) createNginxConfigContext(
142142
func(ctx context.Context, parent, directive *crossplane.Directive) error {
143143
switch directive.Directive {
144144
case "include":
145-
include := ncp.parseIncludeDirective(directive)
145+
include := ncp.parseIncludeDirective(directive, &conf)
146146

147147
nginxConfigContext.Includes = append(nginxConfigContext.Includes, include)
148148
case "log_format":
@@ -267,12 +267,15 @@ func (ncp *NginxConfigParser) findLocalSysLogServers(sysLogServer string) string
267267
return ""
268268
}
269269

270-
func (ncp *NginxConfigParser) parseIncludeDirective(directive *crossplane.Directive) string {
270+
func (ncp *NginxConfigParser) parseIncludeDirective(
271+
directive *crossplane.Directive,
272+
configFile *crossplane.Config,
273+
) string {
271274
var include string
272275
if filepath.IsAbs(directive.Args[0]) {
273276
include = directive.Args[0]
274277
} else {
275-
include = filepath.Join(filepath.Dir(directive.File), directive.Args[0])
278+
include = filepath.Join(filepath.Dir(configFile.File), directive.Args[0])
276279
}
277280

278281
return include

internal/datasource/config/nginx_config_parser_test.go

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,7 @@ func TestNginxConfigParser_Parse(t *testing.T) {
507507
allowedDirectories: []string{dir},
508508
},
509509
{
510-
name: "Test 10: Check with multiple syslog servers",
510+
name: "Test 10: Available NAP syslog server",
511511
instance: protos.NginxPlusInstance([]string{}),
512512
content: testconfig.NginxConfigWithMultipleSysLogs(errorLog.Name(), accessLog.Name(),
513513
"192.168.12.34:1517", "my.domain.com:1517", "127.0.0.1:1515"),
@@ -521,7 +521,7 @@ func TestNginxConfigParser_Parse(t *testing.T) {
521521
expectedLog: "Found valid NAP syslog server",
522522
},
523523
{
524-
name: "Test 10: Check with multiple syslog servers",
524+
name: "Test 11: Unavailable NAP syslog server",
525525
instance: protos.NginxPlusInstance([]string{}),
526526
content: testconfig.NginxConfigWithMultipleSysLogs(errorLog.Name(), accessLog.Name(),
527527
"192.168.12.34:1517", "my.domain.com:1517", "not.allowed:1515"),
@@ -1521,6 +1521,62 @@ func TestNginxConfigParser_checkDuplicate(t *testing.T) {
15211521
}
15221522
}
15231523

1524+
func TestNginxConfigParser_parseIncludeDirective(t *testing.T) {
1525+
parser := NewNginxConfigParser(types.AgentConfig())
1526+
1527+
tests := []struct {
1528+
name string
1529+
confFile string
1530+
expected string
1531+
args []string
1532+
}{
1533+
{
1534+
name: "Test 1: relative path",
1535+
args: []string{"test.conf"},
1536+
confFile: "/etc/nginx/nginx.conf",
1537+
expected: "/etc/nginx/test.conf",
1538+
},
1539+
{
1540+
name: "Test 2: absolute path",
1541+
args: []string{"/usr/local/nginx/conf/vhost.conf"},
1542+
confFile: "/etc/nginx/nginx.conf",
1543+
expected: "/usr/local/nginx/conf/vhost.conf",
1544+
},
1545+
{
1546+
name: "Test 3: wildcard",
1547+
args: []string{"/etc/nginx/conf.d/*.conf"},
1548+
confFile: "/etc/nginx/nginx.conf",
1549+
expected: "/etc/nginx/conf.d/*.conf",
1550+
},
1551+
{
1552+
name: "Test 4: relative path with subdirectory",
1553+
args: []string{"conf.d/default.conf"},
1554+
confFile: "/etc/nginx/nginx.conf",
1555+
expected: "/etc/nginx/conf.d/default.conf",
1556+
},
1557+
{
1558+
name: "Test 5: parent directory reference",
1559+
args: []string{"../sites-enabled/*.conf"},
1560+
confFile: "/etc/nginx/conf.d/nginx.conf",
1561+
expected: "/etc/nginx/sites-enabled/*.conf",
1562+
},
1563+
}
1564+
1565+
for _, tc := range tests {
1566+
t.Run(tc.name, func(t *testing.T) {
1567+
include := parser.parseIncludeDirective(
1568+
&crossplane.Directive{
1569+
Args: tc.args,
1570+
},
1571+
&crossplane.Config{
1572+
File: tc.confFile,
1573+
},
1574+
)
1575+
assert.Equal(t, tc.expected, include)
1576+
})
1577+
}
1578+
}
1579+
15241580
func protoListEqual(protoListA, protoListB []*mpi.File) bool {
15251581
for i := range protoListA {
15261582
res := proto.Equal(protoListA[i], protoListB[i])

internal/watcher/file/file_watcher_service.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,12 @@ func (fws *FileWatcherService) addWatchers(ctx context.Context) {
138138
}
139139

140140
if !slices.Contains(fws.watcher.WatchList(), directory) {
141-
fws.addWatcher(ctx, directory)
142-
fws.filesChanged.Store(true)
141+
err := fws.addWatcher(ctx, directory)
142+
if err != nil {
143+
slog.DebugContext(ctx, "Failed to add file watcher", "directory", directory, "error", err)
144+
} else {
145+
fws.filesChanged.Store(true)
146+
}
143147
}
144148
}
145149
}
@@ -208,7 +212,7 @@ func (fws *FileWatcherService) checkForUpdates(ctx context.Context, ch chan<- Fi
208212
}
209213
}
210214

211-
func (fws *FileWatcherService) addWatcher(ctx context.Context, directory string) {
215+
func (fws *FileWatcherService) addWatcher(ctx context.Context, directory string) error {
212216
slog.DebugContext(ctx, "Checking if file watcher needs to be added", "directory", directory)
213217

214218
if _, err := os.Stat(directory); errors.Is(err, os.ErrNotExist) {
@@ -220,9 +224,7 @@ func (fws *FileWatcherService) addWatcher(ctx context.Context, directory string)
220224

221225
slog.DebugContext(ctx, "Adding watcher", "directory", directory)
222226

223-
if err := fws.watcher.Add(directory); err != nil {
224-
slog.WarnContext(ctx, "Failed to add file watcher", "directory", directory, "error", err)
225-
}
227+
return fws.watcher.Add(directory)
226228
}
227229

228230
func (fws *FileWatcherService) removeWatcher(ctx context.Context, path string) {

internal/watcher/file/file_watcher_service_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func TestFileWatcherService_addWatcher(t *testing.T) {
6262
require.NoError(t, err)
6363
defer os.Remove(testDirectory)
6464

65-
fileWatcherService.addWatcher(ctx, testDirectory)
65+
require.NoError(t, fileWatcherService.addWatcher(ctx, testDirectory))
6666

6767
directoriesBeingWatched := fileWatcherService.watcher.WatchList()
6868
assert.Len(t, directoriesBeingWatched, 1)
@@ -79,7 +79,7 @@ func TestFileWatcherService_addWatcher_Error(t *testing.T) {
7979
tempDir := t.TempDir()
8080
testDirectory := path.Join(tempDir, "test_dir")
8181

82-
fileWatcherService.addWatcher(ctx, testDirectory)
82+
require.Error(t, fileWatcherService.addWatcher(ctx, testDirectory))
8383

8484
directoriesBeingWatched := fileWatcherService.watcher.WatchList()
8585
assert.Empty(t, directoriesBeingWatched)

0 commit comments

Comments
 (0)