Skip to content

Commit

Permalink
Merge pull request #38184 from hashicorp/b-aws_cognito_user_pool-add-…
Browse files Browse the repository at this point in the history
…lambda_config-crash

r/aws_cognito_user_pool: Fix `runtime error: index out of range [0] with length 0` panic when adding `lambda_config`
  • Loading branch information
ewbankkit authored Jun 28, 2024
2 parents c047952 + 3af8c78 commit c0ab671
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .changelog/38168.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
```release-note:bug
data-source/aws_cognito_user_pool_client: Fix `InvalidParameterException: 2 validation errors detected` errors on Read
data-source/aws_cognito_user_pool_client: Fix `InvalidParameterException: 2 validation errors detected` errors on Read
```
3 changes: 3 additions & 0 deletions .changelog/38184.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
resource/aws_cognito_user_pool: Fix `runtime error: index out of range [0] with length 0` panic when adding `lambda_config`
```
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## 5.57.0 (Unreleased)
## 5.56.1 (Unreleased)

BUG FIXES:

Expand Down
10 changes: 9 additions & 1 deletion internal/service/cognitoidp/user_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -997,7 +997,15 @@ func resourceUserPoolUpdate(ctx context.Context, d *schema.ResourceData, meta in
if v, ok := d.GetOk("lambda_config"); ok {
if v, ok := v.([]interface{})[0].(map[string]interface{}); ok && v != nil {
if d.HasChange("lambda_config.0.pre_token_generation") {
v["pre_token_generation_config"].([]interface{})[0].(map[string]interface{})["lambda_arn"] = d.Get("lambda_config.0.pre_token_generation")
preTokenGeneration := d.Get("lambda_config.0.pre_token_generation")
if tfList, ok := v["pre_token_generation_config"].([]interface{}); ok && len(tfList) > 0 && tfList[0] != nil {
v["pre_token_generation_config"].([]interface{})[0].(map[string]interface{})["lambda_arn"] = preTokenGeneration
} else {
v["pre_token_generation_config"] = []interface{}{map[string]interface{}{
"lambda_arn": preTokenGeneration,
"lambda_version": string(awstypes.PreTokenGenerationLambdaVersionTypeV10), // A guess...
}}
}
}

if d.HasChange("lambda_config.0.pre_token_generation_config.0.lambda_arn") {
Expand Down
47 changes: 47 additions & 0 deletions internal/service/cognitoidp/user_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1334,6 +1334,53 @@ func TestAccCognitoIDPUserPool_WithLambda_preGenerationTokenConfig(t *testing.T)
})
}

// https://github.com/hashicorp/terraform-provider-aws/issues/38164.
func TestAccCognitoIDPUserPool_addLambda(t *testing.T) {
ctx := acctest.Context(t)
var pool awstypes.UserPoolType
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
resourceName := "aws_cognito_user_pool.test"
lambdaResourceName := "aws_lambda_function.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t); testAccPreCheckIdentityProvider(ctx, t) },
ErrorCheck: acctest.ErrorCheck(t, names.CognitoIDPServiceID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckUserPoolDestroy(ctx),
Steps: []resource.TestStep{
{
Config: testAccUserPoolConfig_name(rName),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckUserPoolExists(ctx, resourceName, &pool),
resource.TestCheckResourceAttr(resourceName, "lambda_config.#", acctest.Ct0),
),
},
{
Config: testAccUserPoolConfig_lambda(rName),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckUserPoolExists(ctx, resourceName, &pool),
resource.TestCheckResourceAttr(resourceName, "lambda_config.#", acctest.Ct1),
resource.TestCheckResourceAttrPair(resourceName, "lambda_config.0.create_auth_challenge", lambdaResourceName, names.AttrARN),
resource.TestCheckResourceAttrPair(resourceName, "lambda_config.0.custom_message", lambdaResourceName, names.AttrARN),
resource.TestCheckResourceAttrPair(resourceName, "lambda_config.0.define_auth_challenge", lambdaResourceName, names.AttrARN),
resource.TestCheckResourceAttrPair(resourceName, "lambda_config.0.post_authentication", lambdaResourceName, names.AttrARN),
resource.TestCheckResourceAttrPair(resourceName, "lambda_config.0.post_confirmation", lambdaResourceName, names.AttrARN),
resource.TestCheckResourceAttrPair(resourceName, "lambda_config.0.pre_authentication", lambdaResourceName, names.AttrARN),
resource.TestCheckResourceAttrPair(resourceName, "lambda_config.0.pre_sign_up", lambdaResourceName, names.AttrARN),
resource.TestCheckResourceAttrPair(resourceName, "lambda_config.0.pre_token_generation", lambdaResourceName, names.AttrARN),
resource.TestCheckResourceAttrPair(resourceName, "lambda_config.0.user_migration", lambdaResourceName, names.AttrARN),
resource.TestCheckResourceAttrPair(resourceName, "lambda_config.0.verify_auth_challenge_response", lambdaResourceName, names.AttrARN),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func TestAccCognitoIDPUserPool_schemaAttributes(t *testing.T) {
ctx := acctest.Context(t)
var pool1, pool2 awstypes.UserPoolType
Expand Down

0 comments on commit c0ab671

Please sign in to comment.