設定存取權控管

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

  • 將 Google Cloud 專案的存取權授予團隊成員,讓 他們可以設定服務及部署應用程式。

  • 授予應用程式存取權,以便存取 Cloud Storage 等 Google Cloud 服務。所有 Cloud 服務都必須針對每個 API 呼叫進行驗證和授權,包括來自 App Engine 應用程式的呼叫。

  • 授予使用者 Google Cloud 專案資源的存取權。雖然這種用途並不常見,但有時應用程式可能需要代表使用者要求存取 Cloud 資源。舉例來說,應用程式可能需要存取使用者的資料。

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

如要瞭解 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 功能存取權的角色。

    如果使用者也需要存取其他 Cloud 服務,請選取可授予其他 Cloud 服務存取權的角色。

  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 功能存取權的角色。

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

  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 服務的呼叫,應用程式會建立用戶端物件,其中包含應用程式與服務互動所需的憑證和其他資料。如果您未在用戶端的建構函式中指定憑證,用戶端會在應用程式的環境中尋找憑證。

    以下範例說明如何建立 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

    C#

    public object AuthImplicit(string projectId)
    {
        // If you don't specify credentials when constructing the client, the
        // client library will look for credentials in the environment.
        var credential = GoogleCredential.GetApplicationDefault();
        var storage = StorageClient.Create(credential);
        // Make an authenticated API request.
        var buckets = storage.ListBuckets(projectId);
        foreach (var bucket in buckets)
        {
            Console.WriteLine(bucket.Name);
        }
        return null;
    }
    
  2. 根據預設,應用程式的環境包含預設 App Engine 服務帳戶的憑證。

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

您可以採取下列任一做法,覆寫這項預設流程:

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

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

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

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

如要瞭解各種方法的優點,請參閱選擇 Secret 管理解決方案。

授予使用者 Cloud 資源存取權

如要讓應用程式從其他 Google 服務讀取使用者資料,請為網路伺服器應用程式設定 OAuth 2.0。舉例來說,如果您想從 Google 雲端硬碟提取使用者資料並匯入應用程式,請使用 OAuth 2.0 網頁伺服器應用程式流程,在不公開使用者名稱和密碼等其他資料的情況下,分享特定資料。

Google Workspace 全網域委派授權

如果您有 Google Workspace (舊稱 G Suite) 網域,該網域的管理員可以授權應用程式,代表 Google Workspace 網域中的使用者存取使用者資料。舉例來說,如果應用程式使用 Google 日曆 API,將活動新增至 Google Workspace 網域中所有使用者的日曆,就會使用服務帳戶代表使用者存取 Google 日曆 API。

授權服務帳戶代表網域中的使用者存取資料,有時稱為將「全網域授權委派」給服務帳戶。這項功能仍會使用 OAuth 2.0,且需要 Google Workspace 網域管理員將全網域授權授予服務帳戶。

指定服務帳戶

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

  • 各版本服務帳戶:設為已部署服務特定版本身分的服務帳戶。部署現有版本或新版本時,您可以指定服務帳戶做為該版本的身分。舉例來說,如果某個版本需要的權限與應用程式層級預設服務帳戶不同,您可以指派該版本專屬的服務帳戶。詳情請參閱「設定 App Engine 服務帳戶」。
  • 應用程式層級預設服務帳戶:如未設定各版本服務帳戶, Google Cloud 會針對所有已部署的服務,使用應用程式層級預設服務帳戶。建立應用程式時,您會指派應用程式層級的預設服務帳戶。詳情請參閱「指派應用程式層級的預設服務帳戶」。

    如未指派應用程式層級的預設服務帳戶,Google Cloud 會使用自動建立的 App Engine 預設服務帳戶 (PROJECT_ID@appspot.gserviceaccount.com)。

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

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

    如果預設服務帳戶已具備「編輯者」角色,建議您將「編輯者」角色替換為權限較少的角色。如要安全修改服務帳戶的角色,請使用 Policy Simulator 查看變更會造成的影響,然後授予及撤銷適當的角色。