-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathonboard_aws_bulk_roles_multi_org.py
189 lines (159 loc) · 5.34 KB
/
onboard_aws_bulk_roles_multi_org.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
import json
import requests
import getpass
import re
requests.packages.urllib3.disable_warnings() # verify=False throws warnings otherwise
# For each account you want to add, add a new block in aws_accounts
aws_accounts = [
{
"account_name": "Production Acct",
"account_number": 62512450955,
"role_arn": "arn:aws:iam::12435678:role/DivvyCloudCrossAcctRole-Role-SOJ9J0W1B0SO",
"external_id": "divvycloud"
},
{
"account_name": "KB-testtt",
"account_number": 12345654,
"role_arn": "arn:aws:iam::12345654:role/DivvyCloudCrossAcctRole-Role-SOJ9J0W1B0SO",
"external_id": "divvycloud"
}
]
#### Prefix > org mapping:
alias_org_mapping = [
{"alias":"KB",
"org":"KBorg"
},
{"alias":"GP",
"org":"GPorg"
},
{"alias":"bobacct",
"org":"boborg"
}
]
# Username/password to authenticate against the API
username = ""
password = "" # Leave this blank if you don't want it in plaintext and it'll prompt you to input it when running the script.
# API URL
base_url = "https://sales-demo.divvycloud.com"
# Param validation
if not username:
username = input("Username: ")
if not password:
passwd = getpass.getpass('Password:')
else:
passwd = password
if not base_url:
base_url = input("Base URL (EX: http://localhost:8001 or http://45.59.252.4:8001): ")
# Full URL
login_url = base_url + '/v2/public/user/login'
# Shorthand helper function
def get_auth_token():
response = requests.post(
url=login_url,
verify=False,
data=json.dumps({"username": username, "password": passwd}),
headers={
'Content-Type': 'application/json;charset=UTF-8',
'Accept': 'application/json'
})
return response.json()['session_id']
auth_token = get_auth_token()
headers = {
'Content-Type': 'application/json;charset=UTF-8',
'Accept': 'application/json',
'X-Auth-Token': auth_token
}
# Get Org info
def onboard_aws(account_name,account_number,role_arn,external_id):
data = {
"creation_params":
{
"cloud_type":"AWS",
"authentication_type":"instance_assume_role",
"name": account_name,
"account_number":account_number,
"role_arn":role_arn,
"duration":3600,
"external_id": external_id,
"session_name":"DivvyCloud"
}
}
response = requests.post(
url=base_url + '/v2/prototype/cloud/add',
data=json.dumps(data),
verify=False,
headers=headers
)
return response.json()
# Get Org info
def get_orgs():
data = {}
response = requests.get(
url=base_url + '/v2/prototype/domain/organizations/detail/get',
data=json.dumps(data),
verify=False,
headers=headers
)
return response.json()
# Switch to the org to add the account in
def switch_org(name):
data = {"organization_name": name }
response = requests.post(
url=base_url + '/v2/prototype/domain/switch_organization',
data=json.dumps(data),
verify=False,
headers=headers
)
return response
org_list = get_orgs()['organizations']
print("========= List of organizations we can add accounts to: =========")
for org in org_list:
print(org['name'])
print("\n\n=========================================")
# Onboard the accounts
print("Onboarding AWS accounts into DivvyCloud")
print("=========================================")
skipped_accounts = []
added_accounts = []
for account in aws_accounts:
account_name = account['account_name']
account_number = int(account['account_number'])
role_arn = account['role_arn']
external_id = account['external_id']
print("\nWorking on account: " + account_name)
## pull the account name and strip everything pre-hyphen
regex = r"(^[A-Za-z]+)-"
match = re.findall(regex,account_name)
if match:
print("Found an account name prefix to match. Looking for the corresponding org")
org_prefix = match[0]
else:
print("Couldn't find an alias prefix to match to an org. Saving for later parsing.")
skipped_accounts.append(account)
continue
for mapping in alias_org_mapping:
if mapping['alias'] == org_prefix:
print("Switching org to " + mapping['org'] + " and adding account: " + account_name)
# switch org
switch_org(mapping['org'])
# Add account
try:
onboard_output = onboard_aws(account_name,account_number,role_arn,external_id)
except Exception as e:
print ("An error occurred. " + str(e))
try:
if onboard_output['status']:
onboard_status = "Success"
except KeyError:
if onboard_output['error_message']:
onboard_status = "Error"
added_accounts.append("Org: " + mapping['org'] + " Account Name: " + account_name + "| Status: " + onboard_status + " | Account Number: " + str(account_number))
break
print("\n=========================================")
print("Accounts that were attempted to be added:")
for account in added_accounts:
print(account)
print("\n===============================")
print("Accounts that weren't added:")
for account in skipped_accounts:
print(account)