This guide describes how to create and manage tags on regional secrets. You can use tags to group related Secret Manager secrets and store metadata about those resources based on their tags.
About tags
A tag is a key-value pair that can be attached to a resource within Google Cloud. You can use tags to conditionally allow or deny policies based on whether a resource has a specific tag. For example, you can conditionally grant Identity and Access Management (IAM) roles based on whether a resource has a specific tag. For more information, see Tags overview.
Tags are attached to resources by creating a tag binding resource that links the value to the Google Cloud resource.
Required permissions
To get the permissions that you need to manage tags, ask your administrator to grant you the following IAM roles:
-
Tag Viewer (
roles/resourcemanager.tagViewer) on the resources the tags are attached to -
View and manage tags at the organization level:
Organization Viewer (
roles/resourcemanager.organizationViewer) on the organization -
Create, update, and delete tag definitions:
Tag Administrator (
roles/resourcemanager.tagAdmin) on the resource you're creating, updating, or deleting tags for -
Attach and remove tags from resources:
Tag User (
roles/resourcemanager.tagUser) on the tag value and the resources that you are attaching or removing the tag value to
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.
To attach tags to Secret Manager secrets, you need the Secret Manager Admin role (roles/secretmanager.admin).
Create tag keys and values
Before you can attach a tag, you need to create a tag and configure its value. For more information, see Creating a tag and Adding a tag value.
Add tags during resource creation
You can add tags when you create regional secrets. Doing so lets you provide essential metadata for your resources and allows for better organization, cost tracking, and automated policy application.
Console
- Go to the Secret Manager page in the Google Cloud console.
- On the Secret Manager page, click the Regional secrets tab.
- Select the option to create a new regional secret.
- Click Manage tags.
- If your organization doesn't appear in the Manage tags panel, click Select scope for tags and select your organization or project.
- Click Add tag.
- Select the tag key and tag value from the list. You can filter the list using keywords.
- Click Save. The Tags section is updated with the tags information.
- Create your regional secret. The new regional secret is created with the provided tags.
gcloud
Before using any of the command data below, make the following replacements:
- SECRET_ID: the unique identifier of the secret.
- LOCATION: the location of the secret.
- TAG_KEY: the permanent ID or namespaced name of the tag key that's attached-for example,
tagKeys/567890123456. - TAG_VALUE: the permanent ID or namespaced name of the tag value that's attached—for example,
tagValues/567890123456.
Specify multiple tags by separating the tags with a comma, for example, TAGKEY1=TAGVALUE1,TAGKEY2=TAGVALUE2.
Execute the following command:
Linux, macOS, or Cloud Shell
gcloud secrets create SECRET_ID --location=LOCATION --tags=TAG_KEY=TAG_VALUE
Windows (PowerShell)
gcloud secrets create SECRET_ID --location=LOCATION --tags=TAG_KEY=TAG_VALUE
Windows (cmd.exe)
gcloud secrets create SECRET_ID --location=LOCATION --tags=TAG_KEY=TAG_VALUE
REST
Before using any of the request data, make the following replacements:
- LOCATION: the location of the secret
- PROJECT_ID: the ID of the project
- SECRET_ID: the unique identifier of the secret
- TAGKEY_NAME: the permanent ID or namespaced name of the tag key that's attached—for example,
tagKeys/567890123456. - TAGVALUE_NAME: the permanent ID or namespaced name of the tag value that is attached—for example,
tagValues/567890123456.
HTTP method and URL:
POST https://secretmanager.LOCATION.rep.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/secrets?secretId=SECRET_ID
Request JSON body:
{
"tags": {
"TAGKEY_NAME": "TAGVALUE_NAME"
}
}
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?secretId=SECRET_ID"
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?secretId=SECRET_ID" | Select-Object -Expand Content
You should receive a successful status code (2xx) and an empty response.
C#
To run this code, first set up a C# development environment and install the Secret Manager C# SDK. On Compute Engine or GKE, you must authenticate with the cloud-platform scope.
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.
PHP
To run this code, first learn about using PHP on Google Cloud and install the Secret Manager PHP 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.
Ruby
To run this code, first set up a Ruby development environment and install the Secret Manager Ruby SDK. On Compute Engine or GKE, you must authenticate with the cloud-platform scope.
Add tags to existing resources
To add a tag to existing regional secrets, follow these steps:
List tags attached to resources
You can view a list of tag bindings directly attached to or inherited by the regional secret.
Detach tags from resources
You can detach tags that have been directly attached to a regional secret. Inherited tags can be overridden by attaching a tag with the same key and a different value, but they can't be detached.
Delete tag keys and values
When removing a tag key or value definition, ensure that the tag is detached from the regional secret. You must delete existing tag attachments, called tag bindings, before deleting the tag definition itself. For more information, see Deleting tags.
Identity and Access Management conditions and tags
You can use tags and IAM conditions to conditionally grant role bindings to users in your hierarchy. Changing or deleting the tag attached to a resource can remove user access to that resource if an IAM policy with conditional role bindings has been applied. For more information, see Identity and Access Management conditions and tags.
What's next
- See the other services that support tags.
- See Tags and access control to learn how to use tags with IAM.