From 853cd558f464e225676c4b8d19c41c0887d4114b Mon Sep 17 00:00:00 2001 From: SitaLakshmi Date: Thu, 3 Nov 2022 19:43:28 +0530 Subject: [PATCH 01/18] docs(auth-samples): add API Key auth samples and tests --- auth/api-client/api_key_test.py | 83 +++++++++++++++++++++ auth/api-client/create_api_key.py | 54 ++++++++++++++ auth/api-client/delete_api_key.py | 46 ++++++++++++ auth/api-client/lookup_api_key.py | 47 ++++++++++++ auth/api-client/requirements-test.txt | 2 +- auth/api-client/requirements.txt | 6 +- auth/api-client/restrict_api_key_android.py | 68 +++++++++++++++++ auth/api-client/restrict_api_key_api.py | 65 ++++++++++++++++ auth/api-client/restrict_api_key_http.py | 64 ++++++++++++++++ auth/api-client/restrict_api_key_ios.py | 64 ++++++++++++++++ auth/api-client/restrict_api_key_server.py | 65 ++++++++++++++++ 11 files changed, 562 insertions(+), 2 deletions(-) create mode 100644 auth/api-client/api_key_test.py create mode 100644 auth/api-client/create_api_key.py create mode 100644 auth/api-client/delete_api_key.py create mode 100644 auth/api-client/lookup_api_key.py create mode 100644 auth/api-client/restrict_api_key_android.py create mode 100644 auth/api-client/restrict_api_key_api.py create mode 100644 auth/api-client/restrict_api_key_http.py create mode 100644 auth/api-client/restrict_api_key_ios.py create mode 100644 auth/api-client/restrict_api_key_server.py diff --git a/auth/api-client/api_key_test.py b/auth/api-client/api_key_test.py new file mode 100644 index 00000000000..a6de2aeb304 --- /dev/null +++ b/auth/api-client/api_key_test.py @@ -0,0 +1,83 @@ +# Copyright 2022 Google Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +import re + +import pytest +from _pytest.capture import CaptureFixture + +import authenticate_with_api_key +import create_api_key +import delete_api_key +import lookup_api_key +import restrict_api_key_android +import restrict_api_key_api +import restrict_api_key_http +import restrict_api_key_ios +import restrict_api_key_server + +from google.cloud.api_keys_v2 import Key +import google.auth.transport.requests +import os + +CREDENTIALS, PROJECT = google.auth.default() +SERVICE_ACCOUNT_FILE = os.getenv("GOOGLE_APPLICATION_CREDENTIALS") + + +@pytest.fixture(scope="module") +def api_key(): + api_key = create_api_key.create_api_key(PROJECT, "global") + yield api_key + delete_api_key.delete_api_key(PROJECT, "global", api_key.name.rsplit("/")[-1]) + + +def test_authenticate_with_api_key(api_key: Key, capsys: CaptureFixture): + authenticate_with_api_key.authenticate_with_api_key(PROJECT, api_key.key_string) + out, err = capsys.readouterr() + assert re.search("Successfully authenticated using the API key", out) + + +def test_lookup_api_key(api_key: Key, capsys: CaptureFixture): + lookup_api_key.lookup_api_key(api_key.key_string) + out, err = capsys.readouterr() + assert re.search(f"Successfully retrieved the API key name: {api_key.name}", out) + + +def test_restrict_api_key_android(api_key: Key, capsys: CaptureFixture): + restrict_api_key_android.restrict_api_key_android(PROJECT, "global", api_key.name.rsplit("/")[-1]) + out, err = capsys.readouterr() + assert re.search(f"Successfully updated the API key: {api_key.name}", out) + + +def test_restrict_api_key_api(api_key: Key, capsys: CaptureFixture): + restrict_api_key_api.restrict_api_key_api(PROJECT, "global", api_key.name.rsplit("/")[-1]) + out, err = capsys.readouterr() + assert re.search(f"Successfully updated the API key: {api_key.name}", out) + + +def test_restrict_api_key_http(api_key: Key, capsys: CaptureFixture): + restrict_api_key_http.restrict_api_key_http(PROJECT, "global", api_key.name.rsplit("/")[-1]) + out, err = capsys.readouterr() + assert re.search(f"Successfully updated the API key: {api_key.name}", out) + + +def test_restrict_api_key_ios(api_key: Key, capsys: CaptureFixture): + restrict_api_key_ios.restrict_api_key_ios(PROJECT, "global", api_key.name.rsplit("/")[-1]) + out, err = capsys.readouterr() + assert re.search(f"Successfully updated the API key: {api_key.name}", out) + + +def test_restrict_api_key_server(api_key: Key, capsys: CaptureFixture): + restrict_api_key_server.restrict_api_key_server(PROJECT, "global", api_key.name.rsplit("/")[-1]) + out, err = capsys.readouterr() + assert re.search(f"Successfully updated the API key: {api_key.name}", out) diff --git a/auth/api-client/create_api_key.py b/auth/api-client/create_api_key.py new file mode 100644 index 00000000000..4775814cceb --- /dev/null +++ b/auth/api-client/create_api_key.py @@ -0,0 +1,54 @@ +# Copyright 2022 Google Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# [START auth_cloud_create_api_key] + +from google.cloud import api_keys_v2 +from google.cloud.api_keys_v2 import Key + + +def create_api_key(project_id: str, location: str) -> Key: + """ + Create and restrict an API key. + + // TODO(Developer): + // 1. Before running this sample, + // set up ADC as described in https://cloud.google.com/docs/authentication/external/set-up-adc + // 2. Make sure you have the necessary permission to create API keys. + + Args: + project_id: Google Cloud project id. + location: Can only be "global". + """ + + # Create the API Keys client. + client = api_keys_v2.ApiKeysClient() + + key = api_keys_v2.Key() + key.display_name = "My first API key" + + # Initialize request and set arguments. + request = api_keys_v2.CreateKeyRequest() + request.parent = f"projects/{project_id}/locations/{location}" + request.key = key + + # Make the request and wait for the operation to complete. + response = client.create_key(request=request).result() + + print(f"Successfully created an API key: {response.name}") + # Use response.key_string to authenticate. + # Use response.name to restrict the key. + return response + +# [END auth_cloud_create_api_key] diff --git a/auth/api-client/delete_api_key.py b/auth/api-client/delete_api_key.py new file mode 100644 index 00000000000..1b477db3b5d --- /dev/null +++ b/auth/api-client/delete_api_key.py @@ -0,0 +1,46 @@ +# Copyright 2022 Google Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# [START auth_cloud_delete_api_key] + +from google.cloud import api_keys_v2 + + +def delete_api_key(project_id: str, location: str, key_id: str) -> None: + """ + Delete an API key. + + // TODO(Developer): + // 1. Before running this sample, + // set up ADC as described in https://cloud.google.com/docs/authentication/external/set-up-adc + // 2. Make sure you have the necessary permission to delete API keys. + + Args: + project_id: Google Cloud project id that has the API key to delete. + location: "global" + key_id: The API key id to delete. + """ + + # Create the API Keys client. + client = api_keys_v2.ApiKeysClient() + + # Initialize the delete request and set the argument. + delete_key_request = api_keys_v2.DeleteKeyRequest() + delete_key_request.name = f"projects/{project_id}/locations/{location}/keys/{key_id}" + + # Make the request and wait for the operation to complete. + result = client.delete_key(delete_key_request).result() + print(f"Successfully deleted the API key: {result.name}") + +# [END auth_cloud_delete_api_key] diff --git a/auth/api-client/lookup_api_key.py b/auth/api-client/lookup_api_key.py new file mode 100644 index 00000000000..9c2c86b0199 --- /dev/null +++ b/auth/api-client/lookup_api_key.py @@ -0,0 +1,47 @@ +# Copyright 2022 Google Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# [START auth_cloud_lookup_api_key] + +from google.cloud import api_keys_v2 + + +def lookup_api_key(api_key_string: str) -> None: + """ + Retrieve name (full path) of an API key using the API key string. + + // TODO(Developer): + // 1. Before running this sample, + // set up ADC as described in https://cloud.google.com/docs/authentication/external/set-up-adc + // 2. Make sure you have the necessary permission to view API keys. + + Args: + api_key_string: API key string to retrieve the API key name. + """ + + # Create the API Keys client. + client = api_keys_v2.ApiKeysClient() + + # Initialize the lookup request and set the API key string. + # Optionally, you can also set the etag (version). + lookup_key_request = api_keys_v2.LookupKeyRequest() + lookup_key_request.key_string = api_key_string + # lookup_key_request.etag = etag + + # Make the request and obtain the response. + lookup_key_response = client.lookup_key(lookup_key_request) + + print(f"Successfully retrieved the API key name: {lookup_key_response.name}") + +# [END auth_cloud_lookup_api_key] diff --git a/auth/api-client/requirements-test.txt b/auth/api-client/requirements-test.txt index 4bd417eba1f..fb466e5093e 100644 --- a/auth/api-client/requirements-test.txt +++ b/auth/api-client/requirements-test.txt @@ -1,2 +1,2 @@ -pytest==7.0.1 +pytest==7.1.2 mock==4.0.3 diff --git a/auth/api-client/requirements.txt b/auth/api-client/requirements.txt index e93cc1550c0..f9ff301fca2 100644 --- a/auth/api-client/requirements.txt +++ b/auth/api-client/requirements.txt @@ -1,3 +1,7 @@ google-api-python-client==2.47.0 google-auth-httplib2==0.1.0 -google-auth==2.6.2 +google-auth==2.11.0 +google-cloud-api-keys==0.2.0 +google-cloud-compute==1.5.1 +google-cloud-language==2.5.2 +google-cloud-storage==2.5.0 diff --git a/auth/api-client/restrict_api_key_android.py b/auth/api-client/restrict_api_key_android.py new file mode 100644 index 00000000000..5529f3a7dc6 --- /dev/null +++ b/auth/api-client/restrict_api_key_android.py @@ -0,0 +1,68 @@ +# Copyright 2022 Google Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# [START auth_cloud_restrict_api_key_android] + +from google.cloud import api_keys_v2 +from google.cloud.api_keys_v2 import Key + + +def restrict_api_key_android(project_id: str, location: str, key_id: str) -> Key: + """ + Restrict an API key based on android applications. Specifies the Android application that can use the key. + + // TODO(Developer): Replace the variables before running this sample. + + Args: + project_id: Google Cloud project id. + location: Can only be "global". + key_id: ID of the key to restrict. This ID is auto-created during key creation. + This is different from the key string. To obtain the key_id, + you can also use the lookup api: client.lookup_key() + """ + + # Create the API Keys client. + client = api_keys_v2.ApiKeysClient() + + # Specify the android application's package name and SHA1 fingerprint. + allowed_application = api_keys_v2.AndroidApplication() + allowed_application.package_name = "com.google.appname" + allowed_application.sha1_fingerprint = "0873D391E987982FBBD30873D391E987982FBBD3" + + # Restrict the API key usage by specifying the allowed applications. + android_key_restriction = api_keys_v2.AndroidKeyRestrictions() + android_key_restriction.allowed_applications = [allowed_application] + + # Set the restriction(s). + # For more information on API key restriction, see: https://cloud.google.com/docs/authentication/api-keys + restrictions = api_keys_v2.Restrictions() + restrictions.android_key_restrictions = android_key_restriction + + key = api_keys_v2.Key() + key.name = f"projects/{project_id}/locations/{location}/keys/{key_id}" + key.restrictions = restrictions + + # Initialize request and set arguments. + request = api_keys_v2.UpdateKeyRequest() + request.key = key + request.update_mask = "restrictions" + + # Make the request and wait for the operation to complete. + response = client.update_key(request=request).result() + + print(f"Successfully updated the API key: {response.name}") + # Use response.key_string to authenticate. + return response + +# [END auth_cloud_restrict_api_key_android] diff --git a/auth/api-client/restrict_api_key_api.py b/auth/api-client/restrict_api_key_api.py new file mode 100644 index 00000000000..bfe6214e711 --- /dev/null +++ b/auth/api-client/restrict_api_key_api.py @@ -0,0 +1,65 @@ +# Copyright 2022 Google Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# [START auth_cloud_restrict_api_key_api] + +from google.cloud import api_keys_v2 +from google.cloud.api_keys_v2 import Key + + +def restrict_api_key_api(project_id: str, location: str, key_id: str) -> Key: + """ + Restrict an API key. Restrictions specify which APIs can be called using the API key. + + // TODO(Developer): Replace the variables before running the sample. + + Args: + project_id: Google Cloud project id. + location: Can only be "global". + key_id: ID of the key to restrict. This ID is auto-created during key creation. + This is different from the key string. To obtain the key_id, + you can also use the lookup api: client.lookup_key() + """ + + # Create the API Keys client. + client = api_keys_v2.ApiKeysClient() + + # Restrict the API key usage by specifying the target service and methods. + # The API key can only be used to authenticate the specified methods in the service. + api_target = api_keys_v2.ApiTarget() + api_target.service = "translate.googleapis.com" + api_target.methods = ["transate.googleapis.com.TranslateText"] + + # Set the API restriction(s). + # For more information on API key restriction, see: https://cloud.google.com/docs/authentication/api-keys + restrictions = api_keys_v2.Restrictions() + restrictions.api_targets = [api_target] + + key = api_keys_v2.Key() + key.name = f"projects/{project_id}/locations/{location}/keys/{key_id}" + key.restrictions = restrictions + + # Initialize request and set arguments. + request = api_keys_v2.UpdateKeyRequest() + request.key = key + request.update_mask = "restrictions" + + # Make the request and wait for the operation to complete. + response = client.update_key(request=request).result() + + print(f"Successfully updated the API key: {response.name}") + # Use response.key_string to authenticate. + return response + +# [END auth_cloud_restrict_api_key_api] diff --git a/auth/api-client/restrict_api_key_http.py b/auth/api-client/restrict_api_key_http.py new file mode 100644 index 00000000000..8dda740261c --- /dev/null +++ b/auth/api-client/restrict_api_key_http.py @@ -0,0 +1,64 @@ +# Copyright 2022 Google Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# [START auth_cloud_restrict_api_key_http] + +from google.cloud import api_keys_v2 +from google.cloud.api_keys_v2 import Key + + +def restrict_api_key_http(project_id: str, location: str, key_id: str) -> Key: + """ + Restrict an API key. To restrict the websites that can use your API key, + you add one or more HTTP referrer restrictions. + + // TODO(Developer): Replace the variables before running this sample. + + Args: + project_id: Google Cloud project id. + location: Can only be "global". + key_id: ID of the key to restrict. This ID is auto-created during key creation. + This is different from the key string. To obtain the key_id, + you can also use the lookup api: client.lookup_key() + """ + + # Create the API Keys client. + client = api_keys_v2.ApiKeysClient() + + # Restrict the API key usage to specific websites by adding them to the list of allowed_referrers. + browser_key_restrictions = api_keys_v2.BrowserKeyRestrictions() + browser_key_restrictions.allowed_referrers = ["www.example.com/*"] + + # Set the API restriction. + # For more information on API key restriction, see: https://cloud.google.com/docs/authentication/api-keys + restrictions = api_keys_v2.Restrictions() + restrictions.browser_key_restrictions = browser_key_restrictions + + key = api_keys_v2.Key() + key.name = f"projects/{project_id}/locations/{location}/keys/{key_id}" + key.restrictions = restrictions + + # Initialize request and set arguments. + request = api_keys_v2.UpdateKeyRequest() + request.key = key + request.update_mask = "restrictions" + + # Make the request and wait for the operation to complete. + response = client.update_key(request=request).result() + + print(f"Successfully updated the API key: {response.name}") + # Use response.key_string to authenticate. + return response + +# [END auth_cloud_restrict_api_key_http] diff --git a/auth/api-client/restrict_api_key_ios.py b/auth/api-client/restrict_api_key_ios.py new file mode 100644 index 00000000000..638705841dc --- /dev/null +++ b/auth/api-client/restrict_api_key_ios.py @@ -0,0 +1,64 @@ +# Copyright 2022 Google Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# [START auth_cloud_restrict_api_key_ios] + +from google.cloud import api_keys_v2 +from google.cloud.api_keys_v2 import Key + + +def restrict_api_key_ios(project_id: str, location: str, key_id: str) -> Key: + """ + Restrict an API key. You can restrict usage of an API key to specific iOS apps + by providing the bundle ID of each app. + + // TODO(Developer): Replace the variables before running this sample. + + Args: + project_id: Google Cloud project id. + location: Can only be "global". + key_id: ID of the key to restrict. This ID is auto-created during key creation. + This is different from the key string. To obtain the key_id, + you can also use the lookup api: client.lookup_key() + """ + + # Create the API Keys client. + client = api_keys_v2.ApiKeysClient() + + # Restrict the API key usage by specifying the bundle ID(s) of iOS app(s) that can use the key. + ios_key_restrictions = api_keys_v2.IosKeyRestrictions() + ios_key_restrictions.allowed_bundle_ids = ["com.google.gmail", "com.google.drive"] + + # Set the API restriction. + # For more information on API key restriction, see: https://cloud.google.com/docs/authentication/api-keys + restrictions = api_keys_v2.Restrictions() + restrictions.ios_key_restrictions = ios_key_restrictions + + key = api_keys_v2.Key() + key.name = f"projects/{project_id}/locations/{location}/keys/{key_id}" + key.restrictions = restrictions + + # Initialize request and set arguments. + request = api_keys_v2.UpdateKeyRequest() + request.key = key + request.update_mask = "restrictions" + + # Make the request and wait for the operation to complete. + response = client.update_key(request=request).result() + + print(f"Successfully updated the API key: {response.name}") + # Use response.key_string to authenticate. + return response + +# [END auth_cloud_restrict_api_key_ios] diff --git a/auth/api-client/restrict_api_key_server.py b/auth/api-client/restrict_api_key_server.py new file mode 100644 index 00000000000..ffc0f66a23f --- /dev/null +++ b/auth/api-client/restrict_api_key_server.py @@ -0,0 +1,65 @@ +# Copyright 2022 Google Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# [START auth_cloud_restrict_api_key_server] + +from google.cloud import api_keys_v2 +from google.cloud.api_keys_v2 import Key + + +def restrict_api_key_server(project_id: str, location: str, key_id: str) -> Key: + """ + Restrict the API key based on IP addresses. You can specify one or more IP addresses of the callers, + for example web servers or cron jobs, that are allowed to use your API key. + + // TODO(Developer): Replace the variables before running this sample. + + Args: + project_id: Google Cloud project id. + location: Can only be "global". + key_id: ID of the key to restrict. This ID is auto-created during key creation. + This is different from the key string. To obtain the key_id, + you can also use the lookup api: client.lookup_key() + """ + + # Create the API Keys client. + client = api_keys_v2.ApiKeysClient() + + # Restrict the API key usage by specifying the IP addresses. + # You can specify the IP addresses in IPv4 or IPv6 or a subnet using CIDR notation. + server_key_restrictions = api_keys_v2.ServerKeyRestrictions() + server_key_restrictions.allowed_ips = ["198.51.100.0/24", "2000:db8::/64"] + + # Set the API restriction. + # For more information on API key restriction, see: https://cloud.google.com/docs/authentication/api-keys + restrictions = api_keys_v2.Restrictions() + restrictions.server_key_restrictions = server_key_restrictions + + key = api_keys_v2.Key() + key.name = f"projects/{project_id}/locations/{location}/keys/{key_id}" + key.restrictions = restrictions + + # Initialize request and set arguments. + request = api_keys_v2.UpdateKeyRequest() + request.key = key + request.update_mask = "restrictions" + + # Make the request and wait for the operation to complete. + response = client.update_key(request=request).result() + + print(f"Successfully updated the API key: {response.name}") + # Use response.key_string to authenticate. + return response + +# [END auth_cloud_restrict_api_key_server] From 31f2280f82d805b14ad8f681374da32ff39993d8 Mon Sep 17 00:00:00 2001 From: SitaLakshmi Date: Thu, 3 Nov 2022 20:41:31 +0530 Subject: [PATCH 02/18] updated copyright --- auth/api-client/api_key_test.py | 2 +- auth/api-client/create_api_key.py | 2 +- auth/api-client/delete_api_key.py | 2 +- auth/api-client/lookup_api_key.py | 2 +- auth/api-client/restrict_api_key_android.py | 2 +- auth/api-client/restrict_api_key_api.py | 2 +- auth/api-client/restrict_api_key_http.py | 2 +- auth/api-client/restrict_api_key_ios.py | 2 +- auth/api-client/restrict_api_key_server.py | 2 +- 9 files changed, 9 insertions(+), 9 deletions(-) diff --git a/auth/api-client/api_key_test.py b/auth/api-client/api_key_test.py index a6de2aeb304..55c519ec81a 100644 --- a/auth/api-client/api_key_test.py +++ b/auth/api-client/api_key_test.py @@ -1,4 +1,4 @@ -# Copyright 2022 Google Inc. +# Copyright 2022 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. diff --git a/auth/api-client/create_api_key.py b/auth/api-client/create_api_key.py index 4775814cceb..19d44d5f1cd 100644 --- a/auth/api-client/create_api_key.py +++ b/auth/api-client/create_api_key.py @@ -1,4 +1,4 @@ -# Copyright 2022 Google Inc. +# Copyright 2022 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. diff --git a/auth/api-client/delete_api_key.py b/auth/api-client/delete_api_key.py index 1b477db3b5d..1b1107ab5fc 100644 --- a/auth/api-client/delete_api_key.py +++ b/auth/api-client/delete_api_key.py @@ -1,4 +1,4 @@ -# Copyright 2022 Google Inc. +# Copyright 2022 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. diff --git a/auth/api-client/lookup_api_key.py b/auth/api-client/lookup_api_key.py index 9c2c86b0199..d338b9a854a 100644 --- a/auth/api-client/lookup_api_key.py +++ b/auth/api-client/lookup_api_key.py @@ -1,4 +1,4 @@ -# Copyright 2022 Google Inc. +# Copyright 2022 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. diff --git a/auth/api-client/restrict_api_key_android.py b/auth/api-client/restrict_api_key_android.py index 5529f3a7dc6..219bbc90a94 100644 --- a/auth/api-client/restrict_api_key_android.py +++ b/auth/api-client/restrict_api_key_android.py @@ -1,4 +1,4 @@ -# Copyright 2022 Google Inc. +# Copyright 2022 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. diff --git a/auth/api-client/restrict_api_key_api.py b/auth/api-client/restrict_api_key_api.py index bfe6214e711..ef01b213ada 100644 --- a/auth/api-client/restrict_api_key_api.py +++ b/auth/api-client/restrict_api_key_api.py @@ -1,4 +1,4 @@ -# Copyright 2022 Google Inc. +# Copyright 2022 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. diff --git a/auth/api-client/restrict_api_key_http.py b/auth/api-client/restrict_api_key_http.py index 8dda740261c..af8205c95bb 100644 --- a/auth/api-client/restrict_api_key_http.py +++ b/auth/api-client/restrict_api_key_http.py @@ -1,4 +1,4 @@ -# Copyright 2022 Google Inc. +# Copyright 2022 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. diff --git a/auth/api-client/restrict_api_key_ios.py b/auth/api-client/restrict_api_key_ios.py index 638705841dc..125a09bcc7d 100644 --- a/auth/api-client/restrict_api_key_ios.py +++ b/auth/api-client/restrict_api_key_ios.py @@ -1,4 +1,4 @@ -# Copyright 2022 Google Inc. +# Copyright 2022 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. diff --git a/auth/api-client/restrict_api_key_server.py b/auth/api-client/restrict_api_key_server.py index ffc0f66a23f..dab387d5790 100644 --- a/auth/api-client/restrict_api_key_server.py +++ b/auth/api-client/restrict_api_key_server.py @@ -1,4 +1,4 @@ -# Copyright 2022 Google Inc. +# Copyright 2022 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. From 201da8ba307c79486f78819a668c16964386a804 Mon Sep 17 00:00:00 2001 From: SitaLakshmi Date: Thu, 3 Nov 2022 20:48:32 +0530 Subject: [PATCH 03/18] added authenticating with api key --- auth/api-client/authenticate_with_api_key.py | 49 ++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 auth/api-client/authenticate_with_api_key.py diff --git a/auth/api-client/authenticate_with_api_key.py b/auth/api-client/authenticate_with_api_key.py new file mode 100644 index 00000000000..66182689086 --- /dev/null +++ b/auth/api-client/authenticate_with_api_key.py @@ -0,0 +1,49 @@ +# Copyright 2022 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# [START auth_cloud_authenticate_api_key] + +from google.cloud import language_v1 + + +def authenticate_with_api_key(quota_project_id: str, api_key_string: str) -> None: + """ + Demonstrate authenticating with an API key for Google Language service. + + // TODO(Developer): Replace these variables before running the sample. + + Args: + quota_project_id: Google Cloud project id that should be used for quota and billing purposes. + api_key_string: The API key to authenticate to the service. + """ + + # Initialize the Language Service client and set the API key and the quota project id. + client = language_v1.LanguageServiceClient(client_options={"api_key": api_key_string, + "quota_project_id": quota_project_id}) + + text = "Hello, world!" + document = language_v1.Document( + content=text, type_=language_v1.Document.Type.PLAIN_TEXT + ) + + # Make a request to analyze the sentiment of the text. + sentiment = client.analyze_sentiment( + request={"document": document} + ).document_sentiment + + print("Text: {}".format(text)) + print("Sentiment: {}, {}".format(sentiment.score, sentiment.magnitude)) + print("Successfully authenticated using the API key") + +# [END auth_cloud_authenticate_api_key] From 67081d22f3cf56de0abf2dc35a70f07b1ff21509 Mon Sep 17 00:00:00 2001 From: SitaLakshmi Date: Thu, 3 Nov 2022 22:27:51 +0530 Subject: [PATCH 04/18] lint fix --- auth/api-client/api_key_test.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/auth/api-client/api_key_test.py b/auth/api-client/api_key_test.py index 55c519ec81a..57496488d67 100644 --- a/auth/api-client/api_key_test.py +++ b/auth/api-client/api_key_test.py @@ -12,9 +12,12 @@ # See the License for the specific language governing permissions and # limitations under the License. import re +import os + +import google.auth.transport.requests -import pytest from _pytest.capture import CaptureFixture +import pytest import authenticate_with_api_key import create_api_key @@ -27,8 +30,6 @@ import restrict_api_key_server from google.cloud.api_keys_v2 import Key -import google.auth.transport.requests -import os CREDENTIALS, PROJECT = google.auth.default() SERVICE_ACCOUNT_FILE = os.getenv("GOOGLE_APPLICATION_CREDENTIALS") From 9b91429530c438b1d87a8e1199a63f92546a3d77 Mon Sep 17 00:00:00 2001 From: SitaLakshmi Date: Thu, 3 Nov 2022 22:34:26 +0530 Subject: [PATCH 05/18] lint fix --- auth/api-client/api_key_test.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/auth/api-client/api_key_test.py b/auth/api-client/api_key_test.py index 57496488d67..b9632e302a7 100644 --- a/auth/api-client/api_key_test.py +++ b/auth/api-client/api_key_test.py @@ -11,14 +11,16 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. -import re import os - -import google.auth.transport.requests +import re from _pytest.capture import CaptureFixture import pytest +import google.auth.transport.requests + +from google.cloud.api_keys_v2 import Key + import authenticate_with_api_key import create_api_key import delete_api_key @@ -29,8 +31,6 @@ import restrict_api_key_ios import restrict_api_key_server -from google.cloud.api_keys_v2 import Key - CREDENTIALS, PROJECT = google.auth.default() SERVICE_ACCOUNT_FILE = os.getenv("GOOGLE_APPLICATION_CREDENTIALS") From 3d6fd3d4021b97906e7ec254080da36518e4c598 Mon Sep 17 00:00:00 2001 From: SitaLakshmi Date: Thu, 3 Nov 2022 22:56:17 +0530 Subject: [PATCH 06/18] lint fix --- auth/api-client/api_key_test.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/auth/api-client/api_key_test.py b/auth/api-client/api_key_test.py index b9632e302a7..ed3bcfc1cda 100644 --- a/auth/api-client/api_key_test.py +++ b/auth/api-client/api_key_test.py @@ -15,9 +15,8 @@ import re from _pytest.capture import CaptureFixture -import pytest - import google.auth.transport.requests +import pytest from google.cloud.api_keys_v2 import Key From 400923cbaf626b370988d9a83f116351363ae95c Mon Sep 17 00:00:00 2001 From: SitaLakshmi Date: Thu, 3 Nov 2022 23:02:25 +0530 Subject: [PATCH 07/18] lint fix again! --- auth/api-client/api_key_test.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/auth/api-client/api_key_test.py b/auth/api-client/api_key_test.py index ed3bcfc1cda..e23e2df8fa8 100644 --- a/auth/api-client/api_key_test.py +++ b/auth/api-client/api_key_test.py @@ -16,9 +16,8 @@ from _pytest.capture import CaptureFixture import google.auth.transport.requests -import pytest - from google.cloud.api_keys_v2 import Key +import pytest import authenticate_with_api_key import create_api_key From b8f0610cae7fb7eaa18915a43dc1f2b9ed49e9df Mon Sep 17 00:00:00 2001 From: Sita Lakshmi Sangameswaran Date: Tue, 15 Nov 2022 23:07:18 +0530 Subject: [PATCH 08/18] Apply suggestions from code review Co-authored-by: Dan Lee <71398022+dandhlee@users.noreply.github.com> --- auth/api-client/authenticate_with_api_key.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/auth/api-client/authenticate_with_api_key.py b/auth/api-client/authenticate_with_api_key.py index 66182689086..b8ed866af8e 100644 --- a/auth/api-client/authenticate_with_api_key.py +++ b/auth/api-client/authenticate_with_api_key.py @@ -19,7 +19,7 @@ def authenticate_with_api_key(quota_project_id: str, api_key_string: str) -> None: """ - Demonstrate authenticating with an API key for Google Language service. + Authenticates with an API key for Google Language service. // TODO(Developer): Replace these variables before running the sample. From e59276dde3f73d99d23ec91432028dc8ad7feabf Mon Sep 17 00:00:00 2001 From: Sita Lakshmi Sangameswaran Date: Tue, 15 Nov 2022 23:11:06 +0530 Subject: [PATCH 09/18] Apply suggestions from code review Co-authored-by: Dan Lee <71398022+dandhlee@users.noreply.github.com> --- auth/api-client/authenticate_with_api_key.py | 6 +++--- auth/api-client/create_api_key.py | 10 +++++----- auth/api-client/delete_api_key.py | 10 +++++----- auth/api-client/lookup_api_key.py | 10 +++++----- auth/api-client/restrict_api_key_android.py | 9 ++++++--- auth/api-client/restrict_api_key_api.py | 7 ++++--- auth/api-client/restrict_api_key_http.py | 7 ++++--- auth/api-client/restrict_api_key_ios.py | 7 ++++--- auth/api-client/restrict_api_key_server.py | 7 ++++--- 9 files changed, 40 insertions(+), 33 deletions(-) diff --git a/auth/api-client/authenticate_with_api_key.py b/auth/api-client/authenticate_with_api_key.py index b8ed866af8e..b33f16c4be7 100644 --- a/auth/api-client/authenticate_with_api_key.py +++ b/auth/api-client/authenticate_with_api_key.py @@ -21,7 +21,7 @@ def authenticate_with_api_key(quota_project_id: str, api_key_string: str) -> Non """ Authenticates with an API key for Google Language service. - // TODO(Developer): Replace these variables before running the sample. + TODO(Developer): Replace these variables before running the sample. Args: quota_project_id: Google Cloud project id that should be used for quota and billing purposes. @@ -42,8 +42,8 @@ def authenticate_with_api_key(quota_project_id: str, api_key_string: str) -> Non request={"document": document} ).document_sentiment - print("Text: {}".format(text)) - print("Sentiment: {}, {}".format(sentiment.score, sentiment.magnitude)) + print(f"Text: {text}") + print("Sentiment: {sentiment.score}, {sentiment.magnitude}") print("Successfully authenticated using the API key") # [END auth_cloud_authenticate_api_key] diff --git a/auth/api-client/create_api_key.py b/auth/api-client/create_api_key.py index 19d44d5f1cd..a829d0f198a 100644 --- a/auth/api-client/create_api_key.py +++ b/auth/api-client/create_api_key.py @@ -20,12 +20,12 @@ def create_api_key(project_id: str, location: str) -> Key: """ - Create and restrict an API key. + Creates and restrict an API key. - // TODO(Developer): - // 1. Before running this sample, - // set up ADC as described in https://cloud.google.com/docs/authentication/external/set-up-adc - // 2. Make sure you have the necessary permission to create API keys. + TODO(Developer): + 1. Before running this sample, + set up ADC as described in https://cloud.google.com/docs/authentication/external/set-up-adc + 2. Make sure you have the necessary permission to create API keys. Args: project_id: Google Cloud project id. diff --git a/auth/api-client/delete_api_key.py b/auth/api-client/delete_api_key.py index 1b1107ab5fc..b288da12f02 100644 --- a/auth/api-client/delete_api_key.py +++ b/auth/api-client/delete_api_key.py @@ -19,12 +19,12 @@ def delete_api_key(project_id: str, location: str, key_id: str) -> None: """ - Delete an API key. + Deletes an API key. - // TODO(Developer): - // 1. Before running this sample, - // set up ADC as described in https://cloud.google.com/docs/authentication/external/set-up-adc - // 2. Make sure you have the necessary permission to delete API keys. + TODO(Developer): + 1. Before running this sample, + set up ADC as described in https://cloud.google.com/docs/authentication/external/set-up-adc + 2. Make sure you have the necessary permission to delete API keys. Args: project_id: Google Cloud project id that has the API key to delete. diff --git a/auth/api-client/lookup_api_key.py b/auth/api-client/lookup_api_key.py index d338b9a854a..69a632ccc1f 100644 --- a/auth/api-client/lookup_api_key.py +++ b/auth/api-client/lookup_api_key.py @@ -19,12 +19,12 @@ def lookup_api_key(api_key_string: str) -> None: """ - Retrieve name (full path) of an API key using the API key string. + Retrieves name (full path) of an API key using the API key string. - // TODO(Developer): - // 1. Before running this sample, - // set up ADC as described in https://cloud.google.com/docs/authentication/external/set-up-adc - // 2. Make sure you have the necessary permission to view API keys. + TODO(Developer): + 1. Before running this sample, + set up ADC as described in https://cloud.google.com/docs/authentication/external/set-up-adc + 2. Make sure you have the necessary permission to view API keys. Args: api_key_string: API key string to retrieve the API key name. diff --git a/auth/api-client/restrict_api_key_android.py b/auth/api-client/restrict_api_key_android.py index 219bbc90a94..5997351a4f9 100644 --- a/auth/api-client/restrict_api_key_android.py +++ b/auth/api-client/restrict_api_key_android.py @@ -20,9 +20,11 @@ def restrict_api_key_android(project_id: str, location: str, key_id: str) -> Key: """ - Restrict an API key based on android applications. Specifies the Android application that can use the key. + Restricts an API key based on android applications. + + Specifies the Android application that can use the key. - // TODO(Developer): Replace the variables before running this sample. + TODO(Developer): Replace the variables before running this sample. Args: project_id: Google Cloud project id. @@ -45,7 +47,8 @@ def restrict_api_key_android(project_id: str, location: str, key_id: str) -> Key android_key_restriction.allowed_applications = [allowed_application] # Set the restriction(s). - # For more information on API key restriction, see: https://cloud.google.com/docs/authentication/api-keys + # For more information on API key restriction, see: + # https://cloud.google.com/docs/authentication/api-keys restrictions = api_keys_v2.Restrictions() restrictions.android_key_restrictions = android_key_restriction diff --git a/auth/api-client/restrict_api_key_api.py b/auth/api-client/restrict_api_key_api.py index ef01b213ada..f93df260f80 100644 --- a/auth/api-client/restrict_api_key_api.py +++ b/auth/api-client/restrict_api_key_api.py @@ -20,9 +20,9 @@ def restrict_api_key_api(project_id: str, location: str, key_id: str) -> Key: """ - Restrict an API key. Restrictions specify which APIs can be called using the API key. + Restricts an API key. Restrictions specify which APIs can be called using the API key. - // TODO(Developer): Replace the variables before running the sample. + TODO(Developer): Replace the variables before running the sample. Args: project_id: Google Cloud project id. @@ -42,7 +42,8 @@ def restrict_api_key_api(project_id: str, location: str, key_id: str) -> Key: api_target.methods = ["transate.googleapis.com.TranslateText"] # Set the API restriction(s). - # For more information on API key restriction, see: https://cloud.google.com/docs/authentication/api-keys + # For more information on API key restriction, see: + # https://cloud.google.com/docs/authentication/api-keys restrictions = api_keys_v2.Restrictions() restrictions.api_targets = [api_target] diff --git a/auth/api-client/restrict_api_key_http.py b/auth/api-client/restrict_api_key_http.py index af8205c95bb..42618ee74fa 100644 --- a/auth/api-client/restrict_api_key_http.py +++ b/auth/api-client/restrict_api_key_http.py @@ -20,10 +20,10 @@ def restrict_api_key_http(project_id: str, location: str, key_id: str) -> Key: """ - Restrict an API key. To restrict the websites that can use your API key, + Restricts an API key. To restrict the websites that can use your API key, you add one or more HTTP referrer restrictions. - // TODO(Developer): Replace the variables before running this sample. + TODO(Developer): Replace the variables before running this sample. Args: project_id: Google Cloud project id. @@ -41,7 +41,8 @@ def restrict_api_key_http(project_id: str, location: str, key_id: str) -> Key: browser_key_restrictions.allowed_referrers = ["www.example.com/*"] # Set the API restriction. - # For more information on API key restriction, see: https://cloud.google.com/docs/authentication/api-keys + # For more information on API key restriction, see: + # https://cloud.google.com/docs/authentication/api-keys restrictions = api_keys_v2.Restrictions() restrictions.browser_key_restrictions = browser_key_restrictions diff --git a/auth/api-client/restrict_api_key_ios.py b/auth/api-client/restrict_api_key_ios.py index 125a09bcc7d..d97d0876d4a 100644 --- a/auth/api-client/restrict_api_key_ios.py +++ b/auth/api-client/restrict_api_key_ios.py @@ -20,10 +20,10 @@ def restrict_api_key_ios(project_id: str, location: str, key_id: str) -> Key: """ - Restrict an API key. You can restrict usage of an API key to specific iOS apps + Restricts an API key. You can restrict usage of an API key to specific iOS apps by providing the bundle ID of each app. - // TODO(Developer): Replace the variables before running this sample. + TODO(Developer): Replace the variables before running this sample. Args: project_id: Google Cloud project id. @@ -41,7 +41,8 @@ def restrict_api_key_ios(project_id: str, location: str, key_id: str) -> Key: ios_key_restrictions.allowed_bundle_ids = ["com.google.gmail", "com.google.drive"] # Set the API restriction. - # For more information on API key restriction, see: https://cloud.google.com/docs/authentication/api-keys + # For more information on API key restriction, see: + # https://cloud.google.com/docs/authentication/api-keys restrictions = api_keys_v2.Restrictions() restrictions.ios_key_restrictions = ios_key_restrictions diff --git a/auth/api-client/restrict_api_key_server.py b/auth/api-client/restrict_api_key_server.py index dab387d5790..1a1aa683a9d 100644 --- a/auth/api-client/restrict_api_key_server.py +++ b/auth/api-client/restrict_api_key_server.py @@ -20,10 +20,10 @@ def restrict_api_key_server(project_id: str, location: str, key_id: str) -> Key: """ - Restrict the API key based on IP addresses. You can specify one or more IP addresses of the callers, + Restricts the API key based on IP addresses. You can specify one or more IP addresses of the callers, for example web servers or cron jobs, that are allowed to use your API key. - // TODO(Developer): Replace the variables before running this sample. + TODO(Developer): Replace the variables before running this sample. Args: project_id: Google Cloud project id. @@ -42,7 +42,8 @@ def restrict_api_key_server(project_id: str, location: str, key_id: str) -> Key: server_key_restrictions.allowed_ips = ["198.51.100.0/24", "2000:db8::/64"] # Set the API restriction. - # For more information on API key restriction, see: https://cloud.google.com/docs/authentication/api-keys + # For more information on API key restriction, see: + # https://cloud.google.com/docs/authentication/api-keys restrictions = api_keys_v2.Restrictions() restrictions.server_key_restrictions = server_key_restrictions From 0059063160e08c51d9ece4e9baee22c577d95bf2 Mon Sep 17 00:00:00 2001 From: Sita Lakshmi Sangameswaran Date: Wed, 16 Nov 2022 01:13:27 +0530 Subject: [PATCH 10/18] added return docstring and removed location parameter --- auth/api-client/authenticate_with_api_key.py | 2 +- auth/api-client/create_api_key.py | 12 +++++++----- auth/api-client/delete_api_key.py | 5 ++--- auth/api-client/restrict_api_key_android.py | 12 +++++++----- auth/api-client/restrict_api_key_api.py | 8 +++++--- auth/api-client/restrict_api_key_http.py | 8 +++++--- auth/api-client/restrict_api_key_ios.py | 8 +++++--- auth/api-client/restrict_api_key_server.py | 8 +++++--- 8 files changed, 37 insertions(+), 26 deletions(-) diff --git a/auth/api-client/authenticate_with_api_key.py b/auth/api-client/authenticate_with_api_key.py index b33f16c4be7..1e2b32ca145 100644 --- a/auth/api-client/authenticate_with_api_key.py +++ b/auth/api-client/authenticate_with_api_key.py @@ -43,7 +43,7 @@ def authenticate_with_api_key(quota_project_id: str, api_key_string: str) -> Non ).document_sentiment print(f"Text: {text}") - print("Sentiment: {sentiment.score}, {sentiment.magnitude}") + print(f"Sentiment: {sentiment.score}, {sentiment.magnitude}") print("Successfully authenticated using the API key") # [END auth_cloud_authenticate_api_key] diff --git a/auth/api-client/create_api_key.py b/auth/api-client/create_api_key.py index a829d0f198a..39f2c3bbfbf 100644 --- a/auth/api-client/create_api_key.py +++ b/auth/api-client/create_api_key.py @@ -18,7 +18,7 @@ from google.cloud.api_keys_v2 import Key -def create_api_key(project_id: str, location: str) -> Key: +def create_api_key(project_id: str) -> Key: """ Creates and restrict an API key. @@ -29,7 +29,9 @@ def create_api_key(project_id: str, location: str) -> Key: Args: project_id: Google Cloud project id. - location: Can only be "global". + + Returns: + response: Returns the created API Key. """ # Create the API Keys client. @@ -40,15 +42,15 @@ def create_api_key(project_id: str, location: str) -> Key: # Initialize request and set arguments. request = api_keys_v2.CreateKeyRequest() - request.parent = f"projects/{project_id}/locations/{location}" + request.parent = f"projects/{project_id}/locations/global" request.key = key # Make the request and wait for the operation to complete. response = client.create_key(request=request).result() print(f"Successfully created an API key: {response.name}") - # Use response.key_string to authenticate. - # Use response.name to restrict the key. + # For authenticating with the API key, use the value in "response.key_string". + # To restrict the usage of this API key, use the value in "response.name". return response # [END auth_cloud_create_api_key] diff --git a/auth/api-client/delete_api_key.py b/auth/api-client/delete_api_key.py index b288da12f02..fe365729257 100644 --- a/auth/api-client/delete_api_key.py +++ b/auth/api-client/delete_api_key.py @@ -17,7 +17,7 @@ from google.cloud import api_keys_v2 -def delete_api_key(project_id: str, location: str, key_id: str) -> None: +def delete_api_key(project_id: str, key_id: str) -> None: """ Deletes an API key. @@ -28,7 +28,6 @@ def delete_api_key(project_id: str, location: str, key_id: str) -> None: Args: project_id: Google Cloud project id that has the API key to delete. - location: "global" key_id: The API key id to delete. """ @@ -37,7 +36,7 @@ def delete_api_key(project_id: str, location: str, key_id: str) -> None: # Initialize the delete request and set the argument. delete_key_request = api_keys_v2.DeleteKeyRequest() - delete_key_request.name = f"projects/{project_id}/locations/{location}/keys/{key_id}" + delete_key_request.name = f"projects/{project_id}/locations/global/keys/{key_id}" # Make the request and wait for the operation to complete. result = client.delete_key(delete_key_request).result() diff --git a/auth/api-client/restrict_api_key_android.py b/auth/api-client/restrict_api_key_android.py index 5997351a4f9..ff0cf7d15ce 100644 --- a/auth/api-client/restrict_api_key_android.py +++ b/auth/api-client/restrict_api_key_android.py @@ -18,22 +18,24 @@ from google.cloud.api_keys_v2 import Key -def restrict_api_key_android(project_id: str, location: str, key_id: str) -> Key: +def restrict_api_key_android(project_id: str, key_id: str) -> Key: """ Restricts an API key based on android applications. - + Specifies the Android application that can use the key. TODO(Developer): Replace the variables before running this sample. Args: project_id: Google Cloud project id. - location: Can only be "global". key_id: ID of the key to restrict. This ID is auto-created during key creation. This is different from the key string. To obtain the key_id, you can also use the lookup api: client.lookup_key() + + Returns: + response: Returns the updated API Key. """ - + # Create the API Keys client. client = api_keys_v2.ApiKeysClient() @@ -53,7 +55,7 @@ def restrict_api_key_android(project_id: str, location: str, key_id: str) -> Key restrictions.android_key_restrictions = android_key_restriction key = api_keys_v2.Key() - key.name = f"projects/{project_id}/locations/{location}/keys/{key_id}" + key.name = f"projects/{project_id}/locations/global/keys/{key_id}" key.restrictions = restrictions # Initialize request and set arguments. diff --git a/auth/api-client/restrict_api_key_api.py b/auth/api-client/restrict_api_key_api.py index f93df260f80..f5d903fcf87 100644 --- a/auth/api-client/restrict_api_key_api.py +++ b/auth/api-client/restrict_api_key_api.py @@ -18,7 +18,7 @@ from google.cloud.api_keys_v2 import Key -def restrict_api_key_api(project_id: str, location: str, key_id: str) -> Key: +def restrict_api_key_api(project_id: str, key_id: str) -> Key: """ Restricts an API key. Restrictions specify which APIs can be called using the API key. @@ -26,10 +26,12 @@ def restrict_api_key_api(project_id: str, location: str, key_id: str) -> Key: Args: project_id: Google Cloud project id. - location: Can only be "global". key_id: ID of the key to restrict. This ID is auto-created during key creation. This is different from the key string. To obtain the key_id, you can also use the lookup api: client.lookup_key() + + Returns: + response: Returns the updated API Key. """ # Create the API Keys client. @@ -48,7 +50,7 @@ def restrict_api_key_api(project_id: str, location: str, key_id: str) -> Key: restrictions.api_targets = [api_target] key = api_keys_v2.Key() - key.name = f"projects/{project_id}/locations/{location}/keys/{key_id}" + key.name = f"projects/{project_id}/locations/global/keys/{key_id}" key.restrictions = restrictions # Initialize request and set arguments. diff --git a/auth/api-client/restrict_api_key_http.py b/auth/api-client/restrict_api_key_http.py index 42618ee74fa..9c071001d90 100644 --- a/auth/api-client/restrict_api_key_http.py +++ b/auth/api-client/restrict_api_key_http.py @@ -18,7 +18,7 @@ from google.cloud.api_keys_v2 import Key -def restrict_api_key_http(project_id: str, location: str, key_id: str) -> Key: +def restrict_api_key_http(project_id: str, key_id: str) -> Key: """ Restricts an API key. To restrict the websites that can use your API key, you add one or more HTTP referrer restrictions. @@ -27,10 +27,12 @@ def restrict_api_key_http(project_id: str, location: str, key_id: str) -> Key: Args: project_id: Google Cloud project id. - location: Can only be "global". key_id: ID of the key to restrict. This ID is auto-created during key creation. This is different from the key string. To obtain the key_id, you can also use the lookup api: client.lookup_key() + + Returns: + response: Returns the updated API Key. """ # Create the API Keys client. @@ -47,7 +49,7 @@ def restrict_api_key_http(project_id: str, location: str, key_id: str) -> Key: restrictions.browser_key_restrictions = browser_key_restrictions key = api_keys_v2.Key() - key.name = f"projects/{project_id}/locations/{location}/keys/{key_id}" + key.name = f"projects/{project_id}/locations/global/keys/{key_id}" key.restrictions = restrictions # Initialize request and set arguments. diff --git a/auth/api-client/restrict_api_key_ios.py b/auth/api-client/restrict_api_key_ios.py index d97d0876d4a..ae649d3d1a4 100644 --- a/auth/api-client/restrict_api_key_ios.py +++ b/auth/api-client/restrict_api_key_ios.py @@ -18,7 +18,7 @@ from google.cloud.api_keys_v2 import Key -def restrict_api_key_ios(project_id: str, location: str, key_id: str) -> Key: +def restrict_api_key_ios(project_id: str, key_id: str) -> Key: """ Restricts an API key. You can restrict usage of an API key to specific iOS apps by providing the bundle ID of each app. @@ -27,10 +27,12 @@ def restrict_api_key_ios(project_id: str, location: str, key_id: str) -> Key: Args: project_id: Google Cloud project id. - location: Can only be "global". key_id: ID of the key to restrict. This ID is auto-created during key creation. This is different from the key string. To obtain the key_id, you can also use the lookup api: client.lookup_key() + + Returns: + response: Returns the updated API Key. """ # Create the API Keys client. @@ -47,7 +49,7 @@ def restrict_api_key_ios(project_id: str, location: str, key_id: str) -> Key: restrictions.ios_key_restrictions = ios_key_restrictions key = api_keys_v2.Key() - key.name = f"projects/{project_id}/locations/{location}/keys/{key_id}" + key.name = f"projects/{project_id}/locations/global/keys/{key_id}" key.restrictions = restrictions # Initialize request and set arguments. diff --git a/auth/api-client/restrict_api_key_server.py b/auth/api-client/restrict_api_key_server.py index 1a1aa683a9d..ac9b5cdce28 100644 --- a/auth/api-client/restrict_api_key_server.py +++ b/auth/api-client/restrict_api_key_server.py @@ -18,7 +18,7 @@ from google.cloud.api_keys_v2 import Key -def restrict_api_key_server(project_id: str, location: str, key_id: str) -> Key: +def restrict_api_key_server(project_id: str, key_id: str) -> Key: """ Restricts the API key based on IP addresses. You can specify one or more IP addresses of the callers, for example web servers or cron jobs, that are allowed to use your API key. @@ -27,10 +27,12 @@ def restrict_api_key_server(project_id: str, location: str, key_id: str) -> Key: Args: project_id: Google Cloud project id. - location: Can only be "global". key_id: ID of the key to restrict. This ID is auto-created during key creation. This is different from the key string. To obtain the key_id, you can also use the lookup api: client.lookup_key() + + Returns: + response: Returns the updated API Key. """ # Create the API Keys client. @@ -48,7 +50,7 @@ def restrict_api_key_server(project_id: str, location: str, key_id: str) -> Key: restrictions.server_key_restrictions = server_key_restrictions key = api_keys_v2.Key() - key.name = f"projects/{project_id}/locations/{location}/keys/{key_id}" + key.name = f"projects/{project_id}/locations/global/keys/{key_id}" key.restrictions = restrictions # Initialize request and set arguments. From 3efa9eb5a72621b9aceb910bc8b5bfe65dae6947 Mon Sep 17 00:00:00 2001 From: Sita Lakshmi Sangameswaran Date: Wed, 16 Nov 2022 01:26:12 +0530 Subject: [PATCH 11/18] modified test file and lint fix --- auth/api-client/api_key_test.py | 18 +++++++++++------- auth/api-client/create_api_key.py | 2 +- auth/api-client/restrict_api_key_android.py | 4 ++-- auth/api-client/restrict_api_key_api.py | 2 +- auth/api-client/restrict_api_key_http.py | 2 +- auth/api-client/restrict_api_key_ios.py | 2 +- auth/api-client/restrict_api_key_server.py | 2 +- 7 files changed, 18 insertions(+), 14 deletions(-) diff --git a/auth/api-client/api_key_test.py b/auth/api-client/api_key_test.py index e23e2df8fa8..9ced6afa601 100644 --- a/auth/api-client/api_key_test.py +++ b/auth/api-client/api_key_test.py @@ -35,9 +35,13 @@ @pytest.fixture(scope="module") def api_key(): - api_key = create_api_key.create_api_key(PROJECT, "global") + api_key = create_api_key.create_api_key(PROJECT) yield api_key - delete_api_key.delete_api_key(PROJECT, "global", api_key.name.rsplit("/")[-1]) + delete_api_key.delete_api_key(PROJECT, get_key_id(api_key.name)) + + +def get_key_id(api_key_name: str): + return api_key_name.rsplit("/")[-1] def test_authenticate_with_api_key(api_key: Key, capsys: CaptureFixture): @@ -53,30 +57,30 @@ def test_lookup_api_key(api_key: Key, capsys: CaptureFixture): def test_restrict_api_key_android(api_key: Key, capsys: CaptureFixture): - restrict_api_key_android.restrict_api_key_android(PROJECT, "global", api_key.name.rsplit("/")[-1]) + restrict_api_key_android.restrict_api_key_android(PROJECT, get_key_id(api_key.name)) out, err = capsys.readouterr() assert re.search(f"Successfully updated the API key: {api_key.name}", out) def test_restrict_api_key_api(api_key: Key, capsys: CaptureFixture): - restrict_api_key_api.restrict_api_key_api(PROJECT, "global", api_key.name.rsplit("/")[-1]) + restrict_api_key_api.restrict_api_key_api(PROJECT, get_key_id(api_key.name)) out, err = capsys.readouterr() assert re.search(f"Successfully updated the API key: {api_key.name}", out) def test_restrict_api_key_http(api_key: Key, capsys: CaptureFixture): - restrict_api_key_http.restrict_api_key_http(PROJECT, "global", api_key.name.rsplit("/")[-1]) + restrict_api_key_http.restrict_api_key_http(PROJECT, get_key_id(api_key.name)) out, err = capsys.readouterr() assert re.search(f"Successfully updated the API key: {api_key.name}", out) def test_restrict_api_key_ios(api_key: Key, capsys: CaptureFixture): - restrict_api_key_ios.restrict_api_key_ios(PROJECT, "global", api_key.name.rsplit("/")[-1]) + restrict_api_key_ios.restrict_api_key_ios(PROJECT, get_key_id(api_key.name)) out, err = capsys.readouterr() assert re.search(f"Successfully updated the API key: {api_key.name}", out) def test_restrict_api_key_server(api_key: Key, capsys: CaptureFixture): - restrict_api_key_server.restrict_api_key_server(PROJECT, "global", api_key.name.rsplit("/")[-1]) + restrict_api_key_server.restrict_api_key_server(PROJECT, get_key_id(api_key.name)) out, err = capsys.readouterr() assert re.search(f"Successfully updated the API key: {api_key.name}", out) diff --git a/auth/api-client/create_api_key.py b/auth/api-client/create_api_key.py index 39f2c3bbfbf..8c5825b93b1 100644 --- a/auth/api-client/create_api_key.py +++ b/auth/api-client/create_api_key.py @@ -29,7 +29,7 @@ def create_api_key(project_id: str) -> Key: Args: project_id: Google Cloud project id. - + Returns: response: Returns the created API Key. """ diff --git a/auth/api-client/restrict_api_key_android.py b/auth/api-client/restrict_api_key_android.py index ff0cf7d15ce..616c1208d32 100644 --- a/auth/api-client/restrict_api_key_android.py +++ b/auth/api-client/restrict_api_key_android.py @@ -31,11 +31,11 @@ def restrict_api_key_android(project_id: str, key_id: str) -> Key: key_id: ID of the key to restrict. This ID is auto-created during key creation. This is different from the key string. To obtain the key_id, you can also use the lookup api: client.lookup_key() - + Returns: response: Returns the updated API Key. """ - + # Create the API Keys client. client = api_keys_v2.ApiKeysClient() diff --git a/auth/api-client/restrict_api_key_api.py b/auth/api-client/restrict_api_key_api.py index f5d903fcf87..e902c2b9a81 100644 --- a/auth/api-client/restrict_api_key_api.py +++ b/auth/api-client/restrict_api_key_api.py @@ -29,7 +29,7 @@ def restrict_api_key_api(project_id: str, key_id: str) -> Key: key_id: ID of the key to restrict. This ID is auto-created during key creation. This is different from the key string. To obtain the key_id, you can also use the lookup api: client.lookup_key() - + Returns: response: Returns the updated API Key. """ diff --git a/auth/api-client/restrict_api_key_http.py b/auth/api-client/restrict_api_key_http.py index 9c071001d90..f8b0e068415 100644 --- a/auth/api-client/restrict_api_key_http.py +++ b/auth/api-client/restrict_api_key_http.py @@ -30,7 +30,7 @@ def restrict_api_key_http(project_id: str, key_id: str) -> Key: key_id: ID of the key to restrict. This ID is auto-created during key creation. This is different from the key string. To obtain the key_id, you can also use the lookup api: client.lookup_key() - + Returns: response: Returns the updated API Key. """ diff --git a/auth/api-client/restrict_api_key_ios.py b/auth/api-client/restrict_api_key_ios.py index ae649d3d1a4..17e4bf1f474 100644 --- a/auth/api-client/restrict_api_key_ios.py +++ b/auth/api-client/restrict_api_key_ios.py @@ -30,7 +30,7 @@ def restrict_api_key_ios(project_id: str, key_id: str) -> Key: key_id: ID of the key to restrict. This ID is auto-created during key creation. This is different from the key string. To obtain the key_id, you can also use the lookup api: client.lookup_key() - + Returns: response: Returns the updated API Key. """ diff --git a/auth/api-client/restrict_api_key_server.py b/auth/api-client/restrict_api_key_server.py index ac9b5cdce28..a34e991da4a 100644 --- a/auth/api-client/restrict_api_key_server.py +++ b/auth/api-client/restrict_api_key_server.py @@ -30,7 +30,7 @@ def restrict_api_key_server(project_id: str, key_id: str) -> Key: key_id: ID of the key to restrict. This ID is auto-created during key creation. This is different from the key string. To obtain the key_id, you can also use the lookup api: client.lookup_key() - + Returns: response: Returns the updated API Key. """ From 4414b057d8b6c6ec5bf14bc482615c9541c5a402 Mon Sep 17 00:00:00 2001 From: Sita Lakshmi Sangameswaran Date: Wed, 16 Nov 2022 01:38:45 +0530 Subject: [PATCH 12/18] lookup api call reformat and lint fix --- auth/api-client/create_api_key.py | 2 +- auth/api-client/lookup_api_key.py | 9 +++++---- auth/api-client/restrict_api_key_api.py | 2 +- auth/api-client/restrict_api_key_http.py | 2 +- auth/api-client/restrict_api_key_ios.py | 2 +- auth/api-client/restrict_api_key_server.py | 2 +- 6 files changed, 10 insertions(+), 9 deletions(-) diff --git a/auth/api-client/create_api_key.py b/auth/api-client/create_api_key.py index 8c5825b93b1..707ab5d8849 100644 --- a/auth/api-client/create_api_key.py +++ b/auth/api-client/create_api_key.py @@ -29,7 +29,7 @@ def create_api_key(project_id: str) -> Key: Args: project_id: Google Cloud project id. - + Returns: response: Returns the created API Key. """ diff --git a/auth/api-client/lookup_api_key.py b/auth/api-client/lookup_api_key.py index 69a632ccc1f..b6ab728c6cc 100644 --- a/auth/api-client/lookup_api_key.py +++ b/auth/api-client/lookup_api_key.py @@ -34,10 +34,11 @@ def lookup_api_key(api_key_string: str) -> None: client = api_keys_v2.ApiKeysClient() # Initialize the lookup request and set the API key string. - # Optionally, you can also set the etag (version). - lookup_key_request = api_keys_v2.LookupKeyRequest() - lookup_key_request.key_string = api_key_string - # lookup_key_request.etag = etag + lookup_key_request = api_keys_v2.LookupKeyRequest( + key_string=api_key_string, + # Optionally, you can also set the etag (version). + # etag=etag, + ) # Make the request and obtain the response. lookup_key_response = client.lookup_key(lookup_key_request) diff --git a/auth/api-client/restrict_api_key_api.py b/auth/api-client/restrict_api_key_api.py index e902c2b9a81..00f97fe680f 100644 --- a/auth/api-client/restrict_api_key_api.py +++ b/auth/api-client/restrict_api_key_api.py @@ -29,7 +29,7 @@ def restrict_api_key_api(project_id: str, key_id: str) -> Key: key_id: ID of the key to restrict. This ID is auto-created during key creation. This is different from the key string. To obtain the key_id, you can also use the lookup api: client.lookup_key() - + Returns: response: Returns the updated API Key. """ diff --git a/auth/api-client/restrict_api_key_http.py b/auth/api-client/restrict_api_key_http.py index f8b0e068415..991e6476c47 100644 --- a/auth/api-client/restrict_api_key_http.py +++ b/auth/api-client/restrict_api_key_http.py @@ -30,7 +30,7 @@ def restrict_api_key_http(project_id: str, key_id: str) -> Key: key_id: ID of the key to restrict. This ID is auto-created during key creation. This is different from the key string. To obtain the key_id, you can also use the lookup api: client.lookup_key() - + Returns: response: Returns the updated API Key. """ diff --git a/auth/api-client/restrict_api_key_ios.py b/auth/api-client/restrict_api_key_ios.py index 17e4bf1f474..c5028b8161e 100644 --- a/auth/api-client/restrict_api_key_ios.py +++ b/auth/api-client/restrict_api_key_ios.py @@ -30,7 +30,7 @@ def restrict_api_key_ios(project_id: str, key_id: str) -> Key: key_id: ID of the key to restrict. This ID is auto-created during key creation. This is different from the key string. To obtain the key_id, you can also use the lookup api: client.lookup_key() - + Returns: response: Returns the updated API Key. """ diff --git a/auth/api-client/restrict_api_key_server.py b/auth/api-client/restrict_api_key_server.py index a34e991da4a..11e45c6b455 100644 --- a/auth/api-client/restrict_api_key_server.py +++ b/auth/api-client/restrict_api_key_server.py @@ -30,7 +30,7 @@ def restrict_api_key_server(project_id: str, key_id: str) -> Key: key_id: ID of the key to restrict. This ID is auto-created during key creation. This is different from the key string. To obtain the key_id, you can also use the lookup api: client.lookup_key() - + Returns: response: Returns the updated API Key. """ From d6a3fa77a2a1a1f41af17226581df4bbc67e3514 Mon Sep 17 00:00:00 2001 From: Sita Lakshmi Sangameswaran Date: Wed, 16 Nov 2022 01:47:44 +0530 Subject: [PATCH 13/18] lint fix --- auth/api-client/create_api_key.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/auth/api-client/create_api_key.py b/auth/api-client/create_api_key.py index 707ab5d8849..b56e9ee158c 100644 --- a/auth/api-client/create_api_key.py +++ b/auth/api-client/create_api_key.py @@ -29,7 +29,7 @@ def create_api_key(project_id: str) -> Key: Args: project_id: Google Cloud project id. - + Returns: response: Returns the created API Key. """ From c2ce08e42a653d38aed2906f2f30371b7fc9c0ff Mon Sep 17 00:00:00 2001 From: Dan Lee <71398022+dandhlee@users.noreply.github.com> Date: Tue, 15 Nov 2022 19:35:51 -0600 Subject: [PATCH 14/18] Update auth/api-client/restrict_api_key_android.py --- auth/api-client/restrict_api_key_android.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/auth/api-client/restrict_api_key_android.py b/auth/api-client/restrict_api_key_android.py index 616c1208d32..9e5f657726e 100644 --- a/auth/api-client/restrict_api_key_android.py +++ b/auth/api-client/restrict_api_key_android.py @@ -29,8 +29,8 @@ def restrict_api_key_android(project_id: str, key_id: str) -> Key: Args: project_id: Google Cloud project id. key_id: ID of the key to restrict. This ID is auto-created during key creation. - This is different from the key string. To obtain the key_id, - you can also use the lookup api: client.lookup_key() + This is different from the key string. To obtain the key_id, + you can also use the lookup api: client.lookup_key() Returns: response: Returns the updated API Key. From 143525bdfefbe3d75a91c179e619455b52f8550d Mon Sep 17 00:00:00 2001 From: Dan Lee <71398022+dandhlee@users.noreply.github.com> Date: Tue, 15 Nov 2022 19:35:59 -0600 Subject: [PATCH 15/18] Update auth/api-client/restrict_api_key_api.py --- auth/api-client/restrict_api_key_api.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/auth/api-client/restrict_api_key_api.py b/auth/api-client/restrict_api_key_api.py index 00f97fe680f..bc3198e5c4d 100644 --- a/auth/api-client/restrict_api_key_api.py +++ b/auth/api-client/restrict_api_key_api.py @@ -27,8 +27,8 @@ def restrict_api_key_api(project_id: str, key_id: str) -> Key: Args: project_id: Google Cloud project id. key_id: ID of the key to restrict. This ID is auto-created during key creation. - This is different from the key string. To obtain the key_id, - you can also use the lookup api: client.lookup_key() + This is different from the key string. To obtain the key_id, + you can also use the lookup api: client.lookup_key() Returns: response: Returns the updated API Key. From d830bcc29e8b88868bf90b7a765eaa06dfa4c122 Mon Sep 17 00:00:00 2001 From: Dan Lee <71398022+dandhlee@users.noreply.github.com> Date: Tue, 15 Nov 2022 19:36:06 -0600 Subject: [PATCH 16/18] Update auth/api-client/restrict_api_key_http.py --- auth/api-client/restrict_api_key_http.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/auth/api-client/restrict_api_key_http.py b/auth/api-client/restrict_api_key_http.py index 991e6476c47..f6c6cdf3461 100644 --- a/auth/api-client/restrict_api_key_http.py +++ b/auth/api-client/restrict_api_key_http.py @@ -28,8 +28,8 @@ def restrict_api_key_http(project_id: str, key_id: str) -> Key: Args: project_id: Google Cloud project id. key_id: ID of the key to restrict. This ID is auto-created during key creation. - This is different from the key string. To obtain the key_id, - you can also use the lookup api: client.lookup_key() + This is different from the key string. To obtain the key_id, + you can also use the lookup api: client.lookup_key() Returns: response: Returns the updated API Key. From 1f9a3f7d1e7cc8645982c49f3b1a08ec9867571d Mon Sep 17 00:00:00 2001 From: Dan Lee <71398022+dandhlee@users.noreply.github.com> Date: Tue, 15 Nov 2022 19:36:13 -0600 Subject: [PATCH 17/18] Update auth/api-client/restrict_api_key_ios.py --- auth/api-client/restrict_api_key_ios.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/auth/api-client/restrict_api_key_ios.py b/auth/api-client/restrict_api_key_ios.py index c5028b8161e..f808e2dcac8 100644 --- a/auth/api-client/restrict_api_key_ios.py +++ b/auth/api-client/restrict_api_key_ios.py @@ -28,8 +28,8 @@ def restrict_api_key_ios(project_id: str, key_id: str) -> Key: Args: project_id: Google Cloud project id. key_id: ID of the key to restrict. This ID is auto-created during key creation. - This is different from the key string. To obtain the key_id, - you can also use the lookup api: client.lookup_key() + This is different from the key string. To obtain the key_id, + you can also use the lookup api: client.lookup_key() Returns: response: Returns the updated API Key. From c6bf1eea67549c5089a1ed31eed28e1288f3808b Mon Sep 17 00:00:00 2001 From: Dan Lee <71398022+dandhlee@users.noreply.github.com> Date: Tue, 15 Nov 2022 19:36:21 -0600 Subject: [PATCH 18/18] Update auth/api-client/restrict_api_key_server.py --- auth/api-client/restrict_api_key_server.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/auth/api-client/restrict_api_key_server.py b/auth/api-client/restrict_api_key_server.py index 11e45c6b455..aefe2a31751 100644 --- a/auth/api-client/restrict_api_key_server.py +++ b/auth/api-client/restrict_api_key_server.py @@ -28,8 +28,8 @@ def restrict_api_key_server(project_id: str, key_id: str) -> Key: Args: project_id: Google Cloud project id. key_id: ID of the key to restrict. This ID is auto-created during key creation. - This is different from the key string. To obtain the key_id, - you can also use the lookup api: client.lookup_key() + This is different from the key string. To obtain the key_id, + you can also use the lookup api: client.lookup_key() Returns: response: Returns the updated API Key.