設定存取權控管

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

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

  • 授予應用程式存取權,允許存取 Google Cloud 服務,例如 Cloud Storage。所有 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
  2. 根據預設,應用程式環境會包含預設 App Engine 服務帳戶的憑證。

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

如要覆寫這項預設流程,請採取下列任一做法:

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

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

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

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

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

授予使用者 Cloud 資源存取權

如果想要讓應用程式讀取其他 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 服務及執行該版本的工作時,系統就會使用這個帳戶。