-
Notifications
You must be signed in to change notification settings - Fork 384
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
Open
matejvasek
wants to merge
8
commits into
containers:main
Choose a base branch
from
matejvasek:creds-store-fix
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e12be70
Use credstore form docker config
matejvasek 1045cb0
Fix test reading non-testing config
matejvasek f448235
Add test for credStore
matejvasek 7ed9a4b
Fix style
matejvasek ac05c2c
revert withTmpHome
matejvasek 174d302
fixup
matejvasek 7cac62b
add tests
matejvasek e5c4f94
more error handling
matejvasek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,16 @@ func GetAllCredentials(sys *types.SystemContext) (map[string]types.DockerAuthCon | |
for registry := range auths.AuthConfigs { | ||
addRegistry(registry) | ||
} | ||
if auths.CredsStore != "" { | ||
creds, err := listAuthsFromCredHelper(auths.CredsStore) | ||
if err == nil { | ||
for registry := range creds { | ||
addRegistry(registry) | ||
} | ||
} else { | ||
return nil, err | ||
} | ||
} | ||
} | ||
// External helpers. | ||
default: | ||
|
@@ -390,6 +404,21 @@ func RemoveAuthentication(sys *types.SystemContext, key string) error { | |
if innerHelper, exists := auths.CredHelpers[key]; exists { | ||
removeFromCredHelper(innerHelper) | ||
} | ||
if auths.CredsStore != "" { | ||
c, err := getAuthFromCredHelper(auths.CredsStore, key) | ||
if err != nil { | ||
multiErr = multierror.Append(multiErr, errors.Wrapf(err, "removing credentials for %s from credential helper %s", key, auths.CredsStore)) | ||
} else { | ||
if c.Username != "" || c.IdentityToken != "" { | ||
err = deleteAuthFromCredHelper(auths.CredsStore, key) | ||
if err != nil { | ||
multiErr = multierror.Append(multiErr, errors.Wrapf(err, "removing credentials for %s from credential helper %s", key, auths.CredsStore)) | ||
} else { | ||
isLoggedIn = true | ||
} | ||
} | ||
} | ||
} | ||
if _, ok := auths.AuthConfigs[key]; ok { | ||
isLoggedIn = true | ||
delete(auths.AuthConfigs, key) | ||
|
@@ -440,6 +469,20 @@ 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.
|
||
creds, err := listAuthsFromCredHelper(auths.CredsStore) | ||
if err == nil { | ||
for registry := range creds { | ||
err = deleteAuthFromCredHelper(auths.CredsStore, registry) | ||
if err != nil { | ||
return false, err | ||
} | ||
} | ||
} else { | ||
return false, err | ||
} | ||
|
||
} | ||
return true, nil | ||
}) | ||
// External helpers. | ||
|
@@ -646,6 +689,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.) | ||
|
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,3 @@ | ||
#!/usr/bin/env sh | ||
|
||
exec /usr/bin/env go run ./testdata/docker-credential-mock-helper.go "$@" |
126 changes: 126 additions & 0 deletions
126
pkg/docker/config/testdata/docker-credential-mock-helper.go
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,126 @@ | ||
///usr/bin/true; exec /usr/bin/env go run "$0" "$@" | ||
|
||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"github.com/pkg/errors" | ||
"io/ioutil" | ||
"os" | ||
"strings" | ||
"syscall" | ||
) | ||
|
||
type userPasswordPair struct { | ||
User, Password string | ||
} | ||
|
||
type serverUserSecretTriple struct { | ||
ServerURL, Username, Secret string | ||
} | ||
|
||
func load() (map[string]userPasswordPair, error) { | ||
credentials := make(map[string]userPasswordPair) | ||
fname, ok := os.LookupEnv("CRED_HELPER_STORE_FILE") | ||
if !ok { | ||
return credentials, fmt.Errorf("the CRED_HELPER_STORE_FILE envvar not set") | ||
} | ||
f, err := os.OpenFile(fname, os.O_RDONLY, 0644) | ||
if err != nil { | ||
return credentials, err | ||
} | ||
defer f.Close() | ||
dec := json.NewDecoder(f) | ||
err = dec.Decode(&credentials) | ||
return credentials, err | ||
} | ||
|
||
func store(credentials map[string]userPasswordPair) error { | ||
fname, ok := os.LookupEnv("CRED_HELPER_STORE_FILE") | ||
if !ok { | ||
return fmt.Errorf("the CRED_HELPER_STORE_FILE envvar not set") | ||
} | ||
f, err := os.OpenFile(fname, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644) | ||
if err != nil { | ||
return err | ||
} | ||
defer f.Close() | ||
enc := json.NewEncoder(f) | ||
return enc.Encode(&credentials) | ||
|
||
} | ||
|
||
func fatal(err error) { | ||
fmt.Fprintf(os.Stderr, "error: %v\n", err) | ||
var se syscall.Errno | ||
if ok := errors.As(err, &se); ok { | ||
os.Exit(int(se)) | ||
} | ||
os.Exit(-1) | ||
} | ||
|
||
func main() { | ||
if len(os.Args) != 2 { | ||
fmt.Fprintf(os.Stderr, "exactly one argument expected\n") | ||
os.Exit(int(syscall.EINVAL)) | ||
} | ||
|
||
credentials, err := load() | ||
if err != nil { | ||
fatal(err) | ||
} | ||
|
||
switch os.Args[1] { | ||
case "list": | ||
outData := make(map[string]string) | ||
for server, up := range credentials { | ||
outData[server] = up.User | ||
} | ||
enc := json.NewEncoder(os.Stdout) | ||
err = enc.Encode(&outData) | ||
if err != nil { | ||
fatal(err) | ||
} | ||
case "get": | ||
inData, err := ioutil.ReadAll(os.Stdin) | ||
if err != nil { | ||
fatal(err) | ||
} | ||
serverURL := string(inData) | ||
serverURL = strings.Trim(serverURL, "\r\n") | ||
up := credentials[serverURL] | ||
outData := serverUserSecretTriple{ServerURL: serverURL, Username: up.User, Secret: up.Password} | ||
enc := json.NewEncoder(os.Stdout) | ||
if err = enc.Encode(outData); err != nil { | ||
fatal(err) | ||
} | ||
case "erase": | ||
inData, err := ioutil.ReadAll(os.Stdin) | ||
if err != nil { | ||
fatal(err) | ||
} | ||
serverURL := string(inData) | ||
serverURL = strings.Trim(serverURL, "\r\n") | ||
delete(credentials, serverURL) | ||
err = store(credentials) | ||
if err != nil { | ||
fatal(err) | ||
} | ||
case "store": | ||
inData := serverUserSecretTriple{} | ||
enc := json.NewDecoder(os.Stdin) | ||
err = enc.Decode(&inData) | ||
if err != nil { | ||
fatal(err) | ||
} | ||
credentials[inData.ServerURL] = userPasswordPair{User: inData.Username, Password: inData.Secret} | ||
err = store(credentials) | ||
if err != nil { | ||
fatal(err) | ||
} | ||
default: | ||
fmt.Fprintf(os.Stderr, "unknown sub-command %s\n", os.Args[1]) | ||
os.Exit(int(syscall.EINVAL)) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.)