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

Convert boolean and numeric values to strings for value comparison #12

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions aws_policy_equivalence.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"fmt"
"reflect"
"regexp"
"strconv"
"strings"

"github.com/mitchellh/mapstructure"
Expand Down Expand Up @@ -377,17 +378,30 @@ func newAWSStringSet(members interface{}) awsStringSet {
return awsStringSet{single}
}

if single, ok := members.(bool); ok {
return awsStringSet{strconv.FormatBool(single)}
}

if single, ok := members.(float64); ok {
return awsStringSet{strconv.FormatFloat(single, 'f', -1, 64)}
}

if multiple, ok := members.([]interface{}); ok {
if len(multiple) == 0 {
return awsStringSet{}
}
actions := make([]string, len(multiple))
for i, action := range multiple {
if _, ok := action.(string); !ok {
var actions []string
for _, action := range multiple {
switch action := action.(type) {
case string:
actions = append(actions, action)
case bool:
actions = append(actions, strconv.FormatBool(action))
case float64:
actions = append(actions, strconv.FormatFloat(action, 'f', -1, 64))
default:
return nil
}

actions[i] = action.(string)
}
return awsStringSet(actions)
}
Expand Down
148 changes: 146 additions & 2 deletions aws_policy_equivalence_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,24 @@ func TestPolicyEquivalence(t *testing.T) {
policy2: policyTest33,
equivalent: false,
},
{
name: "Boolean condition with and without quotes on single value",
policy1: policyTest34a,
policy2: policyTest34b,
equivalent: true,
},
{
name: "Numeric condition with and without quotes on single value",
policy1: policyTest35a,
policy2: policyTest35b,
equivalent: true,
},
{
name: "Numeric condition with and without quotes on array of values",
policy1: policyTest36a,
policy2: policyTest36b,
equivalent: true,
},
}

for _, tc := range cases {
Expand Down Expand Up @@ -1292,7 +1310,7 @@ const policyTest32 = `{
"Action": [
"s3:PutObject"
],
"Resource": 42
"Resource": {}
}
]
}`
Expand All @@ -1306,7 +1324,133 @@ const policyTest33 = `{
"Action": [
"s3:PutObject"
],
"Resource": [42]
"Resource": [[42]]
}
]
}`

const policyTest34a = `{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "statement1",
"Effect": "Allow",
"Action": [
"s3:PutObject"
],
"Resource": [
"arn:aws:s3:::examplebucket/*"
],
"Condition": {
"Bool": {
"aws:MultiFactorAuthPresent": true
}
}
}
]
}`

const policyTest34b = `{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "statement1",
"Effect": "Allow",
"Action": [
"s3:PutObject"
],
"Resource": [
"arn:aws:s3:::examplebucket/*"
],
"Condition": {
"Bool": {
"aws:MultiFactorAuthPresent": "true"
}
}
}
]
}`

const policyTest35a = `{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "statement1",
"Effect": "Allow",
"Action": [
"s3:PutObject"
],
"Resource": [
"arn:aws:s3:::examplebucket/*"
],
"Condition": {
"NumericLessThanEquals": {
"aws:MultiFactorAuthAge": 100
}
}
}
]
}`

const policyTest35b = `{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "statement1",
"Effect": "Allow",
"Action": [
"s3:PutObject"
],
"Resource": [
"arn:aws:s3:::examplebucket/*"
],
"Condition": {
"NumericLessThanEquals": {
"aws:MultiFactorAuthAge": "100"
}
}
}
]
}`

const policyTest36a = `{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "statement1",
"Effect": "Allow",
"Action": [
"s3:PutObject"
],
"Resource": [
"arn:aws:s3:::examplebucket/*"
],
"Condition": {
"NumericEquals": {
"aws:MultiFactorAuthAge": [100.01, 200.2]
}
}
}
]
}`

const policyTest36b = `{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "statement1",
"Effect": "Allow",
"Action": [
"s3:PutObject"
],
"Resource": [
"arn:aws:s3:::examplebucket/*"
],
"Condition": {
"NumericEquals": {
"aws:MultiFactorAuthAge": ["100.01", "200.2"]
}
}
}
]
}`