Skip to content

Commit 6f52b71

Browse files
committed
Create a helper function to unify permission and ownership setting in a cross-platform way
1 parent 8149a46 commit 6f52b71

2 files changed

Lines changed: 30 additions & 34 deletions

File tree

config/init_server_creds.go

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -708,19 +708,8 @@ func GeneratePEM(dir string) (key jwk.Key, err error) {
708708
return nil, errors.Wrap(err, "failed to get pelican user for setting ownership")
709709
}
710710

711-
// Windows does not have "chown", has to work differently
712-
currentOS := runtime.GOOS
713-
if currentOS == "windows" {
714-
cmd := exec.Command("icacls", fname, "/grant", user.Username+":F")
715-
output, err := cmd.CombinedOutput()
716-
if err != nil {
717-
return nil, errors.Wrapf(err, "failed to chown generated key %v to daemon group %v: %s",
718-
fname, user.Groupname, string(output))
719-
}
720-
} else { // Else we are running on linux/mac
721-
if err = os.Chown(fname, user.Uid, user.Gid); err != nil {
722-
return nil, errors.Wrapf(err, "failed to chown key file %s", fname)
723-
}
711+
if err = SetOwnershipAndPermissions(fname, 0640, user); err != nil {
712+
return nil, errors.Wrapf(err, "failed to set ownership and permissions for %s", fname)
724713
}
725714

726715
if err = generatePrivateKeyToFile(keyFile, elliptic.P256()); err != nil {

config/mkdirall.go

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,28 @@ import (
2828
"github.com/pkg/errors"
2929
)
3030

31+
// Apply the given permission bits and ownership to path in a cross-platform way.
32+
// - On Windows it runs “icacls path /grant username:F”
33+
// - On Linux/macOS it does os.Chmod(path, perm) then os.Chown(path, uid, gid)
34+
func SetOwnershipAndPermissions(path string, perm os.FileMode, user User) error {
35+
if runtime.GOOS == "windows" {
36+
cmd := exec.Command("icacls", path, "/grant", user.Username+":F")
37+
if out, err := cmd.CombinedOutput(); err != nil {
38+
return errors.Wrapf(err, "unable to modify ACLs on %q: %s", path, string(out))
39+
}
40+
return nil
41+
}
42+
43+
// Assume macOS or Linux
44+
if err := os.Chmod(path, perm); err != nil {
45+
return errors.Wrapf(err, "unable to chmod %q to %v", path, perm)
46+
}
47+
if err := os.Chown(path, user.Uid, user.Gid); err != nil {
48+
return errors.Wrapf(err, "unable to chown %q to %d:%d", path, user.Uid, user.Gid)
49+
}
50+
return nil
51+
}
52+
3153
// This is the pelican version of `MkdirAll`; ensures that any created directory
3254
// is owned by a given uid/gid. This allows the created directory to be owned by
3355
// the xrootd user.
@@ -76,27 +98,12 @@ func MkdirAll(path string, perm os.FileMode, uid int, gid int) error {
7698
}
7799

78100
// Set ownership on the directory that we just created.
79-
if runtime.GOOS == "windows" {
80-
username, err := GetDaemonUser() // FIXME (brianaydemir): This is not the correct user.
81-
if err != nil {
82-
return err
83-
}
84-
cmd := exec.Command("icacls", path, "/grant", username+":F")
85-
output, err := cmd.CombinedOutput()
86-
if err != nil {
87-
return errors.Wrapf(err, "unable to modify discretionary ACLs on directory %v: %s", path, string(output))
88-
}
89-
} else { // Assume macOS or Linux.
90-
// Any default system umask may prevent previous application of the permissions
91-
// from taking effect. To override this, set the permissions explicitly
92-
// after creation.
93-
if err = os.Chmod(path, perm); err != nil {
94-
return errors.Wrapf(err, "unable to chmod on directory %v to %v", path, perm)
95-
}
96-
97-
if err = os.Chown(path, uid, gid); err != nil {
98-
return errors.Wrapf(err, "unable to chown on directory %v to %v:%v", path, uid, gid)
99-
}
101+
user, err := GetPelicanUser()
102+
if err != nil {
103+
return errors.Wrap(err, "failed to get pelican user")
104+
}
105+
if err = SetOwnershipAndPermissions(path, perm, User{Uid: uid, Gid: gid, Username: user.Username}); err != nil {
106+
return errors.Wrapf(err, "failed to set ownership and permissions for %s", path)
100107
}
101108
return nil
102109
}

0 commit comments

Comments
 (0)