篩選密鑰和密鑰版本清單

本頁說明在 Secret Manager 中篩選 Secret 和 Secret 版本的程序。在含有大量密鑰的環境中,篩選功能可協助快速找出特定密鑰或版本,不必手動捲動瀏覽整個清單。您可以根據標籤、建立日期或 Secret 名稱中的特定模式等條件進行篩選,集中管理特定 Secret 群組。

在 Secret Manager 中,您可以使用 Google Cloud 控制台中的「Filter」(篩選器) 選項,或在 API 呼叫中指定篩選條件,篩選密鑰和密鑰版本。在 Google Cloud CLI 中,您可以在列出密鑰時加入 filter 字串,篩選密鑰和密鑰版本。

篩選 Secret

如要篩選 Secret,請使用下列其中一種方法:

控制台

  1. 前往 Google Cloud 控制台的「Secret Manager」頁面。

    前往 Secret Manager

  2. 在「Secrets」表格中,按一下「篩選器」欄位。

  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:bsecret 經過網址編碼後會變成 name%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\""
    }
  ]
}

篩選密鑰版本

如要篩選密鑰版本,請按照下列步驟操作:

  • 在 Google Cloud 控制台中,選取密鑰以存取其版本,然後使用「Versions」(版本) 表格中的「Filter」(篩選器) 選項。

  • 如果您使用 Google Cloud CLI 或 Secret Manager API,請在列出密鑰版本時加入 filter 字串。

篩選器範例

用途 篩選器
名稱包含 mysecret 子字串的密鑰 name:mysecret
具有特定標籤的 Secret labels.environment=production
在日期/時間範圍內建立的密鑰 create_time<2021-01-01T06:00:00Z AND create_time>2021-01-01T01:00:00Z
自動複製 Secret replication.automatic:*
Secret 採用使用者自管複製政策,但未儲存在任何指定區域 replication.user_managed.replicas.location:* AND NOT replication.user_managed.replicas.location:(us-central1 OR us-east1)
使用 CMEK 金鑰加密的 Secret replication.user_managed.replicas.customerManagedEncryption:*
使用特定 CMEK 金鑰加密的密鑰 replication.user_managed.replicas.customerManagedEncryption.kmsKeyName=projects/p/locations/us-central1/keyRings/kr/cryptoKeys/my-cmek-key
未設定輪替週期的 Secret NOT rotation.next_rotation_time:*
Secret 輪替週期 > 30 天 rotation.rotation_period>259200s
已設定到期日的 Secret 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。

空格等同於 AND,因此下列項目等同:
name:"myapp" AND name:"secret1"
name:"myapp" name:"secret1"

OR 邏輯 OR。
*

萬用字元。

可單獨使用,其中 field:* 表示已設定 field。

與 Cloud Search API 一致,除非使用括號明確定義不同的順序,否則系統會在 AND 作業之前評估 OR 作業。

篩選 time 值時,請將時間編碼為 RFC 3399 格式的字串,例如 2020-10-15T01:30:15Z。

存取子欄位時,請使用 dot 語法。舉例來說,Secret 資源可能包含 labels 欄位,其值為鍵/值 map。如果使用 color 標籤,您可以按照下列方式篩選子欄位 labels.color 的 Secret 結果:

labels.color=red

如要只列出已設定 color 標籤的 Secret,請使用萬用字元:

labels.color:*

加引號的字串會解譯為單一值,而非一串值。

篩選欄位

您可以篩選 Secret 或 SecretVersion 物件的任何欄位。

List 方法 可篩選欄位的連結
projects.secrets.list Secret 欄位
projects.secrets.versions.list SecretVersion 欄位

結果總數

如果在清單要求中設定 filter,回應就不會指出結果總數 (回應中的 total_size=0)。

後續步驟