Metadata-Version: 2.4
Name: hixumo-hive
Version: 0.0.1
Summary: Hixumo Hive for Python: your app's environment values, without a .env file.
Keywords: hixumo,hive,secrets,environment,dotenv
Author: Hixumo
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-Expression: LicenseRef-Proprietary
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
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: Programming Language :: Python :: 3.14
Classifier: Programming Language :: Python :: 3.15
Classifier: Typing :: Typed
License-File: LICENSE
Requires-Dist: cryptography >=42 ; extra == "snapshot"
Provides-Extra: snapshot

# hixumo-hive

Your Python app's values from Hixumo Hive, without a `.env` file: Django, FastAPI, Flask, Celery or a
script. Python 3.10 or newer; no other package is needed, and no Node.js.

```sh
pip install hixumo-hive
```

```python
import os

from hixumo_hive import load_hive

load_hive()

database_url = os.environ["DATABASE_URL"]
```

With `HIVE_TOKEN` (an access token) in the server's environment. Hive is asked **once, when the app
starts**, however often `load_hive` is called. Nothing polls: a changed value reaches the app at its next
start. Under `hive run` (a laptop, a test) the values are there already and Hive is not asked at all.

Call it before the modules that read `os.environ` as they are imported. A package's own `__init__.py` runs
before the modules in it, too. In an async app, call it as the app starts, or
`await asyncio.to_thread(load_hive)`.

## No code

```sh
python -m hixumo_hive run -- gunicorn app:app
```

The command runs with the values in its environment, and takes this process's place: signals and the exit
code are its own. Its workers find the values there, so Hive is asked once for all of them. `HIVE_TOKEN`
and the snapshot settings are not passed on to it. When the values cannot be read, the command does not
start, one line says why, and the exit code is 1. `HIVE_OPTIONAL=1` lets it start with nothing to read
from; anything else, a refusal or a setting that is wrong, still stops it.

Or import `hixumo_hive.register` first: it loads the values as it is imported, with `HIVE_OPTIONAL` too.

## Frameworks

- **Django:** the first lines of `settings.py` (the file loaded first, when the settings are split), above
  everything in it that reads `os.environ`. `manage.py`, WSGI, ASGI and Celery all load the settings;
  what their own files read before that (`DJANGO_SETTINGS_MODULE`, for one) cannot come from Hive. For a
  build step with nothing to read from (`collectstatic`), use `load_hive(optional=True)`, and
  `os.environ.get(...)` for the keys the settings read.
- **FastAPI, Flask:** above the app's own imports, in the file the server loads. Linters may report the
  imports below it (E402): say so there, or use "No code".
- **Workers:** with several, each worker asks once when it loads the app. `gunicorn --preload` and "No code" ask
  once for all. A development server's reloader (`runserver`, `flask run --debug`) keeps the values it
  started with: restart it for new ones. So does whatever starts itself again from a process that loaded them
  (`gunicorn --preload` with USR2, `os.exec…`): restart the process that loaded them.

## `load_hive(...)`

| Option | Default | Does |
|---|---|---|
| `required` | | Keys that must be there and not empty; else `missing_keys`, all of them named, and nothing is set |
| `environment` | `HIVE_ENV` | The environment it must be (its name, any case, or its id); another is `wrong_environment` |
| `inject` | `True` | Put the values in `os.environ` |
| `override` | `True` | Hive's values win over what `os.environ` has, as in `hive run`; `False` keeps what is there |
| `optional` | `False` | With nothing to read from, an empty result instead of `no_source`. Never hides a refusal |
| `token` | `HIVE_TOKEN` | An access token. Given in code, `HIVE_SNAPSHOT` is left out |
| `api_url` | `HIVE_API_URL`, else the one built in | The Hive API's address. Given in code, `HIVE_SNAPSHOT` is left out |
| `snapshot`, `snapshot_use` | `HIVE_SNAPSHOT`, `HIVE_SNAPSHOT_USE` | See "Snapshots" |
| `timeout` | `20.0` | Seconds for the whole read, tries and waits included (1 to 120) |

