Created
June 28, 2022 14:08
-
-
Save imjasonh/5f0053abe38349067935cd7bf59fd4b1 to your computer and use it in GitHub Desktop.
Demonstrating go-containerregistry basic auth keychain
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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