我通过String和正则表达式中的模式匹配来验证KMS ARN值。
示例输入:arn:aws:kms:us-east-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab
规则:
获取AWS区域的代码:
>>> import boto3
>>> session_obj = boto3.session.Session()
>>> session_obj.get_available_regions('lambda')
KMS验证代码:
def validate_aws_kms_arns(kms_arns_str):
"""
Validate KMS ARNS
Format: arn:aws:kms:$RegionCode$:$AccountNumber$:key/$UUID$
$RegionCode$ : list of region Ids
$AccountNumber$ : Interger account number.
$UUID$: uuid
"""
# Get Region Value list from AWS.
lambda_region_list = ['ap-northeast-1', 'ap-northeast-2', 'ap-south-1', 'ap-southeast-1', 'ap-southeast-2', 'ca-central-1', 'eu-central-1', 'eu-west-1', 'eu-west-2', 'eu-west-3', 'sa-east-1', 'us-east-1', 'us-east-2', 'us-west-1', 'us-west-2']
pattern = re.compile("\Aarn:aws:kms:({}):\d+:key/([\w-]+)\Z".format(
'|'.join(lambda_region_list))
)
match_obj = pattern.match(kms_arns_str)
if bool(pattern.match(kms_arns_str)):
uuid_value = match_obj.groups()[1]
else:
print("Invalid KMS ARNS value: {}".format(kms_arns_str))
return False
# UUID Validation.
# ...
return True
我们还需要验证分区和服务的KMS ARNS值吗? 以上代码验证KMS值是否正确?