iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🌊

How to Store Multi-line Strings in AWS Secrets Manager

に公開

Introduction

AWS Secrets Manager can be used to manage sensitive information in AWS.
https://aws.amazon.com/jp/secrets-manager/

Single-line information such as access keys can be registered via the GUI or CLI, and the values can be verified on the GUI.
However, string data that spans multiple lines, such as various key information, cannot be registered or verified via the GUI.
This document explores how to register multi-line string data in Secrets Manager and verify the data.

Prerequisites

We will use version 2 of the aws CLI.
You need to additionally set the --cli-binary-format raw-in-base64-out option, which was not required in version 1.

▶  aws --version
aws-cli/2.15.28 Python/3.11.8 Darwin/23.2.0 source/arm64 prompt/off

Steps

The following procedure demonstrates how to store string data composed of multiple lines in Secrets Manager.

target.txt
1. I'm hungry
2. I'm sleepy
3. I want money
  1. Add newline codes to the target string
    Since we will register via the CLI using JSON, first add newline codes to the string.

    awk '{printf "%s\\n", $0}' target.txt
    

    By executing this command, you can obtain a string with newline codes added as follows:

    1. I'm hungry\n2. I'm sleepy\n3. I want money\n
    
  2. Create a key/value format JSON to register in Secrets Manager
    In this case, we want to register in the format of key = body and value = 1. I'm hungry\n2. I'm sleepy\n3. I want money\n, so create the following JSON.

    target.json
    {
        "body": "1. I'm hungry\n2. I'm sleepy\n3. I want money\n"
    }
    
  3. Register with Secrets Manager using the aws CLI
    Register the value to Secrets Manager using the aws secretsmanager put-secret-value command.

    First, create a Secrets Manager Secret named multiline-sample in advance.

    Register the value to that Secret using the following command.

    aws secretsmanager put-secret-value --cli-binary-format raw-in-base64-out --secret-id multiline-sample --secret-binary file://target.json
    

    It is successful if you receive a result like the following:

    {
        "ARN": "arn:aws:secretsmanager:ap-northeast-1:***:secret:multiline-sample-SxTUYv",
        "Name": "multiline-sample",
        "VersionId": "***",
        "VersionStages": [
            "AWSCURRENT"
        ]
    }
    
  4. Verify the values in Secrets Manager

    After executing the above command, if you try to verify the value using Retrieve secret value in the GUI, only a message like the following will be displayed, and you cannot verify the registered value.

    To verify the registered value, you need to use the CLI.
    You can check the value by using the following CLI:

    aws secretsmanager get-secret-value --secret-id multiline-sample | jq -r .SecretBinary | base64 -D
    

    You can verify the value in the JSON format you set during registration.

    {
            "body": "1. I'm hungry\n2. I'm sleepy\n3. I want money\n"
    }
    

    On the JSON, newline codes remain, but if you are using the AWS SDK, the SDK side can parse the newline codes and handle them as a multi-line string.

https://dev.classmethod.jp/articles/manage-binary-secrets-with-aws-secrets-manager/
https://blog.serverworks.co.jp/cli-invalid-base64

Discussion