Metadata-Version: 2.4
Name: shiza
Version: 0.2.0
Summary: SHIZA Secrets: load .env secrets from your SHIZA vault at runtime, or pull, push and run with them from the CLI
License-Expression: MIT
Keywords: shiza,secrets,dotenv,env,cli
Classifier: Programming Language :: Python :: 3
Classifier: Operating System :: OS Independent
Classifier: Topic :: Security
Classifier: Environment :: Console
Requires-Python: >=3.8
Description-Content-Type: text/markdown
License-File: LICENSE
Dynamic: license-file

# shiza

Load `.env` secrets from [SHIZA Secrets](https://vault.shiza.ai/secrets) at runtime, so your services keep one read-only token instead of every API key. Also pulls, pushes and runs with `.env` files from the command line. Standard library only, Python 3.8 or newer.

A Node.js package with the same commands is available as [`shiza-cli`](https://www.npmjs.com/package/shiza-cli) on npm.

```
pip install shiza
```

## Use it in a service

Your hosting platform keeps only two variables:

```
SHIZA_URL=https://vault.shiza.ai
SHIZA_TOKEN=shz_...        # read-only token limited to one environment
```

**Option 1: wrap the start command (no code changes).** Change your start command, for example from `gunicorn app:app` to:

```
shiza run --env production -- gunicorn app:app
```

The secrets are fetched once at startup and passed to your app as normal environment variables. Nothing is written to disk. On Linux and macOS `shiza` replaces itself with your command, so your app keeps the process ID and receives shutdown signals directly, which matters in containers.

**Option 2: load them in code.** Call `load()` before anything reads `os.environ`:

```python
import shiza

shiza.load(env="production")
# os.environ["OPENAI_API_KEY"] is now set
```

In Django put it at the top of `settings.py`; in Flask or FastAPI, at the top of the module that creates the app.

`load()` arguments: `env`, `files` (limit to some `.env` paths), `override` (default `True`: vault values replace existing variables), `allow_unsafe` (see below), `url` and `token` (default to `SHIZA_URL`, `SHIZA_TOKEN`, then the `shiza login` config). `fetch_secrets()` takes the same arguments and returns a dict with `project`, `environment`, `files`, `secrets` and `skipped`, without changing `os.environ`. Failures raise `shiza.ShizaError` with a `code` matching the CLI exit codes.

## Variables that are skipped

A vault value for a variable that controls how a runtime loads code or trusts TLS would let anyone who can edit the project run code on every service that loads it. These are left out by default: by `run` and `load()`, which report them in `skipped`, and by `env pull`, which comments them out in the written file so dotenv, docker compose or direnv never load them. The list includes `PATH`, `NODE_OPTIONS`, `NODE_PATH`, `NODE_TLS_REJECT_UNAUTHORIZED`, `NODE_EXTRA_CA_CERTS`, `PYTHONPATH`, `PYTHONSTARTUP`, `PYTHONHTTPSVERIFY`, `BASH_ENV`, `PROMPT_COMMAND`, `PS4`, `PERL5OPT`, `RUBYOPT`, `JAVA_TOOL_OPTIONS`, `CLASSPATH`, `DOTNET_STARTUP_HOOKS`, `LESSOPEN`, `GCONV_PATH`, `SSL_CERT_FILE`, `REQUESTS_CA_BUNDLE`, `OPENSSL_CONF`, `SSLKEYLOGFILE`, `SHIZA_URL`, `SHIZA_TOKEN`, and anything starting with `LD_`, `DYLD_`, `BASH_FUNC_`, `GIT_` or `NPM_CONFIG_`. Set them on the host instead, or pass `allow_unsafe=True` / `--allow-unsafe-env` if you really mean it.

The list is a safety net, not a guarantee: give write access to a project only to people and tokens you would trust with the services that load it.

## Security behaviour

- The server URL must be `https://`. Plain `http://` is accepted only for `localhost`, so the token never crosses a network unencrypted.
- Redirects are refused, so the token is never sent to another address. Point `SHIZA_URL` at the final `https://` address.
- `env pull` and `env push` refuse paths that leave `--dir`, including through symlinks. A symlinked `.env` that points to another file inside `--dir` is followed; one that points outside is refused. Files are never written through a symlink.
- Files are written with mode 600.
- On/off flags take no value: `--allow-unsafe-env=false` is an error rather than "on".

## Get a token

In the web app open the project, click **Tokens**, and create a service token. Pick an environment to limit the token to that environment, and tick **Allow writes** only if the token needs to push. The token is shown once.

## Log in (for local use)

```
shiza login --url https://vault.shiza.ai
```

The token is asked for without being shown. This stores the URL and token in `~/.config/shiza/config.json` (mode 600), the same file the Node.js CLI uses. The environment variables `SHIZA_URL` and `SHIZA_TOKEN` override the file, which is what you want in CI and on servers.

## Commands

| Command | What it does |
|---|---|
| `shiza env ls` | List the environments and `.env` files the token can see, with secret counts. |
| `shiza env pull [--env NAME] [--dir DIR] [--file PATH...] [--force] [--allow-unsafe-env]` | Write every file of the environment to `DIR/<path>` with mode 600. Never overwrites an existing file without `--force`; prints what would change and exits with code 6. |
| `shiza env push [--env NAME] [--dir DIR] [--file PATH...] [--replace]` | Upload local files by path and merge them. `--replace` makes the local file the full set. Needs a token with **Allow writes**. |
| `shiza run [--env NAME] [--file PATH...] [--allow-unsafe-env] -- <command...>` | Run a command with the secrets in its environment, writing nothing. The root `.env` is applied first, then the other files in order. |

`--json` prints machine-readable output for `env ls`, `env pull` and `env push`. `--quiet` hides informational lines on stderr. `python -m shiza` works the same as `shiza`.

If the token is limited to one environment, `--env` can be omitted.

## Exit codes

| Code | Meaning |
|---|---|
| 0 | ok |
| 1 | network or server error |
| 2 | usage error |
| 3 | token missing or rejected |
| 4 | not allowed (read-only token, wrong environment) or not found |
| 5 | rate limited |
| 6 | refused to overwrite a local file (use `--force`) |

## Local development server

If the server uses a private CA, set `SSL_CERT_FILE=/path/to/ca.pem`. For a throwaway local server only, `SHIZA_INSECURE_TLS=1` skips certificate checks for the `shiza` client (not the rest of your process). Never set it in production.

## Publishing

```
cd shiza-py
python -m pip install --upgrade build twine
python -m build
python -m twine upload dist/*
```
