|
| 1 | +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"). You |
| 4 | +# may not use this file except in compliance with the License. A copy of |
| 5 | +# the License is located at |
| 6 | +# |
| 7 | +# http://aws.amazon.com/apache2.0/ |
| 8 | +# |
| 9 | +# or in the "license" file accompanying this file. This file is |
| 10 | +# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF |
| 11 | +# ANY KIND, either express or implied. See the License for the specific |
| 12 | +# language governing permissions and limitations under the License. |
| 13 | +from __future__ import absolute_import |
| 14 | + |
| 15 | +import json |
| 16 | + |
| 17 | +from sagemaker.core.common_utils import aws_partition, sts_regional_endpoint |
| 18 | + |
| 19 | +PRINCIPAL_TEMPLATE = ( |
| 20 | + '["{account_id}", "{role_arn}", ' |
| 21 | + '"arn:{partition}:iam::{account_id}:role/{sagemaker_role}"] ' |
| 22 | +) |
| 23 | + |
| 24 | +KEY_ALIAS = "SageMakerTestKMSKey" |
| 25 | +POLICY_NAME = "default" |
| 26 | +KEY_POLICY = """ |
| 27 | +{{ |
| 28 | + "Version": "2012-10-17", |
| 29 | + "Id": "{id}", |
| 30 | + "Statement": [ |
| 31 | + {{ |
| 32 | + "Sid": "Enable IAM User Permissions", |
| 33 | + "Effect": "Allow", |
| 34 | + "Principal": {{ |
| 35 | + "AWS": {principal} |
| 36 | + }}, |
| 37 | + "Action": "kms:*", |
| 38 | + "Resource": "*" |
| 39 | + }} |
| 40 | + ] |
| 41 | +}} |
| 42 | +""" |
| 43 | + |
| 44 | + |
| 45 | +def _get_kms_key_arn(kms_client, alias): |
| 46 | + try: |
| 47 | + response = kms_client.describe_key(KeyId="alias/" + alias) |
| 48 | + return response["KeyMetadata"]["Arn"] |
| 49 | + except kms_client.exceptions.NotFoundException: |
| 50 | + return None |
| 51 | + |
| 52 | + |
| 53 | +def _get_kms_key_id(kms_client, alias): |
| 54 | + try: |
| 55 | + response = kms_client.describe_key(KeyId="alias/" + alias) |
| 56 | + return response["KeyMetadata"]["KeyId"] |
| 57 | + except kms_client.exceptions.NotFoundException: |
| 58 | + return None |
| 59 | + |
| 60 | + |
| 61 | +def _create_kms_key( |
| 62 | + kms_client, account_id, region, role_arn=None, sagemaker_role="SageMakerRole", alias=KEY_ALIAS |
| 63 | +): |
| 64 | + if role_arn: |
| 65 | + principal = PRINCIPAL_TEMPLATE.format( |
| 66 | + partition=aws_partition(region), |
| 67 | + account_id=account_id, |
| 68 | + role_arn=role_arn, |
| 69 | + sagemaker_role=sagemaker_role, |
| 70 | + ) |
| 71 | + else: |
| 72 | + principal = '"{account_id}"'.format(account_id=account_id) |
| 73 | + |
| 74 | + response = kms_client.create_key( |
| 75 | + Policy=KEY_POLICY.format( |
| 76 | + id=POLICY_NAME, principal=principal, sagemaker_role=sagemaker_role |
| 77 | + ), |
| 78 | + Description="KMS key for SageMaker Python SDK integ tests", |
| 79 | + ) |
| 80 | + key_arn = response["KeyMetadata"]["Arn"] |
| 81 | + |
| 82 | + if alias: |
| 83 | + kms_client.create_alias(AliasName="alias/" + alias, TargetKeyId=key_arn) |
| 84 | + return key_arn |
| 85 | + |
| 86 | + |
| 87 | +def _add_role_to_policy( |
| 88 | + kms_client, account_id, role_arn, region, alias=KEY_ALIAS, sagemaker_role="SageMakerRole" |
| 89 | +): |
| 90 | + key_id = _get_kms_key_id(kms_client, alias) |
| 91 | + policy = kms_client.get_key_policy(KeyId=key_id, PolicyName=POLICY_NAME) |
| 92 | + policy = json.loads(policy["Policy"]) |
| 93 | + principal = policy["Statement"][0]["Principal"]["AWS"] |
| 94 | + |
| 95 | + if role_arn not in principal or sagemaker_role not in principal: |
| 96 | + principal = PRINCIPAL_TEMPLATE.format( |
| 97 | + partition=aws_partition(region), |
| 98 | + account_id=account_id, |
| 99 | + role_arn=role_arn, |
| 100 | + sagemaker_role=sagemaker_role, |
| 101 | + ) |
| 102 | + |
| 103 | + kms_client.put_key_policy( |
| 104 | + KeyId=key_id, |
| 105 | + PolicyName=POLICY_NAME, |
| 106 | + Policy=KEY_POLICY.format(id=POLICY_NAME, principal=principal), |
| 107 | + ) |
| 108 | + |
| 109 | + |
| 110 | +def get_or_create_kms_key( |
| 111 | + sagemaker_session, role_arn=None, alias=KEY_ALIAS, sagemaker_role="SageMakerRole" |
| 112 | +): |
| 113 | + kms_client = sagemaker_session.boto_session.client("kms") |
| 114 | + kms_key_arn = _get_kms_key_arn(kms_client, alias) |
| 115 | + |
| 116 | + region = sagemaker_session.boto_region_name |
| 117 | + sts_client = sagemaker_session.boto_session.client( |
| 118 | + "sts", region_name=region, endpoint_url=sts_regional_endpoint(region) |
| 119 | + ) |
| 120 | + account_id = sts_client.get_caller_identity()["Account"] |
| 121 | + |
| 122 | + if kms_key_arn is None: |
| 123 | + return _create_kms_key(kms_client, account_id, region, role_arn, sagemaker_role, alias) |
| 124 | + |
| 125 | + if role_arn: |
| 126 | + _add_role_to_policy(kms_client, account_id, role_arn, region, alias, sagemaker_role) |
| 127 | + |
| 128 | + return kms_key_arn |
0 commit comments