Skip to content

Commit

Permalink
fix: Container workflow creates files as root even when the commands …
Browse files Browse the repository at this point in the history
…are executed by a non-root user
  • Loading branch information
rahulguptajss committed Jan 9, 2024
1 parent 8b892ca commit 406a4e3
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 10 deletions.
73 changes: 63 additions & 10 deletions cmd/tools/generate/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ func generateDocker(kind int) {
}

color.DetectConsole("")

out, err = os.Create(opts.outputPath)
if err != nil {
logErrAndExit(err)
Expand Down Expand Up @@ -342,14 +343,14 @@ func copyFiles(srcPath, destPath string) error {
}
// requires specific permissions
dirsPermissions := map[string]os.FileMode{
"container": 0755,
"prometheus": 0755,
"container": 0755,
"grafana": 0755,
}
// requires specific permissions
filePermissions := map[string]os.FileMode{
filePermissionsInDir := map[string]os.FileMode{
"container": 0644,
"prometheus": 0644,
"grafana": 0640,
"grafana": 0644,
}

return filepath.Walk(srcPath, func(path string, info os.FileInfo, err error) error {
Expand All @@ -369,11 +370,30 @@ func copyFiles(srcPath, destPath string) error {
if dirsToExclude[info.Name()] {
return filepath.SkipDir
}
// Check if the directory is in the dirsPermissions map
if perm, ok := dirsPermissions[info.Name()]; ok {
return os.MkdirAll(dest, perm)
// Check if the current directory or any of its parent directories are in dirsPermissions
dirCreated := false
for dir, perm := range dirsPermissions {
if strings.HasPrefix(relPath, dir) {
err = os.MkdirAll(dest, perm)
if err != nil {
return err
}
dirCreated = true
break
}
}
if !dirCreated {
err = os.MkdirAll(dest, 0750)
if err != nil {
return err
}
}
return os.MkdirAll(dest, 0750)
err = changeOwner(dest)
if err != nil {
return err
}

return nil
}

// Skip excluded files
Expand All @@ -382,7 +402,7 @@ func copyFiles(srcPath, destPath string) error {
}

// Check if the file is under a directory in the filePermissions map
for dir, perm := range filePermissions {
for dir, perm := range filePermissionsInDir {
if strings.HasPrefix(relPath, dir) {
return copyFile(path, dest, perm)
}
Expand All @@ -405,7 +425,40 @@ func copyFile(srcPath, destPath string, perm os.FileMode) error {
defer silentClose(destFile)

_, err = io.Copy(destFile, srcFile)
return err
if err != nil {
return err
}

err = changeOwner(destPath)
if err != nil {
return err
}

return nil
}

func changeOwner(path string) error {
// Get the UID and GID from the environment variables
uidStr := os.Getenv("UID")
gidStr := os.Getenv("GID")

// If the UID and GID are set, change the owner and group of the file
if uidStr != "" && gidStr != "" {
uid, err := strconv.Atoi(uidStr)
if err != nil {
return err
}
gid, err := strconv.Atoi(gidStr)
if err != nil {
return err
}
err = os.Chown(path, uid, gid)
if err != nil {
return err
}
}

return nil
}

func asComposePath(path string) string {
Expand Down
1 change: 1 addition & 0 deletions docs/install/containerd.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ Create your `harvest-compose.yml` file like this:

```sh
docker run --rm \
--env UID=$(id -u) --env GID=$(id -g) \
--entrypoint "bin/harvest" \
--volume "$(pwd):/opt/temp" \
--volume "$(pwd)/harvest.yml:/opt/harvest/harvest.yml" \
Expand Down
3 changes: 3 additions & 0 deletions docs/install/containers.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ Pollers:

```sh
docker run --rm \
--env UID=$(id -u) --env GID=$(id -g) \
--entrypoint "bin/harvest" \
--volume "$(pwd):/opt/temp" \
--volume "$(pwd)/harvest.yml:/opt/harvest/harvest.yml" \
Expand All @@ -84,6 +85,7 @@ By default, the above command uses the harvest configuration file(`harvest.yml`)
```sh
HYML="/opt/custom_harvest.yml"; \
docker run --rm \
--env UID=$(id -u) --env GID=$(id -g) \
--entrypoint "bin/harvest" \
--volume "$(pwd):/opt/temp" \
--volume "${HYML}:${HYML}" \
Expand Down Expand Up @@ -198,6 +200,7 @@ To upgrade Harvest:

```sh
docker run --rm \
--env UID=$(id -u) --env GID=$(id -g) \
--entrypoint "bin/harvest" \
--volume "$(pwd):/opt/temp" \
--volume "$(pwd)/harvest.yml:/opt/harvest/harvest.yml" \
Expand Down
2 changes: 2 additions & 0 deletions docs/install/harvest-containers.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ Pollers:

```sh
docker run --rm \
--env UID=$(id -u) --env GID=$(id -g) \
--entrypoint "bin/harvest" \
--volume "$(pwd):/opt/temp" \
--volume "$(pwd)/harvest.yml:/opt/harvest/harvest.yml" \
Expand Down Expand Up @@ -98,6 +99,7 @@ To upgrade Harvest:

```sh
docker run --rm \
--env UID=$(id -u) --env GID=$(id -g) \
--entrypoint "bin/harvest" \
--volume "$(pwd):/opt/temp" \
--volume "$(pwd)/harvest.yml:/opt/harvest/harvest.yml" \
Expand Down
2 changes: 2 additions & 0 deletions docs/install/k8.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ To run Harvest resources in Kubernetes, please execute the following commands:

```
docker run --rm \
--env UID=$(id -u) --env GID=$(id -g) \
--entrypoint "bin/harvest" \
--volume "$(pwd):/opt/temp" \
--volume "$(pwd)/harvest.yml:/opt/harvest/harvest.yml" \
Expand Down Expand Up @@ -407,6 +408,7 @@ Please note the following assumptions for the steps below:

```
docker run --rm \
--env UID=$(id -u) --env GID=$(id -g) \
--entrypoint "bin/harvest" \
--volume "$(pwd):/opt/temp" \
--volume "$(pwd)/harvest.yml:/opt/harvest/harvest.yml" \
Expand Down
1 change: 1 addition & 0 deletions docs/install/podman.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ With these changes, the [standard Harvest compose instructions](containers.md#do

```sh
docker run --rm \
--env UID=$(id -u) --env GID=$(id -g) \
--entrypoint "bin/harvest" \
--volume "$(pwd):/opt/temp" \
--volume "$(pwd)/harvest.yml:/opt/harvest/harvest.yml" \
Expand Down

0 comments on commit 406a4e3

Please sign in to comment.