-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
config: preserve ownership and permissions on configfile
When running `docker login` or `docker logout`, the CLI updates the configuration file by creating a temporary file, to replace the old one (if exists). When using `sudo`, this caused the file to be created as `root`, making it inaccessible to the current user. This patch updates the CLI to fetch permissions and ownership of the existing configuration file, and applies those permissions to the new file, so that it has the same permissions as the existing file (if any). Currently, only done for "Unix-y" systems (Mac, Linux), but can be implemented for Windows in future if there's a need. Signed-off-by: Sebastiaan van Stijn <[email protected]>
- Loading branch information
Showing
3 changed files
with
43 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// +build !windows | ||
|
||
package configfile | ||
|
||
import ( | ||
"os" | ||
"syscall" | ||
) | ||
|
||
// copyFilePermissions copies file ownership and permissions from "src" to "dst", | ||
// ignoring any error during the process. | ||
func copyFilePermissions(src, dst string) { | ||
var ( | ||
mode os.FileMode = 0600 | ||
uid, gid int | ||
) | ||
|
||
fi, err := os.Stat(src) | ||
if err != nil { | ||
return | ||
} | ||
if fi.Mode().IsRegular() { | ||
mode = fi.Mode() | ||
} | ||
if err := os.Chmod(dst, mode); err != nil { | ||
return | ||
} | ||
|
||
uid = int(fi.Sys().(*syscall.Stat_t).Uid) | ||
gid = int(fi.Sys().(*syscall.Stat_t).Gid) | ||
|
||
if uid > 0 && gid > 0 { | ||
_ = os.Chown(dst, uid, gid) | ||
} | ||
} |
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,5 @@ | ||
package configfile | ||
|
||
func copyFilePermissions(src, dst string) { | ||
// TODO implement for Windows | ||
} |