Secret data is immutable and most operations take place on secret versions. A secret version contains the actual secret data, along with state and metadata about the secret. This page describes how to add a secret version.
For more information about versioning, see this video on versioning.
Required roles
To get the permissions that you need to add a secret version, ask your administrator to grant you the following IAM roles on a secret:
-
Secret Manager Secret Version Adder (
roles/secretmanager.secretVersionAdder
) -
Secret Manager Secret Version Manager (
roles/secretmanager.secretVersionManager
)
For more information about granting roles, see Manage access to projects, folders, and organizations.
You might also be able to get the required permissions through custom roles or other predefined roles.
IAM roles can't be granted on a secret version.
Add a secret version
To add a secret version, use one of the following methods:
Console
-
Go to the Secret Manager page in the Google Cloud console.
-
On the Secret Manager page, click the Regional secrets tab, and then locate the secret for which you want to add the new version.
-
Click the
Actions menu associated with that secret, and then click Add new version. The Add new version dialog appears. -
In the Secret value field, enter a value for the secret such as
abcd1234
. Alternatively, you can upload a file containing the secret value. -
Click Add new version.
gcloud
Add a secret version from the contents of a file on disk
Before using any of the command data below, make the following replacements:
- SECRET_ID: the ID of the secret or fully qualified identifier for the secret
- LOCATION: the Google Cloud location of the secret
- FILE_PATH: the full path (including file name) to the file containing the version details
Execute the following command:
Linux, macOS, or Cloud Shell
gcloud secrets versions add SECRET_ID --location=LOCATION --data-file="FILE_PATH"
Windows (PowerShell)
gcloud secrets versions add SECRET_ID --location=LOCATION --data-file="FILE_PATH"
Windows (cmd.exe)
gcloud secrets versions add SECRET_ID --location=LOCATION --data-file="FILE_PATH"
The response contains the newly created secret version.
Add a secret version directly on the command line
You can also add a secret version directly on the command line, but this is discouraged because it appears as plaintext in the list of processes and may be captured by other system users. Note that the command with the plaintext will also be in your shell history.
Before using any of the command data below, make the following replacements:
- SECRET_DATA: the data that you want to store in the secret version
- SECRET_ID: the ID of the secret or fully qualified identifier for the secret
- LOCATION: the Google Cloud location of the secret
Execute the following command:
Linux, macOS, or Cloud Shell
echo -n "SECRET_DATA" | \ gcloud secrets versions add SECRET_ID --location=LOCATION --data-file=-
Windows (PowerShell)
echo -n "SECRET_DATA" | ` gcloud secrets versions add SECRET_ID --location=LOCATION --data-file=-
Windows (cmd.exe)
echo -n "SECRET_DATA" | ^ gcloud secrets versions add SECRET_ID --location=LOCATION --data-file=-
The response contains the newly created secret version.
Optional: Add a version from a file's contents when first creating a secret
Before using any of the command data below, make the following replacements:
- SECRET_ID: the ID of the secret or fully qualified identifier for the secret
- LOCATION: the Google Cloud location of the secret
- FILE_PATH: the full path (including file name) to the file containing the version details
Execute the following command:
Linux, macOS, or Cloud Shell
gcloud secrets create SECRET_ID --location=LOCATION --data-file="FILE_PATH"
Windows (PowerShell)
gcloud secrets create SECRET_ID --location=LOCATION --data-file="FILE_PATH"
Windows (cmd.exe)
gcloud secrets create SECRET_ID --location=LOCATION --data-file="FILE_PATH"
The response contains the newly created secret version.
REST
Base64-encode the secret data and save it as a shell variable.
$ SECRET_DATA=$(echo "seCr3t" | base64)
Before using any of the request data, make the following replacements:
- LOCATION: the Google Cloud location of the secret
- PROJECT_ID: the Google Cloud project ID
- SECRET_ID: the ID of the secret or fully qualified identifier for the secret
HTTP method and URL:
POST https://secretmanager.LOCATION.rep.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/secrets/SECRET_ID:addVersion
Request JSON body:
{"payload": {"data": "${SECRET_DATA}"}}
To send your request, choose one of these options:
curl
Save the request body in a file named request.json
,
and execute the following command:
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://secretmanager.LOCATION.rep.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/secrets/SECRET_ID:addVersion"
PowerShell
Save the request body in a file named request.json
,
and execute the following command:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://secretmanager.LOCATION.rep.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/secrets/SECRET_ID:addVersion" | Select-Object -Expand Content
You should receive a JSON response similar to the following:
{ "name": "projects/PROJECT_ID/locations/LOCATION/secrets/SECRET_ID/versions/1", "createTime": "2024-03-25T08:24:13.153705Z", "state": "ENABLED", "etag": "\"161477e6071da9\"" }
Go
To run this code, first set up a Go development environment and install the Secret Manager Go SDK. On Compute Engine or GKE, you must authenticate with the cloud-platform scope.
Java
To run this code, first set up a Java development environment and install the Secret Manager Java SDK. On Compute Engine or GKE, you must authenticate with the cloud-platform scope.
Node.js
To run this code, first set up a Node.js development environment and install the Secret Manager Node.js SDK. On Compute Engine or GKE, you must authenticate with the cloud-platform scope.
Python
To run this code, first set up a Python development environment and install the Secret Manager Python SDK. On Compute Engine or GKE, you must authenticate with the cloud-platform scope.
Secret version states
A secret version can be in one of the following states at any given time:
-
Enabled - In this state, the secret version can be accessed and described. This is the default state for a new secret version.
-
Disabled - In this state, the secret version cannot be accessed, but the secret's contents still exist. The secret version can be re-enabled to restore access.
-
Destroyed - In this state, the secret version's contents are discarded. The secret version cannot be changed to another state.
You are billed for both enabled and disabled secret versions. You are not billed for secret versions that are in the destroyed state.
What's next
- Access a regional secret version
- Assign an alias to a regional secret version
- List regional secret versions and view version details