Skip to content

Commit

Permalink
Merge pull request #26657 from mtt88/iam-policy-doc-condition-bool-value
Browse files Browse the repository at this point in the history
Handle bool value in condition when importing json policy
  • Loading branch information
ewbankkit authored Sep 6, 2022
2 parents 411b91d + ef511c5 commit 0b0f040
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changelog/26657.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
data-source/aws_iam_policy_document: Correctly handle unquoted Boolean values in `Condition`
```
83 changes: 83 additions & 0 deletions internal/service/iam/policy_document_data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,24 @@ func TestAccIAMPolicyDocumentDataSource_singleConditionValue(t *testing.T) {
})
}

func TestAccIAMPolicyDocumentDataSource_conditionWithBoolValue(t *testing.T) {
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t) },
ErrorCheck: acctest.ErrorCheck(t, iam.EndpointsID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccPolicyDocumentConfig_conditionWithBoolValue,
Check: resource.ComposeTestCheckFunc(
acctest.CheckResourceAttrEquivalentJSON("data.aws_iam_policy_document.test", "json",
testAccPolicyDocumentConditionWithBoolValueExpectedJSON(),
),
),
},
},
})
}

func TestAccIAMPolicyDocumentDataSource_source(t *testing.T) {
// This really ought to be able to be a unit test rather than an
// acceptance test, but just instantiating the AWS provider requires
Expand Down Expand Up @@ -1222,6 +1240,71 @@ data "aws_iam_policy_document" "test" {
}
`

const testAccPolicyDocumentConfig_conditionWithBoolValue = `
data "aws_partition" "current" {}
data "aws_iam_policy_document" "test" {
source_policy_documents = [<<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "RestrictAccessToSpecialTag",
"Effect": "Deny",
"Action": [
"ec2:CreateTags",
"ec2:DeleteTags"
],
"Resource": "arn:${data.aws_partition.current.partition}:ec2:*:*:vpc/*",
"Condition": {
"Null": {
"aws:ResourceTag/SpecialTag": false
},
"StringLike": {
"aws:ResourceAccount": [
"123456"
],
"aws:PrincipalArn": "arn:${data.aws_partition.current.partition}:iam::*:role/AWSAFTExecution"
}
}
}
]
}
EOF
]
}
`

func testAccPolicyDocumentConditionWithBoolValueExpectedJSON() string {
return fmt.Sprintf(`{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "RestrictAccessToSpecialTag",
"Effect": "Deny",
"Action": [
"ec2:CreateTags",
"ec2:DeleteTags"
],
"Resource": "arn:%[1]s:ec2:*:*:vpc/*",
"Condition": {
"Null": {
"aws:ResourceTag/SpecialTag": "false"
},
"StringLike": {
"aws:ResourceAccount": [
"123456"
],
"aws:PrincipalArn": [
"arn:%[1]s:iam::*:role/AWSAFTExecution"
]
}
}
}
]
}`, acctest.Partition())
}

func testAccPolicyDocumentExpectedJSONStatementPrincipalIdentifiersStringAndSlice() string {
return fmt.Sprintf(`{
"Version": "2012-10-17",
Expand Down
3 changes: 3 additions & 0 deletions internal/service/iam/policy_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"sort"
"strconv"
)

const (
Expand Down Expand Up @@ -199,6 +200,8 @@ func (cs *IAMPolicyStatementConditionSet) UnmarshalJSON(b []byte) error {
switch var_values := var_values.(type) {
case string:
out = append(out, IAMPolicyStatementCondition{Test: test_key, Variable: var_key, Values: []string{var_values}})
case bool:
out = append(out, IAMPolicyStatementCondition{Test: test_key, Variable: var_key, Values: strconv.FormatBool(var_values)})
case []interface{}:
values := []string{}
for _, v := range var_values {
Expand Down

0 comments on commit 0b0f040

Please sign in to comment.