在 Secret Manager 中创建轮替时间表

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

定期轮替密文有助于:

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

工作原理

Secret Manager 向 Pub/Sub 主题发送 SECRET_ROTATE 消息 在 Secret 的 next_rotation_time 上配置该 Secret。此时间戳为 通过以下两种方式之一进行设置:

  1. 用户在创建或更新密钥时提供。

  2. 如果提供了 rotation_period,Secret Manager 将发送 SECRET_ROTATE 后显示 rotation_period 消息。next_rotation_time 将更新为 以反映新的下次轮替时间。

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

备注

  • 只能在 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的时长不得少于 1 小时。如需时间戳格式方面的指导, 请参阅 gcloud 日期时间参考

  • 传送错误:Secret Manager 将自动重试失败的尝试 发送消息。Secret Manager 不对交付情况做出任何保证 有关 Secret 或主题配置、权限或配额的配置是否有误。

  • 进行中的轮替必须先完成,然后才能开始另一次轮替,以防止并发轮替产生意外行为。通知已开启 在 Secret Manager 尝试将消息发送到 Pub/Sub。如果存在正在执行的轮替,则会跳过已安排的轮替。 Secret Manager 会自动重新尝试发送消息的失败尝试 最多七天,7 天之后轮替将被取消。

示例

配置密钥的轮替

使用 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
  {
    "rotation": {"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

移除密钥的轮替时间表。这会移除 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

后续步骤