Skip to content

Update file watcher to only monitor directories referenced in the nginx configuration #1134

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions internal/collector/containermetricsreceiver/internal/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,20 @@ func ReadLines(filename string) ([]string, error) {
}

// nolint: revive
func ReadLinesOffsetN(filename string, offset uint, n int) ([]string, error) {
func ReadLinesOffsetN(filename string, offset uint, n int) (lines []string, err error) {
f, err := os.Open(filename)
defer func() {
closeErr := f.Close()
if closeErr != nil {
if err == nil {
err = closeErr
}
}
}()

if err != nil {
return []string{}, err
}
defer f.Close()

var ret []string

Expand Down
4 changes: 4 additions & 0 deletions internal/config/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,10 @@ func (c *Config) AreReceiversConfigured() bool {
}

func isAllowedDir(dir string, allowedDirs []string) bool {
if !strings.HasSuffix(dir, "/") && filepath.Ext(dir) == "" {
dir += "/"
}

for _, allowedDirectory := range allowedDirs {
if strings.HasPrefix(dir, allowedDirectory) {
return true
Expand Down
4 changes: 2 additions & 2 deletions internal/config/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ func TestTypes_IsDirectoryAllowed(t *testing.T) {
allowed: true,
allowedDirs: []string{
AgentDirName,
"/etc/nginx",
"/etc/nginx/",
"/var/log/nginx/",
},
fileDir: "/etc/nginx/nginx.conf",
fileDir: "/etc/nginx",
},
{
name: "Test 2: directory not allowed",
Expand Down
17 changes: 16 additions & 1 deletion internal/datasource/config/nginx_config_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func (ncp *NginxConfigParser) Parse(ctx context.Context, instance *mpi.Instance)
return ncp.createNginxConfigContext(ctx, instance, payload)
}

// nolint: cyclop,revive,gocognit
// nolint: cyclop,revive,gocognit,gocyclo
func (ncp *NginxConfigParser) createNginxConfigContext(
ctx context.Context,
instance *mpi.Instance,
Expand Down Expand Up @@ -138,6 +138,10 @@ func (ncp *NginxConfigParser) createNginxConfigContext(
err := ncp.crossplaneConfigTraverse(ctx, &conf,
func(ctx context.Context, parent, directive *crossplane.Directive) error {
switch directive.Directive {
case "include":
include := ncp.parseIncludeDirective(directive)

nginxConfigContext.Includes = append(nginxConfigContext.Includes, include)
case "log_format":
formatMap = ncp.formatMap(directive)
case "access_log":
Expand Down Expand Up @@ -214,6 +218,17 @@ func (ncp *NginxConfigParser) createNginxConfigContext(
return nginxConfigContext, nil
}

func (ncp *NginxConfigParser) parseIncludeDirective(directive *crossplane.Directive) string {
var include string
if filepath.IsAbs(directive.Args[0]) {
include = directive.Args[0]
} else {
include = filepath.Join(filepath.Dir(directive.File), directive.Args[0])
}

return include
}

func (ncp *NginxConfigParser) addAccessLog(accessLog *model.AccessLog,
accessLogs []*model.AccessLog,
) []*model.AccessLog {
Expand Down
13 changes: 7 additions & 6 deletions internal/datasource/host/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,16 +205,17 @@ func (i *Info) containerID() string {
// mountInfo is the path: "/proc/self/mountinfo"
func containerIDFromMountInfo(mountInfo string) (string, error) {
mInfoFile, err := os.Open(mountInfo)
if err != nil {
return "", fmt.Errorf("could not read %s: %w", mountInfo, err)
}
defer func(f *os.File, fileName string) {
closeErr := f.Close()
if closeErr != nil {
slog.Error("Unable to close file", "file", fileName, "error", closeErr)
}
}(mInfoFile, mountInfo)

if err != nil {
return "", fmt.Errorf("could not read %s: %w", mountInfo, err)
}

fileScanner := bufio.NewScanner(mInfoFile)
fileScanner.Split(bufio.ScanLines)

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

func readOsRelease(path string) (map[string]string, error) {
f, err := os.Open(path)
if err != nil {
return nil, fmt.Errorf("release file %s is unreadable: %w", path, err)
}
defer func(f *os.File, fileName string) {
closeErr := f.Close()
if closeErr != nil {
slog.Error("Unable to close file", "file", fileName, "error", closeErr)
}
}(f, path)
if err != nil {
return nil, fmt.Errorf("release file %s is unreadable: %w", path, err)
}

info, err := parseOsReleaseFile(f)
if err != nil {
Expand Down
7 changes: 4 additions & 3 deletions internal/file/file_manager_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -854,15 +854,16 @@ func (fms *FileManagerService) writeManifestFile(updatedFiles map[string]*model.

// 0600 ensures only root can read/write
newFile, err := os.OpenFile(fms.manifestFilePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, filePerm)
if err != nil {
return fmt.Errorf("failed to read manifest file: %w", err)
}
defer func() {
if closeErr := newFile.Close(); closeErr != nil {
writeError = closeErr
}
}()

if err != nil {
return fmt.Errorf("failed to read manifest file: %w", err)
}

_, err = newFile.Write(manifestJSON)
if err != nil {
return fmt.Errorf("failed to write manifest file: %w", err)
Expand Down
1 change: 1 addition & 0 deletions internal/model/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type NginxConfigContext struct {
AccessLogs []*AccessLog
ErrorLogs []*ErrorLog
NAPSysLogServers []string
Includes []string
}

type APIDetails struct {
Expand Down
Loading
Loading