Skip to content
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

otelcol-config: chown sumologic-remote.yaml #1670

Merged
merged 3 commits into from
Sep 10, 2024
Merged
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
61 changes: 60 additions & 1 deletion pkg/tools/otelcol-config/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,74 @@ func getConfDWriter(values *flagValues, fileName string) func(doc []byte) (int,

func getSumologicRemoteWriter(values *flagValues) func([]byte) (int, error) {
docPath := filepath.Join(values.ConfigDir, SumologicRemoteDotYaml)

return func(doc []byte) (int, error) {
if doc == nil {
// Special case: when doc is nil, we delete the file. This tells
// the packaging that it should not use --remote-config for
// otelcol-sumo.
return 0, os.Remove(docPath)
}
return len(doc), os.WriteFile(docPath, doc, 0600)

if err := os.WriteFile(docPath, doc, 0600); err != nil {
return 0, fmt.Errorf("error writing sumologic-remote.yaml: %s", err)
}

if err := setSumologicRemoteOwner(values); err != nil {
return len(doc), fmt.Errorf("error setting sumologic-remote.yaml owner: %s", err)
}

return len(doc), nil
}
}

func setSumologicRemoteOwner(values *flagValues) error {
if runtime.GOOS == "windows" {
// windows does not have the concept of uid and gid ownership
return nil
}

baseConfigPath := filepath.Join(values.ConfigDir, SumologicDotYaml)
docPath := filepath.Join(values.ConfigDir, SumologicRemoteDotYaml)

// check who owns the base configuration file
stat, err := os.Stat(baseConfigPath)
if err != nil {
// maybe it doesn't exist, stat the parent dir instead
stat, err = os.Stat(values.ConfigDir)
if err != nil {
// something is seriously wrong
return fmt.Errorf("error reading config dir: %s", err)
}
}

var uid, gid uint32

sys, ok := stat.Sys().(*syscall.Stat_t)
if ok {
uid = sys.Uid
gid = sys.Gid
} else {
// we're not on a supported platform for chown
return nil
}

if int(uid) == syscall.Getuid() {
// we're already that user
return nil
}

// set the owner to be consistent with the other configuration
if err := os.Chown(docPath, int(uid), int(gid)); err != nil {
if err.(*os.PathError).Err == syscall.EPERM {
// we don't have permission to chown, skip it
return nil
}
return err
}

return nil

}

func getHostMetricsFilename() string {
Expand Down
Loading