Google Cloud Audit Log Protobufs

0.4.0 · active · verified Sat Mar 28

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

Install

Imports

Quickstart

This quickstart demonstrates how to programmatically construct an `AuditLog` message and then serialize it to bytes, simulating how it might be stored or transmitted. It then shows how to deserialize those bytes back into an `AuditLog` object and access its fields. This reflects the library's primary use case of defining and manipulating audit log protobuf messages.

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}")

view raw JSON →