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

chown labdir #1675

Closed
wants to merge 4 commits into from
Closed
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
2 changes: 1 addition & 1 deletion .github/workflows/cicd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ jobs:
matrix:
runtime:
- "docker"
- "podman"
# - "podman"
needs:
- unit-test
- staticcheck
Expand Down
6 changes: 6 additions & 0 deletions cmd/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,12 @@ func deployFn(_ *cobra.Command, _ []string) error {
// log new version availability info if ready
newVerNotification(vCh)

// setting uid/gid of a calling user to the lab directory
err = utils.SetUIDAndGID(c.TopoPaths.TopologyLabDir())
if err != nil {
log.Infof("error adjusting LabDir permissions: %v. Continuing anyways", err)
}

// print table summary
return printContainerInspect(containers, deployFormat)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func newVerNotification(vc chan string) {
case ver, ok := <-vc:
if ok {
relSlug := docsLinkFromVer(ver)
log.Infof("🎉 New containerlab version %s is available! Release notes: https://containerlab.dev/rn/%s\nRun 'containerlab version upgrade' to upgrade or go check other installation options at https://containerlab.dev/install/\n", ver, relSlug)
log.Infof("🎉 New containerlab version %s is available! Release notes: https://containerlab.dev/rn/%s\nRun 'sudo containerlab version upgrade' to upgrade or go check other installation options at https://containerlab.dev/install/\n", ver, relSlug)
}
default:
return
Expand Down
49 changes: 49 additions & 0 deletions utils/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"os/exec"
"os/user"
"path/filepath"
"strconv"
"strings"

log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -301,3 +302,51 @@ func FileLines(path, commentStr string) ([]string, error) {

return lines, nil
}

// SetUIDAndGID changes the UID and GID
// of the given path recursively to the values taken from
// SUDO_UID and SUDO_GID. Which should reflect be the non-root
// user that called clab via sudo.
func SetUIDAndGID(fsPath string) error {
// here we trust sudo to set up env variables
// a missing SUDO_UID env var indicates the root user
// runs clab without sudo
uid, isSet := os.LookupEnv("SUDO_UID")
if !isSet {
// nothing to do, already running as root
return nil
}

gid, isSet := os.LookupEnv("SUDO_GID")
if !isSet {
return errors.New("failed to lookup SUDO_GID env var")
}

iUID, err := strconv.Atoi(uid)
if err != nil {
return fmt.Errorf("unable to convert SUDO_UID %q to int", uid)
}

iGID, err := strconv.Atoi(gid)
if err != nil {
return fmt.Errorf("unable to convert SUDO_GID %q to int", gid)
}

err = recursiveChown(fsPath, iUID, iGID)
if err != nil {
return err
}

return nil
}

// recursiveChown function recursively chowns a path.
func recursiveChown(path string, uid, gid int) error {
return filepath.Walk(path, func(name string, info os.FileInfo, err error) error {
if err == nil {
err = os.Chown(name, uid, gid)
}

return err
})
}