-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate.py
232 lines (202 loc) · 6.94 KB
/
create.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
import time
import json
import uuid
import argparse
import requests
import subprocess
parser = argparse.ArgumentParser(
description='Creates AWS Enterprise app on Azure')
parser.add_argument(
'--appname', type=str, help='Application name', required=False, default="")
parser.add_argument(
'--appdesc',
type=str,
help='Application description',
required=False,
default="")
parser.add_argument(
'--email',
type=str,
help='SAML Signing Certificate Notification Email',
required=False,
default="")
parser.add_argument(
'--accountid', type=str, help='AWS Account ID', required=False, default="")
parser.add_argument(
'--accesskey',
type=str,
help='AWS Access Key ID',
required=False,
default="")
parser.add_argument(
'--secretkey',
type=str,
help='AWS Secret Access Key',
required=False,
default="")
args = parser.parse_args()
email = args.email
app_name = args.appname
app_description = args.appdesc
account_id = args.accountid
aws_access_key_id = args.accesskey
aws_secret_access_key = args.secretkey
idp_identifier = "https://signin.aws.amazon.com/saml#%s" % account_id
api_url = "https://main.iam.ad.ext.azure.com/api"
access_token = json.loads(
subprocess.check_output([
"az", "account", "get-access-token", "--resource",
"74658136-14ec-4630-ad9b-26e160ff0fc6"
]))['accessToken']
headers = {
'Content-type': 'application/json',
'Authorization': 'Bearer ' + access_token,
'x-ms-client-request-id': str(uuid.uuid4())
}
def create_app():
try:
print("Creating enterprise application name: %s" % app_name)
with open('payloads/create_app.json') as f:
payload = json.load(f)
payload.update({
"displayName": app_name,
"description": app_description
})
s = requests.Session()
s.headers.update(headers)
response = s.post(
api_url + '/GalleryApplications/galleryApplications',
data=json.dumps(payload))
response = json.loads(response.content)
return response['objectId'], response['appId']
except Exception as e:
print('Failed to create applicaiton')
print(e)
def enable_saml(app_objectId):
try:
print("Enable SAML SSO for enterprise application")
payload = {"currentSingleSignOnMode": "federated", "signInUrl": None}
s = requests.Session()
s.headers.update(headers)
response = s.post(
api_url + '/ApplicationSso/' + app_objectId + '/SingleSignOn',
data=json.dumps(payload))
return response.status_code
except Exception as e:
print('Failed to enable saml')
print(e)
def set_saml(app_objectId):
try:
print("Setting enterprise application %s saml settings" % app_name)
with open('payloads/set_saml.json') as f:
payload = json.load(f)
payload.update({
"objectId": app_objectId,
"certificateNotificationEmail": email,
"idpReplyUrl": "https://signin.aws.amazon.com/saml",
"replyUrls": ["https://signin.aws.amazon.com/saml"],
"idpIdentifier": idp_identifier,
"identifierUris": [
idp_identifier,
]
})
s = requests.Session()
s.headers.update(headers)
response = s.post(
api_url + '/ApplicationSso/' + app_objectId +
'/FederatedSsoConfigV2',
data=json.dumps(payload))
return response.status_code
except Exception as e:
print('Failed to set saml settings')
print(e)
def set_saml_claims(app_objectId):
try:
print("Setting enterprise application %s saml claims" % app_name)
with open('payloads/set_saml.json') as f:
payload = json.load(f)
payload.update({
"objectId": app_objectId,
"certificateNotificationEmail": email,
"idpReplyUrl": "https://signin.aws.amazon.com/saml",
"replyUrls": ["https://signin.aws.amazon.com/saml"],
"idpIdentifier": idp_identifier,
"identifierUris": [
idp_identifier,
]
})
s = requests.Session()
s.headers.update(headers)
response = s.post(
api_url + '/ApplicationSso/' + app_objectId +
'/FederatedSsoClaimsPolicyV2',
data=json.dumps(payload))
return response.status_code
except Exception as e:
print('Failed to set saml claims')
print(e)
def create_provisioning_template(app_objectId):
try:
print("Setting enterprise application %s provisioning template" %
app_name)
with open('payloads/create_provisioningtemplate.json') as f:
payload = json.load(f)
s = requests.Session()
s.headers.update(headers)
response = s.post(
api_url + '/UserProvisioning/' + app_objectId +
'/ProvisioningTasks',
data=json.dumps(payload))
return response.status_code
except Exception as e:
print('Failed to set provisioning template')
print(e)
def set_aws_creds(app_objectId):
try:
print("Setting enterprise application %s aws credentials" % app_name)
with open('payloads/set_awscredentials.json') as f:
payload = json.load(f)
payload['fieldValues'].update({
"clientsecret": aws_access_key_id,
"secrettoken": aws_secret_access_key
})
s = requests.Session()
s.headers.update(headers)
response = s.put(
api_url + '/UserProvisioning/' + app_objectId + '/Credentials',
data=json.dumps(payload))
return response.status_code
except Exception as e:
print('Failed to set aws credentials')
print(e)
def start_provisioning(app_objectId):
try:
print("Setting enterprise application %s to start provisioning" %
app_name)
headers.update({'Content-Length': '0'})
s = requests.Session()
s.headers.update(headers)
response = s.post(
api_url + '/UserProvisioning/' + app_objectId + '/start', )
return response.status_code
except Exception as e:
print('Failed to start provisioning')
print(e)
try:
app_objectId, app_appId = create_app()
time.sleep(5)
if enable_saml(app_objectId) == 204:
if create_provisioning_template(app_objectId):
time.sleep(5)
set_aws_creds(app_objectId)
time.sleep(15)
if set_saml(app_objectId) == 204:
time.sleep(5)
if set_saml_claims(app_objectId) == 204:
time.sleep(5)
start_provisioning(app_objectId)
print("Aplication %s with appId: %s created." %
(app_name, app_appId))
except Exception as e:
print("Application %s creation failed" % app_name)
print(e)