Google Cloud Audit Log Protobufs
The `google-cloud-audit-log` library provides the generated Python classes for Google Cloud Audit Log protocol buffer definitions. It enables developers to work with AuditLog messages, which describe activities in Google Cloud, allowing for parsing, constructing, and serializing these structured log entries. The current version is 0.4.0, and releases typically occur to update protobuf definitions as Google Cloud services evolve.
Warnings
- breaking The `protobuf` dependency requirements have changed across minor versions. Ensure your environment uses a compatible `protobuf` version to avoid runtime errors. For version `0.4.0`, `protobuf>=3.19.5, <5.0.0` is generally required.
- gotcha This library provides only the protobuf definitions for Google Cloud Audit Logs. It does NOT include a client library for fetching audit logs from Google Cloud APIs (e.g., Stackdriver Logging). Users are responsible for obtaining the serialized audit log data from other sources (e.g., Pub/Sub, Cloud Logging API) and then using this library to parse and interpret it.
- gotcha The import paths for protobuf messages are direct to the `_pb2` files (e.g., `from google.cloud.audit import audit_log_pb2`). There are no higher-level client objects or `types` modules provided by this library, as its sole purpose is to expose the generated protobuf classes.
- gotcha This library requires Python 3.7 or newer. Attempting to use it with older Python versions will result in installation failures or runtime errors.
Install
-
pip install google-cloud-audit-log
Imports
- AuditLog
from google.cloud.audit import audit_log_pb2
- RequestMetadata
from google.cloud.audit import audit_log_pb2
Quickstart
from google.cloud.audit import audit_log_pb2
from google.protobuf.json_format import MessageToJson, Parse
# Example: Construct an AuditLog message programmatically
audit_log_entry = audit_log_pb2.AuditLog(
service_name="compute.googleapis.com",
method_name="v1.compute.instances.insert",
resource_name="projects/my-project/zones/us-central1-a/instances/my-instance",
authentication_info=audit_log_pb2.AuthenticationInfo(
principal_email="user@example.com"
),
request_metadata=audit_log_pb2.RequestMetadata(
caller_ip="192.168.1.1",
caller_supplied_user_agent="gcloud-sdk",
)
)
print("Constructed AuditLog (JSON format):")
print(MessageToJson(audit_log_entry))
# Example: Simulate receiving a serialized AuditLog (e.g., from a message queue)
# In a real scenario, `serialized_data` would be bytes received from a source.
# For this example, we'll serialize the object we just created.
serialized_data = audit_log_entry.SerializeToString()
# Parse the received data back into an AuditLog object
parsed_log_entry = audit_log_pb2.AuditLog()
parsed_log_entry.ParseFromString(serialized_data)
print("\nParsed AuditLog (JSON format):")
print(MessageToJson(parsed_log_entry))
# Accessing fields from the parsed log entry
print(f"\nService Name: {parsed_log_entry.service_name}")
print(f"Method Name: {parsed_log_entry.method_name}")
if parsed_log_entry.authentication_info.principal_email:
print(f"Principal Email: {parsed_log_entry.authentication_info.principal_email}")