本頁面說明如何擷取一段時間內建立的所有不同版本密鑰清單,以及如何查看特定密鑰版本的中繼資料。
必要的角色
如要取得列出密鑰版本及查看版本詳細資料所需的權限,請要求管理員在密鑰、專案、資料夾或機構中,授予您 Secret Manager 檢視者 (roles/secretmanager.viewer
) IAM 角色。如要進一步瞭解如何授予角色,請參閱「管理專案、資料夾和機構的存取權」。
列出密鑰版本
列出密鑰版本有下列好處:
-
您可以查看密鑰的變更記錄,瞭解密鑰的變更時間和變更者。這是稽核和法規遵循的必要條件。
-
如果密鑰意外更新或遭盜用,您可以復原為先前已知的正常版本。
-
您可以找出不再使用的版本,並安全地刪除。
-
你可以排解問題。舉例來說,如果應用程式發生問題,您可以檢查先前的密鑰版本,看看是否是密鑰變更所致。
如要列出密碼的所有版本,請使用下列其中一種方法:
控制台
-
前往 Google Cloud 控制台的「Secret Manager」頁面。
-
在「Secret Manager」頁面中,按一下「區域性密鑰」分頁標籤。
-
按一下密鑰即可存取其版本。
密鑰所屬的版本會顯示在「版本」表格中。
gcloud
使用下方的任何指令資料之前,請先替換以下項目:
- SECRET_ID:密鑰的 ID 或密鑰的完整 ID。
- LOCATION:密鑰的 Google Cloud 位置
執行下列指令:
Linux、macOS 或 Cloud Shell
gcloud secrets versions list SECRET_ID --location=LOCATION
Windows (PowerShell)
gcloud secrets versions list SECRET_ID --location=LOCATION
Windows (cmd.exe)
gcloud secrets versions list SECRET_ID --location=LOCATION
回應會包含密碼。
REST
使用任何要求資料之前,請先替換以下項目:
- LOCATION:密鑰的 Google Cloud 位置
- PROJECT_ID:專案 ID Google Cloud
- SECRET_ID:密鑰的 ID 或密鑰的完整 ID
HTTP 方法和網址:
GET https://secretmanager.LOCATION.rep.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/secrets/SECRET_ID/versions
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.LOCATION.rep.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/secrets/SECRET_ID/versions"
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.LOCATION.rep.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/secrets/SECRET_ID/versions" | Select-Object -Expand Content
您應該會收到如下的 JSON 回應:
{ "versions": [ { "name": "projects/PROJECT_ID/locations/LOCATION/secrets/SECRET_ID/versions/VERSION_ID", "createTime": "2024-09-04T06:41:57.859674Z", "state": "ENABLED", "etag": "\"1621457b3c1459\"" } ], "totalSize": 1 }
Go
如要執行這段程式碼,請先設定 Go 開發環境,並安裝 Secret Manager Go SDK。在 Compute Engine 或 GKE 上,您必須使用 cloud-platform 範圍進行驗證。
Java
如要執行這段程式碼,請先設定 Java 開發環境,並安裝 Secret Manager Java SDK。在 Compute Engine 或 GKE 上,您必須使用 cloud-platform 範圍進行驗證。
Node.js
如要執行這段程式碼,請先設定 Node.js 開發環境,並安裝 Secret Manager Node.js SDK。在 Compute Engine 或 GKE 上,您必須使用 cloud-platform 範圍進行驗證。
Python
如要執行這段程式碼,請先設定 Python 開發環境,然後安裝 Secret Manager Python SDK。在 Compute Engine 或 GKE 上,您必須使用 cloud-platform 範圍進行驗證。
取得密鑰版本的詳細資料
這個程序可讓您查看密鑰版本的 ID、建立日期和時間、加密詳細資料和狀態等中繼資料。查看密鑰版本的中繼資料,表示存取密鑰版本的相關資訊,但不是實際的密鑰值。如要查看密鑰值,請參閱「存取區域性密鑰版本」。
如要查看私密版本的元資料,請選擇下列其中一種做法:
控制台
-
前往 Google Cloud 控制台的「Secret Manager」頁面。
-
在「Secret Manager」頁面中,按一下「區域性密鑰」分頁標籤。
-
按一下密鑰即可存取其版本。
密鑰所屬的版本會顯示在「版本」表格中。 表格中也會顯示每個版本的版本 ID 和中繼資料。
gcloud
使用下方的任何指令資料之前,請先替換以下項目:
- VERSION_ID:密鑰版本的 ID
- SECRET_ID:密鑰的 ID 或密鑰的完整 ID。
- LOCATION:密鑰的 Google Cloud 位置
執行下列指令:
Linux、macOS 或 Cloud Shell
gcloud secrets versions describe VERSION_ID --secret=SECRET_ID --location=LOCATION
Windows (PowerShell)
gcloud secrets versions describe VERSION_ID --secret=SECRET_ID --location=LOCATION
Windows (cmd.exe)
gcloud secrets versions describe VERSION_ID --secret=SECRET_ID --location=LOCATION
回應會包含密碼。
REST
使用任何要求資料之前,請先替換以下項目:
- LOCATION:密鑰的 Google Cloud 位置
- PROJECT_ID:專案 ID Google Cloud
- SECRET_ID:密鑰的 ID 或密鑰的完整 ID
- VERSION_ID:密鑰版本的 ID
HTTP 方法和網址:
GET https://secretmanager.LOCATION.rep.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/secrets/SECRET_ID/versions/VERSION_ID
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.LOCATION.rep.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/secrets/SECRET_ID/versions/VERSION_ID"
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.LOCATION.rep.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/secrets/SECRET_ID/versions/VERSION_ID" | Select-Object -Expand Content
您應該會收到如下的 JSON 回應:
{ "name": "projects/PROJECT_ID/locations/LOCATION/secrets/SECRET_ID/versions/VERSION_ID", "createTime": "2024-09-04T06:41:57.859674Z", "state": "ENABLED", "etag": "\"1621457b3c1459\"" }
Go
如要執行這段程式碼,請先設定 Go 開發環境,並安裝 Secret Manager Go SDK。在 Compute Engine 或 GKE 上,您必須使用 cloud-platform 範圍進行驗證。
Java
如要執行這段程式碼,請先設定 Java 開發環境,並安裝 Secret Manager Java SDK。在 Compute Engine 或 GKE 上,您必須使用 cloud-platform 範圍進行驗證。
Node.js
如要執行這段程式碼,請先設定 Node.js 開發環境,並安裝 Secret Manager Node.js SDK。在 Compute Engine 或 GKE 上,您必須使用 cloud-platform 範圍進行驗證。
Python
如要執行這段程式碼,請先設定 Python 開發環境,然後安裝 Secret Manager Python SDK。在 Compute Engine 或 GKE 上,您必須使用 cloud-platform 範圍進行驗證。