|
| 1 | +from stacker.blueprints.base import Blueprint |
| 2 | + |
| 3 | +from troposphere import ( |
| 4 | + GetAtt, |
| 5 | + Output, |
| 6 | + Ref, |
| 7 | + iam, |
| 8 | +) |
| 9 | + |
| 10 | +from awacs.aws import Policy |
| 11 | +from awacs.helpers.trust import ( |
| 12 | + get_default_assumerole_policy, |
| 13 | + get_lambda_assumerole_policy |
| 14 | +) |
| 15 | + |
| 16 | + |
| 17 | +class Roles(Blueprint): |
| 18 | + VARIABLES = { |
| 19 | + "Ec2Roles": { |
| 20 | + "type": list, |
| 21 | + "description": "names of ec2 roles to create", |
| 22 | + "default": [], |
| 23 | + }, |
| 24 | + "LambdaRoles": { |
| 25 | + "type": list, |
| 26 | + "description": "names of lambda roles to create", |
| 27 | + "default": [], |
| 28 | + }, |
| 29 | + } |
| 30 | + |
| 31 | + def __init__(self, *args, **kwargs): |
| 32 | + super(Roles, self).__init__(*args, **kwargs) |
| 33 | + self.roles = [] |
| 34 | + self.policies = [] |
| 35 | + |
| 36 | + def create_role(self, name, assumerole_policy): |
| 37 | + t = self.template |
| 38 | + |
| 39 | + role = t.add_resource( |
| 40 | + iam.Role( |
| 41 | + name, |
| 42 | + AssumeRolePolicyDocument=assumerole_policy, |
| 43 | + ) |
| 44 | + ) |
| 45 | + |
| 46 | + t.add_output( |
| 47 | + Output(name + "RoleName", Value=Ref(role)) |
| 48 | + ) |
| 49 | + t.add_output( |
| 50 | + Output(name + "RoleArn", Value=GetAtt(role.title, "Arn")) |
| 51 | + ) |
| 52 | + |
| 53 | + self.roles.append(role) |
| 54 | + return role |
| 55 | + |
| 56 | + def create_ec2_role(self, name): |
| 57 | + return self.create_role(name, get_default_assumerole_policy()) |
| 58 | + |
| 59 | + def create_lambda_role(self, name): |
| 60 | + return self.create_role(name, get_lambda_assumerole_policy()) |
| 61 | + |
| 62 | + def generate_policy_statements(self): |
| 63 | + """Should be overridden on a subclass to create policy statements. |
| 64 | +
|
| 65 | + By subclassing this blueprint, and overriding this method to generate |
| 66 | + a list of :class:`awacs.aws.Statement` types, a |
| 67 | + :class:`troposphere.iam.PolicyType` will be created and attached to |
| 68 | + the roles specified here. |
| 69 | +
|
| 70 | + If not specified, no Policy will be created. |
| 71 | + """ |
| 72 | + |
| 73 | + return [] |
| 74 | + |
| 75 | + def create_policy(self, name): |
| 76 | + statements = self.generate_policy_statements() |
| 77 | + if not statements: |
| 78 | + return |
| 79 | + |
| 80 | + t = self.template |
| 81 | + policy_prefix = self.context.get_fqn(self.name) |
| 82 | + |
| 83 | + policy = t.add_resource( |
| 84 | + iam.PolicyType( |
| 85 | + "{}Policy".format(name), |
| 86 | + PolicyName="{}-{}-policy".format(policy_prefix, name), |
| 87 | + PolicyDocument=Policy( |
| 88 | + Statement=statements, |
| 89 | + ), |
| 90 | + Roles=[Ref(role) for role in self.roles], |
| 91 | + ) |
| 92 | + ) |
| 93 | + |
| 94 | + t.add_output( |
| 95 | + Output(name + "PolicyName", Value=Ref(policy)) |
| 96 | + ) |
| 97 | + self.policies.append(policy) |
| 98 | + |
| 99 | + def create_template(self): |
| 100 | + variables = self.get_variables() |
| 101 | + |
| 102 | + for role in variables['Ec2Roles']: |
| 103 | + self.create_ec2_role(role) |
| 104 | + |
| 105 | + for role in variables['LambdaRoles']: |
| 106 | + self.create_lambda_role(role) |
| 107 | + |
| 108 | + self.create_policy() |
0 commit comments