|
| 1 | +import json |
| 2 | +from dataclasses import dataclass, field |
| 3 | +from urllib.request import Request, urlopen |
| 4 | + |
| 5 | +from django.conf import settings |
| 6 | + |
| 7 | + |
| 8 | +class CatalogRelatedList: |
| 9 | + def __init__(self, items=None): |
| 10 | + self._items = list(items or []) |
| 11 | + |
| 12 | + def all(self): |
| 13 | + return list(self._items) |
| 14 | + |
| 15 | + def count(self): |
| 16 | + return len(self._items) |
| 17 | + |
| 18 | + def add(self, item): |
| 19 | + self._items.append(item) |
| 20 | + |
| 21 | + def __iter__(self): |
| 22 | + return iter(self._items) |
| 23 | + |
| 24 | + def __len__(self): |
| 25 | + return len(self._items) |
| 26 | + |
| 27 | + |
| 28 | +@dataclass(frozen=True) |
| 29 | +class CatalogQuestionBridgeItem: |
| 30 | + id: int |
| 31 | + domain_id: int | None |
| 32 | + code: str |
| 33 | + text: str |
| 34 | + help_text: str |
| 35 | + why_it_matters: str |
| 36 | + question_kind: str |
| 37 | + question_kind_label: str |
| 38 | + wizard_step: str |
| 39 | + wizard_step_label: str |
| 40 | + weight: int |
| 41 | + is_required: bool |
| 42 | + applies_to_iso27001: bool |
| 43 | + applies_to_nis2: bool |
| 44 | + applies_to_cra: bool |
| 45 | + applies_to_ai_act: bool |
| 46 | + applies_to_iec62443: bool |
| 47 | + applies_to_iso_sae_21434: bool |
| 48 | + applies_to_product_security: bool |
| 49 | + sort_order: int |
| 50 | + created_at: str |
| 51 | + updated_at: str |
| 52 | + |
| 53 | + @property |
| 54 | + def pk(self) -> int: |
| 55 | + return self.id |
| 56 | + |
| 57 | + def get_question_kind_display(self) -> str: |
| 58 | + return self.question_kind_label |
| 59 | + |
| 60 | + def get_wizard_step_display(self) -> str: |
| 61 | + return self.wizard_step_label |
| 62 | + |
| 63 | + |
| 64 | +@dataclass |
| 65 | +class CatalogDomainBridgeItem: |
| 66 | + id: int |
| 67 | + code: str |
| 68 | + name: str |
| 69 | + description: str |
| 70 | + weight: int |
| 71 | + sort_order: int |
| 72 | + question_count: int |
| 73 | + created_at: str |
| 74 | + updated_at: str |
| 75 | + questions: CatalogRelatedList = field(default_factory=CatalogRelatedList) |
| 76 | + |
| 77 | + @property |
| 78 | + def pk(self) -> int: |
| 79 | + return self.id |
| 80 | + |
| 81 | + def __str__(self) -> str: |
| 82 | + return self.name |
| 83 | + |
| 84 | + |
| 85 | +@dataclass(frozen=True) |
| 86 | +class CatalogDomainLibraryBridgeResult: |
| 87 | + domains: list[CatalogDomainBridgeItem] |
| 88 | + question_count: int |
| 89 | + |
| 90 | + |
| 91 | +class CatalogRustClient: |
| 92 | + @staticmethod |
| 93 | + def _base_url() -> str: |
| 94 | + base = (getattr(settings, 'RUST_BACKEND_URL', '') or '').strip() |
| 95 | + return base.rstrip('/') |
| 96 | + |
| 97 | + @staticmethod |
| 98 | + def fetch_domain_library(request, timeout: int = 8) -> CatalogDomainLibraryBridgeResult: |
| 99 | + base = CatalogRustClient._base_url() |
| 100 | + if not base: |
| 101 | + raise RuntimeError('RUST_BACKEND_URL ist nicht gesetzt.') |
| 102 | + |
| 103 | + rust_request = CatalogRustClient._authenticated_request(request, f'{base}/api/v1/catalog/domains') |
| 104 | + with urlopen(rust_request, timeout=timeout) as response: |
| 105 | + payload = json.loads(response.read().decode('utf-8')) |
| 106 | + |
| 107 | + domains = [ |
| 108 | + CatalogRustClient._domain_from_payload(item) |
| 109 | + for item in payload.get('domains', []) |
| 110 | + if isinstance(item, dict) |
| 111 | + ] |
| 112 | + return CatalogDomainLibraryBridgeResult( |
| 113 | + domains=domains, |
| 114 | + question_count=int(payload.get('question_count') or 0), |
| 115 | + ) |
| 116 | + |
| 117 | + @staticmethod |
| 118 | + def _domain_from_payload(item: dict) -> CatalogDomainBridgeItem: |
| 119 | + domain = CatalogDomainBridgeItem( |
| 120 | + id=int(item.get('id') or 0), |
| 121 | + code=str(item.get('code') or ''), |
| 122 | + name=str(item.get('name') or ''), |
| 123 | + description=str(item.get('description') or ''), |
| 124 | + weight=int(item.get('weight') or 0), |
| 125 | + sort_order=int(item.get('sort_order') or 0), |
| 126 | + question_count=int(item.get('question_count') or 0), |
| 127 | + created_at=str(item.get('created_at') or ''), |
| 128 | + updated_at=str(item.get('updated_at') or ''), |
| 129 | + ) |
| 130 | + for question_payload in item.get('questions', []): |
| 131 | + if isinstance(question_payload, dict): |
| 132 | + domain.questions.add(CatalogRustClient._question_from_payload(question_payload)) |
| 133 | + return domain |
| 134 | + |
| 135 | + @staticmethod |
| 136 | + def _question_from_payload(item: dict) -> CatalogQuestionBridgeItem: |
| 137 | + return CatalogQuestionBridgeItem( |
| 138 | + id=int(item.get('id') or 0), |
| 139 | + domain_id=CatalogRustClient._optional_int(item.get('domain_id')), |
| 140 | + code=str(item.get('code') or ''), |
| 141 | + text=str(item.get('text') or ''), |
| 142 | + help_text=str(item.get('help_text') or ''), |
| 143 | + why_it_matters=str(item.get('why_it_matters') or ''), |
| 144 | + question_kind=str(item.get('question_kind') or ''), |
| 145 | + question_kind_label=str(item.get('question_kind_label') or ''), |
| 146 | + wizard_step=str(item.get('wizard_step') or ''), |
| 147 | + wizard_step_label=str(item.get('wizard_step_label') or ''), |
| 148 | + weight=int(item.get('weight') or 0), |
| 149 | + is_required=bool(item.get('is_required')), |
| 150 | + applies_to_iso27001=bool(item.get('applies_to_iso27001')), |
| 151 | + applies_to_nis2=bool(item.get('applies_to_nis2')), |
| 152 | + applies_to_cra=bool(item.get('applies_to_cra')), |
| 153 | + applies_to_ai_act=bool(item.get('applies_to_ai_act')), |
| 154 | + applies_to_iec62443=bool(item.get('applies_to_iec62443')), |
| 155 | + applies_to_iso_sae_21434=bool(item.get('applies_to_iso_sae_21434')), |
| 156 | + applies_to_product_security=bool(item.get('applies_to_product_security')), |
| 157 | + sort_order=int(item.get('sort_order') or 0), |
| 158 | + created_at=str(item.get('created_at') or ''), |
| 159 | + updated_at=str(item.get('updated_at') or ''), |
| 160 | + ) |
| 161 | + |
| 162 | + @staticmethod |
| 163 | + def _authenticated_request(request, url: str) -> Request: |
| 164 | + user = getattr(request, 'user', None) |
| 165 | + user_id = getattr(user, 'id', None) |
| 166 | + tenant_id = getattr(getattr(user, 'tenant', None), 'id', None) |
| 167 | + if not user_id or not tenant_id: |
| 168 | + raise RuntimeError('Catalog-Rust-Bridge braucht authentifizierten Tenant-Kontext.') |
| 169 | + |
| 170 | + rust_request = Request(url) |
| 171 | + rust_request.add_header('Accept', 'application/json') |
| 172 | + rust_request.add_header('X-ISCY-Tenant-ID', str(tenant_id)) |
| 173 | + rust_request.add_header('X-ISCY-User-ID', str(user_id)) |
| 174 | + user_email = (getattr(user, 'email', '') or '').strip() |
| 175 | + if user_email: |
| 176 | + rust_request.add_header('X-ISCY-User-Email', user_email) |
| 177 | + return rust_request |
| 178 | + |
| 179 | + @staticmethod |
| 180 | + def _optional_int(value) -> int | None: |
| 181 | + if value in (None, ''): |
| 182 | + return None |
| 183 | + return int(value) |
| 184 | + |
| 185 | + |
| 186 | +class CatalogBridge: |
| 187 | + @staticmethod |
| 188 | + def fetch_domain_library(request): |
| 189 | + backend = str(getattr(settings, 'CATALOG_BACKEND', 'rust_service') or '').strip().lower() |
| 190 | + if backend != 'rust_service': |
| 191 | + return None |
| 192 | + |
| 193 | + if not CatalogRustClient._base_url(): |
| 194 | + if CatalogBridge._strict_rust_mode(): |
| 195 | + raise RuntimeError('Rust catalog backend ist aktiv, aber RUST_BACKEND_URL ist nicht gesetzt.') |
| 196 | + return None |
| 197 | + |
| 198 | + try: |
| 199 | + return CatalogRustClient.fetch_domain_library(request) |
| 200 | + except Exception as exc: |
| 201 | + if CatalogBridge._strict_rust_mode(): |
| 202 | + raise RuntimeError('Rust catalog backend ist aktiv, aber nicht erreichbar.') from exc |
| 203 | + return None |
| 204 | + |
| 205 | + @staticmethod |
| 206 | + def _strict_rust_mode() -> bool: |
| 207 | + return bool(getattr(settings, 'RUST_STRICT_MODE', False)) |
0 commit comments