-
Notifications
You must be signed in to change notification settings - Fork 0
/
iam_import.py
executable file
·60 lines (45 loc) · 2.66 KB
/
iam_import.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
#!/usr/bin/python
# -*- coding: utf8 -*-
import boto3
import json
import sys
session = boto3.Session(profile_name=sys.argv[1])
client = session.client('iam')
settings=json.load(open("iam_export.json"))
for g in settings['GroupDetailList']:
client.create_group(Path=g['Path'],GroupName=g['GroupName'])
for u in settings['UserDetailList']:
client.create_user(Path=u['Path'],UserName=u['UserName'])
for ug in u['GroupList']:
client.add_user_to_group(GroupName=ug,UserName=u['UserName'])
for r in settings['RoleDetailList']:
if r['Path'].startswith('/aws-service-role/'):
client.create_service_linked_role(AWSServiceName=r['Path'][18:-1])
else:
client.create_role(Path=r['Path'],RoleName=r['RoleName'],AssumeRolePolicyDocument=json.dumps(r['AssumeRolePolicyDocument']),MaxSessionDuration=3600)
for q in settings['RoleDetailList']:
for uq in q['InstanceProfileList']:
client.add_role_to_instance_profile(RoleName=q['RoleName'],InstanceProfileName=client.create_instance_profile(InstanceProfileName=uq['InstanceProfileName'])['InstanceProfile']['InstanceProfileName'])
for q in settings['UserDetailList']:
if 'UserPolicyList' in q:
for uq in q['UserPolicyList']:
client.attach_user_policy(PolicyArn=client.create_policy(PolicyName=uq['PolicyName'],Path=q['Path'],PolicyDocument=json.dumps(uq['PolicyDocument']))['Policy']['Arn'],UserName=q['UserName'])
for q in settings['UserDetailList']:
for uq in q['AttachedManagedPolicies']:
client.attach_user_policy(PolicyArn=uq['PolicyArn'],UserName=q['UserName'])
for q in settings['GroupDetailList']:
for uq in q['GroupPolicyList']:
client.attach_group_policy(PolicyArn=client.create_policy(PolicyName=uq['PolicyName'],Path=q['Path'],PolicyDocument=json.dumps(uq['PolicyDocument']))['Policy']['Arn'],GroupName=q['GroupName'])
for q in settings['GroupDetailList']:
for uq in q['AttachedManagedPolicies']:
client.attach_group_policy(PolicyArn=uq['PolicyArn'],GroupName=q['GroupName'])
for q in settings['RoleDetailList']:
if not q['Path'].startswith('/aws-service-role/'):
if 'RolePolicyList' in q:
for uq in q['RolePolicyList']:
client.attach_role_policy(PolicyArn=client.create_policy(PolicyName=uq['PolicyName'],Path=q['Path'],PolicyDocument=json.dumps(uq['PolicyDocument']))['Policy']['Arn'],RoleName=q['RoleName'])
for q in settings['RoleDetailList']:
if not q['Path'].startswith('/aws-service-role/'):
for uq in q['AttachedManagedPolicies']:
if not q['Path'].startswith('/service-role/'):
client.attach_role_policy(PolicyArn=uq['PolicyArn'],RoleName=q['RoleName'])