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

Add Additional Error Messages for KMS Key lookup on imported dataset #748

Merged
merged 1 commit into from
Sep 15, 2023
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
17 changes: 17 additions & 0 deletions backend/dataall/modules/dataset_sharing/aws/kms_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,20 @@ def get_key_id(self, key_alias: str):
return None
else:
return response['KeyMetadata']['KeyId']

def check_key_exists(self, key_alias: str):
try:
key_exist = False
paginator = self._client.get_paginator('list_aliases')
for page in paginator.paginate():
key_aliases = [alias["AliasName"] for alias in page['Aliases']]
if key_alias in key_aliases:
key_exist = True
break
except Exception as e:
log.error(
f'Failed to list kms key aliases in account {self._account_id}: {e}'
)
return None
else:
return key_exist
11 changes: 10 additions & 1 deletion backend/dataall/modules/datasets/services/dataset_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,22 @@ def check_dataset_account(session, environment):
def check_imported_resources(environment, data):
kms_alias = data.get('KmsKeyAlias')
if kms_alias not in [None, "Undefined", "", "SSE-S3"]:
key_exists = KmsClient(environment.AwsAccountId, environment.region).check_key_exists(
key_alias=f"alias/{kms_alias}"
)
if not key_exists:
raise exceptions.AWSResourceNotFound(
action=IMPORT_DATASET,
message=f'KMS key with alias={kms_alias} cannot be found - Please check if KMS Key Alias exists in account {environment.AwsAccountId}',
)

key_id = KmsClient(environment.AwsAccountId, environment.region).get_key_id(
key_alias=f"alias/{kms_alias}"
)
if not key_id:
raise exceptions.AWSResourceNotFound(
action=IMPORT_DATASET,
message=f'KMS key with alias={kms_alias} cannot be found',
message=f'Data.all Environment Pivot Role does not have kms:DescribeKey Permission to KMS key with alias={kms_alias}',
)
return True

Expand Down