Skip to content

Commit ec6fc45

Browse files
committed
docs(auditlog_ng): document automatic tenant and user injection via IAS middleware
1 parent 3e53758 commit ec6fc45

1 file changed

Lines changed: 51 additions & 0 deletions

File tree

src/sap_cloud_sdk/core/auditlog_ng/user-guide.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,8 @@ event.object_type = "resource"
168168
event.object_id = "resource-001"
169169
```
170170

171+
> **Tip:** When using `StarletteIASTelemetryMiddleware` (see [Automatic tenant and user injection](#automatic-tenant-and-user-injection)), `common.tenant_id` and `common.user_initiator_id` are filled automatically from the incoming IAS JWT. You only need to set them explicitly if you want to override the values from the token.
172+
171173
### Step 4: Send the Event
172174

173175
**Binary protobuf:**
@@ -284,6 +286,55 @@ Events are validated against protobuf constraints using `protovalidate` before s
284286

285287
---
286288

289+
## Automatic tenant and user injection
290+
291+
When `StarletteIASTelemetryMiddleware` is registered on your app, it parses the
292+
incoming `Authorization: Bearer <token>` header on every request and stores the
293+
IAS claims in the current async context.
294+
295+
`AuditClient.send()` reads that context automatically before validation and
296+
back-fills two fields on the event's `common` block — only if they are not
297+
already set by the caller:
298+
299+
| Field populated | IAS claim used |
300+
|---|---|
301+
| `common.tenant_id` | `app_tid` |
302+
| `common.user_initiator_id` | `user_uuid` |
303+
304+
### Setup
305+
306+
Register the middleware once when your app starts:
307+
308+
```python
309+
from sap_cloud_sdk.core.telemetry import auto_instrument
310+
from sap_cloud_sdk.core.telemetry.middleware import StarletteIASTelemetryMiddleware
311+
312+
app = FastAPI(...)
313+
auto_instrument(middlewares=[StarletteIASTelemetryMiddleware(app=app)])
314+
```
315+
316+
### Usage
317+
318+
With the middleware in place, you can omit `tenant_id` and `user_initiator_id`
319+
from every event — they are injected automatically:
320+
321+
```python
322+
event = pb.DataAccess()
323+
event.common.timestamp.FromDatetime(datetime.now(timezone.utc))
324+
# tenant_id and user_initiator_id are filled from the IAS JWT automatically
325+
event.channel_type = "API"
326+
event.channel_id = "agent-v1"
327+
event.object_type = "resource"
328+
event.object_id = "resource-001"
329+
330+
event_id = client.send(event)
331+
```
332+
333+
If neither the middleware nor an explicit value provides `tenant_id`, the event
334+
will fail `protovalidate` validation and raise a `ValidationError`.
335+
336+
---
337+
287338
## Running the Unit Tests
288339

289340
```bash

0 commit comments

Comments
 (0)