Skip to content

Commit 33114b6

Browse files
svia3svia3
andauthored
add SSO user support + add_env_action to dz-import (#4782)
Co-authored-by: svia3 <[email protected]>
1 parent cddb473 commit 33114b6

File tree

2 files changed

+50
-16
lines changed

2 files changed

+50
-16
lines changed

ml_ops/sm-datazone_import/import-sagemaker-domain.py

Lines changed: 50 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414

1515
class SageMakerDomainImporter:
16-
1716
def __init__(self, region, stage, federation_role, account_id) -> None:
1817
self.region = region
1918
self.stage = stage
@@ -171,7 +170,7 @@ def _map_users(self):
171170

172171
# get role name from arn "arn:aws:iam::047923724610:role/service-role/AmazonSageMaker-ExecutionRole-20241008T155288"
173172
role_name = self.sm_user_info["exec_role_arn"][
174-
self.sm_user_info["exec_role_arn"].rfind("/") + 1 :
173+
self.sm_user_info["exec_role_arn"].rfind("/") + 1:
175174
] # rfind searches from back of string
176175
self.iam_client.tag_role(RoleName=role_name, Tags=sm_exec_role_tags)
177176
print(
@@ -183,21 +182,38 @@ def _map_users(self):
183182
print(t)
184183

185184
print("--------------------------------------------------------------------")
186-
print("Getting DataZone UserProfiles in account...")
187-
dz_users = self.dz_client.search_user_profiles(
188-
domainIdentifier=self.dz_domain_id, userType="DATAZONE_IAM_USER"
189-
)["items"]
185+
print("Getting IAM DataZone UserProfiles in account...")
190186

191-
dz_users_map_id_to_arn = {}
192-
for dz_user in dz_users:
193-
user_role_arn = dz_user["details"]["iam"]["arn"]
194-
dz_user_id = dz_user["id"]
195-
print(f"UserId: {dz_user_id} UserRole: {user_role_arn}")
196-
dz_users_map_id_to_arn[dz_user_id] = user_role_arn
187+
user_types = [
188+
"SSO_USER",
189+
# remove DATAZONE_USER as these are redundant with others.
190+
"DATAZONE_SSO_USER",
191+
"DATAZONE_IAM_USER",
192+
]
193+
all_dz_users = [] # [( payload, type ), ... ]
194+
for user_type in user_types:
195+
search_response = self.dz_client.search_user_profiles(
196+
domainIdentifier=self.dz_domain_id, userType=user_type
197+
)["items"]
198+
dz_user_map = {"Items": search_response, "Type": user_type}
199+
all_dz_users.append(dz_user_map)
200+
201+
# For all user types, iterate through all users.
202+
for dz_user_map in all_dz_users:
203+
dz_users, user_type = dz_user_map["Items"], dz_user_map["Type"]
204+
for dz_user in dz_users:
205+
user_name = "None"
206+
if user_type == "DATAZONE_IAM_USER":
207+
user_name = dz_user["details"]["iam"]["arn"]
208+
if user_type == "SSO_USER" or user_type == "DATAZONE_SSO_USER":
209+
user_name = dz_user["details"]["sso"]["username"]
210+
211+
dz_user_id = dz_user["id"]
212+
print(f"UserId: {dz_user_id}\tUserType: {user_type}\tUser: {user_name}")
197213

198214
self.dz_users_id_list = []
199215
while True:
200-
dz_uzer = input("Enter a DataZone UserId to map to (or 'done' to finish): ")
216+
dz_uzer = input("Enter a DataZone UserId to map to ('done' to finish): ")
201217
if dz_uzer == "done":
202218
break
203219
self.dz_users_id_list.append(dz_uzer)
@@ -295,6 +311,24 @@ def _configure_environment(self):
295311

296312
return self.env_id
297313

314+
def _add_environment_action(self):
315+
items = self.dz_client.list_environment_actions(
316+
domainIdentifier=self.dz_domain_id, environmentIdentifier=self.env_id
317+
)["items"]
318+
sm_env_action = None
319+
for item in items:
320+
if "sageMaker" in item["parameters"]:
321+
sm_env_action = item
322+
323+
if sm_env_action is None:
324+
self.dz_client.create_environment_action(
325+
domainIdentifier=self.dz_domain_id,
326+
environmentIdentifier=self.env_id,
327+
name="SageMaker Environment Action Link",
328+
description="Link from DataZone Data Portal to SageMaker Studio",
329+
parameters={"sageMaker": {}},
330+
)
331+
298332
def _associate_fed_role(self):
299333
# Associate fed role
300334
print("--------------------------------------------------------------------")
@@ -397,7 +431,7 @@ def _debug_print_results(self):
397431
f"Listing linked items for domain {self.dz_domain_id}, project {self.dz_project_id}, and environment {self.env_id}."
398432
)
399433
list_result = self.byod_client.list_linked_types(
400-
self.dz_domain_id,
434+
domainIdentifier=self.dz_domain_id,
401435
projectIdentifier=self.dz_project_id,
402436
environmentIdentifier=self.env_id,
403437
)
@@ -418,10 +452,10 @@ def _get_env_link(self):
418452
link = link_response["actionLink"]
419453
print(link)
420454
except botocore.exceptions.ClientError as error:
421-
print(error)
422455
print(
423456
"Environment action link could not be generated - this is most likely due to the current principal is not a user of the DataZone project."
424457
)
458+
print(error)
425459

426460
def import_interactive(self):
427461
print(
@@ -434,6 +468,7 @@ def import_interactive(self):
434468
self._map_users()
435469
self._configure_blueprint()
436470
self._configure_environment()
471+
self._add_environment_action()
437472
self._associate_fed_role()
438473
self._link_domain()
439474
self._link_users()

ml_ops/sm-datazone_import/offboard-sagemaker-domain.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111

1212
class SageMakerDomainOffboarder:
13-
1413
def _offboard_sm_domain(self):
1514
print("List of SageMaker Domains for your account.")
1615
print("--------------------------------------------------------------------")

0 commit comments

Comments
 (0)