Browse Source

feat(aws): Enable setting custom endpoints for AWS ECR for ECRAuthorizationToken generator (#4821)

Signed-off-by: Michael Tweten <mtweten@gmail.com>
Michael Tweten 1 year ago
parent
commit
1947224853
3 changed files with 74 additions and 2 deletions
  1. 2 0
      docs/snippets/provider-aws-access.md
  2. 6 2
      pkg/generator/ecr/ecr.go
  3. 66 0
      pkg/generator/ecr/resolver.go

+ 2 - 0
docs/snippets/provider-aws-access.md

@@ -191,3 +191,5 @@ Use the following environment variables to point the controller to your custom e
 | AWS_SECRETSMANAGER_ENDPOINT | Endpoint for the Secrets Manager Service. The controller uses this endpoint to fetch secrets from AWS Secrets Manager.                                               |
 | AWS_SECRETSMANAGER_ENDPOINT | Endpoint for the Secrets Manager Service. The controller uses this endpoint to fetch secrets from AWS Secrets Manager.                                               |
 | AWS_SSM_ENDPOINT            | Endpoint for the AWS Secure Systems Manager. The controller uses this endpoint to fetch secrets from SSM Parameter Store.                                            |
 | AWS_SSM_ENDPOINT            | Endpoint for the AWS Secure Systems Manager. The controller uses this endpoint to fetch secrets from SSM Parameter Store.                                            |
 | AWS_STS_ENDPOINT            | Endpoint for the Security Token Service. The controller uses this endpoint when creating a session and when doing `assumeRole` or `assumeRoleWithWebIdentity` calls. |
 | AWS_STS_ENDPOINT            | Endpoint for the Security Token Service. The controller uses this endpoint when creating a session and when doing `assumeRole` or `assumeRoleWithWebIdentity` calls. |
+| AWS_ECR_ENDPOINT            | Endpoint for the ECR Service. The controller uses this endpoint to fetch authorization tokens from ECR.                                                              |
+| AWS_ECR_PUBLIC_ENDPOINT     | Endpoint for the Public ECR Service. The controller uses this endpoint to fetch authorization tokens from ECR.                                                       |

+ 6 - 2
pkg/generator/ecr/ecr.go

@@ -155,11 +155,15 @@ type ecrPrivateFactoryFunc func(aws *aws.Config) ecrAPI
 type ecrPublicFactoryFunc func(aws *aws.Config) ecrPublicAPI
 type ecrPublicFactoryFunc func(aws *aws.Config) ecrPublicAPI
 
 
 func ecrPrivateFactory(cfg *aws.Config) ecrAPI {
 func ecrPrivateFactory(cfg *aws.Config) ecrAPI {
-	return ecr.NewFromConfig(*cfg)
+	return ecr.NewFromConfig(*cfg, func(o *ecr.Options) {
+		o.EndpointResolverV2 = ecrCustomEndpointResolver{}
+	})
 }
 }
 
 
 func ecrPublicFactory(cfg *aws.Config) ecrPublicAPI {
 func ecrPublicFactory(cfg *aws.Config) ecrPublicAPI {
-	return ecrpublic.NewFromConfig(*cfg)
+	return ecrpublic.NewFromConfig(*cfg, func(o *ecrpublic.Options) {
+		o.EndpointResolverV2 = ecrPublicCustomEndpointResolver{}
+	})
 }
 }
 
 
 func parseSpec(data []byte) (*genv1alpha1.ECRAuthorizationToken, error) {
 func parseSpec(data []byte) (*genv1alpha1.ECRAuthorizationToken, error) {

+ 66 - 0
pkg/generator/ecr/resolver.go

@@ -0,0 +1,66 @@
+/*
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+	http://www.apache.org/licenses/LICENSE-2.0
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+*/
+
+package ecr
+
+import (
+	"fmt"
+	"github.com/aws/aws-sdk-go-v2/service/ecr"
+	"github.com/aws/aws-sdk-go-v2/service/ecrpublic"
+	smithyendpoints "github.com/aws/smithy-go/endpoints"
+	"net/url"
+	"os"
+)
+
+import (
+	"context"
+)
+
+const (
+	ECREndpointEnv       = "AWS_ECR_ENDPOINT"
+	ECRPublicEndpointEnv = "AWS_ECR_PUBLIC_ENDPOINT"
+)
+
+type ecrCustomEndpointResolver struct{}
+
+// ResolveEndpoint returns a ResolverFunc with
+// customizable endpoints.
+
+func (c ecrCustomEndpointResolver) ResolveEndpoint(ctx context.Context, params ecr.EndpointParameters) (smithyendpoints.Endpoint, error) {
+	endpoint := smithyendpoints.Endpoint{}
+	if v := os.Getenv(ECREndpointEnv); v != "" {
+		url, err := url.Parse(v)
+		if err != nil {
+			return endpoint, fmt.Errorf("failed to parse ecr endpoint %s: %w", v, err)
+		}
+		endpoint.URI = *url
+		return endpoint, nil
+	}
+	defaultResolver := ecr.NewDefaultEndpointResolverV2()
+	return defaultResolver.ResolveEndpoint(ctx, params)
+}
+
+type ecrPublicCustomEndpointResolver struct{}
+
+func (c ecrPublicCustomEndpointResolver) ResolveEndpoint(ctx context.Context, params ecrpublic.EndpointParameters) (smithyendpoints.Endpoint, error) {
+	endpoint := smithyendpoints.Endpoint{}
+	if v := os.Getenv(ECRPublicEndpointEnv); v != "" {
+		url, err := url.Parse(v)
+		if err != nil {
+			return endpoint, fmt.Errorf("failed to parse ecr public endpoint %s: %w", v, err)
+		}
+		endpoint.URI = *url
+		return endpoint, nil
+	}
+	defaultResolver := ecrpublic.NewDefaultEndpointResolverV2()
+	return defaultResolver.ResolveEndpoint(ctx, params)
+}