Skip to content

Commit 035de23

Browse files
authored
Update file watcher to only monitor directories referenced in the nginx configuration (#1134)
1 parent 2b6a9f2 commit 035de23

File tree

13 files changed

+367
-162
lines changed

13 files changed

+367
-162
lines changed

internal/collector/containermetricsreceiver/internal/utils.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,20 @@ func ReadLines(filename string) ([]string, error) {
2222
}
2323

2424
// nolint: revive
25-
func ReadLinesOffsetN(filename string, offset uint, n int) ([]string, error) {
25+
func ReadLinesOffsetN(filename string, offset uint, n int) (lines []string, err error) {
2626
f, err := os.Open(filename)
27+
defer func() {
28+
closeErr := f.Close()
29+
if closeErr != nil {
30+
if err == nil {
31+
err = closeErr
32+
}
33+
}
34+
}()
35+
2736
if err != nil {
2837
return []string{}, err
2938
}
30-
defer f.Close()
3139

3240
var ret []string
3341

internal/config/types.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,10 @@ func (c *Config) AreReceiversConfigured() bool {
415415
}
416416

417417
func isAllowedDir(dir string, allowedDirs []string) bool {
418+
if !strings.HasSuffix(dir, "/") && filepath.Ext(dir) == "" {
419+
dir += "/"
420+
}
421+
418422
for _, allowedDirectory := range allowedDirs {
419423
if strings.HasPrefix(dir, allowedDirectory) {
420424
return true

internal/config/types_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ func TestTypes_IsDirectoryAllowed(t *testing.T) {
2525
allowed: true,
2626
allowedDirs: []string{
2727
AgentDirName,
28-
"/etc/nginx",
28+
"/etc/nginx/",
2929
"/var/log/nginx/",
3030
},
31-
fileDir: "/etc/nginx/nginx.conf",
31+
fileDir: "/etc/nginx",
3232
},
3333
{
3434
name: "Test 2: directory not allowed",

internal/datasource/config/nginx_config_parser.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ func (ncp *NginxConfigParser) Parse(ctx context.Context, instance *mpi.Instance)
100100
return ncp.createNginxConfigContext(ctx, instance, payload)
101101
}
102102

103-
// nolint: cyclop,revive,gocognit
103+
// nolint: cyclop,revive,gocognit,gocyclo
104104
func (ncp *NginxConfigParser) createNginxConfigContext(
105105
ctx context.Context,
106106
instance *mpi.Instance,
@@ -138,6 +138,10 @@ func (ncp *NginxConfigParser) createNginxConfigContext(
138138
err := ncp.crossplaneConfigTraverse(ctx, &conf,
139139
func(ctx context.Context, parent, directive *crossplane.Directive) error {
140140
switch directive.Directive {
141+
case "include":
142+
include := ncp.parseIncludeDirective(directive)
143+
144+
nginxConfigContext.Includes = append(nginxConfigContext.Includes, include)
141145
case "log_format":
142146
formatMap = ncp.formatMap(directive)
143147
case "access_log":
@@ -214,6 +218,17 @@ func (ncp *NginxConfigParser) createNginxConfigContext(
214218
return nginxConfigContext, nil
215219
}
216220

221+
func (ncp *NginxConfigParser) parseIncludeDirective(directive *crossplane.Directive) string {
222+
var include string
223+
if filepath.IsAbs(directive.Args[0]) {
224+
include = directive.Args[0]
225+
} else {
226+
include = filepath.Join(filepath.Dir(directive.File), directive.Args[0])
227+
}
228+
229+
return include
230+
}
231+
217232
func (ncp *NginxConfigParser) addAccessLog(accessLog *model.AccessLog,
218233
accessLogs []*model.AccessLog,
219234
) []*model.AccessLog {

internal/datasource/host/info.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -205,16 +205,17 @@ func (i *Info) containerID() string {
205205
// mountInfo is the path: "/proc/self/mountinfo"
206206
func containerIDFromMountInfo(mountInfo string) (string, error) {
207207
mInfoFile, err := os.Open(mountInfo)
208-
if err != nil {
209-
return "", fmt.Errorf("could not read %s: %w", mountInfo, err)
210-
}
211208
defer func(f *os.File, fileName string) {
212209
closeErr := f.Close()
213210
if closeErr != nil {
214211
slog.Error("Unable to close file", "file", fileName, "error", closeErr)
215212
}
216213
}(mInfoFile, mountInfo)
217214

215+
if err != nil {
216+
return "", fmt.Errorf("could not read %s: %w", mountInfo, err)
217+
}
218+
218219
fileScanner := bufio.NewScanner(mInfoFile)
219220
fileScanner.Split(bufio.ScanLines)
220221

@@ -308,15 +309,15 @@ func (i *Info) releaseInfo(ctx context.Context, osReleaseLocation string) (relea
308309

309310
func readOsRelease(path string) (map[string]string, error) {
310311
f, err := os.Open(path)
311-
if err != nil {
312-
return nil, fmt.Errorf("release file %s is unreadable: %w", path, err)
313-
}
314312
defer func(f *os.File, fileName string) {
315313
closeErr := f.Close()
316314
if closeErr != nil {
317315
slog.Error("Unable to close file", "file", fileName, "error", closeErr)
318316
}
319317
}(f, path)
318+
if err != nil {
319+
return nil, fmt.Errorf("release file %s is unreadable: %w", path, err)
320+
}
320321

321322
info, err := parseOsReleaseFile(f)
322323
if err != nil {

internal/file/file_manager_service.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -869,15 +869,16 @@ func (fms *FileManagerService) writeManifestFile(updatedFiles map[string]*model.
869869

870870
// 0600 ensures only root can read/write
871871
newFile, err := os.OpenFile(fms.manifestFilePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, filePerm)
872-
if err != nil {
873-
return fmt.Errorf("failed to read manifest file: %w", err)
874-
}
875872
defer func() {
876873
if closeErr := newFile.Close(); closeErr != nil {
877874
writeError = closeErr
878875
}
879876
}()
880877

878+
if err != nil {
879+
return fmt.Errorf("failed to read manifest file: %w", err)
880+
}
881+
881882
_, err = newFile.Write(manifestJSON)
882883
if err != nil {
883884
return fmt.Errorf("failed to write manifest file: %w", err)

internal/model/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ type NginxConfigContext struct {
1919
AccessLogs []*AccessLog
2020
ErrorLogs []*ErrorLog
2121
NAPSysLogServers []string
22+
Includes []string
2223
}
2324

2425
type APIDetails struct {

0 commit comments

Comments
 (0)