停用及啟用服務帳戶金鑰

本頁說明如何使用Google Cloud 控制台、Google Cloud CLIIdentity and Access Management API 或其中一個 Google Cloud 用戶端程式庫,停用及啟用服務帳戶金鑰。

事前準備

  • Enable the IAM API.

    Enable the API

  • 設定驗證方法。

    Select the tab for how you plan to use the samples on this page:

    gcloud

      In the Google Cloud console, activate Cloud Shell.

      Activate Cloud Shell

      At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.

      Java

      如要在本機開發環境中使用本頁的 Java 範例,請安裝並初始化 gcloud CLI,然後使用使用者憑證設定應用程式預設憑證。

      1. Install the Google Cloud CLI.

      2. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

      3. To initialize the gcloud CLI, run the following command:

        gcloud init
      4. If you're using a local shell, then create local authentication credentials for your user account:

        gcloud auth application-default login

        You don't need to do this if you're using Cloud Shell.

        If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.

      詳情請參閱 Google Cloud 驗證說明文件中的「 為本機開發環境設定 ADC」。

      REST

      如要在本機開發環境中使用本頁的 REST API 範例,請使用您提供給 gcloud CLI 的憑證。

        After installing the Google Cloud CLI, initialize it by running the following command:

        gcloud init

        If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

      詳情請參閱 Google Cloud 驗證說明文件中的「Authenticate for using REST」。

    1. 瞭解服務帳戶憑證

必要的角色

如要取得停用及啟用服務帳戶金鑰所需的權限,請要求管理員授予您專案或服務帳戶的「服務帳戶金鑰管理員」 (roles/iam.serviceAccountKeyAdmin) IAM 角色,您要管理金鑰的服務帳戶也適用這項要求。如要進一步瞭解如何授予角色,請參閱「管理專案、資料夾和機構的存取權」。

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

詳情請參閱「服務帳戶角色」。

身分與存取權管理基本角色也包含管理服務帳戶金鑰的權限。您不應在正式版環境中授予基本角色,但可以在開發或測試環境中授予。

停用服務帳戶金鑰

停用服務帳戶金鑰後,您就無法再使用該金鑰向 Google API 進行驗證。您隨時可以啟用已停用的金鑰

建議您先停用服務帳戶金鑰,確認不再需要該金鑰後,再刪除金鑰。接著即可刪除金鑰。

您可以在 Google Cloud 控制台中查看已停用的金鑰,但無法使用Google Cloud 控制台停用金鑰。請改用 gcloud CLI 或 REST API。

gcloud

執行 gcloud iam service-accounts keys disable 指令,停用服務帳戶金鑰。

替換下列值:

  • KEY_ID:要停用的金鑰 ID。如要找出金鑰 ID,請列出服務帳戶的所有金鑰,找出要停用的金鑰,然後複製其 ID。
  • SA_NAME:金鑰所屬服務帳戶的名稱。
  • PROJECT_ID:您的 Google Cloud 專案 ID。
gcloud iam service-accounts keys disable KEY_ID \
    --iam-account=SA_NAME@PROJECT_ID.iam.gserviceaccount.com \
    --project=PROJECT_ID

輸出:

Disabled key [KEY_ID] for service account
[SA_NAME@PROJECT_ID.iam.gserviceaccount.com]

Java

如要瞭解如何安裝及使用 IAM 的用戶端程式庫,請參閱 IAM 用戶端程式庫。 詳情請參閱 IAM Java API 參考說明文件

如要向 IAM 進行驗證,請設定應用程式預設憑證。 詳情請參閱「事前準備」。


import com.google.cloud.iam.admin.v1.IAMClient;
import java.io.IOException;


public class DisableServiceAccountKey {

  public static void main(String[] args) throws IOException {
    // TODO(Developer): Replace the below variables before running.
    String projectId = "gcloud-project-id";
    String serviceAccountName = "service-account-name";
    String serviceAccountKeyName = "service-account-key-name";

    disableServiceAccountKey(projectId, serviceAccountName, serviceAccountKeyName);
  }

  // Disables a service account key.
  public static void disableServiceAccountKey(String projectId,
                                              String accountName,
                                              String key) throws IOException {
    // Construct the service account email.
    // You can modify the ".iam.gserviceaccount.com" to match the service account name in which
    // you want to disable the key.
    // See, https://cloud.google.com/iam/docs/creating-managing-service-account-keys#disabling
    String email = String.format("%s@%s.iam.gserviceaccount.com", accountName, projectId);
    String name = String.format("projects/%s/serviceAccounts/%s/keys/%s", projectId, email, key);

    // Initialize client that will be used to send requests.
    // This client only needs to be created once, and can be reused for multiple requests.
    try (IAMClient iamClient = IAMClient.create()) {
      iamClient.disableServiceAccountKey(name);

      System.out.println("Disabled service account key: " + name);
    }
  }
}

