# Assertions & Evidence

ipsonic-probe extends existing YAML checks without adding runtime dependencies.

## Structured assertions

`tests.structured` is a list of up to 64 rules with unique `id` values. Types: `status`, `header`, `body_regex`, `json_value`. Operators: `eq`, `ne`, `gt`, `ge`, `lt`, `le` (numeric operators only for numeric values). JSON paths are simple dot-separated object keys, not full JSONPath. Regex checks are limited to responses up to 1 MiB and patterns up to 256 characters. There is no arbitrary expression evaluation. Failure messages contain the rule ID and type but never actual response values.

```yaml
name: Example API
url: https://example.org/api/health
tests:
  structured:
    - id: http.ok
      type: status
      expected: 200
    - id: api.healthy
      type: json_value
      path: status
      expected: healthy
    - id: api.revision
      type: json_value
      path: version
      operator: ne
      expected: unknown
```

## Opt-in evidence and drift detection

The `evidence` section selects a small subset of technical observations. `http_headers` only accepts an allowlist of low-risk headers; no cookies, authorization or response bodies are persisted. `tls_fingerprint` records SHA-256 of the leaf certificate from a separate verified TLS connection (or an unverified connection if explicitly configured). `dns_rrset` queries the selected DNS RRset (A, AAAA, CNAME, MX, NS, CAA) and sorts the returned values. TTLs are excluded. These observations are captured separately from the check and may introduce additional requests and latency. Evidence is empty by default.

```yaml
evidence:
  http_headers: [content-type, strict-transport-security]
  tls_fingerprint: true
```

`--baseline-save` and `--baseline-compare` use baseline schema 2 and include evidence, status, and failed assertion IDs. Baseline files are created with mode 0600 and are never overwritten. Existing schema-1 baselines must be recreated explicitly. Do not place confidential information in allowlisted HTTP headers. Evidence for backends and retries is collected in execution order; keep backend order stable.

## JSON result schema v1

`--output json` emits `schema_version: 1`, `status`, `code`, `service_name`, `elapsed_seconds`, `failures`, `assertion_results` (each with `id`, `severity`, `message`), `thresholds`, and `evidence`. Exit codes: 0 OK, 1 WARNING, 2 CRITICAL, 3 UNKNOWN. `response_time` Nagios perfdata is emitted in seconds as `response_time=<seconds>s;<warning>;<critical>;0;`.

## Security boundaries

Never embed credentials in URLs or literals; prefer environment variables. The redaction layer masks known configured secrets, but cannot guarantee removal of arbitrary confidential response-derived data. HTTP headers are allowlisted and truncated, and structured assertion diagnostics deliberately exclude raw actual/expected values. Baseline files should be stored in restricted directories. Response bodies are not persisted. TLS fingerprint inspection makes a separate connection and is not supported through an HTTP proxy. The timeout budget is best-effort for underlying worker threads; it does not forcibly interrupt a blocked socket.

## Local regression testing

Run `python3 -m unittest discover -s tests -q` and `python3 ipsonic-probe.py --validate --strict examples/NNN-example.yaml`. Functional tests use local HTTP servers and do not need internet access.

## New examples

- `182-structured-api-assertions.yaml`: stable IDs and JSON assertions.
- `183-http-header-evidence.yaml`: safe HTTP header observation.
- `184-tls-fingerprint-evidence.yaml`: leaf-certificate SHA-256.
- `185-dns-rrset-evidence.yaml`: DNS answer drift.

Commands:

```sh
python3 ipsonic-probe.py --validate --strict examples/182-structured-api-assertions.yaml
python3 ipsonic-probe.py examples/183-http-header-evidence.yaml --baseline-save /tmp/header-baseline.json
python3 ipsonic-probe.py examples/183-http-header-evidence.yaml --baseline-compare /tmp/header-baseline.json
```
