Metadata-Version: 2.4
Name: sitetus-collector
Version: 0.1.1
Summary: Read-only Sitetus collector for Python web applications: answers the panel's signed pull with a Level 1 inventory and health envelope.
Author: Codemarkt
License-Expression: MIT
Project-URL: Homepage, https://sitetus.com
Project-URL: Source, https://github.com/sitetus/collector-python
Keywords: sitetus,monitoring,inventory,collector,wsgi,asgi,django,flask,fastapi
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: System :: Monitoring
Classifier: Framework :: Django
Classifier: Framework :: Flask
Classifier: Framework :: FastAPI
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: cryptography>=3.4.8
Provides-Extra: dev
Requires-Dist: pytest>=7; extra == "dev"
Requires-Dist: build; extra == "dev"
Requires-Dist: jsonschema; extra == "dev"
Requires-Dist: flask; extra == "dev"
Requires-Dist: django; extra == "dev"
Requires-Dist: fastapi; extra == "dev"
Requires-Dist: starlette; extra == "dev"
Requires-Dist: httpx; extra == "dev"
Dynamic: license-file

# sitetus-collector (Python)

Read-only collector for the [Sitetus](https://sitetus.com) monitoring panel. It lets the panel read a package inventory and a few health facts from **inside** your Python web application, through signed requests the panel sends. Installing it is optional: Sitetus works fully without a collector; the collector only adds inventory and inside-out health data.

```bash
pip install sitetus-collector
python -m sitetus_collector pair <code>
```

## What this package will not do

A public commitment, enforced by the code and its tests:

- It does not read or transmit `.env` or configuration values — only booleans and counts: whether the framework's debug switch is on, whether the secret key is unset or carries Django's `django-insecure-` prefix, whether `ALLOWED_HOSTS` contains `*`.
- It does not transmit file contents — only sizes, counts, ages and octal modes — and never sends a path beyond the project root.
- It does not touch the database and collects no personal data.
- It makes no outbound requests of its own.
- It keeps no secret on the server and writes nothing except its own nonce file.
- It never uses a request parameter as a path, command or import target.
- It reports the SHA-256 of its own installed source in every envelope.
- It answers `404` to every request that is not signed by the panel, after its expiry date, and after 90 days without a valid pull.
- The code is open source (MIT).

## How it works

- You mount the collector on a **random path** that the configuration carries (`/sitetus-<random>/`).
- The panel sends a **signed** `GET` to that path a few times a day. The collector verifies the signature with the embedded **public key** and answers with a read-only JSON envelope (`schema_version` 1, the same shape the PHP and WordPress collectors send).
- Any unsigned or invalid request gets a bodiless **404** — as if the route did not exist. No secret lives on your server: the public key and the project key are identifiers, not secrets.
- The collector serves `GET`, `HEAD` and `POST` on its own path only, and reads at most 1 MiB of request body: the method and the path are decided before a byte is read, and a larger body — announced or streamed — gets the same bodiless 404.
- The panel's requests come from fixed addresses: `https://sitetus.com/ips.txt`.

## Install

```bash
pip install sitetus-collector
```

Python 3.10+. The only runtime dependency is [`cryptography`](https://pypi.org/project/cryptography/) 3.4.8 or newer (Ed25519 and RSA signature verification; the standard library has neither).

## Pair

Copy the pairing code from the site's Collector page in the panel, then:

```bash
python -m sitetus_collector pair <code>
```

This writes `sitetus.collector.json` next to your project (project key, public key, algorithm, install path, nonce file name, expiry — nothing secret). The command prints the path the panel must pull from; if the pairing code did not carry one, a random path is generated and you enter it in the panel.

Hosts without a writable filesystem can pass the same document through the `SITETUS_COLLECTOR_CONFIG` environment variable (JSON) or point `SITETUS_COLLECTOR_CONFIG_FILE` at the file.

## Mount

**Django** — `urls.py`:

```python
from sitetus_collector.adapters.django import urlpatterns as sitetus_urlpatterns

urlpatterns = [
    # ... your routes
] + sitetus_urlpatterns()
```

**Flask**:

```python
from sitetus_collector.adapters.flask import blueprint

app.register_blueprint(blueprint())
```

**FastAPI**:

```python
from sitetus_collector.adapters.starlette import router

app.include_router(router(app=app))
```

**Starlette**:

```python
from sitetus_collector.adapters.starlette import install

install(app)
```

**Any WSGI or ASGI application**:

```python
from sitetus_collector import Collector
from sitetus_collector.adapters.wsgi import wsgi_middleware   # or adapters.asgi.asgi_middleware

application = wsgi_middleware(application, Collector())
```

The adapters register both `/<path>` and `/<path>/` and never redirect between them: the panel signs the exact path it requested.

## Nonce store and multi-process deployments

Every pull carries a nonce; a replayed nonce is refused with `403`. The default store is an in-process table (1000 entries, pruned on a 20-minute window). With several worker processes (gunicorn `-w 4`, uvicorn `--workers`) each worker has its own table, so a replay inside the skew window could be accepted by a sibling worker; on serverless hosts the table disappears with the instance. Set `SITETUS_NONCE_FILE=/path/to/.sitetus-nonce.json` (or run from the directory holding `sitetus.collector.json`, where the nonce file is created next to it) to share one locked file between processes on the same host. The file store is also the only one that enables the 90-day sleep, because sleep needs a durable "last valid pull" timestamp. Multi-host deployments behind one hostname have no shared store in this version.

## Build-time manifest

Containers that copy only the virtualenv, and function platforms, may have no lock file at runtime. Run at build time:

```bash
python -m sitetus_collector manifest
```

This writes `sitetus.manifest.json` (package inventory and the filesystem-based checks). At runtime the collector prefers live reads and uses the manifest only for what it cannot read live.

## Other commands

```bash
python -m sitetus_collector hash      # the package hash reported as collector.file_hash
python -m sitetus_collector inspect   # print the envelope this installation would answer with
```

## What the envelope contains

`runtime` (Python version, implementation, wsgi/asgi, OS family), `framework` (django | flask | fastapi | starlette | unknown, version), `packages` (ecosystem `pypi`; names normalised per PEP 503 with versions from `poetry.lock`, `uv.lock`, `Pipfile.lock`, pinned `requirements.txt` lines, or the installed distributions; the lock file's SHA-256), and `health.checks`: `disk_usage`, `debug_flag`, `secret_key_default`, `allowed_hosts_wildcard`, `env_in_static`, `env_file_permissions`, `lockfile_missing`, `outdated_lockfile`, `git_dir_in_root`. Each check carries a status (`ok | warn | fail | unknown`) and a number or boolean; a check that cannot be measured says `unknown` with a dictionary key, never free text.

## Development

```bash
python -m venv .venv && . .venv/bin/activate
pip install -e ".[dev]"
pytest
python -m build
```

Security reports: write to **security@sitetus.com**; please do not open a public issue.

## Uninstall

Remove the package and `sitetus.collector.json`; the panel side can also revoke the collector with one click.

## License

MIT — `LICENSE`.
