Bird
Raised Fist0
GCPcloud~5 mins

Audit logging in GCP - Commands & Configuration

Choose your learning style10 modes available
Introduction
Audit logging helps you keep track of who did what in your cloud projects. It records actions on your resources so you can check for mistakes or security issues.
When you want to see who changed a virtual machine or storage bucket in your project.
When you need to investigate why a service stopped working by checking recent actions.
When you want to keep a record of all access to sensitive data for compliance.
When you want to monitor if someone deleted or created resources without permission.
When you want to set alerts based on specific actions recorded in logs.
Commands
This command creates a sink to export audit logs about resource creation to a Cloud Storage bucket named 'my-audit-logs-bucket'. It filters logs to only include creation actions.
Terminal
gcloud logging sinks create my-audit-sink storage.googleapis.com/my-audit-logs-bucket --log-filter="resource.type=project AND protoPayload.methodName:Create"
Expected OutputExpected
Created sink [my-audit-sink].
→
--log-filter - Filters logs to export only specific audit events.
This command lists all logging sinks in your project so you can verify the sink was created.
Terminal
gcloud logging sinks list
Expected OutputExpected
NAME DESTINATION FILTER my-audit-sink storage.googleapis.com/my-audit-logs-bucket resource.type=project AND protoPayload.methodName:Create
This command lists the files in the Cloud Storage bucket where audit logs are exported, confirming logs are being saved.
Terminal
gsutil ls gs://my-audit-logs-bucket
Expected OutputExpected
gs://my-audit-logs-bucket/2024/06/01/00000000000000000000.json
Key Concept

If you remember nothing else from audit logging, remember: audit logs record who did what and when, helping you track changes and security events.

Common Mistakes
Not enabling audit logs in the Google Cloud project before creating sinks.
Without enabling audit logs, no logs are generated or exported, so sinks have no data.
Enable audit logging in the project settings or ensure default audit logs are active before creating sinks.
Using incorrect log filters that exclude important audit events.
Filters that are too narrow or wrong syntax cause missing logs in the export.
Test filters carefully and use simple filters first, then refine as needed.
Not granting the sink service account permission to write to the Cloud Storage bucket.
Without write permission, logs cannot be saved to the bucket, causing export failures.
Grant the sink's service account the 'Storage Object Creator' role on the bucket.
Summary
Create a logging sink to export audit logs to a storage bucket with a filter for specific actions.
List sinks to verify the sink creation and configuration.
Check the storage bucket to confirm audit logs are being exported and saved.

Practice

(1/5)
1. What is the main purpose of audit logging in Google Cloud Platform?
easy
A. To create virtual machines
B. To record actions for security and troubleshooting
C. To speed up network traffic
D. To increase storage capacity automatically

Solution

  1. Step 1: Understand audit logging function

    Audit logging captures user and system actions in your cloud environment.
  2. Step 2: Identify the purpose

    This helps with security monitoring and troubleshooting issues.
  3. Final Answer:

    To record actions for security and troubleshooting -> Option B
  4. Quick Check:

    Audit logging = record actions [OK]
Hint: Audit logs track actions for security and fixes [OK]
Common Mistakes:
  • Confusing audit logs with storage or network features
  • Thinking audit logs create resources
  • Assuming audit logs improve performance
2. Which command correctly creates a sink to export audit logs to a Cloud Storage bucket named my-logs-bucket?
easy
A. gcloud logging sinks create my-sink storage.googleapis.com/my-logs-bucket --log-filter="resource.type=\"gce_instance\""
B. gcloud logging create sink my-sink storage://my-logs-bucket
C. gcloud logs create sink my-sink gs://my-logs-bucket
D. gcloud logging sinks add my-sink storage.googleapis.com/my-logs-bucket

Solution

  1. Step 1: Identify correct gcloud command syntax

    The correct command uses gcloud logging sinks create followed by sink name and destination URL.
  2. Step 2: Verify destination format

    Cloud Storage buckets use storage.googleapis.com/BUCKET_NAME format.
  3. Final Answer:

    gcloud logging sinks create my-sink storage.googleapis.com/my-logs-bucket --log-filter="resource.type=\"gce_instance\"" -> Option A
  4. Quick Check:

    Correct sink creation syntax = gcloud logging sinks create my-sink storage.googleapis.com/my-logs-bucket --log-filter="resource.type=\"gce_instance\"" [OK]
