編輯密鑰

本頁說明如何編輯密碼的中繼資料。您可以更新 Secret 的許多屬性,例如加密類型、輪替政策、到期日、標籤和事件通知。您也可以為密鑰版本新增註解及設定別名。您無法編輯密鑰名稱或值,也無法編輯複製政策。如要為密鑰新增值,請建立新的密鑰版本

必要的角色

如要取得更新 Secret 中繼資料所需的權限,請要求系統管理員在 Secret 或專案中授予您「Secret Manager 管理員 」(roles/secretmanager.admin) IAM 角色。如要進一步瞭解如何授予角色,請參閱「管理專案、資料夾和組織的存取權」。

您或許也能透過自訂角色或其他預先定義的角色,取得必要權限。

編輯密鑰

如要編輯密鑰,請使用下列其中一種做法:

控制台

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

    前往 Secret Manager

  2. 如要編輯密鑰,請使用下列其中一種方式:

    • 在清單中找出密鑰,然後點選與該密鑰相關的「動作」選單。在「動作」選單中,按一下「編輯」

    • 按一下密鑰名稱,前往密鑰詳細資料頁面。在密鑰詳細資料頁面中,按一下「編輯密鑰」

  3. 在「編輯密鑰」頁面中,視需要更新密鑰的屬性,然後按一下「更新密鑰」

gcloud

使用下列任何指令資料之前,請先替換以下項目:

  • SECRET_ID:密鑰 ID
  • KEY:標籤鍵
  • VALUE:標籤的對應值

執行下列指令:

Linux、macOS 或 Cloud Shell

gcloud secrets update SECRET_ID \
    --update-labels=KEY=VALUE

Windows (PowerShell)

gcloud secrets update SECRET_ID `
    --update-labels=KEY=VALUE

Windows (cmd.exe)

gcloud secrets update SECRET_ID ^
    --update-labels=KEY=VALUE

回應會傳回更新後的密鑰。

REST

使用任何要求資料之前,請先修改下列項目的值:

  • PROJECT_ID: Google Cloud 專案 ID
  • SECRET_ID:密鑰 ID
  • KEY:標籤鍵
  • VALUE:標籤的對應值

HTTP 方法和網址:

PATCH https://secretmanager.googleapis.com/v1/projects/PROJECT_ID/secrets/SECRET_ID?updateMask=labels

JSON 要求內文:

{'labels': {'KEY': 'VAL'}}

如要傳送要求,請選擇以下其中一個選項:

curl

將要求主體儲存在名為 request.json 的檔案中,然後執行下列指令:

curl -X PATCH \
-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/SECRET_ID?updateMask=labels"

PowerShell

將要求主體儲存在名為 request.json 的檔案中,然後執行下列指令:

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

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

您應該會收到如下的 JSON 回覆:

{
  "name": "projects/PROJECT_ID/locations/LOCATION/secrets/SECRET_ID",
  "createTime": "2024-09-02T07:14:00.281541Z",
  "labels": {
    "key": "value"
  },
  "etag": "\"16211dc7d040b1\""
}

C#

如要執行這段程式碼,請先設定 C# 開發環境,並安裝 Secret Manager C# SDK。在 Compute Engine 或 GKE 上,您必須使用 cloud-platform 範圍進行驗證


using Google.Protobuf.WellKnownTypes;
using Google.Cloud.SecretManager.V1;

public class UpdateSecretSample
{
    public Secret UpdateSecret(string projectId = "my-project", string secretId = "my-secret")
    {
        // Create the client.
        SecretManagerServiceClient client = SecretManagerServiceClient.Create();

        // Build the secret with updated fields.
        Secret secret = new Secret
        {
            SecretName = new SecretName(projectId, secretId),
        };
        secret.Labels["secretmanager"] = "rocks";

        // Build the field mask.
        FieldMask fieldMask = FieldMask.FromString("labels");

        // Call the API.
        Secret updatedSecret = client.UpdateSecret(secret, fieldMask);
        return updatedSecret;
    }
}

Go

如要執行這段程式碼,請先設定 Go 開發環境,並安裝 Secret Manager Go SDK。在 Compute Engine 或 GKE 上,您必須使用 cloud-platform 範圍進行驗證

import (
	"context"
	"fmt"
	"io"

	secretmanager "cloud.google.com/go/secretmanager/apiv1"
	"cloud.google.com/go/secretmanager/apiv1/secretmanagerpb"
	"google.golang.org/genproto/protobuf/field_mask"
)

// updateSecret updates the metadata about an existing secret.
func updateSecret(w io.Writer, name string) error {
	// name := "projects/my-project/secrets/my-secret"

	// Create the client.
	ctx := context.Background()
	client, err := secretmanager.NewClient(ctx)
	if err != nil {
		return fmt.Errorf("failed to create secretmanager client: %w", err)