Skip to content

Instantly share code, notes, and snippets.

@imjasonh
Created June 28, 2022 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save imjasonh/5f0053abe38349067935cd7bf59fd4b1 to your computer and use it in GitHub Desktop.
Save imjasonh/5f0053abe38349067935cd7bf59fd4b1 to your computer and use it in GitHub Desktop.
Demonstrating go-containerregistry basic auth keychain
package main
import (
"fmt"
"log"
"github.com/google/go-containerregistry/pkg/authn"
"github.com/google/go-containerregistry/pkg/crane"
"github.com/google/go-containerregistry/pkg/v1/google"
)
func main() {
keychain := authn.NewMultiKeychain(
basicKeychain{"abc.xyz", "username", "password"},
basicKeychain{"def.xyz", "otheruser", "otherpass"},
google.Keychain, // for gcr.io
)
if err := crane.Copy("abc.xyz/foo/bar", "def.xyz/foo/bar",
crane.WithAuthFromKeychain(keychain)); err != nil {
log.Fatal(err)
}
}
type basicKeychain struct{ registry, username, password string }
func (b basicKeychain) Resolve(r authn.Resource) (authn.Authenticator, error) {
if r.RegistryStr() != b.registry {
return nil, fmt.Errorf("basicKeychain: only %q is supported", b.registry)
}
return b, nil
}
func (b basicKeychain) Authorization() (*authn.AuthConfig, error) {
return &authn.AuthConfig{
Username: b.username,
Password: b.password,
}, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment