在 Secret Manager 中创建轮替时间表

Secret Manager 支持密文的轮替时间表。Secret Manager 会根据提供的轮替频率和轮替时间向基于该 Secret 配置的 Pub/Sub 主题发送消息。本主题介绍如何为您的 Secret 设置一次性或定期时间表,以便在需要轮替 Secret 时收到通知。

定期轮替密文有助于:

  • 限制泄露的密文保持有效并公开漏洞的时长。
  • 持续执行轮替流程,以确保流程可靠性。

工作原理

Secret Manager 向 Secret 的 next_rotation_time 中针对该 Secret 配置的 Pub/Sub 主题发送 SECRET_ROTATE 消息。您可以通过以下两种方式之一设置此时间戳:

  1. 由用户在创建或更新 Secret 时提供。

  2. 如果提供了 rotation_period,Secret Manager 将在经过的 rotation_period 之后发送一条 SECRET_ROTATE 消息。next_rotation_time 将会更新,以反映新的旋转时间。

您必须配置 Pub/Sub 订阅者以接收 SECRET_ROTATE 消息并对其执行操作。如有必要,请实现其他工作流,例如添加新的 Secret 版本和触发应用部署。

Notes

  • 只能通过 Secret Manager v1 API 和 Google Cloud CLI 管理轮替政策。

  • 必须在密文上配置 Pub/Sub 主题。如需了解如何创建 Pub/Sub 主题和订阅,请参阅 Pub/Sub 快速入门。如需了解如何在密文上配置主题,请参阅 Secret Manager 的事件通知

  • 如果指定了 rotation_period,则必须设置 next_rotation_time

  • next_rotation_time 不能设置为将来不到五分钟的时间。rotation_period 的时长不得超过一小时。如需时间戳格式方面的指导,请参阅 gcloud 日期时间参考文档

  • 传送错误:Secret Manager 将自动重试失败的发送消息尝试。如果 Secret 或主题配置、权限或配额配置错误,Secret Manager 将无法保证有关传送的内容。

  • 为了防止并发轮替产生意外行为,必须先完成正在运行的轮替,然后才能启动另一个轮替。当 Secret Manager 尝试将消息发送到 Pub/Sub 时,通知会被视为传输中。如果存在正在进行的轮替,则系统会跳过安排的轮替。 Secret Manager 最多会自动重试失败的消息尝试(最多 7 天),之后系统会取消轮替。

示例

配置 Secret 的轮替

使用 next_rotation_time 创建密文,该密文自 2021 年 6 月 1 日起每 30 天轮替一次:

gcloud

gcloud secrets create secret-id \
    --replication-policy "automatic" \
    --next-rotation-time="2021-06-01T09:00:00Z" \
    --rotation-period="2592000s" \
    --topics="full-topic-name"

API

 curl "https://secretmanager.googleapis.com/v1/projects/${PROJECT_ID}/secrets?secretId=${SECRET_ID}" \
  --request "POST" \
  --header "Content-Type: application/json" \
  --header "Authorization: Bearer $(gcloud auth print-access-token)" \
  --data-binary @- <<EOF
{
"replication":{
  "automatic":{}
},
"topics": {"name" : "projects/${PROJECT_ID}/topics/${TOPIC_NAME}"},
"rotation":
  {
    "next_rotation_time": "2021-06-01T09:00:00Z",
    "rotation_period" : '2592000s'
  },
}
EOF

更新密钥的轮替设置

有效的轮替 updateMask 路径包括 rotationrotation.next_rotation_timerotation.rotation_period

更新 Secret 的 next_rotation_time

gcloud

gcloud secrets update secret-id \
  --next-rotation-time "2022-06-01T09:00:00Z"

API

  $ curl "https://secretmanager.googleapis.com/v1/projects/${PROJECT_ID}/secrets/${SECRET_ID}?updateMask=rotation.next_rotation_time" \
      --request "PATCH" \
      --header "Authorization: Bearer $(gcloud auth print-access-token)" \
      --header "Content-Type: application/json" \
      --data-binary @- <<EOF
  {
    "next_rotation_time": "2040-06-01T09:00:00Z"
  }
  EOF

为密文停用轮替

移除 Secret 的 next_rotation_time

gcloud

gcloud secrets update secret-id \
  --remove-next-rotation-time

API

  $ curl "https://secretmanager.googleapis.com/v1/projects/${PROJECT_ID}/secrets/${SECRET_ID}?updateMask=rotation.next_rotation_time" \
      --request "PATCH" \
      --header "Authorization: Bearer $(gcloud auth print-access-token)" \
      --header "Content-Type: application/json" \
      --data-binary @- <<EOF
  {}
  EOF

移除 Secret 的轮替时间表。这会移除 next_rotation_timerotation_period

gcloud

gcloud secrets update secret-id \
  --remove-rotation-schedule

API

  $ curl "https://secretmanager.googleapis.com/v1/projects/${PROJECT_ID}/secrets/${SECRET_ID}?updateMask=rotation" \
      --request "PATCH" \
      --header "Authorization: Bearer $(gcloud auth print-access-token)" \
      --header "Content-Type: application/json" \
      --data-binary @- <<EOF
  {}
  EOF

后续步骤