/
iam_users.py
169 lines (158 loc) · 7.57 KB
/
iam_users.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
from awacs.aws import Allow, Deny, Action, Principal, Statement, PolicyDocument, MultiFactorAuthPresent, Condition
from awacs.aws import Bool as AWACSBool
from awacs.sts import AssumeRole
from paco import utils
from paco.cftemplates.cftemplates import StackTemplate
import random
import string
import troposphere
import troposphere.iam
class IAMUsers(StackTemplate):
def __init__(self, stack, paco_ctx):
super().__init__(stack, paco_ctx, iam_capabilities=['CAPABILITY_NAMED_IAM'])
self.set_aws_name('Accounts')
# Troposphere Template Initialization
self.init_template('IAM Users')
template = self.template
# IAM Users
is_enabled = False
for iam_user in self.resource.values():
if iam_user.is_enabled() == False:
continue
self.add_iam_user(iam_user)
is_enabled = True
self.set_enabled(is_enabled)
def add_iam_user(self, iam_user):
# Parameters
username_param = self.create_cfn_parameter(
name=self.create_cfn_logical_id('Username'+utils.md5sum(str_data=iam_user.username)),
param_type='String',
description='The name of the user.',
value=iam_user.username,
)
if iam_user.console_access_enabled == True:
user_password = utils.md5sum(str_data=iam_user.username)[:8]
if self.paco_ctx.legacy_flag('iam_user_default_password_2019_10_12') == False:
user_password += '@Aim19!'
print("{}: default password: {}".format(iam_user.username, user_password))
password_param = self.create_cfn_parameter(
name=self.create_cfn_logical_id('Password'+utils.md5sum(str_data=user_password)),
param_type='String',
description='The password to assign of the user',
noecho=True,
value=user_password,
)
# IAMUser Resource
iam_user_dict = {
'UserName': troposphere.Ref(username_param),
}
# Console Access
if iam_user.console_access_enabled == True:
iam_user_dict['LoginProfile'] = {
'Password': troposphere.Ref(password_param),
'PasswordResetRequired': True
}
iam_user_res = troposphere.iam.User.from_dict(
self.create_cfn_logical_id('IAMUser'+iam_user.name),
iam_user_dict
)
self.template.add_resource(iam_user_res)
# Account Delegate Assume Role
# - A list of account delegate roles in each of the accounts
assume_role_arn_list = []
account_list = iam_user.account_whitelist
if iam_user.account_whitelist[0] == 'all':
account_list = self.paco_ctx.project['accounts'].keys()
for account_name in account_list:
account_ref = 'paco.ref accounts.'+account_name
account_id = self.paco_ctx.get_ref(account_ref+'.id')
delegate_role_arn = "arn:aws:iam::{}:role/IAM-User-Account-Delegate-Role-{}".format(
account_id,
self.create_resource_name(iam_user.name, filter_id='IAM.Role.RoleName')
)
assume_role_arn_list.append(delegate_role_arn)
if len(assume_role_arn_list) > 0:
user_policy_dict = {
'ManagedPolicyName': 'IAM-User-AssumeRole-Policy-{}'.format(
self.create_resource_name(iam_user.name, '-').capitalize()
),
'PolicyDocument': PolicyDocument(
Version="2012-10-17",
Statement=[
Statement(
Effect=Allow,
Action=[AssumeRole],
Resource=assume_role_arn_list
),
Statement(
Sid='AllowViewAccountInfo',
Effect=Allow,
Action=[
Action('iam', 'GetAccountPasswordPolicy'),
Action('iam', 'GetAccountSummary'),
Action('iam', 'ListVirtualMFADevices'),
Action('iam', 'ListUsers'),
],
Resource=['*']
),
Statement(
Sid='AllowManageOwnPasswords',
Effect=Allow,
Action=[
Action('iam', 'ChangePassword'),
Action('iam', 'GetUser'),
],
Resource=['arn:aws:iam::*:user/{}'.format(iam_user.username)]
),
Statement(
Sid='AllowManageOwnVirtualMFADevice',
Effect=Allow,
Action=[
Action('iam', 'CreateVirtualMFADevice'),
Action('iam', 'DeleteVirtualMFADevice'),
],
Resource=['arn:aws:iam::*:mfa/{}'.format(iam_user.username)]
),
Statement(
Sid='AllowManageOwnUserMFA',
Effect=Allow,
Action=[
Action('iam', 'DeactivateMFADevice'),
Action('iam', 'EnableMFADevice'),
Action('iam', 'ListMFADevices'),
Action('iam', 'ResyncMFADevice'),
],
Resource=['arn:aws:iam::*:user/{}'.format(iam_user.username)]
),
Statement(
Sid='DenyAllExceptListedIfNoMFA',
Effect=Deny,
NotAction=[
Action('iam', 'CreateVirtualMFADevice'),
Action('iam', 'EnableMFADevice'),
Action('iam', 'ChangePassword'),
Action('iam', 'GetUser'),
Action('iam', 'ListMFADevices'),
Action('iam', 'ListVirtualMFADevices'),
Action('iam', 'ResyncMFADevice'),
Action('sts', 'GetSessionToken'),
Action('iam', 'ListUsers'),
],
Resource=['*'],
Condition=Condition(
[
AWACSBool({
MultiFactorAuthPresent: False
})
]
)
) ],
),
'Users': [troposphere.Ref(iam_user_res)]
}
# Policy
user_policy_res = troposphere.iam.ManagedPolicy.from_dict(
self.create_cfn_logical_id('IAMUserPolicy'+iam_user.name),
user_policy_dict
)
self.template.add_resource(user_policy_res)