Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modified update existing role to amend the AssumeRole statement rather than rewriting it. #1423

Merged
merged 2 commits into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 40 additions & 17 deletions src/databricks/labs/ucx/assessment/aws.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,21 +216,19 @@ def _s3_actions(self, actions):
return s3_actions

def _aws_role_trust_doc(self, external_id="0000"):
return self._get_json_for_cli(
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::414351767826:role/unity-catalog-prod-UCMasterRole-14S5ZJVKOTYTL"
},
"Action": "sts:AssumeRole",
"Condition": {"StringEquals": {"sts:ExternalId": external_id}},
}
],
}
)
return {
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::414351767826:role/unity-catalog-prod-UCMasterRole-14S5ZJVKOTYTL"
},
"Action": "sts:AssumeRole",
"Condition": {"StringEquals": {"sts:ExternalId": external_id}},
}
],
}

def _aws_s3_policy(self, s3_prefixes, account_id, role_name, kms_key=None):
"""
Expand Down Expand Up @@ -287,16 +285,41 @@ def create_uc_role(self, role_name: str) -> str | None:
the AssumeRole condition will be modified later with the external ID captured from the UC credential.
https://docs.databricks.com/en/connect/unity-catalog/storage-credentials.html
"""
return self._create_role(role_name, self._aws_role_trust_doc())
return self._create_role(role_name, self._get_json_for_cli(self._aws_role_trust_doc()))

def update_uc_trust_role(self, role_name: str, external_id: str = "0000") -> str | None:
"""
Modify an existing IAM role for Unity Catalog to access the S3 buckets with the external ID
captured from the UC credential.
https://docs.databricks.com/en/connect/unity-catalog/storage-credentials.html
"""
role_document = self._run_json_command(f"iam get-role --role-name {role_name}")
role = role_document.get("Role")
if not role:
logger.error(f"Role {role_name} doesn't exist")
return None
policy_document = role.get("AssumeRolePolicyDocument")
if not policy_document:
logger.error(f"Role {role_name} doesn't have an AssumeRolePolicyDocument")
return None
for idx, statement in enumerate(policy_document["Statement"]):
effect = statement.get("Effect")
action = statement.get("Action")
principal = statement.get("Principal")
if not (effect and action and principal):
continue
if effect != "Allow":
continue
if action != "sts:AssumeRole":
continue
principal = principal.get("AWS")
if not principal:
continue
if not self._is_uc_principal(principal):
continue
policy_document["Statement"][idx] = self._aws_role_trust_doc(external_id)
update_role = self._run_json_command(
f"iam update-assume-role-policy --role-name {role_name} --policy-document {self._aws_role_trust_doc(external_id)}"
f"iam update-assume-role-policy --role-name {role_name} --policy-document {self._get_json_for_cli(policy_document)}"
)
if not update_role:
return None
Expand Down
44 changes: 43 additions & 1 deletion tests/unit/assessment/test_aws.py
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,45 @@ def test_update_uc_trust_role(mocker):
mocker.patch("shutil.which", return_value="/path/aws")

def command_call(cmd: str):
if "iam get-role" in cmd:
return (
0,
"""
{
"Role": {
"Path": "/",
"RoleName": "Test-Role",
"RoleId": "ABCD",
"Arn": "arn:aws:iam::0123456789:role/Test-Role",
"CreateDate": "2024-01-01T12:00:00+00:00",
"AssumeRolePolicyDocument": {
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::0123456789:root"
},
"Action": "sts:AssumeRole"
},
{
"Effect":"Allow",
"Principal": {
"AWS":"arn:aws:iam::414351767826:role/unity-catalog-prod-UCMasterRole-14S5ZJVKOTYTL"
},
"Action":"sts:AssumeRole",
"Condition":{"StringEquals":{"sts:ExternalId":"00000"}}
}
]
},
"Description": "",
"MaxSessionDuration": 3600,
"RoleLastUsed": {}
}
}
""",
"",
)
command_calls.append(cmd)
return 0, '{"Role": {"Arn": "arn:aws:iam::123456789012:role/Test-Role"}}', ""

Expand All @@ -554,8 +593,11 @@ def command_call(cmd: str):
'/path/aws iam update-assume-role-policy --role-name test_role '
'--policy-document '
'{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Principal":'
'{"AWS":"arn:aws:iam::0123456789:root"},"Action":"sts:AssumeRole"},'
'{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Principal":'
'{"AWS":"arn:aws:iam::414351767826:role/unity-catalog-prod-UCMasterRole-14S5ZJVKOTYTL"}'
',"Action":"sts:AssumeRole","Condition":{"StringEquals":{"sts:ExternalId":"1234"}}}]} --output json'
',"Action":"sts:AssumeRole","Condition":{"StringEquals":{"sts:ExternalId":"1234"}}}]}]} '
'--output json'
) in command_calls


Expand Down
Loading