diff --git a/pkg/tools/otelcol-config/main.go b/pkg/tools/otelcol-config/main.go index 806d10efcd..7cbfe9a1bb 100644 --- a/pkg/tools/otelcol-config/main.go +++ b/pkg/tools/otelcol-config/main.go @@ -88,6 +88,7 @@ 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 @@ -95,7 +96,16 @@ func getSumologicRemoteWriter(values *flagValues) func([]byte) (int, error) { // 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 } } diff --git a/pkg/tools/otelcol-config/main_unix.go b/pkg/tools/otelcol-config/main_unix.go new file mode 100644 index 0000000000..c692e1dedb --- /dev/null +++ b/pkg/tools/otelcol-config/main_unix.go @@ -0,0 +1,50 @@ +package main + +import ( + "fmt" + "os" + "path/filepath" + "syscall" +) + +// use chown to set the owner of sumologic-remote.yaml to otelcol-sumo +// or whatever the user should be, based on the ownership of sumologic.yaml +// or its parent directory +func setSumologicRemoteOwner(values *flagValues) error { + 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) + } + } + + sys, ok := stat.Sys().(*syscall.Stat_t) + if !ok { + // the platform does not has the expected sys somehow, + // so just bail out with no error + return nil + } + + if int(sys.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(sys.Uid), int(sys.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 +} diff --git a/pkg/tools/otelcol-config/main_windows.go b/pkg/tools/otelcol-config/main_windows.go new file mode 100644 index 0000000000..9bc4fe579e --- /dev/null +++ b/pkg/tools/otelcol-config/main_windows.go @@ -0,0 +1,6 @@ +package main + +// this is only a stub for Windows build support +func setSumologicRemoteOwner(*flagValues) error { + return nil +}