Skip to main content

keboola.vcr

VCR recording, sanitization, and validation for Keboola component HTTP interactions.

Installation

pip install keboola.vcr

Usage

from keboola.vcr.recorder import VCRRecorder
from keboola.vcr.sanitizers import DefaultSanitizer

recorder = VCRRecorder(
    cassette_dir="tests/cassettes/my_test",
    secrets={"api_key": "secret"},
)

Features

  • Recording: Captures real HTTP interactions via vcrpy and stores them as JSON cassettes
  • Sanitization: Redacts secrets, tokens, and sensitive fields before saving cassettes
  • Scaffolding: Generates test directory structures from component config definitions
  • Validation: Compares output snapshots to detect regressions

Sanitizing data before the component reads it

By default, sanitizers scrub the cassette only — during recording the component still receives the real API response, so real values can end up in the component's output tables. To stop real customer data from ever reaching the component, tag a sanitizer with scrub_before_read=True. The recorder then redacts those fields in the response before the component reads them, so the output is clean regardless of format (CSV, Parquet, or direct-to-Storage).

# tests/.../sanitizers.py
from keboola.vcr import BodyFieldSanitizer

def get_sanitizers(config):
    return [
        # PII the component only reads and emits → redacted before the component reads it
        BodyFieldSanitizer(fields=["customer_name", "email", "phone"], scrub_before_read=True),
    ]

Rule: only tag fields the component consumes and emits. Never tag a value the component sends back to the live API during recording — a pagination cursor, an async job ID, or an OAuth token — because it must stay real for the follow-up request to succeed. Token/secret sanitizers stay untagged (cassette-only) for exactly this reason. If a redacted value is sent to the live API, recording aborts with an error naming the tagged sanitizer(s) so you can find and untag the offending field.

Caution: only tag field-scoped body sanitizers (e.g. BodyFieldSanitizer) with scrub_before_read. Do not tag whole-response sanitizers like DefaultSanitizer or HeaderSanitizer — their header whitelisting would also apply to the component-visible response during recording, and can strip pagination or rate-limit headers the component needs to keep working.

Recording proxy (PHP and other non-Python components)

A component that is not written in Python cannot run inside VCRRecorder. For those, keboola.vcr.proxy is a mitmproxy addon that records the component's HTTPS traffic into the same cassette format, runs the same sanitizers, and replays it offline. The component code does not change: its HTTP client only needs to honour HTTPS_PROXY (Guzzle does) and trust the proxy CA.

pip install "keboola.vcr[proxy]"        # mitmproxy 12, Python 3.12+
python -m keboola.vcr.proxy --mode replay --config tests/vcr.json --confdir /vcr-ca
  • Ports: 8080 is the proxy. 8081 is the control port: POST /start {"cassette": "<abs path>"} starts a test case, GET /status reports it so far without ending it, POST /stop ends it (record mode writes the cassette; the answer lists unmatched requests in replay), GET /health answers once the proxy runs.
  • Network: both ports listen on 0.0.0.0 (--listen-host, --control-host) so the test container reaches them over the compose network. The control port has no authentication: never publish it to the host or run the proxy outside a test or CI network.
  • Record (--mode record or VCR_MODE=record): live traffic, sanitized before it is written. Traffic outside a started case (test fixture set-up) passes through and is not recorded. If a value of a secret_env variable is still in the cassette after sanitizing, the proxy refuses to write it and /stop returns an error.
  • Replay (default): no upstream connection at all (upstream_cert=false, lazy connect). A request with no recorded answer gets HTTP 418 with an explanation and is listed in the /stop answer.
  • Matching: method, scheme, host, port, path and the sorted query, plus the JSON or form body (multipart boundaries masked); never headers. The body is ignored on OAuth token endpoints (Google, Microsoft, Firebolt by default). Each interaction plays once, in recorded order among equal requests.
  • Binary bodies (file downloads, uploads) bypass the text sanitizers and are stored base64-encoded, with "encoding": "base64" next to the body. A binary request body matches on its exact bytes (multipart boundaries masked), so upload chunks need no ignore_body entry.
  • Check what the component reads before you sanitize. DefaultSanitizer keeps only the content-type, content-length and accept response headers, and it redacts any body field named code. A component that polls an async operation needs "additional_safe_headers": ["location", "retry-after"]; one that branches on an API error code ({"error": {"code": ...}}) needs sensitive_fields without code. Otherwise replay takes a different branch than the recording did.
  • No time freezing: a request whose URL or body carries the current date will not match on replay. Pin such values in the test config.
  • Where it runs: wherever the component's tests run, usually its local Docker Compose stack, with the credentials in the folder's .env. CI can run the same stack. It cannot record a platform debug job: the platform runs only the component image, without the proxy container (use VCRRecorder.record_debug_run in Python).

