Skip to content

Commit

Permalink
Merge pull request #449 from tatsuyafw/support-for-testing-absence-of…
Browse files Browse the repository at this point in the history
…-inline-policies

Support for testing absence of inline policies
  • Loading branch information
k1LoW authored Feb 20, 2019
2 parents d93634f + 4b78189 commit 7e5ce9b
Show file tree
Hide file tree
Showing 10 changed files with 114 additions and 1 deletion.
10 changes: 9 additions & 1 deletion doc/_resource_types/iam_group.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe iam_group('my-iam-group') do
end
```

### have_inline_group
### have_inline_policy

```ruby
describe iam_group('my-iam-group') do
Expand Down Expand Up @@ -67,6 +67,14 @@ DOC
end
```

You can test absence of inline policies.

```ruby
describe iam_group('my-iam-group') do
it { should_not have_inline_policy }
end
```

### advanced

`iam_group` can use `Aws::IAM::Group` resource (see http://docs.aws.amazon.com/sdkforruby/api/Aws/IAM/Group.html).
Expand Down
8 changes: 8 additions & 0 deletions doc/_resource_types/iam_role.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ DOC
end
```

You can test absence of inline policies.

```ruby
describe iam_role('my-iam-role') do
it { should_not have_inline_policy }
end
```

### advanced

`iam_role` can use `Aws::IAM::Role` resource (see http://docs.aws.amazon.com/sdkforruby/api/Aws/IAM/Role.html).
Expand Down
8 changes: 8 additions & 0 deletions doc/_resource_types/iam_user.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ DOC
end
```

You can test absence of inline policies.

```ruby
describe iam_user('my-iam-user') do
it { should_not have_inline_policy }
end
```

### belong_to_iam_group

```ruby
Expand Down
59 changes: 59 additions & 0 deletions doc/resource_types.md
Original file line number Diff line number Diff line change
Expand Up @@ -1670,6 +1670,49 @@ end

### have_inline_policy

```ruby
describe iam_group('my-iam-group') do
it { should have_inline_policy('InlineEC2FullAccess') }
it do
should have_inline_policy('InlineEC2FullAccess').policy_document(<<-'DOC')
{
"Statement": [
{
"Action": "ec2:*",
"Effect": "Allow",
"Resource": "*"
},
{
"Effect": "Allow",
"Action": "elasticloadbalancing:*",
"Resource": "*"
},
{
"Effect": "Allow",
"Action": "cloudwatch:*",
"Resource": "*"
},
{
"Effect": "Allow",
"Action": "autoscaling:*",
"Resource": "*"
}
]
}
DOC
end
end
```

You can test absence of inline policies.

```ruby
describe iam_group('my-iam-group') do
it { should_not have_inline_policy }
end
```


### its(:path), its(:group_name), its(:group_id), its(:arn), its(:create_date)
### :unlock: Advanced use

Expand Down Expand Up @@ -1798,6 +1841,14 @@ DOC
end
```

You can test absence of inline policies.

```ruby
describe iam_role('my-iam-role') do
it { should_not have_inline_policy }
end
```


### its(:path), its(:role_name), its(:role_id), its(:arn), its(:create_date), its(:assume_role_policy_document), its(:description), its(:max_session_duration), its(:permissions_boundary), its(:tags)
### :unlock: Advanced use
Expand Down Expand Up @@ -1878,6 +1929,14 @@ DOC
end
```

You can test absence of inline policies.

```ruby
describe iam_user('my-iam-user') do
it { should_not have_inline_policy }
end
```


### belong_to_iam_group

Expand Down
9 changes: 9 additions & 0 deletions lib/awspec/type/iam_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,21 @@ def has_iam_policy?(policy_id)
end

def has_inline_policy?(policy_name, document = nil)
return has_any_inline_policies? unless policy_name

res = iam_client.get_group_policy({
group_name: id,
policy_name: policy_name
})
return JSON.parse(URI.decode(res.policy_document)) == JSON.parse(document) if document
res
end

private

def has_any_inline_policies?
res = iam_client.list_group_policies(group_name: id)
!res.policy_names.empty?
end
end
end
9 changes: 9 additions & 0 deletions lib/awspec/type/iam_role.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,21 @@ def has_iam_policy?(policy_id)
end

def has_inline_policy?(policy_name, document = nil)
return has_any_inline_policies? unless policy_name

res = iam_client.get_role_policy({
role_name: resource_via_client.role_name,
policy_name: policy_name
})
return JSON.parse(URI.decode(res.policy_document)) == JSON.parse(document) if document
res
end

private

def has_any_inline_policies?
res = iam_client.list_role_policies(role_name: resource_via_client.role_name)
!res.policy_names.empty?
end
end
end
9 changes: 9 additions & 0 deletions lib/awspec/type/iam_user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,21 @@ def has_iam_policy?(policy_id)
end

def has_inline_policy?(policy_name, document = nil)
return has_any_inline_policies? unless policy_name

res = iam_client.get_user_policy({
user_name: resource_via_client.user_name,
policy_name: policy_name
})
return JSON.parse(URI.decode(res.policy_document)) == JSON.parse(document) if document
res
end

private

def has_any_inline_policies?
res = iam_client.list_user_policies(user_name: resource_via_client.user_name)
!res.policy_names.empty?
end
end
end
1 change: 1 addition & 0 deletions spec/type/iam_group_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
it { should exist }
it { should have_iam_user('my-iam-user') }
it { should have_iam_policy('ReadOnlyAccess') }
it { should have_inline_policy }
it { should have_inline_policy('InlineEC2FullAccess') }
it do
should have_inline_policy('InlineEC2FullAccess').policy_document(<<-'DOC')
Expand Down
1 change: 1 addition & 0 deletions spec/type/iam_role_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
describe iam_role('my-iam-role') do
it { should exist }
it { should have_iam_policy('ReadOnlyAccess') }
it { should have_inline_policy }
it { should have_inline_policy('AllowS3BucketAccess') }
it do
should have_inline_policy('AllowS3BucketAccess').policy_document(<<-'DOC')
Expand Down
1 change: 1 addition & 0 deletions spec/type/iam_user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
it { should exist }
it { should belong_to_iam_group('my-iam-group') }
it { should have_iam_policy('ReadOnlyAccess') }
it { should have_inline_policy }
it { should have_inline_policy('AllowS3BucketAccess') }
it do
should have_inline_policy('AllowS3BucketAccess').policy_document(<<-'DOC')
Expand Down

0 comments on commit 7e5ce9b

Please sign in to comment.