kubernetes / cloud-provider-aws

Cloud provider for AWS
https://cloud-provider-aws.sigs.k8s.io/
Apache License 2.0
395 stars 302 forks source link

Private ECR repository contains "public.ecr.aws" breaks the GetCredentials #651

Closed YanshuoH closed 1 year ago

YanshuoH commented 1 year ago

What happened:

In our company, we intend to do image copy from public repository to our private ECR repository, just because we don't want to encounter network issue that may cause the image pulling to be slow/failed.

Currently our naming convention of the repository looks like: $ecr_repository_url$public_repository_name:$image_tag.

A concrete example would be, for image public.ecr.aws/aws-observability/aws-for-fluent-bit:2.31.12, we'll do a replication as 112233445566.dkr.ecr.cn-northwest-1.amazonaws.com.cn/public.ecr.aws/aws-observability/aws-for-fluent-bit:2.31.12

So as for kubelet's image pulling, we're facing Failed getting credential from external registry credential provider: error execing credential provider plugin ecr-credential-provider for image 112233445566.dkr.ecr.cn-northwest-1.amazonaws.com.cn/public.ecr.aws/aws-observability/aws-for-fluent-bit: exit status 1.

The above error results to image pulling failed with 401 Unauthorized issue.

After digging the source code, I've noticed that the ecrPlugin.GetCredentials uses a keyword public.ecr.aws to determine if the given image is a public repository or not.

Thus in our case, a credential request was falsely made to publilc registry instead of private registry.

Screen Shot 2023-09-04 at 20 19 27

What you expected to happen:

Even with the keyword public.ecr.aws present in the image url, the ecr-credential-provider shall still be able to determin if the image is in private registry or not.

How to reproduce it (as minimally and precisely as possible):

Create a private repository and image with keyword public.ecr.aws, for example, 112233445566.dkr.ecr.cn-northwest-1.amazonaws.com.cn/public.ecr.aws/aws-observability/aws-for-fluent-bit:2.31.12.

Then invoke directly the ecr-credential-provider with:

echo '{"kind":"CredentialProviderRequest","apiVersion":"credentialprovider.kubelet.k8s.io/v1","image":"112233445566.dkr.ecr.cn-northwest-1.amazonaws.com.cn/public.ecr.aws/aws-observability/aws-for-fluent-bit:2.31.12"}' | /etc/eks/image-credential-provider/ecr-credential-provider
I0904 12:24:55.935639   30684 main.go:95] Getting creds for public registry
E0904 12:24:57.143893   30684 main.go:261] Error running credential provider plugin: UnrecognizedClientException: The security token included in the request is invalid
    status code: 400, request id: 05cad298-32ce-4775-85b2-d23ce746f26e

For comparaision, use another repository and image, 112233445566.dkr.ecr.cn-northwest-1.amazonaws.com.cn/aws-observability/aws-for-fluent-bit:2.31.12:

echo '{"kind":"CredentialProviderRequest","apiVersion":"credentialprovider.kubelet.k8s.io/v1","image":"112233445566.dkr.ecr.cn-northwest-1.amazonaws.com.cn/aws-observability/aws-for-fluent-bit:2.31.12"}' | /etc/eks/image-credential-provider/ecr-credential-provider
I0904 12:26:23.827086   31239 main.go:126] Getting creds for private registry 112233445566.dkr.ecr.cn-northwest-1.amazonaws.com.cn/aws-observability/aws-for-fluent-bit:2.31.12
{"kind":"CredentialProviderResponse","apiVersion":"credentialprovider.kubelet.k8s.io/v1","cacheKeyType":"Registry","cacheDuration":"6h0m0s","auth":{"112233445566.dkr.ecr.cn-northwest-1.amazonaws.com.cn":{"username":"AWS","password":"REDACTED"}}}

Anything else we need to know?:

Nein.

Environment:

/kind bug

cartermckinnon commented 1 year ago

/triage accepted