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

Add convenience method to simply add statements to a Policy #29

Merged
merged 2 commits into from
Oct 25, 2018
Merged
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
2 changes: 1 addition & 1 deletion cumulus/chain/step.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ def __init__(self):

def handle(self, chain_context):
# type: (chaincontext.ChainContext) -> None
raise NotImplemented("handle must be implemented")
raise NotImplementedError("handle must be implemented")
8 changes: 4 additions & 4 deletions cumulus/steps/dev_tools/code_build_action.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,10 @@ def create_project(self, chain_context, codebuild_role, codebuild_environment, n
)
chain_context.template.add_resource(sg)
vpc_config = {'VpcConfig': codebuild.VpcConfig(
VpcId=self.vpc_config.vpc_id,
Subnets=self.vpc_config.subnets,
SecurityGroupIds=[Ref(sg)],
)}
VpcId=self.vpc_config.vpc_id,
Subnets=self.vpc_config.subnets,
SecurityGroupIds=[Ref(sg)],
)}

project_name = "Project%s" % name

Expand Down
40 changes: 20 additions & 20 deletions cumulus/types/codebuild/buildaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ def __init__(self, **kwargs):
super(SourceS3Action, self).__init__(**kwargs)

self.ActionTypeId = troposphere.codepipeline.ActionTypeId(
Category="Source",
Owner="AWS",
Version="1",
Provider='S3',
)
Category="Source",
Owner="AWS",
Version="1",
Provider='S3',
)
self.RunOrder = "1"


Expand All @@ -25,11 +25,11 @@ def __init__(self, **kwargs):
super(SourceCodeCommitAction, self).__init__(**kwargs)

self.ActionTypeId = troposphere.codepipeline.ActionTypeId(
Category="Source",
Owner="AWS",
Version="1",
Provider="CodeCommit",
)
Category="Source",
Owner="AWS",
Version="1",
Provider="CodeCommit",
)
self.RunOrder = "1"


Expand All @@ -41,11 +41,11 @@ def __init__(self, **kwargs):
super(CodeBuildAction, self).__init__(**kwargs)

self.ActionTypeId = troposphere.codepipeline.ActionTypeId(
Category="Build",
Owner="AWS",
Version="1",
Provider="CodeBuild"
)
Category="Build",
Owner="AWS",
Version="1",
Provider="CodeBuild"
)
self.RunOrder = "1"


Expand All @@ -57,11 +57,11 @@ def __init__(self, **kwargs):
super(LambdaAction, self).__init__(**kwargs)

self.ActionTypeId = troposphere.codepipeline.ActionTypeId(
Category="Invoke",
Owner="AWS",
Version="1",
Provider='Lambda',
)
Category="Invoke",
Owner="AWS",
Version="1",
Provider='Lambda',
)
self.RunOrder = "1"


Expand Down
31 changes: 31 additions & 0 deletions cumulus/util/policy_mutator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import awacs
import troposphere
from awacs.aws import PolicyDocument # noqa
from troposphere.iam import Policy # noqa


class PolicyMutator:
def __init__(self):
pass

@staticmethod
def add_statement_to_policy(policy, statement):
"""

:type policy: troposphere.iam.Policy
:type statement: awacs.aws.Statement
"""
if type(policy) is not troposphere.iam.Policy:
raise AssertionError("Expected to find troposphere.iam.Policy but found: %s" % type(policy))

if not isinstance(policy.PolicyDocument, awacs.aws.PolicyDocument):
msg = "Expected policy.PolicyDocument to be awacs.aws.PolicyDocument but found: %s" \
% type(policy.PolicyDocument)
raise AssertionError(msg)

if not isinstance(statement, awacs.aws.Statement):
msg = "Expected statement to be awacs.aws.Statement but found: %s " % type(statement)
raise AssertionError(msg)

policy.PolicyDocument.Statement.append(statement)
return policy
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ replace = __version__ = '{new_version}'
universal = 1

[flake8]
ignore = W605 # this is for components/userdata/windows.py
exclude = docs
max-line-length = 140

Expand Down
4 changes: 2 additions & 2 deletions tests/stacker_test/blueprints/s3_bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ def create_template(self):
t = self.template

t.add_resource(Bucket(
"S3Bucket",
BucketName='bswift-int-test-asdf'
"S3Bucket",
BucketName='bswift-int-test-asdf'
))
96 changes: 96 additions & 0 deletions tests/unit/util/test_policy_mutator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# try:
# #python 3
# from unittest.mock import patch
# except:
# #python 2
# from mock import patch

import unittest

import awacs
from awacs import aws # noqa
import troposphere
from troposphere import iam

from cumulus.util.policy_mutator import PolicyMutator

DEFAULT_STATEMENT_NAME = "DefaultStatement"


class TestPolicyMutator(unittest.TestCase):

def setUp(self):
pass
self.simple_policy = iam.Policy(
PolicyName="TestPolicy",
PolicyDocument=awacs.aws.PolicyDocument(
Version="2012-10-17",
Id="PipelinePolicy",
Statement=[
awacs.aws.Statement(
Sid=("%s" % DEFAULT_STATEMENT_NAME),
Effect=awacs.aws.Allow,
Action=[awacs.aws.Action("s3", "*")],
Resource=['*'],
),
],
)
)

self.dummy_statement = awacs.aws.Statement()

def tearDown(self):
pass
del self.simple_policy

def test_should_raise_assertion_error_on_wrong_policy_type(self):
policy = "not what you want"
self.assertRaises(
AssertionError,
PolicyMutator.add_statement_to_policy,
policy,
self.dummy_statement,
)

def test_should_raise_assertion_error_if_policydocument_is_not_awacs(self):
policy = troposphere.iam.Policy(
PolicyDocument={}
)
self.assertRaises(
AssertionError,
PolicyMutator.add_statement_to_policy,
policy,
self.dummy_statement,
)

def test_should_raise_assertion_error_on_wrong_statement_type(self):
policy = self.simple_policy
self.assertRaises(
AssertionError,
PolicyMutator.add_statement_to_policy,
policy,
{"statment": "is wrong"},
)

def test_should_add_statement_to_existing_policy(self):
pass
policy = self.simple_policy
lambda_policy_name = 'LambdaPolicy'
statement = awacs.aws.Statement(
Sid=('%s' % lambda_policy_name),
Effect=awacs.aws.Allow,
Action=[
awacs.aws.Action("lambda", "*")
],
Resource=["*"]
)

found_default = list(filter(lambda x: x.Sid == DEFAULT_STATEMENT_NAME, policy.PolicyDocument.Statement))
self.assertTrue(found_default, "Did not find the statement I was looking for")
self.assertIsInstance(found_default[0], awacs.aws.Statement)

policy = PolicyMutator.add_statement_to_policy(policy, statement)

found_sut = list(filter(lambda x: x.Sid == lambda_policy_name, policy.PolicyDocument.Statement))
self.assertTrue(found_sut, "Did not find the statement I was looking for")
self.assertIsInstance(found_default[0], awacs.aws.Statement)