Skip to content

Commit

Permalink
Merge pull request #40562 from hashicorp/add-validation-for-iam-polic…
Browse files Browse the repository at this point in the history
…y-document-sid

add validation for iam policy document sid
  • Loading branch information
YakDriver authored Dec 13, 2024
2 parents 43dfec0 + d716a3d commit 83bf679
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .changelog/40562.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
data-source/aws_iam_policy_document: Add plan-time validation that the `statement` `sid` is valid, including on alphanumeric characters
```
6 changes: 4 additions & 2 deletions internal/service/iam/policy_document_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"strconv"
"strings"

"github.com/YakDriver/regexache"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -143,8 +144,9 @@ func dataSourcePolicyDocument() *schema.Resource {
"principals": principalsSchema(),
names.AttrResources: setOfStringSchema(),
"sid": {
Type: schema.TypeString,
Optional: true,
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringMatch(regexache.MustCompile(`^[a-zA-Z0-9]*$`), "must only include alphanumeric characters"),
},
},
},
Expand Down
27 changes: 27 additions & 0 deletions internal/service/iam/policy_document_data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,21 @@ func TestAccIAMPolicyDocumentDataSource_overrideList(t *testing.T) {
})
}

func TestAccIAMPolicyDocumentDataSource_validateSid(t *testing.T) {
ctx := acctest.Context(t)
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t) },
ErrorCheck: acctest.ErrorCheck(t, names.IAMServiceID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccPolicyDocumentDataSourceConfig_invalidSid,
ExpectError: regexache.MustCompile(`must only include alphanumeric characters`),
},
},
})
}

func TestAccIAMPolicyDocumentDataSource_noStatementMerge(t *testing.T) {
ctx := acctest.Context(t)
resource.ParallelTest(t, resource.TestCase{
Expand Down Expand Up @@ -1022,6 +1037,18 @@ data "aws_iam_policy_document" "test_source_conflicting" {
}
`

var testAccPolicyDocumentDataSourceConfig_invalidSid = `
data "aws_iam_policy_document" "test" {
statement {
sid = "Invalid_SID"
actions = [
"s3:ListAllMyBuckets",
"s3:GetBucketLocation",
]
}
}
`

var testAccPolicyDocumentSourceConflictingExpectedJSON = `{
"Version": "2012-10-17",
"Statement": [
Expand Down

0 comments on commit 83bf679

Please sign in to comment.