Skip to content

Instantly share code, notes, and snippets.

@imjasonh
Created Jun 28, 2022
Embed
What would you like to do?
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