How to set up ongoing replication from your third-party secrets manager to AWS Secrets Manager

Secrets managers are a great tool to securely store your secrets and provide access to secret material to a set of individuals, applications, or systems that you trust. Across your environments, you might have multiple secrets managers hosted on different providers, which can increase the complexity of maintaining a consistent operating model for your secrets. In these situations, centralizing your secrets in a single source of truth, and replicating subsets of secrets across your other secrets managers, can simplify your operating model.

   <p>This blog post explains how you can use your third-party secrets manager as the source of truth for your secrets, while replicating a subset of these secrets to <a href="https://aws.amazon.com/secrets-manager/" target="_blank" rel="noopener">AWS Secrets Manager</a>. By doing this, you will be able to use secrets that originate and are managed from your third-party secrets manager in Amazon Web Services (AWS) applications or in <a href="https://docs.aws.amazon.com/secretsmanager/latest/userguide/integrating.html" target="_blank" rel="noopener">AWS services that use Secrets Manager secrets</a>.</p> 
   <p>I’ll demonstrate this approach in this post by setting up a sample open-source <a href="https://www.vaultproject.io/" target="_blank" rel="noopener">HashiCorp Vault</a> to create and maintain secrets and create a replication mechanism that enables you to use these secrets in AWS by using AWS Secrets Manager. Although this post uses HashiCorp Vault as an example, you can also modify the replication mechanism to use secrets managers from other providers.</p> 
    <p><strong>Important:</strong> This blog post is intended to provide guidance that you can use when planning and implementing a secrets replication mechanism. The examples in this post are <strong>not</strong> intended to be run directly in production, and you will need to take security hardening requirements into consideration before deploying this solution. As an example, HashiCorp provides tutorials on <a href="https://learn.hashicorp.com/tutorials/vault/production-hardening" target="_blank" rel="noopener">hardening production vaults</a>.</p> 
   <p>You can use these links to navigate through this post:</p> 

   <p><a href="https://aws.amazon.com/blogs/security/how-to-set-up-ongoing-replication-from-your-third-party-secrets-manager-to-aws-secrets-manager/#why_when_consider_replicating_secrets">Why and when to consider replicating secrets</a><br><a href="https://aws.amazon.com/blogs/security/how-to-set-up-ongoing-replication-from-your-third-party-secrets-manager-to-aws-secrets-manager/#two_approaches_secrets_replication">Two approaches to secrets replication</a><br><a href="https://aws.amazon.com/blogs/security/how-to-set-up-ongoing-replication-from-your-third-party-secrets-manager-to-aws-secrets-manager/#replicate_secrets">Replicate secrets to AWS Secrets Manager with the pull model</a><br><span><a href="https://aws.amazon.com/blogs/security/how-to-set-up-ongoing-replication-from-your-third-party-secrets-manager-to-aws-secrets-manager/#solution_overview">Solution overview</a></span><br><span><a href="https://aws.amazon.com/blogs/security/how-to-set-up-ongoing-replication-from-your-third-party-secrets-manager-to-aws-secrets-manager/#set_up_solution">Set up the solution</a></span><br><span><a href="https://aws.amazon.com/blogs/security/how-to-set-up-ongoing-replication-from-your-third-party-secrets-manager-to-aws-secrets-manager/#step_1">Step 1: Deploy the solution by using the AWS CDK toolkit</a></span><br><span><a href="https://aws.amazon.com/blogs/security/how-to-set-up-ongoing-replication-from-your-third-party-secrets-manager-to-aws-secrets-manager/#step_2">Step 2: Initialize the HashiCorp Vault</a></span><br><span><a href="https://aws.amazon.com/blogs/security/how-to-set-up-ongoing-replication-from-your-third-party-secrets-manager-to-aws-secrets-manager/#step_3">Step 3: Update the Vault connection secret</a></span><br><span><a href="https://aws.amazon.com/blogs/security/how-to-set-up-ongoing-replication-from-your-third-party-secrets-manager-to-aws-secrets-manager/#step_4">Step 4: (Optional) Set up email notifications for replication failures</a></span><br><span><a href="https://aws.amazon.com/blogs/security/how-to-set-up-ongoing-replication-from-your-third-party-secrets-manager-to-aws-secrets-manager/#test_secret_replication">Test your secret replication</a></span><br><span><a href="https://aws.amazon.com/blogs/security/how-to-set-up-ongoing-replication-from-your-third-party-secrets-manager-to-aws-secrets-manager/#update_a_secret">Update a secret</a></span><br><span><a href="https://aws.amazon.com/blogs/security/how-to-set-up-ongoing-replication-from-your-third-party-secrets-manager-to-aws-secrets-manager/#secret_replication_logic">Secret replication logic</a></span><br><span><a href="https://aws.amazon.com/blogs/security/how-to-set-up-ongoing-replication-from-your-third-party-secrets-manager-to-aws-secrets-manager/#use_your_secret">Use your secret</a></span><br><span><a href="https://aws.amazon.com/blogs/security/how-to-set-up-ongoing-replication-from-your-third-party-secrets-manager-to-aws-secrets-manager/#manage_permissions">Manage permissions</a></span><br><span><a href="https://aws.amazon.com/blogs/security/how-to-set-up-ongoing-replication-from-your-third-party-secrets-manager-to-aws-secrets-manager/#customizing_sample_solution">Options for customizing the sample solution</a></span></p> 
   <h2 id="why_when_consider_replicating_secrets">Why and when to consider replicating secrets</h2> 
   <p>The primary use case for this post is for customers who are running applications on AWS and are currently using a third-party secrets manager to manage their secrets, hosted on-premises, in the AWS Cloud, or with a third-party provider. These customers typically have existing secrets vending processes, deployment pipelines, and procedures and processes around the management of these secrets. Customers with such a setup might want to keep their existing third-party secrets manager and have a set of secrets that are accessible to workloads running outside of AWS, as well as workloads running within AWS, by using AWS Secrets Manager.</p> 
   <p>Another use case is for customers who are in the process of migrating workloads to the AWS Cloud and want to maintain a (temporary) hybrid form of secrets management. By replicating secrets from an existing third-party secrets manager, customers can migrate their secrets to the AWS Cloud one-by-one, test that they work, integrate the secrets with the intended applications and systems, and once the migration is complete, remove the third-party secrets manager.</p> 
   <p>Additionally, some AWS services, such as <a href="https://aws.amazon.com/getting-started/hands-on/set-up-shared-database-connection-amazon-rds-proxy/" target="_blank" rel="noopener">Amazon Relational Database Service (Amazon RDS) Proxy</a>, <a href="https://docs.aws.amazon.com/directconnect/latest/UserGuide/direct-connect-mac-sec-getting-started.html" target="_blank" rel="noopener">AWS Direct Connect MACsec</a>, and <a href="https://docs.aws.amazon.com/directoryservice/latest/admin-guide/ad_connector_seamlessly_join_linux_instance.html" target="_blank" rel="noopener">AD Connector seamless join (Linux)</a>, only support secrets from AWS Secrets Manager. Customers can use secret replication if they have a third-party secrets manager and want to be able to use third-party secrets in services that require integration with AWS Secrets Manager. That way, customers don’t have to manage secrets in two places.</p> 
   <h2 id="two_approaches_secrets_replication">Two approaches to secrets replication</h2> 
   <p>In this post, I’ll discuss two main models to replicate secrets from an external third-party secrets manager to AWS Secrets Manager: a pull model and a push model.</p> 
   <p><strong>Pull model</strong><br>In a pull model, you can use AWS services such as <a href="https://aws.amazon.com/eventbridge/" target="_blank" rel="noopener">Amazon EventBridge</a> and <a href="https://aws.amazon.com/lambda/" target="_blank" rel="noopener">AWS Lambda</a> to periodically call your external secrets manager to fetch secrets and updates to those secrets. The main benefit of this model is that it doesn’t require any major configuration to your third-party secrets manager. The AWS resources and mechanism used for pulling secrets must have appropriate permissions and network access to those secrets. However, there could be a delay between the time a secret is created and updated and when it’s picked up for replication, depending on the time interval configured between pulls from AWS to the external secrets manager.</p> 
   <p><strong>Push model</strong><br>In this model, rather than periodically polling for updates, the external secrets manager pushes updates to AWS Secrets Manager as soon as a secret is added or changed. The main benefit of this is that there is minimal delay between secret creation, or secret updating, and when that data is available in AWS Secrets Manager. The push model also minimizes the network traffic required for replication since it’s a unidirectional flow. However, this model adds a layer of complexity to the replication, because it requires additional configuration in the third-party secrets manager. More specifically, the push model is dependent on the third-party secrets manager’s ability to run event-based push integrations with AWS resources. This will require a custom integration to be developed and managed on the third-party secrets manager’s side.</p> 
   <p>This blog post focuses on the pull model to provide an example integration that requires no additional configuration on the third-party secrets manager. </p> 
   <h2 id="replicate_secrets">Replicate secrets to AWS Secrets Manager with the pull model</h2> 
   <p>In this section, I’ll walk through an example of how to use the pull model to replicate your secrets from an external secrets manager to AWS Secrets Manager. </p> 
   <h3 id="solution_overview">Solution overview</h3> 
   <div id="attachment_28353" class="wp-caption alignleft"> 
    <img aria-describedby="caption-attachment-28353" src="https://www.infracom.com.sg/wp-content/uploads/2023/02/img1-4-1024x765-1.png" alt="Figure 1: Secret replication architecture diagram" width="760" class="size-large wp-image-28353"> 
    <p id="caption-attachment-28353" class="wp-caption-text">Figure 1: Secret replication architecture diagram</p> 
   <p>The architecture shown in Figure 1 consists of the following main steps, numbered in the diagram:</p> 
    <li>A <a href="https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/ScheduledEvents.html#CronExpressions" target="_blank" rel="noopener">Cron expression</a> in Amazon EventBridge invokes an AWS Lambda function every 30 minutes.</li> 
    <li>To connect to the third-party secrets manager, the Lambda function, written in NodeJS, fetches a set of user-defined API keys belonging to the secrets manager from AWS Secrets Manager. These API keys have been scoped down to give read-only access to secrets that should be replicated, to adhere to the principle of least privilege. There is more information on this in <strong><a href="https://aws.amazon.com/blogs/security/how-to-set-up-ongoing-replication-from-your-third-party-secrets-manager-to-aws-secrets-manager/#step_3">Step 3: Update the Vault connection secret</a></strong>.</li> 
    <li>The third step has two variants depending on where your third-party secrets manager is hosted: 
      <li>The Lambda function is configured to fetch secrets from a third-party secrets manager that is hosted outside AWS. This requires sufficient networking and routing to allow communication from the Lambda function.<br><blockquote> 
        <p><strong>Note: </strong>Depending on the location of your third-party secrets manager, you might have to consider different networking topologies. For example, you might need to set up hybrid connectivity between your external environment and the AWS Cloud by using <a href="https://docs.aws.amazon.com/vpn/latest/s2svpn/VPC_VPN.html" target="_blank" rel="noopener">AWS Site-to-Site VPN</a> or <a href="https://docs.aws.amazon.com/directconnect/latest/UserGuide/Welcome.html" target="_blank" rel="noopener">AWS Direct Connect</a>, or both.</p> 
       </blockquote> </li> 
      <li>The Lambda function is configured to fetch secrets from a third-party secrets manager running on <a href="https://aws.amazon.com/ec2/" target="_blank" rel="noopener">Amazon Elastic Compute Cloud (Amazon EC2)</a>.</li> 
      <p><strong>Important</strong>: To simplify the deployment of this example integration, I’ll use a secrets manager hosted on a publicly available Amazon EC2 instance within the same VPC as the Lambda function (3b). This minimizes the additional networking components required to interact with the secrets manager. More specifically, the EC2 instance runs an open-source HashiCorp Vault. In the rest of this post, I’ll refer to the HashiCorp Vault’s API keys as <em>Vault tokens</em>.</p> 
     </blockquote> </li> 
    <li>The Lambda function compares the version of the secret that it just fetched from the third-party secrets manager against the version of the secret that it has in AWS Secrets Manager (by tag). The function will create a new secret in AWS Secrets Manager if the secret does not exist yet, and will update it if there is a new version. The Lambda function will only consider secrets from the third-party secrets manager for replication if they match a specified prefix. For example, <span>hybrid-aws-secrets/</span>.</li> 
    <li>In case there is an error synchronizing the secret, an email notification is sent to the email addresses which are subscribed to the <a href="https://aws.amazon.com/sns/" target="_blank" rel="noopener">Amazon Simple Notification Service (Amazon SNS)</a> Topic deployed. This sample application uses email notifications with Amazon SNS as an example, but you could also integrate with services like ServiceNow, Jira, Slack, or PagerDuty. <a href="https://aws.amazon.com/premiumsupport/knowledge-center/sns-lambda-webhooks-chime-slack-teams/" target="_blank" rel="noopener">Learn more</a> about how to use webhooks to publish Amazon SNS messages to external services.</li> 
   <h3 id="set_up_solution">Set up the solution</h3> 
   <p>In this section, I walk through deploying the pull model solution displayed in Figure 1 using the following steps:<br><a href="https://aws.amazon.com/blogs/security/how-to-set-up-ongoing-replication-from-your-third-party-secrets-manager-to-aws-secrets-manager/#step_1">Step 1: Deploy the solution by using the AWS CDK toolkit</a><br><a href="https://aws.amazon.com/blogs/security/how-to-set-up-ongoing-replication-from-your-third-party-secrets-manager-to-aws-secrets-manager/#step_2">Step 2: Initialize the HashiCorp Vault</a><br><a href="https://aws.amazon.com/blogs/security/how-to-set-up-ongoing-replication-from-your-third-party-secrets-manager-to-aws-secrets-manager/#step_3">Step 3: Update the Vault connection secret</a><br><a href="https://aws.amazon.com/blogs/security/how-to-set-up-ongoing-replication-from-your-third-party-secrets-manager-to-aws-secrets-manager/#step_4">Step 4: (Optional) Set up email notifications for replication failures</a></p> 
   <h4 id="step_1">Step 1: Deploy the solution by using the AWS CDK toolkit</h4> 
   <p>For this blog post, I’ve created an <a href="https://aws.amazon.com/cdk/" target="_blank" rel="noopener">AWS Cloud Development Kit (AWS CDK)</a> script, which can be found in this <a href="https://github.com/aws-samples/aws-secrets-manager-hybrid-secret-replication-from-hashicorp-vault" target="_blank" rel="noopener">AWS GitHub repository</a>. Using the AWS CDK, I’ve defined the infrastructure depicted in Figure 1 as Infrastructure as Code (IaC), written in TypeScript, ready for you to deploy and try out. The AWS CDK is an open-source software development framework that allows you to write your cloud application infrastructure as code using common programming languages such as TypeScript, Python, Java, Go, and so on.</p> 
   <p>To deploy the solution, the following should be in place on your system:</p> 
    <li><a href="https://git-scm.com/downloads" target="_blank" rel="noopener">Git</a></li> 
    <li><a href="https://nodejs.org/en/download/" target="_blank" rel="noopener">Node</a> (version 16 or higher)</li> 
    <li><a href="https://stedolan.github.io/jq/download/" target="_blank" rel="noopener">jq</a></li> 
    <li><a href="https://www.npmjs.com/package/aws-cdk" target="_blank" rel="noopener">AWS CDK Toolkit</a>. Install using npm (included in Node setup) by running <span>npm install -g aws-cdk</span> in a local terminal.</li> 
    <li>An AWS access key ID and secret access key configured as this setup will interact with your AWS account. See <a href="https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-quickstart.html" target="_blank" rel="noopener">Configuration basics</a> in the AWS Command Line Interface User Guide for more details.</li> 
    <li><a href="https://www.docker.com/products/docker-desktop/" rel="noopener" target="_blank">Docker</a> installed and running on your machine</li> 
   <p><strong>To deploy the solution</strong></p> 
    <li>Clone the CDK script for secret replication.<br><span>git clone https://github.com/aws-samples/aws-secrets-manager-hybrid-secret-replication-from-hashicorp-vault.git SecretReplication</span></li> 
    <li>Use the cloned project as the working directory.<br><span>cd SecretReplication</span></li> 
    <li>Install the required dependencies to deploy the application.<br><span>npm install</span></li> 
    <li>Adjust any configuration values for your setup in the <span>cdk.json</span> file. For example, you can adjust the <span>secretsPrefix</span> value to change which prefix is used by the Lambda function to determine the subset of secrets that should be replicated from the third-party secrets manager.</li> 
    <li>Bootstrap your AWS environments with some resources that are required to deploy the solution. With correctly configured AWS credentials, run the following command.<br><span>cdk bootstrap</span> <p>The core resources created by bootstrapping are an <a href="https://aws.amazon.com/ecr/" target="_blank" rel="noopener">Amazon Elastic Container Registry (Amazon ECR)</a> repository for the AWS Lambda Docker image, an <a href="https://aws.amazon.com/s3/" target="_blank" rel="noopener">Amazon Simple Storage Service (Amazon S3)</a> bucket for static assets, and <a href="https://aws.amazon.com/iam/" target="_blank" rel="noopener">AWS Identity and Access Management (IAM)</a> roles with corresponding IAM policies. You can find a full list of the resources by going to the CDKToolkit stack in <a href="https://aws.amazon.com/cloudformation/" target="_blank" rel="noopener">AWS CloudFormation</a> after the command has finished.</p> </li> 
    <li>Deploy the infrastructure.<br><span>cdk deploy </span> <p>This command deploys the infrastructure shown in Figure 1 for you by using AWS CloudFormation. For a full list of resources, you can view the SecretsManagerReplicationStack in AWS CloudFormation after the deployment has completed.</p> </li> 
    <p><strong>Note:</strong> If your local environment does not have a terminal that allows you to run these commands, consider using <a href="https://aws.amazon.com/cloud9/" target="_blank" rel="noopener">AWS Cloud9</a> or <a href="https://aws.amazon.com/cloudshell/" target="_blank" rel="noopener">AWS CloudShell</a>.</p> 
   <p>After the deployment has finished, you should see an output in your terminal that looks like the one shown in Figure 2. If successful, the output provides the IP address of the sample HashiCorp Vault and its web interface.</p> 
   <div id="attachment_28354" class="wp-caption alignleft"> 
    <img aria-describedby="caption-attachment-28354" src="https://www.infracom.com.sg/wp-content/uploads/2023/02/img2-5.png" alt="Figure 2: AWS CDK deployment output" width="760" class="size-full wp-image-28354"> 
    <p id="caption-attachment-28354" class="wp-caption-text">Figure 2: AWS CDK deployment output</p> 
   <h4 id="step_2">Step 2: Initialize the HashiCorp Vault</h4> 
   <p>As part of the output of the deployment script, you will be given a URL to access the user interface of the open-source HashiCorp Vault. To simplify accessibility, the URL points to a publicly available Amazon EC2 instance running the HashiCorp Vault user interface as shown in step 3b in Figure 1.</p> 
   <p>Let’s look at the HashiCorp Vault that was just created. Go to the URL in your browser, and you should see the <strong>Raft Storage</strong> initialize page, as shown in Figure 3.</p> 
   <div id="attachment_28355" class="wp-caption alignleft"> 
    <img aria-describedby="caption-attachment-28355" src="https://www.infracom.com.sg/wp-content/uploads/2023/02/img3-3-1024x712-1.png" alt="Figure 3: HashiCorp Vault Raft Storage initialize page" width="760" class="size-large wp-image-28355"> 
    <p id="caption-attachment-28355" class="wp-caption-text">Figure 3: HashiCorp Vault Raft Storage initialize page</p> 
   <p>The vault requires an initial configuration to set up storage and get the initial set of root keys. You can go through the steps manually in the HashiCorp Vault’s user interface, but I recommend that you use the <span>initialise_vault.sh</span> script that is included as part of the SecretsManagerReplication project instead. </p> 
   <p>Using the HashiCorp Vault API, the initialization script will automatically do the following:</p> 
    <li>Initialize the <a href="https://www.vaultproject.io/docs/configuration/storage/raft" target="_blank" rel="noopener">Raft storage</a> to allow the Vault to store secrets locally on the instance.</li> 
    <li>Create an initial set of unseal keys for the Vault. Importantly, for demo purposes, the script uses a single key share. For production environments, it’s recommended to use multiple key shares so that multiple shares are needed to reconstruct the root key, in case of an emergency.</li> 
    <li>Store the unseal keys in <span>init/vault_init_output.json</span> in your project.</li> 
    <li>Unseals the HashiCorp Vault by using the unseal keys generated earlier.</li> 
    <li>Enables two key-value secrets engines: 
      <li>An engine named after the prefix that you’re using for replication, defined in the <span>cdk.json</span> file. In this example, this is <span>hybrid-aws-secrets</span>. We’re going to use the secrets in this engine for replication to AWS Secrets Manager.</li> 
      <li>An engine called <span>super-secret-engine</span>, which you’re going to use to show that your replication mechanism does not have access to secrets outside the engine used for replication.</li> 
     </ol> </li> 
    <li>Creates three example secrets, two in <span>hybrid-aws-secrets</span>, and one in <span>super-secret-engine</span>.</li> 
    <li>Creates a read-only policy, which you can see in the <span>init/replication-policy-payload.json</span> file after the script has finished running, that allows read-only access to only the secrets that should be replicated.</li> 
    <li>Creates a new vault token that has the read-only policy attached so that it can be used by the AWS Lambda function later on to fetch secrets for replication.</li> 
   <p>To run the initialization script, go back to your terminal, and run the following command.<br><span>./initialise_vault.sh</span></p> 
   <p>The script will then ask you for the IP address of your HashiCorp Vault. Provide the IP address (excluding the port) and choose <strong>Enter</strong>. Input y so that the script creates a couple of sample secrets.</p> 
   <p>If everything is successful, you should see an output that includes tokens to access your HashiCorp Vault, similar to that shown in Figure 4.</p> 
   <div id="attachment_28356" class="wp-caption alignleft"> 
    <img aria-describedby="caption-attachment-28356" src="https://www.infracom.com.sg/wp-content/uploads/2023/02/img4-4-1024x222-1.png" alt="Figure 4: Initialize HashiCorp Vault bash script output" width="760" class="size-large wp-image-28356"> 
    <p id="caption-attachment-28356" class="wp-caption-text">Figure 4: Initialize HashiCorp Vault bash script output</p> 
   <p>The setup script has outputted two tokens: one root token that you will use for administrator tasks, and a read-only token that will be used to read secret information for replication. Make sure that you can access these tokens while you’re following the rest of the steps in this post.</p> 
    <p><strong>Note:</strong> The root token is only used for demonstration purposes in this post. In your production environments, you should not use root tokens for regular administrator actions. Instead, you should use scoped down roles depending on your organizational needs. In this case, the root token is used to highlight that there are secrets under <span>super-secret-engine/</span> which are not meant for replication. These secrets cannot be seen, or accessed, by the read-only token.</p> 
   <p>Go back to your browser and refresh your HashiCorp Vault UI. You should now see the <strong>Sign in to Vault</strong> page. Sign in using the Token method, and use the root token. If you don’t have the root token in your terminal anymore, you can find it in the <span>init/vault_init_output.json</span> file.</p> 
   <p>After you sign in, you should see the overview page with three secrets engines enabled for you, as shown in Figure 5.</p> 
   <div id="attachment_28357" class="wp-caption alignleft"> 
    <img aria-describedby="caption-attachment-28357" src="https://www.infracom.com.sg/wp-content/uploads/2023/02/img5-2-1024x710-1.png" alt="Figure 5: HashiCorp Vault secrets engines overview" width="760" class="size-large wp-image-28357"> 
    <p id="caption-attachment-28357" class="wp-caption-text">Figure 5: HashiCorp Vault secrets engines overview</p> 
   <p>If you explore <span>hybrid-aws-secrets</span> and <span>super-secret-engine</span>, you can see the secrets that were automatically created by the initialization script. For example, <span>first-secret-for-replication</span>, which contains a sample key-value secret with the key <span>secrets</span> and value <span>manager</span>.</p> 
   <p>If you navigate to <strong>Policies</strong> in the top navigation bar, you can also see the <span>aws-replication-read-only</span> policy, as shown in Figure 6. This policy provides read-only access to only the <span>hybrid-aws-secrets</span> path.</p> 
   <div id="attachment_28358" class="wp-caption alignleft"> 
    <img aria-describedby="caption-attachment-28358" src="https://www.infracom.com.sg/wp-content/uploads/2023/02/img6-2-1024x711-1.png" alt="Figure 6: Read-only HashiCorp Vault token policy" width="760" class="size-large wp-image-28358"> 
    <p id="caption-attachment-28358" class="wp-caption-text">Figure 6: Read-only HashiCorp Vault token policy</p> 
   <p>The read-only policy is attached to the read-only token that we’re going to use in the secret replication Lambda function. This policy is important because it scopes down the access that the Lambda function obtains by using the token to a specific prefix meant for replication. For secret replication we only need to perform read operations. This policy ensures that we can read, but cannot add, alter, or delete any secrets in HashiCorp Vault using the token.</p> 
   <p>You can verify the read-only token permissions by signing into the HashiCorp Vault user interface using the read-only token rather than the root token. Now, you should only see <span>hybrid-aws-secrets</span>. You no longer have access to <span>super-secret-engine</span>, which you saw in Figure 5. If you try to create or update a secret, you will get a permission denied error.</p> 
   <p>Great! Your HashiCorp Vault is now ready to have its secrets replicated from <span>hybrid-aws-secrets</span> to AWS Secrets Manager. The next section describes a final configuration that you need to do to allow access to the secrets in HashiCorp Vault by the replication mechanism in AWS.</p> 
   <h4 id="step_3">Step 3: Update the Vault connection secret</h4> 
   <p>To allow secret replication, you must give the AWS Lambda function access to the HashiCorp Vault read-only token that was created by the initialization script. To do that, you need to update the <span>vault-connection-secret</span> that was initialized in AWS Secrets Manager as part of your AWS CDK deployment.</p> 
   <p>For demonstration purposes, I’ll show you how to do that by using the AWS Management Console, but you can also do it programmatically by using the <a href="https://aws.amazon.com/cli/" target="_blank" rel="noopener">AWS Command Line Interface (AWS CLI)</a> or AWS SDK with the <a href="https://awscli.amazonaws.com/v2/documentation/api/latest/reference/secretsmanager/update-secret.html" target="_blank" rel="noopener">update-secret</a> command.</p> 
   <p><strong>To update the Vault connection secret (console)</strong></p> 
    <li>In the AWS Management Console, go to <strong>AWS Secrets Manager</strong> &gt; <strong>Secrets</strong> &gt; <strong>hybrid-aws-secrets/vault-connection-secret</strong>.</li> 
    <li>Under <strong>Secret Value</strong>, choose <strong>Retrieve Secret Value</strong>, and then choose <strong>Edit</strong>.</li> 
    <li>Update the <span>vaultToken</span> value to contain the read-only token that was generated by the initialization script.</li> 
   <div id="attachment_28359" class="wp-caption alignleft"> 
    <img aria-describedby="caption-attachment-28359" src="https://www.infracom.com.sg/wp-content/uploads/2023/02/img7-2-1024x902-1.png" alt="Figure 7: AWS Secrets Manager - Vault connection secret page" width="760" class="size-large wp-image-28359"> 
    <p id="caption-attachment-28359" class="wp-caption-text">Figure 7: AWS Secrets Manager – Vault connection secret page</p> 
   <h4 id="step_4">Step 4: (Optional) Set up email notifications for replication failures</h4> 
   <p>As highlighted in Figure 1, the Lambda function will send an email by using Amazon SNS to a designated email address whenever one or more secrets fails to be replicated. You will need to configure the solution to use the correct email address. To do this, go to the <span>cdk.json</span> file at the root of the <span>SecretReplication</span> folder and adjust the <span>notificationEmail</span> parameter to an email address that you own. Once done, deploy the changes using the <span>cdk deploy</span> command. Within a few minutes, you’ll get an email requesting you to confirm the subscription. Going forward, you will receive an email notification if one or more secrets fails to replicate.</p> 
   <h3 id="test_secret_replication">Test your secret replication</h3> 
   <p>You can either wait up to 30 minutes for the Lambda function to be invoked automatically to replicate the secrets, or you can manually invoke the function.</p> 
   <p><strong>To test your secret replication</strong></p> 
    <li><a href="https://console.aws.amazon.com/lambda/" target="_blank" rel="noopener">Open the AWS Lambda console</a> and find the Secret Replication function (the name starts with <span>SecretsManagerReplication-SecretReplication</span>).</li> 
    <li>Navigate to the <strong>Test</strong> tab.</li> 
    <li>For the text event action, select <strong>Create new event</strong>, create an event using the default parameters, and then choose the <strong>Test</strong> button on the right-hand side, as shown in Figure 8.</li> 
   <div id="attachment_28360" class="wp-caption alignleft"> 
    <img aria-describedby="caption-attachment-28360" src="https://www.infracom.com.sg/wp-content/uploads/2023/02/img8-1-1024x249-1.png" alt="Figure 8: AWS Lambda - Test page to manually invoke the function" width="760" class="size-large wp-image-28360"> 
    <p id="caption-attachment-28360" class="wp-caption-text">Figure 8: AWS Lambda – Test page to manually invoke the function</p> 
   <p>This will run the function. You should see a success message, as shown in Figure 9. If this is the first time the Lambda function has been invoked, you will see in the results that two secrets have been created.</p> 
   <div id="attachment_28361" class="wp-caption alignleft"> 
    <img aria-describedby="caption-attachment-28361" src="https://www.infracom.com.sg/wp-content/uploads/2023/02/img9-1-1024x190-1.png" alt="Figure 9: AWS Lambda function output" width="760" class="size-large wp-image-28361"> 
    <p id="caption-attachment-28361" class="wp-caption-text">Figure 9: AWS Lambda function output</p> 
   <p>You can find the corresponding logs for the Lambda function invocation in a Log group in <a href="https://aws.amazon.com/cloudwatch/" target="_blank" rel="noopener">AWS CloudWatch</a> matching the name /aws/lambda/SecretsManagerReplication-SecretReplicationLambdaF-XXXX.</p> 
   <p>To verify that the secrets were added, navigate to AWS Secrets Manager in the console, and in addition to the <span>vault-connection-secret</span> that you edited before, you should now also see the two new secrets with the same <span>hybrid-aws-secrets</span> prefix, as shown in Figure 10.</p> 
   <div id="attachment_28362" class="wp-caption alignleft"> 
    <img aria-describedby="caption-attachment-28362" src="https://www.infracom.com.sg/wp-content/uploads/2023/02/img10-1.png" alt="Figure 10: AWS Secrets Manager overview - New replicated secrets" width="760" class="size-full wp-image-28362"> 
    <p id="caption-attachment-28362" class="wp-caption-text">Figure 10: AWS Secrets Manager overview – New replicated secrets</p> 
   <p>For example, if you look at <span>first-secret-for-replication</span>, you can see the first version of the secret, with the secret key <span>secrets</span> and secret value <span>manager</span>, as shown in Figure 11.</p> 
   <div id="attachment_28363" class="wp-caption alignleft"> 
    <img aria-describedby="caption-attachment-28363" src="https://www.infracom.com.sg/wp-content/uploads/2023/02/img11-1-1024x584-1.png" alt="Figure 11: AWS Secrets Manager – New secret overview showing values and version number" width="760" class="size-large wp-image-28363"> 
    <p id="caption-attachment-28363" class="wp-caption-text">Figure 11: AWS Secrets Manager – New secret overview showing values and version number</p> 
   <p>Success! You now have access to the secret values that originate from HashiCorp Vault in AWS Secrets Manager. Also, notice how there is a <span>version</span> tag attached to the secret. This is something that is necessary to update the secret, which you will learn more about in the next two sections.</p> 
   <h3 id="update_a_secret">Update a secret</h3> 
   <p>It’s a recommended security practice to rotate secrets frequently. The Lambda function in this solution not only replicates secrets when they are created — it also periodically checks if existing secrets in AWS Secrets Manager should be updated when the third-party secrets manager (HashiCorp Vault in this case) has a new version of the secret. To validate that this works, you can manually update a secret in your HashiCorp Vault and observe its replication in AWS Secrets Manager in the same way as described in the previous section. You will notice that the <span>version</span> tag of your secret gets updated automatically when there is a new secret replication from the third-party secrets manager to AWS Secrets Manager.</p> 
   <h3 id="secret_replication_logic">Secret replication logic</h3> 
   <p>This section will explain in more detail the logic behind the secret replication. Consider the following sequence diagram, which explains the overall logic implemented in the Lambda function.</p> 
   <div id="attachment_28367" class="wp-caption alignleft"> 
    <img aria-describedby="caption-attachment-28367" src="https://www.infracom.com.sg/wp-content/uploads/2023/02/img12-2.png" alt="Figure 12: State diagram for secret replication logic" width="760" class="size-full wp-image-28367"> 
    <p id="caption-attachment-28367" class="wp-caption-text">Figure 12: State diagram for secret replication logic</p> 
   <p>This diagram highlights that the Lambda function will first fetch a list of secret names from the HashiCorp Vault. Then, the function will get a list of secrets from AWS Secrets Manager, matching the prefix that was configured for replication. AWS Secrets Manager will return a list of the secrets that match this prefix and will also return their metadata and tags. Note that the function has not fetched any secret material yet.</p> 
   <p>Next, the function will loop through each secret name that HashiCorp Vault gave and will check if the secret exists in AWS Secrets Manager:</p> 
    <li>If there is no secret that matches that name, the function will fetch the secret material from HashiCorp Vault, including the version number, and create a new secret in AWS Secrets Manager. It will also add a <span>version</span> tag to the secret to match the version.</li> 
    <li>If there is a secret matching that name in AWS Secrets Manager already, the Lambda function will first fetch the metadata from that secret in HashiCorp Vault. This is required to get the version number of the secret, because the version number was not exposed when the function got the list of secrets from HashiCorp Vault initially. If the secret version from HashiCorp Vault does not match the version value of the secret in AWS Secrets Manager (for example, the version in HashiCorp vault is <span>2</span>, and the version in AWS Secrets manager is <span>1</span>), an update is required to get the values synchronized again. Only now will the Lambda function fetch the actual secret material from HashiCorp Vault and update the secret in AWS Secrets Manager, including the version number in the tag.</li> 
   <p>The Lambda function fetches metadata about the secrets, rather than just fetching the secret material from HashiCorp Vault straight away. Typically, secrets don’t update very often. If this Lambda function is called every 30 minutes, then it should not have to add or update any secrets in the majority of invocations. By using metadata to determine whether you need the secret material to create or update secrets, you minimize the number of times secret material is fetched both from HashiCorp Vault and AWS Secrets Manager.</p> 
    <p><strong>Note:</strong> The AWS Lambda function has permissions to pull certain secrets from HashiCorp Vault. It is important to thoroughly review the Lambda code and any subsequent changes to it to prevent leakage of secrets. For example, you should ensure that the Lambda function does not get updated with code that unintentionally logs secret material outside the Lambda function.</p> 
   <h3 id="use_your_secret">Use your secret</h3> 
   <p>Now that you have created and replicated your secrets, you can use them in your AWS applications or AWS services that are integrated with Secrets Manager. For example, you can use the secrets when you set up connectivity for a proxy in Amazon RDS, as follows.</p> 
   <p><strong>To use a secret when creating a proxy in Amazon RDS</strong></p> 
    <li>Go to the Amazon RDS service in the console.</li> 
    <li>In the left navigation pane, choose <strong>Proxies</strong>, and then choose <strong>Create Proxy</strong>.</li> 
    <li>On the <strong>Connectivity</strong> tab, you can now select <span>first-secret-for-replication</span> <strong>or</strong> <span>second-secret-for-replication</span>, which were created by the Lambda function after replicating them from the HashiCorp Vault.</li> 
   <div id="attachment_28368" class="wp-caption alignleft"> 
    <img aria-describedby="caption-attachment-28368" src="https://www.infracom.com.sg/wp-content/uploads/2023/02/img13-1-1024x485-1.png" alt="Figure 13: Amazon RDS Proxy - Example of using replicated AWS Secrets Manager secrets" width="760" class="size-large wp-image-28368"> 
    <p id="caption-attachment-28368" class="wp-caption-text">Figure 13: Amazon RDS Proxy – Example of using replicated AWS Secrets Manager secrets</p> 
   <p>It is important to remember that the consumers of the replicated secrets in AWS Secrets Manager will require scoped-down IAM permissions to use the secrets and <a href="https://aws.amazon.com/kms/" target="_blank" rel="noopener">AWS Key Management Service (AWS KMS)</a> keys that were used to encrypt the secrets. For example, see <strong>Step 3: Create IAM role and policy</strong> on the <a href="https://aws.amazon.com/getting-started/hands-on/set-up-shared-database-connection-amazon-rds-proxy/" target="_blank" rel="noopener">Set up shared database connections with Amazon RDS Proxy</a> page.</p> 
   <h3 id="manage_permissions">Manage permissions</h3> 
   <p>Due to the sensitive nature of the secrets, it is important that you scope down the permissions to the least amount required to prevent inadvertent access to your secrets. The setup adopts a least-privilege permission strategy, where only the necessary actions are explicitly allowed on the resources that are required for replication. However, the permissions should be reviewed in accordance to your security standards.</p> 
   <p>In the architecture of this solution, there are two main places where you control access to the management of your secrets in Secrets Manager.</p> 
   <p><strong>Lambda execution IAM role</strong>: The IAM role assumed by the Lambda function during execution contains the appropriate permissions for secret replication. There is an additional safety measure, which explicitly denies any action to a resource that is <em>not</em> required for the replication. For example, the Lambda function only has permission to publish to the Amazon SNS topic that is created for the failed replications, and will explicitly deny a publish action to any other topic. Even if someone accidentally adds an allow to the policy for a different topic, the explicit deny will still block this action.</p> 
   <p><strong>AWS KMS key policy: </strong>When other services need to access the replicated secret in AWS Secrets Manager, they need permission to use the <span>hybrid-aws-secrets-encryption-key</span> AWS KMS key. You need to allow the principal these permissions through the AWS KMS key policy. Additionally, you can manage permissions to the AWS KMS key for the principal through an identity policy. For example, this is required when accessing AWS KMS keys across AWS accounts. See <a href="https://docs.aws.amazon.com/kms/latest/developerguide/key-policy-services.html" target="_blank" rel="noopener">Permissions for AWS services in key policies</a> and <a href="https://docs.aws.amazon.com/kms/latest/developerguide/cmks-in-iam-policies.html" target="_blank" rel="noopener">Specifying KMS keys in IAM policy statements</a> in the AWS KMS Developer Guide.</p> 
   <h3 id="customizing_sample_solution">Options for customizing the sample solution</h3> 
   <p>The solution that was covered in this post provides an example for replication of secrets from HashiCorp Vault to AWS Secrets Manager using the pull model. This section contains additional customization options that you can consider when setting up the solution, or your own variation of it.</p> 
    <li>Depending on the solution that you’re using, you might have access to different metadata attached to the secrets, which you can use to determine if a secret should be updated. For example, if you have access to data that represents a <span>last_updated_datetime</span> property, you could use this to infer whether or not a secret ought to be updated.</li> 
    <li>It is a recommended practice to not use long-lived tokens wherever possible. In this sample, I used a static vault token to give the Lambda function access to the HashiCorp Vault. Depending on the solution that you’re using, you might be able to implement better authentication and authorization mechanisms. For example, HashiCorp Vault allows you to use <a href="https://www.vaultproject.io/docs/auth/aws" target="_blank" rel="noopener">IAM auth</a> by using AWS IAM, rather than a static token.</li> 
    <li>This post addressed the creation of secrets and updating of secrets, but for your production setup, you should also consider deletion of secrets. Depending on your requirements, you can choose to implement a strategy that works best for you to handle secrets in AWS Secrets Manager once the original secret in HashiCorp Vault has been deleted. In the pull model, you could consider removing a secret in AWS Secrets Manager if the corresponding secret in your external secrets manager is no longer present.</li> 
    <li>In the sample setup, the same AWS KMS key is used to encrypt both the environment variables of the Lambda function, and the secrets in AWS Secrets Manager. You could choose to add an additional AWS KMS key (which would incur additional cost), to have two separate keys for these tasks. This would allow you to apply more granular permissions for the two keys in the corresponding KMS key policies or IAM identity policies that use the keys.</li> 
   <p>In this blog post, you’ve seen how you can approach replicating your secrets from an external secrets manager to AWS Secrets Manager. This post focused on a pull model, where the solution periodically fetched secrets from an external HashiCorp Vault and automatically created or updated the corresponding secret in AWS Secrets Manager. By using this model, you can now use your external secrets in your AWS Cloud applications or services that have an integration with AWS Secrets Manager.</p> 
   <p>If you have feedback about this post, submit comments in the Comments section below. If you have questions about this post, start a new thread on the <a href="https://repost.aws/tags/TAJAqKRtZ5RY6XCoNAUTcVzQ/aws-secrets-manager" rel="noopener" target="_blank">AWS Secrets Manager re:Post</a> or <a href="https://console.aws.amazon.com/support/home" rel="noopener" target="_blank">contact AWS Support</a>.</p> 
   <p><strong>Want more AWS Security news? Follow us on <a title="Twitter" href="https://twitter.com/AWSsecurityinfo" target="_blank" rel="noopener noreferrer">Twitter</a>.</strong></p> 

   <!-- '"` -->