Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pkg/authn/{amazon,azure,google} #1231

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions cmd/krane/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ module github.com/google/go-containerregistry/cmd/krane
go 1.17

replace (
github.com/google/go-containerregistry => ../..
github.com/google/go-containerregistry/pkg/authn/k8schain => ../../pkg/authn/k8schain
github.com/google/go-containerregistry => ../../
github.com/google/go-containerregistry/pkg/authn/k8schain => ../../pkg/authn/k8schain/
)

require (
github.com/google/go-containerregistry v0.7.0
github.com/google/go-containerregistry v0.8.0
github.com/google/go-containerregistry/pkg/authn/k8schain v0.0.0-20211223213658-2874338840a6
)

Expand Down
15 changes: 14 additions & 1 deletion hack/presubmit.sh
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,20 @@ mkdir -p /tmp/gendoc && go run cmd/crane/help/main.go --dir /tmp/gendoc && diff
go test ./...
./pkg/name/internal/must_test.sh

# Check that each of the sub-Go-modules build and pass tests (if any)

pushd ${PROJECT_ROOT}/pkg/authn/k8schain
trap popd EXIT

go test ./...

pushd ${PROJECT_ROOT}/cmd/krane
trap popd EXIT
go build ./...

pushd ${PROJECT_ROOT}/pkg/authn/amazon
trap popd EXIT
go build ./...

pushd ${PROJECT_ROOT}/pkg/authn/azure
trap popd EXIT
go build ./...
51 changes: 5 additions & 46 deletions pkg/authn/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,52 +53,11 @@ If those are not found, `DefaultKeychain` will look for credentials configured u
[`pkg/v1/google.Keychain`](https://pkg.go.dev/github.com/google/go-containerregistry/pkg/v1/google#Keychain) provides a `Keychain` implementation that emulates [`docker-credential-gcr`](https://github.com/GoogleCloudPlatform/docker-credential-gcr) to find credentials in the environment.
See [`google.NewEnvAuthenticator`](https://pkg.go.dev/github.com/google/go-containerregistry/pkg/v1/google#NewEnvAuthenticator) and [`google.NewGcloudAuthenticator`](https://pkg.go.dev/github.com/google/go-containerregistry/pkg/v1/google#NewGcloudAuthenticator) for more information.

To emulate other credential helpers without requiring them to be available as executables, [`NewKeychainFromHelper`](https://pkg.go.dev/github.com/google/go-containerregistry/pkg/authn#NewKeychainFromHelper) provides an adapter that takes a Go implementation satisfying a subset of the [`credentials.Helper`](https://pkg.go.dev/github.com/docker/docker-credential-helpers/credentials#Helper) interface, and makes it available as a `Keychain`.

This means that you can emulate, for example, [Amazon ECR's `docker-credential-ecr-login` credential helper](https://github.com/awslabs/amazon-ecr-credential-helper) using the same implementation:

```go
import (
ecr "github.com/awslabs/amazon-ecr-credential-helper/ecr-login"
"github.com/awslabs/amazon-ecr-credential-helper/ecr-login/api"

"github.com/google/go-containerregistry/pkg/authn"
"github.com/google/go-containerregistry/pkg/v1/remote"
)

func main() {
// ...
ecrHelper := ecr.ECRHelper{ClientFactory: api.DefaultClientFactory()}
img, err := remote.Get(ref, remote.WithAuthFromKeychain(authn.NewKeychainFromHelper(ecrHelper)))
if err != nil {
panic(err)
}
// ...
}
```
[`pkg/authn/amazon.Keychain`](https://pkg.go.dev/github.com/google/go-containerregistry/pkg/authn/amazon#Keychain) provides a `Keychain` implementation that emulates [Amazon ECR's `docker-credential-ecr-login` credential helper](https://github.com/awslabs/amazon-ecr-credential-helper).

Likewise, you can emulate [Azure's ACR `docker-credential-acr-env` credential helper](https://github.com/chrismellard/docker-credential-acr-env):
[`pkg/authn/azure.Keychain`](https://pkg.go.dev/github.com/google/go-containerregistry/pkg/authn/azure#Keychain) provides a `Keychain` implementation that emulates [Azure's ACR `docker-credential-acr-env` credential helper](https://github.com/chrismellard/docker-credential-acr-env).

```go
import (
"github.com/chrismellard/docker-credential-acr-env/pkg/credhelper"

"github.com/google/go-containerregistry/pkg/authn"
"github.com/google/go-containerregistry/pkg/v1/remote"
)

func main() {
// ...
acrHelper := credhelper.NewACRCredentialsHelper()
img, err := remote.Get(ref, remote.WithAuthFromKeychain(authn.NewKeychainFromHelper(acrHelper)))
if err != nil {
panic(err)
}
// ...
}
```

<!-- TODO(jasonhall): Wrap these in docker-credential-magic and reference those from here. -->
To emulate other credential helpers without requiring them to be available as executables, [`NewKeychainFromHelper`](https://pkg.go.dev/github.com/google/go-containerregistry/pkg/authn#NewKeychainFromHelper) provides an adapter that takes a Go implementation satisfying a subset of the [`credentials.Helper`](https://pkg.go.dev/github.com/docker/docker-credential-helpers/credentials#Helper) interface, and makes it available as a `Keychain`.

## Using Multiple `Keychain`s

Expand All @@ -110,8 +69,8 @@ For example:
kc := authn.NewMultiKeychain(
authn.DefaultKeychain,
google.Keychain,
authn.NewFromHelper(ecr.ECRHelper{ClientFactory: api.DefaultClientFactory{}}),
authn.NewFromHelper(acr.ACRCredHelper{}),
amazon.Keychain,
azure.Keychain,
)
```

Expand Down
32 changes: 32 additions & 0 deletions pkg/authn/amazon/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
module github.com/google/go-containerregistry/pkg/authn/amazon

go 1.17

require (
github.com/awslabs/amazon-ecr-credential-helper/ecr-login v0.0.0-20211027214941-f15886b5ccdc
github.com/google/go-containerregistry v0.8.0
)

replace github.com/google/go-containerregistry => ../../../

require (
github.com/aws/aws-sdk-go-v2 v1.7.1 // indirect
github.com/aws/aws-sdk-go-v2/config v1.5.0 // indirect
github.com/aws/aws-sdk-go-v2/credentials v1.3.1 // indirect
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.3.0 // indirect
github.com/aws/aws-sdk-go-v2/internal/ini v1.1.1 // indirect
github.com/aws/aws-sdk-go-v2/service/ecr v1.4.1 // indirect
github.com/aws/aws-sdk-go-v2/service/ecrpublic v1.4.1 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.2.1 // indirect
github.com/aws/aws-sdk-go-v2/service/sso v1.3.1 // indirect
github.com/aws/aws-sdk-go-v2/service/sts v1.6.0 // indirect
github.com/aws/smithy-go v1.6.0 // indirect
github.com/docker/cli v20.10.12+incompatible // indirect
github.com/docker/docker v20.10.12+incompatible // indirect
github.com/docker/docker-credential-helpers v0.6.4 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/sirupsen/logrus v1.8.1 // indirect
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e // indirect
)
Loading