-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgcp_integrations.py
More file actions
509 lines (403 loc) · 17.8 KB
/
gcp_integrations.py
File metadata and controls
509 lines (403 loc) · 17.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
"""[FACT] Google Cloud Platform integrations for Constitutional Guardian.
[HYPOTHESIS] Native GCP service integration provides scalable, auditable,
production-grade deployment for real-time constitutional governance.
This module demonstrates use of:
- Cloud Pub/Sub: Federation event streaming
- Cloud Storage: Immutable receipt storage
- Secret Manager: DBC key encryption
- Cloud Logging: Structured audit trails
- Cloud Run: Serverless container deployment
- Cloud Build: CI/CD pipeline automation
"""
import json
import os
import re
from collections.abc import Callable
from dataclasses import asdict, dataclass
from datetime import datetime
from typing import Any, cast
# [FACT] Google Cloud client libraries
try:
from google.cloud import logging as cloud_logging
from google.cloud import pubsub_v1, secretmanager, storage
GCP_AVAILABLE = True
except ImportError:
GCP_AVAILABLE = False
_RECEIPT_ID_SAFE_PATTERN = re.compile(r"[^A-Za-z0-9._-]+")
def sanitize_receipt_id(receipt_id: str | None) -> str:
"""[FACT] Normalize receipt identifiers before using them in storage paths."""
normalized = _RECEIPT_ID_SAFE_PATTERN.sub("_", receipt_id or "").strip("._-")
return normalized or "receipt"
@dataclass
class FederationEvent:
"""[FACT] Federation event for Pub/Sub streaming."""
event_id: str
node_id: str
event_type: str # 'drift_detected', 'receipt_created', 'compliance_check'
timestamp: str
payload: dict[str, Any]
receipt_hash: str
class CloudPubSubFederation:
"""[FACT] Cloud Pub/Sub integration for cross-node federation messaging.
[HYPOTHESIS] Pub/Sub provides at-least-once delivery guarantees required
for constitutional quorum attestation across distributed nodes.
Google Cloud Service: Cloud Pub/Sub
Documentation: https://cloud.google.com/pubsub/docs
"""
def __init__(self, project_id: str | None = None):
self.project_id = project_id or os.getenv("GOOGLE_CLOUD_PROJECT")
self.subscription_name = os.getenv("PUBSUB_SUBSCRIPTION", "guardian-subscription")
topic_raw = os.getenv("PUBSUB_TOPIC", "helix-events").strip()
if topic_raw.startswith("projects/"):
self.topic_path = topic_raw
self.topic_name = topic_raw.rsplit("/", 1)[-1]
else:
self.topic_name = topic_raw
self.topic_path = ""
if GCP_AVAILABLE and self.project_id:
self.publisher = pubsub_v1.PublisherClient()
self.subscriber = pubsub_v1.SubscriberClient()
if not self.topic_path:
self.topic_path = self.publisher.topic_path(self.project_id, self.topic_name)
self.subscription_path = self.subscriber.subscription_path(
self.project_id, self.subscription_name
)
else:
self.publisher = None
self.subscriber = None
def publish_federation_event(self, event: FederationEvent) -> str:
"""[FACT] Publish federation event to Pub/Sub topic.
Args:
event: Federation event to broadcast to all nodes
Returns:
Message ID from Pub/Sub publish
Example:
>>> pubsub = CloudPubSubFederation()
>>> event = FederationEvent(...)
>>> msg_id = pubsub.publish_federation_event(event)
"""
if not self.publisher:
# [HYPOTHESIS] Fallback to local logging when GCP unavailable
return self._local_fallback_publish(event)
message_data = json.dumps(asdict(event)).encode("utf-8")
# [FACT] Pub/Sub publish with retry policy
future = self.publisher.publish(
self.topic_path,
message_data,
node_id=event.node_id,
event_type=event.event_type,
receipt_hash=event.receipt_hash,
)
return str(future.result())
def _local_fallback_publish(self, event: FederationEvent) -> str:
"""[FACT] Local fallback when GCP Pub/Sub unavailable."""
print(f"[LOCAL MODE] Federation event: {event.event_id}")
return f"local-{event.event_id}"
def subscribe_to_federation(self, callback: Callable[[FederationEvent], None]) -> None:
"""[FACT] Subscribe to federation events from all nodes.
Args:
callback: Function to process received events
"""
if not self.subscriber:
print("[LOCAL MODE] Pub/Sub subscription not available")
return
def _message_handler(message: Any) -> None:
data = json.loads(message.data.decode("utf-8"))
event = FederationEvent(**data)
callback(event)
message.ack()
streaming_pull_future = self.subscriber.subscribe(
self.subscription_path, callback=_message_handler
)
try:
streaming_pull_future.result()
except KeyboardInterrupt:
streaming_pull_future.cancel()
class CloudStorageReceipts:
"""[FACT] Cloud Storage integration for immutable receipt archival.
[HYPOTHESIS] GCS provides WORM (Write Once Read Many) semantics required
for non-repudiable constitutional audit trails.
Google Cloud Service: Cloud Storage
Documentation: https://cloud.google.com/storage/docs
"""
def __init__(self, bucket_name: str | None = None):
self.bucket_name = bucket_name or os.getenv("GCS_RECEIPT_BUCKET")
if GCP_AVAILABLE and self.bucket_name:
self.client = storage.Client()
self.bucket = self.client.bucket(self.bucket_name)
else:
self.client = None
self.bucket = None
def store_receipt(self, receipt_id: str, receipt_data: dict) -> str:
"""[FACT] Store cryptographic receipt in GCS with versioning.
Args:
receipt_id: Unique receipt identifier
receipt_data: Signed receipt payload
Returns:
GCS URI of stored receipt (gs://bucket/path)
Example:
>>> gcs = CloudStorageReceipts()
>>> uri = gcs.store_receipt('rcp_123', {...})
>>> print(uri) # gs://constitutional-receipts/2026/03/04/rcp_123.json
"""
if not self.client:
return self._local_fallback_store(receipt_id, receipt_data)
# [FACT] Organize receipts by date for efficient querying
today = datetime.utcnow().strftime("%Y/%m/%d")
safe_receipt_id = sanitize_receipt_id(receipt_id)
blob_path = f"receipts/{today}/{safe_receipt_id}.json"
blob = self.bucket.blob(blob_path)
# [FACT] Enable object versioning for audit compliance
blob.metadata = {
"receipt_id": sanitize_receipt_id(receipt_id),
"timestamp": receipt_data.get("timestamp", ""),
"node_id": receipt_data.get("node_id", ""),
"content_hash": receipt_data.get("content_hash", ""),
}
blob.upload_from_string(json.dumps(receipt_data, indent=2), content_type="application/json")
return f"gs://{self.bucket_name}/{blob_path}"
def retrieve_receipt(self, receipt_id: str) -> dict | None:
"""[FACT] Retrieve receipt from GCS by ID."""
if not self.client:
return None
# [HYPOTHESIS] Search across date prefixes for receipt
blobs = self.client.list_blobs(self.bucket_name, prefix="receipts/")
for blob in blobs:
if receipt_id in blob.name:
data = blob.download_as_string()
return cast(dict[Any, Any], json.loads(data))
return None
def list_receipts(self, limit: int | None = None) -> list[dict[str, Any]]:
"""[FACT] List receipt payloads from GCS, newest first by object update time."""
if not self.client:
return []
blobs = list(self.client.list_blobs(self.bucket_name, prefix="receipts/"))
blobs.sort(key=lambda blob: blob.updated or datetime.min)
if limit is not None:
blobs = blobs[-max(limit, 0) :]
receipts: list[dict[str, Any]] = []
for blob in blobs:
data = blob.download_as_string()
receipts.append(cast(dict[str, Any], json.loads(data)))
return receipts
def _local_fallback_store(self, receipt_id: str, receipt_data: dict) -> str:
"""[FACT] Local filesystem fallback for development."""
import os
safe_receipt_id = sanitize_receipt_id(receipt_id)
local_path = f"docs/receipts/gcs_fallback/{safe_receipt_id}.json"
os.makedirs(os.path.dirname(local_path), exist_ok=True)
with open(local_path, "w") as f:
json.dump(receipt_data, f, indent=2)
return f"file://{local_path}"
class SecretManagerDBC:
"""[FACT] Secret Manager integration for DBC encryption keys.
[HYPOTHESIS] Secret Manager provides hardware-backed encryption (HSM)
required for production DBC (Distributed Bearer Credential) security.
Google Cloud Service: Secret Manager
Documentation: https://cloud.google.com/secret-manager/docs
"""
def __init__(self, project_id: str | None = None):
self.project_id = project_id or os.getenv("GOOGLE_CLOUD_PROJECT")
if GCP_AVAILABLE and self.project_id:
self.client = secretmanager.SecretManagerServiceClient()
else:
self.client = None
def store_dbc_key(self, agent_name: str, key_material: bytes) -> str:
"""[FACT] Store DBC private key in Secret Manager.
Args:
agent_name: Name of the federated agent
key_material: Encrypted DBC private key bytes
Returns:
Secret version resource name
Example:
>>> secrets = SecretManagerDBC()
>>> version = secrets.store_dbc_key('KIMI', encrypted_key)
>>> print(version) # projects/123/secrets/dbc-key-KIMI/versions/1
"""
if not self.client:
return self._local_fallback_store(agent_name, key_material)
secret_id = f"dbc-key-{agent_name}"
parent = f"projects/{self.project_id}"
try:
# [FACT] Create secret if it doesn't exist
secret = self.client.create_secret(
request={
"parent": parent,
"secret_id": secret_id,
"secret": {
"replication": {"automatic": {}},
"labels": {"agent": agent_name, "type": "dbc-key"},
},
}
)
except Exception:
# [HYPOTHESIS] Secret may already exist
secret = self.client.get_secret(request={"name": f"{parent}/secrets/{secret_id}"})
# [FACT] Add new secret version with key material
version = self.client.add_secret_version(
request={"parent": secret.name, "payload": {"data": key_material}}
)
return str(version.name)
def retrieve_dbc_key(self, agent_name: str) -> bytes | None:
"""[FACT] Retrieve DBC private key from Secret Manager."""
if not self.client:
return self._local_fallback_retrieve(agent_name)
secret_id = f"dbc-key-{agent_name}"
name = f"projects/{self.project_id}/secrets/{secret_id}/versions/latest"
response = self.client.access_secret_version(request={"name": name})
return bytes(response.payload.data)
def _local_fallback_store(self, agent_name: str, key_material: bytes) -> str:
"""[FACT] Local fallback using environment variables."""
print(f"[LOCAL MODE] Would store DBC key for {agent_name}")
return f"local-{agent_name}"
def _local_fallback_retrieve(self, agent_name: str) -> bytes | None:
"""[FACT] Local fallback for key retrieval."""
key = os.getenv(f"HELIX_DBC_KEY_{agent_name.upper()}")
return key.encode() if key else None
class CloudAuditLogger:
"""[FACT] Cloud Logging integration for structured audit trails.
[HYPOTHESIS] Cloud Logging provides centralized, immutable audit logs
required for constitutional compliance verification.
Google Cloud Service: Cloud Logging
Documentation: https://cloud.google.com/logging/docs
"""
def __init__(self, log_name: str = "constitutional-guardian"):
self.log_name = log_name
if GCP_AVAILABLE:
self.client = cloud_logging.Client()
self.logger = self.client.logger(log_name)
else:
self.client = None
self.logger = None
def log_compliance_check(
self, session_id: str, text: str, result: dict, receipt_id: str | None = None
) -> None:
"""[FACT] Log constitutional compliance check to Cloud Logging.
Args:
session_id: Unique session identifier
text: Validated text content
result: Compliance check results
receipt_id: Associated receipt ID
Example:
>>> logger = CloudAuditLogger()
>>> logger.log_compliance_check(
... session_id="sess_123",
... text="[FACT] Water boils at 100C",
... result={"compliant": True, "markers": {...}},
... receipt_id="rcp_456"
... )
"""
log_entry = {
"event_type": "compliance_check",
"session_id": session_id,
"text_sample": text[:100] if text else "", # Truncate for privacy
"compliant": result.get("compliant", False),
"epistemic_markers": result.get("epistemic_markers", {}),
"agency_violations": result.get("agency_violations", []),
"receipt_id": sanitize_receipt_id(receipt_id),
"timestamp": datetime.utcnow().isoformat(),
"severity": "INFO" if result.get("compliant") else "WARNING",
}
if self.logger:
# [FACT] Structured logging with severity
self.logger.log_struct(log_entry)
else:
# [HYPOTHESIS] Fallback to stdout for local development
print(f"[AUDIT] {json.dumps(log_entry)}")
def log_drift_detection(
self, session_id: str, drift_type: str, severity: str, details: dict
) -> None:
"""[FACT] Log constitutional drift detection event."""
log_entry = {
"event_type": "drift_detection",
"session_id": session_id,
"drift_type": drift_type, # 'DRIFT-A', 'DRIFT-C', 'DRIFT-E'
"severity": severity,
"details": details,
"timestamp": datetime.utcnow().isoformat(),
}
if self.logger:
self.logger.log_struct(log_entry)
else:
print(f"[DRIFT ALERT] {json.dumps(log_entry)}")
def log_federation_event(self, event: FederationEvent) -> None:
"""[FACT] Log cross-node federation event."""
log_entry = {
"event_type": "federation",
"event_id": event.event_id,
"node_id": event.node_id,
"federation_event_type": event.event_type,
"receipt_hash": event.receipt_hash,
"timestamp": event.timestamp,
}
if self.logger:
self.logger.log_struct(log_entry)
else:
print(f"[FEDERATION] {json.dumps(log_entry)}")
class CloudRunDeployment:
"""[FACT] Cloud Run deployment configuration and health checks.
[HYPOTHESIS] Cloud Run provides the serverless, auto-scaling infrastructure
required for real-time constitutional guardians with zero operational overhead.
Google Cloud Service: Cloud Run
Documentation: https://cloud.google.com/run/docs
"""
def __init__(self, service_name: str = "constitutional-guardian"):
self.service_name = service_name
self.region = os.getenv("GOOGLE_CLOUD_REGION", "us-central1")
self.project_id = os.getenv("GOOGLE_CLOUD_PROJECT")
def get_deployment_info(self) -> dict:
"""[FACT] Return current Cloud Run deployment information.
Returns:
Dict with service URL, revision, and status
Example:
>>> deployment = CloudRunDeployment()
>>> info = deployment.get_deployment_info()
>>> print(info['url']) # https://constitutional-guardian-xyz-uc.a.run.app
"""
if not self.project_id:
return {
"status": "local_development",
"service": self.service_name,
"region": self.region,
}
# [FACT] Construct Cloud Run service URL
service_url = f"https://{self.service_name}-{self.project_id}-{self.region}.a.run.app"
return {
"status": "deployed",
"service": self.service_name,
"project": self.project_id,
"region": self.region,
"url": service_url,
"console_link": (
f"https://console.cloud.google.com/run/detail/{self.region}/{self.service_name}"
),
}
def generate_deploy_command(self) -> str:
"""[FACT] Generate gcloud command for deployment."""
return (
f"gcloud run deploy {self.service_name} \\\n"
f" --source . \\\n"
f" --region {self.region} \\\n"
f" --allow-unauthenticated \\\n"
f" --memory 1Gi \\\n"
f" --cpu 2 \\\n"
f" --port 8180 \\\n"
f' --set-env-vars="HELIX_NODE_ID=GCS-GUARDIAN"'
)
# [FACT] Convenience function for service initialization
def initialize_gcp_services() -> dict:
"""[FACT] Initialize all GCP services for Constitutional Guardian.
Returns:
Dict containing initialized service clients
Example:
>>> services = initialize_gcp_services()
>>> services['pubsub'].publish_federation_event(event)
>>> services['storage'].store_receipt('rcp_123', {...})
"""
return {
"pubsub": CloudPubSubFederation(),
"storage": CloudStorageReceipts(),
"secrets": SecretManagerDBC(),
"logging": CloudAuditLogger(),
"run": CloudRunDeployment(),
}