过滤 secret 和 secret 版本列表

本页介绍了在 Secret Manager 中过滤 secret 和 secret 版本的过程。 在包含大量 secret 的环境中,过滤功能有助于快速识别特定 secret 或版本,而无需手动滚动浏览整个列表。您可以根据标签、创建日期或密钥名称中的特定模式等条件进行过滤,从而有针对性地管理特定组的密钥。

在 Secret Manager 中,您可以使用 Google Cloud 控制台中的过滤选项或在 API 调用中指定过滤条件来过滤密钥和密钥版本。在 Google Cloud CLI 中,您可以在列出密文时添加 filter 字符串,以过滤密文和密文版本。

过滤 Secret

如需过滤 Secret,请使用以下方法之一:

控制台

  1. 在 Google Cloud 控制台中,转到 Secret Manager 页面。

    前往 Secret Manager

  2. 密钥表中,点击过滤条件字段。

  3. 选择过滤条件属性及其对应的值,例如 Location:asia-east1

    系统会根据输入的值自动过滤表格。结果会按名称升序排列。

gcloud

在使用下面的命令数据之前,请先进行以下替换:

  • FILTER:过滤条件字符串,例如 name:asecret OR name:bsecret。 gcloud CLI 还支持正则表达式,例如 name ~ "secret_ab.*"

执行以下命令:

Linux、macOS 或 Cloud Shell

gcloud secrets list --filter="FILTER"

Windows (PowerShell)

gcloud secrets list --filter="FILTER"

Windows (cmd.exe)

gcloud secrets list --filter="FILTER"

REST

在使用任何请求数据之前,请先进行以下替换:

  • PROJECT_ID: Google Cloud 项目 ID
  • FILTER:过滤条件字符串。过滤条件指定为 filter 查询字符串参数,并且必须采用网址编码。例如,过滤条件 name:asecret OR name:bsecretname%3Aasecret+OR+name%3Absecret 形式进行网址编码。API 不支持正则表达式。

HTTP 方法和网址:

GET https://secretmanager.googleapis.com/v1/projects/PROJECT_ID/secrets?filter=FILTER

请求 JSON 正文:

{}

如需发送请求,请选择以下方式之一:

curl

将请求正文保存在名为 request.json 的文件中,然后执行以下命令:

curl -X GET \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://secretmanager.googleapis.com/v1/projects/PROJECT_ID/secrets?filter=FILTER"

PowerShell

将请求正文保存在名为 request.json 的文件中,然后执行以下命令:

$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }

Invoke-WebRequest `
-Method GET `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://secretmanager.googleapis.com/v1/projects/PROJECT_ID/secrets?filter=FILTER" | Select-Object -Expand Content

您应该收到类似以下内容的 JSON 响应:

{
  "secrets": [
    {
      "name": "projects/PROJECT_ID/locations/LOCATION/secrets/SECRET_ID",
      "createTime": "2024-09-02T07:14:00.281541Z",
      "etag": "\"16211dd90b37e7\""
    }
  ]
}

过滤 Secret 版本

如需过滤 Secret 版本,请执行以下操作:

  • 在 Google Cloud 控制台中,选择要访问其版本的密钥,然后使用版本表中的过滤条件选项。

  • 如果您使用的是 Google Cloud CLI 或 Secret Manager API,请在列出 Secret 版本时添加 filter 字符串。

过滤条件示例

使用场景 过滤
名称包含 mysecret 子字符串的密文 name:mysecret
具有特定标签的密文 labels.environment=production
在日期/时间范围内创建的 Secret create_time<2021-01-01T06:00:00Z AND create_time>2021-01-01T12:00:00Z
具有自动复制功能的密文 replication.automatic:*
具有用户管理的复制功能但未存储在任一给定区域中的密文 replication.user_managed.replicas.location:* AND NOT replication.user_managed.replicas.location:(us-central1 OR us-east1)
使用 CMEK 密钥加密的密文 replication.user_managed.replicas.customerManagedEncryption:*
使用特定 CMEK 加密的 Secret replication.user_managed.replicas.customerManagedEncryption.kmsKeyName=projects/p/locations/us-central1/keyRings/kr/cryptoKeys/my-cmek-key
没有轮替周期的密文 NOT rotation.next_rotation_time:*
轮替周期超过 30 天的密文 rotation.rotation_period>259200s
设置了过期时间的密文 expire_time:*
在指定日期之前过期的密文 expire_time<2021-07-31
已启用或已停用的版本 state:(ENABLED OR DISABLED)
已销毁的版本,在指定日期后销毁 state:DESTROYED AND destroy_time>2021-01-01

过滤器语法

过滤器语法由要过滤的对象的一个或多个字段上的表达式组成。

您可以使用以下表达式运算符。

运算符 说明
= 相等。
> 大于。
< 小于。
>= 大于或等于。
<= 小于或等于。
!=
-
NOT
不相等。 以下表达式是等效的:
name!="topsecret"
-name="topsecret"
NOT name="topsecret"
:

包含关系。这是一个不区分大小写的子字符串匹配。

例如,name:"myapp" 用于过滤资源名称中包含 myapp(不区分大小写)的资源。

AND

逻辑与。

空格相当于 AND,因此以下表达式是等效的:
name:"myapp" AND name:"secret1"
name:"myapp" name:"secret1"

OR 逻辑或。
*

通配符。

可用作独立项,其中 field:* 表示已设置 field

与 Cloud Search API 一致,除非使用圆括号明确定义其他顺序,否则 OR 操作会在 AND 操作之前进行求值。

过滤 time 值时,请将时间编码为 RFC 3399 格式的字符串,例如 2020-10-15T01:30:15Z

访问子字段时,请使用点式语法。例如,Secret 资源可能包含 labels 字段,其值为键值对 map。如果使用 color 标签,您可以按子字段 labels.color 过滤 Secret 结果,如下所示:

labels.color=red

如果您只想列出设置了 color 标签的密文,请使用通配符:

labels.color:*

用英文引号引起来的一个字符串会被解释为单个值,而不是一系列值。

过滤字段

您可以按 SecretSecretVersion 对象的任何字段进行过滤。

list 方法 可过滤字段的链接
projects.secrets.list Secret 字段
projects.secrets.versions.list SecretVersion 字段

结果总数

如果在 list 请求中设置了 filter,则响应不会指明结果总数(在响应中,total_size=0)。

后续步骤