API キーは、アプリケーションまたはウェブサイトを に識別します Google Cloud。API キーの制限により、キーを使用できるのはお客様のアプリとウェブサイトのみになります。セキュリティ上の理由から、次の項目を指定する制限を追加することをおすすめします。
クライアントが、API キーの制限を満たさないリクエストを発行するために、制限付きの API キーを使用すると、リクエストは失敗します。たとえば、API キーで特定のドメインから HTTP リクエストを発行する必要があるにもかかわらず、別のドメインから HTTP リクエストを受信した場合、リクエストはエラーで拒否されます。
CreateKey メソッドを使用して API キーを作成するときに制限を追加することも、キーの作成後に UpdateKey メソッドを使用して制限を追加することもできます。このページでは、API キーに追加できる制限と、その制限を追加する方法について説明します。
クライアントの制限を追加する
クライアントの制限では、API キーの使用を許可するウェブサイト、IP アドレス、またはアプリを指定します。クライアントの制限は、API を呼び出すクライアントのタイプに基づいて追加します。次のいずれかのタイプのクライアント制限を指定できます。
browserKeyRestrictions: キーの使用が許可されている HTTP リファラー(ウェブサイト)。serverKeyRestrictions: キーの使用が許可されている呼び出し元の IP アドレス。androidKeyRestrictions: キーの使用が許可されている Android アプリ。iosKeyRestrictions: キーの使用が許可されている iOS アプリ。
ブラウザの制限を追加する
次の例は、UpdateKey メソッドを呼び出して HTTP リファラーを www.example.com に制限する方法を示しています。
curl -X PATCH \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ -H "Content-Type: application/json; charset=utf-8" \ -d '{ "restrictions" : { "browserKeyRestrictions": { "allowedReferrers": "www.example.com" } }, "etag": "ETAG" }' \ 'https://apikeys.googleapis.com/v2/projects/PROJECT_NUMBER/locations/global/keys/KEY_ID?updateMask=restrictions'
operations.get からのレスポンスに "done": true が含まれている場合、response
には制限付きの更新された Key が含まれます。
次の例は、特定の URL のリストからの HTTP リクエストのみを許可する新しい API キーを作成する方法を示しています。
curl -X POST \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ -H "Content-Type: application/json; charset=utf-8" \ -d '{ "displayName" : "API key with browser restrictions", "restrictions" : { "browserKeyRestrictions": { "allowedReferrers": ["www.example.com", "www.example-2.com"] } } }' \ 'https://apikeys.googleapis.com/v2/projects/PROJECT_NUMBER/locations/global/keys'
次の表に、シナリオとブラウザの制限の例を示します。
| シナリオ | 制限事項 |
|---|---|
| サイトの任意の URL を許可する | allowedReferers リストに 2 つの URL を設定する必要があります。
|
| 特定の URL を許可する | 正確なパスの URL を追加してください。例:www.example.com/pathwww.example.com/path/path
|
| 1 つのサブドメインまたはネイキッド ドメイン内の任意の URL を許可する | ドメイン全体を許可するには、「allowedReferers」リストに 2 つの URL を設定する必要があります。
|
サーバーの制限を追加する
API キーの使用を許可する呼び出し元(ウェブサーバーや cron ジョブなど)の IP アドレスを 1 つ以上指定できます。IP アドレスは、次のいずれかの形式で指定できます。
- IPv4(
198.51.100.1) - IPv6(
2001:db8::1) - CIDR 表記を使用したサブネット(
198.51.100.0/24、2001:db8::/64)
次の例は、allowedIps のリストを含む API キーを作成する方法を示しています。
curl -X POST \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ -H "Content-Type: application/json; charset=utf-8" \ -d '{ "displayName" : "API key with server restrictions with IPv4, IPv6 and CIDR", "restrictions" : { "serverKeyRestrictions": { "allowedIps": ["198.51.100.1","198.51.100.0/24","2001:db8::1","2001:db8::/64"] } } }' \ 'https://apikeys.googleapis.com/v2/projects/PROJECT_NUMBER/locations/global/keys'
Android の制限を追加する
API キーの使用を Android アプリのみに制限できます。API キーを作成 または更新するときに、アプリごとにパッケージ名と 20 バイトの SHA-1 フィンガープリント を指定します。
たとえば、
keytool ユーティリティ
を実行して、次のフィンガープリントを作成したとします。
Certificate fingerprint: SHA1: DA:39:A3:EE:5E:6B:4B:0D:32:55:BF:EF:95:60:18:90:AF:D8:07:09
次の例は、フィンガープリントとパッケージ名を使用して androidKeyRestrictions を含む API キーを作成する方法を示しています。
curl -X POST \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ -H "Content-Type: application/json; charset=utf-8" \ -d '{ "displayName" : "API key with Android restrictions", "restrictions" : { "androidKeyRestrictions": { "allowedApplications": [ { "sha1Fingerprint": "DA:39:A3:EE:5E:6B:4B:0D:32:55:BF:EF:95:60:18:90:AF:D8:07:09", "packageName": "com.example.my.app" } ] } } }' \ 'https://apikeys.googleapis.com/v2/projects/PROJECT_NUMBER/locations/global/keys'
iOS の制限を追加する
キーの作成時または更新時に各アプリのバンドル ID を指定することで、API キーの使用を iOS アプリのみに制限できます。次の例は、API キーの作成時に iosKeyRestrictions を設定する方法を示しています。
curl -X POST \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ -H "Content-Type: application/json; charset=utf-8" \ -d '{ "displayName" : "API key with iOS restrictions", "restrictions" : { "iosKeyRestrictions": { "allowedBundleIds": ["com.example.my.app1", "com.example.my.app2"] } } }' \ 'https://apikeys.googleapis.com/v2/projects/PROJECT_NUMBER/locations/global/keys'
API 制限を追加する
API 制限を使用すると、API キーを使用して呼び出すことのできる Google Cloud API を指定できます。 すべての API キーにクライアント制限と API 制限の両方を追加することをおすすめします。
API 制限で 1 つ以上のサービスを指定できます。次の例は、新しい API キーの使用をサービス translate.googleapis.com と datastore.googleapis.com のみに制限する方法を示しています。
curl -X POST \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ -H "Content-Type: application/json; charset=utf-8" \ -d '{ "displayName" : "API key with API restrictions", "restrictions": { "api_targets": [ { "service": "translate.googleapis.com" }, { "service" : "datastore.googleapis.com" } ] }, }' \ 'https://apikeys.googleapis.com/v2/projects/PROJECT_NUMBER/locations/global/keys'
プロジェクトで有効になっているサービスのリストを取得するには、
Google Cloud プロジェクトで、gcloud services list
コマンドを使用します。
特定のサービスでの使用に API キーを制限するだけでなく、必要に応じて各サービスのメソッドを指定して、API キーをさらに制限することもできます。次の例は、前のキーを translate.googleapis.com の許可されたメソッドのみに制限する方法を示しています。
curl -X PATCH \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ -H "Content-Type: application/json; charset=utf-8" \ -d '{ "restrictions": { "api_targets": [ { "service": "translate.googleapis.com", "methods": [ "Get*", "DetectLanguage" ] }, { "service" : "datastore.googleapis.com" } ] }, "etag": "ETAG" }' \ 'https://apikeys.googleapis.com/v2/projects/PROJECT_NUMBER/locations/global/keys/KEY_ID?updateMask=restrictions'