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

Addressed a bug with AWS UC Role Update. Adding unit tests. #1429

Merged
merged 1 commit 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
67 changes: 35 additions & 32 deletions src/databricks/labs/ucx/assessment/aws.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,18 +216,20 @@ 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": [self._databricks_trust_statement(external_id)],
}
)

@staticmethod
def _databricks_trust_statement(external_id="0000"):
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}},
}
],
"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 @@ -285,7 +287,7 @@ 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._get_json_for_cli(self._aws_role_trust_doc()))
return self._create_role(role_name, self._aws_role_trust_doc())

def update_uc_trust_role(self, role_name: str, external_id: str = "0000") -> str | None:
"""
Expand All @@ -299,27 +301,28 @@ def update_uc_trust_role(self, role_name: str, external_id: str = "0000") -> str
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)
if policy_document and policy_document.get("Statement"):
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._databricks_trust_statement(external_id)
policy_document_json = self._get_json_for_cli(policy_document)
else:
policy_document_json = 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._get_json_for_cli(policy_document)}"
f"iam update-assume-role-policy --role-name {role_name} " f"--policy-document {policy_document_json}"
)
if not update_role:
return None
Expand Down
55 changes: 49 additions & 6 deletions tests/unit/assessment/test_aws.py
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,7 @@ def command_call(cmd: str):
) in command_calls


def test_update_uc_trust_role(mocker):
def test_update_uc_trust_role_append(mocker):
command_calls = []
mocker.patch("shutil.which", return_value="/path/aws")

Expand Down Expand Up @@ -592,11 +592,54 @@ def command_call(cmd: str):
assert (
'/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"}}}]}]} '
'{"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":"1234"}}}]} '
'--output json'
) in command_calls


def test_update_uc_trust_role(mocker):
command_calls = []
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",
"Description": "",
"MaxSessionDuration": 3600,
"RoleLastUsed": {}
}
}
""",
"",
)
command_calls.append(cmd)
return 0, '{"Role": {"Arn": "arn:aws:iam::123456789012:role/Test-Role"}}', ""

aws = AWSResources("Fake_Profile", command_call)
aws.update_uc_trust_role("test_role", "1234")
assert (
'/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::414351767826:role/unity-catalog-prod-UCMasterRole-14S5ZJVKOTYTL"},'
'"Action":"sts:AssumeRole","Condition":{"StringEquals":{"sts:ExternalId":"1234"}}}]} '
'--output json'
) in command_calls

Expand Down
Loading