-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.py
239 lines (211 loc) · 10.3 KB
/
cli.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
import sys
import os
import click
import json
import csmutils.model as csm_model
import csmutils.roles as csm_roles
import csmutils.policies as csm_policies
import csmutils.profiles as csm_profiles
import awsutils.roles as aws_roles
import awsutils.policies as aws_policies
import utils.utils as utils
from utils.context import CSMContext
pass_context = click.make_pass_decorator(CSMContext, ensure=True)
@click.group()
@click.version_option()
@click.option('-v', '--verbose', is_flag=True,
help='Enables verbose mode.')
@click.option('--mfa', is_flag=True,
help='Use MFA for authentication.')
@click.option('--pp', is_flag=True,
help='Enables pretty print mode.')
@click.option('--model_dir', default='json',
help='Directory where model file exists')
@click.option('--model_file', default='model-v2.json',
help='Model file')
@click.option('--templates_folder', default='policy_templates',
help='Policy templates folder')
@click.option('--org_id', help='Org id for ARNs')
@click.option('--dry_run', is_flag=True, default=False,
help='Do not make actual changes to AWS')
@pass_context
def cli(ctx, mfa, verbose, pp, model_dir, model_file, templates_folder, org_id, dry_run):
if 'AWS_REGION' in os.environ:
ctx.region = os.environ['AWS_REGION']
elif 'AWS_DEFAULT_REGION' in os.environ:
ctx.region = os.environ['AWS_DEFAULT_REGION']
else:
ctx.log('Error: AWS Region is not defined', color='red')
sys.exit(1)
if org_id != None:
ctx.orgId = org_id
elif 'AWS_ORG_ID' in os.environ:
orgid = os.environ['AWS_ORG_ID']
if orgid[0] == 'i':
ctx.orgId = orgid[1:]
else:
ctx.orgId = orgid
else:
ctx.log('Error: AWS Org ID is not defined', color='red')
sys.exit(1)
ctx.verbose = verbose
ctx.prettyprint = pp
ctx.dry_run = dry_run
ctx.modelDir = model_dir
ctx.modelFile = model_file
ctx.templateDir = templates_folder
if mfa:
ctx.getTempCredentials()
else:
ctx.setDefaultClients()
########################################################################
## Manage Roles
########################################################################
@cli.group('roles', short_help='manage AWS instance roles')
@pass_context
def roles(ctx):
ctx.templates = utils.loadPolicyTemplates(ctx)
ctx.model = utils.loadModel(ctx)
ctx.modelPolicies = utils.loadModelPolicies(ctx)
csm_policies.getAWSPolicies(ctx)
csm_roles.getAWSRoles(ctx)
@roles.command('audit', short_help='Audit AWS roles and policies')
@click.option('-r','--region', help='Audit only this region')
@click.option('-e','--env', help='Audit only this env')
@click.option('--role', help='Audit only for this role in region-env-role format')
@click.option('--no_diff', is_flag=True, help='Do not show diff output', default=False)
@click.option('--diff_type', type=click.Choice(['context','ndiff','unified']), help='Use context style diff output', default='unified')
@click.option('--context_lines', type=click.INT, help='Number of diff context lines to use.', default=0)
@pass_context
def roles_audit(ctx,region, env, role, no_diff, diff_type, context_lines):
csm_roles.auditRoles(ctx, region, env, role, no_diff, diff_type, context_lines)
@roles.command('compare', short_help='Compare AWS roles to model')
@click.option('-r','--region', help='Compare only for this region')
@click.option('-e','--env', help='Compare only for this env')
@click.option('--role', help='Compare only this role in region-env-role format')
@pass_context
def roles_compare(ctx,region, env, role):
csm_roles.compareRoles(ctx, region, env, role)
@roles.command('update', short_help='Update AWS roles from model')
@click.option('-r','--region', help='Update only for this region')
@click.option('-e','--env', help='Update only for this env')
@click.option('--role', help='Update only for this role in region-env-role format')
@click.option('--constrain', is_flag=True, default=False,help='Constrain policies to the model')
@pass_context
def roles_update(ctx,region, env, role, constrain):
csm_roles.updateRoles(ctx, region, env, role, constrain)
@roles.command('show', short_help='Show AWS roles')
@click.option('-r','--region', help='Show only for this region')
@click.option('-e','--env', help='Show only for this env')
@click.option('--role', help='Show only for this role in region-env-role format')
@pass_context
def roles_show_aws(ctx,region, env, role):
csm_roles.showRoles(ctx, region, env, role)
@roles.command('delete', short_help='Deleta an AWS role')
@click.option('--role', help='Role in region-env-role format')
@pass_context
def roles_delete_aws(ctx, role):
csm_roles.deleteRole(ctx, role)
########################################################################
## Manage Instance Profiles
########################################################################
@cli.group('profiles', short_help='manage AWS instance profiles')
@pass_context
def profiles(ctx):
ctx.templates = utils.loadPolicyTemplates(ctx)
ctx.model = utils.loadModel(ctx)
ctx.modelPolicies = utils.loadModelPolicies(ctx)
csm_policies.getAWSPolicies(ctx)
csm_roles.getAWSRoles(ctx)
@profiles.command('show', short_help='Show AWS Instance Profiles')
@click.option('-r','--region', help='Show only for this region')
@click.option('-e','--env', help='Show only for this env')
@click.option('--profilename', help='Show only for this profile name')
@click.option('--id', help='Show only for this profile id')
@click.option('--instances', is_flag=True, help='Show associated instances', default=False)
@pass_context
def profiles_show(ctx, region, env, profilename, id, instances):
csm_profiles.showInstanceProfiles(ctx, region, env, profilename, id, instances)
########################################################################
## Manage Policies
########################################################################
@cli.group('policies', short_help='manage AWS policies')
@pass_context
def policies(ctx):
ctx.templates = utils.loadPolicyTemplates(ctx)
ctx.model = utils.loadModel(ctx)
ctx.modelPolicies = utils.loadModelPolicies(ctx)
csm_policies.getAWSPolicies(ctx)
csm_roles.getAWSRoles(ctx)
@policies.command('delete', short_help='Compare model and AWS')
@click.option('-p','--policy', help='Create only this Policy')
@pass_context
def policies_delete(ctx, policy):
aws_policies.deletePolicy(ctx, policy)
@policies.command('create', short_help='Compare model and AWS')
@click.option('-r','--region', help='Create only for this region')
@click.option('-e','--env', help='Create only for this env')
@click.option('-s','--service', help='Create only for this service')
@click.option('-p','--policy', help='Create only this Policy')
@pass_context
def policies_create(ctx, region, env, service, policy):
csm_policies.createPolicy(ctx,region,env,service,policy)
@policies.command('compare', short_help='Compare model and AWS')
@click.option('-r','--region', help='Create only for this region')
@click.option('-e','--env', help='Create only for this env')
@click.option('-s','--service', help='Create only for this service')
@click.option('-p','--policy', help='Show only for this policy')
@click.option('--no_diff', is_flag=True, help='Do not show diff output', default=False)
@click.option('--diff_type', type=click.Choice(['context','ndiff','unified']), help='Use context style diff output', default='unified')
@click.option('--context_lines', type=click.INT, help='Number of diff context lines to use.', default=0)
@pass_context
def policies_compare(ctx, region, env, service, policy, no_diff, diff_type, context_lines):
csm_policies.compareAllPolicies(ctx, region, env, service, policy, no_diff, diff_type, context_lines)
@policies.command('update', short_help='Compare model and AWS')
@click.option('-r','--region', help='Create only for this region')
@click.option('-e','--env', help='Create only for this env')
@click.option('-s','--service', help='Create only for this service')
@click.option('-p','--policy', help='Show only for this policy')
@click.option('--constrain', is_flag=True, default=False,help='Constrain policies to the model')
@click.option('--force', is_flag=True, default=False,help='Force a document upgrade, even if it matches')
@pass_context
def policies_update(ctx, region, env, service, policy, constrain, force):
csm_policies.updatePolicies(ctx, region, env, service, policy, constrain, force)
@policies.command('show', short_help='Show the current AWS policy(s)')
@click.option('-r','--region', help='Create only for this region')
@click.option('-e','--env', help='Create only for this env')
@click.option('-s','--service', help='Create only for this service')
@click.option('-p','--policy', help='Show only for this policy')
@pass_context
def policies_show_aws_policy(ctx, region, env, service, policy):
csm_policies.showAWSPolicy(ctx, region, env, service, policy)
@policies.command('show_unattached', short_help='Show all unattached policies not in the model')
@pass_context
def policies_show_model_policy(ctx):
notInModel = True
unattached = True
csm_policies.showAWS(ctx, notInModel, unattached)
########################################################################
## Model Commands
########################################################################
@cli.group('model', short_help='manage the model')
@pass_context
def model(ctx):
ctx.templates = utils.loadPolicyTemplates(ctx)
ctx.model = utils.loadModel(ctx)
ctx.modelPolicies = utils.loadModelPolicies(ctx)
#csm_policies.getAWSPolicies(ctx)
#csm_roles.getAWSRoles(ctx)
@model.command('show', short_help='Show the model')
@click.option('-r','--region', help='Create only for this region')
@click.option('-e','--env', help='Create only for this env')
@click.option('--role', help='Create only for this role')
@click.option('-s','--service', help='Create only for this service')
@click.option('-p','--policy', help='Show only for this policy')
@pass_context
def model_show(ctx, region, env, role, service, policy):
csm_model.showModel(ctx, region, env, role, service, policy)
@model.command('json', short_help='Show the instantiated model object')
@pass_context
def model_json(ctx):
csm_model.showModeJson(ctx)