refactor: make common authn function

This commit is contained in:
mrjoelkamp
2024-06-18 11:55:30 -05:00
parent ff38975c76
commit 08e823e05b
6 changed files with 42 additions and 74 deletions

View File

@@ -5,15 +5,12 @@ import (
"log"
"os"
ecr "github.com/awslabs/amazon-ecr-credential-helper/ecr-login"
acr "github.com/chrismellard/docker-credential-acr-env/pkg/credhelper"
"github.com/docker/attest/internal/embed"
"github.com/docker/attest/pkg/oci"
"github.com/docker/attest/pkg/tuf"
"github.com/google/go-containerregistry/pkg/authn"
"github.com/google/go-containerregistry/pkg/name"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/empty"
"github.com/google/go-containerregistry/pkg/v1/google"
"github.com/google/go-containerregistry/pkg/v1/layout"
"github.com/google/go-containerregistry/pkg/v1/remote"
)
@@ -34,15 +31,9 @@ func PushImageToRegistry(image v1.Image, imageName string) error {
if err != nil {
log.Fatalf("Failed to parse image name: %v", err)
}
// Create a multi-keychain that will use the default Docker, Google, ECR or ACR keychain
keychain := authn.NewMultiKeychain(
authn.DefaultKeychain,
google.Keychain,
authn.NewKeychainFromHelper(ecr.NewECRHelper()),
authn.NewKeychainFromHelper(acr.NewACRCredentialsHelper()),
)
// Push the image to the registry
return remote.Write(ref, image, remote.WithAuthFromKeychain(keychain))
return remote.Write(ref, image, oci.MultiKeychainOption())
}
func PushIndexToRegistry(image v1.ImageIndex, imageName string) error {
@@ -51,15 +42,8 @@ func PushIndexToRegistry(image v1.ImageIndex, imageName string) error {
if err != nil {
log.Fatalf("Failed to parse image name: %v", err)
}
// Create a multi-keychain that will use the default Docker, Google, ECR or ACR keychain
keychain := authn.NewMultiKeychain(
authn.DefaultKeychain,
google.Keychain,
authn.NewKeychainFromHelper(ecr.NewECRHelper()),
authn.NewKeychainFromHelper(acr.NewACRCredentialsHelper()),
)
// Push the index to the registry
return remote.WriteIndex(ref, image, remote.WithAuthFromKeychain(keychain))
return remote.WriteIndex(ref, image, oci.MultiKeychainOption())
}
func SaveImageAsOCILayout(image v1.Image, path string) error {