Skip to content
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

Support the platform specific authentication of krane in "auth get" command #1413

Merged
merged 7 commits into from Jul 20, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions .github/workflows/ecr-auth.yaml
Expand Up @@ -39,6 +39,20 @@ jobs:
# List the tags
krane ls ${{ env.AWS_ACCOUNT }}.dkr.ecr.${{ env.AWS_REGION }}.amazonaws.com/go-containerregistry-test

- name: Test krane auth get + ECR
env:
GITHUB_TOKEN: ${{ github.token }}
shell: bash
run: |
CRED1=$(krane auth get ${{ env.AWS_ACCOUNT }}.dkr.ecr.${{ env.AWS_REGION }}.amazonaws.com)
CRED2=$(krane auth get ${{ env.AWS_ACCOUNT }}.dkr.ecr.${{ env.AWS_REGION }}.amazonaws.com)
if [[ "$CRED1" == "" ]] ; then
exit 1
fi
if [[ "$CRED1" == "$CRED2" ]] ; then
echo "credentials are cached by infrastructure"
fi

crane-ecr-login:
runs-on: ubuntu-latest
env:
Expand Down
15 changes: 15 additions & 0 deletions .github/workflows/ghcr-auth.yaml
Expand Up @@ -30,3 +30,18 @@ jobs:
run: |
# List the tags
krane ls ghcr.io/${{ github.repository }}/testimage

- name: Test krane auth get + GHCR
env:
GITHUB_TOKEN: ${{ github.token }}
shell: bash
run: |
CRED1=$(krane auth get ghcr.io)
CRED2=$(krane auth get ghcr.io)
if [[ "$CRED1" == "" ]] ; then
exit 1
fi
if [[ "$CRED1" == "$CRED2" ]] ; then
echo "credentials are cached by infrastructure"
fi

36 changes: 24 additions & 12 deletions cmd/crane/cmd/auth.go
Expand Up @@ -26,19 +26,20 @@ import (
"github.com/docker/cli/cli/config"
"github.com/docker/cli/cli/config/types"
"github.com/google/go-containerregistry/pkg/authn"
"github.com/google/go-containerregistry/pkg/crane"
"github.com/google/go-containerregistry/pkg/name"
"github.com/spf13/cobra"
)

// NewCmdAuth creates a new cobra.Command for the auth subcommand.
func NewCmdAuth(argv ...string) *cobra.Command {
func NewCmdAuth(options []crane.Option, argv ...string) *cobra.Command {
cmd := &cobra.Command{
Use: "auth",
Short: "Log in or access credentials",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, _ []string) error { return cmd.Usage() },
}
cmd.AddCommand(NewCmdAuthGet(argv...), NewCmdAuthLogin(argv...))
cmd.AddCommand(NewCmdAuthGet(options, argv...), NewCmdAuthLogin(argv...))
return cmd
}

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

// NewCmdAuthGet creates a new `crane auth get` command.
func NewCmdAuthGet(argv ...string) *cobra.Command {
func NewCmdAuthGet(options []crane.Option, argv ...string) *cobra.Command {
if len(argv) == 0 {
argv = []string{os.Args[0]}
}

baseCmd := strings.Join(argv, " ")
eg := fmt.Sprintf(` # Read configured credentials for reg.example.com
echo "reg.example.com" | %s get
{"username":"AzureDiamond","password":"hunter2"}`, strings.Join(argv, " "))
$ echo "reg.example.com" | %s get
{"username":"AzureDiamond","password":"hunter2"}
# or
$ %s get reg.example.com
{"username":"AzureDiamond","password":"hunter2"}`, baseCmd, baseCmd)

return &cobra.Command{
Use: "get",
Use: "get [REGISTRY_ADDR]",
Short: "Implements a credential helper",
Example: eg,
Args: cobra.NoArgs,
Args: cobra.MaximumNArgs(1),
RunE: func(_ *cobra.Command, args []string) error {
b, err := ioutil.ReadAll(os.Stdin)
if err != nil {
return err
registryAddr := ""
if len(args) == 1 {
registryAddr = args[0]
} else {
b, err := ioutil.ReadAll(os.Stdin)
if err != nil {
return err
}
registryAddr = strings.TrimSpace(string(b))
}
reg, err := name.NewRegistry(strings.TrimSpace(string(b)))

reg, err := name.NewRegistry(registryAddr)
if err != nil {
return err
}
authorizer, err := authn.DefaultKeychain.Resolve(reg)
authorizer, err := crane.GetOptions(options...).Keychain.Resolve(reg)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/crane/cmd/root.go
Expand Up @@ -93,7 +93,7 @@ func New(use, short string, options []crane.Option) *cobra.Command {

commands := []*cobra.Command{
NewCmdAppend(&options),
NewCmdAuth("crane", "auth"),
NewCmdAuth(options, "crane", "auth"),
NewCmdBlob(&options),
NewCmdCatalog(&options),
NewCmdConfig(&options),
Expand Down
5 changes: 4 additions & 1 deletion cmd/crane/doc/crane_auth_get.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions cmd/gcrane/main.go
Expand Up @@ -38,11 +38,12 @@ const (
)

func main() {
options := []crane.Option{crane.WithAuthFromKeychain(gcrane.Keychain)}
// Same as crane, but override usage and keychain.
root := cmd.New(use, short, []crane.Option{crane.WithAuthFromKeychain(gcrane.Keychain)})
root := cmd.New(use, short, options)

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

// Maintain a map of google-specific commands that we "override".
used := make(map[string]bool)
Expand Down
3 changes: 3 additions & 0 deletions pkg/crane/options.go
Expand Up @@ -29,6 +29,7 @@ type Options struct {
Name []name.Option
Remote []remote.Option
Platform *v1.Platform
Keychain authn.Keychain
}

// GetOptions exposes the underlying []remote.Option, []name.Option, and
Expand All @@ -44,6 +45,7 @@ func makeOptions(opts ...Option) Options {
Remote: []remote.Option{
remote.WithAuthFromKeychain(authn.DefaultKeychain),
},
Keychain: authn.DefaultKeychain,
}
for _, o := range opts {
o(&opt)
Expand Down Expand Up @@ -86,6 +88,7 @@ func WithAuthFromKeychain(keys authn.Keychain) Option {
return func(o *Options) {
// Replace the default keychain at position 0.
o.Remote[0] = remote.WithAuthFromKeychain(keys)
o.Keychain = keys
}
}

Expand Down