API キーを使用する
このページでは、API Gateway で API キーを使用する方法について説明します。
API キーは、割り当て、課金、モニタリングのためにGoogle Cloud プロジェクトを識別する文字列です。デベロッパーは、 Google Cloud コンソールでプロジェクトの API キーを生成します。デベロッパーは、このキーをクエリ パラメータとして、またはリクエスト ヘッダーとしてすべての API 呼び出しに埋め込みます。
API 構成で API キーを必須として指定した場合、API Gateway はその API キーを使用して、関連付けられている Google Cloud プロジェクトを検索します。現在お使いの Google Cloud プロジェクトまたは API が有効になっている他のGoogle Cloud プロジェクトで API キーが生成されていない場合、API ゲートウェイ はリクエストを拒否します。
API キーを作成するか、 Google Cloud プロジェクト内ですでに使用可能な API キーを表示するには、[API とサービス] > [認証情報] ページに移動します。
API キーを使用する
割り当てなどの API Gateway 機能を使用するには、API キーを渡すことで、API Gateway がクライアント アプリケーションに関連付けられている Google Cloud プロジェクトを識別できます。
API Gateway の API キー認証を構成する
API キーを使用してゲートウェイへのアクセスを保護するには:
- サービスの API キーサポートを有効にします。次のコマンドを入力します。ここで、
- MANAGED_SERVICE_NAME は、API のデプロイ時に作成されたマネージド サービスの名前を指定します。これは、
gcloud api-gateway apis describeコマンドで一覧表示されるマネージド サービスのプロパティで確認できます。 - PROJECT_ID は Google Cloud プロジェクトの名前を指定します。
次に例を示します。gcloud services enable MANAGED_SERVICE_NAME.apigateway.PROJECT_ID.cloud.goog
gcloud services enable my-api-123abc456def1.apigateway.my-project.cloud.goog
- MANAGED_SERVICE_NAME は、API のデプロイ時に作成されたマネージド サービスの名前を指定します。これは、
- API 構成の作成に使用される OpenAPI 仕様を変更して、すべてのトラフィックに API キー検証セキュリティ ポリシーを適用する手順を含めます。以下に示すように、
securityタイプとsecurityDefinitionsまたはsecuritySchemesを追加します。OpenAPI 2.0
# openapi2-functions.yaml swagger: '2.0' info: title: API_ID optional-string description: Sample API on API Gateway with a Google Cloud Functions backend version: 1.0.0 schemes: - https produces: - application/json paths: /hello: get: summary: Greet a user operationId: hello x-google-backend: address: https://GCP_REGION-PROJECT_ID.cloudfunctions.net/helloGET security: - api_key: [] responses: '200': description: A successful response schema: type: string securityDefinitions: # This section configures basic authentication with an API key. api_key: type: "apiKey" name: "key" in: "query"
securityDefinitionは、仕様で定義されているすべてのパスへのアクセスをリクエストするときに、keyという名前のクエリ パラメータとして渡される API キーをリクエストするように API を構成します。OpenAPI 3.x
# openapi-functions.yaml openapi: 3.0.4 info: title: API_ID optional-string description: Sample API on API Gateway with a Google Cloud Functions backend version: 1.0.0 # Define reusable components in x-google-api-management x-google-api-management: backend: functions_backend: address: https://GATEWAY_LOCATION-PROJECT_ID.cloudfunctions.net/helloGET pathTranslation: APPEND_PATH_TO_ADDRESS protocol: "http/1.1" # Apply the backend configuration by referencing it by name. Set at the root so this applies to all operations unless overridden. x-google-backend: functions_backend components: # This section configures basic authentication with an API key. securitySchemes: google_api_key: type: apiKey name: x-api-key in: header security: - google_api_key: [] paths: /hello: get: summary: Greet a user operationId: hello responses: '200': description: A successful response content: application/json: schema: type: string
securitySchemesは、仕様で定義されているすべてのパスへのアクセスをリクエストするときに、keyという名前のクエリ パラメータとして渡される API キーをリクエストするように API を構成します。 - 次のコマンドを使用して、変更した OpenAPI 説明で新しい API 構成を作成します。
次に例を示します。gcloud api-gateway api-configs create NEW_CONFIG_ID \ --api=API_ID --openapi-spec=NEW_API_DEFINITION \ --project=PROJECT_ID --backend-auth-service-account=SERVICE_ACCOUNT_EMAIL
gcloud api-gateway api-configs create my-config-key \ --api=my-api --openapi-spec=openapi-functions.yaml \ --project=my-project --backend-auth-service-account=0000000000000compute@developer.gserviceaccount.com
- 次のコマンドを実行して、新しい API 構成で既存のゲートウェイを更新します。
次に例を示します。gcloud api-gateway gateways update GATEWAY_ID \ --api=API_ID --api-config=NEW_CONFIG_ID \ --location=GATEWAY_LOCATION --project=PROJECT_ID
gcloud api-gateway gateways update my-gateway \ --api=my-api --api-config=my-config-key \ --location=us-central1 --project=my-project
API キーの制限
デフォルトでは、API キーは制限されていないため、不正使用に対して脆弱です。可能な限り、API 制限を追加します。API の制限では、API キーを使用した呼び出しを許可する API を指定します。本番環境アプリケーションで使用されるすべての API キーには、API 制限を追加する必要があります。
API の制限を追加するには:
API 構成に記載されている API のタイトルを確認します。次の例では、API のタイトルは
My Example Configです。OpenAPI 2.0
# openapi.yaml swagger: '2.0' info: title: My Example Config description: Sample API on API Gateway version: 1.0.0 ...
OpenAPI 3.x
# openapi.yaml openapi: 3.0.4 info: title: My Example Config description: Sample API on API Gateway version: 1.0.0 ...
Google Cloud コンソールで、[API とサービス] > [認証情報] ページに移動します。
API に使用する API キーの名前を選択します。
API キーの詳細ページの [API の制限] セクションで、[キーを制限] をクリックします。
使用可能な API のプルダウン リストから、API キーを使用してアクセスする API を選択します。たとえば、
My Example Configを選択します。[保存] をクリックします。
制限は直ちに有効になります。