-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add basic support for guessing additional actions #22
- Loading branch information
Showing
10 changed files
with
269 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import pytest | ||
|
||
from trailscraper.iam import Action | ||
|
||
|
||
@pytest.mark.parametrize("test_input,expected", [ | ||
(Action('autoscaling', 'DescribeLaunchConfigurations'), "LaunchConfiguration"), | ||
(Action('autoscaling', 'CreateLaunchConfiguration'), "LaunchConfiguration"), | ||
(Action('autoscaling', 'DeleteLaunchConfiguration'), "LaunchConfiguration"), | ||
(Action('autoscaling', 'UpdateAutoScalingGroup'), "AutoScalingGroup"), | ||
]) | ||
def test_create_base_action(test_input, expected): | ||
assert test_input._base_action() == expected | ||
|
||
|
||
@pytest.mark.parametrize("test_input,expected", [ | ||
(Action('autoscaling', 'DescribeLaunchConfigurations'), [ | ||
Action('autoscaling', 'CreateLaunchConfiguration'), | ||
Action('autoscaling', 'DeleteLaunchConfiguration'), | ||
]), | ||
(Action('autoscaling', 'CreateLaunchConfiguration'), [ | ||
Action('autoscaling', 'DeleteLaunchConfiguration'), | ||
Action('autoscaling', 'DescribeLaunchConfigurations'), | ||
]), | ||
(Action('autoscaling', 'DeleteLaunchConfiguration'), [ | ||
Action('autoscaling', 'CreateLaunchConfiguration'), | ||
Action('autoscaling', 'DescribeLaunchConfigurations'), | ||
]), | ||
(Action('autoscaling', 'UpdateAutoScalingGroup'), [ | ||
Action('autoscaling', 'CreateAutoScalingGroup'), | ||
Action('autoscaling', 'DeleteAutoScalingGroup'), | ||
Action('autoscaling', 'DescribeAutoScalingGroups'), | ||
]), | ||
(Action('autoscaling', 'DeleteAutoScalingGroup'), [ | ||
Action('autoscaling', 'CreateAutoScalingGroup'), | ||
Action('autoscaling', 'UpdateAutoScalingGroup'), | ||
Action('autoscaling', 'DescribeAutoScalingGroups'), | ||
]), | ||
]) | ||
def test_find_create_action(test_input, expected): | ||
assert test_input.matching_actions() == expected | ||
|
||
|
||
# TODO: | ||
# * Attach/Detach? | ||
# * list | ||
# * Encrypt/Decrypt/GenerateDataKey? | ||
# * Put |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from trailscraper.iam import all_known_iam_permissions, Action, known_iam_actions | ||
|
||
|
||
def test_all_iam_permissions(): | ||
permissions = all_known_iam_permissions() | ||
|
||
assert permissions != [] | ||
assert "ec2:DescribeInstances" in permissions | ||
assert len(permissions) == len(set(permissions)), "expected no duplicates" | ||
|
||
|
||
def test_known_iam_action_for_prefix(): | ||
actions = known_iam_actions("acm") | ||
assert len(actions) == 10 | ||
assert Action("acm","DescribeCertificate") in actions | ||
|
||
|
||
def test_known_iam_action_for_prefix_does_not_fail_if_action_not_found(): | ||
assert known_iam_actions("something-unknown") == [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
from StringIO import StringIO | ||
|
||
from click.testing import CliRunner | ||
|
||
from trailscraper import cli | ||
from trailscraper.iam import PolicyDocument, Statement, Action, parse_policy_document | ||
|
||
|
||
def test_should_guess_create_statements(): | ||
input_policy = PolicyDocument( | ||
Version="2012-10-17", | ||
Statement=[ | ||
Statement( | ||
Effect="Allow", | ||
Action=[ | ||
Action('autoscaling', 'DescribeLaunchConfigurations'), | ||
], | ||
Resource=["*"] | ||
), | ||
Statement( | ||
Effect="Allow", | ||
Action=[ | ||
Action('sts', 'AssumeRole'), | ||
], | ||
Resource=[ | ||
"arn:aws:iam::111111111111:role/someRole" | ||
] | ||
) | ||
] | ||
) | ||
|
||
expected_output = PolicyDocument( | ||
Version="2012-10-17", | ||
Statement=[ | ||
Statement( | ||
Effect="Allow", | ||
Action=[ | ||
Action('autoscaling', 'DescribeLaunchConfigurations'), | ||
], | ||
Resource=["*"] | ||
), | ||
Statement( | ||
Effect="Allow", | ||
Action=[ | ||
Action('autoscaling', 'CreateLaunchConfiguration'), | ||
Action('autoscaling', 'DeleteLaunchConfiguration'), | ||
], | ||
Resource=["*"] | ||
), | ||
Statement( | ||
Effect="Allow", | ||
Action=[ | ||
Action('sts', 'AssumeRole'), | ||
], | ||
Resource=[ | ||
"arn:aws:iam::111111111111:role/someRole" | ||
] | ||
) | ||
] | ||
) | ||
|
||
runner = CliRunner() | ||
result = runner.invoke(cli.root_group, args=["guess"], input=StringIO(input_policy.to_json())) | ||
assert result.exit_code == 0 | ||
assert parse_policy_document(StringIO(result.output)) == expected_output | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
"""Logic to guess related IAM statements""" | ||
from trailscraper.iam import PolicyDocument, Statement | ||
|
||
|
||
def _guess_actions(actions): | ||
return [item for action in actions | ||
for item in action.matching_actions()] | ||
|
||
|
||
def _extend_statement(statement): | ||
extended_actions = _guess_actions(statement.Action) | ||
if extended_actions: | ||
return [statement, Statement(Action=extended_actions, | ||
Effect=statement.Effect, | ||
Resource=["*"])] | ||
|
||
return [statement] | ||
|
||
|
||
def guess_statements(policy): | ||
"""Guess additional create actions""" | ||
extended_statements = [item for statement in policy.Statement | ||
for item in _extend_statement(statement)] | ||
|
||
return PolicyDocument(Version=policy.Version, Statement=extended_statements) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.