Skip to content

Review file permission for otel collector config #1037

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

Merged
merged 3 commits into from
Apr 17, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
33 changes: 24 additions & 9 deletions internal/collector/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,26 @@ func createURIs(cfg *config.Config) []string {
return []string{cfg.Collector.ConfigPath}
}

func createFile(err error, confPath string) error {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you remove the error from the function parameters and perform that check before calling this function?

if !os.IsNotExist(err) {
return err
}

// Create if doesn't exist.
_, createErr := os.Create(confPath)
if createErr != nil {
return createErr
}

// Set the file permissions to 600.
permissionErr := os.Chmod(confPath, configFilePermission)
if permissionErr != nil {
return permissionErr
}

return nil
}

// Generates a OTel Collector config to a file by injecting the Metrics Config to a Go template.
func writeCollectorConfig(conf *config.Collector) error {
otelcolTemplate, err := template.New(otelTemplatePath).Parse(otelcolTemplate)
Expand All @@ -82,17 +102,12 @@ func writeCollectorConfig(conf *config.Collector) error {

confPath := filepath.Clean(conf.ConfigPath)

// Check if file exists.
// Check if file exists, if not create it.
_, err = os.Stat(confPath)
if err != nil {
if !os.IsNotExist(err) {
return err
}

// Create if doesn't exist.
_, createErr := os.Create(confPath)
if createErr != nil {
return createErr
fileErr := createFile(err, confPath)
if fileErr != nil {
return fileErr
}
}

Expand Down
16 changes: 16 additions & 0 deletions internal/collector/settings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,3 +171,19 @@ func TestTemplateWrite(t *testing.T) {
// Convert to string for human readable error messages.
assert.Equal(t, string(expected), string(actual))
}

func TestFilePermissions(t *testing.T) {
tmpDir := t.TempDir()

cfg := types.AgentConfig()
actualConfPath := filepath.Join(tmpDir, "nginx-agent-otelcol-test.yaml")
cfg.Collector.ConfigPath = actualConfPath

err := writeCollectorConfig(cfg.Collector)
require.NoError(t, err)

// Check file permissions are 600
fileInfo, err := os.Stat(actualConfPath)
require.NoError(t, err)
assert.Equal(t, os.FileMode(0o600), fileInfo.Mode())
}