GCP Introduction
GCP Pricing
GCP Threats
GCP Misconfigurations
- Getting Started with GCP Audit
- CloudSql Audit
- Cloud Tasks Monitoring
- Dataflow Monitoring
- Function Monitoring
- Monitoring Compliance
- PubSubLite Monitoring
- Spanner Monitoring
- NoSQL Monitoring
- Compute Audit
- IAM Audit
- BigQuery Monitoring
- CDN Monitoring
- DNS Monitoring
- KMS Monitoring
- Kubernetes Audit
- Load Balancer Monitoring
- Log Monitoring
- Storage Audit
- Pub/Sub Monitoring
- VPC Audit
- IAM Deep Dive
GCP Threats
Cloud Monitoring Should Monitor IAM Key Authentication Events Count
More Info:
Ensure Cloud Monitoring monitors IAM key authentication events count
Risk Level
Medium
Address
Security
Compliance Standards
CBP
Triage and Remediation
Remediation
To remediate the misconfiguration of not monitoring IAM Key Authentication Events Count in GCP, you can follow the below steps using the GCP console:
-
Open the GCP console and navigate to the Security Command Center.
-
Click on the “Security Health Analytics” tab on the left-hand side of the screen.
-
Under the “Security Health Analytics” tab, click on the “Security Health Analytics Findings” option.
-
In the “Findings” section, search for the finding “IAM Key Authentication Events Count Not Monitored”.
-
Click on the finding to view the details.
-
In the details section, click on the “Remediation Steps” tab.
-
Follow the recommended remediation steps to enable monitoring of IAM Key Authentication Events Count.
-
The recommended remediation steps include creating a new custom dashboard in the GCP console, adding a widget to the dashboard to monitor IAM Key Authentication Events Count, and configuring the widget to display the count of authentication events.
-
Once the remediation steps are completed, the finding for “IAM Key Authentication Events Count Not Monitored” should be resolved in the Security Health Analytics section.
By following these steps, you can remediate the misconfiguration of not monitoring IAM Key Authentication Events Count in GCP using the GCP console.
To remediate the misconfiguration of not monitoring IAM key authentication events count in GCP using GCP CLI, follow these steps:
-
Open the Google Cloud Console and select the project for which you want to remediate the misconfiguration.
-
Open the Cloud Shell by clicking on the icon in the top right corner of the console.
-
In the Cloud Shell, enter the following command to enable the Cloud Audit Logs API:
gcloud services enable cloudaudit.googleapis.com
- Once the API is enabled, you can create a sink to export the audit logs to a destination of your choice. For example, to export the logs to Cloud Storage, use the following command:
gcloud logging sinks create [SINK_NAME] storage.googleapis.com/[BUCKET_NAME] --log-filter='protoPayload.methodName="google.iam.credentials.v1.IAMCredentials.GenerateAccessToken"'
Replace [SINK_NAME]
with a name for the sink and [BUCKET_NAME]
with the name of the Cloud Storage bucket you want to export the logs to.
- After creating the sink, grant the necessary permissions to the Cloud Storage bucket by running the following command:
gsutil iam ch serviceAccount:[PROJECT_NUMBER][email protected]:objectCreator,objectViewer gs://[BUCKET_NAME]
Replace [PROJECT_NUMBER]
with the project number and [BUCKET_NAME]
with the name of the Cloud Storage bucket.
- Finally, test the sink by generating an access token using an IAM key and checking if the event count is logged in the Cloud Audit Logs. You can do this by running the following command:
gcloud iam service-accounts keys create [KEY_FILE_NAME].json --iam-account [IAM_ACCOUNT_EMAIL]
Replace [KEY_FILE_NAME]
with a name for the key file and [IAM_ACCOUNT_EMAIL]
with the email address of the IAM key.
- After generating the access token, check the Cloud Audit Logs to confirm that the event count has been logged. You can do this by navigating to the Cloud Logging section of the Google Cloud Console and selecting the sink you created in step 4.
By following these steps, you can remediate the misconfiguration of not monitoring IAM key authentication events count in GCP using GCP CLI.
To remediate the misconfiguration in GCP where cloud monitoring should monitor IAM key authentication events count using Python, follow the below steps:
-
First, you need to enable the required APIs for the project. You can do this by navigating to the APIs & Services section in the GCP console and searching for the required APIs, which in this case are the Cloud Monitoring API and the Cloud IAM API.
-
Next, you need to create a service account with the required permissions to access the Cloud Monitoring API and the Cloud IAM API. You can do this by navigating to the Service Accounts section in the GCP console and creating a new service account with the required permissions.
-
Once the service account is created, you need to download the service account key in JSON format. This key will be used to authenticate the Python script to access the Cloud Monitoring API and the Cloud IAM API.
-
Now, you can use the Python client libraries for the Cloud Monitoring API and the Cloud IAM API to write a script that will retrieve the IAM key authentication events count.
-
Here is a sample Python script that retrieves the IAM key authentication events count:
from google.oauth2 import service_account
from googleapiclient.discovery import build
# Replace with the path to your service account key file
KEY_FILE_LOCATION = '/path/to/key.json'
# Replace with your project ID
PROJECT_ID = 'your-project-id'
# Replace with the name of the metric you want to retrieve
METRIC_NAME = 'iam.googleapis.com/iam/authorized_keys_count'
# Replace with the time interval you want to retrieve data for
TIME_INTERVAL = '1h'
# Authenticate with the service account key file
credentials = service_account.Credentials.from_service_account_file(
KEY_FILE_LOCATION)
# Build the Cloud Monitoring API client
monitoring_client = build('monitoring', 'v3', credentials=credentials)
# Build the Cloud IAM API client
iam_client = build('iam', 'v1', credentials=credentials)
# Retrieve the list of service accounts in the project
service_accounts = iam_client.projects().serviceAccounts().list(
name='projects/{}'.format(PROJECT_ID)).execute().get('accounts')
# Loop through each service account and retrieve the IAM key authentication events count
for service_account in service_accounts:
resource = 'projects/{}/serviceAccounts/{}'.format(
PROJECT_ID, service_account['email'])
response = monitoring_client.projects().timeSeries().list(
name='projects/{}'.format(PROJECT_ID),
filter='metric.type="{}" resource.type="service_account" resource.labels.email="{}"'.format(
METRIC_NAME, service_account['email']),
interval_startTime='{}s'.format(int((time.time() - 3600) * 1000000000)),
interval_endTime='{}s'.format(int(time.time() * 1000000000)),
aggregation_alignmentPeriod=TIME_INTERVAL,
aggregation_perSeriesAligner='ALIGN_SUM'
).execute()
# Print the IAM key authentication events count for the service account
print('Service Account: {}'.format(resource))
if 'timeSeries' in response:
for time_series in response['timeSeries']:
print(' Metric: {}'.format(time_series['metric']['type']))
for point in time_series['points']:
print(' Value: {}'.format(point['value']['int64Value']))
else:
print(' No data available')
-
Replace the
KEY_FILE_LOCATION
,PROJECT_ID
,METRIC_NAME
, andTIME_INTERVAL
variables with the appropriate values for your environment. -
Run the Python script to retrieve the IAM key authentication events count for your GCP project.
By following these steps, you can remediate the misconfiguration in GCP where cloud monitoring should monitor IAM key authentication events count using Python.