Skip to content

Commit

Permalink
Merge pull request #40763 from danquack/29673/cognito-identity
Browse files Browse the repository at this point in the history
GetOpenIdTokenForDeveloperIdentity ephemeral resource
  • Loading branch information
ewbankkit authored Jan 8, 2025
2 parents eb41f1a + e2c549a commit 0568ae8
Show file tree
Hide file tree
Showing 6 changed files with 268 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changelog/40763.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-ephemeral
aws_cognito_identity_openid_token_for_developer_identity
```
1 change: 1 addition & 0 deletions .ci/.golangci3.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ linters-settings:
- int32validator.*
- int64validator.*
- listvalidator.*
- mapvalidator.*
- setvalidator.*
- stringvalidator.*
- SetDefaultCreateTimeout
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package cognitoidentity

import (
"context"

"github.com/YakDriver/regexache"
"github.com/aws/aws-sdk-go-v2/service/cognitoidentity"
"github.com/hashicorp/terraform-plugin-framework-validators/int64validator"
"github.com/hashicorp/terraform-plugin-framework-validators/mapvalidator"
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/ephemeral"
"github.com/hashicorp/terraform-plugin-framework/ephemeral/schema"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-provider-aws/internal/framework"
fwflex "github.com/hashicorp/terraform-provider-aws/internal/framework/flex"
fwtypes "github.com/hashicorp/terraform-provider-aws/internal/framework/types"
)

// @EphemeralResource("aws_cognito_identity_openid_token_for_developer_identity", name="Open ID Connect Token For Developer Identity")
func newOpenIDTokenForDeveloperIdentityEphemeralResource(context.Context) (ephemeral.EphemeralResourceWithConfigure, error) {
return &openIDTokenForDeveloperIdentityEphemeralResource{}, nil
}

type openIDTokenForDeveloperIdentityEphemeralResource struct {
framework.EphemeralResourceWithConfigure
}

func (*openIDTokenForDeveloperIdentityEphemeralResource) Metadata(_ context.Context, request ephemeral.MetadataRequest, response *ephemeral.MetadataResponse) {
response.TypeName = "aws_cognito_identity_openid_token_for_developer_identity"
}

func (e *openIDTokenForDeveloperIdentityEphemeralResource) Schema(ctx context.Context, request ephemeral.SchemaRequest, response *ephemeral.SchemaResponse) {
response.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
"identity_id": schema.StringAttribute{
Optional: true,
Computed: true,
Validators: []validator.String{
stringvalidator.RegexMatches(regexache.MustCompile(`[\w-]+:[0-9a-f-]+`), "A unique identifier in the format REGION:GUID."),
stringvalidator.LengthBetween(1, 55),
},
},
"identity_pool_id": schema.StringAttribute{
Required: true,
Validators: []validator.String{
stringvalidator.RegexMatches(regexache.MustCompile(`[\w-]+:[0-9a-f-]+`), "A unique identifier in the format REGION:GUID."),
stringvalidator.LengthBetween(1, 55),
},
},
"logins": schema.MapAttribute{
CustomType: fwtypes.MapOfStringType,
Required: true,
Validators: []validator.Map{
mapvalidator.KeysAre(
stringvalidator.LengthBetween(1, 128),
),
mapvalidator.ValueStringsAre(
stringvalidator.LengthBetween(1, 50000),
),
mapvalidator.SizeAtMost(10),
},
},
"principal_tags": schema.MapAttribute{
CustomType: fwtypes.MapOfStringType,
Optional: true,
Validators: []validator.Map{
mapvalidator.KeysAre(
stringvalidator.LengthBetween(1, 128),
),
mapvalidator.ValueStringsAre(
stringvalidator.LengthBetween(1, 256),
),
mapvalidator.SizeAtMost(50),
},
},
"token_duration": schema.Int64Attribute{
Optional: true,
Validators: []validator.Int64{
int64validator.Between(1, 86400),
},
},
"token": schema.StringAttribute{
Computed: true,
Sensitive: true,
},
},
}
}

func (e *openIDTokenForDeveloperIdentityEphemeralResource) Open(ctx context.Context, request ephemeral.OpenRequest, response *ephemeral.OpenResponse) {
var data openIDTokenForDeveloperIdentityEphemeralResourceModel
response.Diagnostics.Append(request.Config.Get(ctx, &data)...)
if response.Diagnostics.HasError() {
return
}

conn := e.Meta().CognitoIdentityClient(ctx)

var input cognitoidentity.GetOpenIdTokenForDeveloperIdentityInput
response.Diagnostics.Append(fwflex.Expand(ctx, data, &input)...)
if response.Diagnostics.HasError() {
return
}

output, err := conn.GetOpenIdTokenForDeveloperIdentity(ctx, &input)

if err != nil {
response.Diagnostics.AddError("creating Cognito Identity Open ID Connect Token For Developer Identity", err.Error())

return
}

data.IdentityID = fwflex.StringToFramework(ctx, output.IdentityId)
data.Token = fwflex.StringToFramework(ctx, output.Token)

response.Diagnostics.Append(response.Result.Set(ctx, &data)...)
}

type openIDTokenForDeveloperIdentityEphemeralResourceModel struct {
IdentityID types.String `tfsdk:"identity_id"`
IdentityPoolID types.String `tfsdk:"identity_pool_id"`
Logins fwtypes.MapOfString `tfsdk:"logins"`
PrincipalTags fwtypes.MapOfString `tfsdk:"principal_tags"`
Token types.String `tfsdk:"token"`
TokenDuration types.Int64 `tfsdk:"token_duration"`
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package cognitoidentity_test

import (
"fmt"
"testing"

"github.com/hashicorp/go-uuid"
sdkacctest "github.com/hashicorp/terraform-plugin-testing/helper/acctest"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/knownvalue"
"github.com/hashicorp/terraform-plugin-testing/statecheck"
"github.com/hashicorp/terraform-plugin-testing/tfjsonpath"
"github.com/hashicorp/terraform-plugin-testing/tfversion"
"github.com/hashicorp/terraform-provider-aws/internal/acctest"
"github.com/hashicorp/terraform-provider-aws/names"
)

func TestAccCognitoIdentityOpenIDTokenForDeveloperIdentityEphemeral_basic(t *testing.T) {
ctx := acctest.Context(t)
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
uuid, err := uuid.GenerateUUID()
developerProviderName := sdkacctest.RandString(10)
echoResourceName := "echo.test"
dataPath := tfjsonpath.New("data")
if err != nil {
t.Logf("error generating uuid: %s", err.Error())
t.Fail()
}

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
acctest.PreCheck(ctx, t)
acctest.PreCheckPartitionHasService(t, names.CognitoIdentityEndpointID)
testAccPreCheck(ctx, t)
},
ErrorCheck: acctest.ErrorCheck(t, names.CognitoIdentityServiceID),
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
tfversion.SkipBelow(tfversion.Version1_10_0),
},
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
ProtoV6ProviderFactories: acctest.ProtoV6ProviderFactories(ctx, acctest.ProviderNameEcho),
CheckDestroy: acctest.CheckDestroyNoop,
Steps: []resource.TestStep{
{
Config: testAccOpenIDTokenForDeveloperIdentityEphemeralConfig_basic(rName, developerProviderName, uuid),
ConfigStateChecks: []statecheck.StateCheck{
statecheck.ExpectKnownValue(echoResourceName, dataPath.AtMapKey("token"), knownvalue.NotNull()),
},
},
},
})
}

func testAccOpenIDTokenForDeveloperIdentityEphemeralConfig_basic(rName, developerProviderName, uuid string) string {
return acctest.ConfigCompose(
acctest.ConfigWithEchoProvider("ephemeral.aws_cognito_identity_openid_token_for_developer_identity.test"),
testAccPoolConfig_developerProviderName(rName, developerProviderName),
fmt.Sprintf(`
data "aws_region" "current" {}
ephemeral "aws_cognito_identity_openid_token_for_developer_identity" "test" {
identity_pool_id = aws_cognito_identity_pool.test.id
logins = {
%[2]q = "user123"
}
}
`, rName, developerProviderName, uuid))
}
9 changes: 9 additions & 0 deletions internal/service/cognitoidentity/service_package_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
---
subcategory: "Cognito Identity"
layout: "aws"
page_title: "AWS: aws_cognito_identity_openid_token_for_developer_identity"
description: |-
Terraform ephemeral resource for managing an AWS Cognito Identity Open ID Token for Developer Identity.
---


# Ephemeral: aws_cognito_identity_openid_token_for_developer_identity

Terraform ephemeral resource for managing an AWS Cognito Identity Open ID Token for Developer Identity.

~> Ephemeral resources are a new feature and may evolve as we continue to explore their most effective uses. [Learn more](https://developer.hashicorp.com/terraform/language/v1.10.x/resources/ephemeral).

## Example Usage

### Basic Usage

```terraform
data "aws_cognito_identity_pool" "example" {
identity_pool_name = "test pool"
}
ephemeral "aws_cognito_identity_openid_token_for_developer_identity" "example" {
identity_pool_id = data.aws_cognito_identity_pool.example.id
logins = {
"login.mycompany.myapp" : "USER_IDENTIFIER"
}
}
```

## Argument Reference

The following arguments are required:

* `identity_pool_id` - (Required) An identity pool ID in the format REGION:GUID.

The following arguments are optional:

* `identity_id` - (Optional) A unique identifier in the format REGION:GUID.

* `logins` - (Optional) A set of optional name-value pairs that map provider names to provider tokens. Each name-value pair represents a user from a public provider or developer provider. If the user is from a developer provider, the name-value pair will follow the syntax `"developer_provider_name": "developer_user_identifier"`. The developer provider is the "domain" by which Cognito will refer to your users; you provided this domain while creating/updating the identity pool. The developer user identifier is an identifier from your backend that uniquely identifies a user. When you create an identity pool, you can specify the supported logins.

* `principal_tags` - (Optional) Use this operation to configure attribute mappings for custom providers.

* `token_duration` - (Optional) The expiration time of the token, in seconds. You can specify a custom expiration time for the token so that you can cache it. If you don't provide an expiration time, the token is valid for 15 minutes. You can exchange the token with Amazon STS for temporary AWS credentials, which are valid for a maximum of one hour. The maximum token duration you can set is 24 hours. You should take care in setting the expiration time for a token, as there are significant security implications: an attacker could use a leaked token to access your AWS resources for the token's duration.

## Attribute Reference

This resource exports the following attributes in addition to the arguments above:

* `token` - An OpenID token.

0 comments on commit 0568ae8

Please sign in to comment.