Skip to content

Commit d3f6867

Browse files
committed
cli/config/credentials: skip saving config-file if credentials didn't change
Before this change, the config-file was always updated, even if there were no changes to save. This could cause issues when the config-file already had credentials set and was read-only for the current user. For example, on NixOS, this poses a problem because `config.json` is a symlink to a write-protected file; $ readlink ~/.docker/config.json /home/username/.config/sops-nix/secrets/ghcr_auth $ readlink -f ~/.docker/config.json /run/user/1000/secrets.d/28/ghcr_auth Which causes `docker login` to fail, even if no changes were to be made; Error saving credentials: rename /home/derek/.docker/config.json2180380217 /home/username/.config/sops-nix/secrets/ghcr_auth: invalid cross-device link This patch updates the code to only update the config file if changes were detected. It there's nothing to save, it skips updating the file, as well as skips printing the warning about credentials being stored insecurely. With this patch applied: $ docker login -u yourname Password: WARNING! Your credentials are stored unencrypted in '/root/.docker/config.json'. Configure a credential helper to remove this warning. See https://docs.docker.com/go/credential-store/ Login Succeeded $ docker login -u yourname Password: Login Succeeded Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent 8a7c5ae commit d3f6867

1 file changed

Lines changed: 10 additions & 1 deletion

File tree

cli/config/credentials/file_store.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ func NewFileStore(file store) Store {
3030

3131
// Erase removes the given credentials from the file store.
3232
func (c *fileStore) Erase(serverAddress string) error {
33+
if _, exists := c.file.GetAuthConfigs()[serverAddress]; !exists {
34+
// nothing to do; no credentials found for the given serverAddress
35+
return nil
36+
}
3337
delete(c.file.GetAuthConfigs(), serverAddress)
3438
return c.file.Save()
3539
}
@@ -70,9 +74,14 @@ https://docs.docker.com/go/credential-store/
7074
// CLI invocation (no need to warn the user multiple times per command).
7175
var alreadyPrinted atomic.Bool
7276

73-
// Store saves the given credentials in the file store.
77+
// Store saves the given credentials in the file store. This function is
78+
// idempotent and does not update the file if credentials did not change.
7479
func (c *fileStore) Store(authConfig types.AuthConfig) error {
7580
authConfigs := c.file.GetAuthConfigs()
81+
if oldAuthConfig, ok := authConfigs[authConfig.ServerAddress]; ok && oldAuthConfig == authConfig {
82+
// Credentials didn't change, so skip updating the configuration file.
83+
return nil
84+
}
7685
authConfigs[authConfig.ServerAddress] = authConfig
7786
if err := c.file.Save(); err != nil {
7887
return err

0 commit comments

Comments
 (0)