12
12
import subprocess
13
13
import sys
14
14
import tempfile
15
+ import time
15
16
import uuid
16
17
import zipfile
17
18
from datetime import datetime , timedelta
18
19
19
- from azure .cli .core import CLIError
20
20
from azure .common .client_factory import get_client_from_cli_profile
21
21
from azure .common .credentials import get_cli_profile
22
22
from azure .core .exceptions import ResourceExistsError
23
23
from azure .cosmosdb .table .tableservice import TableService
24
24
from azure .graphrbac import GraphRbacManagementClient
25
25
from azure .graphrbac .models import (
26
+ Application ,
26
27
ApplicationCreateParameters ,
28
+ ApplicationUpdateParameters ,
27
29
AppRole ,
28
30
GraphErrorException ,
29
31
OptionalClaims ,
48
50
DeploymentMode ,
49
51
DeploymentProperties ,
50
52
)
51
- import time
52
53
from azure .mgmt .storage import StorageManagementClient
53
54
from azure .storage .blob import (
54
55
BlobServiceClient ,
59
60
from msrest .serialization import TZ_UTC
60
61
61
62
from data_migration import migrate
62
- from register_pool_application import (
63
+ from registration import (
63
64
add_application_password ,
64
65
authorize_application ,
65
66
get_application ,
66
67
register_application ,
67
- update_registration ,
68
+ update_pool_registration ,
68
69
)
69
70
70
71
USER_IMPERSONATION = "311a71cc-e848-46a1-bdf8-97ff7156d8e6"
@@ -225,12 +226,11 @@ def create_password(self, object_id):
225
226
while True :
226
227
time .sleep (wait )
227
228
count += 1
228
- try :
229
- return add_application_password (object_id )
230
- except CLIError as err :
231
- if count > timeout_seconds / wait :
232
- raise err
233
- logger .info ("creating password failed, trying again" )
229
+ password = add_application_password (object_id )
230
+ if password :
231
+ return password
232
+ if count > timeout_seconds / wait :
233
+ raise Exception ("creating password failed, trying again" )
234
234
235
235
def setup_rbac (self ):
236
236
"""
@@ -256,6 +256,25 @@ def setup_rbac(self):
256
256
logger .error ("unable to query RBAC. Provide client_id and client_secret" )
257
257
sys .exit (1 )
258
258
259
+ app_roles = [
260
+ AppRole (
261
+ allowed_member_types = ["Application" ],
262
+ display_name = "CliClient" ,
263
+ id = str (uuid .uuid4 ()),
264
+ is_enabled = True ,
265
+ description = "Allows access from the CLI." ,
266
+ value = "CliClient" ,
267
+ ),
268
+ AppRole (
269
+ allowed_member_types = ["Application" ],
270
+ display_name = "ManagedNode" ,
271
+ id = str (uuid .uuid4 ()),
272
+ is_enabled = True ,
273
+ description = "Allow access from a lab machine." ,
274
+ value = "ManagedNode" ,
275
+ ),
276
+ ]
277
+
259
278
if not existing :
260
279
logger .info ("creating Application registration" )
261
280
url = "https://%s.azurewebsites.net" % self .application_name
@@ -273,24 +292,7 @@ def setup_rbac(self):
273
292
resource_app_id = "00000002-0000-0000-c000-000000000000" ,
274
293
)
275
294
],
276
- app_roles = [
277
- AppRole (
278
- allowed_member_types = ["Application" ],
279
- display_name = "CliClient" ,
280
- id = str (uuid .uuid4 ()),
281
- is_enabled = True ,
282
- description = "Allows access from the CLI." ,
283
- value = "CliClient" ,
284
- ),
285
- AppRole (
286
- allowed_member_types = ["Application" ],
287
- display_name = "LabMachine" ,
288
- id = str (uuid .uuid4 ()),
289
- is_enabled = True ,
290
- description = "Allow access from a lab machine." ,
291
- value = "LabMachine" ,
292
- ),
293
- ],
295
+ app_roles = app_roles ,
294
296
)
295
297
app = client .applications .create (params )
296
298
@@ -303,7 +305,27 @@ def setup_rbac(self):
303
305
)
304
306
client .service_principals .create (service_principal_params )
305
307
else :
306
- app = existing [0 ]
308
+ app : Application = existing [0 ]
309
+ existing_role_values = [app_role .value for app_role in app .app_roles ]
310
+ has_missing_roles = any (
311
+ [role .value not in existing_role_values for role in app_roles ]
312
+ )
313
+
314
+ if has_missing_roles :
315
+ # disabling the existing app role first to allow the update
316
+ # this is a requirement to update the application roles
317
+ for role in app .app_roles :
318
+ role .is_enabled = False
319
+
320
+ client .applications .patch (
321
+ app .object_id , ApplicationUpdateParameters (app_roles = app .app_roles )
322
+ )
323
+
324
+ # overriding the list of app roles
325
+ client .applications .patch (
326
+ app .object_id , ApplicationUpdateParameters (app_roles = app_roles )
327
+ )
328
+
307
329
creds = list (client .applications .list_password_credentials (app .object_id ))
308
330
client .applications .update_password_credentials (app .object_id , creds )
309
331
@@ -612,7 +634,7 @@ def deploy_app(self):
612
634
def update_registration (self ):
613
635
if not self .create_registration :
614
636
return
615
- update_registration (self .application_name )
637
+ update_pool_registration (self .application_name )
616
638
617
639
def done (self ):
618
640
logger .info (TELEMETRY_NOTICE )
@@ -766,19 +788,6 @@ def main():
766
788
767
789
logging .getLogger ("deploy" ).setLevel (logging .INFO )
768
790
769
- # TODO: using az_cli resets logging defaults. For now, force these
770
- # to be WARN level
771
- if not args .verbose :
772
- for entry in [
773
- "adal-python" ,
774
- "msrest.universal_http" ,
775
- "urllib3.connectionpool" ,
776
- "az_command_data_logger" ,
777
- "msrest.service_client" ,
778
- "azure.core.pipeline.policies.http_logging_policy" ,
779
- ]:
780
- logging .getLogger (entry ).setLevel (logging .WARN )
781
-
782
791
if args .start_at != states [0 ][0 ]:
783
792
logger .warning (
784
793
"*** Starting at a non-standard deployment state. "
0 commit comments