Security risks involved in using API-Keys are below: • API keys are simple encrypted strings • API keys do not identify the user or the application making the API request • API keys are typically accessible to clients, making it easy to discover and steal an API key In light of these potential risks, Google recommends using the standard authentication flow instead of API-Keys. However, there are limited cases where API keys are more appropriate. For example, if there is a mobile application that needs to use the Google Cloud Translation API, but doesn’t otherwise need a backend server, API keys are the simplest way to authenticate to that API. In order to reduce attack surfaces by providing least privileges, API-Keys can be restricted to use (call) only APIs required by an application.
To remediate the misconfiguration of ensuring API keys are restricted to necessary APIs in GCP using GCP CLI, follow the below steps:
First, identify the API keys that are not restricted to necessary APIs. You can use the following command to list all the API keys in your project:
Copy
Ask AI
gcloud beta services api-keys list
Once you have identified the API keys that are not restricted to necessary APIs, you can use the following command to update the API key and restrict it to specific APIs:
Replace the [API_KEY_NAME] with the name of the API key that you want to update, [REFERER_URL] with the URL of the referring website, and [API_LIST] with the list of APIs that you want to allow for this API key.
If you want to restrict the API key to a specific IP address, use the following command:
This command will display the details of the API key, including the allowed APIs and the restricted referer or IP address.By following the above steps, you can remediate the misconfiguration of ensuring API keys are restricted to necessary APIs in GCP using GCP CLI.
Using Python
To remediate the misconfiguration “Ensure API Keys Are Restricted To Necessary APIs” for GCP using Python, follow these steps:
Identify the API keys that are not restricted to necessary APIs. You can do this by using the following command in the GCP Cloud Shell:
Copy
Ask AI
gcloud beta iam service-accounts keys list --iam-account [SA-NAME] --filter="keyRestrictions.type:unspecified"
Replace [SA-NAME] with the name of the service account that you want to check.
Create a new API key with restricted access. You can do this by using the following Python code:
Copy
Ask AI
from google.oauth2 import service_accountfrom googleapiclient.discovery import buildSCOPES = ['https://www.googleapis.com/auth/cloud-platform']SERVICE_ACCOUNT_FILE = '[PATH TO YOUR SERVICE ACCOUNT JSON FILE]'credentials = service_account.Credentials.from_service_account_file( SERVICE_ACCOUNT_FILE, scopes=SCOPES)service = build('iamcredentials', 'v1', credentials=credentials)response = service.projects().serviceAccounts().keys().create( name='projects/-/serviceAccounts/[SA-NAME]', body={ 'keyAlgorithm': 'KEY_ALG_RSA_2048', 'keyRestrictions': { 'allowedRegions': ['us-central1'], 'allowedServices': ['storage.googleapis.com'] } }).execute()print(response)
Replace [PATH TO YOUR SERVICE ACCOUNT JSON FILE] with the path to your service account JSON file, and [SA-NAME] with the name of the service account that you want to create the API key for.This code creates an API key with restricted access to the Storage API in the us-central1 region.
Delete the old API key. You can do this by using the following command in the GCP Cloud Shell:
Copy
Ask AI
gcloud beta iam service-accounts keys delete [KEY-ID] --iam-account [SA-NAME]
Replace [KEY-ID] with the ID of the old API key that you want to delete, and [SA-NAME] with the name of the service account that the API key belongs to.
Verify that the new API key has restricted access. You can do this by using the following command in the GCP Cloud Shell:
Copy
Ask AI
gcloud beta iam service-accounts keys list --iam-account [SA-NAME] --filter="keyRestrictions.type:asymmetric_public"
Replace [SA-NAME] with the name of the service account that you created the new API key for. This command should return the new API key that you just created with restricted access.
By following these steps, you can remediate the misconfiguration “Ensure API Keys Are Restricted To Necessary APIs” for GCP using Python.