Unable to retrieve secret value from secrets manager to the password section in AWS AD connector boto3 code

395 views Asked by At

I am trying to create an AD connector using boto3, inside the password section I need to retrieve value from the already created secrets manager. I am unable to figure out what value can I pass.

   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'
               },
        ]
     )
1

There are 1 answers

1
b0tting On

I think the "Password" parameter where you extract the Password line is incorrect. The "sm" object is a dict with the response result, it has no secret_value_from_json method. To extract a single secret value, you need to put something along the lines of the following after the statement where you retrieve the secret value:

           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"]

(And then of course replace the Password parameter value with Password = sm_password)