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

New data source aws_ecr_authorization_token #12395

Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
81 changes: 81 additions & 0 deletions aws/data_source_aws_ecr_authorization_token.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package aws

import (
"encoding/base64"
"fmt"
"log"
"strings"
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ecr"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)

func dataSourceAwsEcrAuthorizationToken() *schema.Resource {
return &schema.Resource{
Read: dataSourceAwsEcrAuthorizationTokenRead,

Schema: map[string]*schema.Schema{
"registry_id": {
Type: schema.TypeString,
Optional: true,
},
"authorization_token": {
Type: schema.TypeString,
Computed: true,
Sensitive: true,
},
"proxy_endpoint": {
Type: schema.TypeString,
Computed: true,
},
"expires_at": {
Type: schema.TypeString,
Computed: true,
},
"user_name": {
Type: schema.TypeString,
Computed: true,
},
"password": {
Type: schema.TypeString,
Computed: true,
Sensitive: true,
},
},
}
}

func dataSourceAwsEcrAuthorizationTokenRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ecrconn
params := &ecr.GetAuthorizationTokenInput{}
if v, ok := d.GetOk("registry_id"); ok && len(v.(string)) > 0 {
params.RegistryIds = []*string{aws.String(v.(string))}
}
log.Printf("[DEBUG] Getting ECR authorization token")
out, err := conn.GetAuthorizationToken(params)
if err != nil {
return fmt.Errorf("error getting ECR authorization token: %s", err)
}
log.Printf("[DEBUG] Received ECR AuthorizationData %v", out.AuthorizationData)
authorizationData := out.AuthorizationData[0]
authorizationToken := aws.StringValue(authorizationData.AuthorizationToken)
expiresAt := aws.TimeValue(authorizationData.ExpiresAt).Format(time.RFC3339)
proxyEndpoint := aws.StringValue(authorizationData.ProxyEndpoint)
authBytes, err := base64.URLEncoding.DecodeString(authorizationToken)
if err != nil {
d.SetId("")
return fmt.Errorf("error decoding ECR authorization token: %s", err)
}
basicAuthorization := strings.Split(string(authBytes), ":")
userName := basicAuthorization[0]
password := basicAuthorization[1]
anGie44 marked this conversation as resolved.
Show resolved Hide resolved
d.SetId(authorizationToken)
edgarpoce marked this conversation as resolved.
Show resolved Hide resolved
d.Set("authorization_token", authorizationToken)
d.Set("proxy_endpoint", proxyEndpoint)
d.Set("expires_at", expiresAt)
d.Set("user_name", userName)
d.Set("password", password)
return nil
}
60 changes: 60 additions & 0 deletions aws/data_source_aws_ecr_authorization_token_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package aws

import (
"fmt"
"regexp"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
)

func TestAccAWSEcrAuthorizationTokenDataSource_basic(t *testing.T) {
rName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "data.aws_ecr_authorization_token.repo"
edgarpoce marked this conversation as resolved.
Show resolved Hide resolved

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccCheckAwsEcrAuthorizationTokenDataSourceBasicConfig,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(resourceName, "authorization_token"),
resource.TestCheckResourceAttrSet(resourceName, "proxy_endpoint"),
resource.TestCheckResourceAttrSet(resourceName, "expires_at"),
resource.TestCheckResourceAttrSet(resourceName, "user_name"),
resource.TestMatchResourceAttr(resourceName, "user_name", regexp.MustCompile(`AWS`)),
resource.TestCheckResourceAttrSet(resourceName, "password"),
),
},
{
edgarpoce marked this conversation as resolved.
Show resolved Hide resolved
Config: testAccCheckAwsEcrAuthorizationTokenDataSourceRepositoryConfig(rName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet("aws_ecr_repository.repo", "registry_id"),
resource.TestCheckResourceAttrSet(resourceName, "authorization_token"),
resource.TestCheckResourceAttrSet(resourceName, "proxy_endpoint"),
resource.TestCheckResourceAttrSet(resourceName, "expires_at"),
resource.TestCheckResourceAttrSet(resourceName, "user_name"),
resource.TestMatchResourceAttr(resourceName, "user_name", regexp.MustCompile(`AWS`)),
resource.TestCheckResourceAttrSet(resourceName, "password"),
),
},
},
})
}

var testAccCheckAwsEcrAuthorizationTokenDataSourceBasicConfig = `
data "aws_ecr_authorization_token" "repo" {}
`

func testAccCheckAwsEcrAuthorizationTokenDataSourceRepositoryConfig(rName string) string {
return fmt.Sprintf(`
resource "aws_ecr_repository" "repo" {
name = %q
}
data "aws_ecr_authorization_token" "repo" {
registry_id = "${aws_ecr_repository.repo.registry_id}"
}
`, rName)
}
1 change: 1 addition & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ func Provider() terraform.ResourceProvider {
"aws_ec2_transit_gateway_route_table": dataSourceAwsEc2TransitGatewayRouteTable(),
"aws_ec2_transit_gateway_vpc_attachment": dataSourceAwsEc2TransitGatewayVpcAttachment(),
"aws_ec2_transit_gateway_vpn_attachment": dataSourceAwsEc2TransitGatewayVpnAttachment(),
"aws_ecr_authorization_token": dataSourceAwsEcrAuthorizationToken(),
"aws_ecr_image": dataSourceAwsEcrImage(),
"aws_ecr_repository": dataSourceAwsEcrRepository(),
"aws_ecs_cluster": dataSourceAwsEcsCluster(),
Expand Down
3 changes: 3 additions & 0 deletions website/aws.erb
Original file line number Diff line number Diff line change
Expand Up @@ -1191,6 +1191,9 @@
<li>
<a href="#">Data Sources</a>
<ul class="nav nav-auto-expand">
<li>
<a href="/docs/providers/aws/d/ecr_authorization_token.html">aws_ecr_authorization_token</a>
</li>
<li>
<a href="/docs/providers/aws/d/ecr_image.html">aws_ecr_image</a>
</li>
Expand Down
34 changes: 34 additions & 0 deletions website/docs/d/ecr_authorization_token.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
---
subcategory: "ECR"
layout: "aws"
page_title: "AWS: aws_ecr_authorization_token"
description: |-
Provides details about an ECR Authorization Token
---

# Data Source: aws_ecr_authorization_token

The ECR Authorization Token data source allows the authorization token, proxy endpoint, token expiration date, user name and password to be retrieved for an ECR repository.

## Example Usage

```hcl
data "aws_ecr_authorization_token" "token" {
}
```

## Argument Reference

The following arguments are supported:

* `registry_id` - (Optional) AWS account ID of the ECR Repository. If not specified the default account is assumed.

## Attributes Reference

In addition to the argument above, the following attributes are exported:

* `authorization_token` - Temporary IAM authentication credentials to access the ECR repository encoded in base64 in the form of `user_name:password`.
* `proxy_endpoint` - The registry URL to use in the docker login command.
* `expires_at` - The time in UTC RFC3339 format when the authorization token expires.
* `user_name` - User name decoded from the authorization token.
* `password` - Password decoded from the authorization token.