iTranslated by AI
How to Store Multi-line Strings in AWS Secrets Manager
Introduction
AWS Secrets Manager can be used to manage sensitive information in AWS.
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.
1. I'm hungry
2. I'm sleepy
3. I want money
-
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.txtBy 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 -
Create a key/value format JSON to register in Secrets Manager
In this case, we want to register in the format of key =bodyand 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" } -
Register with Secrets Manager using the
awsCLI
Register the value to Secrets Manager using theaws secretsmanager put-secret-valuecommand.First, create a Secrets Manager Secret named
multiline-samplein 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.jsonIt 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" ] } -
Verify the values in Secrets Manager
After executing the above command, if you try to verify the value using
Retrieve secret valuein 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 -DYou 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.
Reference Links
Discussion