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

fixed the user data encoding by using the proper base64 #541

Merged
merged 2 commits into from
Oct 5, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions senza/components/elastigroup.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,8 +315,8 @@ def extract_user_data(configuration, elastigroup_config, info: dict, force, acco
if not force and docker_image.registry:
check_docker_image_exists(docker_image)

user_data = base64.urlsafe_b64encode(generate_user_data(taupage_config, account_info.Region).encode('utf-8'))
elastigroup_config["compute"]["launchSpecification"]["userData"] = user_data.decode('utf-8')
elastigroup_config["compute"]["launchSpecification"]["userData"] = \
{"Fn::Base64": generate_user_data(taupage_config, account_info.Region)}


def extract_load_balancer_name(configuration, elastigroup_config: dict):
Expand Down
2 changes: 1 addition & 1 deletion senza/patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def patch_elastigroup(group, properties, elastigroup_id, spotinst_account_data):
if key == 'UserData':
if should_patch_user_data(val, current_properties[key]):
patched_user_data = patch_user_data(current_properties[key], val)
encoded_user_data = base64.urlsafe_b64encode(patched_user_data.encode('utf-8')).decode('utf-8')
encoded_user_data = base64.b64encode(patched_user_data.encode('utf-8')).decode('utf-8')
properties_to_patch[key] = encoded_user_data
else:
if current_properties[key] != val:
Expand Down
38 changes: 34 additions & 4 deletions tests/test_elastigroup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@

from senza.spotinst import MissingSpotinstAccount
from senza.components.elastigroup import (component_elastigroup, ELASTIGROUP_DEFAULT_PRODUCT,
ELASTIGROUP_DEFAULT_STRATEGY, resolve_account_id, SPOTINST_API_URL, extract_block_mappings,
extract_auto_scaling_rules, ensure_instance_monitoring, ensure_default_strategy, extract_autoscaling_capacity,
ensure_default_product, fill_standard_tags, extract_subnets, extract_load_balancer_name, extract_public_ips,
extract_image_id, extract_security_group_ids, extract_instance_types, extract_instance_profile)
ELASTIGROUP_DEFAULT_STRATEGY, resolve_account_id, SPOTINST_API_URL,
extract_block_mappings,
extract_auto_scaling_rules, ensure_instance_monitoring,
ensure_default_strategy, extract_autoscaling_capacity,
ensure_default_product, fill_standard_tags, extract_subnets,
extract_load_balancer_name, extract_public_ips,
extract_image_id, extract_security_group_ids, extract_instance_types,
extract_instance_profile)


def test_component_elastigroup_defaults(monkeypatch):
Expand Down Expand Up @@ -67,6 +71,32 @@ def test_component_elastigroup_defaults(monkeypatch):
assert "thirdPartiesIntegration" in properties["group"]


def test_raw_user_data_and_base64_encoding_cf_function_used(monkeypatch):
configuration = {
"Name": "eg1",
"SecurityGroups": {"Ref": "sg1"},
"InstanceType": "big",
"TaupageConfig": {
"runtime": "Docker",
"source": "some/fake/artifact:test"
}
}
args = MagicMock()
args.region = "reg1"
info = {'StackName': 'foobar', 'StackVersion': '0.1', 'SpotinstAccessToken': 'token1'}
definition = {"Resources": {},
"Mappings": {"Senza": {"Info": info}, "ServerSubnets": {"reg1": {"Subnets": ["sn1", "sn2", "sn3"]}}}}

mock_resolve_account_id = MagicMock()
mock_resolve_account_id.return_value = 'act-12345abcdef'
monkeypatch.setattr('senza.components.elastigroup.resolve_account_id', mock_resolve_account_id)

result = component_elastigroup(definition, configuration, args, info, True, MagicMock())

launch_specification = result["Resources"]["eg1"]["Properties"]["group"]["compute"]["launchSpecification"]
assert "some/fake/artifact:test" in launch_specification["userData"]["Fn::Base64"]


def test_missing_access_token():
with pytest.raises(click.UsageError):
component_elastigroup({}, {}, MagicMock(), MagicMock(), False, MagicMock())
Expand Down
6 changes: 3 additions & 3 deletions tests/test_patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ def create_lc(properties_to_patch, *args):

monkeypatch.setattr('senza.spotinst.components.elastigroup_api.patch_elastigroup', create_lc)

properties = {'ImageId': 'mynewimage', 'InstanceType': 'mynewinstancetyoe', 'UserData': {'source': 'newsource'}}
properties = {'ImageId': 'mynewimage', 'InstanceType': 'mynewinstancetyoe', 'UserData': {'source': 'newsource >'}}
group = {'compute': {
'launchSpecification': {
'userData': codecs.encode(b'#firstline\nsource: oldsource', 'base64').decode('utf-8'),
'userData': base64.b64encode('#firstline\nsource: oldsource\n'.encode('utf-8')).decode('utf-8'),
'imageId': 'myoldimage'
},
'instanceTypes': {
Expand All @@ -58,7 +58,7 @@ def create_lc(properties_to_patch, *args):

assert changed
assert new_lc['ImageId'] == 'mynewimage'
assert new_lc['UserData'] == base64.urlsafe_b64encode('#firstline\nsource: newsource\n'.encode('utf-8')).decode('utf-8')
assert new_lc['UserData'] == base64.b64encode('#firstline\nsource: newsource >\n'.encode('utf-8')).decode('utf-8')
assert new_lc['InstanceType'] == 'mynewinstancetyoe'


Expand Down