|
| 1 | +# Data Model: Artifact Content Composition |
| 2 | + |
| 3 | +**Feature**: INFP-504 | **Date**: 2026-03-20 |
| 4 | + |
| 5 | +## New Entities |
| 6 | + |
| 7 | +### ExecutionContext (Flag enum) |
| 8 | + |
| 9 | +**Location**: `infrahub_sdk/template/filters.py` |
| 10 | + |
| 11 | +```python |
| 12 | +class ExecutionContext(Flag): |
| 13 | + CORE = auto() # API server computed attributes — most restrictive |
| 14 | + WORKER = auto() # Prefect background workers |
| 15 | + LOCAL = auto() # Local CLI / unrestricted rendering |
| 16 | + ALL = CORE | WORKER | LOCAL |
| 17 | +``` |
| 18 | + |
| 19 | +**Semantics**: Represents where template code executes. A filter's `allowed_contexts` flags are an allowlist — fewer flags means less trusted. |
| 20 | + |
| 21 | +### FilterDefinition (modified) |
| 22 | + |
| 23 | +**Location**: `infrahub_sdk/template/filters.py` |
| 24 | + |
| 25 | +```python |
| 26 | +@dataclass |
| 27 | +class FilterDefinition: |
| 28 | + name: str |
| 29 | + allowed_contexts: ExecutionContext |
| 30 | + source: str |
| 31 | + |
| 32 | + @property |
| 33 | + def trusted(self) -> bool: |
| 34 | + """Backward compatibility: trusted means allowed in all contexts.""" |
| 35 | + return self.allowed_contexts == ExecutionContext.ALL |
| 36 | +``` |
| 37 | + |
| 38 | +**Migration**: |
| 39 | + |
| 40 | +| Current | New | |
| 41 | +| ------- | --- | |
| 42 | +| `FilterDefinition("abs", trusted=True, source="jinja2")` | `FilterDefinition("abs", allowed_contexts=ExecutionContext.ALL, source="jinja2")` | |
| 43 | +| `FilterDefinition("safe", trusted=False, source="jinja2")` | `FilterDefinition("safe", allowed_contexts=ExecutionContext.LOCAL, source="jinja2")` | |
| 44 | + |
| 45 | +### JinjaFilterError (new exception) |
| 46 | + |
| 47 | +**Location**: `infrahub_sdk/template/exceptions.py` |
| 48 | + |
| 49 | +```python |
| 50 | +class JinjaFilterError(JinjaTemplateError): |
| 51 | + def __init__(self, filter_name: str, message: str, hint: str | None = None) -> None: |
| 52 | + self.filter_name = filter_name |
| 53 | + self.hint = hint |
| 54 | + full_message = f"Filter '{filter_name}': {message}" |
| 55 | + if hint: |
| 56 | + full_message += f" — {hint}" |
| 57 | + super().__init__(full_message) |
| 58 | +``` |
| 59 | + |
| 60 | +**Inheritance**: `Error` → `JinjaTemplateError` → `JinjaFilterError` |
| 61 | + |
| 62 | +### InfrahubFilters (new class) |
| 63 | + |
| 64 | +**Location**: `infrahub_sdk/template/infrahub_filters.py` (new file) |
| 65 | + |
| 66 | +```python |
| 67 | +class InfrahubFilters: |
| 68 | + def __init__(self, client: InfrahubClient) -> None: |
| 69 | + self.client = client |
| 70 | + |
| 71 | + async def artifact_content(self, storage_id: str) -> str: |
| 72 | + """Retrieve artifact content by storage_id.""" |
| 73 | + ... |
| 74 | + |
| 75 | + async def file_object_content(self, storage_id: str) -> str: |
| 76 | + """Retrieve file object content by storage_id.""" |
| 77 | + ... |
| 78 | +``` |
| 79 | + |
| 80 | +**Key design decisions**: |
| 81 | + |
| 82 | +- Methods are `async` — Jinja2's `auto_await` handles them in async rendering mode |
| 83 | +- Holds an `InfrahubClient` (async only), not `InfrahubClientSync` |
| 84 | +- Each method validates inputs and catches `AuthenticationError` to wrap in `JinjaFilterError` |
| 85 | + |
| 86 | +## Modified Entities |
| 87 | + |
| 88 | +### Jinja2Template (modified constructor) |
| 89 | + |
| 90 | +**Location**: `infrahub_sdk/template/__init__.py` |
| 91 | + |
| 92 | +```python |
| 93 | +def __init__( |
| 94 | + self, |
| 95 | + template: str | Path, |
| 96 | + template_directory: Path | None = None, |
| 97 | + filters: dict[str, Callable] | None = None, |
| 98 | + client: InfrahubClient | None = None, # NEW |
| 99 | +) -> None: |
| 100 | +``` |
| 101 | + |
| 102 | +**Changes**: |
| 103 | + |
| 104 | +- New optional `client` parameter |
| 105 | +- When `client` provided: instantiate `InfrahubFilters`, register `artifact_content` and `file_object_content` |
| 106 | +- Always register `from_json` and `from_yaml` (no client needed) |
| 107 | +- File-based environment must add `enable_async=True` for async filter support |
| 108 | + |
| 109 | +### Jinja2Template.validate() (modified signature) |
| 110 | + |
| 111 | +```python |
| 112 | +def validate(self, restricted: bool = True, context: ExecutionContext | None = None) -> None: |
| 113 | +``` |
| 114 | + |
| 115 | +**Changes**: |
| 116 | + |
| 117 | +- New optional `context` parameter (takes precedence over `restricted` when provided) |
| 118 | +- Backward compat: `restricted=True` → `ExecutionContext.CORE`, `restricted=False` → `ExecutionContext.LOCAL` |
| 119 | +- Validation logic: filter allowed if `filter.allowed_contexts & context` is truthy |
| 120 | + |
| 121 | +### ObjectStore (new method) |
| 122 | + |
| 123 | +**Location**: `infrahub_sdk/object_store.py` |
| 124 | + |
| 125 | +```python |
| 126 | +async def get_file_by_storage_id(self, storage_id: str, tracker: str | None = None) -> str: |
| 127 | + """Retrieve file object content by storage_id. |
| 128 | +
|
| 129 | + Raises error if content-type is not text-based. |
| 130 | + """ |
| 131 | + ... |
| 132 | +``` |
| 133 | + |
| 134 | +**API endpoint**: `GET /api/files/by-storage-id/{storage_id}` |
| 135 | + |
| 136 | +**Content-type check**: Allow `text/*`, `application/json`, `application/yaml`, `application/x-yaml`. Reject all others. |
| 137 | + |
| 138 | +## New Filter Registrations |
| 139 | + |
| 140 | +```python |
| 141 | +# In AVAILABLE_FILTERS: |
| 142 | + |
| 143 | +# Infrahub client-dependent filters (worker context only) |
| 144 | +FilterDefinition("artifact_content", allowed_contexts=ExecutionContext.WORKER, source="infrahub"), |
| 145 | +FilterDefinition("file_object_content", allowed_contexts=ExecutionContext.WORKER, source="infrahub"), |
| 146 | + |
| 147 | +# Parsing filters (trusted, all contexts) |
| 148 | +FilterDefinition("from_json", allowed_contexts=ExecutionContext.ALL, source="infrahub"), |
| 149 | +FilterDefinition("from_yaml", allowed_contexts=ExecutionContext.ALL, source="infrahub"), |
| 150 | +``` |
| 151 | + |
| 152 | +## Relationships |
| 153 | + |
| 154 | +```text |
| 155 | +Jinja2Template |
| 156 | + ├── has-a → InfrahubFilters (when client provided) |
| 157 | + ├── uses → FilterDefinition registry (for validation) |
| 158 | + └── uses → ExecutionContext (for context-aware validation) |
| 159 | +
|
| 160 | +InfrahubFilters |
| 161 | + ├── has-a → InfrahubClient |
| 162 | + └── uses → ObjectStore (for content retrieval) |
| 163 | +
|
| 164 | +JinjaFilterError |
| 165 | + └── extends → JinjaTemplateError → Error |
| 166 | +``` |
0 commit comments