问题描述
我正在尝试使用 boto3 创建 AD 连接器,在密码部分中,我需要从已创建的机密管理器中检索值。我无法弄清楚我可以传递什么值。
from aws_cdk import core as CDK
from aws_cdk import core
from aws_cdk import aws_ec2 as ec2
import botocore
import boto3
from aws_cdk import core
class AdConnectorBoto3Stack(cdk.Stack):
def __init__(self,scope: cdk.Construct,construct_id: str,**kwargs) -> None:
super().__init__(scope,construct_id,**kwargs)
# The code that defines your stack goes here
client = boto3.client('ds')
sm_client = boto3.client('sm')
sm = client.get_secret_value(
SecretId='arn value',#VersionId='string',#VersionStage='string'
)
adconnector = client.connect_directory(
Name='corp.example.com',ShortName='AWS',Password=sm.secret_value_from_json("Key").to_string(),#Description='string',Size='Small',ConnectSettings={
'VpcId': 'vpc-0123456789','subnetIds': [
'subnet-123456','subnet-77899'
],'CustomerDnsIps': [
'192.168.0.169','192.168.0.237'
],'CustomerUserName': 'admin'
},Tags=[
{
'Key': 'app','Value': 'adconnector'
},]
)
解决方法
我认为提取密码行的“密码”参数不正确。 “sm”对象是一个带有响应结果的字典,它没有 secret_value_from_json 方法。要提取单个秘密值,您需要在检索秘密值的语句之后添加以下内容:
import json
if 'SecretString' in sm:
secret = json.loads(get_secret_value_response['SecretString'])
else:
secret = json.loads(base64.b64decode(get_secret_value_response['SecretBinary']))
sm_password = secret["Key"]
(然后当然用 Password = sm_password 替换 Password 参数值)