Skip to content

Commit

Permalink
Update required ami test logic (#239)
Browse files Browse the repository at this point in the history
* Update required ami test logic

* style: ran black

* make options in new ami test configurable

* rename test file based on new test name

* small style fix
  • Loading branch information
jvehent authored and ajvb committed Mar 22, 2019
1 parent df63494 commit 07229a0
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 28 deletions.
15 changes: 15 additions & 0 deletions aws/ec2/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,18 @@ def ec2_security_groups_with_in_use_flag():
sec_group["InUse"] = False

return sec_groups


def ec2_images_owned_by(account_ids):
"Returns a list of EC2 images owned by a list of provided account ids"
return (
botocore_client.get(
"ec2",
"describe_images",
[],
{"Filters": [{"Name": "owner-id", "Values": account_ids}]},
)
.extract_key("Images")
.flatten()
.values()
)
46 changes: 46 additions & 0 deletions aws/ec2/test_ec2_instance_on_acceptable_ami.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import pytest

from aws.ec2.helpers import ec2_instance_test_id
from aws.ec2.resources import ec2_instances, ec2_images_owned_by

from datetime import datetime, timedelta


@pytest.fixture
def owned_amis(pytestconfig):
return ec2_images_owned_by(pytestconfig.custom_config.aws.owned_ami_account_ids)


@pytest.fixture
def max_ami_age(pytestconfig):
return pytestconfig.custom_config.aws.max_ami_age_in_days


@pytest.mark.ec2
@pytest.mark.parametrize("ec2_instance", ec2_instances(), ids=ec2_instance_test_id)
def test_ec2_instance_on_acceptable_ami(ec2_instance, owned_amis, max_ami_age):
"""
Checks that all EC2 instances are running on acceptable AMIs, meaning
an AMI that is not older than X days and is owned by us.
Default is 180 days.
"""
for tag in ec2_instance["Tags"]:
if tag["Key"] == "Name":
instanceName = tag["Value"]

minAge = datetime.now() - timedelta(days=max_ami_age)
foundAmi = False
for ami in owned_amis:
if ami["ImageId"] == ec2_instance["ImageId"]:
assert (
ami["CreationDate"] > minAge
), "Instance {} {} is running on an AMI created on {} that's older than 180 days".format(
instanceName, ec2_instance["InstanceId"], ami["CreationDate"]
)
foundAmi = True
break

if not foundAmi:
assert False, "Instance {} {} uses AMI {} not owned by us".format(
instanceName, ec2_instance["InstanceId"], ec2_instance["ImageId"]
)
25 changes: 0 additions & 25 deletions aws/ec2/test_ec2_instance_running_required_amis.py

This file was deleted.

6 changes: 3 additions & 3 deletions config.yaml.example
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,16 @@ aws:
- Type
- App
- Env
required_amis:
- ami-00000000000000000
- ami-55555555555555555
whitelisted_ports_global:
- 25
whitelisted_ports:
- test_param_id: '*bastion'
ports:
- 22
- 2222
max_ami_age_in_days: 90
owned_ami_account_ids:
- 1234567890
gsuite:
domain: 'example.com'
min_number_of_owners: 2
Expand Down
4 changes: 4 additions & 0 deletions custom_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ def __init__(self, config):
self.access_key_expires_after = config.get("access_key_expires_after", None)
self.admin_policies = frozenset(config.get("admin_policies", []))
self.admin_groups = frozenset(config.get("admin_groups", []))
self.owned_ami_account_ids = [
str(x) for x in config.get("owned_ami_account_ids", [])
]
self.max_ami_age_in_days = config.get("max_ami_age_in_days", 180)
super().__init__(config)

def get_whitelisted_ports(self, test_id):
Expand Down

0 comments on commit 07229a0

Please sign in to comment.