From 02c07122d855bd8fcdb2fd8e2ab1c63320e64755 Mon Sep 17 00:00:00 2001 From: jonjohnsonjr Date: Tue, 5 Jan 2021 13:22:45 -0800 Subject: [PATCH] crane auth get: convert back to credentials (#898) Before, this would just print the AuthConfig that we get back from the config file read. This is often what we want, but it prevents crane from acting as a credential helper itself. By converting back to the credential helper structs, we can use crane as a meta-credential-helper that can multiplex over anything in your config file. --- cmd/crane/cmd/auth.go | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/cmd/crane/cmd/auth.go b/cmd/crane/cmd/auth.go index 0eb4e7b06..4beeb8f34 100644 --- a/cmd/crane/cmd/auth.go +++ b/cmd/crane/cmd/auth.go @@ -44,6 +44,25 @@ func NewCmdAuth(argv ...string) *cobra.Command { return cmd } +type credentials struct { + Username string + Secret string +} + +// https://github.com/docker/cli/blob/2291f610ae73533e6e0749d4ef1e360149b1e46b/cli/config/credentials/native_store.go#L100-L109 +func toCreds(config *authn.AuthConfig) credentials { + creds := credentials{ + Username: config.Username, + Secret: config.Password, + } + + if config.IdentityToken != "" { + creds.Username = "" + creds.Secret = config.IdentityToken + } + return creds +} + // NewCmdAuthGet creates a new `crane auth get` command. func NewCmdAuthGet(argv ...string) *cobra.Command { if len(argv) == 0 { @@ -76,7 +95,11 @@ func NewCmdAuthGet(argv ...string) *cobra.Command { if err != nil { log.Fatal(err) } - if err := json.NewEncoder(os.Stdout).Encode(auth); err != nil { + + // Convert back to a form that credential helpers can parse so that this + // can act as a meta credential helper. + creds := toCreds(auth) + if err := json.NewEncoder(os.Stdout).Encode(creds); err != nil { log.Fatal(err) } },