Hint: Use 'gcloud logging sinks create' with full storage URL [OK]
Common Mistakes:
  • Using wrong command verbs like 'create sink' or 'add sink'
  • Incorrect bucket URL format (missing storage.googleapis.com)
  • Using 'gs://' instead of full URL
3. Given this filter in a sink configuration: resource.type="bigquery_resource" AND protoPayload.methodName="jobservice.jobcompleted", what logs will be exported?
medium
A. Only logs from Compute Engine instances
B. All error logs from Cloud Storage
C. All BigQuery job completion audit logs
D. All network traffic logs

Solution

  1. Step 1: Analyze the resource type filter

    The filter specifies resource.type="bigquery_resource", so logs relate to BigQuery.
  2. Step 2: Analyze the method name filter

    protoPayload.methodName="jobservice.jobcompleted" means only job completion events are included.
  3. Final Answer:

    All BigQuery job completion audit logs -> Option C
  4. Quick Check:

    Filter matches BigQuery job completions = All BigQuery job completion audit logs [OK]
Hint: Filter resource and method to find exported logs [OK]
Common Mistakes:
  • Confusing resource types with unrelated services
  • Ignoring methodName filter
  • Assuming all logs are exported
4. You created a sink to export audit logs to a Pub/Sub topic but logs are not appearing. What is the most likely cause?
medium
A. The sink lacks permission to publish to the Pub/Sub topic
B. The sink filter is too broad and excludes all logs
C. Audit logging is disabled in the project
D. The Pub/Sub topic name is misspelled in the sink

Solution

  1. Step 1: Check sink permissions

    Sinks need permission to write to destinations like Pub/Sub topics.
  2. Step 2: Identify common permission issue

    If logs do not appear, the sink likely lacks the pubsub.publisher role on the topic.
  3. Final Answer:

    The sink lacks permission to publish to the Pub/Sub topic -> Option A
  4. Quick Check:

    Missing sink permissions = The sink lacks permission to publish to the Pub/Sub topic [OK]
Hint: Check sink destination permissions first when logs missing [OK]
Common Mistakes:
  • Assuming audit logging is off without checking
  • Blaming filter without verifying permissions
  • Ignoring typo in topic name
5. You want to export only admin activity audit logs for Cloud Storage buckets in your project to a BigQuery dataset for analysis. Which sink filter is correct?
hard
A. resource.type="gcs_bucket" AND severity="ERROR"
B. logName:"cloudaudit.googleapis.com/activity" AND resource.type="gcs_bucket" AND protoPayload.methodName:"storage.objects.create"
C. logName:"cloudaudit.googleapis.com/data_access" AND resource.type="gcs_bucket"
D. logName:"cloudaudit.googleapis.com/admin_activity" AND resource.type="gcs_bucket"

Solution

  1. Step 1: Identify admin activity logs

    Admin activity logs use logName:"cloudaudit.googleapis.com/admin_activity".
  2. Step 2: Filter for Cloud Storage buckets

    Use resource.type="gcs_bucket" to target storage buckets.
  3. Step 3: Confirm filter matches requirement

    logName:"cloudaudit.googleapis.com/admin_activity" AND resource.type="gcs_bucket" correctly combines admin activity logs with GCS buckets.
  4. Final Answer:

    logName:"cloudaudit.googleapis.com/admin_activity" AND resource.type="gcs_bucket" -> Option D
  5. Quick Check:

    Admin activity + GCS bucket filter = logName:"cloudaudit.googleapis.com/admin_activity" AND resource.type="gcs_bucket" [OK]
Hint: Use admin_activity logName for admin logs [OK]
Common Mistakes:
  • Using data_access logs instead of admin_activity
  • Filtering by specific methods unnecessarily
  • Confusing severity with log type
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard