設定存取權控管

存取權控管功能可決定哪些使用者有權存取 Google Cloud 專案中的服務和資源。在 App Engine 中,設定存取權控管有幾種不同的用途:

本頁面將概略說明如何在各項用途中設定存取權控管。

如要瞭解 Google Cloud Platform 如何處理存取權控管,請參閱 Identity and Access Management (IAM) 總覽

授予團隊成員存取權限

如要授予開發人員對 Google Cloud 專案的存取權,請建立下列項目之一或兩者:

  • 使用者帳戶:與 Google 帳戶相關聯,用於代表專案中的特定使用者。

    使用者帳戶可用於透過下列工具進行驗證:

    • Google Cloud 控制台
    • Google Cloud CLI
    • 使用 gcloud CLI 測試及部署 App Engine 應用程式的 IDE 和建構工具
  • 服務帳戶:用於代表應用程式或程序,而非使用者。在自動化建構、測試和部署程序中使用服務帳戶,特別是如果有多位開發人員可以執行這些程序時。

    服務帳戶可用於透過下列工具進行驗證:

    • gcloud CLI
    • 使用 gcloud CLI 工具測試及部署 App Engine 應用程式的 IDE 和建構工具

建立使用者帳戶

  1. 在 Google Cloud 控制台中開啟「IAM」頁面。

    開啟「IAM」頁面

  2. 按一下 [選取專案],選擇專案,然後按一下 [開啟]

  3. 按一下「新增」。

  4. 輸入電子郵件地址。

  5. 選取授予 App Engine 功能存取權的角色。

    如果使用者也需要存取其他雲端服務,請選取可存取其他雲端服務的角色

  6. 按一下 [儲存]

使用者現在可以登入 Google Cloud 控制台,並授權使用 gcloud CLI

您也可以透過 gcloud、REST API 或用戶端程式庫建立使用者帳戶。

建立服務帳戶

  1. 在 Google Cloud 主控台中開啟「Service Accounts」頁面。

    開啟「Service Accounts」(服務帳戶) 頁面

  2. 選取您的專案並按一下 [Open] (開啟)

  3. 按一下「Create Service Account」(建立服務帳戶)

  4. 輸入服務帳戶名稱。這應該是使用者容易閱讀的顯示名稱。

  5. 按一下 [建立]。

  6. 選取授予 App Engine 功能存取權的角色。

    如果服務帳戶也需要存取其他雲端服務,請選取可授予其他雲端服務存取權的角色

  7. 按一下「繼續」

  8. 視需要指定可管理服務帳戶的使用者帳戶。您也可以指定使用者帳戶,讓他們可以透過服務帳戶間接存取服務帳戶有權存取的所有資源。

  9. 按一下 [儲存]

    畫面上會顯示現有的服務帳戶清單。

  10. 您可以選擇在Google Cloud以外使用服務帳戶,請按照這篇文章的操作說明建立服務帳戶金鑰。

後續步驟

  • 如果您在自動化建構和部署程序中使用服務帳戶,請使用服務帳戶授權 gcloud CLI
  • 如果您使用服務帳戶搭配 IDE,請按照 IDE 提供的操作說明操作。
  • 如果您需要在存取其他 Google Cloud 服務或執行工作時,為 App Engine 應用程式版本使用專屬身分,可以在 App Engine 中指定使用者管理的服務帳戶

授予應用程式存取 Cloud 服務的權限

每個對 Cloud 服務的呼叫都需要經過驗證和授權,包括 App Engine 應用程式對其他 Cloud 服務 (例如 Cloud Storage) 的呼叫。

