Skip to content

Commit 5749ee6

Browse files
authored
Support the platform specific authentication of krane in "auth get" command (#1413)
* Support the platform specific authentication of krane in "auth get" command * Add tests for "krane auth get" * Update the doc * Use gcrane.Keychain in gcrane * Fix misaligned doc * Remove a space * Remove unused environment variable from the ECR test
1 parent 31786c6 commit 5749ee6

File tree

7 files changed

+63
-17
lines changed

7 files changed

+63
-17
lines changed

.github/workflows/ecr-auth.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,18 @@ jobs:
3939
# List the tags
4040
krane ls ${{ env.AWS_ACCOUNT }}.dkr.ecr.${{ env.AWS_REGION }}.amazonaws.com/go-containerregistry-test
4141
42+
- name: Test krane auth get + ECR
43+
shell: bash
44+
run: |
45+
CRED1=$(krane auth get ${{ env.AWS_ACCOUNT }}.dkr.ecr.${{ env.AWS_REGION }}.amazonaws.com)
46+
CRED2=$(krane auth get ${{ env.AWS_ACCOUNT }}.dkr.ecr.${{ env.AWS_REGION }}.amazonaws.com)
47+
if [[ "$CRED1" == "" ]] ; then
48+
exit 1
49+
fi
50+
if [[ "$CRED1" == "$CRED2" ]] ; then
51+
echo "credentials are cached by infrastructure"
52+
fi
53+
4254
crane-ecr-login:
4355
runs-on: ubuntu-latest
4456
env:

.github/workflows/ghcr-auth.yaml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,18 @@ jobs:
3030
run: |
3131
# List the tags
3232
krane ls ghcr.io/${{ github.repository }}/testimage
33+
34+
- name: Test krane auth get + GHCR
35+
env:
36+
GITHUB_TOKEN: ${{ github.token }}
37+
shell: bash
38+
run: |
39+
CRED1=$(krane auth get ghcr.io)
40+
CRED2=$(krane auth get ghcr.io)
41+
if [[ "$CRED1" == "" ]] ; then
42+
exit 1
43+
fi
44+
if [[ "$CRED1" == "$CRED2" ]] ; then
45+
echo "credentials are cached by infrastructure"
46+
fi
47+

cmd/crane/cmd/auth.go

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,20 @@ import (
2626
"github.com/docker/cli/cli/config"
2727
"github.com/docker/cli/cli/config/types"
2828
"github.com/google/go-containerregistry/pkg/authn"
29+
"github.com/google/go-containerregistry/pkg/crane"
2930
"github.com/google/go-containerregistry/pkg/name"
3031
"github.com/spf13/cobra"
3132
)
3233

3334
// NewCmdAuth creates a new cobra.Command for the auth subcommand.
34-
func NewCmdAuth(argv ...string) *cobra.Command {
35+
func NewCmdAuth(options []crane.Option, argv ...string) *cobra.Command {
3536
cmd := &cobra.Command{
3637
Use: "auth",
3738
Short: "Log in or access credentials",
3839
Args: cobra.NoArgs,
3940
RunE: func(cmd *cobra.Command, _ []string) error { return cmd.Usage() },
4041
}
41-
cmd.AddCommand(NewCmdAuthGet(argv...), NewCmdAuthLogin(argv...))
42+
cmd.AddCommand(NewCmdAuthGet(options, argv...), NewCmdAuthLogin(argv...))
4243
return cmd
4344
}
4445

@@ -62,30 +63,41 @@ func toCreds(config *authn.AuthConfig) credentials {
6263
}
6364

6465
// NewCmdAuthGet creates a new `crane auth get` command.
65-
func NewCmdAuthGet(argv ...string) *cobra.Command {
66+
func NewCmdAuthGet(options []crane.Option, argv ...string) *cobra.Command {
6667
if len(argv) == 0 {
6768
argv = []string{os.Args[0]}
6869
}
6970

71+
baseCmd := strings.Join(argv, " ")
7072
eg := fmt.Sprintf(` # Read configured credentials for reg.example.com
71-
echo "reg.example.com" | %s get
72-
{"username":"AzureDiamond","password":"hunter2"}`, strings.Join(argv, " "))
73+
$ echo "reg.example.com" | %s get
74+
{"username":"AzureDiamond","password":"hunter2"}
75+
# or
76+
$ %s get reg.example.com
77+
{"username":"AzureDiamond","password":"hunter2"}`, baseCmd, baseCmd)
7378

7479
return &cobra.Command{
75-
Use: "get",
80+
Use: "get [REGISTRY_ADDR]",
7681
Short: "Implements a credential helper",
7782
Example: eg,
78-
Args: cobra.NoArgs,
83+
Args: cobra.MaximumNArgs(1),
7984
RunE: func(_ *cobra.Command, args []string) error {
80-
b, err := ioutil.ReadAll(os.Stdin)
81-
if err != nil {
82-
return err
85+
registryAddr := ""
86+
if len(args) == 1 {
87+
registryAddr = args[0]
88+
} else {
89+
b, err := ioutil.ReadAll(os.Stdin)
90+
if err != nil {
91+
return err
92+
}
93+
registryAddr = strings.TrimSpace(string(b))
8394
}
84-
reg, err := name.NewRegistry(strings.TrimSpace(string(b)))
95+
96+
reg, err := name.NewRegistry(registryAddr)
8597
if err != nil {
8698
return err
8799
}
88-
authorizer, err := authn.DefaultKeychain.Resolve(reg)
100+
authorizer, err := crane.GetOptions(options...).Keychain.Resolve(reg)
89101
if err != nil {
90102
return err
91103
}

cmd/crane/cmd/root.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ func New(use, short string, options []crane.Option) *cobra.Command {
9393

9494
commands := []*cobra.Command{
9595
NewCmdAppend(&options),
96-
NewCmdAuth("crane", "auth"),
96+
NewCmdAuth(options, "crane", "auth"),
9797
NewCmdBlob(&options),
9898
NewCmdCatalog(&options),
9999
NewCmdConfig(&options),

cmd/crane/doc/crane_auth_get.md

Lines changed: 5 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/gcrane/main.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,12 @@ const (
3838
)
3939

4040
func main() {
41+
options := []crane.Option{crane.WithAuthFromKeychain(gcrane.Keychain)}
4142
// Same as crane, but override usage and keychain.
42-
root := cmd.New(use, short, []crane.Option{crane.WithAuthFromKeychain(gcrane.Keychain)})
43+
root := cmd.New(use, short, options)
4344

4445
// Add or override commands.
45-
gcraneCmds := []*cobra.Command{gcmd.NewCmdList(), gcmd.NewCmdGc(), gcmd.NewCmdCopy(), cmd.NewCmdAuth("gcrane", "auth")}
46+
gcraneCmds := []*cobra.Command{gcmd.NewCmdList(), gcmd.NewCmdGc(), gcmd.NewCmdCopy(), cmd.NewCmdAuth(options, "gcrane", "auth")}
4647

4748
// Maintain a map of google-specific commands that we "override".
4849
used := make(map[string]bool)

pkg/crane/options.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ type Options struct {
2929
Name []name.Option
3030
Remote []remote.Option
3131
Platform *v1.Platform
32+
Keychain authn.Keychain
3233
}
3334

3435
// GetOptions exposes the underlying []remote.Option, []name.Option, and
@@ -44,6 +45,7 @@ func makeOptions(opts ...Option) Options {
4445
Remote: []remote.Option{
4546
remote.WithAuthFromKeychain(authn.DefaultKeychain),
4647
},
48+
Keychain: authn.DefaultKeychain,
4749
}
4850
for _, o := range opts {
4951
o(&opt)
@@ -86,6 +88,7 @@ func WithAuthFromKeychain(keys authn.Keychain) Option {
8688
return func(o *Options) {
8789
// Replace the default keychain at position 0.
8890
o.Remote[0] = remote.WithAuthFromKeychain(keys)
91+
o.Keychain = keys
8992
}
9093
}
9194

0 commit comments

Comments
 (0)