It gives a `Hive`: `environment`, `source` (`hive`, `snapshot`, `run` or `none`), `created_at` (a snapshot's),
`values` (read-only), `get(key, default)`, `require(key)` (raises when missing or empty), `hive[key]`,
`key in hive`, `keys()`, `len(hive)`. Printing it shows how many keys there are, never a value. It
cannot be pickled, and is no `Mapping` on purpose: error trackers walk those.

A `HiveError` has a `code` and a sentence; neither holds a value, a token or a key, and nor do the local
variables of its traceback: `no_source`, `bad_token`, `bad_options`, `token_refused`, `tokens_off`,
`not_found`, `paused`, `refused`, `rate_limited`, `unreachable`, `not_hive`, `wrong_environment`,
`missing_keys`, `missing_key`, `unfit_value`, `snapshot_missing`, `snapshot_key`, `snapshot_unreadable`,
`snapshot_package`, `unexpected`. A call that does not get through is tried three times within `timeout`; a
refusal never is. A failed read is answered again for 5 seconds before Hive is asked again (after a
429, for as long as Hive asked, a minute at most). Ctrl+C, or what a signal handler raises (a task's time
limit), is never one of these: it goes to whoever waits for it.

- **Forks, spawned processes and commands** find the values in the environment they inherit and ask for
  nothing, when the process that starts them has loaded the values (with `inject`, the default):
  `HIVE_LOADED` there (names and a hash, never a value) says what was loaded, and with what. The SDK
  for Node.js understands it too. One that has another `HIVE_TOKEN`, `HIVE_SNAPSHOT` or `HIVE_API_URL` of its
  own reads with that instead. `multiprocessing`'s fork server (Linux's default from Python 3.14) is such a
  starting process: load the values before it starts, or each child asks.
- **Values named like a setting** (`HIVE_TOKEN`, `HIVE_API_URL`, `HIVE_ENV`, `HIVE_SNAPSHOT…`) are values:
  `get()` has them, and they are never put in `os.environ`.
- **The token stays in the environment.** `hive run` and "No code" take it out of their command's; `load_hive`
  runs inside the app and leaves it, so the app's child processes inherit it.
- **Proxies and certificates:** `HTTPS_PROXY` and `NO_PROXY` are kept to (through a tunnel: the proxy sees no
  token), and certificates are Python's own (`SSL_CERT_FILE` for others; on macOS, python.org's Python
  needs its "Install Certificates.command" run once).

## Snapshots: no call to Hive at run time

For functions that start often (serverless), or a start that must not depend on Hive. The build saves
the environment's values in one encrypted file with the Hive CLI (it needs Node.js there), and the app opens it:

```sh
npx @hixumo/hive snapshot key                     # once: keep it as the secret HIVE_SNAPSHOT_KEY
npx @hixumo/hive snapshot --env production        # in the build: writes hive.snapshot.json
pip install "hixumo-hive[snapshot]"               # with the cryptography package, which opens it
```

At run time: `HIVE_SNAPSHOT=hive.snapshot.json` and `HIVE_SNAPSHOT_KEY`, the same code as above, and no
`HIVE_TOKEN`. Ship the file with the app. The cryptography package has compiled code: install it
for the platform the app runs on.

`HIVE_SNAPSHOT_USE=offline` (with `HIVE_TOKEN`) asks Hive first and uses the snapshot only when Hive cannot
answer (the network, an error on its side, too many requests), with one line on stderr. A refusal Hive gives (a
revoked token, tokens turned off, a paused space) is never answered from it.

- AES-256-GCM with a key made from 32 random bytes; no password is taken as a key.
- Its values change with the next build.
- Revoking a token does not reach a snapshot already made: for that, make a new key and change the
  values it held.

## Licence

Not open source. It may be installed and run, unchanged, to use Hixumo Hive, and included
(unchanged except by bundling or minifying) in your own application that uses Hixumo Hive; copying,
changing or redistributing it otherwise needs Hixumo's permission. See `LICENSE`.

