audit_trail.visibility¶
What a caller may see of the audit log: Visibility and its rules.
A Visibility holds row rules and field rules. The row rules (allowlists,
exclusions and per-type verb rules) become one SQL condition on
audit_activity, so paging and counts only ever see the rows the caller
may see. The field rules name values to hide in the rows that are shown:
they are masked in Python after the rows are read and compacted, and never
written back.
narrow combines two of them into one that shows only what both show.
UNRESTRICTED is the Visibility that restricts nothing, and
Visibility.NOTHING the one that shows nothing.
A default for every read is set with AuditTrail(visibility=resolver): a
VisibilityResolver that maps the caller's AuditContext to a
Visibility. by_actor_type builds one from a mapping of actor types.
UNRESTRICTED
module-attribute
¶
The Visibility that restricts nothing.
narrow returns the other side unchanged when one side is
UNRESTRICTED. Compare with is: an equal Visibility() is not it.
VisibilityResolver
module-attribute
¶
Maps the caller's AuditContext to what the caller may see.
Set as AuditTrail(visibility=...) or LabelResolver(visibility=...),
it gives the default Visibility of every read. It must return a
Visibility; an exception it raises fails the read.
TypeRule
dataclass
¶
TypeRule(
verbs: Collection[str] | None = None,
exclude_verbs: Collection[str] = (),
hidden_fields: Collection[str] = (),
)
Rules for the entries of one object type, on top of the global ones.
A rule only hides more: an entry of the type must pass both the global rules and this one.
Attributes:
| Name | Type | Description |
|---|---|---|
verbs |
Collection[str] | None
|
Allowed verbs for the type. |
exclude_verbs |
Collection[str]
|
Verbs hidden for the type. |
hidden_fields |
Collection[str]
|
Attribute keys of the type's |
Raises:
| Type | Description |
|---|---|
TypeError
|
A field is a |
Visibility
dataclass
¶
Visibility(
object_types: Collection[str] | None = None,
verbs: Collection[str] | None = None,
scope_ids: Collection[str] | None = None,
exclude_object_types: Collection[str] = (),
exclude_verbs: Collection[str] = (),
hidden_fields: Collection[str] = (),
hidden_header_fields: Collection[str] = (),
types: Mapping[str, TypeRule] = dict(),
mark_hidden: bool = False,
)
What the caller may see; applied to every row the query returns.
The library knows nothing about the host's permissions: the host builds a
Visibility from its own rules. It restricts both which transactions
are listed and which of their entries a group shows.
object_types, verbs and scope_ids are sets of allowed values.
None means no restriction; an empty collection allows nothing. A row
whose column is NULL never passes a restriction that is set, so
access control fails closed: with scope_ids={"t1"}, entries without a
scope are hidden.
The exclusions hide the rows whose column is one of their values. They
never hide a row whose column is NULL.
types adds rules for single object types (see TypeRule). A type
without a rule gets only the global rules.
The field rules mask values in what a read returns, after compaction, so compaction still works on the real values:
- A hidden field (
hidden_fields, or thehidden_fieldsof the entry's type rule) keeps its key indata.changes,data.payloadanddata.state, and each of its non-null values becomesMASKED("[hidden]"): both sides of[old, new], every id of a relationship change, and a state value whole.NonestaysNone; a redacted, erased or hashed value is masked like any other. An update whose fields are all hidden is still shown. - A hidden header field is
NoneonTransactionHeaderand listed in itsmasked, and is left out of each entry'sdata.context.actor_idandscope_idalso becomeMASKEDin each entry's column of that name (when notNone), andcorrelation_idbecomesNonein it.metahides all ofmeta,meta.<key>one key of it. Withactor_idhidden,access_summaryreturns a single count over all actors.
Attributes:
| Name | Type | Description |
|---|---|---|
object_types |
Collection[str] | None
|
Allowed |
verbs |
Collection[str] | None
|
Allowed |
scope_ids |
Collection[str] | None
|
Allowed |
exclude_object_types |
Collection[str]
|
Hidden |
exclude_verbs |
Collection[str]
|
Hidden |
hidden_fields |
Collection[str]
|
Attribute keys of |
hidden_header_fields |
Collection[str]
|
Header fields whose values are hidden: names
of |
types |
Mapping[str, TypeRule]
|
Rules per object type. |
mark_hidden |
bool
|
Whether a group says that some of its entries are
hidden: sets |
Raises:
| Type | Description |
|---|---|
TypeError
|
A collection field is a |
ValueError
|
|
predicate ¶
Return the SQL condition on audit_activity rows.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
table
|
Table
|
The |
required |
Returns:
| Type | Description |
|---|---|
ColumnElement[bool]
|
The condition; |
Source code in audit_trail/visibility.py
narrow ¶
Return a Visibility that shows only what both show.
Allowlists intersect, globally and per type; exclusions and hidden
fields add up; mark_hidden is set when either sets it. A type
with a rule on one side only keeps that rule. UNRESTRICTED on
either side returns the other side unchanged. A result that allows
no entry (an empty object_types, verbs or scope_ids) is
Visibility.NOTHING itself, which shows the same: nothing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Visibility
|
The other |
required |
Returns:
| Type | Description |
|---|---|
Visibility
|
The combined |
Source code in audit_trail/visibility.py
by_actor_type ¶
by_actor_type(
mapping: Mapping[str, Visibility],
*,
default: Visibility = Visibility.NOTHING,
same_scope: bool = False,
unscoped: Collection[str] = (),
models: _Models | None = None,
) -> VisibilityResolver
Build a resolver that picks a Visibility by the caller's actor type.
The resolver looks ctx.actor_type up in mapping. An actor type
that is not in it gets default, which shows nothing unless given, and
a warning is logged the first time each such type is seen (for the
first 1024 such types; later ones get no warning).
Example::
audit = AuditTrail(
engine,
visibility=by_actor_type(
{
"admin": UNRESTRICTED,
"user": Visibility(object_types={"Invoice"}),
},
same_scope=True,
unscoped={"admin"},
),
)
With models, the rules in each audited model's
AuditOptions(visibility=...) are added too::
class Invoice(Base, Audited):
...
__audit__ = AuditOptions(
visibility={"user": TypeRule(hidden_fields={"iban"})},
)
resolver = by_actor_type({"user": Visibility()}, models=Base)
# "user" gets Visibility(types={"Invoice": TypeRule(hidden_fields={"iban"})})
Each model rule is narrowed into its actor type's types[object_type],
where object_type is the one the model's entries are written with
(AuditOptions.object_type or the class name). A model's rules are
those of every __audit__ up its MRO, narrowed together, so a
subclass with its own __audit__ keeps its bases' rules. Model rules
only hide more: they also narrow an actor type mapped to
UNRESTRICTED, and an actor type named only in model rules is not in
mapping, so it gets default (narrowed by its model rules) and
the warning.
The models are read on the first call of the resolver, not when it is
built, so the order of imports does not matter, and read again when a
registry has gained or lost mappers since. A hidden field of a model
rule, or of a types rule in mapping or default for a model's
object_type, that is not an audited attribute of that model (a
column that is not excluded, or a collection relationship the model
tracks) makes that call raise ValueError naming the model, the
actor type and the field; so does every later call until it is fixed.
check_models reports the model rules' fields without a resolver.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mapping
|
Mapping[str, Visibility]
|
|
required |
default
|
Visibility
|
The |
NOTHING
|
same_scope
|
bool
|
Narrow the |
False
|
unscoped
|
Collection[str]
|
Actor types that |
()
|
models
|
_Models | None
|
The registry of the audited models, a declarative base
class using it, or a sequence of them, whose
|
None
|
Returns:
| Type | Description |
|---|---|
VisibilityResolver
|
The resolver. |
Raises:
| Type | Description |
|---|---|
TypeError
|
|
Source code in audit_trail/visibility.py
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 | |