-
Notifications
You must be signed in to change notification settings - Fork 386
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement credsStore in credential config files #1179
base: main
Are you sure you want to change the base?
Changes from 4 commits
e12be70
1045cb0
f448235
7ed9a4b
ac05c2c
174d302
7cac62b
e5c4f94
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,6 +30,7 @@ type dockerAuthConfig struct { | |
type dockerConfigFile struct { | ||
AuthConfigs map[string]dockerAuthConfig `json:"auths"` | ||
CredHelpers map[string]string `json:"credHelpers,omitempty"` | ||
CredsStore string `json:"credsStore,omitempty"` | ||
} | ||
|
||
type authPath struct { | ||
|
@@ -85,6 +86,9 @@ func SetCredentials(sys *types.SystemContext, key, username, password string) (s | |
} | ||
return false, setAuthToCredHelper(ch, key, username, password) | ||
} | ||
if auths.CredsStore != "" { | ||
return false, setAuthToCredHelper(auths.CredsStore, key, username, password) | ||
} | ||
creds := base64.StdEncoding.EncodeToString([]byte(username + ":" + password)) | ||
newCreds := dockerAuthConfig{Auth: creds} | ||
auths.AuthConfigs[key] = newCreds | ||
|
@@ -156,6 +160,13 @@ func GetAllCredentials(sys *types.SystemContext) (map[string]types.DockerAuthCon | |
for registry := range auths.AuthConfigs { | ||
addRegistry(registry) | ||
} | ||
if auths.CredsStore != "" { | ||
if creds, err := listAuthsFromCredHelper(auths.CredsStore); err == nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is it OK to completely ignore errors here? I’d expect this to work similarly to a helper returned by |
||
for registry := range creds { | ||
addRegistry(registry) | ||
} | ||
} | ||
} | ||
} | ||
// External helpers. | ||
default: | ||
|
@@ -440,6 +451,14 @@ func RemoveAllAuthentication(sys *types.SystemContext) error { | |
} | ||
auths.CredHelpers = make(map[string]string) | ||
auths.AuthConfigs = make(map[string]dockerAuthConfig) | ||
if auths.CredsStore != "" { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
if creds, err := listAuthsFromCredHelper(auths.CredsStore); err == nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is it OK to completely ignore errors here? I’d expect this to work similarly to a helper returned by |
||
for registry := range creds { | ||
_ = deleteAuthFromCredHelper(auths.CredsStore, registry) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here. |
||
} | ||
} | ||
|
||
} | ||
return true, nil | ||
}) | ||
// External helpers. | ||
|
@@ -646,6 +665,12 @@ func findAuthentication(ref reference.Named, registry, path string, legacyFormat | |
return getAuthFromCredHelper(ch, registry) | ||
} | ||
|
||
if auths.CredsStore != "" { | ||
if cred, err := getAuthFromCredHelper(auths.CredsStore, registry); err == nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is it OK to completely ignore errors here? |
||
return cred, err | ||
} | ||
} | ||
|
||
// Support for different paths in auth. | ||
// (This is not a feature of ~/.docker/config.json; we support it even for | ||
// those files as an extension.) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -559,6 +559,7 @@ func TestGetAuthFailsOnBadInput(t *testing.T) { | |
} | ||
|
||
func TestGetAllCredentials(t *testing.T) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add tests for the other modified functions as well. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Are those functions properly tested right now anyway? I believe they are not tested with respect to helpers right now. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
For instance the mock helper does noting for the store operation so you can really assert it written anything when calling the |
||
defer withTmpHome(t)() | ||
// Create a temporary authentication file. | ||
tmpFile, err := ioutil.TempFile("", "auth.json.") | ||
require.NoError(t, err) | ||
|
@@ -636,6 +637,42 @@ func TestGetAllCredentials(t *testing.T) { | |
require.Equal(t, d.password, conf.Password, "%v", d) | ||
} | ||
|
||
// test "credStore" | ||
f, err := os.OpenFile(authFilePath, os.O_RDWR|os.O_TRUNC, 0644) | ||
require.NoError(t, err) | ||
_, err = f.Write([]byte(`{ "credsStore": "helper-registry" }`)) | ||
require.NoError(t, err) | ||
err = f.Close() | ||
require.NoError(t, err) | ||
sys = types.SystemContext{ | ||
AuthFilePath: authFilePath, | ||
SystemRegistriesConfPath: "", | ||
SystemRegistriesConfDirPath: "", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is (sadly?) the same as leaving the values unset, in particular it doesn’t disable any ordinary users’ configuration. So if this data matters, it should point at real files. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @mtrmac Do I need to set it? I think that for this test |
||
} | ||
authConfigs, err = GetAllCredentials(&sys) | ||
require.NoError(t, err) | ||
require.Equal(t, 1, len(authConfigs)) | ||
require.Equal(t, "foo", authConfigs["registry-a.com"].Username) | ||
require.Equal(t, "bar", authConfigs["registry-a.com"].Password) | ||
} | ||
|
||
func withTmpHome(t *testing.T) func() { | ||
t.Helper() | ||
dir, err := ioutil.TempDir("", "test-home") | ||
if err != nil { | ||
t.Fatal(err) | ||
return func() {} | ||
} | ||
oldHome, oldHomeExists := os.LookupEnv("HOME") | ||
os.Setenv("HOME", dir) | ||
return func() { | ||
if oldHomeExists { | ||
os.Setenv("HOME", oldHome) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don’t immediately see that this is necessary (the existing So, to make some of the code testable, we had to introduce There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @mtrmac without this the test is failing on my machine because the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I you don't have There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I don't what to do about this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The test is touching not only testing config files but also actual user config files, that's not good. |
||
} else { | ||
os.Unsetenv("HOME") | ||
} | ||
os.RemoveAll(dir) | ||
} | ||
} | ||
|
||
func TestAuthKeysForRef(t *testing.T) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please keep the order of handling
CredHelpers
/CredsStore
/AuthConfigs
consistent throughout the package, to make it easier to check that all operations handle the same data. (I guess in the order above, unless there’s a specific reason to do something else.)