REST

projects.serviceAccounts.keys.disable 方法會停用服務帳戶金鑰。

使用任何要求資料之前,請先替換以下項目:

  • PROJECT_ID:您的 Google Cloud 專案 ID。專案 ID 為英數字串,例如 my-project
  • SA_NAME:要停用金鑰的服務帳戶名稱。
  • KEY_ID:要停用的金鑰 ID。如要找出金鑰的 ID,請列出服務帳戶的所有金鑰,找出要停用的金鑰,然後從 name 欄位結尾複製其 ID。金鑰 ID 是 keys/ 後方的所有內容。

HTTP 方法和網址:

POST https://iam.googleapis.com/v1/projects/PROJECT_ID/serviceAccounts/SA_NAME@PROJECT_ID.iam.gserviceaccount.com/keys/KEY_ID:disable

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

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

{
}

啟用服務帳戶金鑰

停用服務帳戶金鑰後,隨時可以啟用金鑰,然後使用金鑰向 Google API 進行驗證。

您無法使用 Google Cloud 控制台啟用服務帳戶金鑰。請改用 gcloud CLI 或 REST API。

gcloud

執行 gcloud iam service-accounts keys enable 指令,啟用服務帳戶金鑰。

替換下列值:

  • KEY_ID:要啟用的金鑰 ID。如要找出金鑰 ID,請列出服務帳戶的所有金鑰,找出要啟用的金鑰,然後複製其 ID。
  • SA_NAME:金鑰所屬服務帳戶的名稱。
  • PROJECT_ID:您的 Google Cloud 專案 ID。
gcloud iam service-accounts keys enable KEY_ID \
    --iam-account=SA_NAME@PROJECT_ID.iam.gserviceaccount.com\
    --project=PROJECT_ID

輸出:

Enabled key [KEY_ID] for service account
[SA_NAME@PROJECT_ID.iam.gserviceaccount.com]

Java

如要瞭解如何安裝及使用 IAM 的用戶端程式庫,請參閱 IAM 用戶端程式庫。 詳情請參閱 IAM Java API 參考說明文件

如要向 IAM 進行驗證,請設定應用程式預設憑證。 詳情請參閱「事前準備」。


import com.google.cloud.iam.admin.v1.IAMClient;
import java.io.IOException;


public class EnableServiceAccountKey {

  public static void main(String[] args) throws IOException {
    // TODO(Developer): Replace the below variables before running.
    String projectId = "gcloud-project-id";
    String serviceAccountName = "service-account-name";
    String serviceAccountKeyName = "service-account-key-name";

    enableServiceAccountKey(projectId, serviceAccountName, serviceAccountKeyName);
  }

  // Enables a service account key.
  public static void enableServiceAccountKey(String projectId,
                                             String accountName,
                                             String key) throws IOException {
    // Construct the service account email.
    // You can modify the ".iam.gserviceaccount.com" to match the service account name in which
    // you want to enable the key.
    // See, https://cloud.google.com/iam/docs/creating-managing-service-account-keys#enabling
    String email = String.format("%s@%s.iam.gserviceaccount.com", accountName, projectId);
    String name = String.format("projects/%s/serviceAccounts/%s/keys/%s", projectId, email, key);

    // Initialize client that will be used to send requests.
    // This client only needs to be created once, and can be reused for multiple requests.
    try (IAMClient iamClient = IAMClient.create()) {
      iamClient.enableServiceAccountKey(name);

      System.out.println("Enabled service account key: " + name);
    }
  }
}

REST

projects.serviceAccounts.keys.enable 方法可啟用服務帳戶金鑰。

使用任何要求資料之前,請先替換以下項目:

  • PROJECT_ID:您的 Google Cloud 專案 ID。專案 ID 為英數字串,例如 my-project
  • SA_NAME:要啟用金鑰的服務帳戶名稱。
  • KEY_ID:要啟用的金鑰 ID。如要找出金鑰的 ID,請列出服務帳戶的所有金鑰,找出要啟用的金鑰,然後從 name 欄位結尾複製其 ID。金鑰 ID 是 keys/ 後方的所有內容。

HTTP 方法和網址:

POST https://iam.googleapis.com/v1/projects/PROJECT_ID/serviceAccounts/SA_NAME@PROJECT_ID.iam.gserviceaccount.com/keys/KEY_ID:enable

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

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

{
}

後續步驟

歡迎試用

如果您未曾使用過 Google Cloud,歡迎建立帳戶來評估我們的產品在實際工作環境中的成效。新客戶還能獲得價值 $300 美元的免費抵免額,可用於執行、測試及部署工作負載。

免費試用