Skip to content

feat: a block of Python where a command would go - #42

Closed
AlexeyShalaev wants to merge 1 commit into
feat/plan-locksfrom
feat/python-hooks
Closed

AlexeyShalaev wants to merge 1 commit into
feat/plan-locksfrom
feat/python-hooks

Conversation

@AlexeyShalaev

Copy link
Copy Markdown
Member

Part 3a of #32, scoped to hooks — exactly what the epic describes under "Inline Python blocks (the authentik model)".

hooks:
  before_drop:
    python: |
      if event.operation.size_bytes and event.operation.size_bytes > 10 * 2**30:
          log.warning("exporting %s before drop", event.partition.name)
          export_to_s3(event.partition.name, event.window)
  after_create:
    python_file: hooks/notify.py

The block sees event (the same PartitionEvent a hook class is handed) and log; nothing else is injected. Raising refuses the operation, exactly as a non-zero exit does for a command.

Every one of the epic's four notes on 3a is done:

  • Raising aborts and leaves it for the next run — documented at config level, tested against a real DROP (the partition stays detached and undropped).
  • File reference — python_file, resolved relative to the document. Not a YAML tag: a plain key needs no custom loader and works in JSON too.
  • validate must compile every snippet without executing it — inline blocks compile the moment the document validates (a SyntaxError is a ValidationError with the line in it); file-backed ones are read and compiled by every command before a connection is opened, so validate fails on a missing or broken file by name.
  • Must not fire during plan — already structurally true, and tested.

On security, the epic's own words made mechanical: code hooks are off unless enabled, and the flag is the existing --allow-hooks — a Python hook is a hook, and the trust argument (the document already authorises DROP; that stops holding when the document comes from a less trusted place than the DSN) is the same one word for word. The docs say there is no sandbox, and why claiming one would be worse than having none.

What is deliberately not here: retention predicates as Python (Callback in config). They would run during plan, which is a different gate and a different promise about what plan is allowed to do. The compile/namespace machinery here is what they would reuse; a follow-up if wanted.

9 unit tests on the runner (both mirrors), 5 on the document, 4 on the CLI, 2 against a real PostgreSQL. Stacked on #41.

The authentik model, for the Python-adjacent audience: a phase in a
document takes a block of Python -- inline as python, or from a file as
python_file relative to the document -- evaluated with event and log in
scope, raising to refuse. PythonHooks is the same from code, in both
mirrors, on the shared runner in pg_partsmith.python_hooks.

Every block is compiled when the document is read, and a file-backed one by
validate: a SyntaxError is a validation error with a line number in it, not
something a CronJob finds at 03:00 after some of the run's DDL has
committed. Tracebacks name the phase, or the file, rather than <string>.

It is behind the same --allow-hooks as a command, because a Python hook is
a hook and the trust argument is the same one. No sandbox is claimed: exec
with a filtered __builtins__ is not a security boundary, and the docs say
so rather than pretending otherwise. Isolation, if wanted, is a command
hook in a container of its own.

Retention predicates as Python are deliberately not in this change. They
would run during plan, not apply, which is a different gate and a different
promise about what plan is allowed to do.
@AlexeyShalaev

Copy link
Copy Markdown
Member Author

Folded into #43, one PR for the whole epic; this branch stays for history.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant