Skip to content
This repository was archived by the owner on Oct 13, 2023. It is now read-only.

Commit 19d2ee3

Browse files
thaJeztahcpuguy83
authored andcommitted
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 <github@gone.nl> (cherry picked from commit 22a291f703b2511daaa8e0d325ab48523e817558) Signed-off-by: Brian Goff <cpuguy83@gmail.com> Upstream-commit: 14010c88b4517cd34ec89d2cad799916964f6808 Component: cli
1 parent d4e6d9b commit 19d2ee3

3 files changed

Lines changed: 43 additions & 0 deletions

File tree

components/cli/cli/config/configfile/file.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,9 @@ func (configFile *ConfigFile) Save() error {
196196
os.Remove(temp.Name())
197197
return err
198198
}
199+
// Try copying the current config file (if any) ownership and permissions
200+
copyFilePermissions(configFile.Filename, temp.Name())
201+
199202
return os.Rename(temp.Name(), configFile.Filename)
200203
}
201204

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// +build !windows
2+
3+
package configfile
4+
5+
import (
6+
"os"
7+
"syscall"
8+
)
9+
10+
// copyFilePermissions copies file ownership and permissions from "src" to "dst",
11+
// ignoring any error during the process.
12+
func copyFilePermissions(src, dst string) {
13+
var (
14+
mode os.FileMode = 0600
15+
uid, gid int
16+
)
17+
18+
fi, err := os.Stat(src)
19+
if err != nil {
20+
return
21+
}
22+
if fi.Mode().IsRegular() {
23+
mode = fi.Mode()
24+
}
25+
if err := os.Chmod(dst, mode); err != nil {
26+
return
27+
}
28+
29+
uid = int(fi.Sys().(*syscall.Stat_t).Uid)
30+
gid = int(fi.Sys().(*syscall.Stat_t).Gid)
31+
32+
if uid > 0 && gid > 0 {
33+
_ = os.Chown(dst, uid, gid)
34+
}
35+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package configfile
2+
3+
func copyFilePermissions(src, dst string) {
4+
// TODO implement for Windows
5+
}

0 commit comments

Comments
 (0)