Skip to content

Commit

Permalink
crane auth get: convert back to credentials (#898)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jonjohnsonjr committed Jan 5, 2021
1 parent 3584fa0 commit 02c0712
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion cmd/crane/cmd/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "<token>"
creds.Secret = config.IdentityToken
}
return creds
}

// NewCmdAuthGet creates a new `crane auth get` command.
func NewCmdAuthGet(argv ...string) *cobra.Command {
if len(argv) == 0 {
Expand Down Expand Up @@ -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)
}
},
Expand Down

0 comments on commit 02c0712

Please sign in to comment.