tests/vcr.json replaces the VCR_SANITIZERS list of a Python component:

{
  "secret_env": ["OAUTH_APP_SECRET", "OAUTH_REFRESH_TOKEN"],
  "sanitizers": [
    {"class": "DefaultSanitizer", "additional_sensitive_fields": ["sessionId"]},
    {"class": "BodyFieldSanitizer", "fields": ["email"], "scrub_before_read": true},
    {"class": "UrlPatternSanitizer", "patterns": [["/users/[0-9]+", "/users/0"]]},
    {"class": "BodyFieldSanitizer", "fields": ["name"], "urls": ["graph.microsoft.com/v1.0/sites"]},
    {"class": "BodyPatternSanitizer", "patterns": [["<SessionToken>[^<]*</SessionToken>", "<SessionToken>REDACTED</SessionToken>"]]}
  ],
  "match": {"ignore_body": ["api.example.com/upload/*"], "ignore_body_fields": ["requestId"],
            "allow_playback_repeats": false},
  "unmatched_status": 418
}
Key Meaning
secret_env Env var names whose values (record mode only) are redacted everywhere and checked before writing
sanitizers Sanitizer classes from keboola.vcr.sanitizers with their keyword arguments. DefaultSanitizer always runs first; declare it at most once to extend it. CallbackSanitizer is not available (it needs code); for XML or plain-text bodies use BodyPatternSanitizer (regex [pattern, replacement] pairs on request and response bodies)
sanitizers[].urls Optional host/path globs: the sanitizer applies only to those requests and their answers (a field that is personal data on one endpoint but needed on others)
match.body false turns body matching off
match.ignore_body Extra host/path globs whose body is not matched (added to the token endpoint defaults)
match.ignore_body_fields JSON or form fields left out of the body comparison (request ids, timestamps)
match.allow_playback_repeats Reuse the last matching interaction once all are played

The PHP side (one trait for keboola/datadir-tests, a CA helper and a compose example) is keboola/php-vcr-tests. Validate recordings with the component-developer:vcr-cassette-validator agent like any other cassette.

Development

uv sync --all-groups
uv run pytest tests/

Metadata

Release files for keboola.vcr 0.9.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for keboola.vcr 0.9.2
File Size Uploaded Provenance Actions
keboola_vcr-0.9.2.tar.gz 60.7 kB Without provenance Details for keboola_vcr-0.9.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for keboola.vcr 0.9.2
File Interpreter ABI Platform Actions
keboola_vcr-0.9.2-py3-none-any.whl Python 3 none any Details for keboola_vcr-0.9.2-py3-none-any.whl

Total release size: 126.7 kB

Release files / keboola_vcr-0.9.2.tar.gz

Download URL keboola_vcr-0.9.2.tar.gz
Size 60.7 kB
Tags Source
SHA-256 checksum
How to use checksums
5bfc03d07b48c7a8ca00c842da4b5fc4d777e3352425c6038dc76b1e1699db25
BLAKE2b-256 checksum
How to use checksums
c0b923db6ed0b11a408e446380b993b55bc513287ec1aa020884348736145e43
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.13.7 {"installer":{"name":"uv","version":"0.13.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / keboola_vcr-0.9.2-py3-none-any.whl

Download URL keboola_vcr-0.9.2-py3-none-any.whl
Size 66.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
19818ed8cde23d52eb6c612157c50a22e2d71604f639850ce7dd53fd34256bad
BLAKE2b-256 checksum
How to use checksums
7a7da18976560c24ce643c3e7225d7f398b5c403b79e6ebeb653b53e408cabef
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.13.7 {"installer":{"name":"uv","version":"0.13.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Security for keboola.vcr 0.9.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page