Skip to content

Commit

Permalink
Add auth options to gcrane (#1021)
Browse files Browse the repository at this point in the history
This allows overriding the use of gcrane.Keychain
  • Loading branch information
jonjohnsonjr committed May 19, 2021
1 parent 426caf7 commit 8b535fa
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions pkg/gcrane/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"context"
"runtime"

"github.com/google/go-containerregistry/pkg/authn"
"github.com/google/go-containerregistry/pkg/crane"
"github.com/google/go-containerregistry/pkg/v1/google"
"github.com/google/go-containerregistry/pkg/v1/remote"
Expand Down Expand Up @@ -81,3 +82,30 @@ func WithContext(ctx context.Context) Option {
o.crane = append(o.crane, crane.WithContext(ctx))
}
}

// WithKeychain is a functional option for overriding the default
// authenticator for remote operations, using an authn.Keychain to find
// credentials.
//
// By default, gcrane will use gcrane.Keychain.
func WithKeychain(keys authn.Keychain) Option {
return func(o *options) {
// Replace the default keychain at position 0.
o.remote[0] = remote.WithAuthFromKeychain(keys)
o.google[0] = google.WithAuthFromKeychain(keys)
o.crane[0] = crane.WithAuthFromKeychain(keys)
}
}

// WithAuth is a functional option for overriding the default authenticator
// for remote operations.
//
// By default, gcrane will use gcrane.Keychain.
func WithAuth(auth authn.Authenticator) Option {
return func(o *options) {
// Replace the default keychain at position 0.
o.remote[0] = remote.WithAuth(auth)
o.google[0] = google.WithAuth(auth)
o.crane[0] = crane.WithAuth(auth)
}
}

0 comments on commit 8b535fa

Please sign in to comment.