根據預設,App Engine 應用程式對同一專案中服務的呼叫會獲得授權。以下說明預設流程的運作方式:

  1. 如要啟動對雲端服務的呼叫,應用程式會建立用戶端物件,其中包含應用程式與服務互動所需的憑證和其他資料。如果您未在用戶端建構函式中指定憑證,用戶端會在應用程式環境中尋找憑證。

    以下是建立 Cloud Storage 用戶端的範例:

    Go

    
    // implicit uses Application Default Credentials to authenticate.
    func implicit() {
    	ctx := context.Background()
    
    	// For API packages whose import path is starting with "cloud.google.com/go",
    	// such as cloud.google.com/go/storage in this case, if there are no credentials
    	// provided, the client library will look for credentials in the environment.
    	storageClient, err := storage.NewClient(ctx)
    	if err != nil {
    		log.Fatal(err)
    	}
    	defer storageClient.Close()
    
    	it := storageClient.Buckets(ctx, "project-id")
    	for {
    		bucketAttrs, err := it.Next()
    		if err == iterator.Done {
    			break
    		}
    		if err != nil {
    			log.Fatal(err)
    		}
    		fmt.Println(bucketAttrs.Name)
    	}
    
    	// For packages whose import path is starting with "google.golang.org/api",
    	// such as google.golang.org/api/cloudkms/v1, use NewService to create the client.
    	kmsService, err := cloudkms.NewService(ctx)
    	if err != nil {
    		log.Fatal(err)
    	}
    
    	_ = kmsService
    }
    

    Java

    static void authImplicit() {
      // If you don't specify credentials when constructing the client, the client library will
      // look for credentials via the environment variable GOOGLE_APPLICATION_CREDENTIALS.
      Storage storage = StorageOptions.getDefaultInstance().getService();
    
      System.out.println("Buckets:");
      Page<Bucket> buckets = storage.list();
      for (Bucket bucket : buckets.iterateAll()) {
        System.out.println(bucket.toString());
      }
    }

    Node.js

    // Imports the Google Cloud client library.
    const {Storage} = require('@google-cloud/storage');
    
    // Instantiates a client. If you don't specify credentials when constructing
    // the client, the client library will look for credentials in the
    // environment.
    const storage = new Storage();
    // Makes an authenticated API request.
    async function listBuckets() {
      try {
        const results = await storage.getBuckets();
    
        const [buckets] = results;
    
        console.log('Buckets:');
        buckets.forEach(bucket => {
          console.log(bucket.name);
        });
      } catch (err) {
        console.error('ERROR:', err);
      }
    }
    listBuckets();

    PHP

    // Imports the Cloud Storage client library.
    use Google\Cloud\Storage\StorageClient;
    
    /**
     * Authenticate to a cloud client library using a service account implicitly.
     *
     * @param string $projectId The Google project ID.
     */
    function auth_cloud_implicit($projectId)
    {
        $config = [
            'projectId' => $projectId,
        ];
    
        # If you don't specify credentials when constructing the client, the
        # client library will look for credentials in the environment.
        $storage = new StorageClient($config);
    
        # Make an authenticated API request (listing storage buckets)
        foreach ($storage->buckets() as $bucket) {
            printf('Bucket: %s' . PHP_EOL, $bucket->name());
        }
    }

    Python

    def implicit():
        from google.cloud import storage
    
        # If you don't specify credentials when constructing the client, the
        # client library will look for credentials in the environment.
        storage_client = storage.Client()
    
        # Make an authenticated API request
        buckets = list(storage_client.list_buckets())
        print(buckets)
    
    

    Ruby

    # project_id = "Your Google Cloud project ID"
    
    require "google/cloud/storage"
    
    # If you don't specify credentials when constructing the client, the client
    # library will look for credentials in the environment.
    storage = Google::Cloud::Storage.new project: project_id
    
    # Make an authenticated API request
    storage.buckets.each do |bucket|
      puts bucket.name
    end
  2. 根據預設,應用程式環境會包含 預設 App Engine 服務帳戶的憑證。

    您建立 App Engine 應用程式時,Google 會建立這個服務帳戶,並授予該帳戶 Google Cloud 專案中所有 Cloud 服務的完整權限,以便管理及使用這些服務。

您可以透過下列任一方式覆寫這個預設流程:

  • 設定 GOOGLE_APPLICATION_CREDENTIALS 環境變數。如果設定了這個變數,Cloud 服務會使用變數指定的憑證,而非預設服務帳戶。

  • 為 Cloud 服務例項化 Client 物件時,請指定憑證。舉例來說,如果應用程式要呼叫其他專案中的 Cloud 服務,您可能需要手動傳遞憑證

如果您在程式碼中設定 GOOGLE_APPLICATION_CREDENTIALS 環境變數或傳遞憑證,建議您透過下列任一方式儲存憑證:

  • 將憑證儲存在安全位置 (例如 Datastore 模式的 Firestore),並在執行階段擷取憑證。
  • 將憑證保留在程式碼中,但使用 Cloud KMS 等金鑰庫加密憑證。

如要瞭解各項方法的優點,請參閱 選擇密鑰管理解決方案

授予使用者雲端資源存取權

如果您想要讓應用程式讀取其他 Google 服務中的使用者資料,就需要設定網路伺服器應用程式適用的 OAuth 2.0。舉例來說,如果您想要從 Google 雲端硬碟提取使用者資料,並將該資料帶入您的應用程式,則可使用網路伺服器應用程式適用的 OAuth 2.0 來共用特定資料,同時保有使用者名稱和密碼等其他資料的私密性。

Google Workspace 全網域授權委派

如果您有 Google Workspace (舊稱 G Suite) 網域,網域管理員可以授權應用程式,讓應用程式代表 Google Workspace 網域中的使用者存取使用者資料。舉例來說,透過 Google Calendar API 在 Google Workspace 網域中所有使用者的日曆中加入事件的應用程式,都可以使用服務帳戶來代表這些使用者存取 Google Calendar API。

這種授權服務帳戶代表所有網域使用者存取資料的方式,有時稱為「委派全網域授權」給服務帳戶。這種方法仍然使用 OAuth 2.0,並且需要由 Google Workspace 網域管理員委派全網域授權給服務帳戶

指定服務帳戶

App Engine 可讓您使用兩種服務帳戶:

  • 如果未指定使用者管理的服務帳戶,則 App Engine 預設服務帳戶會是 App Engine 應用程式所有版本的預設身分。

    根據貴機構的政策設定,系統可能會自動為預設服務帳戶授予專案的編輯者角色。強烈建議您 套用 iam.automaticIamGrantsForDefaultServiceAccounts 機構政策限制,停用自動角色授予功能。如果您是在 2024 年 5 月 3 日之後建立機構,系統預設會強制執行這項限制。

    如果您停用自動角色授予功能,就必須決定要將哪些角色授予預設服務帳戶,然後自行授予這些角色

    如果預設服務帳戶已具有「編輯者」角色,建議您將「編輯者」角色替換為權限較低的角色。如要安全地修改服務帳戶的角色,請使用政策模擬器查看變更的影響,然後授予及撤銷適當的角色

  • 使用者自行管理的服務帳戶是您在身分與存取權管理 (IAM) 中建立的服務帳戶。您可以為版本指定使用者管理的服務帳戶,系統會在存取其他 App Engine 服務和執行該版本的任務時使用該帳戶。