Buckets:

download
raw
19.4 kB
"""
Shared fixtures and models for Integration Config API tests.
Constitutional Hash: 608508a9bd224290
Phase 10 Task 7: Enterprise Integration Configuration API
"""
import base64
import secrets
from dataclasses import dataclass, field
from datetime import UTC, datetime, timezone
from enum import Enum
from typing import Any, ClassVar, Optional
import pytest
# Constitutional Hash for all operations
from enhanced_agent_bus._compat.constants import CONSTITUTIONAL_HASH
from enhanced_agent_bus._compat.types import JSONDict
# ============================================================================
# Integration Types and Models
# ============================================================================
class IntegrationType(Enum):
"""Types of enterprise integrations."""
LDAP = "ldap"
SAML = "saml"
OIDC = "oidc"
KAFKA = "kafka"
SIEM = "siem"
DATA_WAREHOUSE = "data_warehouse"
VAULT = "vault"
class IntegrationStatus(Enum):
"""Status of an integration."""
ACTIVE = "active"
INACTIVE = "inactive"
TESTING = "testing"
ERROR = "error"
PENDING = "pending"
@dataclass
class IntegrationConfig:
"""Configuration for an enterprise integration.
Constitutional Hash: 608508a9bd224290
"""
id: str
tenant_id: str
integration_type: IntegrationType
name: str
config: JSONDict # Contains encrypted sensitive data
status: IntegrationStatus = IntegrationStatus.PENDING
enabled: bool = True
last_health_check: datetime | None = None
health_status: str | None = None
created_at: datetime = field(default_factory=lambda: datetime.now(UTC))
updated_at: datetime = field(default_factory=lambda: datetime.now(UTC))
created_by: str | None = None
# Encrypted fields tracking
encrypted_fields: set[str] = field(default_factory=set)
def to_dict(self, redact_sensitive: bool = True) -> JSONDict:
"""Serialize to dictionary, optionally redacting sensitive fields."""
config_data = {}
for key, value in self.config.items():
if redact_sensitive and key in self.encrypted_fields:
config_data[key] = "********" # Redacted
else:
config_data[key] = value
return {
"id": self.id,
"tenant_id": self.tenant_id,
"integration_type": self.integration_type.value,
"name": self.name,
"config": config_data,
"status": self.status.value,
"enabled": self.enabled,
"last_health_check": (
self.last_health_check.isoformat() if self.last_health_check else None
),
"health_status": self.health_status,
"created_at": self.created_at.isoformat(),
"updated_at": self.updated_at.isoformat(),
"created_by": self.created_by,
"constitutional_hash": CONSTITUTIONAL_HASH,
}
class EncryptionService:
"""Service for encrypting/decrypting sensitive configuration data.
Constitutional Hash: 608508a9bd224290
"""
def __init__(self, vault_client: Any | None = None):
"""Initialize with optional Vault client."""
self._vault_client = vault_client
self._local_key = secrets.token_bytes(32) # Fallback local key
self._key_id = "local-dev-key"
async def encrypt(self, plaintext: str, key_id: str | None = None) -> dict[str, str]:
"""Encrypt plaintext data."""
if self._vault_client:
# Use Vault transit engine
return await self._vault_encrypt(plaintext, key_id)
else:
# Local encryption for development
return self._local_encrypt(plaintext)
async def decrypt(self, ciphertext: str, key_id: str | None = None) -> str:
"""Decrypt ciphertext data."""
if self._vault_client:
return await self._vault_decrypt(ciphertext, key_id)
else:
return self._local_decrypt(ciphertext)
def _local_encrypt(self, plaintext: str) -> dict[str, str]:
"""Local encryption using simple XOR (dev only)."""
data = plaintext.encode("utf-8")
key = self._local_key[: len(data)] * (len(data) // len(self._local_key) + 1)
encrypted = bytes(a ^ b for a, b in zip(data, key[: len(data)], strict=False))
return {
"ciphertext": base64.b64encode(encrypted).decode("utf-8"),
"key_id": self._key_id,
}
def _local_decrypt(self, ciphertext: str) -> str:
"""Local decryption."""
encrypted = base64.b64decode(ciphertext)
key = self._local_key[: len(encrypted)] * (len(encrypted) // len(self._local_key) + 1)
decrypted = bytes(a ^ b for a, b in zip(encrypted, key[: len(encrypted)], strict=False))
return decrypted.decode("utf-8")
async def _vault_encrypt(self, plaintext: str, key_id: str | None) -> dict[str, str]:
"""Encrypt using Vault transit engine."""
key_name = key_id or "acgs-integration-key"
result = await self._vault_client.secrets.transit.encrypt_data(
name=key_name,
plaintext=base64.b64encode(plaintext.encode()).decode(),
)
return {
"ciphertext": result["data"]["ciphertext"],
"key_id": key_name,
}
async def _vault_decrypt(self, ciphertext: str, key_id: str | None) -> str:
"""Decrypt using Vault transit engine."""
key_name = key_id or "acgs-integration-key"
result = await self._vault_client.secrets.transit.decrypt_data(
name=key_name,
ciphertext=ciphertext,
)
return base64.b64decode(result["data"]["plaintext"]).decode()
class IntegrationHealthChecker:
"""Health checker for enterprise integrations.
Constitutional Hash: 608508a9bd224290
"""
async def check_ldap(self, config: JSONDict) -> JSONDict:
"""Check LDAP integration health."""
return {
"healthy": True,
"latency_ms": 5.2,
"message": "LDAP connection successful",
}
async def check_saml(self, config: JSONDict) -> JSONDict:
"""Check SAML integration health."""
return {
"healthy": True,
"latency_ms": 12.3,
"message": "SAML IdP metadata accessible",
}
async def check_oidc(self, config: JSONDict) -> JSONDict:
"""Check OIDC integration health."""
return {
"healthy": True,
"latency_ms": 8.1,
"message": "OIDC discovery endpoint reachable",
}
async def check_kafka(self, config: JSONDict) -> JSONDict:
"""Check Kafka integration health."""
return {
"healthy": True,
"latency_ms": 15.0,
"message": "Kafka brokers reachable",
}
async def check_health(self, integration_type: IntegrationType, config: JSONDict) -> JSONDict:
"""Check health for any integration type."""
checkers = {
IntegrationType.LDAP: self.check_ldap,
IntegrationType.SAML: self.check_saml,
IntegrationType.OIDC: self.check_oidc,
IntegrationType.KAFKA: self.check_kafka,
}
checker = checkers.get(integration_type)
if checker:
return await checker(config)
return {"healthy": False, "message": "Unknown integration type"}
class AuditService:
"""Audit logging service for integration operations.
Constitutional Hash: 608508a9bd224290
"""
def __init__(self):
self._audit_log: list[JSONDict] = []
def log(
self,
action: str,
tenant_id: str,
integration_id: str,
actor: str | None = None,
details: JSONDict | None = None,
) -> JSONDict:
"""Log an audit event."""
entry = {
"id": f"audit-{len(self._audit_log) + 1}",
"action": action,
"tenant_id": tenant_id,
"integration_id": integration_id,
"actor": actor,
"details": details or {},
"timestamp": datetime.now(UTC).isoformat(),
"constitutional_hash": CONSTITUTIONAL_HASH,
}
self._audit_log.append(entry)
return entry
def get_logs(
self,
tenant_id: str | None = None,
integration_id: str | None = None,
) -> list[JSONDict]:
"""Get audit logs with optional filters."""
logs = self._audit_log
if tenant_id:
logs = [log for log in logs if log["tenant_id"] == tenant_id]
if integration_id:
logs = [log for log in logs if log["integration_id"] == integration_id]
return logs
class IntegrationConfigService:
"""Service for managing enterprise integration configurations.
Constitutional Hash: 608508a9bd224290
Provides:
- CRUD operations for integrations
- Encryption of sensitive configuration
- Health checking and monitoring
- Audit logging
"""
# Sensitive fields that should be encrypted
SENSITIVE_FIELDS: ClassVar[set] = {
"password",
"secret",
"api_key",
"client_secret",
"private_key",
"certificate",
"token",
"credentials",
}
def __init__(
self,
encryption_service: EncryptionService | None = None,
health_checker: IntegrationHealthChecker | None = None,
audit_service: AuditService | None = None,
constitutional_hash: str = CONSTITUTIONAL_HASH,
):
"""Initialize the service."""
if constitutional_hash != CONSTITUTIONAL_HASH:
raise ValueError(f"Invalid constitutional hash: {constitutional_hash}")
self._integrations: dict[str, IntegrationConfig] = {}
self._encryption = encryption_service or EncryptionService()
self._health_checker = health_checker or IntegrationHealthChecker()
self._audit = audit_service or AuditService()
self._constitutional_hash = constitutional_hash
self._next_id = 1
async def create_integration(
self,
tenant_id: str,
integration_type: IntegrationType,
name: str,
config: JSONDict,
created_by: str | None = None,
) -> IntegrationConfig:
"""Create a new integration with encrypted sensitive data."""
integration_id = f"int-{self._next_id}"
self._next_id += 1
# Encrypt sensitive fields
encrypted_config = {}
encrypted_fields = set()
for key, value in config.items():
if self._is_sensitive_field(key):
encrypted = await self._encryption.encrypt(str(value))
encrypted_config[key] = encrypted["ciphertext"]
encrypted_fields.add(key)
else:
encrypted_config[key] = value
integration = IntegrationConfig(
id=integration_id,
tenant_id=tenant_id,
integration_type=integration_type,
name=name,
config=encrypted_config,
encrypted_fields=encrypted_fields,
status=IntegrationStatus.PENDING,
created_by=created_by,
)
self._integrations[integration_id] = integration
# Audit log
self._audit.log(
action="create",
tenant_id=tenant_id,
integration_id=integration_id,
actor=created_by,
details={"integration_type": integration_type.value, "name": name},
)
return integration
def get_integration(
self,
integration_id: str,
tenant_id: str | None = None,
) -> IntegrationConfig | None:
"""Get an integration by ID."""
integration = self._integrations.get(integration_id)
if integration and tenant_id and integration.tenant_id != tenant_id:
return None # Tenant isolation
return integration
def list_integrations(
self,
tenant_id: str,
integration_type: IntegrationType | None = None,
status: IntegrationStatus | None = None,
enabled_only: bool = False,
) -> list[IntegrationConfig]:
"""list integrations for a tenant."""
integrations = [i for i in self._integrations.values() if i.tenant_id == tenant_id]
if integration_type:
integrations = [i for i in integrations if i.integration_type == integration_type]
if status:
integrations = [i for i in integrations if i.status == status]
if enabled_only:
integrations = [i for i in integrations if i.enabled]
return sorted(integrations, key=lambda i: i.name)
async def update_integration(
self,
integration_id: str,
tenant_id: str,
updates: JSONDict,
updated_by: str | None = None,
) -> IntegrationConfig | None:
"""Update an integration configuration."""
integration = self.get_integration(integration_id, tenant_id)
if not integration:
return None
# Update allowed fields
if "name" in updates:
integration.name = updates["name"]
if "enabled" in updates:
integration.enabled = updates["enabled"]
if "status" in updates:
integration.status = IntegrationStatus(updates["status"])
if "config" in updates:
# Encrypt any new sensitive fields
new_config = updates["config"]
for key, value in new_config.items():
if self._is_sensitive_field(key):
encrypted = await self._encryption.encrypt(str(value))
integration.config[key] = encrypted["ciphertext"]
integration.encrypted_fields.add(key)
else:
integration.config[key] = value
integration.updated_at = datetime.now(UTC)
# Audit log
self._audit.log(
action="update",
tenant_id=tenant_id,
integration_id=integration_id,
actor=updated_by,
details={"updated_fields": list(updates.keys())},
)
return integration
async def delete_integration(
self,
integration_id: str,
tenant_id: str,
deleted_by: str | None = None,
) -> bool:
"""Delete an integration with audit archival."""
integration = self.get_integration(integration_id, tenant_id)
if not integration:
return False
# Archive to audit before deletion
self._audit.log(
action="delete",
tenant_id=tenant_id,
integration_id=integration_id,
actor=deleted_by,
details={
"integration_type": integration.integration_type.value,
"name": integration.name,
"archived_config": integration.to_dict(redact_sensitive=True),
},
)
del self._integrations[integration_id]
return True
async def test_connectivity(
self,
integration_id: str,
tenant_id: str,
) -> JSONDict:
"""Test connectivity for an integration."""
integration = self.get_integration(integration_id, tenant_id)
if not integration:
return {"success": False, "error": "Integration not found"}
try:
result = await self._health_checker.check_health(
integration.integration_type,
integration.config,
)
# Update health status
integration.last_health_check = datetime.now(UTC)
integration.health_status = "healthy" if result.get("healthy") else "unhealthy"
if result.get("healthy"):
integration.status = IntegrationStatus.ACTIVE
else:
integration.status = IntegrationStatus.ERROR
return {
"success": result.get("healthy", False),
"latency_ms": result.get("latency_ms"),
"message": result.get("message"),
"integration_id": integration_id,
"timestamp": datetime.now(UTC).isoformat(),
}
except (RuntimeError, ValueError, TypeError, AssertionError) as e:
integration.status = IntegrationStatus.ERROR
integration.health_status = f"error: {str(e)}"
return {
"success": False,
"error": str(e),
"integration_id": integration_id,
}
async def check_all_health(self, tenant_id: str) -> list[JSONDict]:
"""Check health of all integrations for a tenant."""
integrations = self.list_integrations(tenant_id, enabled_only=True)
results = []
for integration in integrations:
result = await self.test_connectivity(integration.id, tenant_id)
results.append(
{
"integration_id": integration.id,
"name": integration.name,
"type": integration.integration_type.value,
**result,
}
)
return results
def get_health_summary(self, tenant_id: str) -> JSONDict:
"""Get health summary for a tenant's integrations."""
integrations = self.list_integrations(tenant_id)
healthy = sum(1 for i in integrations if i.health_status == "healthy")
unhealthy = sum(1 for i in integrations if i.health_status == "unhealthy")
unknown = sum(1 for i in integrations if i.health_status is None)
return {
"total": len(integrations),
"healthy": healthy,
"unhealthy": unhealthy,
"unknown": unknown,
"tenant_id": tenant_id,
"timestamp": datetime.now(UTC).isoformat(),
"constitutional_hash": CONSTITUTIONAL_HASH,
}
def _is_sensitive_field(self, field_name: str) -> bool:
"""Check if a field contains sensitive data."""
field_lower = field_name.lower()
return any(s in field_lower for s in self.SENSITIVE_FIELDS)
def get_audit_logs(
self,
tenant_id: str,
integration_id: str | None = None,
) -> list[JSONDict]:
"""Get audit logs for integrations."""
return self._audit.get_logs(tenant_id, integration_id)
# ============================================================================
# Fixtures
# ============================================================================
@pytest.fixture
def encryption_service():
"""Create encryption service for testing."""
return EncryptionService()
@pytest.fixture
def health_checker():
"""Create health checker for testing."""
return IntegrationHealthChecker()
@pytest.fixture
def audit_service():
"""Create audit service for testing."""
return AuditService()
@pytest.fixture
def integration_service(encryption_service, health_checker, audit_service):
"""Create integration config service for testing."""
return IntegrationConfigService(
encryption_service=encryption_service,
health_checker=health_checker,
audit_service=audit_service,
)

Xet Storage Details

Size:
19.4 kB
·
Xet hash:
2ec990e70f648c38eb2599fbf217fc9385a6fa60d7639cac552534c57efd9034

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.