Skip to content

Commit

Permalink
Merge 776d89b into 0f8ab9f
Browse files Browse the repository at this point in the history
  • Loading branch information
flomotlik committed Nov 13, 2020
2 parents 0f8ab9f + 776d89b commit 71a1386
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 18 deletions.
1 change: 1 addition & 0 deletions docs/examples/s3-lambda/stack.config.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
stack: s3-lambda
s3: true
capabilities:
- CAPABILITY_IAM
artifacts:
Expand Down
2 changes: 1 addition & 1 deletion formica/change_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def create(
self.__change_and_wait(change_set_type, optional_arguments)
else:
if s3:
with temporary_bucket() as t:
with temporary_bucket(self.stack) as t:
file_name = t.add(template)
t.upload()
bucket_name = t.name
Expand Down
9 changes: 5 additions & 4 deletions formica/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def collect_stack_set_vars(args):
def collect_vars(args):
variables = collect_stack_set_vars(args)
if args.artifacts:
variables.update(artifact_variables(args.artifacts))
variables.update(artifact_variables(args.artifacts, vars(args).get("stack", "")))

return variables

Expand Down Expand Up @@ -74,14 +74,14 @@ def main_account_id():
return identity["Account"]


def artifact_variables(artifacts):
def artifact_variables(artifacts, seed):
class Artifact:
def __init__(self, key, bucket):
self.key = key
self.bucket = bucket

artifact_keys = {}
with temporary_bucket() as t:
with temporary_bucket(seed=seed) as t:
for a in artifacts:
artifact_keys[a] = t.add_file(a)
finished_vars = {key: Artifact(value, t.name) for key, value in artifact_keys.items()}
Expand All @@ -91,7 +91,8 @@ def __init__(self, key, bucket):
def with_artifacts(function):
def handle_artifacts(args):
if args.artifacts:
with temporary_bucket() as t:
seed = vars(args).get("stack", "")
with temporary_bucket(seed=seed) as t:
for a in args.artifacts:
t.add_file(a)
t.upload()
Expand Down
10 changes: 6 additions & 4 deletions formica/s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@


class TemporaryS3Bucket(object):
def __init__(self):
def __init__(self, seed):
self.objects = {}
self.uploaded = False
self.__sts = boto3.client("sts")
self.s3_bucket = None
self.files = {}
self.seed = seed

def __digest(self, body):

Expand Down Expand Up @@ -47,7 +48,8 @@ def name(self):
[key for key, _ in self.objects.items()] + [key for key, _ in self.files.items()]
).encode()
account_id = self.__sts.get_caller_identity()["Account"]
name_digest_input = BytesIO((account_id + self.__sts.meta.region_name + body_hashes.decode()).encode())
to_hash = self.seed + account_id + self.__sts.meta.region_name + body_hashes.decode()
name_digest_input = BytesIO(to_hash.encode())
body_hashes_hash = self.__digest(name_digest_input)
return "formica-deploy-{}".format(body_hashes_hash)

Expand Down Expand Up @@ -77,8 +79,8 @@ def upload(self):


@contextmanager
def temporary_bucket():
temp_bucket = TemporaryS3Bucket()
def temporary_bucket(seed):
temp_bucket = TemporaryS3Bucket(seed=seed)
try:
yield temp_bucket
finally:
Expand Down
16 changes: 11 additions & 5 deletions tests/unit/test_change_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,16 @@ def client(mocker):
client = mocker.patch('formica.change_set.cf')
return client


@pytest.fixture
def temp_bucket(mocker):
t = mocker.patch('formica.change_set.temporary_bucket')
def temp_bucket_function(mocker):
return mocker.patch('formica.change_set.temporary_bucket')


@pytest.fixture
def temp_bucket(mocker, temp_bucket_function):
tempbucket_mock = mocker.Mock()
t.return_value.__enter__.return_value = tempbucket_mock
temp_bucket_function.return_value.__enter__.return_value = tempbucket_mock
return tempbucket_mock


Expand All @@ -55,7 +60,7 @@ def test_submits_changeset_and_waits(client):
StackName=STACK, ChangeSetName=CHANGESETNAME)


def test_creates_and_removes_bucket_for_s3_flag(client, temp_bucket):
def test_creates_and_removes_bucket_for_s3_flag(client, temp_bucket_function, temp_bucket):
change_set = ChangeSet(STACK)
bucket_name = 'formica-deploy-{}'.format("test")
bucket_path = '{}-template.json'.format(STACK)
Expand All @@ -66,6 +71,7 @@ def test_creates_and_removes_bucket_for_s3_flag(client, temp_bucket):
change_set.create(template=TEMPLATE, change_set_type=CHANGE_SET_TYPE, s3=True)

temp_bucket.add.assert_called_with(TEMPLATE)
temp_bucket_function.assert_called_with(STACK)

client.create_change_set.assert_called_with(
StackName=STACK, TemplateURL=template_url,
Expand Down Expand Up @@ -296,7 +302,7 @@ def test_change_set_without_named_properties(client):
PROPERTY_CHANGE = CHANGESET['Changes'][1]['ResourceChange']['Details'][1]
assert PROPERTY_CHANGE['Target']['Attribute'] == 'Properties'
assert PROPERTY_CHANGE['Target']['Name'] == 'BucketName'

# Remove the "Name" attribute
del PROPERTY_CHANGE['Target']['Name']

Expand Down
9 changes: 5 additions & 4 deletions tests/unit/test_s3.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from formica.s3 import temporary_bucket
import pytest
from .constants import STACK


@pytest.fixture
Expand All @@ -11,7 +12,7 @@ def bucket(mocker, boto_resource, boto_client):
STRING_KEY = "b45cffe084dd3d20d928bee85e7b0f21"
BINARY_BODY = "binary".encode()
BINARY_KEY = "9d7183f16acce70658f686ae7f1a4d20"
BUCKET_NAME = "formica-deploy-c1b5d5393a375cd8a51e91f2f3cec8bd"
BUCKET_NAME = "formica-deploy-88dec80484e3155b2c8cf023b635fb31"
FILE_NAME = "testfile"
FILE_BODY = "file-body"
FILE_KEY = "de858a1b070b29a579e2d8861b53ad20"
Expand All @@ -24,7 +25,7 @@ def test_s3_bucket_context(mocker, bucket, uuid4, boto_client):
mock_open = mocker.mock_open(read_data=FILE_BODY.encode())
mocker.patch('formica.s3.open', mock_open)

with temporary_bucket() as temp_bucket:
with temporary_bucket(seed=STACK) as temp_bucket:
string_return = temp_bucket.add(STRING_BODY)
binary_return = temp_bucket.add(BINARY_BODY)
file_return = temp_bucket.add_file(FILE_NAME)
Expand Down Expand Up @@ -53,7 +54,7 @@ def test_s3_bucket_context(mocker, bucket, uuid4, boto_client):
def test_does_not_delete_objects_if_empty(bucket):
bucket.return_value.objects.all.return_value = []

with temporary_bucket():
with temporary_bucket(seed=STACK):
pass

bucket.return_value.delete_objects.assert_not_called()
Expand All @@ -62,7 +63,7 @@ def test_does_not_delete_objects_if_empty(bucket):
def test_does_not_use_s3_api_when_planning(bucket):
bucket.return_value.objects.all.return_value = []

with temporary_bucket() as temp_bucket:
with temporary_bucket(seed=STACK) as temp_bucket:
temp_bucket.add(STRING_BODY)
temp_bucket.add(BINARY_BODY)

Expand Down

0 comments on commit 71a1386

Please sign in to comment.