|
| 1 | +// Copyright (c) HashiCorp, Inc. |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | + |
| 4 | +package ecr |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "fmt" |
| 9 | + "strings" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/aws/aws-sdk-go-v2/aws" |
| 13 | + "github.com/aws/aws-sdk-go-v2/service/ecr" |
| 14 | + "github.com/hashicorp/terraform-plugin-framework/ephemeral" |
| 15 | + "github.com/hashicorp/terraform-plugin-framework/ephemeral/schema" |
| 16 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 17 | + "github.com/hashicorp/terraform-provider-aws/internal/framework" |
| 18 | + "github.com/hashicorp/terraform-provider-aws/internal/smerr" |
| 19 | + itypes "github.com/hashicorp/terraform-provider-aws/internal/types" |
| 20 | + "github.com/hashicorp/terraform-provider-aws/names" |
| 21 | +) |
| 22 | + |
| 23 | +// @EphemeralResource(aws_ecr_authorization_token, name="AuthorizationToken") |
| 24 | +func newAuthorizationTokenEphemeralResource(_ context.Context) (ephemeral.EphemeralResourceWithConfigure, error) { |
| 25 | + return &authorizationTokenEphemeralResource{}, nil |
| 26 | +} |
| 27 | + |
| 28 | +type authorizationTokenEphemeralResource struct { |
| 29 | + framework.EphemeralResourceWithModel[authorizationTokenEphemeralResourceModel] |
| 30 | +} |
| 31 | + |
| 32 | +func (e *authorizationTokenEphemeralResource) Schema(ctx context.Context, _ ephemeral.SchemaRequest, response *ephemeral.SchemaResponse) { |
| 33 | + response.Schema = schema.Schema{ |
| 34 | + Attributes: map[string]schema.Attribute{ |
| 35 | + "authorization_token": schema.StringAttribute{ |
| 36 | + Computed: true, |
| 37 | + Sensitive: true, |
| 38 | + }, |
| 39 | + "expires_at": schema.StringAttribute{ |
| 40 | + Computed: true, |
| 41 | + }, |
| 42 | + names.AttrPassword: schema.StringAttribute{ |
| 43 | + Computed: true, |
| 44 | + Sensitive: true, |
| 45 | + }, |
| 46 | + "proxy_endpoint": schema.StringAttribute{ |
| 47 | + Computed: true, |
| 48 | + }, |
| 49 | + names.AttrUserName: schema.StringAttribute{ |
| 50 | + Computed: true, |
| 51 | + }, |
| 52 | + }, |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +func (e *authorizationTokenEphemeralResource) Open(ctx context.Context, request ephemeral.OpenRequest, response *ephemeral.OpenResponse) { |
| 57 | + conn := e.Meta().ECRClient(ctx) |
| 58 | + data := authorizationTokenEphemeralResourceModel{} |
| 59 | + |
| 60 | + smerr.AddEnrich(ctx, &response.Diagnostics, request.Config.Get(ctx, &data)) |
| 61 | + if response.Diagnostics.HasError() { |
| 62 | + return |
| 63 | + } |
| 64 | + |
| 65 | + input := ecr.GetAuthorizationTokenInput{} |
| 66 | + |
| 67 | + out, err := conn.GetAuthorizationToken(ctx, &input) |
| 68 | + if err != nil { |
| 69 | + smerr.AddError(ctx, &response.Diagnostics, err) |
| 70 | + return |
| 71 | + } |
| 72 | + |
| 73 | + if len(out.AuthorizationData) == 0 { |
| 74 | + smerr.AddError(ctx, &response.Diagnostics, fmt.Errorf("no authorization data returned")) |
| 75 | + return |
| 76 | + } |
| 77 | + |
| 78 | + authorizationData := out.AuthorizationData[0] |
| 79 | + authorizationToken := aws.ToString(authorizationData.AuthorizationToken) |
| 80 | + expiresAt := aws.ToTime(authorizationData.ExpiresAt).Format(time.RFC3339) |
| 81 | + proxyEndpoint := aws.ToString(authorizationData.ProxyEndpoint) |
| 82 | + authBytes, err := itypes.Base64Decode(authorizationToken) |
| 83 | + if err != nil { |
| 84 | + smerr.AddError(ctx, &response.Diagnostics, fmt.Errorf("decoding ECR authorization token: %w", err)) |
| 85 | + return |
| 86 | + } |
| 87 | + |
| 88 | + basicAuthorization := strings.Split(string(authBytes), ":") |
| 89 | + if len(basicAuthorization) != 2 { |
| 90 | + smerr.AddError(ctx, &response.Diagnostics, fmt.Errorf("unknown ECR authorization token format")) |
| 91 | + return |
| 92 | + } |
| 93 | + |
| 94 | + userName := basicAuthorization[0] |
| 95 | + password := basicAuthorization[1] |
| 96 | + |
| 97 | + data.AuthorizationToken = types.StringValue(authorizationToken) |
| 98 | + data.ProxyEndpoint = types.StringValue(proxyEndpoint) |
| 99 | + data.ExpiresAt = types.StringValue(expiresAt) |
| 100 | + data.UserName = types.StringValue(userName) |
| 101 | + data.Password = types.StringValue(password) |
| 102 | + |
| 103 | + smerr.AddEnrich(ctx, &response.Diagnostics, response.Result.Set(ctx, &data)) |
| 104 | +} |
| 105 | + |
| 106 | +type authorizationTokenEphemeralResourceModel struct { |
| 107 | + framework.WithRegionModel |
| 108 | + AuthorizationToken types.String `tfsdk:"authorization_token"` |
| 109 | + ExpiresAt types.String `tfsdk:"expires_at"` |
| 110 | + Password types.String `tfsdk:"password"` |
| 111 | + ProxyEndpoint types.String `tfsdk:"proxy_endpoint"` |
| 112 | + UserName types.String `tfsdk:"user_name"` |
| 113 | +} |
0 commit comments