【问题标题】:Unable to retrieve secret value from secrets manager to the password section in AWS AD connector boto3 code无法从密钥管理器检索密钥值到 AWS AD 连接器 boto3 代码中的密码部分
【发布时间】:2021-07-09 11:49:24
【问题描述】:

我正在尝试使用 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'
               },
        ]
     )

【问题讨论】:

    标签: boto3 aws-cdk aws-secrets-manager aws-directory-services


    【解决方案1】:

    我认为您提取密码行的“密码”参数不正确。 “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参数值)

    【讨论】:

    • 谢谢@b0tting !!这运作良好。我能够传递值。