Skip to content
This repository was archived by the owner on Nov 1, 2023. It is now read-only.

Commit e76064b

Browse files
authored
assign scaleset to a role (#185)
1 parent 59cfc52 commit e76064b

File tree

3 files changed

+207
-119
lines changed

3 files changed

+207
-119
lines changed

src/deployment/deploy.py

Lines changed: 52 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,20 @@
1212
import subprocess
1313
import sys
1414
import tempfile
15+
import time
1516
import uuid
1617
import zipfile
1718
from datetime import datetime, timedelta
1819

19-
from azure.cli.core import CLIError
2020
from azure.common.client_factory import get_client_from_cli_profile
2121
from azure.common.credentials import get_cli_profile
2222
from azure.core.exceptions import ResourceExistsError
2323
from azure.cosmosdb.table.tableservice import TableService
2424
from azure.graphrbac import GraphRbacManagementClient
2525
from azure.graphrbac.models import (
26+
Application,
2627
ApplicationCreateParameters,
28+
ApplicationUpdateParameters,
2729
AppRole,
2830
GraphErrorException,
2931
OptionalClaims,
@@ -48,7 +50,6 @@
4850
DeploymentMode,
4951
DeploymentProperties,
5052
)
51-
import time
5253
from azure.mgmt.storage import StorageManagementClient
5354
from azure.storage.blob import (
5455
BlobServiceClient,
@@ -59,12 +60,12 @@
5960
from msrest.serialization import TZ_UTC
6061

6162
from data_migration import migrate
62-
from register_pool_application import (
63+
from registration import (
6364
add_application_password,
6465
authorize_application,
6566
get_application,
6667
register_application,
67-
update_registration,
68+
update_pool_registration,
6869
)
6970

7071
USER_IMPERSONATION = "311a71cc-e848-46a1-bdf8-97ff7156d8e6"
@@ -225,12 +226,11 @@ def create_password(self, object_id):
225226
while True:
226227
time.sleep(wait)
227228
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")
234234

235235
def setup_rbac(self):
236236
"""
@@ -256,6 +256,25 @@ def setup_rbac(self):
256256
logger.error("unable to query RBAC. Provide client_id and client_secret")
257257
sys.exit(1)
258258

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+
259278
if not existing:
260279
logger.info("creating Application registration")
261280
url = "https://%s.azurewebsites.net" % self.application_name
@@ -273,24 +292,7 @@ def setup_rbac(self):
273292
resource_app_id="00000002-0000-0000-c000-000000000000",
274293
)
275294
],
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,
294296
)
295297
app = client.applications.create(params)
296298

@@ -303,7 +305,27 @@ def setup_rbac(self):
303305
)
304306
client.service_principals.create(service_principal_params)
305307
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+
307329
creds = list(client.applications.list_password_credentials(app.object_id))
308330
client.applications.update_password_credentials(app.object_id, creds)
309331

@@ -612,7 +634,7 @@ def deploy_app(self):
612634
def update_registration(self):
613635
if not self.create_registration:
614636
return
615-
update_registration(self.application_name)
637+
update_pool_registration(self.application_name)
616638

617639
def done(self):
618640
logger.info(TELEMETRY_NOTICE)
@@ -766,19 +788,6 @@ def main():
766788

767789
logging.getLogger("deploy").setLevel(logging.INFO)
768790

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-
782791
if args.start_at != states[0][0]:
783792
logger.warning(
784793
"*** Starting at a non-standard deployment state. "

0 commit comments

Comments
 (0)