Metadata-Version: 2.4
Name: grippo
Version: 0.1.3
Classifier: Development Status :: 4 - Beta
Classifier: Programming Language :: Python :: 3.14
Classifier: Programming Language :: Rust
Summary: Parse, match and scrub data at scale: Python binding for grippo.
License-Expression: Apache-2.0
Requires-Python: >=3.14
Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
Project-URL: Repository, https://github.com/hyperi-io/grippo-rs

# grippo

The Python binding for [grippo](https://github.com/hyperi-io/grippo-rs): secrets, credential fields and personal data found in log text and replaced with tokens, grippo's field scanners and literal sets, regex patterns, and CEL filters over raw JSON.

```python
from datetime import UTC, datetime

from grippo import Field, LiteralSet, Scrubber

scrubber = Scrubber()  # gitleaks' rules, every layer, mask redaction
text, report = scrubber.scrub_text("login password=hunter2 from alice@example.com")
record, report = scrubber.scrub_value(
    {"user": {"password": "hunter2"}, "at": datetime.now(UTC)}
)
Field("MAC").fullmatch("00:1a:2b:3c:4d:5e")  # True
LiteralSet(["db-1", "alice"], boundary=True).find_all(
    "alice on db-1"
)  # [(0, 5, 1), (9, 13, 0)]
```

## Scrubbing

Build one `Scrubber` and share it across threads.

- `scrub_text_or_none(text)` gives the scrubbed text, or `None` when nothing changes, so a caller that only needs to know skips the copy.
- A value runs on through a lone surrogate in `scrub_text` and is masked whole. Where the credential pair layer rewrites a text, each surrogate in it comes back as three U+FFFD, as `errors="replace"` decodes it.
- `scrub_value(value, *, default=None, max_depth=32, max_strings=64)` reads any Python value the way a log record's fields are read. A dict's own keys and string values are always read. Inside a container, keys and strings past `max_strings` read `<elided>` and the rest is counted. A container `max_depth` deep reads `<depth limit>`, and one inside itself `<cycle>`. NaN and the infinities read by name. Anything else, such as `bytes`, `datetime`, `Decimal` or a `set`, goes through `default`, `str` when `None`, and one that raises reads `<unprintable TYPE>`. Keys are scrubbed too, and their order is kept.
- In `scrub_value`, a tuple, or a two-item list inside a list, whose first item names a credential keeps the name and masks the value, as a header list holds them. An int past 64 bits reads as its decimal text. `None` or an empty string under a credential name is kept, as there is nothing in it.
- `Scrubber(only_rules=[...])` builds just those config rules, and refuses an id the config has no rule for. `without_rules=[...]` leaves rules out by id, a PII check included. `skipped()` lists each rule the build left out and why.
- `Scrubber(redaction="hash", hash_key=...)` writes `[<RULE>_<16 hex>]`, the first 8 bytes of HMAC-SHA256 over the secret. An empty key hashes as HMAC does with `b""`.
- `is_credential_name(name, parent="", *, extra=())` says whether a field name holds a credential, and `mask_text(text, mask, *, extra=())` masks every credential value in a text. `extra` adds terms to `SENSITIVE_FIELDS`, a `frozenset`, for that call.
- The text layer and `mask_text` mask a header value an HTTP client's error quotes as refused, whole and past `\` escapes: `Illegal header value b'Basic dXNlcg=='` keeps its head and quotes and loses the value. One that never closes is masked to the end of its line.
- `mask_url_userinfo(text, mask=MASK_VALUE, *, keep_user=True)` masks every URL password, and the user too with `keep_user=False`. `MASK_VALUE` is `***REDACTED***`.
- `url_passwords(text)` lists every URL password in a text, as written.
- `rule_token(rule)` gives the stem of the token a rule writes: `rule_token("credit-card")` is `CREDIT_CARD`, written as `[CREDIT_CARD_REDACTED]`.
- The PII layer checks card numbers, IBANs, email addresses, phone numbers, and AU ABN, ACN, Medicare and TFN numbers.

## Fields and literal sets

- `Field(name)` is one of grippo's field parsers, named for the grok pattern it reads: `IPV4`, `IPV6`, `MAC`, `UUID`, `HOSTNAME`, `EMAILADDRESS`, `TIMESTAMP_ISO8601`, `UNIXPATH` and the rest in `FIELD_NAMES`. Each reads exactly as its `regex`, leftmost first, with no regex run unless a non-ASCII digit needs one.
- `fullmatch(text)` says whether the field, read from the start, takes all of the text. `scan(text, at=0)` gives where it ends from an index, and `find_all(text)` the `(start, end)` of each match. Indices are `str` indices.
- `Field("IPV4", validate=True)` also holds each octet to 255 or less, and `PORT` to 16 bits.
- `LiteralSet(literals, *, boundary=False)` finds every literal in one pass. `find_all(text)` gives `(start, end, index)`: leftmost first, the longest at one start, none overlapping. With `boundary`, a match has no ASCII letter or digit beside it, and a literal that fails that gives way to a shorter one at the same start, as `(?<![A-Za-z0-9])(?:...)(?![A-Za-z0-9])` does with its alternatives longest first.

## Patterns and CEL filters

```python
from grippo import Program, Refusal, StrMatcher

StrMatcher("^web-[0-9]+$").is_match("web-12")  # True
program = Program.compile('host.startsWith("prod-") && severity > 3')
program.matches(b'{"host":"prod-1","severity":5}')  # True
program.explain()  # 'and(prefix(host, "prod-"), Gt(severity, Int(3)))'
refused = Program.compile("size(tags) > 0")
isinstance(refused, Refusal)  # True
refused.why, refused.span  # ('a function call outside the subset', (0, 4))
```

- `StrMatcher(pattern)` reads a regex as Rust's regex does: `$` matches only at the very end, never before a final newline, and lookaround or a backreference doesn't build. An empty pattern raises `ValueError`, since it would match everywhere. `is_match(text)` runs the cheapest check that gives the regex's answer, and `tier` names it: `Byte`, `Literal`, `LiteralSet` or `Regex`. `hint` says what would reach a faster one.
- `Program.compile(text, *, allow_regex=False)` lowers a CEL filter to grippo's own checks over raw JSON, with cel's meaning and no interpreter. A form outside what grippo lowers, such as `size()`, a macro or a comparison of two fields, comes back as a `Refusal`, not an exception, so the caller runs its interpreter for that filter. `why` says what the form is and `span` where it sits, in `str` indices. `matches()` lowers only with `allow_regex=True`.
- `eval(record)` takes the record as `bytes` or `str` and gives `True`, `False`, or the name of the error cel would raise: `missing_root`, `missing_field`, `type_mismatch`, `malformed` or `overflow`. `matches(record)` is `True` only on `True`.
- `explain()` names each check the filter runs, one line, for a startup log or a UI that shows what the runtime does.
- `has()` takes a member, `has(a.b)`, as cel does. `has(a)` on a bare name is refused.

