forked from hashicorp/consul-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow setting user/group ownership of templete output
Related issues hashicorp#639, hashicorp#1497, hashicorp#1185 and also nomad/5020 Signed-off-by: Alessandro De Blasis <[email protected]>
- Loading branch information
Showing
7 changed files
with
355 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
//go:build !windows | ||
// +build !windows | ||
|
||
package renderer | ||
|
||
import ( | ||
"os" | ||
"syscall" | ||
) | ||
|
||
func getFileOwnership(path string) (*int, *int) { | ||
file_info, err := os.Stat(path) | ||
|
||
if err != nil { | ||
return nil, nil | ||
} | ||
|
||
file_sys := file_info.Sys() | ||
st := file_sys.(*syscall.Stat_t) | ||
return intPtr(int(st.Uid)), intPtr(int(st.Gid)) | ||
} | ||
|
||
func setFileOwnership(path string, uid, gid *int) error { | ||
wantedUid := sanitizeUidGid(uid) | ||
wantedGid := sanitizeUidGid(gid) | ||
if wantedUid == -1 && wantedGid == -1 { | ||
return nil //noop | ||
} | ||
return os.Chown(path, wantedUid, wantedGid) | ||
} | ||
|
||
func isChownNeeded(path string, uid, gid *int) bool { | ||
wantedUid := sanitizeUidGid(uid) | ||
wantedGid := sanitizeUidGid(gid) | ||
if wantedUid == -1 && wantedGid == -1 { | ||
return false | ||
} | ||
|
||
currUid, currGid := getFileOwnership(path) | ||
return wantedUid != *currUid || wantedGid != *currGid | ||
} | ||
|
||
// sanitizeUidGid sanitizes the uid/gid so that can be an input for os.Chown | ||
func sanitizeUidGid(id *int) int { | ||
if id == nil { | ||
return -1 | ||
} | ||
return *id | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
//go:build windows | ||
// +build windows | ||
|
||
package renderer | ||
|
||
import ( | ||
"log" | ||
"os" | ||
) | ||
|
||
func setFileOwnership(path string, uid, gid *int) error { | ||
if uid != nil || gid != nil { | ||
log.Printf("[WARN] (runner) cannot set uid/gid for rendered files on Windows") | ||
} | ||
return nil | ||
} | ||
|
||
func isChownNeeded(path string, wantedUid, wantedGid *int) bool { | ||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.