Set up notifications on a secret

This page explains how to configure and use event notifications for your secrets in Secret Manager.

Overview

Secret Manager integrates with Pub/Sub to provide event notifications for changes to both secrets and secret versions. You can use these notifications to initiate workflows, such as restarting an application when a new secret version is added, or notifying security engineers when a secret is deleted. For more information on how to use these notifications to start workflows, see the Pub/Sub documentation.

How event notifications work in Secret Manager

Secrets can be configured with a list of up to 10 Pub/Sub topics. Whenever an operation is performed that modifies the secret or one of its versions, Secret Manager automatically publishes a message to each of the Pub/Sub topics on that secret. Get, List, and Access calls don't result in message publications.

Pub/Sub messages have a set of attribute key-value pairs containing metadata about the event, as well as a data field containing a full JSON serialization of the Secret or SecretVersion resource which was created or modified. This JSON is a UTF-8 encoded string that represents the Secret or SecretVersion resource in exactly the form specified by the Secret Manager public API, encoded in JSON as specified in the proto3 JSON Mapping.

Event types

The following is a list of event types supported by Secret Manager.

Event type Description
SECRET_CREATE Sent when a new secret is successfully created.
SECRET_UPDATE Sent when a new secret is successfully updated.
SECRET_DELETE Sent when a secret is deleted, either because of a user-initiated request or secret expiration.
SECRET_VERSION_ADD Sent when a new secret version is successfully added.
SECRET_VERSION_ENABLE Sent when a secret version is enabled.
SECRET_VERSION_DISABLE Sent when a secret version is disabled.
SECRET_VERSION_DESTROY Sent when a secret version is destroyed.
SECRET_VERSION_DESTROY_SCHEDULED Sent when a destruction delay duration is configured on the secret and the user attempts to destroy a secret version.
SECRET_ROTATE Sent when it is time to rotate a secret. See Create rotation schedules for more information.
TOPIC_CONFIGURED

This is a test message with no body or attributes other than eventType: TOPIC_CONFIGURED. This is sent when a secret is created or updated with a list of Pub/Sub topics, but does not indicate that the operation was successful.

A SECRET_CREATE or SECRET_UPDATE message is sent immediately afterwards if the operation was successful.

Whenever topics are updated on a secret, a TOPIC_CONFIGURED message is sent to all topics on the secret, including ones that were already present.

Notification format

Notifications sent to the Pub/Sub topic consist of two parts:

  • Attributes: A set of key-value pairs describing the event.

  • Data: A string that contains the metadata of the changed object.

Attributes

Attributes are key-value pairs contained in notifications sent by Secret Manager to your Pub/Sub topic. All notifications other than TOPIC_CONFIGURED test messages always contain the following set of key:value pairs, regardless of the notification's data:

Attribute name Example Description
eventType SECRET_CREATE The type of event that has just occurred. See Event types for a list of possible values.
dataFormat JSON_API_V1 The format of the object data.
secretId projects/p/secrets/my-secret The full resource name of the secret on which the event occurred.
timestamp 2021-01-20T11:17:45.081104-08:00 The time the event occurred.

In addition, notifications sometimes contain the following set of key-value pairs:

Attribute name Example Description
versionId projects/p/secrets/my-secret/versions/456

The name of the secret version on which the event occurred.

This is only present on SECRET_VERSION_ADD, SECRET_VERSION_ENABLE, SECRET_VERSION_DISABLE, and SECRET_VERSION_DESTROY event notifications.

deleteType REQUESTED Whether the delete was requested by a user (REQUESTED) or due to secret expiration (EXPIRATION). Only present on SECRET_DELETE event notifications.

Data

The data field is a UTF-8 string that contains the metadata of the changed object. Data is either a secret or secret version.

For SECRET_DELETE notifications, the metadata contained in the data field represents the object metadata as it was before the delete. For all other notifications, the metadata included in the data field represents the object metadata after the change occurs.

Limitations

  • Event notifications is available only in the Secret Manager v1 API and Google Cloud CLI.

  • You can't reliably use event notifications with Pub/Sub topics that have a message storage policy where enforceInTransit is set to true.

    Secret Manager publishes all event notifications from a global endpoint. If a Pub/Sub topic has enforceInTransit set to true, it restricts publishing to specific allowedPersistenceRegions. The regional restriction conflicts with the global publishing endpoint for Secret Manager, causing the publish request to fail with a FAILED_PRECONDITION error.

Before you begin

You may choose to store all resources in the same project or to store secrets and Pub/Sub topics in separate projects.

  1. To set up Secret Manager, complete the following:

    • Create or use an existing project to hold your Secret Manager resources.

    • If necessary, complete the steps mentioned in the Enable the Secret Manager API page.

  2. To set up Pub/Sub, complete the following:

  3. Authenticate to Google Cloud using the following command:

        $ gcloud auth login --update-adc
        

Create a service agent identity

To create a service agent identity for each project that requires secrets with event notifications, follow these steps:

  1. To create a service identity with Google Cloud CLI, run the following command:

          $ gcloud beta services identity create \
              --service "secretmanager.googleapis.com" \
              --project "PROJECT_ID"
        

    This command returns a service account name, with the following format:

        service-PROJECT_NUMBER@gcp-sa-secretmanager.iam.gserviceaccount.com
        
  2. Grant this service account permission to publish on the Pub/Sub topics configured on your secrets.

  3. Save the service account name as an environment variable using the following command:

        # This is from the output of the command above
        $ export SM_SERVICE_ACCOUNT="service-...."
        

The environment variables for the Secret Manager project, Pub/Sub project, and Secret Manager service account must be set the entire time you are following this procedure.

Create Pub/Sub topics

Follow the Pub/Sub quickstart to create topics in your Pub/Sub project in the Google Cloud console. Alternatively, create topics in the Google Cloud CLI using the following command:

gcloud

Before using any of the command data below, make the following replacements:

  • PUBSUB_PROJECT_ID: the ID of the project in which to create subscriptions
  • PUBSUB_TOPIC_NAME: the name of the topic

Execute the following command:

Linux, macOS, or Cloud Shell

gcloud pubsub topics create "projects/PUBSUB_PROJECT_ID/topics/PUBSUB_TOPIC_NAME"

Windows (PowerShell)

gcloud pubsub topics create "projects/PUBSUB_PROJECT_ID/topics/PUBSUB_TOPIC_NAME"

Windows (cmd.exe)

gcloud pubsub topics create "projects/PUBSUB_PROJECT_ID/topics/PUBSUB_TOPIC_NAME"

Repeat this multiple times if you want to create multiple Pub/Sub topics on the secret.

Grant the service account for Secret Manager permission to publish on the topics

You can grant permissions to the Secret Manager service account through the Google Cloud console or through Google Cloud CLI.

To grant the Pub/Sub Publisher role (roles/pubsub.publisher) on the Pub/Sub topic, use the following command:

gcloud

Before using any of the command data below, make the following replacements:

  • PUBSUB_TOPIC_NAME: the name of the topic

Execute the following command:

Linux, macOS, or Cloud Shell

gcloud pubsub topics add-iam-policy-binding PUBSUB_TOPIC_NAME \
    --member "serviceAccount:${SM_SERVICE_ACCOUNT}" \
    --role "roles/pubsub.publisher"

Windows (PowerShell)

gcloud pubsub topics add-iam-policy-binding PUBSUB_TOPIC_NAME `
    --member "serviceAccount:${SM_SERVICE_ACCOUNT}" `
    --role "roles/pubsub.publisher"

Windows (cmd.exe)

gcloud pubsub topics add-iam-policy-binding PUBSUB_TOPIC_NAME ^
    --member