diff --git a/.gitea/CODEOWNERS b/.gitea/CODEOWNERS new file mode 100644 index 0000000..c40bdfb --- /dev/null +++ b/.gitea/CODEOWNERS @@ -0,0 +1 @@ +* @mdaleo404 diff --git a/.gitea/workflows/ci.yml b/.gitea/workflows/ci.yml new file mode 100644 index 0000000..c42f43b --- /dev/null +++ b/.gitea/workflows/ci.yml @@ -0,0 +1,91 @@ +name: CI + +on: + pull_request: + workflow_dispatch: + +permissions: + contents: read + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + +jobs: + lint: + name: lint-and-types + runs-on: ubuntu-latest + timeout-minutes: 15 + + steps: + - name: Checkout code + uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 + with: + persist-credentials: false + + - name: Set up Python + uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # v5.6.0 + with: + python-version: "3.13" + + - name: Install Poetry + run: | + # Pinned to the release that generated poetry.lock: the content hash in the + # lock file depends on Poetry's hashing rules, so a different Poetry can + # report the lock as stale. Invoked as a module so the interpreter's Poetry + # is used even if an older one is earlier on PATH. + pip install "poetry==2.5.0" + python -m poetry --version + + - name: Install dev dependencies + run: python -m poetry install --with dev + + - name: Lint (ruff) + run: python -m poetry run ruff check . + + - name: Type check (mypy) + run: python -m poetry run mypy + + tests: + name: tests (python ${{ matrix.python-version }}) + runs-on: ubuntu-latest + timeout-minutes: 20 + strategy: + fail-fast: false + matrix: + # The package requires Python 3.12 or newer. + python-version: ["3.12", "3.13", "3.14"] + + steps: + - name: Checkout code + uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 + with: + persist-credentials: false + + - name: Set up Python + uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # v5.6.0 + with: + python-version: ${{ matrix.python-version }} + + - name: Install Poetry + run: | + # Pinned to the release that generated poetry.lock: the content hash in the + # lock file depends on Poetry's hashing rules, so a different Poetry can + # report the lock as stale. Invoked as a module so the interpreter's Poetry + # is used even if an older one is earlier on PATH. + pip install "poetry==2.5.0" + python -m poetry --version + + - name: Install project and dev dependencies + run: python -m poetry install --with dev + + # The generated-role validation tests run real ansible-lint and + # ansible-playbook --syntax-check, so the pinned collections must be present. + - name: Install pinned Ansible collections + run: > + python -m poetry run ansible-galaxy collection install + --requirements-file tests/requirements.yml + --collections-path collections + + - name: Run tests + run: python -m poetry run pytest -q diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS new file mode 100644 index 0000000..c40bdfb --- /dev/null +++ b/.github/CODEOWNERS @@ -0,0 +1 @@ +* @mdaleo404 diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml new file mode 100644 index 0000000..7b664bb --- /dev/null +++ b/.github/FUNDING.yml @@ -0,0 +1,2 @@ +github: mdaleo404 +ko_fi: mdaleo404 diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..c42f43b --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,91 @@ +name: CI + +on: + pull_request: + workflow_dispatch: + +permissions: + contents: read + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + +jobs: + lint: + name: lint-and-types + runs-on: ubuntu-latest + timeout-minutes: 15 + + steps: + - name: Checkout code + uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 + with: + persist-credentials: false + + - name: Set up Python + uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # v5.6.0 + with: + python-version: "3.13" + + - name: Install Poetry + run: | + # Pinned to the release that generated poetry.lock: the content hash in the + # lock file depends on Poetry's hashing rules, so a different Poetry can + # report the lock as stale. Invoked as a module so the interpreter's Poetry + # is used even if an older one is earlier on PATH. + pip install "poetry==2.5.0" + python -m poetry --version + + - name: Install dev dependencies + run: python -m poetry install --with dev + + - name: Lint (ruff) + run: python -m poetry run ruff check . + + - name: Type check (mypy) + run: python -m poetry run mypy + + tests: + name: tests (python ${{ matrix.python-version }}) + runs-on: ubuntu-latest + timeout-minutes: 20 + strategy: + fail-fast: false + matrix: + # The package requires Python 3.12 or newer. + python-version: ["3.12", "3.13", "3.14"] + + steps: + - name: Checkout code + uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 + with: + persist-credentials: false + + - name: Set up Python + uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # v5.6.0 + with: + python-version: ${{ matrix.python-version }} + + - name: Install Poetry + run: | + # Pinned to the release that generated poetry.lock: the content hash in the + # lock file depends on Poetry's hashing rules, so a different Poetry can + # report the lock as stale. Invoked as a module so the interpreter's Poetry + # is used even if an older one is earlier on PATH. + pip install "poetry==2.5.0" + python -m poetry --version + + - name: Install project and dev dependencies + run: python -m poetry install --with dev + + # The generated-role validation tests run real ansible-lint and + # ansible-playbook --syntax-check, so the pinned collections must be present. + - name: Install pinned Ansible collections + run: > + python -m poetry run ansible-galaxy collection install + --requirements-file tests/requirements.yml + --collections-path collections + + - name: Run tests + run: python -m poetry run pytest -q diff --git a/.gitignore b/.gitignore index 36b13f1..c5b0742 100644 --- a/.gitignore +++ b/.gitignore @@ -174,3 +174,5 @@ cython_debug/ # PyPI configuration file .pypirc +# Various +collections/ diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..98fe721 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,27 @@ +repos: + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v6.0.0 + hooks: + - id: trailing-whitespace + name: Enforce removal of trailing whitespace + - id: end-of-file-fixer + name: Enforce end of the file format + - id: check-yaml + name: Check YAML syntax + - id: check-toml + name: Check TOML syntax + - id: check-merge-conflict + name: Check for merge conflict markers + - repo: local + hooks: + - id: ruff + name: Lint with ruff + entry: poetry run ruff check --fix + language: system + types: [python] + - id: mypy + name: Type-check with mypy + entry: poetry run mypy + language: system + pass_filenames: false + types_or: [python, pyi] diff --git a/README.md b/README.md index 932e4d6..4c4c10c 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,258 @@ +[![Licence](https://img.shields.io/badge/GPL--3.0--or--later-orange?label=Licence)](https://git.sysmd.uk/mdaleo404/compose2role/src/branch/main/LICENSE) +[![Gitea Release](https://img.shields.io/gitea/v/release/mdaleo404/compose2role?gitea_url=https%3A%2F%2Fgit.sysmd.uk%2F&style=flat&color=orange&logo=gitea)](https://git.sysmd.uk/mdaleo404/compose2role/releases) +[![pre-commit](https://img.shields.io/badge/pre--commit-enabled-blue?logo=pre-commit&style=flat)](https://git.sysmd.uk/mdaleo404/compose2role/src/branch/main/.pre-commit-config.yaml) + # compose2role +Generate Ansible roles from Docker Compose projects discovered on remote hosts. + +`compose2role` inspects a Docker Compose project over SSH and writes an Ansible role that +reproduces it. The role restores the captured Compose files, dotenv files, and file-backed secrets +byte for byte (comments, quoting, ordering, and line endings included), then applies them with +`docker compose` under the original project name. Applying a role to its source host is an in-place +adoption, not a migration. + +It is deliberately **not** a Compose rewriter, normalizer, or migration tool. It is remote-only: +`generate` and `audit` require `--host` plus exactly one of `--project` or `--all`; a local-stack +invocation is a usage error. The daemon is named, not guessed (`--docker-context`), and privilege is +explicit (`--sudo` is never inferred). + +Angle-bracket tokens such as ``, ``, and `` are placeholders. + +## Documentation + +The in-depth reference lives on the [SysMD](https://www.sysmd.uk) site. This README covers the model, requirements, quick +start, and generated role at a glance. + +| Page | Covers | +| --- | --- | +| [Installation and configuration](https://www.sysmd.uk/docs/compose2role/install/) | Requirements, install, configuration discovery, environment variables | +| [Remote workflow](https://www.sysmd.uk/docs/compose2role/workflow/) | Discovery, capture, Docker context selection, audit, safe regeneration | +| [Command reference](https://www.sysmd.uk/docs/compose2role/reference/) | Options, JSON output, exit codes | +| [Generated role reference](https://www.sysmd.uk/docs/compose2role/generated-role/) | Layout, variables, tasks, playbook boundary | +| [Secrets and SOPS](https://www.sysmd.uk/docs/compose2role/secrets/) | Capture, classification, secret policy, encryption | +| [Safety and limitations](https://www.sysmd.uk/docs/compose2role/safety/) | Security boundaries, refusals, unsupported features | +| [Troubleshooting](https://www.sysmd.uk/docs/compose2role/troubleshooting/) | SSH, Docker, SOPS, drift, and policy failures | +| [Development and validation](https://www.sysmd.uk/docs/compose2role/development/) | Tests, linting, type checks, Ansible validation | + +## What it does + +- Discovers Compose projects that a remote Docker daemon already knows about. +- Captures the ordered Compose files, the root `.env`, contained `env_file` inputs, and contained + file-backed Compose secrets. +- Canonicalizes the project with the remote Compose implementation and classifies effective + environment values as secret or plain. +- Reports stable, value-free `C2Rxxx` findings and audits drift against an existing role. +- Renders the complete plan before touching the output directory, writes atomically, and refuses to + overwrite a differing or unmanaged role unless `--force` is given. +- Records the selected Docker context so the role deploys to the same daemon. + +It does not read or convert local Compose stacks, rewrite captured source, copy bind-mounted data, +follow paths or symlinks outside the project root, support Compose `include`, build contexts, +service `extends`, `profiles`, file-backed configs, or secret-shaped non-environment mapping keys, +create or guess a SOPS configuration, persist plaintext secret values, tear down an existing stack, +or generate playbooks. + +## Requirements + +- **Controller** — Python >= 3.12, an OpenSSH client, `sops`, and `gpg` when your SOPS backend uses + it. +- **Target host** — a running Docker daemon with Compose v2 or newer, an SSH account that can run + `docker compose ls`, `docker compose config`, and `docker ps`, and passwordless `sudo -n` when + `--sudo` is selected. +- **SOPS** — an operator-owned configuration with a creation rule matching `vars/secrets.sops.yml`. +- **To apply generated roles** — Ansible and the collections the role declares + (`community.docker` always, `community.sops` when the role contains secrets). Neither Ansible nor + its collections are needed to generate roles. + +Rootless daemons are common; name the daemon with a Docker context. See +[Docker context selection](https://www.sysmd.uk/docs/compose2role/workflow/#docker-context-selection). + +## Install + +### From PyPI + +```console +pipx install compose2role +# or +pip install compose2role +``` + +`pip` installs only the Python package. `ssh` and `sops` are external binaries and must already be +on `PATH`; `gpg` is needed when your SOPS backend uses it. Ansible and its collections are not +needed to generate roles. + +### From source + +```console +poetry install +poetry run compose2role --version +``` + +The package installs a `compose2role` console script. Examples in this document use +`poetry run compose2role` because they assume a checkout; if you installed the published package, +run `compose2role` directly. + +For generated-role validation, install the pinned test collections into the checkout: + +```console +poetry run ansible-galaxy collection install \ + --requirements-file tests/requirements.yml --collections-path collections +``` + +`collections/` is local, gitignored test state. + +## Quick start + +Replace the placeholders with your own host, raw project name, role name, and paths. Each step is +safe to repeat. + +### 1. Check the toolchain + +```console +poetry run compose2role doctor --host \ + --sops-config +``` + +A green host result means `discover` will reach the same daemon. `doctor` prints one line per check +with a remediation hint, then `N ok, N warnings, N errors`. + +### 2. Discover projects + +```console +poetry run compose2role discover --host --docker-context +``` + +Copy the raw project name exactly; it is the value for `--project`. Use `--format json` for +machine-readable output, or omit `--docker-context` to use the remote CLI's default context. + +### 3. Audit before writing + +```console +poetry run compose2role audit --host --project -v +``` + +Audit resolves the project through the same pipeline as generation, reports findings, and compares +the captured source with any existing role. It writes nothing and never encrypts anything. + +### 4. Preview the role + +```console +poetry run compose2role generate --host --project \ + --output --sops-config --check +``` + +`--check` renders the full plan and lists planned files without writing. Use `--diff` instead to +review value-free `added`, `changed`, `unchanged`, and `withheld` paths. + +### 5. Generate the role + +```console +poetry run compose2role generate --host --project \ + --output --sops-config +``` + +An unchanged project prints `unchanged` and writes nothing. A differing or unmanaged role is +refused unless `--force` is given, which backs up the previous role and swaps atomically. Use +`--all` instead of `--project` to generate every discovered project; each succeeds or fails +independently. + +### 6. Write and run a playbook + +Generation writes roles only. Write the play that applies one: + +```yaml +--- +- name: Deploy + hosts: + become: true # rootful daemons; remove or replace it for a rootless daemon + roles: + - +``` + +The role's tasks are tagged `source`, `secrets`, and `compose`: + +```console +ansible-playbook -i .yml --roles-path --tags source,secrets +ansible-playbook -i .yml --roles-path --tags compose +``` + +To deploy to a different host, override `_project_dir` and any external prerequisites. + +## Commands at a glance + +| Command | Purpose | Key options | +| --- | --- | --- | +| `doctor` | Diagnose the controller, configuration, roles, or a remote host. | `--host`, `--sudo`, `--docker-context`, `--format` | +| `discover` | List the projects the remote daemon knows about. | `--host`, `--docker-context`, `--format text\|table\|json` | +| `audit` | Resolve a project, report findings, and compare drift; writes nothing. | `--host`, `--project`/`--all`, `--fail-on`, `-v` | +| `generate` | Render and write roles. | `--host`, `--project`/`--all`, `--output`, `--sops-config`, `--check`, `--diff`, `--force` | + +All commands accept `--config` where meaningful. See the +[command reference](https://www.sysmd.uk/docs/compose2role/reference/) for the full option list, +JSON shapes, and exit codes. + +## Generated role + +```text +// +├── defaults/main.yml +├── tasks/main.yml +├── meta/main.yml +├── meta/requirements.yml +├── .compose2role.json +├── templates/ # captured source, mirroring original relative paths +└── vars/ + └── secrets.sops.yml # only when the role has secrets +``` + +`meta/requirements.yml` declares the collections the role needs. Install them on the Ansible +controller that applies the role: + +```console +ansible-galaxy collection install -r //meta/requirements.yml +``` + +`defaults/main.yml` defines `_project_dir`, the original raw name as +`_compose_project_name`, and `_docker_context` when one was selected. Captured non-secret +values become lower-case role variables; secret values live only in `vars/secrets.sops.yml`. +`.compose2role.json` records provenance: generator version, mode, names, source paths, external +prerequisites, and per-file hashes, never a secret value. + +See the [generated role reference](https://www.sysmd.uk/docs/compose2role/generated-role/) for +variable naming, task details, and linting. + +## Safety + +- Secret values may exist in controller memory while the plan is rendered, but never reach plaintext + role files, defaults, templates, provenance, logs, diffs, findings, exception text, or command + output. Only encrypted `vars/secrets.sops.yml` is persisted. +- The SOPS configuration is operator-owned; the tool never copies it or invents recipients. +- Captured inputs must be regular, non-symlinked files inside the project root. Bind-mounted data is + never read or copied. +- Generated roles apply with `state: present` and `remove_orphans: false`; they never remove + services, volumes, networks, or images. + +See [Safety and limitations](https://www.sysmd.uk/docs/compose2role/safety/) for the full refusal +rules and [Secrets and SOPS](https://www.sysmd.uk/docs/compose2role/secrets/) for classification +and policy details. + +## Development + +```console +poetry run pre-commit run --all-files +poetry run pytest -q +poetry run ruff check . +poetry run mypy +``` + +The pre-commit hooks run ruff, mypy, and file hygiene; pytest is not a hook. Enable them on +commit once with `poetry run pre-commit install`. + +The suite runs real `ansible-lint` and `ansible-playbook --syntax-check` against generated roles. +Without the pinned collections in `collections/`, those tests fail. Remote tests stub `ssh` and +`sops` through `PATH`, so they need no real host, network, sudo, or secrets. `mypy` checks only +`src` in strict mode. + +See [Development and validation](https://www.sysmd.uk/docs/compose2role/development/) for focused +tests and the dedicated Ansible environment setup. diff --git a/poetry.lock b/poetry.lock new file mode 100644 index 0000000..5bce6dc --- /dev/null +++ b/poetry.lock @@ -0,0 +1,1547 @@ +# This file is automatically @generated by Poetry 2.5.0 and should not be changed by hand. + +[[package]] +name = "ansible-compat" +version = "26.10.0" +description = "Ansible compatibility goodies" +optional = false +python-versions = ">=3.10" +groups = ["dev"] +files = [ + {file = "ansible_compat-26.10.0-py3-none-any.whl", hash = "sha256:700f0778fbb348daf6ee58941871e8ca77a987702993e8bf7895761d036d6450"}, + {file = "ansible_compat-26.10.0.tar.gz", hash = "sha256:b0fcc0b9585611045b4b91268d3b4337c9159ae0dc550b69a553a33af48af826"}, +] + +[package.dependencies] +ansible-core = ">=2.16.19,<2.17 || >=2.18.dev0" +jsonschema = ">=4.6.0" +packaging = ">=22.0" +pyyaml = ">=6.0.1" +subprocess-tee = ">=0.4.1" + +[[package]] +name = "ansible-core" +version = "2.21.5" +description = "Radically simple IT automation" +optional = false +python-versions = ">=3.12" +groups = ["dev"] +files = [ + {file = "ansible_core-2.21.5-py3-none-any.whl", hash = "sha256:08886f9f3e23d129aa98a01271c4a47833516e1cb7c737a246788cb474f41222"}, + {file = "ansible_core-2.21.5.tar.gz", hash = "sha256:65bb3148c58ebb843cd5074004c805f404f152114d98476dd823324616c9c6cb"}, +] + +[package.dependencies] +cryptography = "*" +jinja2 = ">=3.1.0" +packaging = "*" +PyYAML = ">=5.1" +resolvelib = ">=0.8.0,<2.0.0" + +[[package]] +name = "ansible-lint" +version = "26.10.1" +description = "Checks playbooks for practices and behavior that could potentially be improved" +optional = false +python-versions = ">=3.10" +groups = ["dev"] +files = [ + {file = "ansible_lint-26.10.1-py3-none-any.whl", hash = "sha256:cc26d405ee38c3390e80ec18c4137b5685c36d03ce7d4d8233b0470d9a0e21e9"}, + {file = "ansible_lint-26.10.1.tar.gz", hash = "sha256:a746c6f3cb5719d1c202bc14e956ef3b0eca704025d1aa92707a27fc73ed73ea"}, +] + +[package.dependencies] +ansible-compat = ">=26.3.0" +ansible-core = ">=2.16.19,<2.17 || >=2.18.dev0" +black = ">=25.2.0" +cffi = ">=1.15.1" +cryptography = ">=46.0.7" +distro = ">=1.9.0" +filelock = ">=3.8.2" +jsonschema = ">=4.10.0" +packaging = ">=22.0" +pathspec = ">=1.0.3,<1.2.0" +pyyaml = ">=6.0.1" +referencing = ">=0.36.2" +ruamel-yaml = ">=0.18.11" +subprocess-tee = ">=0.4.1" +wcmatch = {version = ">=8.5.0", markers = "python_version >= \"3.12\""} +yamllint = ">=1.38.0" + +[[package]] +name = "attrs" +version = "26.1.0" +description = "Classes Without Boilerplate" +optional = false +python-versions = ">=3.9" +groups = ["dev"] +files = [ + {file = "attrs-26.1.0-py3-none-any.whl", hash = "sha256:c647aa4a12dfbad9333ca4e71fe62ddc36f4e63b2d260a37a8b83d2f043ac309"}, + {file = "attrs-26.1.0.tar.gz", hash = "sha256:d03ceb89cb322a8fd706d4fb91940737b6642aa36998fe130a9bc96c985eff32"}, +] + +[[package]] +name = "black" +version = "26.10.1" +description = "The uncompromising code formatter." +optional = false +python-versions = ">=3.10" +groups = ["dev"] +files = [ + {file = "black-26.10.1-cp310-cp310-macosx_10_9_x86_64.whl", hash = "sha256:51d5e417e700fe6ec0b0ecdc408c6f6cb5def80328f31f724993d82c6486b746"}, + {file = "black-26.10.1-cp310-cp310-macosx_11_0_arm64.whl", hash = "sha256:70ccbd175b7f6be29d2b727ee7ca6b4c54053df59da653a6df80b175d20a94fa"}, + {file = "black-26.10.1-cp310-cp310-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:d5bd3518d8e97138fef295230b1e9804076d69fa4e3594071494a8c68abe6266"}, + {file = "black-26.10.1-cp310-cp310-win_amd64.whl", hash = "sha256:0ce08b367307b0fd91c9dd1d4084e62b05b3055475f951f0f34a46b6e2393b64"}, + {file = "black-26.10.1-cp310-cp310-win_arm64.whl", hash = "sha256:19fa8f5beb5e77c54c9c7e21d00cc93ed6c8b6228ee385616906d6befe081143"}, + {file = "black-26.10.1-cp311-cp311-macosx_10_9_x86_64.whl", hash = "sha256:9a0219b29cd70e49f920acb7081e6ce5025c719008447c521d0200dcad93206a"}, + {file = "black-26.10.1-cp311-cp311-macosx_11_0_arm64.whl", hash = "sha256:7bdade400bfe24d78a7762896acc2f9a8e1a17fb0fd0536bf6b7c7097cf3eec7"}, + {file = "black-26.10.1-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:ff57f63029aa1353fa8b1b0c8971fd88a6c92dc766608d2eee33ad2deb23270e"}, + {file = "black-26.10.1-cp311-cp311-win_amd64.whl", hash = "sha256:3414a0c52901964dceabd98c7c56beac0f964115a116ecedcce7247359b14017"}, + {file = "black-26.10.1-cp311-cp311-win_arm64.whl", hash = "sha256:1935b32f5326028019856e18cb42b4da63db23765dc84464cec723e0de478a9b"}, + {file = "black-26.10.1-cp312-cp312-macosx_10_13_x86_64.whl", hash = "sha256:fe85fc4019bee59bc495c0f2a8ee76c5cd02c7015508d94a967ba2376f39a52c"}, + {file = "black-26.10.1-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:182f6c32be38074b16d378498c498b32cb51928178ee611485344972c35ec9c6"}, + {file = "black-26.10.1-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:b5347d760f0c02bb00dd249384cab71c3bf828b4f68d5b401eb116e0390f147d"}, + {file = "black-26.10.1-cp312-cp312-win_amd64.whl", hash = "sha256:4d9a90516db1d99c25dbb20cc0998e0e01531dd903466c7744e56d66f864220a"}, + {file = "black-26.10.1-cp312-cp312-win_arm64.whl", hash = "sha256:2ffbc023a12d0c729408823b8f10514490bd0baa301d0d4e21a7240249f9507f"}, + {file = "black-26.10.1-cp313-cp313-macosx_10_13_x86_64.whl", hash = "sha256:b6272cfd7e1e8e271f5b0e0207259fe2834687e5cb9b5f620b34a44db9754993"}, + {file = "black-26.10.1-cp313-cp313-macosx_11_0_arm64.whl", hash = "sha256:978113a40223a6aaefc17364176a809a320e6b288683841427fff04c6d7b4130"}, + {file = "black-26.10.1-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:03c0ddd93bb392e71209903a691767eb366fe1a76deb9509ccbaae9e1f14bb52"}, + {file = "black-26.10.1-cp313-cp313-win_amd64.whl", hash = "sha256:f6dba8138cdc99061ef07b958ac082d2aa057b6961d1936f9717c350f02bab5f"}, + {file = "black-26.10.1-cp313-cp313-win_arm64.whl", hash = "sha256:d42dd2fac7c342ae67e64ee99c9532e20b2a84e92c79ed3317fa2ef54c801d93"}, + {file = "black-26.10.1-cp314-cp314-macosx_10_15_x86_64.whl", hash = "sha256:8375962579d537364cc0efa19b1474481915d3a793f9fc0774901814c5e5b5f4"}, + {file = "black-26.10.1-cp314-cp314-macosx_11_0_arm64.whl", hash = "sha256:d8b3a9074a680b3c5749633714e9ae3992a1e5a23343a97ad61cd9b119b444d2"}, + {file = "black-26.10.1-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:289282aa2e09d3162312a3be1788ff21b08e9ea9cc4a81e656024728b32428fb"}, + {file = "black-26.10.1-cp314-cp314-win_amd64.whl", hash = "sha256:5cd88fd7b444ca51f3fc883b6f6657ea53a258b0b2eef6d9f2dfcfa17ce0e27b"}, + {file = "black-26.10.1-cp314-cp314-win_arm64.whl", hash = "sha256:2520037aa62f8a1454d0811b8f5c88b444445b03a4bfba480d8d220893b64c34"}, + {file = "black-26.10.1-py3-none-any.whl", hash = "sha256:28842f9a8207cc1df6eb983a35a14c5a0dfcd603d214fe82d84bef552afd2e3a"}, + {file = "black-26.10.1.tar.gz", hash = "sha256:5f9f83beae62437e060dafd53d7f1fc327e3d3494f74d72ee5c2b73eb90fc4e7"}, +] + +[package.dependencies] +click = ">=8.0.0" +mypy-extensions = ">=0.4.3" +packaging = ">=22.0" +pathspec = ">=1.0.0" +platformdirs = ">=2" +pytokens = ">=0.4.0,<0.5.0" + +[package.extras] +colorama = ["colorama (>=0.4.3)"] +d = ["aiohttp (>=3.10)"] +jupyter = ["ipython (>=7.8.0)", "tokenize-rt (>=3.2.0)"] +uvloop = ["uvloop (>=0.15.2) ; sys_platform != \"win32\"", "winloop (>=0.5.0) ; sys_platform == \"win32\""] + +[[package]] +name = "bracex" +version = "3.2" +description = "Bash style brace expander." +optional = false +python-versions = ">=3.11" +groups = ["dev"] +files = [ + {file = "bracex-3.2-py3-none-any.whl", hash = "sha256:8f3e908a452f66707e1d6d91b3d7d7d4c649d248df18537c0dc710aeb9c30d87"}, + {file = "bracex-3.2.tar.gz", hash = "sha256:0a3d40bfae4caae4d3bfd0ea715874799fab711c38fd4012cc35343e815405cd"}, +] + +[[package]] +name = "cffi" +version = "2.1.1" +description = "Foreign Function Interface for Python calling C code." +optional = false +python-versions = ">=3.10" +groups = ["dev"] +files = [ + {file = "cffi-2.1.1-cp310-cp310-macosx_10_15_x86_64.whl", hash = "sha256:baed1e86cc735622097354b9d1281406caf42ff42a886d29faa8e8d1630333be"}, + {file = "cffi-2.1.1-cp310-cp310-macosx_11_0_arm64.whl", hash = "sha256:ca82be1a1d406ecfe1d25dc16cb33488e5a16bf4438c9fb590484ea29d92478b"}, + {file = "cffi-2.1.1-cp310-cp310-manylinux1_i686.manylinux2014_i686.manylinux_2_17_i686.manylinux_2_5_i686.whl", hash = "sha256:42e2f76b9455f5a9a844f770bf3e200ed3da0e15f5df3db9c31fe80b04b3d004"}, + {file = "cffi-2.1.1-cp310-cp310-manylinux2014_aarch64.manylinux_2_17_aarch64.whl", hash = "sha256:5a59cc1c4442bc3d5c703bf720b51138d0bfc173618807c9ee2490a7541dd3d9"}, + {file = "cffi-2.1.1-cp310-cp310-manylinux2014_ppc64le.manylinux_2_17_ppc64le.whl", hash = "sha256:9f8d177621de5cb38ee3e731eda45d421db093ec0739f46a5594babda7987a98"}, + {file = "cffi-2.1.1-cp310-cp310-manylinux2014_s390x.manylinux_2_17_s390x.whl", hash = "sha256:75f80557d1389eddbd0de2681f6a390a0c5338c31ddaa821381c203fc3fd50d9"}, + {file = "cffi-2.1.1-cp310-cp310-manylinux2014_x86_64.manylinux_2_17_x86_64.whl", hash = "sha256:194cffa889098ced9976c3fc6340305e43f6303657d298da55366907c05c22d6"}, + {file = "cffi-2.1.1-cp310-cp310-musllinux_1_2_aarch64.whl", hash = "sha256:5bb4e7ea95dcd6a014a6fef62e62467d67d8e582326443f3d68e71d6320a9fcf"}, + {file = "cffi-2.1.1-cp310-cp310-musllinux_1_2_i686.whl", hash = "sha256:3d22a20b1fb1632cc72c22f95f7b0d2961c3e1c235f245ba4c606c4771035659"}, + {file = "cffi-2.1.1-cp310-cp310-musllinux_1_2_x86_64.whl", hash = "sha256:1dea0e4d7d4f11f619fe8c1d76caf49e24405b4b5743c0e3be16a500ecd930c9"}, + {file = "cffi-2.1.1-cp310-cp310-win32.whl", hash = "sha256:7ce713ace7c0e4520535b42b77eaa742c16dab813978064913e5a3cf82973b41"}, + {file = "cffi-2.1.1-cp310-cp310-win_amd64.whl", hash = "sha256:a48d62ab9d6f4f98c983223a547af44be6ca3691074c31cecced6facd3ba2dc1"}, + {file = "cffi-2.1.1-cp311-cp311-macosx_10_15_x86_64.whl", hash = "sha256:c8d2c9fd1f2d16f780d15127abb050d13d1a76c03a4bd87d7e4980e45e511e12"}, + {file = "cffi-2.1.1-cp311-cp311-macosx_11_0_arm64.whl", hash = "sha256:398aff33cee2767e3e781d2554c54bd0dff386bb437581e0d8011fde1a942ec1"}, + {file = "cffi-2.1.1-cp311-cp311-manylinux1_i686.manylinux2014_i686.manylinux_2_17_i686.manylinux_2_5_i686.whl", hash = "sha256:154852545011f779917b11c78db2358d095da62a9a172b78ad0a583ee5adc0d0"}, + {file = "cffi-2.1.1-cp311-cp311-manylinux2014_aarch64.manylinux_2_17_aarch64.whl", hash = "sha256:3311ed60d36f83378794e1009ac6258bafbf81f7888b4caa7b35a521e3f95813"}, + {file = "cffi-2.1.1-cp311-cp311-manylinux2014_ppc64le.manylinux_2_17_ppc64le.whl", hash = "sha256:6e192623c49c94421616a5778fba35cf0d5a8d000650c1967ef4448ee5cdd990"}, + {file = "cffi-2.1.1-cp311-cp311-manylinux2014_s390x.manylinux_2_17_s390x.whl", hash = "sha256:a6e721d4b0e45d5b65e87534470e67b18dcd092c83f68fba09f152b9cbc061af"}, + {file = "cffi-2.1.1-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.whl", hash = "sha256:34e261f78cb6ceaaa36f42f2613f4380d94d9c759a9c73c769ee6e0247364632"}, + {file = "cffi-2.1.1-cp311-cp311-musllinux_1_2_aarch64.whl", hash = "sha256:7225e4514edb64eb6740324353e0da0711954fd8d7da4576755b1c6e09b697cd"}, + {file = "cffi-2.1.1-cp311-cp311-musllinux_1_2_i686.whl", hash = "sha256:df913725b79db7bcf03448f36b7bf8815363417d5b58deecf9305e3e30f0f21a"}, + {file = "cffi-2.1.1-cp311-cp311-musllinux_1_2_x86_64.whl", hash = "sha256:f5cfbc5fe74540d335175b656c725d74d90e3730c626d92575eea35029d9afaa"}, + {file = "cffi-2.1.1-cp311-cp311-win32.whl", hash = "sha256:f8ec5e643a9a937f64e1999eb9f75d072263751912dc5cd06d3c85f8f44be7c3"}, + {file = "cffi-2.1.1-cp311-cp311-win_amd64.whl", hash = "sha256:42f6930c31dc7f50732c9ae793c2786c7b6b044195967bbdde40bb9be81c4cc0"}, + {file = "cffi-2.1.1-cp311-cp311-win_arm64.whl", hash = "sha256:c7659f22557c5a0bc4855cd635f55edec690cc008a40768527762cb9fb263455"}, + {file = "cffi-2.1.1-cp312-cp312-macosx_10_15_x86_64.whl", hash = "sha256:c8c69575568085ba0b1b10c0249d779a214aea6f6522e949a0fc9fb0fcb449d0"}, + {file = "cffi-2.1.1-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:f81b3b8f3d4e343550fa4baa0e479bba9f2d29ce9c2e9b51d1ce1718d7442fcf"}, + {file = "cffi-2.1.1-cp312-cp312-manylinux1_i686.manylinux2014_i686.manylinux_2_17_i686.manylinux_2_5_i686.whl", hash = "sha256:811bd1e21d32de12efca32393a0ab3f5133b54fce9bd44b8bd77ab07da14bf6a"}, + {file = "cffi-2.1.1-cp312-cp312-manylinux2014_aarch64.manylinux_2_17_aarch64.whl", hash = "sha256:68e62fe11f30d5ca8289242866f0a5291402d8529ca2178ab8afc5c9694ae890"}, + {file = "cffi-2.1.1-cp312-cp312-manylinux2014_ppc64le.manylinux_2_17_ppc64le.whl", hash = "sha256:4a7c934f7360e8cd64fe9efadcbd10c7c6364f531e432b9a4bf5ccbc9e0e8b50"}, + {file = "cffi-2.1.1-cp312-cp312-manylinux2014_s390x.manylinux_2_17_s390x.whl", hash = "sha256:3143d81e29e1e20a9ce10901ec369012947876596f75a222235965f2b7ae832e"}, + {file = "cffi-2.1.1-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.whl", hash = "sha256:c1453022f490d2459a11819d83ad1d586e9ff65a12ac3e705ffebd46d3685dcf"}, + {file = "cffi-2.1.1-cp312-cp312-musllinux_1_2_aarch64.whl", hash = "sha256:208f941bb9d18e768138677f0a6d2ce01f590df56043dda1df1535ac57c88517"}, + {file = "cffi-2.1.1-cp312-cp312-musllinux_1_2_x86_64.whl", hash = "sha256:210019b6c7cf07f081b4c54635c8cf744377001350e29cc0f81c4377b4797735"}, + {file = "cffi-2.1.1-cp312-cp312-win32.whl", hash = "sha256:046bfc24911b37851ee1b51aab8bffe713d89c68c6a057b09484ce9fd5f69b4e"}, + {file = "cffi-2.1.1-cp312-cp312-win_amd64.whl", hash = "sha256:f53e442b08449d42821fa4a4fba000095af9f62742a500f978a9f557ec44339a"}, + {file = "cffi-2.1.1-cp312-cp312-win_arm64.whl", hash = "sha256:7bde5e4cc5c10140859842b9d383af292b22639a4dffb725314baf45968cef80"}, + {file = "cffi-2.1.1-cp313-cp313-ios_13_0_arm64_iphoneos.whl", hash = "sha256:b5bdfd1c873d4e093aabc0ca84c4ca6dbc4f752afb5c86f146d9742580c9da2e"}, + {file = "cffi-2.1.1-cp313-cp313-ios_13_0_arm64_iphonesimulator.whl", hash = "sha256:31348097ff5bbe827ccc41795d4dd099d9f0625e7def00ee653c137a490c2a6c"}, + {file = "cffi-2.1.1-cp313-cp313-macosx_10_15_x86_64.whl", hash = "sha256:9d2055050ea716bd38b7f7f1579c275386646b4894c155a3e2f3cd62ed41b7c6"}, + {file = "cffi-2.1.1-cp313-cp313-macosx_11_0_arm64.whl", hash = "sha256:19ee6127ee34de7d83ce3d371ebc5ed91addbdcc39f9ab15ce4eb35a4e534971"}, + {file = "cffi-2.1.1-cp313-cp313-manylinux1_i686.manylinux2014_i686.manylinux_2_17_i686.manylinux_2_5_i686.whl", hash = "sha256:6a8dddef476fab96d066d578fc88526767b836ab5ab21754e1d5bf3879c31c7c"}, + {file = "cffi-2.1.1-cp313-cp313-manylinux2014_aarch64.manylinux_2_17_aarch64.whl", hash = "sha256:f16c709686a78c727bbbf059f92b0bf41c6fc60deec706d2dc19f529175a6125"}, + {file = "cffi-2.1.1-cp313-cp313-manylinux2014_ppc64le.manylinux_2_17_ppc64le.whl", hash = "sha256:fcd22650c908d7b7da162bbfaab594a1227a15d1643a98c68b122ac642fa2264"}, + {file = "cffi-2.1.1-cp313-cp313-manylinux2014_s390x.manylinux_2_17_s390x.whl", hash = "sha256:aa9511c62d14da7aacc9b4bf51f3f697a621e83b2d6919008243c3aad168eea3"}, + {file = "cffi-2.1.1-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.whl", hash = "sha256:a931079504ecc49efed7744c476a5c343a92fabf66dec2db95edb1b2fdc770e2"}, + {file = "cffi-2.1.1-cp313-cp313-musllinux_1_2_aarch64.whl", hash = "sha256:a2d7755bef5a12ed488f4ef1f1b69ee9191d7396083b755a5d2295f6edb4768b"}, + {file = "cffi-2.1.1-cp313-cp313-musllinux_1_2_x86_64.whl", hash = "sha256:e0bcb7e0f677f543555d2adff3bf19c05f66cdb4796e5ff602442ab2fe3c4ef7"}, + {file = "cffi-2.1.1-cp313-cp313-win32.whl", hash = "sha256:334644fbac4eff73d985a17a91226df55d0f394160c4cfb880e084c8f7161cac"}, + {file = "cffi-2.1.1-cp313-cp313-win_amd64.whl", hash = "sha256:1aa5645c30469b09530c4ebca77ebf8f17618293c58f8549cb1a543a50236e7d"}, + {file = "cffi-2.1.1-cp313-cp313-win_arm64.whl", hash = "sha256:63bbfd5ded17c4840ac07cd8f1c21ba9d9708141f840b324f422f41b207e3973"}, + {file = "cffi-2.1.1-cp314-cp314-ios_13_0_arm64_iphoneos.whl", hash = "sha256:7dbb61fe3a7699468030f71bbe5f8a0e326a151daa91beb11a6fc1f980c55e1c"}, + {file = "cffi-2.1.1-cp314-cp314-ios_13_0_arm64_iphonesimulator.whl", hash = "sha256:f24fb43132a4c6b4cb4eb029492919b2db645be6808d738f244fd146c03c32cb"}, + {file = "cffi-2.1.1-cp314-cp314-macosx_10_15_x86_64.whl", hash = "sha256:d28630f5854ab07ab1fd4aba756de52326c82e6be15d414b12793f1975048b54"}, + {file = "cffi-2.1.1-cp314-cp314-macosx_11_0_arm64.whl", hash = "sha256:661c298b4821edebead0c91edd2b00374d67ad7c5a1f7a91d4442633b79d6a72"}, + {file = "cffi-2.1.1-cp314-cp314-manylinux2014_aarch64.manylinux_2_17_aarch64.whl", hash = "sha256:58acb8ab8e295e6c5ea12f888cbb13cf21511ef2a3303a23f4325c29d17fe5c1"}, + {file = "cffi-2.1.1-cp314-cp314-manylinux2014_ppc64le.manylinux_2_17_ppc64le.whl", hash = "sha256:456a61fa52d579ebf9df2e9552ead5129855dbaff6c1e5a9b1bc408809bdc062"}, + {file = "cffi-2.1.1-cp314-cp314-manylinux2014_s390x.manylinux_2_17_s390x.whl", hash = "sha256:a4f00aa42f75d6e4595e8866e748cc1705adc0cddfeb2ca86d0d03993d63ba03"}, + {file = "cffi-2.1.1-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.whl", hash = "sha256:b0431303acaea1089ad4b3e9ce4e6518193def1118d4073ca848635ee4ea2e96"}, + {file = "cffi-2.1.1-cp314-cp314-musllinux_1_2_aarch64.whl", hash = "sha256:64faea20f4e2613363a1a9b9c7dd73058f3ecd00133a511e72ad7c511658f527"}, + {file = "cffi-2.1.1-cp314-cp314-musllinux_1_2_x86_64.whl", hash = "sha256:5c58fe613dc5e5336357eff555824a314d8e43282600435c8d1cb6a7a2fedd13"}, + {file = "cffi-2.1.1-cp314-cp314-win32.whl", hash = "sha256:1a18a57b58cfb21fc28d72e876acf10eaed67a1ed96226f92af4df681d571c4c"}, + {file = "cffi-2.1.1-cp314-cp314-win_amd64.whl", hash = "sha256:3222ba5d678f80a030e6afbcc33dc1ae5cb45facabb61cee2c7016b8432fde48"}, + {file = "cffi-2.1.1-cp314-cp314-win_arm64.whl", hash = "sha256:ab36d55f9ed2d067327667c2fea18dda018eb628dd6347aa01dda6cf1f5d3836"}, + {file = "cffi-2.1.1-cp314-cp314t-macosx_10_15_x86_64.whl", hash = "sha256:7750c6449dff7864bb9bb27ddfb0267756189201a3afc911d82b3caacd70dfc3"}, + {file = "cffi-2.1.1-cp314-cp314t-macosx_11_0_arm64.whl", hash = "sha256:0beceaabe56af686895136a2de78db54ecd8e4046b236b8fd6d6cb61389e9bf2"}, + {file = "cffi-2.1.1-cp314-cp314t-manylinux2014_aarch64.manylinux_2_17_aarch64.whl", hash = "sha256:49cbc70e6542d4ccccb936558d1064a8012541e78f821f955cff24e357776c94"}, + {file = "cffi-2.1.1-cp314-cp314t-manylinux2014_ppc64le.manylinux_2_17_ppc64le.whl", hash = "sha256:e2d65b31f36619cda3999b78b2aa9632e76b78448e7a56fc4240824200e7c4fc"}, + {file = "cffi-2.1.1-cp314-cp314t-manylinux2014_s390x.manylinux_2_17_s390x.whl", hash = "sha256:28907ab9bfb6aa13184cfc17c6b8e1023c5ab6fd7076d8c20a35e59fe04f8f29"}, + {file = "cffi-2.1.1-cp314-cp314t-manylinux2014_x86_64.manylinux_2_17_x86_64.whl", hash = "sha256:51b31d1c98274844cfd7838ce00bfc27c7423a4dc00fc0772fc3331c2cc90676"}, + {file = "cffi-2.1.1-cp314-cp314t-musllinux_1_2_aarch64.whl", hash = "sha256:5e7cecbaadb83884793e05828cee59b210b24583b9c7425d0ba6a754fe22eb4e"}, + {file = "cffi-2.1.1-cp314-cp314t-musllinux_1_2_x86_64.whl", hash = "sha256:25792eac27877609e7bb06d42ff88278a6624fff2ba9bbb523c09616b117e80f"}, + {file = "cffi-2.1.1-cp314-cp314t-win32.whl", hash = "sha256:8ef53b2de9bcb9197d31854256575d59dbac0cba72ac627bb291ef5eceb74be4"}, + {file = "cffi-2.1.1-cp314-cp314t-win_amd64.whl", hash = "sha256:616f097f2fe415bc92a247f02e11f634e1f9e9a83d327e3c915c15089c87869e"}, + {file = "cffi-2.1.1-cp314-cp314t-win_arm64.whl", hash = "sha256:ad2c86c495b899d862ea0f4b42891b8713a3bd45dd4105c7fd51c2a72f39f3a5"}, + {file = "cffi-2.1.1-cp315-cp315-ios_13_0_arm64_iphoneos.whl", hash = "sha256:dddad92b554513a31f272570678ba307fb9f618f05e3d4a5eacafff9eae03e1d"}, + {file = "cffi-2.1.1-cp315-cp315-ios_13_0_arm64_iphonesimulator.whl", hash = "sha256:da0e573f9f97159390c89d9f1a9e41908b66d408cc5b58d08cf3847d844c531b"}, + {file = "cffi-2.1.1-cp315-cp315-macosx_10_15_x86_64.whl", hash = "sha256:fb92203a88b3d3053034db775110081c49d28be6551923805e039924093761e4"}, + {file = "cffi-2.1.1-cp315-cp315-macosx_11_0_arm64.whl", hash = "sha256:2ae64be792b8966f2c69538199728b290e34726562896df1e5dc8ffd8d8188e8"}, + {file = "cffi-2.1.1-cp315-cp315-manylinux2014_aarch64.manylinux_2_17_aarch64.whl", hash = "sha256:507a24c282e0f42f8ed737cf048572cbf580468da5555764a8331735e9c736b6"}, + {file = "cffi-2.1.1-cp315-cp315-manylinux2014_ppc64le.manylinux_2_17_ppc64le.whl", hash = "sha256:246fa40ce8645a614ff682e0b70f37134e460eaf93a775e0cbe3cca585a67a80"}, + {file = "cffi-2.1.1-cp315-cp315-manylinux2014_s390x.manylinux_2_17_s390x.whl", hash = "sha256:471cee653ae88de62096552e6d24ccb4a5adb8c8c9f10b5054d0122c15bf2779"}, + {file = "cffi-2.1.1-cp315-cp315-manylinux2014_x86_64.manylinux_2_17_x86_64.whl", hash = "sha256:aeae0e330c9f6acd681f647d46cefd30c29f93e3392882e792e82080c9691399"}, + {file = "cffi-2.1.1-cp315-cp315-musllinux_1_2_aarch64.whl", hash = "sha256:42a494cee34437f05546455144f2b5d9ac09b1face62bcfce597d2e521066688"}, + {file = "cffi-2.1.1-cp315-cp315-musllinux_1_2_x86_64.whl", hash = "sha256:cc572dace3f60ef98d7b12ff411d20f5362feb31a0439eab0085bbfd349982d7"}, + {file = "cffi-2.1.1-cp315-cp315-win32.whl", hash = "sha256:4f42141fc14250de6dde5ee7ea4432be017252d91f19c5ad043c084cea629cac"}, + {file = "cffi-2.1.1-cp315-cp315-win_amd64.whl", hash = "sha256:e6e8cff14d6fb0be70a09c0bdc58096f501952d04624ebf867e0e56da2df8960"}, + {file = "cffi-2.1.1-cp315-cp315-win_arm64.whl", hash = "sha256:27350daa11d4f10c540e6e89dada4c54feb7256ad03e9a4dc075ebad7ba360d1"}, + {file = "cffi-2.1.1-cp315-cp315t-macosx_10_15_x86_64.whl", hash = "sha256:c26608d2222fb1e94487e4a387d85f13eb55d5ed725cb25a0c589ac4ee60e7bc"}, + {file = "cffi-2.1.1-cp315-cp315t-macosx_11_0_arm64.whl", hash = "sha256:4be96343e422f2dfcd12ab5c9f5aebe03f82f737c6bffeca6830b3875cb44aab"}, + {file = "cffi-2.1.1-cp315-cp315t-manylinux2014_aarch64.manylinux_2_17_aarch64.whl", hash = "sha256:937c0052c05a31ca1daf18de3158eed4dbfcb9cc107adbea227728d647be701e"}, + {file = "cffi-2.1.1-cp315-cp315t-manylinux2014_ppc64le.manylinux_2_17_ppc64le.whl", hash = "sha256:df423d40ee8654634421812bc3b196da3f9bd7d32929da813f8394c4348a5358"}, + {file = "cffi-2.1.1-cp315-cp315t-manylinux2014_s390x.manylinux_2_17_s390x.whl", hash = "sha256:a730a083190634c65cca36ba5f489531576ebd79bcd5c8e172130f6453127231"}, + {file = "cffi-2.1.1-cp315-cp315t-manylinux2014_x86_64.manylinux_2_17_x86_64.whl", hash = "sha256:363e05fa78e15116c3c32c210ee36884fd6b9afa6d440e47112c3bd511d64cb6"}, + {file = "cffi-2.1.1-cp315-cp315t-musllinux_1_2_aarch64.whl", hash = "sha256:770de9db11e84213beec501cfcaa013b019820ca881e03344dea5844f7876d94"}, + {file = "cffi-2.1.1-cp315-cp315t-musllinux_1_2_x86_64.whl", hash = "sha256:7da0c5eff80f0197f3b3d1232ec5a682a9325f4ae9016a78f5f5ca35f9ced1f5"}, + {file = "cffi-2.1.1-cp315-cp315t-win32.whl", hash = "sha256:06c72bb76605a4b0cd0aad6930b69d4baf7dd5d806cfc409b824191099700e66"}, + {file = "cffi-2.1.1-cp315-cp315t-win_amd64.whl", hash = "sha256:d9c275eaacd24aa73f94ffd6de08fc3f932424d8b6c376f4bed7cde376fe7bc3"}, + {file = "cffi-2.1.1-cp315-cp315t-win_arm64.whl", hash = "sha256:d18e5ac0f2f03f4f518d3e23db0f0cad7faa1da8620e9c09461d443bbf6e6692"}, + {file = "cffi-2.1.1.tar.gz", hash = "sha256:dd31f52ea1086513bb9df30f8fcee9b8918323ae067a3d5b78bc826a000712be"}, +] + +[package.dependencies] +pycparser = {version = "*", markers = "implementation_name != \"PyPy\""} + +[[package]] +name = "cfgv" +version = "3.5.0" +description = "Validate configuration and produce human readable error messages." +optional = false +python-versions = ">=3.10" +groups = ["dev"] +files = [ + {file = "cfgv-3.5.0-py2.py3-none-any.whl", hash = "sha256:a8dc6b26ad22ff227d2634a65cb388215ce6cc96bbcc5cfde7641ae87e8dacc0"}, + {file = "cfgv-3.5.0.tar.gz", hash = "sha256:d5b1034354820651caa73ede66a6294d6e95c1b00acc5e9b098e917404669132"}, +] + +[[package]] +name = "click" +version = "8.5.0" +description = "Composable command line interface toolkit" +optional = false +python-versions = ">=3.10" +groups = ["main", "dev"] +files = [ + {file = "click-8.5.0-py3-none-any.whl", hash = "sha256:255bc9599cf7748b4b1a446ccc735421bd08a2ae529a8b88597d3de5664ee360"}, + {file = "click-8.5.0.tar.gz", hash = "sha256:ba0d2089de75ea0310e2dde03160e6ca10009947fb95a182f9b54021bb272e34"}, +] + +[[package]] +name = "colorama" +version = "0.4.6" +description = "Cross-platform colored terminal text." +optional = false +python-versions = "!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,!=3.4.*,!=3.5.*,!=3.6.*,>=2.7" +groups = ["dev"] +markers = "sys_platform == \"win32\"" +files = [ + {file = "colorama-0.4.6-py2.py3-none-any.whl", hash = "sha256:4f1d9991f5acc0ca119f9d443620b77f9d6b33703e51011c16baf57afb285fc6"}, + {file = "colorama-0.4.6.tar.gz", hash = "sha256:08695f5cb7ed6e0531a20572697297273c47b8cae5a63ffc6d6ed5c201be6e44"}, +] + +[[package]] +name = "cryptography" +version = "50.0.2" +description = "cryptography is a package which provides cryptographic recipes and primitives to Python developers." +optional = false +python-versions = "!=3.9.0,!=3.9.1,>=3.9" +groups = ["dev"] +files = [ + {file = "cryptography-50.0.2-cp311-abi3-macosx_11_0_arm64.whl", hash = "sha256:fa8f5efb344d6908a1ce62f4a24e2e5780f825d6f53f5f50ec5ffacac72936cb"}, + {file = "cryptography-50.0.2-cp311-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl", hash = "sha256:79def8d059362e7831389ed3be0ecdf58a89386e1271e35dd9f5af84e81bffd0"}, + {file = "cryptography-50.0.2-cp311-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl", hash = "sha256:630ebfea3bf689d075f82316324ff7433dc447fe6bc1bfc76524b74b4a9567d2"}, + {file = "cryptography-50.0.2-cp311-abi3-manylinux_2_28_aarch64.whl", hash = "sha256:f9f6143a8c75945eb960d9eb98905a441394abfa24afaae239d514ffb2586480"}, + {file = "cryptography-50.0.2-cp311-abi3-manylinux_2_28_ppc64le.whl", hash = "sha256:a582ab2ae1d34f67112cadc86702774c9ea4374df6bca6afe672817203c99134"}, + {file = "cryptography-50.0.2-cp311-abi3-manylinux_2_28_x86_64.whl", hash = "sha256:4061c0079120205fb760c58acab6443e217307dcf05e3702cf970e0689972856"}, + {file = "cryptography-50.0.2-cp311-abi3-manylinux_2_31_armv7l.whl", hash = "sha256:ac9ed99d81760c62fe89d5f0815cdfa1ba9a35141cf30f1c2d044f04b4803d2e"}, + {file = "cryptography-50.0.2-cp311-abi3-manylinux_2_34_aarch64.whl", hash = "sha256:87e9ce85beb6b328ba370cc6e6aea483c92617b4c95b1d33a49297eb662bfb04"}, + {file = "cryptography-50.0.2-cp311-abi3-manylinux_2_34_ppc64le.whl", hash = "sha256:f265528741e048bce55c3463ed721fb0aa45a5888d8add8cfeccb3035451bbdc"}, + {file = "cryptography-50.0.2-cp311-abi3-manylinux_2_34_x86_64.whl", hash = "sha256:9dab55f57c74c3cad24c323bacbbd04be4705ba6eb0d92e920b1fc4837ed5079"}, + {file = "cryptography-50.0.2-cp311-abi3-musllinux_1_2_aarch64.whl", hash = "sha256:25784ce8b9621c90c643efb9e1e2162ab3b0224cae446ad5e70e7fcb1ce18b51"}, + {file = "cryptography-50.0.2-cp311-abi3-musllinux_1_2_x86_64.whl", hash = "sha256:85d0d9a31b9098e98534226d5686b47264b95e62ce459dc2e62fdfc809f9fe93"}, + {file = "cryptography-50.0.2-cp311-abi3-win_amd64.whl", hash = "sha256:7afa5a6602a9f29af1f3a2965f831bae7c9d5d597b7cbb716d41ab3b7d89879c"}, + {file = "cryptography-50.0.2-cp314-cp314t-macosx_11_0_arm64.whl", hash = "sha256:f785f6161f202ab04d8ca194158968798e480ca058943907972da5f12e2881e8"}, + {file = "cryptography-50.0.2-cp314-cp314t-manylinux2014_aarch64.manylinux_2_17_aarch64.whl", hash = "sha256:0ecbc5652bdb6fc9eaf89a7d196e20941adfe812f43bc4ca05d9150496821047"}, + {file = "cryptography-50.0.2-cp314-cp314t-manylinux2014_x86_64.manylinux_2_17_x86_64.whl", hash = "sha256:ab50ee449bf968271e820086f10a33d101dd060370abc10bcd22279be2656539"}, + {file = "cryptography-50.0.2-cp314-cp314t-manylinux_2_28_aarch64.whl", hash = "sha256:a9f7355e6fab51f6c369b86fb7571cffa05edee2c2121e0380a37fb9ac1cd5c1"}, + {file = "cryptography-50.0.2-cp314-cp314t-manylinux_2_28_ppc64le.whl", hash = "sha256:94e5e9f108ee10471288214d3d233fbfbb492840a8457eb85178d643ddeb32c7"}, + {file = "cryptography-50.0.2-cp314-cp314t-manylinux_2_28_x86_64.whl", hash = "sha256:241449bf940a5d27309bd317e6f9a2af6932113818bb2b8f5c59ddc7ef16da18"}, + {file = "cryptography-50.0.2-cp314-cp314t-manylinux_2_31_armv7l.whl", hash = "sha256:d8947001be83df1394050758ce0e745dd74fb134eef0a4b5124208dfc3a68c37"}, + {file = "cryptography-50.0.2-cp314-cp314t-manylinux_2_34_aarch64.whl", hash = "sha256:4a20ce1e5cb4284a86692fdcba7cb8754185c6b2e5c56fcef3751cf451d3cdc2"}, + {file = "cryptography-50.0.2-cp314-cp314t-manylinux_2_34_ppc64le.whl", hash = "sha256:84f964e537f916e2cc85199e5a88742e964939b575ac8598b3f9d6cc416cdaf1"}, + {file = "cryptography-50.0.2-cp314-cp314t-manylinux_2_34_x86_64.whl", hash = "sha256:828d49b0ff5a0e3975865571c5d91dbbdd0d38d8289b249a163e9425413a5e05"}, + {file = "cryptography-50.0.2-cp314-cp314t-musllinux_1_2_aarch64.whl", hash = "sha256:deb9fde5c60e437ee4821bc9bc39ff31b42135c27e1dc61ef0a629389c1de62e"}, + {file = "cryptography-50.0.2-cp314-cp314t-musllinux_1_2_x86_64.whl", hash = "sha256:8c71ba2cd31fc93748c38e1b613200ff1c2665cbfd5341fe3a61cfde35a1430e"}, + {file = "cryptography-50.0.2-cp314-cp314t-win_amd64.whl", hash = "sha256:78198641e5be9521beea5aa782bb551a58068d10e6eb04c9c680c1b69f2e7d45"}, + {file = "cryptography-50.0.2-cp315-abi3.abi3t-macosx_11_0_arm64.whl", hash = "sha256:edc3342adf8f697fc5f59c887a304356f147b397809440ed64e2fa6af2f50f37"}, + {file = "cryptography-50.0.2-cp315-abi3.abi3t-manylinux2014_aarch64.manylinux_2_17_aarch64.whl", hash = "sha256:d370b8d1dfcdf7130178137f6fbee6140774a1acc6cacefc4b42643ec11d0a3a"}, + {file = "cryptography-50.0.2-cp315-abi3.abi3t-manylinux2014_x86_64.manylinux_2_17_x86_64.whl", hash = "sha256:f2f9bd7f90c64fe89253f0a2c05e3c4856072660429ce8831b4235bf29403a67"}, + {file = "cryptography-50.0.2-cp315-abi3.abi3t-manylinux_2_28_aarch64.whl", hash = "sha256:e275096ea1e60cc595cda2836fd4a6c725d1125108b868be17f53684d164e2cc"}, + {file = "cryptography-50.0.2-cp315-abi3.abi3t-manylinux_2_28_ppc64le.whl", hash = "sha256:b13478603dcd0a2479ff8e87e2c19a7d525734686fe3c49542472293a204212d"}, + {file = "cryptography-50.0.2-cp315-abi3.abi3t-manylinux_2_28_x86_64.whl", hash = "sha256:58a0c478eeca76fe5e07993c5a0703def34a6dc6a0cda4f5564639b33112ffe7"}, + {file = "cryptography-50.0.2-cp315-abi3.abi3t-manylinux_2_31_armv7l.whl", hash = "sha256:d38cdff612d06fa6a32840d5e1b1f7a27cee4a349aa9085d94a67789d6bfd408"}, + {file = "cryptography-50.0.2-cp315-abi3.abi3t-manylinux_2_34_aarch64.whl", hash = "sha256:fdd28f912fccfec1846a94e2e1e8f9b0012f557f0c46fe4f3eb0d7a87afcf90b"}, + {file = "cryptography-50.0.2-cp315-abi3.abi3t-manylinux_2_34_ppc64le.whl", hash = "sha256:cbc8738fd8526d80f35cb3a40d41f41a2e7030bb3b18b09a6778ef63d291c2fd"}, + {file = "cryptography-50.0.2-cp315-abi3.abi3t-manylinux_2_34_x86_64.whl", hash = "sha256:e105ab60406787da31fccc883fc0f733af1efd78f0136a4599692c4083a73d0c"}, + {file = "cryptography-50.0.2-cp315-abi3.abi3t-musllinux_1_2_aarch64.whl", hash = "sha256:6f8700550aa1474a91e5dc07049c46f98b423b5b1ddd0483e0b51362eeeaf5be"}, + {file = "cryptography-50.0.2-cp315-abi3.abi3t-musllinux_1_2_x86_64.whl", hash = "sha256:c71be1cbfa5cd9a41ee452acf1eccd82b2c05950358b106ec8ceb83411d1a020"}, + {file = "cryptography-50.0.2-cp315-abi3.abi3t-win_amd64.whl", hash = "sha256:c423ab384a46c4dff7217b2ea5ba2e11cffdeab6441acd04cf65a369caf0366c"}, + {file = "cryptography-50.0.2-cp39-abi3-macosx_11_0_arm64.whl", hash = "sha256:0ec5f09541743261e66e291b4a0cbf0fb2997aeaab6d9e9c740b9dba1b58d1c2"}, + {file = "cryptography-50.0.2-cp39-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl", hash = "sha256:c5e67125c7dca78d199ec4e116aa93dbb83494808ecbb8211a2cb09b1bf41dbd"}, + {file = "cryptography-50.0.2-cp39-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl", hash = "sha256:ee247f5c245c9a2fe7c8e2214e295918838e44e00a45a6718451e4004219e767"}, + {file = "cryptography-50.0.2-cp39-abi3-manylinux_2_28_aarch64.whl", hash = "sha256:dfe9763530994147d9af1def057a5b9658b00e8f8fe8743d144d1e0911c2e454"}, + {file = "cryptography-50.0.2-cp39-abi3-manylinux_2_28_ppc64le.whl", hash = "sha256:58ddb5a8e3179d12f19e4ea34d2d32e9d63a4baa142c875c1eb59f41b7243acd"}, + {file = "cryptography-50.0.2-cp39-abi3-manylinux_2_28_x86_64.whl", hash = "sha256:f21e8a22c8605750c7af886bab299a363721264061b4ac0a30efb73cfd58efc5"}, + {file = "cryptography-50.0.2-cp39-abi3-manylinux_2_31_armv7l.whl", hash = "sha256:9c8402a82ea0dc4ceeab793db05f0fafa8ca139ca34fcde5df0f596103c74107"}, + {file = "cryptography-50.0.2-cp39-abi3-manylinux_2_34_aarch64.whl", hash = "sha256:0ddc924c04591c2811ca024d62ecad4f7f6f08af8939c211438f48a16bd23602"}, + {file = "cryptography-50.0.2-cp39-abi3-manylinux_2_34_ppc64le.whl", hash = "sha256:a6557e5f38e065ca9fbdaf7cfc7435ecb1d113aa81a022d1b51921ee7432e227"}, + {file = "cryptography-50.0.2-cp39-abi3-manylinux_2_34_x86_64.whl", hash = "sha256:1981f1db4630889b9ef7803fadef12b056f428cb6b85c27ba57b774793b6093c"}, + {file = "cryptography-50.0.2-cp39-abi3-musllinux_1_2_aarch64.whl", hash = "sha256:7a8701d6b584d76e909e3d305b7d126b41439876a5aaf76cddc67fc230eafa2e"}, + {file = "cryptography-50.0.2-cp39-abi3-musllinux_1_2_x86_64.whl", hash = "sha256:ce47f66801c20ec6c6632453bb5960fe38939e9306970b48b3a5a26de7745d94"}, + {file = "cryptography-50.0.2-cp39-abi3-win_amd64.whl", hash = "sha256:4e81d95e5bafc2d6e34e4bed780e53e4d5b9a2f928573428aa4d35fbec1eb0de"}, + {file = "cryptography-50.0.2-pp311-pypy311_pp73-manylinux_2_28_aarch64.whl", hash = "sha256:92e665960f25fcdc73725b9cec7a3824f279ba97a98653afe9ffac2e43668f67"}, + {file = "cryptography-50.0.2-pp311-pypy311_pp73-manylinux_2_28_x86_64.whl", hash = "sha256:eef4c2f3423810b3070ab391f85436d2f8bbfcb286ac15cbc73190b3563b1f1a"}, + {file = "cryptography-50.0.2-pp311-pypy311_pp73-manylinux_2_34_aarch64.whl", hash = "sha256:7c6d0330c472d96f6a6afe24d80dfdf15176c33096f0a4397ae4c60f3dd3be48"}, + {file = "cryptography-50.0.2-pp311-pypy311_pp73-manylinux_2_34_x86_64.whl", hash = "sha256:1ba34f04897fcdaa73f74145c25f3ec146fbd56593853e88adc2e811303c5f42"}, + {file = "cryptography-50.0.2-pp311-pypy311_pp80-macosx_11_0_arm64.whl", hash = "sha256:3dc4fd8058cea1644971207d530e1a03a184a805ffc8ebdddf0599d78a331b81"}, + {file = "cryptography-50.0.2-pp311-pypy311_pp80-win_amd64.whl", hash = "sha256:7b75de3c8b3be1cdb1052747c929440c3eea46c1bc2cb8a6e3a48388e9b7b452"}, + {file = "cryptography-50.0.2.tar.gz", hash = "sha256:7b46165bb56eb4704e2eaaf86f3c940d19154535d9b0ca7d6d590b04060e00d5"}, +] + +[package.dependencies] +cffi = {version = ">=2.0.0", markers = "platform_python_implementation != \"PyPy\""} + +[package.extras] +ssh = ["bcrypt (>=3.1.5)"] + +[[package]] +name = "distlib" +version = "0.4.4" +description = "Distribution utilities" +optional = false +python-versions = "*" +groups = ["dev"] +files = [ + {file = "distlib-0.4.4-py2.py3-none-any.whl", hash = "sha256:cf09d55b1e85a89576b036aeb71550bfd68320f3f21761ca329eadadad918638"}, + {file = "distlib-0.4.4.tar.gz", hash = "sha256:ca4175b4ac7f71223cc30ed7248c45d2b38c2439d8ee6e498ab67ed63cf3508d"}, +] + +[[package]] +name = "distro" +version = "1.9.0" +description = "Distro - an OS platform information API" +optional = false +python-versions = ">=3.6" +groups = ["dev"] +files = [ + {file = "distro-1.9.0-py3-none-any.whl", hash = "sha256:7bffd925d65168f85027d8da9af6bddab658135b840670a223589bc0c8ef02b2"}, + {file = "distro-1.9.0.tar.gz", hash = "sha256:2fa77c6fd8940f116ee1d6b94a2f90b13b5ea8d019b98bc8bafdcabcdd9bdbed"}, +] + +[[package]] +name = "filelock" +version = "4.2.6" +description = "A platform independent file lock." +optional = false +python-versions = ">=3.11" +groups = ["dev"] +files = [ + {file = "filelock-4.2.6-py3-none-any.whl", hash = "sha256:fd7d4fca497bf3e3d0945a13261f8e684f2aef12ce7cf76aef26e13bf7388b88"}, + {file = "filelock-4.2.6.tar.gz", hash = "sha256:ee7af8ede36dcaf12e1016d9f7c3a63ecd5497d46f85dab21b8c779b8f944e97"}, +] + +[[package]] +name = "identify" +version = "2.6.20" +description = "File identification library for Python" +optional = false +python-versions = ">=3.10" +groups = ["dev"] +files = [ + {file = "identify-2.6.20-py2.py3-none-any.whl", hash = "sha256:6a16b69b93187244e0548cbfd25b3e4a6f9a7a2ad784625c3bec2b8d27b81aaa"}, + {file = "identify-2.6.20.tar.gz", hash = "sha256:ad729860a923858d26917c2f4fb0a1d83d27a75b1e090c06440c573f048f3285"}, +] + +[package.extras] +license = ["ukkonen"] + +[[package]] +name = "iniconfig" +version = "2.3.1" +description = "brain-dead simple config-ini parsing" +optional = false +python-versions = ">=3.10" +groups = ["dev"] +files = [ + {file = "iniconfig-2.3.1-py3-none-any.whl", hash = "sha256:9121e2c1fdb355232495be3194c8dfe87ccc2d5dee45947b78e68f499790d7a7"}, + {file = "iniconfig-2.3.1.tar.gz", hash = "sha256:67f4b9c50da0dedf52af349e7749a80a9057a5031199791b906c3bb3ae878960"}, +] + +[[package]] +name = "jinja2" +version = "3.1.6" +description = "A very fast and expressive template engine." +optional = false +python-versions = ">=3.7" +groups = ["main", "dev"] +files = [ + {file = "jinja2-3.1.6-py3-none-any.whl", hash = "sha256:85ece4451f492d0c13c5dd7c13a64681a86afae63a5f347908daf103ce6d2f67"}, + {file = "jinja2-3.1.6.tar.gz", hash = "sha256:0137fb05990d35f1275a587e9aee6d56da821fc83491a0fb838183be43f66d6d"}, +] + +[package.dependencies] +MarkupSafe = ">=2.0" + +[package.extras] +i18n = ["Babel (>=2.7)"] + +[[package]] +name = "jsonschema" +version = "4.26.0" +description = "An implementation of JSON Schema validation for Python" +optional = false +python-versions = ">=3.10" +groups = ["dev"] +files = [ + {file = "jsonschema-4.26.0-py3-none-any.whl", hash = "sha256:d489f15263b8d200f8387e64b4c3a75f06629559fb73deb8fdfb525f2dab50ce"}, + {file = "jsonschema-4.26.0.tar.gz", hash = "sha256:0c26707e2efad8aa1bfc5b7ce170f3fccc2e4918ff85989ba9ffa9facb2be326"}, +] + +[package.dependencies] +attrs = ">=22.2.0" +jsonschema-specifications = ">=2023.3.6" +referencing = ">=0.28.4" +rpds-py = ">=0.25.0" + +[package.extras] +format = ["fqdn", "idna", "isoduration", "jsonpointer (>1.13)", "rfc3339-validator", "rfc3987", "uri-template", "webcolors (>=1.11)"] +format-nongpl = ["fqdn", "idna", "isoduration", "jsonpointer (>1.13)", "rfc3339-validator", "rfc3986-validator (>0.1.0)", "rfc3987-syntax (>=1.1.0)", "uri-template", "webcolors (>=24.6.0)"] + +[[package]] +name = "jsonschema-specifications" +version = "2025.9.1" +description = "The JSON Schema meta-schemas and vocabularies, exposed as a Registry" +optional = false +python-versions = ">=3.9" +groups = ["dev"] +files = [ + {file = "jsonschema_specifications-2025.9.1-py3-none-any.whl", hash = "sha256:98802fee3a11ee76ecaca44429fda8a41bff98b00a0f2838151b113f210cc6fe"}, + {file = "jsonschema_specifications-2025.9.1.tar.gz", hash = "sha256:b540987f239e745613c7a9176f3edb72b832a4ac465cf02712288397832b5e8d"}, +] + +[package.dependencies] +referencing = ">=0.31.0" + +[[package]] +name = "librt" +version = "0.16.0" +description = "Mypyc runtime library" +optional = false +python-versions = ">=3.9" +groups = ["dev"] +markers = "platform_python_implementation != \"PyPy\"" +files = [ + {file = "librt-0.16.0-cp310-cp310-macosx_10_9_x86_64.whl", hash = "sha256:96f576f2711f8519152ec76d0e599243555c1f07679fa73606ca8c8c868c0be6"}, + {file = "librt-0.16.0-cp310-cp310-macosx_11_0_arm64.whl", hash = "sha256:2bec3818c7da7c96ceae0ef5915a3d16c52dd08f3ea913bf1fe8568c447c7978"}, + {file = "librt-0.16.0-cp310-cp310-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:64c79520414a3fdfc6aabd7593e6169afa14d5f8d9908d4b498db068868b08dd"}, + {file = "librt-0.16.0-cp310-cp310-manylinux2014_i686.manylinux_2_17_i686.manylinux_2_28_i686.whl", hash = "sha256:f81b5b19ce748ef68d4746656b7929762eb2fe99269b266e4be07e2ee4de7144"}, + {file = "librt-0.16.0-cp310-cp310-manylinux2014_ppc64le.manylinux_2_17_ppc64le.manylinux_2_28_ppc64le.whl", hash = "sha256:c71d1b76210a36729fedfc5115069b50a3d8619054745f8758fe5d6f19e86671"}, + {file = "librt-0.16.0-cp310-cp310-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:473eebc7866bb0a0c8849a292b5e7157c1aba5d14d0f0f610c52158d6d964262"}, + {file = "librt-0.16.0-cp310-cp310-manylinux_2_34_riscv64.manylinux_2_39_riscv64.whl", hash = "sha256:4eb1313a19847089ee81e88742abedf285c60538816640b99742d8534b81d26a"}, + {file = "librt-0.16.0-cp310-cp310-musllinux_1_2_aarch64.whl", hash = "sha256:ebefd60b42e2a82b32d136bb5f7c94eadfcd29f772b547df6f3291d1ed855a1c"}, + {file = "librt-0.16.0-cp310-cp310-musllinux_1_2_i686.whl", hash = "sha256:6c5da27e8056439f927ea896735da60e616c477a8293feaa3233d4e7781a6726"}, + {file = "librt-0.16.0-cp310-cp310-musllinux_1_2_ppc64le.whl", hash = "sha256:67e718c7a43f8db325abbbf1404e2d535f12f8f7a1a82259568385cc5274b82a"}, + {file = "librt-0.16.0-cp310-cp310-musllinux_1_2_riscv64.whl", hash = "sha256:314e703f0c19320dc8094e7a784b9cf29e1b67402515abf580069a6363c0b4f1"}, + {file = "librt-0.16.0-cp310-cp310-musllinux_1_2_x86_64.whl", hash = "sha256:943c6bbecbdf7fa575a4f2952fcfd848c88ef95507c3fca411e89d4ac3ff8143"}, + {file = "librt-0.16.0-cp310-cp310-win32.whl", hash = "sha256:7cc365f006891afb006b52d5ee5ee74c09306ffa20e2f8705a32a4450af2f3ba"}, + {file = "librt-0.16.0-cp310-cp310-win_amd64.whl", hash = "sha256:0314058469f4d2fd279ce7c62ac274ac82c3918ef7db62ef0697c4c359370155"}, + {file = "librt-0.16.0-cp311-cp311-macosx_10_9_x86_64.whl", hash = "sha256:fe4372c52d4849096c6cc1cda2817d293ec51440c890474ed59ef38d46556f18"}, + {file = "librt-0.16.0-cp311-cp311-macosx_11_0_arm64.whl", hash = "sha256:c72c5295a84bd249526da9bdca38f2e176d15c31c13bb0063c5053f4ca023421"}, + {file = "librt-0.16.0-cp311-cp311-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:be56ba9c884143495b517f23fe794ae367d58cd89ea0fdd6d437e3c024a87f9f"}, + {file = "librt-0.16.0-cp311-cp311-manylinux2014_i686.manylinux_2_17_i686.manylinux_2_28_i686.whl", hash = "sha256:ef46c1a29ffb8c72e882e22618ec618778eacd0578fb22c6e7cf9c11d15f357b"}, + {file = "librt-0.16.0-cp311-cp311-manylinux2014_ppc64le.manylinux_2_17_ppc64le.manylinux_2_28_ppc64le.whl", hash = "sha256:d3c94211ee0c4f8d649ec06b7c115c0ec4eadb873a0e3154ca15cef3f814b071"}, + {file = "librt-0.16.0-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:94aed6a8308818b91677957d1bd03188869cd7aeb23c5dba7912a6c0402f7602"}, + {file = "librt-0.16.0-cp311-cp311-manylinux_2_34_riscv64.manylinux_2_39_riscv64.whl", hash = "sha256:349c0bcb87ebd07481b6ff781e25cdc699723dbe2212e57dabb27f7a13b7b87d"}, + {file = "librt-0.16.0-cp311-cp311-musllinux_1_2_aarch64.whl", hash = "sha256:001bfd59a7d45b17e3e75f2a8c6405280b35e7b84471792778e718c4f368950e"}, + {file = "librt-0.16.0-cp311-cp311-musllinux_1_2_i686.whl", hash = "sha256:28e038895b998d7a0c7798922ce8a1dc157675df5cf1c9ef0aca809ed804b7a1"}, + {file = "librt-0.16.0-cp311-cp311-musllinux_1_2_ppc64le.whl", hash = "sha256:0dbe4096a7ecc00fa835d24510ad8545a4efef738dac96e0e63516783ccde905"}, + {file = "librt-0.16.0-cp311-cp311-musllinux_1_2_riscv64.whl", hash = "sha256:5cd5b092441053364af968ea12084692cb9d4a22f3ce9524e377880bf028761e"}, + {file = "librt-0.16.0-cp311-cp311-musllinux_1_2_x86_64.whl", hash = "sha256:3ddeb3c9dedb461bb457c6c7d9aa7fbf35329da313d1a7543d00c8d0f3473c96"}, + {file = "librt-0.16.0-cp311-cp311-win32.whl", hash = "sha256:e05108e0849966f53a8d2d3112a7af881d0efaa479bc735bba91108f9f2350a7"}, + {file = "librt-0.16.0-cp311-cp311-win_amd64.whl", hash = "sha256:5f49cff01bd608ef7d97104cb035c75455e79c2d70bf4a506cf773338ac1860d"}, + {file = "librt-0.16.0-cp311-cp311-win_arm64.whl", hash = "sha256:1d28ae980ae2218f9c5b95d191e947296f918c9bf0b400d467a9430275bbe678"}, + {file = "librt-0.16.0-cp312-cp312-macosx_10_13_x86_64.whl", hash = "sha256:fe52bf4641069e7978a14253b036cb9002def1926317e710f2e249f8a8c47742"}, + {file = "librt-0.16.0-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:5bcc2c4726ced915b00de0c9856a4eeabfb3fddb93e10e0b8f735b7709358b6d"}, + {file = "librt-0.16.0-cp312-cp312-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:ff7baa55f8e7c69851419e50a666015d02a74198716fd45c0125a2112e0a389f"}, + {file = "librt-0.16.0-cp312-cp312-manylinux2014_i686.manylinux_2_17_i686.manylinux_2_28_i686.whl", hash = "sha256:b95d5d92ab83d39e760a52091bb1baba664f3a2351e39b1e16801e5747c2f0e9"}, + {file = "librt-0.16.0-cp312-cp312-manylinux2014_ppc64le.manylinux_2_17_ppc64le.manylinux_2_28_ppc64le.whl", hash = "sha256:b6d085d70bce51d43c5c7c36d63490770180d8779e71c49305c87b4213918de7"}, + {file = "librt-0.16.0-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:36e53948e99bbe3ffea257124cfcae1cfb01831555c9a9c903c9f9a72db7fd07"}, + {file = "librt-0.16.0-cp312-cp312-manylinux_2_34_riscv64.manylinux_2_39_riscv64.whl", hash = "sha256:54d11f726aae9df5a6ffbbf0a03a52449bbac84a53ef03669cb41cdfd4ae41bf"}, + {file = "librt-0.16.0-cp312-cp312-musllinux_1_2_aarch64.whl", hash = "sha256:4323193ac0cd025f85af531df8ba91bf24d1973b401697347a6282e8fd3fcf5e"}, + {file = "librt-0.16.0-cp312-cp312-musllinux_1_2_i686.whl", hash = "sha256:e42f8e098b9c5396fefa05fb1cc7e33b0e08fc51da106b5de4a45fd22aac6743"}, + {file = "librt-0.16.0-cp312-cp312-musllinux_1_2_ppc64le.whl", hash = "sha256:39ec1d5a14e37baf1450a6cabf03fe552340808bf1ad9d71824ab90117716459"}, + {file = "librt-0.16.0-cp312-cp312-musllinux_1_2_riscv64.whl", hash = "sha256:d1aabe3925cbb4a08d15b7b20ba4011b53019da0c4173a25155139b7b1baed65"}, + {file = "librt-0.16.0-cp312-cp312-musllinux_1_2_x86_64.whl", hash = "sha256:300c3ffdc459f4a779a8411ecb188e3ac0b1ff3a3a7b099642555dedae06c69b"}, + {file = "librt-0.16.0-cp312-cp312-win32.whl", hash = "sha256:c17194318e4c0c0348b36f36c2ec7534436fe0a4c15582403162a4f08c80797a"}, + {file = "librt-0.16.0-cp312-cp312-win_amd64.whl", hash = "sha256:25a58a19ea8d83b68209f04912df765e9260635ef77646542ed4b4abe6bc7940"}, + {file = "librt-0.16.0-cp312-cp312-win_arm64.whl", hash = "sha256:f7be7cf555bc30ec12622e9447299cc4a9b8ff307548b634794353db0c2065dc"}, + {file = "librt-0.16.0-cp313-cp313-macosx_10_13_x86_64.whl", hash = "sha256:c5e6144e68b577f157519f2ba88ca20e3ed61c29b00e5cdfa76cd2d45acf059a"}, + {file = "librt-0.16.0-cp313-cp313-macosx_11_0_arm64.whl", hash = "sha256:33f41443a1f4e1f099331b3d8120e409fbff84b9760bc1cc9ea496f37ddaa5cc"}, + {file = "librt-0.16.0-cp313-cp313-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:7e510b7770bee609617a3374a96548eb114cae048023e3f049ee449e7ff2db32"}, + {file = "librt-0.16.0-cp313-cp313-manylinux2014_i686.manylinux_2_17_i686.manylinux_2_28_i686.whl", hash = "sha256:efc49c462d4516b8a58b00b490078fa64689fd1fe66970cc190131d7afb8027e"}, + {file = "librt-0.16.0-cp313-cp313-manylinux2014_ppc64le.manylinux_2_17_ppc64le.manylinux_2_28_ppc64le.whl", hash = "sha256:92caf82ebef5e12d21c72242b70d1e92536f1711cf2a727a4c276de4b4469087"}, + {file = "librt-0.16.0-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:17bac7f7a16b328fff77e440287693eb017abde913595b5827ebccbc21ecd8a6"}, + {file = "librt-0.16.0-cp313-cp313-manylinux_2_34_riscv64.manylinux_2_39_riscv64.whl", hash = "sha256:5b976054553670829985ed767feb78fb6bcede0175327c4844dd5c281c1be659"}, + {file = "librt-0.16.0-cp313-cp313-musllinux_1_2_aarch64.whl", hash = "sha256:0058f9d68721094105917254c72ac0569117bb7b13b9769cf45d26d89f9d21cd"}, + {file = "librt-0.16.0-cp313-cp313-musllinux_1_2_i686.whl", hash = "sha256:30b7beaf3f4487b7d8adef1f158b49067cb4d5a19fa7a3bf31a4e7a820e435c5"}, + {file = "librt-0.16.0-cp313-cp313-musllinux_1_2_ppc64le.whl", hash = "sha256:468df902df016a06eb0e40b0747dc8d14e47d7a38b18b63b1fb167d85cb94d63"}, + {file = "librt-0.16.0-cp313-cp313-musllinux_1_2_riscv64.whl", hash = "sha256:aea7b1f2b125dad5de85f049136651bff256c883c65e6b9209b2da0a1ac3cdef"}, + {file = "librt-0.16.0-cp313-cp313-musllinux_1_2_x86_64.whl", hash = "sha256:a8afb6557920860b7a3a596eb804cf37e09e7cf8a803db2478c202acc72d8c2e"}, + {file = "librt-0.16.0-cp313-cp313-pyemscripten_2025_0_wasm32.whl", hash = "sha256:77c7a2b4fe2c1369e0d5aa1cade26740a7b14be32fbc9a5535d617d20065c39d"}, + {file = "librt-0.16.0-cp313-cp313-win32.whl", hash = "sha256:02d89c813d5ff74b17df72d3a34819d132cd168e56b81bf755b809bd9e46b8c4"}, + {file = "librt-0.16.0-cp313-cp313-win_amd64.whl", hash = "sha256:14ed6ebe3e4f85f326d7920011ad30ff49ed9334e62cf88caef9ba973d9e3a92"}, + {file = "librt-0.16.0-cp313-cp313-win_arm64.whl", hash = "sha256:83d4041a3d9b2fd053a8a4e1f22878b3e5833e2712956382d5c048d791454e91"}, + {file = "librt-0.16.0-cp314-cp314-macosx_10_15_x86_64.whl", hash = "sha256:931a0bb0fcac88f263e269e46eb30ba8e21402cd3c62ca40cb97034c0693fab1"}, + {file = "librt-0.16.0-cp314-cp314-macosx_11_0_arm64.whl", hash = "sha256:1bc17e54e5305f8d40b7ca203671ff5a9e59c1d0f8ea0f625dcca53a3984de11"}, + {file = "librt-0.16.0-cp314-cp314-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:877698bf6bca5721d8be345f2fe09778e40ecadea8b58c73075f2b1a53666bf2"}, + {file = "librt-0.16.0-cp314-cp314-manylinux2014_i686.manylinux_2_17_i686.manylinux_2_28_i686.whl", hash = "sha256:5981c011b306781ce561e18e14230a14524a3d8109b97553666c942c18f31a96"}, + {file = "librt-0.16.0-cp314-cp314-manylinux2014_ppc64le.manylinux_2_17_ppc64le.manylinux_2_28_ppc64le.whl", hash = "sha256:afced3dfc17cd805ecf7a3d77996a71cf5f2c75aa66eb0c21a9930f4fc992f86"}, + {file = "librt-0.16.0-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:ca8052401c55d7511dda6760719fda7618067e83535d7d0010096d216c34b667"}, + {file = "librt-0.16.0-cp314-cp314-manylinux_2_34_riscv64.manylinux_2_39_riscv64.whl", hash = "sha256:1e511762a074005bb0aa569166779834e75e438370226930d0ce1866d4b6a33b"}, + {file = "librt-0.16.0-cp314-cp314-musllinux_1_2_aarch64.whl", hash = "sha256:f1e8591bd8a5a628cd7f07954c6a1592359a878bf032957a8e9057a41d644311"}, + {file = "librt-0.16.0-cp314-cp314-musllinux_1_2_i686.whl", hash = "sha256:a4aaefb4ba6c07e1aeebb2795c8958148f1d6f9af3b555b53d23d766edb6d67a"}, + {file = "librt-0.16.0-cp314-cp314-musllinux_1_2_ppc64le.whl", hash = "sha256:d92db7a0f6aee44f1baee94750457e8d2d1c6ccea41842de6268d34e8dc7eddd"}, + {file = "librt-0.16.0-cp314-cp314-musllinux_1_2_riscv64.whl", hash = "sha256:378dfaffb38e59c24a87cde5713cd865d51ff7383fa12947f3907f306ea1ca55"}, + {file = "librt-0.16.0-cp314-cp314-musllinux_1_2_x86_64.whl", hash = "sha256:3e0c39bdc85370422e8b637be76eb1fd07d30967551b03e62267dd156f553152"}, + {file = "librt-0.16.0-cp314-cp314-pyemscripten_2026_0_wasm32.whl", hash = "sha256:1b384b90ab79a7bc30b566895809a636e0666f21f3cf12b54823d025b7e83839"}, + {file = "librt-0.16.0-cp314-cp314-win32.whl", hash = "sha256:52327da75a94012e7f932f913d20d3876bed3c102be00e6c3e8600ff7bdd58a7"}, + {file = "librt-0.16.0-cp314-cp314-win_amd64.whl", hash = "sha256:3f0b8114c44b2ac06ff5dacd08e07e8e807ff4f46083f2a1602685122559be41"}, + {file = "librt-0.16.0-cp314-cp314-win_arm64.whl", hash = "sha256:8caf96a4ef8fb27d0ac0d1ad8337d26a240acd4a02fe4345d0a8f264753e8f99"}, + {file = "librt-0.16.0-cp314-cp314t-macosx_10_15_x86_64.whl", hash = "sha256:953107e2f68d0f3512c48f898b0dbf0ce5cc52bba0f318d847c985dc555ee4cc"}, + {file = "librt-0.16.0-cp314-cp314t-macosx_11_0_arm64.whl", hash = "sha256:ad37d5b9abd49c9a655dcda7ea52a8a752884062ef1ee71ae17c2f2a0f81fe6a"}, + {file = "librt-0.16.0-cp314-cp314t-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:2c4aa329c17bd1aaea4f6e89335d8ccd494b3a5830b6654462273e50e11023f0"}, + {file = "librt-0.16.0-cp314-cp314t-manylinux2014_i686.manylinux_2_17_i686.manylinux_2_28_i686.whl", hash = "sha256:0ead24d2562a49473dddd9efef8581f020007eb0054389c3ee3ffad38b1ca4c9"}, + {file = "librt-0.16.0-cp314-cp314t-manylinux2014_ppc64le.manylinux_2_17_ppc64le.manylinux_2_28_ppc64le.whl", hash = "sha256:02118f56a9c36ddd07dfd9b919d9ecc117ba20a90987d56aa4c429fa34509188"}, + {file = "librt-0.16.0-cp314-cp314t-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:4e29522c62e28595ff7e324c6834ade51127707f0e255b18d1c1cf03d39c1048"}, + {file = "librt-0.16.0-cp314-cp314t-manylinux_2_34_riscv64.manylinux_2_39_riscv64.whl", hash = "sha256:3ff4b2367926b69c6215635902cccb04048e73094e9862900d27cb2c6bbff143"}, + {file = "librt-0.16.0-cp314-cp314t-musllinux_1_2_aarch64.whl", hash = "sha256:c6f1b27bf1632a7e016af9f145f82be95e1edd7721a646505c21059257cb5a04"}, + {file = "librt-0.16.0-cp314-cp314t-musllinux_1_2_i686.whl", hash = "sha256:5696d7f52e7b37217cb3a8f92c744fe835942602fdd4c1a8bc4741d3bfdce15e"}, + {file = "librt-0.16.0-cp314-cp314t-musllinux_1_2_ppc64le.whl", hash = "sha256:6072e92dd876ff6ceeb6cf371e35e51f479349837391341f479b08df4564242b"}, + {file = "librt-0.16.0-cp314-cp314t-musllinux_1_2_riscv64.whl", hash = "sha256:39ca4f2f2fe05de8e63493da592d84311adabe5bef52b193851981da9816b302"}, + {file = "librt-0.16.0-cp314-cp314t-musllinux_1_2_x86_64.whl", hash = "sha256:f9807485a908f00355820f18e91e045ffdcdc5adb68aaec40a1e2b88c5f7bba1"}, + {file = "librt-0.16.0-cp314-cp314t-win32.whl", hash = "sha256:94be5cb7bca4df6201f4183e9e4fa2086c655283d20b38cd84500a69057575a7"}, + {file = "librt-0.16.0-cp314-cp314t-win_amd64.whl", hash = "sha256:d46ca272b251d033dd4527b0dec5f261a28a52bd5fa0f99c117b0a1f8588cc2d"}, + {file = "librt-0.16.0-cp314-cp314t-win_arm64.whl", hash = "sha256:b9d6d4b14e92d876f8026b54c20c445f36425214c1081dc76f74e40db386b82b"}, + {file = "librt-0.16.0-cp315-cp315-macosx_10_15_x86_64.whl", hash = "sha256:6fe436af2eaf630474f491af5d032cbe45f93fcff5c3b9fe4ab194a7255b20ff"}, + {file = "librt-0.16.0-cp315-cp315-macosx_11_0_arm64.whl", hash = "sha256:8ff5d26c529336be9bd7ae04483235d77778ee7d6444a95353102b542601ce81"}, + {file = "librt-0.16.0-cp315-cp315-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:909d8e3c1faee44cb762b1c519ff8613dcc5ceae5c99987a00917b5a31fd1d6a"}, + {file = "librt-0.16.0-cp315-cp315-manylinux2014_i686.manylinux_2_17_i686.manylinux_2_28_i686.whl", hash = "sha256:6d4a64283ee61824b5790de882bc68e2d9d7a5143537cb7a966f7354f71646d4"}, + {file = "librt-0.16.0-cp315-cp315-manylinux2014_ppc64le.manylinux_2_17_ppc64le.manylinux_2_28_ppc64le.whl", hash = "sha256:5810ba811297fdf37a1531a57667cb8ace0842013ca8606bf9eb7c24cf4be154"}, + {file = "librt-0.16.0-cp315-cp315-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:e56aaf8c167548dc8e5d6f3bd0f48dcdd299a23c73be3f744aab79d99e9c7f5d"}, + {file = "librt-0.16.0-cp315-cp315-manylinux_2_34_riscv64.manylinux_2_39_riscv64.whl", hash = "sha256:8f36c58e33b304b525c6c9c5076399c6ebf1109e17b9051a05a407b091b9215b"}, + {file = "librt-0.16.0-cp315-cp315-musllinux_1_2_aarch64.whl", hash = "sha256:242e00b3d4fa37c3d3c1ca5f5c9adb7d909ddb1eac9c41f2787320d00caa0af2"}, + {file = "librt-0.16.0-cp315-cp315-musllinux_1_2_i686.whl", hash = "sha256:0253721561787b8df8443eb347b7a6461015354e5bdd37ee38a41fef220d2bb0"}, + {file = "librt-0.16.0-cp315-cp315-musllinux_1_2_ppc64le.whl", hash = "sha256:3e483a8d69ede8067db70c0e83007423b6925de6fd53afed01d66160f2e9398c"}, + {file = "librt-0.16.0-cp315-cp315-musllinux_1_2_riscv64.whl", hash = "sha256:69ba927445cfaaffb4081003ef5224c55a5c2ab67ef956f416ef744916e44121"}, + {file = "librt-0.16.0-cp315-cp315-musllinux_1_2_x86_64.whl", hash = "sha256:d6a365f2ab45a984d0e00eee0dd17f599ceab8cadab6ea07b6111c8132fc0e42"}, + {file = "librt-0.16.0-cp315-cp315-pyemscripten_2026_5_wasm32.whl", hash = "sha256:f01f3805f2dae4781c0c34b440e31740d082950bdaf89a6f601ad589a28af57a"}, + {file = "librt-0.16.0-cp315-cp315-win32.whl", hash = "sha256:b0e3e721c75d2e79a76d4422c79d7ba705fe1bbafec907037fe7a657a480a0e3"}, + {file = "librt-0.16.0-cp315-cp315-win_amd64.whl", hash = "sha256:bc02954b1295de798bbdb0b4e2d8a28c2117de8b5c73dcbeb27dc32572dfb971"}, + {file = "librt-0.16.0-cp315-cp315-win_arm64.whl", hash = "sha256:c5db585d43449a5f54303d4b2774e45e1babd975cfe1630a3d708c0b80c3e560"}, + {file = "librt-0.16.0-cp315-cp315t-macosx_10_15_x86_64.whl", hash = "sha256:f06c689cb14afd9b612727553a5ec5a40febf113ca41c4413a2b0b334285884b"}, + {file = "librt-0.16.0-cp315-cp315t-macosx_11_0_arm64.whl", hash = "sha256:13b4e8aba90b0b1c82474e9844aa9ffe7ad3faa484350e1da64cb8188d903134"}, + {file = "librt-0.16.0-cp315-cp315t-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:5a269c46ae327d8e6f8c1f85f7516cb52c0fa48127565a1105a4f4a05ff2a0b4"}, + {file = "librt-0.16.0-cp315-cp315t-manylinux2014_i686.manylinux_2_17_i686.manylinux_2_28_i686.whl", hash = "sha256:a33e0dae1f8592146a4764d54ce842b278732d21a84e17c3bbe6b1bc158a2248"}, + {file = "librt-0.16.0-cp315-cp315t-manylinux2014_ppc64le.manylinux_2_17_ppc64le.manylinux_2_28_ppc64le.whl", hash = "sha256:47ada6ea32636492c61aa8ad27ae3b9404bfe7a97e3ba946d1984236cc741da0"}, + {file = "librt-0.16.0-cp315-cp315t-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:c43bd6e642d8a248c114327f98dd25ac5a7cb5aa168ef02f0559b91874df16b8"}, + {file = "librt-0.16.0-cp315-cp315t-manylinux_2_34_riscv64.manylinux_2_39_riscv64.whl", hash = "sha256:c3d1bb7841a816ace6449bb26d3f9560dbfa20e71c568d23f0f62bf1e68f50b1"}, + {file = "librt-0.16.0-cp315-cp315t-musllinux_1_2_aarch64.whl", hash = "sha256:3931f7a3db322e7f44e02a280e3949326ce9579ad388ee8d691dc7c76da9fb70"}, + {file = "librt-0.16.0-cp315-cp315t-musllinux_1_2_i686.whl", hash = "sha256:f4462528b6000afe8f16907b5c7c2553abf1df005ba5140e6eb394541c3624c3"}, + {file = "librt-0.16.0-cp315-cp315t-musllinux_1_2_ppc64le.whl", hash = "sha256:80039ba9b6a7d5f1a0175a4cca6bbefead87bd854c80abad1cb30afe47a830db"}, + {file = "librt-0.16.0-cp315-cp315t-musllinux_1_2_riscv64.whl", hash = "sha256:7a1d272724b581bb6bc769dfdafed6da2ecc9886ba2450311de55a4ac2e1e9cd"}, + {file = "librt-0.16.0-cp315-cp315t-musllinux_1_2_x86_64.whl", hash = "sha256:fbe4fb8c5445f7496d7f7f6bb0807875d09d47e6771ffa175fb2df2895fb86ba"}, + {file = "librt-0.16.0-cp315-cp315t-win32.whl", hash = "sha256:375bfe6b572a8f6cfc398709356046173bf27e64c4c5edaf5f7062f051fb4bf9"}, + {file = "librt-0.16.0-cp315-cp315t-win_amd64.whl", hash = "sha256:bd3150023d3dc2bc70f3784e59ffa1140d56ddba3d8125b3d6f9f85221279bfc"}, + {file = "librt-0.16.0-cp315-cp315t-win_arm64.whl", hash = "sha256:8ceafb70f2a4f0826f11031942e59c0728fd98da112dc346d4352bde1e486866"}, + {file = "librt-0.16.0-cp39-cp39-macosx_10_9_x86_64.whl", hash = "sha256:e1967e36ac4cae0c7e9615ad32e1a513cdacff79f9e8afb28bedc91caf48b4f3"}, + {file = "librt-0.16.0-cp39-cp39-macosx_11_0_arm64.whl", hash = "sha256:20fe0bf9053885e21c62b3e091fb5e73e1c54d1daf2e70eb388d70763bcd4220"}, + {file = "librt-0.16.0-cp39-cp39-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:6a63610fa76524edfa605b5b259a603915c7a6e10e54f003e5503030506e81de"}, + {file = "librt-0.16.0-cp39-cp39-manylinux2014_i686.manylinux_2_17_i686.manylinux_2_28_i686.whl", hash = "sha256:845a511b60ca43b9880dcc84a9784c891d6a2098c829130b320846c69c9c0c68"}, + {file = "librt-0.16.0-cp39-cp39-manylinux2014_ppc64le.manylinux_2_17_ppc64le.manylinux_2_28_ppc64le.whl", hash = "sha256:d608f0bf3b8cbddd0067fe02cb8cab7d13e8eb9386b23a1843d4363044fd9e22"}, + {file = "librt-0.16.0-cp39-cp39-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:df183721229ae51eef90108c115b43e98cb169b6155d34480338e5fc6616df00"}, + {file = "librt-0.16.0-cp39-cp39-manylinux_2_34_riscv64.manylinux_2_39_riscv64.whl", hash = "sha256:4b6183e2e2e0ee00aac2ec07c7f7d151c97e85666b304f574b71cff0f9fccc4e"}, + {file = "librt-0.16.0-cp39-cp39-musllinux_1_2_aarch64.whl", hash = "sha256:273d00be33792a15189331df10f1f1331621b043881e66c6c4377f7f776e1291"}, + {file = "librt-0.16.0-cp39-cp39-musllinux_1_2_i686.whl", hash = "sha256:aa9357a1b4d4fc787bb718a59cb1112c28c8a976d6bfa268b71cc0a4ab8f3a94"}, + {file = "librt-0.16.0-cp39-cp39-musllinux_1_2_ppc64le.whl", hash = "sha256:e9ce0bc440e7fd09b5f51f372f0f6640658f854b8fb05260f819cc669c93c42d"}, + {file = "librt-0.16.0-cp39-cp39-musllinux_1_2_riscv64.whl", hash = "sha256:6c8893eae2fd13c5488d94056f3e6e5cf3142bfb1c4acaf136cb33d760c5964b"}, + {file = "librt-0.16.0-cp39-cp39-musllinux_1_2_x86_64.whl", hash = "sha256:7393c9a48dcce4817dbd4b0d8ff6237efe9b0a0609f5b0adaef315f8541726b5"}, + {file = "librt-0.16.0-cp39-cp39-win32.whl", hash = "sha256:71b93b42784e25b975079573c642a8fedb049a7bb31d70a51721b1666b3b2ced"}, + {file = "librt-0.16.0-cp39-cp39-win_amd64.whl", hash = "sha256:5750a105b42a416f930edc59054927a406effb2550cd5bab92ad7a5842ed5d05"}, + {file = "librt-0.16.0.tar.gz", hash = "sha256:ac38d6d8d66bf3d744148dbbc0b8e193e195a51e364ed55e224631f5721891fc"}, +] + +[[package]] +name = "markupsafe" +version = "3.0.4" +description = "Safely add untrusted strings to HTML/XML markup." +optional = false +python-versions = ">=3.9" +groups = ["main", "dev"] +files = [ + {file = "markupsafe-3.0.4-cp310-cp310-macosx_10_9_x86_64.whl", hash = "sha256:dd8ea6ebee7aedbf7c749fa80521d9ccf1ba473e0d1e14805caafbaad281c889"}, + {file = "markupsafe-3.0.4-cp310-cp310-macosx_11_0_arm64.whl", hash = "sha256:dff05cb7016dff1e9fd68f4122c127b65dfc59de5306cfb7ad92f956f230bee2"}, + {file = "markupsafe-3.0.4-cp310-cp310-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:cf63c214fe879a65e69a386f915e36104fc84254ab141240f8854602d8e0be2a"}, + {file = "markupsafe-3.0.4-cp310-cp310-manylinux2014_armv7l.manylinux_2_17_armv7l.manylinux_2_31_armv7l.whl", hash = "sha256:2a6ef68ae94aed8721934072b27a3b654ea2100b97e4ab864cf1489c90926fbc"}, + {file = "markupsafe-3.0.4-cp310-cp310-manylinux2014_ppc64le.manylinux_2_17_ppc64le.manylinux_2_28_ppc64le.whl", hash = "sha256:fd9f8797427910198f95bced71ddfed61130d7e349213bfb8466c9c99e2c46a8"}, + {file = "markupsafe-3.0.4-cp310-cp310-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:d1aca03ede943eb80ab3d63bb082c84b7aab85ea83bd0fd0c200260945fb49d9"}, + {file = "markupsafe-3.0.4-cp310-cp310-manylinux_2_31_riscv64.manylinux_2_39_riscv64.whl", hash = "sha256:0764a13d34cae40db7bbf3a09b7e9b491bf4603e20b263a7a9d6b8e324975d0a"}, + {file = "markupsafe-3.0.4-cp310-cp310-musllinux_1_2_aarch64.whl", hash = "sha256:9388003072b95f2f1e3fd908604194d653ba21330d811961a78b7da1a77e9e36"}, + {file = "markupsafe-3.0.4-cp310-cp310-musllinux_1_2_armv7l.whl", hash = "sha256:8698d70a8081ee8c090dbb394768b5789a1da8b131b5499f89d071dd3cfaf6be"}, + {file = "markupsafe-3.0.4-cp310-cp310-musllinux_1_2_ppc64le.whl", hash = "sha256:bf053da3c97a4bc5ecfbb218cdd2983febd91c617be8367d139882aa11e490aa"}, + {file = "markupsafe-3.0.4-cp310-cp310-musllinux_1_2_riscv64.whl", hash = "sha256:9438a2648b2195980cb2dd8e53ed7b8df91319e2d0b70ae61a9e1d1bc8d3bec9"}, + {file = "markupsafe-3.0.4-cp310-cp310-musllinux_1_2_x86_64.whl", hash = "sha256:88d59b473bfb03259722600839af9bbd7fa13a2eb514beefeedb95997882f69a"}, + {file = "markupsafe-3.0.4-cp310-cp310-win32.whl", hash = "sha256:4a540e2d3192792fc84eced57bef37851ccb2b41f73291bb17408eea77bcd278"}, + {file = "markupsafe-3.0.4-cp310-cp310-win_amd64.whl", hash = "sha256:5c22873ad1f0532ba40fa1727f3c0fc1bbbaab6d373d4cbe3f0dc74b2e2521c7"}, + {file = "markupsafe-3.0.4-cp310-cp310-win_arm64.whl", hash = "sha256:3d23795802fc8bd72534836d64489bbf0f67c088959091bdb22e10735a5107bf"}, + {file = "markupsafe-3.0.4-cp311-cp311-macosx_10_9_x86_64.whl", hash = "sha256:9e25feb9e330b63edb0278a0acdf85e50d0cb0fbf49c3084abbe4e24ae195346"}, + {file = "markupsafe-3.0.4-cp311-cp311-macosx_11_0_arm64.whl", hash = "sha256:7d3391b2188d18737cb2fa147028b1096236eaa7e156446c650a489fa2cadc91"}, + {file = "markupsafe-3.0.4-cp311-cp311-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:849dd2bb0e5e4ab2b71c7191726a4a8d5aa8a610daa584728cbee0b710ddc4ef"}, + {file = "markupsafe-3.0.4-cp311-cp311-manylinux2014_armv7l.manylinux_2_17_armv7l.manylinux_2_31_armv7l.whl", hash = "sha256:befb4158af32106b9a93db8d6d1d1cbbd418c0d5aca0cabb7b1780abf0c89169"}, + {file = "markupsafe-3.0.4-cp311-cp311-manylinux2014_ppc64le.manylinux_2_17_ppc64le.manylinux_2_28_ppc64le.whl", hash = "sha256:71f88e749ea29f67f21f3b36433c1dc54c7729ed2a6d9e2da2e0d9e0d7b224eb"}, + {file = "markupsafe-3.0.4-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:6da83a088f8ef93b2d483a8232a4dbf4d69d3d8496b568a03c56becac43e1808"}, + {file = "markupsafe-3.0.4-cp311-cp311-manylinux_2_31_riscv64.manylinux_2_39_riscv64.whl", hash = "sha256:8f0fac8b13d14bb06c68195f849371924ae53dd7b1c00fed24650f704383b692"}, + {file = "markupsafe-3.0.4-cp311-cp311-musllinux_1_2_aarch64.whl", hash = "sha256:4a7cdc2a420ca01058182da4253329764d4bfa055564d1eced90e6ba1e8b1d3d"}, + {file = "markupsafe-3.0.4-cp311-cp311-musllinux_1_2_armv7l.whl", hash = "sha256:83b3944fea42a8400edf92fd1770fb8d0d4f7de651353bd2d8525a92dba69a21"}, + {file = "markupsafe-3.0.4-cp311-cp311-musllinux_1_2_ppc64le.whl", hash = "sha256:8138eb83940ec7299024d92d4dee45f601b9e6c5ffde9d25f4e35e326203c707"}, + {file = "markupsafe-3.0.4-cp311-cp311-musllinux_1_2_riscv64.whl", hash = "sha256:811d02d5122171c1941357efd8f9bf4ffe907b7f0a1a4e729a880e4be3f46e3e"}, + {file = "markupsafe-3.0.4-cp311-cp311-musllinux_1_2_x86_64.whl", hash = "sha256:50b5bedc9ed8a94fc8857a42ef4f84a81ea88f8d4f05dc8705fb23ee6d8dcca7"}, + {file = "markupsafe-3.0.4-cp311-cp311-win32.whl", hash = "sha256:2e5a7cd7fdd14fcb1ae5d7d8bf23d24fbd1daefd1fbca2580132e1ea75f098b5"}, + {file = "markupsafe-3.0.4-cp311-cp311-win_amd64.whl", hash = "sha256:fdb4ca07ab75ffadab4a8b135ad59cdbb3156b99310f3d565370da74a15d6bd3"}, + {file = "markupsafe-3.0.4-cp311-cp311-win_arm64.whl", hash = "sha256:569d65055d367e3dcdf30c3f41119467b73d9ee9faf332bdf40402644f5ac08e"}, + {file = "markupsafe-3.0.4-cp312-cp312-macosx_10_13_x86_64.whl", hash = "sha256:61631e08084be9e21a8967ec3139c7616ed7c5e9368e05c86d1b39562c8a57b6"}, + {file = "markupsafe-3.0.4-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:0930db9bdc62d22944e10b066448bb65dc9abe9112880c7cab8da54db4284d5f"}, + {file = "markupsafe-3.0.4-cp312-cp312-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:6a45c3d514f2436064db00d7fc8778d888f0236ebfed649b53d13a59e69ad51b"}, + {file = "markupsafe-3.0.4-cp312-cp312-manylinux2014_armv7l.manylinux_2_17_armv7l.manylinux_2_31_armv7l.whl", hash = "sha256:1e1451fab512d1bcc3dc26988ec1edb0b82c2db909132872cd9356070a6b63df"}, + {file = "markupsafe-3.0.4-cp312-cp312-manylinux2014_ppc64le.manylinux_2_17_ppc64le.manylinux_2_28_ppc64le.whl", hash = "sha256:bd3ce56ae2cbae3ba82b683bc425cd7e48d2ed8b10f3e818186b6f5646d9271c"}, + {file = "markupsafe-3.0.4-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:8e124f974786f831d6043728e38296969d3579db8896fe004682f5758e613581"}, + {file = "markupsafe-3.0.4-cp312-cp312-manylinux_2_31_riscv64.manylinux_2_39_riscv64.whl", hash = "sha256:c02e8f18bdedba082cef725942ac823b9b60656db07f7e265cb31618dfd00d77"}, + {file = "markupsafe-3.0.4-cp312-cp312-musllinux_1_2_aarch64.whl", hash = "sha256:9f098115c247e11d138ab83a28fa0323c77015007ea2df73ba5fd714dfefd67c"}, + {file = "markupsafe-3.0.4-cp312-cp312-musllinux_1_2_armv7l.whl", hash = "sha256:d5f93ebbeb8032d47e349328ec8662d973d9b05a70b3c35df1f91fe419b84749"}, + {file = "markupsafe-3.0.4-cp312-cp312-musllinux_1_2_ppc64le.whl", hash = "sha256:64511c54db4e4987aef4c41923235927428729e8174c5dba488429be70a998ed"}, + {file = "markupsafe-3.0.4-cp312-cp312-musllinux_1_2_riscv64.whl", hash = "sha256:e1a622f13970d81f95d0c72f9dc090dce9085fccfa4c9f2174377ee32bd15786"}, + {file = "markupsafe-3.0.4-cp312-cp312-musllinux_1_2_x86_64.whl", hash = "sha256:c9a7f43c0b202b334cc9184af09bb8f21d3a209e038efaf106936fb69e6b026e"}, + {file = "markupsafe-3.0.4-cp312-cp312-win32.whl", hash = "sha256:f0ec3b750b59375eab5b0fb2b9254810c00a3375be6d789899f1055a1d556237"}, + {file = "markupsafe-3.0.4-cp312-cp312-win_amd64.whl", hash = "sha256:11935df9bf455ed0c04eb87bcd720f02b1fe5e02128a9430f23aed6f93336fc7"}, + {file = "markupsafe-3.0.4-cp312-cp312-win_arm64.whl", hash = "sha256:a4bbd2d87dd233b9fc5812160c3d0ffbe42edc22a26ce0469f58479ede633fe9"}, + {file = "markupsafe-3.0.4-cp313-cp313-android_24_arm64_v8a.whl", hash = "sha256:de8b364c423ef0a4bad9069657d617f9a5d2b2062457a89b1fa16ee199c399c1"}, + {file = "markupsafe-3.0.4-cp313-cp313-android_24_x86_64.whl", hash = "sha256:34bdde374c5932765d7dc685c4a1d191a3207852d67e8e0a9eb6ea85156181f1"}, + {file = "markupsafe-3.0.4-cp313-cp313-ios_13_0_arm64_iphoneos.whl", hash = "sha256:6bd9e1788e15bfcf6a9082de42e30387e7b85d211ab21e57a939bb8cfaaf8d96"}, + {file = "markupsafe-3.0.4-cp313-cp313-ios_13_0_arm64_iphonesimulator.whl", hash = "sha256:5066b244f576f91afc8ee3ba029a89f99d39c79b1853fe9d39bea9f0afbec148"}, + {file = "markupsafe-3.0.4-cp313-cp313-ios_13_0_x86_64_iphonesimulator.whl", hash = "sha256:7a83aa6e4805df46fed18e989d3d16f86ef60cb50bbc8d9ce3a6be89165fbf6e"}, + {file = "markupsafe-3.0.4-cp313-cp313-macosx_10_13_x86_64.whl", hash = "sha256:2d1b7d9308288661f56672b1b157d75fc536714d3638487bbea17b6318a78248"}, + {file = "markupsafe-3.0.4-cp313-cp313-macosx_11_0_arm64.whl", hash = "sha256:73e77980c7207854f00fc4e71fb1626868d5740ab4012623d55c7a99ad122a72"}, + {file = "markupsafe-3.0.4-cp313-cp313-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:7018d4af1cd272e847aa5917983ab5e83e4f6579f9dbfecd4a79c0ca80b144c2"}, + {file = "markupsafe-3.0.4-cp313-cp313-manylinux2014_armv7l.manylinux_2_17_armv7l.manylinux_2_31_armv7l.whl", hash = "sha256:c90d5b3d4e944e065a301d741b3c1d784f6bd1f503aa68b4967e32b2ba313d85"}, + {file = "markupsafe-3.0.4-cp313-cp313-manylinux2014_ppc64le.manylinux_2_17_ppc64le.manylinux_2_28_ppc64le.whl", hash = "sha256:18a801868a884f216e784d7d14db2a4077143ce7610440aee2ce8f734e7cfcde"}, + {file = "markupsafe-3.0.4-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:434139499bb20b502ed3baa1f169e618f924a97e7a777fea1a49446d80106cf6"}, + {file = "markupsafe-3.0.4-cp313-cp313-manylinux_2_31_riscv64.manylinux_2_39_riscv64.whl", hash = "sha256:9e227f3dbe6bde7491cf0a9965d00b88c6b1a4a95d11480ddf88bb96d397c19f"}, + {file = "markupsafe-3.0.4-cp313-cp313-musllinux_1_2_aarch64.whl", hash = "sha256:b8cd1f918b26fd7b1832ece557cc18f2d8747309ff8b3f0ef9d4250c5ad67a39"}, + {file = "markupsafe-3.0.4-cp313-cp313-musllinux_1_2_armv7l.whl", hash = "sha256:a5fcffb37e602b0b3c1638a97746b9b96125caa9bcf6fa41d337a9261de231ee"}, + {file = "markupsafe-3.0.4-cp313-cp313-musllinux_1_2_ppc64le.whl", hash = "sha256:5989cb26b2e1efc6a42216a9f6b5ee495ce5ace2e5b352a9af489976b32d1ee2"}, + {file = "markupsafe-3.0.4-cp313-cp313-musllinux_1_2_riscv64.whl", hash = "sha256:add96447a86d205ab616665d53b2950ee81083757f56e6ea833c8b2917646b46"}, + {file = "markupsafe-3.0.4-cp313-cp313-musllinux_1_2_x86_64.whl", hash = "sha256:2628d3a8cb648ecebb3c5d6b0a1052d400e4d8b7ac0fb786be8d285b50040d17"}, + {file = "markupsafe-3.0.4-cp313-cp313-win32.whl", hash = "sha256:672d207103e6b16ca098611b0f9efad6bc00afd47c03d6ef62186495ca677dc0"}, + {file = "markupsafe-3.0.4-cp313-cp313-win_amd64.whl", hash = "sha256:1f1f9477e174582b0a1b583d60b66e1f2cf5d3fe12cee985e4aedf44766600e5"}, + {file = "markupsafe-3.0.4-cp313-cp313-win_arm64.whl", hash = "sha256:06de8ef6331f6e822c28d577dc8bf43fe398800477c49498f38fc38b67ff33fc"}, + {file = "markupsafe-3.0.4-cp314-cp314-android_24_arm64_v8a.whl", hash = "sha256:4ed644d75aa94a2baf7ec3a96eaa160ea58c742eb9d27c6506053c5c40fc84ed"}, + {file = "markupsafe-3.0.4-cp314-cp314-android_24_x86_64.whl", hash = "sha256:6d2a9efe686f9de00d0d1ea32a4a5a86d558a2277501bd78d964214eab625e59"}, + {file = "markupsafe-3.0.4-cp314-cp314-ios_13_0_arm64_iphoneos.whl", hash = "sha256:8781a792a070cf2bd1b86d3aa943894115faaba6e88122a7bf32d62072742453"}, + {file = "markupsafe-3.0.4-cp314-cp314-ios_13_0_arm64_iphonesimulator.whl", hash = "sha256:971a3bbb75d97ae4e2e8f7d4834236f86f85f0c85e04ab2e191db1123b04f80b"}, + {file = "markupsafe-3.0.4-cp314-cp314-ios_13_0_x86_64_iphonesimulator.whl", hash = "sha256:8909c2f1c6dd65e054ac4b573a91c8384d1492281e55d82d159d653f7a13adf6"}, + {file = "markupsafe-3.0.4-cp314-cp314-macosx_10_15_x86_64.whl", hash = "sha256:4cf3468d5ec187ffffcaca8e61929a37448f215dafc1386a12c750a72fe53634"}, + {file = "markupsafe-3.0.4-cp314-cp314-macosx_11_0_arm64.whl", hash = "sha256:52704c5d36eb6dda8866493decd61111fff86244c9b1ad225ca01b9e91e5970f"}, + {file = "markupsafe-3.0.4-cp314-cp314-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:1caa2fa5a6184fb233153b35f654e6687bd555476f6170f29d8ee9be1a8b0af9"}, + {file = "markupsafe-3.0.4-cp314-cp314-manylinux2014_armv7l.manylinux_2_17_armv7l.manylinux_2_31_armv7l.whl", hash = "sha256:387d8cd30e69b3f0a72877b9ae717033396404e19095b17fe89753a981fda44f"}, + {file = "markupsafe-3.0.4-cp314-cp314-manylinux2014_ppc64le.manylinux_2_17_ppc64le.manylinux_2_28_ppc64le.whl", hash = "sha256:051417f74bcaaefa316276e0ff723f541616ca51043d070da00249d9bddd3e3c"}, + {file = "markupsafe-3.0.4-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:a8e9f292fcda89b324f2f5c91d13f1424a153e40fc2756f38ee23b15835ff300"}, + {file = "markupsafe-3.0.4-cp314-cp314-manylinux_2_31_riscv64.manylinux_2_39_riscv64.whl", hash = "sha256:df1ae86ff54725a01fa1a0510b914ca53a161b7050be74f6204e24aded5971d0"}, + {file = "markupsafe-3.0.4-cp314-cp314-musllinux_1_2_aarch64.whl", hash = "sha256:8965520ac587c94a4ac48b729be3d8b8de00af39699b17585dfb599babe77977"}, + {file = "markupsafe-3.0.4-cp314-cp314-musllinux_1_2_armv7l.whl", hash = "sha256:340cbb1957ba99929cbf19a75626d36ba1ae21d1730b287d1cf7f824a20c4fc7"}, + {file = "markupsafe-3.0.4-cp314-cp314-musllinux_1_2_ppc64le.whl", hash = "sha256:3a93d9616ddecfb393727a0041a562cf0b15a244e20f2bd25efc7949be4c4f17"}, + {file = "markupsafe-3.0.4-cp314-cp314-musllinux_1_2_riscv64.whl", hash = "sha256:d2e56fd3b00222722abfb3f5f0759ddbae4b90811b5ad4343c64030ad1bde70c"}, + {file = "markupsafe-3.0.4-cp314-cp314-musllinux_1_2_x86_64.whl", hash = "sha256:0d9c47709875fdb321452056622e930c52afbc07a7d780762fbb8b4d91ce6fa4"}, + {file = "markupsafe-3.0.4-cp314-cp314-win32.whl", hash = "sha256:38fc55594dab834470b6733dead2ee9e3f657fb0608c769dcafa0ba5ab52f45c"}, + {file = "markupsafe-3.0.4-cp314-cp314-win_amd64.whl", hash = "sha256:c1bc67752d5f21013cfe430df4062441714eab79f65a6a05e01505957e9c35fe"}, + {file = "markupsafe-3.0.4-cp314-cp314-win_arm64.whl", hash = "sha256:7e1636da3d8dfc220b6dd10264db5f2b165e4888c4518594898fbe381049af8a"}, + {file = "markupsafe-3.0.4-cp314-cp314t-macosx_10_15_x86_64.whl", hash = "sha256:805c8b84534fa10891890f0e4be39f3a99e94615d93e8836bf9fa1fdca2feeb2"}, + {file = "markupsafe-3.0.4-cp314-cp314t-macosx_11_0_arm64.whl", hash = "sha256:fa95848c929b6a75f6848d3c9793e59db365ee436776e57db835cdbfa79ba977"}, + {file = "markupsafe-3.0.4-cp314-cp314t-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:e916035e3e9930cbdfdd10abf48861340221857f45509565898e012263f7b289"}, + {file = "markupsafe-3.0.4-cp314-cp314t-manylinux2014_armv7l.manylinux_2_17_armv7l.manylinux_2_31_armv7l.whl", hash = "sha256:b4d12837e0203bbace818ff4a7461afdcd78bcd782351cea148139180d7bcffe"}, + {file = "markupsafe-3.0.4-cp314-cp314t-manylinux2014_ppc64le.manylinux_2_17_ppc64le.manylinux_2_28_ppc64le.whl", hash = "sha256:5086f9975abb1ab531ee6afca1761e4b59a19b446f3f6522ed776963228cfe5a"}, + {file = "markupsafe-3.0.4-cp314-cp314t-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:b4a635a0487774f841cb1fb62e907e7195cc95bc761e053184b8acc3ceb20733"}, + {file = "markupsafe-3.0.4-cp314-cp314t-manylinux_2_31_riscv64.manylinux_2_39_riscv64.whl", hash = "sha256:cb96e6e088d6cf71c1ea977510948320234824cf226e32f6f6e044f7a9c82b34"}, + {file = "markupsafe-3.0.4-cp314-cp314t-musllinux_1_2_aarch64.whl", hash = "sha256:8b5d563170ff8ba3181caa967c99a3c804d1dedb702c7cb93a6a7c32247da978"}, + {file = "markupsafe-3.0.4-cp314-cp314t-musllinux_1_2_armv7l.whl", hash = "sha256:396ec4e65cc889f69786b3b89478b471cee5a3bcf468b9d9bb03e1a30fb291fc"}, + {file = "markupsafe-3.0.4-cp314-cp314t-musllinux_1_2_ppc64le.whl", hash = "sha256:15ba9e28640feef770374b116a6f019c21f52404aeabe516aa7f800587b98cfc"}, + {file = "markupsafe-3.0.4-cp314-cp314t-musllinux_1_2_riscv64.whl", hash = "sha256:d920abdfa61279ba1a2ef9484aab07bf03331f8c08a10120fa332353d06e6932"}, + {file = "markupsafe-3.0.4-cp314-cp314t-musllinux_1_2_x86_64.whl", hash = "sha256:a9f54054101545a9a9cccefddf54316aa6e4491611fcbef9e91b3b6bebec04f6"}, + {file = "markupsafe-3.0.4-cp314-cp314t-win32.whl", hash = "sha256:12a606a492de952afcb43b59a14aaaaad120e708d3663dd0fdf2d738d427a691"}, + {file = "markupsafe-3.0.4-cp314-cp314t-win_amd64.whl", hash = "sha256:a18f38cafc329bac5e3c2b96c765b4c96d3d103421ed22ab7988c1e3fce27464"}, + {file = "markupsafe-3.0.4-cp314-cp314t-win_arm64.whl", hash = "sha256:eba154571c16e032112afac0dc2dfe9e63c2ceb7aedd07bb7eecf2ce26d4dd4c"}, + {file = "markupsafe-3.0.4-cp315-cp315-android_24_arm64_v8a.whl", hash = "sha256:737c9c3981998eba27f11786f84fddcbabc74068b72a4a1f454ea02094b57b65"}, + {file = "markupsafe-3.0.4-cp315-cp315-android_24_x86_64.whl", hash = "sha256:489505b03f692c3f376394e49194fa7a7f9e8558d6e293a7056a0032b0c38163"}, + {file = "markupsafe-3.0.4-cp315-cp315-ios_13_0_arm64_iphoneos.whl", hash = "sha256:077293e425f28ec737dbcad442a71752e28f8ae27cde3d68acd1fb212091cd92"}, + {file = "markupsafe-3.0.4-cp315-cp315-ios_13_0_arm64_iphonesimulator.whl", hash = "sha256:9348cbb300d224fe3b89793262cb093504d4ae927004468463f745188a193e4a"}, + {file = "markupsafe-3.0.4-cp315-cp315-ios_13_0_x86_64_iphonesimulator.whl", hash = "sha256:b807e598953730f82e4eae3bd30f6a122cf6b31c398c6b504c0e04c13c170429"}, + {file = "markupsafe-3.0.4-cp315-cp315-macosx_10_15_x86_64.whl", hash = "sha256:799c39bdf5e2f1292fedd3009f7b3c9e760f10b2420cb9638d56920840ff6db8"}, + {file = "markupsafe-3.0.4-cp315-cp315-macosx_11_0_arm64.whl", hash = "sha256:ae9dcb8fbe244cb82f8a6458b455b927a03685e383d9bacf1ea5ce180b96dc97"}, + {file = "markupsafe-3.0.4-cp315-cp315-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:4bced6e2a6dba6a28f7dd3c6ce14df1b2dd495923f16ea484cad03decd463b2b"}, + {file = "markupsafe-3.0.4-cp315-cp315-manylinux2014_armv7l.manylinux_2_17_armv7l.manylinux_2_31_armv7l.whl", hash = "sha256:3882fb412298575bae3b9c46868251f15cc69307359f87bb1b382e53d6e5a2c9"}, + {file = "markupsafe-3.0.4-cp315-cp315-manylinux2014_ppc64le.manylinux_2_17_ppc64le.manylinux_2_28_ppc64le.whl", hash = "sha256:04e7902ba80ee4bac1d50a549606527a1dcf0476cd81403db41099d3b60ec653"}, + {file = "markupsafe-3.0.4-cp315-cp315-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:925f929d6b59a8b3f8b8c6ac363cd0af7eecc81efb3071770b3c6717c450a369"}, + {file = "markupsafe-3.0.4-cp315-cp315-manylinux_2_31_riscv64.manylinux_2_39_riscv64.whl", hash = "sha256:f68edfc67aabac33708941f26f22a7b8e9f81429bc0cf249fcf7d66b23af8d19"}, + {file = "markupsafe-3.0.4-cp315-cp315-musllinux_1_2_aarch64.whl", hash = "sha256:e5c802729725bd07e2bc3ab7b76dc7e0bbfc53129d8f1eb1c002c24cf774717e"}, + {file = "markupsafe-3.0.4-cp315-cp315-musllinux_1_2_armv7l.whl", hash = "sha256:55ffd6ce583d97dc71dc92e930324c8c0d25aea7e3ade6ae54ef77cedb096811"}, + {file = "markupsafe-3.0.4-cp315-cp315-musllinux_1_2_ppc64le.whl", hash = "sha256:2cb3dd71fc6be918ad4264346a8ed69485f9b7ed7bf35495d8e22807cd6b8bea"}, + {file = "markupsafe-3.0.4-cp315-cp315-musllinux_1_2_riscv64.whl", hash = "sha256:94f5407f7bc64fa6463906b896f9904beeeb7dd8dc116ee8e9056c8714ff9916"}, + {file = "markupsafe-3.0.4-cp315-cp315-musllinux_1_2_x86_64.whl", hash = "sha256:2dad610540cb2e6272855c178f08ae9a1c7ac258a7fb71660553a5f104b42741"}, + {file = "markupsafe-3.0.4-cp315-cp315-win32.whl", hash = "sha256:03470d1a8268e692ecf79ecd565593e59d44219377a7ead61f1f1b94c1f7ff6b"}, + {file = "markupsafe-3.0.4-cp315-cp315-win_amd64.whl", hash = "sha256:d882a373d8093c2941e01291b7ced96e9cbe4781da9a7751ca7e6c70385e5214"}, + {file = "markupsafe-3.0.4-cp315-cp315-win_arm64.whl", hash = "sha256:353bd63081912ab8cfa6a0c7d185934cdf8426f04c618bba6bc4b394f2069b67"}, + {file = "markupsafe-3.0.4-cp315-cp315t-macosx_10_15_x86_64.whl", hash = "sha256:c61750fadcd119d0825bcb7d7d675dd264dcc89cc05292aab5be68ebdbb374ad"}, + {file = "markupsafe-3.0.4-cp315-cp315t-macosx_11_0_arm64.whl", hash = "sha256:1c0df495a977d10460a94941799c72d5b5ab03d3858d949b55b5a66c8f371c99"}, + {file = "markupsafe-3.0.4-cp315-cp315t-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:02fa4acbc6a3fc5c693c34d4dd8c1130b7fe99cc915181b0ddd6f72aeb296002"}, + {file = "markupsafe-3.0.4-cp315-cp315t-manylinux2014_armv7l.manylinux_2_17_armv7l.manylinux_2_31_armv7l.whl", hash = "sha256:05295589e619b9bed252a86b532b8e27350abc372d18ba89b59375325e91ec1e"}, + {file = "markupsafe-3.0.4-cp315-cp315t-manylinux2014_ppc64le.manylinux_2_17_ppc64le.manylinux_2_28_ppc64le.whl", hash = "sha256:be6cb0c799abb0e2ba3e618e6d28ddddf7e485f6c2ce938dfa237daf3905072c"}, + {file = "markupsafe-3.0.4-cp315-cp315t-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:26e9867520db70d37f7fb421a7f0d8adb40171011fb84ce869afa1a83370dfa8"}, + {file = "markupsafe-3.0.4-cp315-cp315t-manylinux_2_31_riscv64.manylinux_2_39_riscv64.whl", hash = "sha256:f03460ff076f70ab595bb45a0205ccea1971443575b6920c52e755dec2b3fbfe"}, + {file = "markupsafe-3.0.4-cp315-cp315t-musllinux_1_2_aarch64.whl", hash = "sha256:436e3ffc6310d3c41878c601db29098102fe5d8a467c49da4a4125254e0980f2"}, + {file = "markupsafe-3.0.4-cp315-cp315t-musllinux_1_2_armv7l.whl", hash = "sha256:4e2c4809c14559aa7ef426f27fb35afbb38104c349a903bf8f3600456764bb38"}, + {file = "markupsafe-3.0.4-cp315-cp315t-musllinux_1_2_ppc64le.whl", hash = "sha256:da2af0d7aebfc2074080d72efa6ab8317c62481ef1f896f65d9999c1c01f4494"}, + {file = "markupsafe-3.0.4-cp315-cp315t-musllinux_1_2_riscv64.whl", hash = "sha256:aa2c838cc024642cc04c6854232f32b43e5e22833dd11119c1766c7873b8370d"}, + {file = "markupsafe-3.0.4-cp315-cp315t-musllinux_1_2_x86_64.whl", hash = "sha256:b91cc9d336957239ff200f30097e6fea2dc6d6fb3c81e853eaa09eac904fd894"}, + {file = "markupsafe-3.0.4-cp315-cp315t-win32.whl", hash = "sha256:e49fb0d1ce92cfa0cb198cc5b1b11cdf9d0638658e2a2db2687e39db7c87fc78"}, + {file = "markupsafe-3.0.4-cp315-cp315t-win_amd64.whl", hash = "sha256:4f6e0852a0283b1b1fd776eeb7b766a5f440b3e2bd31ab51af3b400585f3965c"}, + {file = "markupsafe-3.0.4-cp315-cp315t-win_arm64.whl", hash = "sha256:39dbacefc411633db5b4378b066a9aca70a3d7e2922c9e578d825f844026eeba"}, + {file = "markupsafe-3.0.4-cp39-cp39-macosx_10_9_x86_64.whl", hash = "sha256:f291bcf42ae98eb5107edb162c3c998b4a89648fd8e99ed4cbd12705292788cd"}, + {file = "markupsafe-3.0.4-cp39-cp39-macosx_11_0_arm64.whl", hash = "sha256:ac0c7c9f1609b0c4c114feb1d7a3409564c7fb77e360bed9e97e5d25dfeaf868"}, + {file = "markupsafe-3.0.4-cp39-cp39-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:6768d67d1bce64270e0fdc2e69309d68b9b18ae56ddf6c711d168e9d051c2cac"}, + {file = "markupsafe-3.0.4-cp39-cp39-manylinux2014_armv7l.manylinux_2_17_armv7l.manylinux_2_31_armv7l.whl", hash = "sha256:14bd2d845d62ab678eaf81da89d7b621b51756c72346745c1a594c09d49207a2"}, + {file = "markupsafe-3.0.4-cp39-cp39-manylinux2014_ppc64le.manylinux_2_17_ppc64le.manylinux_2_28_ppc64le.whl", hash = "sha256:007e1ffd9bf65bb6ee96df7b258fc632a4868dd5566037986c64781f35a36e98"}, + {file = "markupsafe-3.0.4-cp39-cp39-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:5e8b3d0b18fd623afa12ecb2ce8d8becef69f9b5440c6330c7972200e0bb84b0"}, + {file = "markupsafe-3.0.4-cp39-cp39-manylinux_2_31_riscv64.manylinux_2_39_riscv64.whl", hash = "sha256:57f9947a7e57a081c1e3e0a2dd0d2dcf290a4531450e6f611e30084c222a7295"}, + {file = "markupsafe-3.0.4-cp39-cp39-musllinux_1_2_aarch64.whl", hash = "sha256:b61687d0828e72bf5cda24a2690188f37170bd31c9359ac97e4e66569f120a16"}, + {file = "markupsafe-3.0.4-cp39-cp39-musllinux_1_2_armv7l.whl", hash = "sha256:0cee7cb0f9a1b6892ea482237d9403b3d1b4603aee057d0ff01f0fac2d019a97"}, + {file = "markupsafe-3.0.4-cp39-cp39-musllinux_1_2_ppc64le.whl", hash = "sha256:94e4c421742086aeee4c32a506eec8859d7634aad943f7e6aacf70f813478768"}, + {file = "markupsafe-3.0.4-cp39-cp39-musllinux_1_2_riscv64.whl", hash = "sha256:9240187afb63d2f9ddc3e032c670356fe941f6e20662ea168a5dc3f1f317e1b3"}, + {file = "markupsafe-3.0.4-cp39-cp39-musllinux_1_2_x86_64.whl", hash = "sha256:e841068dc0be4cb6dfb5c890eb88cbdcff2f4a332393c7ec94e8e618bd32c1a8"}, + {file = "markupsafe-3.0.4-cp39-cp39-win32.whl", hash = "sha256:f61efe1d2fe0de16158a5fe1d1cf3c14bdb6aecd54d8938fd26512c525c1f624"}, + {file = "markupsafe-3.0.4-cp39-cp39-win_amd64.whl", hash = "sha256:2b2b1e18af909b448bb3cf9e3433366f7a8726271fc214e8b10e0f62a78c724b"}, + {file = "markupsafe-3.0.4-cp39-cp39-win_arm64.whl", hash = "sha256:6669c1bf34080161ce49c589cc512ef24d4c704ac9d2b2d3667f519c60418378"}, + {file = "markupsafe-3.0.4.tar.gz", hash = "sha256:2e9ad7dd851bf45fab9f75cbff4cb493fee9979e8d8c7c9c3ee119022518edd6"}, +] + +[[package]] +name = "mypy" +version = "1.20.2" +description = "Optional static typing for Python" +optional = false +python-versions = ">=3.10" +groups = ["dev"] +files = [ + {file = "mypy-1.20.2-cp310-cp310-macosx_10_9_x86_64.whl", hash = "sha256:cf5a4db6dca263010e2c7bff081c89383c72d187ba2cf4c44759aac970e2f0c4"}, + {file = "mypy-1.20.2-cp310-cp310-macosx_11_0_arm64.whl", hash = "sha256:7b0e817b518bff7facd7f85ea05b643ad8bdcce684cf29784987b0a7c8e1f997"}, + {file = "mypy-1.20.2-cp310-cp310-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:97d7b9a485b40f8ca425460e89bf1da2814625b2da627c0dcc6aa46c92631d14"}, + {file = "mypy-1.20.2-cp310-cp310-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:1e1c12f6d2db3d78b909b5f77513c11eb7f2dd2782b96a3ab6dffc7d44575c99"}, + {file = "mypy-1.20.2-cp310-cp310-musllinux_1_2_x86_64.whl", hash = "sha256:89dce27e142d25ffbc154c1819383b69f2e9234dc4ed4766f42e0e8cb264ab5c"}, + {file = "mypy-1.20.2-cp310-cp310-win_amd64.whl", hash = "sha256:f376e37f9bf2a946872fc5fd1199c99310748e3c26c7a26683f13f8bdb756cbd"}, + {file = "mypy-1.20.2-cp310-cp310-win_arm64.whl", hash = "sha256:6e2b469efd811707bc530fd1effef0f5d6eebcb7fe376affae69025da4b979a2"}, + {file = "mypy-1.20.2-cp311-cp311-macosx_10_9_x86_64.whl", hash = "sha256:4077797a273e56e8843d001e9dfe4ba10e33323d6ade647ff260e5cd97d9758c"}, + {file = "mypy-1.20.2-cp311-cp311-macosx_11_0_arm64.whl", hash = "sha256:cdecf62abcc4292500d7858aeae87a1f8f1150f4c4dd08fb0b336ee79b2a6df3"}, + {file = "mypy-1.20.2-cp311-cp311-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:c566c3a88b6ece59b3d70f65bedef17304f48eb52ff040a6a18214e1917b3254"}, + {file = "mypy-1.20.2-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:0deb80d062b2479f2c87ae568f89845afc71d11bc41b04179e58165fd9f31e98"}, + {file = "mypy-1.20.2-cp311-cp311-musllinux_1_2_x86_64.whl", hash = "sha256:bba9ad231e92a3e424b3e56b65aa17704993425bba97e302c832f9466bb85bac"}, + {file = "mypy-1.20.2-cp311-cp311-win_amd64.whl", hash = "sha256:baf593f2765fa3a6b1ef95807dbaa3d25b594f6a52adcc506a6b9cb115e1be67"}, + {file = "mypy-1.20.2-cp311-cp311-win_arm64.whl", hash = "sha256:20175a1c0f49863946ec20b7f63255768058ac4f07d2b9ded6a6b46cfb5a9100"}, + {file = "mypy-1.20.2-cp312-cp312-macosx_10_13_x86_64.whl", hash = "sha256:4dbfcf869f6b0517f70cf0030ba6ea1d6645e132337a7d5204a18d8d5636c02b"}, + {file = "mypy-1.20.2-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:4b6481b228d072315b053210b01ac320e1be243dc17f9e5887ef167f23f5fae4"}, + {file = "mypy-1.20.2-cp312-cp312-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:34397cdced6b90b836e38182076049fdb41424322e0b0728c946b0939ebdf9f6"}, + {file = "mypy-1.20.2-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:a5da6976f20cae27059ea8d0c86e7cef3de720e04c4bb9ee18e3690fdb792066"}, + {file = "mypy-1.20.2-cp312-cp312-musllinux_1_2_x86_64.whl", hash = "sha256:56908d7e08318d39f85b1f0c6cfd47b0cac1a130da677630dac0de3e0623e102"}, + {file = "mypy-1.20.2-cp312-cp312-win_amd64.whl", hash = "sha256:d52ad8d78522da1d308789df651ee5379088e77c76cb1994858d40a426b343b9"}, + {file = "mypy-1.20.2-cp312-cp312-win_arm64.whl", hash = "sha256:785b08db19c9f214dc37d65f7c165d19a30fcecb48abfa30f31b01b5acaabb58"}, + {file = "mypy-1.20.2-cp313-cp313-macosx_10_13_x86_64.whl", hash = "sha256:edfbfca868cdd6bd8d974a60f8a3682f5565d3f5c99b327640cedd24c4264026"}, + {file = "mypy-1.20.2-cp313-cp313-macosx_11_0_arm64.whl", hash = "sha256:e2877a02380adfcdbc69071a0f74d6e9dbbf593c0dc9d174e1f223ffd5281943"}, + {file = "mypy-1.20.2-cp313-cp313-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:7488448de6007cd5177c6cea0517ac33b4c0f5ee9b5e9f2be51ce75511a85517"}, + {file = "mypy-1.20.2-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:bb9c2fa06887e21d6a3a868762acb82aec34e2c6fd0174064f27c93ede68ad15"}, + {file = "mypy-1.20.2-cp313-cp313-musllinux_1_2_x86_64.whl", hash = "sha256:9d56a78b646f2e3daa865bc70cd5ec5a46c50045801ca8ff17a0c43abc97e3ee"}, + {file = "mypy-1.20.2-cp313-cp313-win_amd64.whl", hash = "sha256:2a4102b03bb7481d9a91a6da8d174740c9c8c4401024684b9ca3b7cc5e49852f"}, + {file = "mypy-1.20.2-cp313-cp313-win_arm64.whl", hash = "sha256:a95a9248b0c6fd933a442c03c3b113c3b61320086b88e2c444676d3fd1ca3330"}, + {file = "mypy-1.20.2-cp314-cp314-macosx_10_15_x86_64.whl", hash = "sha256:419413398fe250aae057fd2fe50166b61077083c9b82754c341cf4fd73038f30"}, + {file = "mypy-1.20.2-cp314-cp314-macosx_11_0_arm64.whl", hash = "sha256:e73c07f23009962885c197ccb9b41356a30cc0e5a1d0c2ea8fd8fb1362d7f924"}, + {file = "mypy-1.20.2-cp314-cp314-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:0c64e5973df366b747646fc98da921f9d6eba9716d57d1db94a83c026a08e0fb"}, + {file = "mypy-1.20.2-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:5a65aa591af023864fd08a97da9974e919452cfe19cb146c8a5dc692626445dc"}, + {file = "mypy-1.20.2-cp314-cp314-musllinux_1_2_x86_64.whl", hash = "sha256:4fef51b01e638974a6e69885687e9bd40c8d1e09a6cd291cca0619625cf1f558"}, + {file = "mypy-1.20.2-cp314-cp314-win_amd64.whl", hash = "sha256:913485a03f1bcf5d279409a9d2b9ed565c151f61c09f29991e5faa14033da4c8"}, + {file = "mypy-1.20.2-cp314-cp314-win_arm64.whl", hash = "sha256:c3bae4f855d965b5453784300c12ffc63a548304ac7f99e55d4dc7c898673aa3"}, + {file = "mypy-1.20.2-cp314-cp314t-macosx_10_15_x86_64.whl", hash = "sha256:2de3dcea53babc1c3237a19002bc3d228ce1833278f093b8d619e06e7cc79609"}, + {file = "mypy-1.20.2-cp314-cp314t-macosx_11_0_arm64.whl", hash = "sha256:52b176444e2e5054dfcbcb8c75b0b719865c96247b37407184bbfca5c353f2c2"}, + {file = "mypy-1.20.2-cp314-cp314t-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:688c3312e5dadb573a2c69c82af3a298d43ecf9e6d264e0f95df960b5f6ac19c"}, + {file = "mypy-1.20.2-cp314-cp314t-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:29752dbbf8cc53f89f6ac096d363314333045c257c9c75cbd189ca2de0455744"}, + {file = "mypy-1.20.2-cp314-cp314t-musllinux_1_2_x86_64.whl", hash = "sha256:803203d2b6ea644982c644895c2f78b28d0e208bba7b27d9b921e0ec5eb207c6"}, + {file = "mypy-1.20.2-cp314-cp314t-win_amd64.whl", hash = "sha256:9bcb8aa397ff0093c824182fd76a935a9ba7ad097fcbef80ae89bf6c1731d8ec"}, + {file = "mypy-1.20.2-cp314-cp314t-win_arm64.whl", hash = "sha256:e061b58443f1736f8a37c48978d7ab581636d6ab03e3d4f99e3fa90463bb9382"}, + {file = "mypy-1.20.2-py3-none-any.whl", hash = "sha256:a94c5a76ab46c5e6257c7972b6c8cff0574201ca7dc05647e33e795d78680563"}, + {file = "mypy-1.20.2.tar.gz", hash = "sha256:e8222c26daaafd9e8626dec58ae36029f82585890589576f769a650dd20fd665"}, +] + +[package.dependencies] +librt = {version = ">=0.8.0", markers = "platform_python_implementation != \"PyPy\""} +mypy_extensions = ">=1.0.0" +pathspec = ">=1.0.0" +typing_extensions = [ + {version = ">=4.6.0", markers = "python_version < \"3.15\""}, + {version = ">=4.14.0", markers = "python_version >= \"3.15\""}, +] + +[package.extras] +dmypy = ["psutil (>=4.0)"] +faster-cache = ["orjson"] +install-types = ["pip"] +mypyc = ["setuptools (>=50)"] +native-parser = ["ast-serialize (>=0.1.1,<1.0.0)"] +reports = ["lxml"] + +[[package]] +name = "mypy-extensions" +version = "1.1.0" +description = "Type system extensions for programs checked with the mypy type checker." +optional = false +python-versions = ">=3.8" +groups = ["dev"] +files = [ + {file = "mypy_extensions-1.1.0-py3-none-any.whl", hash = "sha256:1be4cccdb0f2482337c4743e60421de3a356cd97508abadd57d47403e94f5505"}, + {file = "mypy_extensions-1.1.0.tar.gz", hash = "sha256:52e68efc3284861e772bbcd66823fde5ae21fd2fdb51c62a211403730b916558"}, +] + +[[package]] +name = "nodeenv" +version = "1.12.0" +description = "Node.js virtual environment builder" +optional = false +python-versions = ">=3.10" +groups = ["dev"] +files = [ + {file = "nodeenv-1.12.0-py3-none-any.whl", hash = "sha256:80644d65c10cc2be12e3e4b54b3367ff288c7ad24ae75c2dcc65454fc7449d4d"}, + {file = "nodeenv-1.12.0.tar.gz", hash = "sha256:4ea7f88339310afebc3253f2c7a91b94bacf97d73a36a5c108962abdfaca501e"}, +] + +[package.extras] +certifi = ["certifi"] +completion = ["shtab (>=1.11)"] + +[[package]] +name = "packaging" +version = "26.3" +description = "Core utilities for Python packages" +optional = false +python-versions = ">=3.9" +groups = ["dev"] +files = [ + {file = "packaging-26.3-py3-none-any.whl", hash = "sha256:d7193f7c8e4e93f444fde0262bf90af30e16fa0ad0ad44cb553c87339b23cd1c"}, + {file = "packaging-26.3.tar.gz", hash = "sha256:94edc256424af38762eb31306eed28beb9f0efc50a8837492c9d6fd6004aed79"}, +] + +[[package]] +name = "pathspec" +version = "1.1.1" +description = "Utility library for gitignore style pattern matching of file paths." +optional = false +python-versions = ">=3.9" +groups = ["dev"] +files = [ + {file = "pathspec-1.1.1-py3-none-any.whl", hash = "sha256:a00ce642f577bf7f473932318056212bc4f8bfdf53128c78bbd5af0b9b20b189"}, + {file = "pathspec-1.1.1.tar.gz", hash = "sha256:17db5ecd524104a120e173814c90367a96a98d07c45b2e10c2f3919fff91bf5a"}, +] + +[package.extras] +hyperscan = ["hyperscan (>=0.7)"] +optional = ["typing-extensions (>=4)"] +re2 = ["google-re2 (>=1.1)"] + +[[package]] +name = "platformdirs" +version = "4.14.2" +description = "A small Python package for determining appropriate platform-specific dirs, e.g. a `user data dir`." +optional = false +python-versions = ">=3.11" +groups = ["dev"] +files = [ + {file = "platformdirs-4.14.2-py3-none-any.whl", hash = "sha256:dc1f93390f2a9265e44e5afc8667ec7faf737739604e39bc538fec889720c36b"}, + {file = "platformdirs-4.14.2.tar.gz", hash = "sha256:6d13b4c1d66b7548dcec9e3db13c1ba9d9adb65041b5d07869313afc07e6e41b"}, +] + +[[package]] +name = "pluggy" +version = "1.7.0" +description = "plugin and hook calling mechanisms for python" +optional = false +python-versions = ">=3.10" +groups = ["dev"] +files = [ + {file = "pluggy-1.7.0-py3-none-any.whl", hash = "sha256:7dd7b0d8832ba3cb632c306926ded123429211b83641b35dc5c41ad2d34f9bec"}, + {file = "pluggy-1.7.0.tar.gz", hash = "sha256:d1eaa46ebb595891b860ab086b4d09c8588af65ebd4361b8e8f4bb8920b90ba8"}, +] + +[[package]] +name = "pre-commit" +version = "4.7.0" +description = "A framework for managing and maintaining multi-language pre-commit hooks." +optional = false +python-versions = ">=3.11" +groups = ["dev"] +files = [ + {file = "pre_commit-4.7.0-py2.py3-none-any.whl", hash = "sha256:2e229038ad3656081b70c27913157e4b636ef306f643f024a8a8e3f0f7b2f877"}, + {file = "pre_commit-4.7.0.tar.gz", hash = "sha256:1f3deee12b914c8fa184bd2b5953391c2e4f36763e403d4a23dc9983fa7c6e86"}, +] + +[package.dependencies] +cfgv = ">=2.0.0" +identify = ">=1.0.0" +nodeenv = ">=0.11.1" +pyyaml = ">=5.1" +virtualenv = ">=20.10.0" + +[[package]] +name = "pycparser" +version = "3.12" +description = "C parser in Python" +optional = false +python-versions = ">=3.11" +groups = ["dev"] +markers = "implementation_name != \"PyPy\"" +files = [ + {file = "pycparser-3.12-py3-none-any.whl", hash = "sha256:a076f3f489b39b43ed13563a1dafe42447445bec2e294154ec5c57fcdda4c85e"}, + {file = "pycparser-3.12.tar.gz", hash = "sha256:f03387877eab82ce01bbdfd0ba7252ee55a90b1c708d5129e2ce915475cf1db6"}, +] + +[[package]] +name = "pygments" +version = "2.21.0" +description = "Pygments is a syntax highlighting package written in Python." +optional = false +python-versions = ">=3.9" +groups = ["dev"] +files = [ + {file = "pygments-2.21.0-py3-none-any.whl", hash = "sha256:2363c69b61c4a97c838da3b130dcd6468f4848992b21a82f2a63ec34377137d9"}, + {file = "pygments-2.21.0.tar.gz", hash = "sha256:610ca751c9bc2492b38eb9a38a7fbc93edbbb2d7182edaf34e66ae493dee5c8c"}, +] + +[package.extras] +windows-terminal = ["colorama (>=0.4.6)"] + +[[package]] +name = "pytest" +version = "8.4.2" +description = "pytest: simple powerful testing with Python" +optional = false +python-versions = ">=3.9" +groups = ["dev"] +files = [ + {file = "pytest-8.4.2-py3-none-any.whl", hash = "sha256:872f880de3fc3a5bdc88a11b39c9710c3497a547cfa9320bc3c5e62fbf272e79"}, + {file = "pytest-8.4.2.tar.gz", hash = "sha256:86c0d0b93306b961d58d62a4db4879f27fe25513d4b969df351abdddb3c30e01"}, +] + +[package.dependencies] +colorama = {version = ">=0.4", markers = "sys_platform == \"win32\""} +iniconfig = ">=1" +packaging = ">=20" +pluggy = ">=1.5,<2" +pygments = ">=2.7.2" + +[package.extras] +dev = ["argcomplete", "attrs (>=19.2)", "hypothesis (>=3.56)", "mock", "requests", "setuptools", "xmlschema"] + +[[package]] +name = "python-discovery" +version = "1.9.5" +description = "Python interpreter discovery" +optional = false +python-versions = ">=3.9" +groups = ["dev"] +files = [ + {file = "python_discovery-1.9.5-py3-none-any.whl", hash = "sha256:a1fae8fca11e4bc61e38491c2aafff29e430b3c5e272d51ae52fb895bc4c0fac"}, + {file = "python_discovery-1.9.5.tar.gz", hash = "sha256:86279f5d2728bc067de28b7ddc571be2eacc2ae0e7048f8629d2481a0f8848a2"}, +] + +[package.dependencies] +filelock = ">=3.16.1" + +[[package]] +name = "pytokens" +version = "0.4.1" +description = "A Fast, spec compliant Python 3.14+ tokenizer that runs on older Pythons." +optional = false +python-versions = ">=3.8" +groups = ["dev"] +files = [ + {file = "pytokens-0.4.1-cp310-cp310-macosx_11_0_arm64.whl", hash = "sha256:2a44ed93ea23415c54f3face3b65ef2b844d96aeb3455b8a69b3df6beab6acc5"}, + {file = "pytokens-0.4.1-cp310-cp310-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:add8bf86b71a5d9fb5b89f023a80b791e04fba57960aa790cc6125f7f1d39dfe"}, + {file = "pytokens-0.4.1-cp310-cp310-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:670d286910b531c7b7e3c0b453fd8156f250adb140146d234a82219459b9640c"}, + {file = "pytokens-0.4.1-cp310-cp310-musllinux_1_2_x86_64.whl", hash = "sha256:4e691d7f5186bd2842c14813f79f8884bb03f5995f0575272009982c5ac6c0f7"}, + {file = "pytokens-0.4.1-cp310-cp310-win_amd64.whl", hash = "sha256:27b83ad28825978742beef057bfe406ad6ed524b2d28c252c5de7b4a6dd48fa2"}, + {file = "pytokens-0.4.1-cp311-cp311-macosx_11_0_arm64.whl", hash = "sha256:d70e77c55ae8380c91c0c18dea05951482e263982911fc7410b1ffd1dadd3440"}, + {file = "pytokens-0.4.1-cp311-cp311-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:4a58d057208cb9075c144950d789511220b07636dd2e4708d5645d24de666bdc"}, + {file = "pytokens-0.4.1-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:b49750419d300e2b5a3813cf229d4e5a4c728dae470bcc89867a9ad6f25a722d"}, + {file = "pytokens-0.4.1-cp311-cp311-musllinux_1_2_x86_64.whl", hash = "sha256:d9907d61f15bf7261d7e775bd5d7ee4d2930e04424bab1972591918497623a16"}, + {file = "pytokens-0.4.1-cp311-cp311-win_amd64.whl", hash = "sha256:ee44d0f85b803321710f9239f335aafe16553b39106384cef8e6de40cb4ef2f6"}, + {file = "pytokens-0.4.1-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:140709331e846b728475786df8aeb27d24f48cbcf7bcd449f8de75cae7a45083"}, + {file = "pytokens-0.4.1-cp312-cp312-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:6d6c4268598f762bc8e91f5dbf2ab2f61f7b95bdc07953b602db879b3c8c18e1"}, + {file = "pytokens-0.4.1-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:24afde1f53d95348b5a0eb19488661147285ca4dd7ed752bbc3e1c6242a304d1"}, + {file = "pytokens-0.4.1-cp312-cp312-musllinux_1_2_x86_64.whl", hash = "sha256:5ad948d085ed6c16413eb5fec6b3e02fa00dc29a2534f088d3302c47eb59adf9"}, + {file = "pytokens-0.4.1-cp312-cp312-win_amd64.whl", hash = "sha256:3f901fe783e06e48e8cbdc82d631fca8f118333798193e026a50ce1b3757ea68"}, + {file = "pytokens-0.4.1-cp313-cp313-macosx_11_0_arm64.whl", hash = "sha256:8bdb9d0ce90cbf99c525e75a2fa415144fd570a1ba987380190e8b786bc6ef9b"}, + {file = "pytokens-0.4.1-cp313-cp313-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:5502408cab1cb18e128570f8d598981c68a50d0cbd7c61312a90507cd3a1276f"}, + {file = "pytokens-0.4.1-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:29d1d8fb1030af4d231789959f21821ab6325e463f0503a61d204343c9b355d1"}, + {file = "pytokens-0.4.1-cp313-cp313-musllinux_1_2_x86_64.whl", hash = "sha256:970b08dd6b86058b6dc07efe9e98414f5102974716232d10f32ff39701e841c4"}, + {file = "pytokens-0.4.1-cp313-cp313-win_amd64.whl", hash = "sha256:9bd7d7f544d362576be74f9d5901a22f317efc20046efe2034dced238cbbfe78"}, + {file = "pytokens-0.4.1-cp314-cp314-macosx_11_0_arm64.whl", hash = "sha256:4a14d5f5fc78ce85e426aa159489e2d5961acf0e47575e08f35584009178e321"}, + {file = "pytokens-0.4.1-cp314-cp314-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:97f50fd18543be72da51dd505e2ed20d2228c74e0464e4262e4899797803d7fa"}, + {file = "pytokens-0.4.1-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:dc74c035f9bfca0255c1af77ddd2d6ae8419012805453e4b0e7513e17904545d"}, + {file = "pytokens-0.4.1-cp314-cp314-musllinux_1_2_x86_64.whl", hash = "sha256:f66a6bbe741bd431f6d741e617e0f39ec7257ca1f89089593479347cc4d13324"}, + {file = "pytokens-0.4.1-cp314-cp314-win_amd64.whl", hash = "sha256:b35d7e5ad269804f6697727702da3c517bb8a5228afa450ab0fa787732055fc9"}, + {file = "pytokens-0.4.1-cp314-cp314t-macosx_11_0_arm64.whl", hash = "sha256:8fcb9ba3709ff77e77f1c7022ff11d13553f3c30299a9fe246a166903e9091eb"}, + {file = "pytokens-0.4.1-cp314-cp314t-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:79fc6b8699564e1f9b521582c35435f1bd32dd06822322ec44afdeba666d8cb3"}, + {file = "pytokens-0.4.1-cp314-cp314t-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:d31b97b3de0f61571a124a00ffe9a81fb9939146c122c11060725bd5aea79975"}, + {file = "pytokens-0.4.1-cp314-cp314t-musllinux_1_2_x86_64.whl", hash = "sha256:967cf6e3fd4adf7de8fc73cd3043754ae79c36475c1c11d514fc72cf5490094a"}, + {file = "pytokens-0.4.1-cp314-cp314t-win_amd64.whl", hash = "sha256:584c80c24b078eec1e227079d56dc22ff755e0ba8654d8383b2c549107528918"}, + {file = "pytokens-0.4.1-cp38-cp38-macosx_11_0_arm64.whl", hash = "sha256:da5baeaf7116dced9c6bb76dc31ba04a2dc3695f3d9f74741d7910122b456edc"}, + {file = "pytokens-0.4.1-cp38-cp38-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:11edda0942da80ff58c4408407616a310adecae1ddd22eef8c692fe266fa5009"}, + {file = "pytokens-0.4.1-cp38-cp38-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:0fc71786e629cef478cbf29d7ea1923299181d0699dbe7c3c0f4a583811d9fc1"}, + {file = "pytokens-0.4.1-cp38-cp38-musllinux_1_2_x86_64.whl", hash = "sha256:dcafc12c30dbaf1e2af0490978352e0c4041a7cde31f4f81435c2a5e8b9cabb6"}, + {file = "pytokens-0.4.1-cp38-cp38-win_amd64.whl", hash = "sha256:42f144f3aafa5d92bad964d471a581651e28b24434d184871bd02e3a0d956037"}, + {file = "pytokens-0.4.1-cp39-cp39-macosx_11_0_arm64.whl", hash = "sha256:34bcc734bd2f2d5fe3b34e7b3c0116bfb2397f2d9666139988e7a3eb5f7400e3"}, + {file = "pytokens-0.4.1-cp39-cp39-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:941d4343bf27b605e9213b26bfa1c4bf197c9c599a9627eb7305b0defcfe40c1"}, + {file = "pytokens-0.4.1-cp39-cp39-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:3ad72b851e781478366288743198101e5eb34a414f1d5627cdd585ca3b25f1db"}, + {file = "pytokens-0.4.1-cp39-cp39-musllinux_1_2_x86_64.whl", hash = "sha256:682fa37ff4d8e95f7df6fe6fe6a431e8ed8e788023c6bcc0f0880a12eab80ad1"}, + {file = "pytokens-0.4.1-cp39-cp39-win_amd64.whl", hash = "sha256:30f51edd9bb7f85c748979384165601d028b84f7bd13fe14d3e065304093916a"}, + {file = "pytokens-0.4.1-py3-none-any.whl", hash = "sha256:26cef14744a8385f35d0e095dc8b3a7583f6c953c2e3d269c7f82484bf5ad2de"}, + {file = "pytokens-0.4.1.tar.gz", hash = "sha256:292052fe80923aae2260c073f822ceba21f3872ced9a68bb7953b348e561179a"}, +] + +[package.extras] +dev = ["black", "build", "mypy", "pytest", "pytest-cov", "setuptools", "tox", "twine", "wheel"] + +[[package]] +name = "pyyaml" +version = "6.0.3" +description = "YAML parser and emitter for Python" +optional = false +python-versions = ">=3.8" +groups = ["dev"] +files = [ + {file = "PyYAML-6.0.3-cp38-cp38-macosx_10_13_x86_64.whl", hash = "sha256:c2514fceb77bc5e7a2f7adfaa1feb2fb311607c9cb518dbc378688ec73d8292f"}, + {file = "PyYAML-6.0.3-cp38-cp38-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:9c57bb8c96f6d1808c030b1687b9b5fb476abaa47f0db9c0101f5e9f394e97f4"}, + {file = "PyYAML-6.0.3-cp38-cp38-manylinux2014_s390x.manylinux_2_17_s390x.manylinux_2_28_s390x.whl", hash = "sha256:efd7b85f94a6f21e4932043973a7ba2613b059c4a000551892ac9f1d11f5baf3"}, + {file = "PyYAML-6.0.3-cp38-cp38-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:22ba7cfcad58ef3ecddc7ed1db3409af68d023b7f940da23c6c2a1890976eda6"}, + {file = "PyYAML-6.0.3-cp38-cp38-musllinux_1_2_x86_64.whl", hash = "sha256:6344df0d5755a2c9a276d4473ae6b90647e216ab4757f8426893b5dd2ac3f369"}, + {file = "PyYAML-6.0.3-cp38-cp38-win32.whl", hash = "sha256:3ff07ec89bae51176c0549bc4c63aa6202991da2d9a6129d7aef7f1407d3f295"}, + {file = "PyYAML-6.0.3-cp38-cp38-win_amd64.whl", hash = "sha256:5cf4e27da7e3fbed4d6c3d8e797387aaad68102272f8f9752883bc32d61cb87b"}, + {file = "pyyaml-6.0.3-cp310-cp310-macosx_10_13_x86_64.whl", hash = "sha256:214ed4befebe12df36bcc8bc2b64b396ca31be9304b8f59e25c11cf94a4c033b"}, + {file = "pyyaml-6.0.3-cp310-cp310-macosx_11_0_arm64.whl", hash = "sha256:02ea2dfa234451bbb8772601d7b8e426c2bfa197136796224e50e35a78777956"}, + {file = "pyyaml-6.0.3-cp310-cp310-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:b30236e45cf30d2b8e7b3e85881719e98507abed1011bf463a8fa23e9c3e98a8"}, + {file = "pyyaml-6.0.3-cp310-cp310-manylinux2014_s390x.manylinux_2_17_s390x.manylinux_2_28_s390x.whl", hash = "sha256:66291b10affd76d76f54fad28e22e51719ef9ba22b29e1d7d03d6777a9174198"}, + {file = "pyyaml-6.0.3-cp310-cp310-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:9c7708761fccb9397fe64bbc0395abcae8c4bf7b0eac081e12b809bf47700d0b"}, + {file = "pyyaml-6.0.3-cp310-cp310-musllinux_1_2_aarch64.whl", hash = "sha256:418cf3f2111bc80e0933b2cd8cd04f286338bb88bdc7bc8e6dd775ebde60b5e0"}, + {file = "pyyaml-6.0.3-cp310-cp310-musllinux_1_2_x86_64.whl", hash = "sha256:5e0b74767e5f8c593e8c9b5912019159ed0533c70051e9cce3e8b6aa699fcd69"}, + {file = "pyyaml-6.0.3-cp310-cp310-win32.whl", hash = "sha256:28c8d926f98f432f88adc23edf2e6d4921ac26fb084b028c733d01868d19007e"}, + {file = "pyyaml-6.0.3-cp310-cp310-win_amd64.whl", hash = "sha256:bdb2c67c6c1390b63c6ff89f210c8fd09d9a1217a465701eac7316313c915e4c"}, + {file = "pyyaml-6.0.3-cp311-cp311-macosx_10_13_x86_64.whl", hash = "sha256:44edc647873928551a01e7a563d7452ccdebee747728c1080d881d68af7b997e"}, + {file = "pyyaml-6.0.3-cp311-cp311-macosx_11_0_arm64.whl", hash = "sha256:652cb6edd41e718550aad172851962662ff2681490a8a711af6a4d288dd96824"}, + {file = "pyyaml-6.0.3-cp311-cp311-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:10892704fc220243f5305762e276552a0395f7beb4dbf9b14ec8fd43b57f126c"}, + {file = "pyyaml-6.0.3-cp311-cp311-manylinux2014_s390x.manylinux_2_17_s390x.manylinux_2_28_s390x.whl", hash = "sha256:850774a7879607d3a6f50d36d04f00ee69e7fc816450e5f7e58d7f17f1ae5c00"}, + {file = "pyyaml-6.0.3-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:b8bb0864c5a28024fac8a632c443c87c5aa6f215c0b126c449ae1a150412f31d"}, + {file = "pyyaml-6.0.3-cp311-cp311-musllinux_1_2_aarch64.whl", hash = "sha256:1d37d57ad971609cf3c53ba6a7e365e40660e3be0e5175fa9f2365a379d6095a"}, + {file = "pyyaml-6.0.3-cp311-cp311-musllinux_1_2_x86_64.whl", hash = "sha256:37503bfbfc9d2c40b344d06b2199cf0e96e97957ab1c1b546fd4f87e53e5d3e4"}, + {file = "pyyaml-6.0.3-cp311-cp311-win32.whl", hash = "sha256:8098f252adfa6c80ab48096053f512f2321f0b998f98150cea9bd23d83e1467b"}, + {file = "pyyaml-6.0.3-cp311-cp311-win_amd64.whl", hash = "sha256:9f3bfb4965eb874431221a3ff3fdcddc7e74e3b07799e0e84ca4a0f867d449bf"}, + {file = "pyyaml-6.0.3-cp312-cp312-macosx_10_13_x86_64.whl", hash = "sha256:7f047e29dcae44602496db43be01ad42fc6f1cc0d8cd6c83d342306c32270196"}, + {file = "pyyaml-6.0.3-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:fc09d0aa354569bc501d4e787133afc08552722d3ab34836a80547331bb5d4a0"}, + {file = "pyyaml-6.0.3-cp312-cp312-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:9149cad251584d5fb4981be1ecde53a1ca46c891a79788c0df828d2f166bda28"}, + {file = "pyyaml-6.0.3-cp312-cp312-manylinux2014_s390x.manylinux_2_17_s390x.manylinux_2_28_s390x.whl", hash = "sha256:5fdec68f91a0c6739b380c83b951e2c72ac0197ace422360e6d5a959d8d97b2c"}, + {file = "pyyaml-6.0.3-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:ba1cc08a7ccde2d2ec775841541641e4548226580ab850948cbfda66a1befcdc"}, + {file = "pyyaml-6.0.3-cp312-cp312-musllinux_1_2_aarch64.whl", hash = "sha256:8dc52c23056b9ddd46818a57b78404882310fb473d63f17b07d5c40421e47f8e"}, + {file = "pyyaml-6.0.3-cp312-cp312-musllinux_1_2_x86_64.whl", hash = "sha256:41715c910c881bc081f1e8872880d3c650acf13dfa8214bad49ed4cede7c34ea"}, + {file = "pyyaml-6.0.3-cp312-cp312-win32.whl", hash = "sha256:96b533f0e99f6579b3d4d4995707cf36df9100d67e0c8303a0c55b27b5f99bc5"}, + {file = "pyyaml-6.0.3-cp312-cp312-win_amd64.whl", hash = "sha256:5fcd34e47f6e0b794d17de1b4ff496c00986e1c83f7ab2fb8fcfe9616ff7477b"}, + {file = "pyyaml-6.0.3-cp312-cp312-win_arm64.whl", hash = "sha256:64386e5e707d03a7e172c0701abfb7e10f0fb753ee1d773128192742712a98fd"}, + {file = "pyyaml-6.0.3-cp313-cp313-macosx_10_13_x86_64.whl", hash = "sha256:8da9669d359f02c0b91ccc01cac4a67f16afec0dac22c2ad09f46bee0697eba8"}, + {file = "pyyaml-6.0.3-cp313-cp313-macosx_11_0_arm64.whl", hash = "sha256:2283a07e2c21a2aa78d9c4442724ec1eb15f5e42a723b99cb3d822d48f5f7ad1"}, + {file = "pyyaml-6.0.3-cp313-cp313-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:ee2922902c45ae8ccada2c5b501ab86c36525b883eff4255313a253a3160861c"}, + {file = "pyyaml-6.0.3-cp313-cp313-manylinux2014_s390x.manylinux_2_17_s390x.manylinux_2_28_s390x.whl", hash = "sha256:a33284e20b78bd4a18c8c2282d549d10bc8408a2a7ff57653c0cf0b9be0afce5"}, + {file = "pyyaml-6.0.3-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:0f29edc409a6392443abf94b9cf89ce99889a1dd5376d94316ae5145dfedd5d6"}, + {file = "pyyaml-6.0.3-cp313-cp313-musllinux_1_2_aarch64.whl", hash = "sha256:f7057c9a337546edc7973c0d3ba84ddcdf0daa14533c2065749c9075001090e6"}, + {file = "pyyaml-6.0.3-cp313-cp313-musllinux_1_2_x86_64.whl", hash = "sha256:eda16858a3cab07b80edaf74336ece1f986ba330fdb8ee0d6c0d68fe82bc96be"}, + {file = "pyyaml-6.0.3-cp313-cp313-win32.whl", hash = "sha256:d0eae10f8159e8fdad514efdc92d74fd8d682c933a6dd088030f3834bc8e6b26"}, + {file = "pyyaml-6.0.3-cp313-cp313-win_amd64.whl", hash = "sha256:79005a0d97d5ddabfeeea4cf676af11e647e41d81c9a7722a193022accdb6b7c"}, + {file = "pyyaml-6.0.3-cp313-cp313-win_arm64.whl", hash = "sha256:5498cd1645aa724a7c71c8f378eb29ebe23da2fc0d7a08071d89469bf1d2defb"}, + {file = "pyyaml-6.0.3-cp314-cp314-macosx_10_13_x86_64.whl", hash = "sha256:8d1fab6bb153a416f9aeb4b8763bc0f22a5586065f86f7664fc23339fc1c1fac"}, + {file = "pyyaml-6.0.3-cp314-cp314-macosx_11_0_arm64.whl", hash = "sha256:34d5fcd24b8445fadc33f9cf348c1047101756fd760b4dacb5c3e99755703310"}, + {file = "pyyaml-6.0.3-cp314-cp314-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:501a031947e3a9025ed4405a168e6ef5ae3126c59f90ce0cd6f2bfc477be31b7"}, + {file = "pyyaml-6.0.3-cp314-cp314-manylinux2014_s390x.manylinux_2_17_s390x.manylinux_2_28_s390x.whl", hash = "sha256:b3bc83488de33889877a0f2543ade9f70c67d66d9ebb4ac959502e12de895788"}, + {file = "pyyaml-6.0.3-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:c458b6d084f9b935061bc36216e8a69a7e293a2f1e68bf956dcd9e6cbcd143f5"}, + {file = "pyyaml-6.0.3-cp314-cp314-musllinux_1_2_aarch64.whl", hash = "sha256:7c6610def4f163542a622a73fb39f534f8c101d690126992300bf3207eab9764"}, + {file = "pyyaml-6.0.3-cp314-cp314-musllinux_1_2_x86_64.whl", hash = "sha256:5190d403f121660ce8d1d2c1bb2ef1bd05b5f68533fc5c2ea899bd15f4399b35"}, + {file = "pyyaml-6.0.3-cp314-cp314-win_amd64.whl", hash = "sha256:4a2e8cebe2ff6ab7d1050ecd59c25d4c8bd7e6f400f5f82b96557ac0abafd0ac"}, + {file = "pyyaml-6.0.3-cp314-cp314-win_arm64.whl", hash = "sha256:93dda82c9c22deb0a405ea4dc5f2d0cda384168e466364dec6255b293923b2f3"}, + {file = "pyyaml-6.0.3-cp314-cp314t-macosx_10_13_x86_64.whl", hash = "sha256:02893d100e99e03eda1c8fd5c441d8c60103fd175728e23e431db1b589cf5ab3"}, + {file = "pyyaml-6.0.3-cp314-cp314t-macosx_11_0_arm64.whl", hash = "sha256:c1ff362665ae507275af2853520967820d9124984e0f7466736aea23d8611fba"}, + {file = "pyyaml-6.0.3-cp314-cp314t-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:6adc77889b628398debc7b65c073bcb99c4a0237b248cacaf3fe8a557563ef6c"}, + {file = "pyyaml-6.0.3-cp314-cp314t-manylinux2014_s390x.manylinux_2_17_s390x.manylinux_2_28_s390x.whl", hash = "sha256:a80cb027f6b349846a3bf6d73b5e95e782175e52f22108cfa17876aaeff93702"}, + {file = "pyyaml-6.0.3-cp314-cp314t-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:00c4bdeba853cc34e7dd471f16b4114f4162dc03e6b7afcc2128711f0eca823c"}, + {file = "pyyaml-6.0.3-cp314-cp314t-musllinux_1_2_aarch64.whl", hash = "sha256:66e1674c3ef6f541c35191caae2d429b967b99e02040f5ba928632d9a7f0f065"}, + {file = "pyyaml-6.0.3-cp314-cp314t-musllinux_1_2_x86_64.whl", hash = "sha256:16249ee61e95f858e83976573de0f5b2893b3677ba71c9dd36b9cf8be9ac6d65"}, + {file = "pyyaml-6.0.3-cp314-cp314t-win_amd64.whl", hash = "sha256:4ad1906908f2f5ae4e5a8ddfce73c320c2a1429ec52eafd27138b7f1cbe341c9"}, + {file = "pyyaml-6.0.3-cp314-cp314t-win_arm64.whl", hash = "sha256:ebc55a14a21cb14062aa4162f906cd962b28e2e9ea38f9b4391244cd8de4ae0b"}, + {file = "pyyaml-6.0.3-cp39-cp39-macosx_10_13_x86_64.whl", hash = "sha256:b865addae83924361678b652338317d1bd7e79b1f4596f96b96c77a5a34b34da"}, + {file = "pyyaml-6.0.3-cp39-cp39-macosx_11_0_arm64.whl", hash = "sha256:c3355370a2c156cffb25e876646f149d5d68f5e0a3ce86a5084dd0b64a994917"}, + {file = "pyyaml-6.0.3-cp39-cp39-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:3c5677e12444c15717b902a5798264fa7909e41153cdf9ef7ad571b704a63dd9"}, + {file = "pyyaml-6.0.3-cp39-cp39-manylinux2014_s390x.manylinux_2_17_s390x.manylinux_2_28_s390x.whl", hash = "sha256:5ed875a24292240029e4483f9d4a4b8a1ae08843b9c54f43fcc11e404532a8a5"}, + {file = "pyyaml-6.0.3-cp39-cp39-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:0150219816b6a1fa26fb4699fb7daa9caf09eb1999f3b70fb6e786805e80375a"}, + {file = "pyyaml-6.0.3-cp39-cp39-musllinux_1_2_aarch64.whl", hash = "sha256:fa160448684b4e94d80416c0fa4aac48967a969efe22931448d853ada8baf926"}, + {file = "pyyaml-6.0.3-cp39-cp39-musllinux_1_2_x86_64.whl", hash = "sha256:27c0abcb4a5dac13684a37f76e701e054692a9b2d3064b70f5e4eb54810553d7"}, + {file = "pyyaml-6.0.3-cp39-cp39-win32.whl", hash = "sha256:1ebe39cb5fc479422b83de611d14e2c0d3bb2a18bbcb01f229ab3cfbd8fee7a0"}, + {file = "pyyaml-6.0.3-cp39-cp39-win_amd64.whl", hash = "sha256:2e71d11abed7344e42a8849600193d15b6def118602c4c176f748e4583246007"}, + {file = "pyyaml-6.0.3.tar.gz", hash = "sha256:d76623373421df22fb4cf8817020cbb7ef15c725b9d5e45f17e189bfc384190f"}, +] + +[[package]] +name = "referencing" +version = "0.37.0" +description = "JSON Referencing + Python" +optional = false +python-versions = ">=3.10" +groups = ["dev"] +files = [ + {file = "referencing-0.37.0-py3-none-any.whl", hash = "sha256:381329a9f99628c9069361716891d34ad94af76e461dcb0335825aecc7692231"}, + {file = "referencing-0.37.0.tar.gz", hash = "sha256:44aefc3142c5b842538163acb373e24cce6632bd54bdb01b21ad5863489f50d8"}, +] + +[package.dependencies] +attrs = ">=22.2.0" +rpds-py = ">=0.7.0" +typing-extensions = {version = ">=4.4.0", markers = "python_version < \"3.13\""} + +[[package]] +name = "resolvelib" +version = "1.2.1" +description = "Resolve abstract dependencies into concrete ones" +optional = false +python-versions = ">=3.9" +groups = ["dev"] +files = [ + {file = "resolvelib-1.2.1-py3-none-any.whl", hash = "sha256:fb06b66c8da04172d9e72a21d7d06186d8919e32ae5ab5cdf5b9d920be805ac2"}, + {file = "resolvelib-1.2.1.tar.gz", hash = "sha256:7d08a2022f6e16ce405d60b68c390f054efcfd0477d4b9bd019cc941c28fad1c"}, +] + +[package.extras] +lint = ["mypy", "ruff", "types-requests"] +release = ["build", "towncrier", "twine"] +test = ["packaging", "pytest"] + +[[package]] +name = "rpds-py" +version = "2026.9.1" +description = "Python bindings to Rust's persistent data structures (rpds)" +optional = false +python-versions = ">=3.11" +groups = ["dev"] +files = [ + {file = "rpds_py-2026.9.1-cp311-cp311-macosx_10_12_x86_64.whl", hash = "sha256:2711d29b653b3bce48a63d18b9c6b53274669e6d6c4094dddeb4d9a0e45128b2"}, + {file = "rpds_py-2026.9.1-cp311-cp311-macosx_11_0_arm64.whl", hash = "sha256:3231c4c0e521dafa5be0c9f114ee2c2ad46650836f2d72caa86801950c3e7044"}, + {file = "rpds_py-2026.9.1-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:e01b3c878c8641913e688edd1b3f08658c6783d29cf6b826bd3c0d1ae7a1ffaa"}, + {file = "rpds_py-2026.9.1-cp311-cp311-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:3e524c7874ac72884d28e16dd5b8d839fd09e0fe76b020d3fbca23212a7b8c52"}, + {file = "rpds_py-2026.9.1-cp311-cp311-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:761fdae6728ceb99ab182fad2f0cc1e262f610834dc891aea1d1a2a2e634776f"}, + {file = "rpds_py-2026.9.1-cp311-cp311-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:6b723eb406dec5bc9ec516c73ab9c3239a3284e017f7eb89ee2b3258bd504fb7"}, + {file = "rpds_py-2026.9.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:136a1c3fe4402b7008bc81cb62ee538481795b61a7e83df88dff3b3f02b726ff"}, + {file = "rpds_py-2026.9.1-cp311-cp311-manylinux_2_31_riscv64.whl", hash = "sha256:839dde845559254f34885267c6878f60d61d5205180226d976fe488d45fa128e"}, + {file = "rpds_py-2026.9.1-cp311-cp311-manylinux_2_5_i686.manylinux1_i686.whl", hash = "sha256:56cd8b3f77d7b6812f533b662186a1f28316931166ddc00fb893b1b0db7e9888"}, + {file = "rpds_py-2026.9.1-cp311-cp311-musllinux_1_2_aarch64.whl", hash = "sha256:074a4d198bc34d9a8ea425114fc3ded6d11ec01f6a314a8db67454a5152d8834"}, + {file = "rpds_py-2026.9.1-cp311-cp311-musllinux_1_2_i686.whl", hash = "sha256:6beb738155fe8ab8091afdfa5a3226b21c2b1593f1e50ebb90eb25b44dbc0391"}, + {file = "rpds_py-2026.9.1-cp311-cp311-musllinux_1_2_x86_64.whl", hash = "sha256:57492a550a1d88d29d003247e5f78dd8cf04a701fac0e4c8db8745a6d2504e0a"}, + {file = "rpds_py-2026.9.1-cp311-cp311-win32.whl", hash = "sha256:d95a354e02393eada6d7351184671aced9d4cce109dabf927cb7aa99624352a1"}, + {file = "rpds_py-2026.9.1-cp311-cp311-win_amd64.whl", hash = "sha256:ce4d4f52e2a4324396caddbd45a97d8d7be5f42edd25d2355282a9c34f9b2f7f"}, + {file = "rpds_py-2026.9.1-cp311-cp311-win_arm64.whl", hash = "sha256:fdcd198979b4ecffcc1beba366a7fbcf4eb41243691a82fe52ceb0b902f09c12"}, + {file = "rpds_py-2026.9.1-cp312-cp312-macosx_10_12_x86_64.whl", hash = "sha256:50906f5aea24b5a865cbd0a589698288631d9f3a54c3a937c83aefa95a0d14af"}, + {file = "rpds_py-2026.9.1-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:e21c1429e205828ea886a2293a4a2c8e01f4c25d9893ca330e97a6cf73f52e7b"}, + {file = "rpds_py-2026.9.1-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:2693b2728bbcc48d09a981a356954b0c47c53ff25b545856f28a889ea619f69a"}, + {file = "rpds_py-2026.9.1-cp312-cp312-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:8601470267d938bcb7f3ab1a336100af51a4fd5b6ed030ef52461bb3ef5e7e07"}, + {file = "rpds_py-2026.9.1-cp312-cp312-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:3890a6aa36e6baa53d5258a2a25d3ef8b37ad165a6ab27a892d7c3e3a432cd69"}, + {file = "rpds_py-2026.9.1-cp312-cp312-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:6b5b393eda5ea42cca1c1a6665f2a4882b4fd5d1777e41ce0545a107fb008c9d"}, + {file = "rpds_py-2026.9.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:addeda51556dac7c1a2f14cda62db8b621cd12afba3091d03a96c72932387eab"}, + {file = "rpds_py-2026.9.1-cp312-cp312-manylinux_2_31_riscv64.whl", hash = "sha256:d9edf30457d74eebfd76b045535e36f1cd89062566a128a0db2145ca042d787e"}, + {file = "rpds_py-2026.9.1-cp312-cp312-manylinux_2_5_i686.manylinux1_i686.whl", hash = "sha256:815d26356930846a40c7bc1366e7b1b0320ab8a063e66c11298a208bed0fd237"}, + {file = "rpds_py-2026.9.1-cp312-cp312-musllinux_1_2_aarch64.whl", hash = "sha256:3b5a6f40f0a1486b4b36c888123afc67acdbd9f33235927acf5ff295429a0ba3"}, + {file = "rpds_py-2026.9.1-cp312-cp312-musllinux_1_2_i686.whl", hash = "sha256:b5b8b0753718d258fd454283fbd57e14545d3b40583fa672e27cb4f987626bcc"}, + {file = "rpds_py-2026.9.1-cp312-cp312-musllinux_1_2_x86_64.whl", hash = "sha256:46d80bc76b51a6c24f9944368c28d38b8bcbcea1da4f2f8d3ebc31a67e8c6ec6"}, + {file = "rpds_py-2026.9.1-cp312-cp312-win32.whl", hash = "sha256:befc2d6a953e563f8a7bfd87a42c22ebf8a3e980dcb7b6a4d17b70b0e914e8a3"}, + {file = "rpds_py-2026.9.1-cp312-cp312-win_amd64.whl", hash = "sha256:5ce8943f79c2210f7abcc28e86367b03b28d95027fd01c46d2472373ae70c86f"}, + {file = "rpds_py-2026.9.1-cp312-cp312-win_arm64.whl", hash = "sha256:501909f2e4a1e2dee528ef766fe3c469060ebc17e54a8383d404ba07a81a6f02"}, + {file = "rpds_py-2026.9.1-cp313-cp313-macosx_10_12_x86_64.whl", hash = "sha256:a36b70596407634ca82d4b989a3729074a008537a0522e4c8046a67c729103e9"}, + {file = "rpds_py-2026.9.1-cp313-cp313-macosx_11_0_arm64.whl", hash = "sha256:eba5d173f7d5708b22a93815017a4611873ed54db9f268077c0dd1ed99cfc858"}, + {file = "rpds_py-2026.9.1-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:457866b85daf5034296666168b84a69e0b2e89dc4f1af102b46f6448a60b9063"}, + {file = "rpds_py-2026.9.1-cp313-cp313-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:a3a52a3ba86436ab3aef510fbe21512abc2ddd1993005dfe50514bd2284ef025"}, + {file = "rpds_py-2026.9.1-cp313-cp313-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:d7841166b7fa64c9c56404617ae4341448847482d45933b13135d26c130519e5"}, + {file = "rpds_py-2026.9.1-cp313-cp313-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:926bdd3e3b5998ddf70cc64bc8cf57209571f9044542913afb673799fec77dd0"}, + {file = "rpds_py-2026.9.1-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:7868b85224291c6cb6759f9b5adb9745f486d226f62b16a614dd5a2a5ab2b35b"}, + {file = "rpds_py-2026.9.1-cp313-cp313-manylinux_2_31_riscv64.whl", hash = "sha256:3cd182d7291d29b92c521a0069d9c01ba6193628a9a105531d11b40a6d731a33"}, + {file = "rpds_py-2026.9.1-cp313-cp313-manylinux_2_5_i686.manylinux1_i686.whl", hash = "sha256:e6ea1cda8d8c688278430e4268a42f5e5da3bdd74578dfadc0820c3f1766ce83"}, + {file = "rpds_py-2026.9.1-cp313-cp313-musllinux_1_2_aarch64.whl", hash = "sha256:5943980471829f6de242a20b109de3111ba6b77e3af0ffc587028ac854b05e6c"}, + {file = "rpds_py-2026.9.1-cp313-cp313-musllinux_1_2_i686.whl", hash = "sha256:76d3af9732d2dab69f28179b40ba2d87e2f1d5824b4a694780aa787d685e8f36"}, + {file = "rpds_py-2026.9.1-cp313-cp313-musllinux_1_2_x86_64.whl", hash = "sha256:78326f4cb4427a56ba4996c0762b63be45f06b85f086526420d2b3a66e40f84d"}, + {file = "rpds_py-2026.9.1-cp313-cp313-win32.whl", hash = "sha256:172e47169583f46ce118cbec68e6795d0da0f4606b488b6434f8276bca0a058c"}, + {file = "rpds_py-2026.9.1-cp313-cp313-win_amd64.whl", hash = "sha256:3e93b2cd69a9830be33e03945cd7cda940a0a8bfcfbff41d6144f0cb0d3d8bd9"}, + {file = "rpds_py-2026.9.1-cp313-cp313-win_arm64.whl", hash = "sha256:d151e148117294133bf8af7eeace085e7e87432db15ab6adf640330298a47f6f"}, + {file = "rpds_py-2026.9.1-cp314-cp314-macosx_10_12_x86_64.whl", hash = "sha256:c9d1aca01f49170fdcf5c92761b1fafe97f554b721ca4570c5949fff778f0d4b"}, + {file = "rpds_py-2026.9.1-cp314-cp314-macosx_11_0_arm64.whl", hash = "sha256:3f0e9ac28fc067d4d34b88ae43c48e9489455c97fee9633d851f7eeed5a05d35"}, + {file = "rpds_py-2026.9.1-cp314-cp314-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:07deecbfce94c78473018bc7d10b337cc651d12df87a1eb2cb3e4024bc9c33d0"}, + {file = "rpds_py-2026.9.1-cp314-cp314-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:821b2755db9194409254012f429c56643416fb96ef9be090be82ec8826b7f477"}, + {file = "rpds_py-2026.9.1-cp314-cp314-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:3c91c210ae7645626c608400e3519b4a642f837cce09ca830db3beb2e9f274d4"}, + {file = "rpds_py-2026.9.1-cp314-cp314-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:54ac2158a6f96cfbabff0b2eedaf94b90c5ec7ca8317fcadc61e1c2b2e0ff6ef"}, + {file = "rpds_py-2026.9.1-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:eac2f5dbafd585dfe31f86a23ebf0d3ba480a9d49ebc87947267b5608d4ea0cd"}, + {file = "rpds_py-2026.9.1-cp314-cp314-manylinux_2_31_riscv64.whl", hash = "sha256:8aa5dda18d39b6143eb24809d158f9252c88f402749b6f1b62a506cc7d96cc35"}, + {file = "rpds_py-2026.9.1-cp314-cp314-manylinux_2_5_i686.manylinux1_i686.whl", hash = "sha256:5c90e7fa02e8f5de0d10c17595c568ada48c5302e749462c0ea1a4c362111a86"}, + {file = "rpds_py-2026.9.1-cp314-cp314-musllinux_1_2_aarch64.whl", hash = "sha256:e6d198bad4e49dd6732fbd636e2fc5c082f45c8cad0b4acb756b00c82c76072e"}, + {file = "rpds_py-2026.9.1-cp314-cp314-musllinux_1_2_i686.whl", hash = "sha256:96beca19ec79de272e8668585380ff9092c47077c1d7a1e098e00bbd921f4785"}, + {file = "rpds_py-2026.9.1-cp314-cp314-musllinux_1_2_x86_64.whl", hash = "sha256:a5cf77eb04f20b720be95265a3e00eb2a14814074255cc27069c551b2db53118"}, + {file = "rpds_py-2026.9.1-cp314-cp314-win32.whl", hash = "sha256:a03d57b86d2a51d0a66c92177e2be154ad015f357791d306e714569999cdb4cc"}, + {file = "rpds_py-2026.9.1-cp314-cp314-win_amd64.whl", hash = "sha256:837c6b305e26fe0f75b15c92cf3b2ba29e0ae19dc40b1c557b026cb426347d0c"}, + {file = "rpds_py-2026.9.1-cp314-cp314-win_arm64.whl", hash = "sha256:fce4b85234a0cbad67bf8e6e1201ee815d172c9aebad75f25645bc4d834f8e31"}, + {file = "rpds_py-2026.9.1-cp314-cp314t-macosx_10_12_x86_64.whl", hash = "sha256:3a72c11530d71abfb66c8d7696a2f86c43e63fca8b948f1a784ac490f4ec688e"}, + {file = "rpds_py-2026.9.1-cp314-cp314t-macosx_11_0_arm64.whl", hash = "sha256:068c37bba854ec2fe42f7365c640af11dd9895890ccbf2df5070d0c059bd7f96"}, + {file = "rpds_py-2026.9.1-cp314-cp314t-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:d7fca4eb6df565e2a928f1c7dad92d27db8f9df0f449e76423ed5d7e713ed445"}, + {file = "rpds_py-2026.9.1-cp314-cp314t-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:c933c6678c6f116ff8af47a4c6db0868b8ace74af0343016c0ef00f00272ea69"}, + {file = "rpds_py-2026.9.1-cp314-cp314t-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:028ad274ea951dac64491b5d1e65712a4aeabfdbdb9fccf797b57bd899b0c495"}, + {file = "rpds_py-2026.9.1-cp314-cp314t-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:740d0a99cf9de0b17a3943388e9294a59becf75e7c43421f387bd3c7a9901f7c"}, + {file = "rpds_py-2026.9.1-cp314-cp314t-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:0da298fb372dc192610a4b9ecbc68a0cd8b675bbbd1fc519d01b41cfd658333e"}, + {file = "rpds_py-2026.9.1-cp314-cp314t-manylinux_2_31_riscv64.whl", hash = "sha256:eb61be926bb81567c1f48bdc8aa22b9855048dc2efd53871f9f7e6e9a5632346"}, + {file = "rpds_py-2026.9.1-cp314-cp314t-manylinux_2_5_i686.manylinux1_i686.whl", hash = "sha256:42e75466f83cd43f6026c81eab74246efb2bdadafb307b85700632d06c68f299"}, + {file = "rpds_py-2026.9.1-cp314-cp314t-musllinux_1_2_aarch64.whl", hash = "sha256:617f59cde379b4f648a09797b7f683d04b90a46344cddab85639da5aff0f5531"}, + {file = "rpds_py-2026.9.1-cp314-cp314t-musllinux_1_2_i686.whl", hash = "sha256:3edae8c5ddfdb6985d49ae9d150516e5076888879022f91a26c2de9276ce0bdb"}, + {file = "rpds_py-2026.9.1-cp314-cp314t-musllinux_1_2_x86_64.whl", hash = "sha256:0f045bb053c9057720d72c56dffe30dffdc05997b2897a827b9325f0ab6623fa"}, + {file = "rpds_py-2026.9.1-cp314-cp314t-win32.whl", hash = "sha256:bf35d0568abda97233239ce32896d3ad53fccc537832c104e30c94aa5fb93569"}, + {file = "rpds_py-2026.9.1-cp314-cp314t-win_amd64.whl", hash = "sha256:1e8d4d79d828299bf44a55db22a9388ab967b49d17132c88eab0f4360b48da8e"}, + {file = "rpds_py-2026.9.1-cp315-cp315-macosx_10_12_x86_64.whl", hash = "sha256:1d77b649e6f7cdf12ca5c2a98dad0ad37f9ea9b6f960408a92f0cb12bb3d04d9"}, + {file = "rpds_py-2026.9.1-cp315-cp315-macosx_11_0_arm64.whl", hash = "sha256:00ba2d8c7dd4ee537978ddf4b3fbd712bef2d8751603f7f3146b3f4287768e25"}, + {file = "rpds_py-2026.9.1-cp315-cp315-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:ec450527cbf485e13c8d3602a54f428ab0432fdade0ede75efd74b735421c871"}, + {file = "rpds_py-2026.9.1-cp315-cp315-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:306ee1850d8105b5baf977e78d45fcadd12c1a54678d614c9baf217708446e91"}, + {file = "rpds_py-2026.9.1-cp315-cp315-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:ef6b65b03247c54692ad4fd9ee97cb772781927db72e3cb05e70b3db6d1ff14f"}, + {file = "rpds_py-2026.9.1-cp315-cp315-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:a575404ebc9cf2e91edd32eaf570ec1430eb900d4f56724ba7dd4bc1fc9c176d"}, + {file = "rpds_py-2026.9.1-cp315-cp315-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:2c16ab111bc27c646ba8aa005d0527754edc538ebb636f0b1bf8e244b48d1945"}, + {file = "rpds_py-2026.9.1-cp315-cp315-manylinux_2_31_riscv64.whl", hash = "sha256:7664419f27db41d4f1c43a78dccda7dd6e8ef2428df3ee01d0c2a07a6b071297"}, + {file = "rpds_py-2026.9.1-cp315-cp315-manylinux_2_5_i686.manylinux1_i686.whl", hash = "sha256:4b26b03d9d2658ee2fa234f8f4f19f38a09773fe5261028025032e26d4d35af0"}, + {file = "rpds_py-2026.9.1-cp315-cp315-musllinux_1_2_aarch64.whl", hash = "sha256:be3e47e2d91aa3942ff9bf4077a505226005abfc39b6f7554a91c1b9393986b9"}, + {file = "rpds_py-2026.9.1-cp315-cp315-musllinux_1_2_i686.whl", hash = "sha256:6307a0da524939decb8ca4a3933b8ab62525794411d6984fca6726e732804af6"}, + {file = "rpds_py-2026.9.1-cp315-cp315-musllinux_1_2_x86_64.whl", hash = "sha256:159a7aab5c5e8b112c8830f54717ce56da1252ebdbb526f5be2df2309280b9e7"}, + {file = "rpds_py-2026.9.1-cp315-cp315-win32.whl", hash = "sha256:dbc2673f9223d420c91145599b3ba45a8a50c207d1976908e5fb5ddb0c9b9429"}, + {file = "rpds_py-2026.9.1-cp315-cp315-win_amd64.whl", hash = "sha256:75c38c50ab9aca840225d9a9a3810bf11d04bd5c1f186cabbb8aee56db3e9b15"}, + {file = "rpds_py-2026.9.1-cp315-cp315-win_arm64.whl", hash = "sha256:a431156bb41865fc14cd5d79bb9d7bbed83110b0159e34e62ae30951f96c0009"}, + {file = "rpds_py-2026.9.1-cp315-cp315t-macosx_10_12_x86_64.whl", hash = "sha256:ef0d8c843e2827d6c120ab4687e9423fb1d893db1df27b7c1506615bcb9734a0"}, + {file = "rpds_py-2026.9.1-cp315-cp315t-macosx_11_0_arm64.whl", hash = "sha256:45bc6bccf78b20fd834237d18db64965d7ee68ba7f60440a26c7ab71e7b8d51a"}, + {file = "rpds_py-2026.9.1-cp315-cp315t-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:1d55198263bb51f557550c6ed2e6d1cb6a6fed6eb5c9120b741c5926bef8a45d"}, + {file = "rpds_py-2026.9.1-cp315-cp315t-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:a8763f20692da7df39b0afdd1ba3042b004c50a45994f76c2d9a25641f7673db"}, + {file = "rpds_py-2026.9.1-cp315-cp315t-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:e43d4a1f673e8a1cbd8533e809e02b4bf9d4f2280269bb640436556312121250"}, + {file = "rpds_py-2026.9.1-cp315-cp315t-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:ea394a937f17a54c51239348bdbe2e3518124c8d4a8951ba04a311d3095bd18f"}, + {file = "rpds_py-2026.9.1-cp315-cp315t-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:cdeaa99ce822dca76cfb1b993e9120c5ea212f2eb66d48950ad63c349668a018"}, + {file = "rpds_py-2026.9.1-cp315-cp315t-manylinux_2_31_riscv64.whl", hash = "sha256:b4f062343e7ad3fa94f2c66e5ae667dee47ee74dd41a9057c4fbe163236a123d"}, + {file = "rpds_py-2026.9.1-cp315-cp315t-manylinux_2_5_i686.manylinux1_i686.whl", hash = "sha256:22ffd29a63d71fb1b81552c21f2c2b734949b7ac751a9be70675a939a900839b"}, + {file = "rpds_py-2026.9.1-cp315-cp315t-musllinux_1_2_aarch64.whl", hash = "sha256:08dae4a4095150a7c4545a1fb40b98e1ab1744fbc2770d92c977b9dadaa49ab6"}, + {file = "rpds_py-2026.9.1-cp315-cp315t-musllinux_1_2_i686.whl", hash = "sha256:9a0460d43603d1fd9ef59c30278531e15d78581721ddb538fa560aa7817ea4ad"}, + {file = "rpds_py-2026.9.1-cp315-cp315t-musllinux_1_2_x86_64.whl", hash = "sha256:1c2d1f6da5128eabf34e963d7163a818846075a52568250d006c4c953b40f903"}, + {file = "rpds_py-2026.9.1-cp315-cp315t-win32.whl", hash = "sha256:5c6ee90dee3e85e055ddfd502d611643d9b0fd94c818220bda84ec3dacd9b27b"}, + {file = "rpds_py-2026.9.1-cp315-cp315t-win_amd64.whl", hash = "sha256:fe5ad0664ec772b02c45859041aa17655709cced7a31005817fbbbd988c25567"}, + {file = "rpds_py-2026.9.1-pp311-pypy311_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:4c0d2cb595a420b34d5086db0add011e26e2c09d6a024afbac4228bf8f863a30"}, + {file = "rpds_py-2026.9.1-pp311-pypy311_pp73-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:f3d6ed6a98cfd19155996605474982cc470d7601746a6439078f1a5a3fa8b050"}, + {file = "rpds_py-2026.9.1-pp311-pypy311_pp73-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:8171b44a054e5c67fd748ada04187f1250bf35b95f85e52ab64bcf3331a923bb"}, + {file = "rpds_py-2026.9.1-pp311-pypy311_pp73-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:fda1d96e542c37b6c804547dbf489c129fe7c97183a76a5ec275909ba1a063df"}, + {file = "rpds_py-2026.9.1-pp311-pypy311_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:270bdcdaac5d5b6f73c5e22e7e135c7f2a50e789f71d9e241d5be8d90026e19a"}, + {file = "rpds_py-2026.9.1-pp311-pypy311_pp73-manylinux_2_31_riscv64.whl", hash = "sha256:84a6ecc0c940169190d2c23bd969debd48c94dbc855acd60188a68d71d421608"}, + {file = "rpds_py-2026.9.1-pp311-pypy311_pp73-manylinux_2_5_i686.manylinux1_i686.whl", hash = "sha256:ab4b2fda7c2b542f7f9d886cc6a838c5079d2b76f72e6081411faba11adde2c9"}, + {file = "rpds_py-2026.9.1-pp311-pypy311_pp73-musllinux_1_2_aarch64.whl", hash = "sha256:44b32a7c4f0da3d28af31c259e38ddcff096f855e205ed0671d02fcf44f1ea1c"}, + {file = "rpds_py-2026.9.1-pp311-pypy311_pp73-musllinux_1_2_i686.whl", hash = "sha256:a3dbc5ed9514908d5046107d7b1346bde71eea61de6e0e4919c19354f97e769f"}, + {file = "rpds_py-2026.9.1-pp311-pypy311_pp73-musllinux_1_2_x86_64.whl", hash = "sha256:6eae33003518fd4cb4f83a218d5371469dd3001aa3b87128c005b07762f7fe5e"}, + {file = "rpds_py-2026.9.1-pp311-pypy311_pp80-macosx_10_12_x86_64.whl", hash = "sha256:0483515261947e4e8b8e1375bf7463e7eb6ccfb3d86e7b554d90cd5285f20f32"}, + {file = "rpds_py-2026.9.1-pp311-pypy311_pp80-macosx_11_0_arm64.whl", hash = "sha256:4cfaf02209061880210819934de2f4f6aa83dc04dafe6770276acc240a56da31"}, + {file = "rpds_py-2026.9.1-pp312-pypy312_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:6cdc537c8633d7fd92a82e2e0d2ab74320a3f63d5e59fb9cf08711e08fe151c4"}, + {file = "rpds_py-2026.9.1-pp312-pypy312_pp73-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:10e208f2425d973938afcd56e28a7c4be32e27b6a60b5d381f49fb9d8acf9759"}, + {file = "rpds_py-2026.9.1-pp312-pypy312_pp73-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:6c0dbbcc19735fe5f8b0a54c07659d154a9e69f47e15d0a6ab7299215daf62cb"}, + {file = "rpds_py-2026.9.1-pp312-pypy312_pp73-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:684fd492fff4fead00587544e059be2bbcb6f93454f21fa2a91b66fc7508be82"}, + {file = "rpds_py-2026.9.1-pp312-pypy312_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:d1028417bb44037eb3069c1009bd7b7277212876cda22fbe565b0bca9fab6d2c"}, + {file = "rpds_py-2026.9.1-pp312-pypy312_pp73-manylinux_2_31_riscv64.whl", hash = "sha256:492e5e428cbe126221611f47e068f01660352feec4ad18bc0f5ea9b2ae88fb14"}, + {file = "rpds_py-2026.9.1-pp312-pypy312_pp73-manylinux_2_5_i686.manylinux1_i686.whl", hash = "sha256:88b5268892fde430d5531f95bc560b6efbbd67c929662c586afd729a96e7461c"}, + {file = "rpds_py-2026.9.1-pp312-pypy312_pp73-musllinux_1_2_aarch64.whl", hash = "sha256:01445c8d194aa032a08e944f16567672da1c62dbdbefd8b6d0693032e290cf68"}, + {file = "rpds_py-2026.9.1-pp312-pypy312_pp73-musllinux_1_2_i686.whl", hash = "sha256:eef6a03b0b6d08d0835ccfa8ec8d1bc70525e3801387567137b50c557695e6da"}, + {file = "rpds_py-2026.9.1-pp312-pypy312_pp73-musllinux_1_2_x86_64.whl", hash = "sha256:6b9bf3135b4ad5981df9a73d71a35272d650a2985ae9c2746357b24d59de2448"}, + {file = "rpds_py-2026.9.1-pp312-pypy312_pp80-macosx_10_12_x86_64.whl", hash = "sha256:56c6952a9b15047466d0c2347c446a761d4527f89976156341e68f0ce5cc08b0"}, + {file = "rpds_py-2026.9.1-pp312-pypy312_pp80-macosx_11_0_arm64.whl", hash = "sha256:b242c27c8f836305a4a72df9cdd564386ac57b807bd252a063223331c9316b37"}, + {file = "rpds_py-2026.9.1.tar.gz", hash = "sha256:4793ef7f78268b124b73fa933440f01d258bbae01de9fa53e9080c9ab0425a12"}, +] + +[[package]] +name = "ruamel-yaml" +version = "0.18.17" +description = "ruamel.yaml is a YAML parser/emitter that supports roundtrip preservation of comments, seq/map flow style, and map key order" +optional = false +python-versions = ">=3.9" +groups = ["main", "dev"] +files = [ + {file = "ruamel_yaml-0.18.17-py3-none-any.whl", hash = "sha256:9c8ba9eb3e793efdf924b60d521820869d5bf0cb9c6f1b82d82de8295e290b9d"}, + {file = "ruamel_yaml-0.18.17.tar.gz", hash = "sha256:9091cd6e2d93a3a4b157ddb8fabf348c3de7f1fb1381346d985b6b247dcd8d3c"}, +] + +[package.dependencies] +"ruamel.yaml.clib" = {version = ">=0.2.15", markers = "platform_python_implementation == \"CPython\" and python_version < \"3.15\""} + +[package.extras] +docs = ["mercurial (>5.7)", "ryd"] +jinja2 = ["ruamel.yaml.jinja2 (>=0.2)"] + +[[package]] +name = "ruamel-yaml-clib" +version = "0.2.15" +description = "C version of reader, parser and emitter for ruamel.yaml derived from libyaml" +optional = false +python-versions = ">=3.9" +groups = ["main", "dev"] +markers = "python_version < \"3.15\" and platform_python_implementation == \"CPython\"" +files = [ + {file = "ruamel_yaml_clib-0.2.15-cp310-cp310-macosx_10_9_x86_64.whl", hash = "sha256:88eea8baf72f0ccf232c22124d122a7f26e8a24110a0273d9bcddcb0f7e1fa03"}, + {file = "ruamel_yaml_clib-0.2.15-cp310-cp310-macosx_11_0_arm64.whl", hash = "sha256:9b6f7d74d094d1f3a4e157278da97752f16ee230080ae331fcc219056ca54f77"}, + {file = "ruamel_yaml_clib-0.2.15-cp310-cp310-manylinux1_i686.manylinux_2_28_i686.manylinux_2_5_i686.whl", hash = "sha256:4be366220090d7c3424ac2b71c90d1044ea34fca8c0b88f250064fd06087e614"}, + {file = "ruamel_yaml_clib-0.2.15-cp310-cp310-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:1f66f600833af58bea694d5892453f2270695b92200280ee8c625ec5a477eed3"}, + {file = "ruamel_yaml_clib-0.2.15-cp310-cp310-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:da3d6adadcf55a93c214d23941aef4abfd45652110aed6580e814152f385b862"}, + {file = "ruamel_yaml_clib-0.2.15-cp310-cp310-musllinux_1_2_aarch64.whl", hash = "sha256:e9fde97ecb7bb9c41261c2ce0da10323e9227555c674989f8d9eb7572fc2098d"}, + {file = "ruamel_yaml_clib-0.2.15-cp310-cp310-musllinux_1_2_i686.whl", hash = "sha256:05c70f7f86be6f7bee53794d80050a28ae7e13e4a0087c1839dcdefd68eb36b6"}, + {file = "ruamel_yaml_clib-0.2.15-cp310-cp310-musllinux_1_2_x86_64.whl", hash = "sha256:6f1d38cbe622039d111b69e9ca945e7e3efebb30ba998867908773183357f3ed"}, + {file = "ruamel_yaml_clib-0.2.15-cp310-cp310-win32.whl", hash = "sha256:fe239bdfdae2302e93bd6e8264bd9b71290218fff7084a9db250b55caaccf43f"}, + {file = "ruamel_yaml_clib-0.2.15-cp310-cp310-win_amd64.whl", hash = "sha256:468858e5cbde0198337e6a2a78eda8c3fb148bdf4c6498eaf4bc9ba3f8e780bd"}, + {file = "ruamel_yaml_clib-0.2.15-cp311-cp311-macosx_10_9_x86_64.whl", hash = "sha256:c583229f336682b7212a43d2fa32c30e643d3076178fb9f7a6a14dde85a2d8bd"}, + {file = "ruamel_yaml_clib-0.2.15-cp311-cp311-macosx_11_0_arm64.whl", hash = "sha256:56ea19c157ed8c74b6be51b5fa1c3aff6e289a041575f0556f66e5fb848bb137"}, + {file = "ruamel_yaml_clib-0.2.15-cp311-cp311-manylinux1_i686.manylinux_2_28_i686.manylinux_2_5_i686.whl", hash = "sha256:5fea0932358e18293407feb921d4f4457db837b67ec1837f87074667449f9401"}, + {file = "ruamel_yaml_clib-0.2.15-cp311-cp311-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:ef71831bd61fbdb7aa0399d5c4da06bea37107ab5c79ff884cc07f2450910262"}, + {file = "ruamel_yaml_clib-0.2.15-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:617d35dc765715fa86f8c3ccdae1e4229055832c452d4ec20856136acc75053f"}, + {file = "ruamel_yaml_clib-0.2.15-cp311-cp311-musllinux_1_2_aarch64.whl", hash = "sha256:1b45498cc81a4724a2d42273d6cfc243c0547ad7c6b87b4f774cb7bcc131c98d"}, + {file = "ruamel_yaml_clib-0.2.15-cp311-cp311-musllinux_1_2_i686.whl", hash = "sha256:def5663361f6771b18646620fca12968aae730132e104688766cf8a3b1d65922"}, + {file = "ruamel_yaml_clib-0.2.15-cp311-cp311-musllinux_1_2_x86_64.whl", hash = "sha256:014181cdec565c8745b7cbc4de3bf2cc8ced05183d986e6d1200168e5bb59490"}, + {file = "ruamel_yaml_clib-0.2.15-cp311-cp311-win32.whl", hash = "sha256:d290eda8f6ada19e1771b54e5706b8f9807e6bb08e873900d5ba114ced13e02c"}, + {file = "ruamel_yaml_clib-0.2.15-cp311-cp311-win_amd64.whl", hash = "sha256:bdc06ad71173b915167702f55d0f3f027fc61abd975bd308a0968c02db4a4c3e"}, + {file = "ruamel_yaml_clib-0.2.15-cp312-cp312-macosx_10_13_x86_64.whl", hash = "sha256:cb15a2e2a90c8475df45c0949793af1ff413acfb0a716b8b94e488ea95ce7cff"}, + {file = "ruamel_yaml_clib-0.2.15-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:64da03cbe93c1e91af133f5bec37fd24d0d4ba2418eaf970d7166b0a26a148a2"}, + {file = "ruamel_yaml_clib-0.2.15-cp312-cp312-manylinux1_i686.manylinux_2_28_i686.manylinux_2_5_i686.whl", hash = "sha256:f6d3655e95a80325b84c4e14c080b2470fe4f33b6846f288379ce36154993fb1"}, + {file = "ruamel_yaml_clib-0.2.15-cp312-cp312-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:71845d377c7a47afc6592aacfea738cc8a7e876d586dfba814501d8c53c1ba60"}, + {file = "ruamel_yaml_clib-0.2.15-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:11e5499db1ccbc7f4b41f0565e4f799d863ea720e01d3e99fa0b7b5fcd7802c9"}, + {file = "ruamel_yaml_clib-0.2.15-cp312-cp312-musllinux_1_2_aarch64.whl", hash = "sha256:4b293a37dc97e2b1e8a1aec62792d1e52027087c8eea4fc7b5abd2bdafdd6642"}, + {file = "ruamel_yaml_clib-0.2.15-cp312-cp312-musllinux_1_2_i686.whl", hash = "sha256:512571ad41bba04eac7268fe33f7f4742210ca26a81fe0c75357fa682636c690"}, + {file = "ruamel_yaml_clib-0.2.15-cp312-cp312-musllinux_1_2_x86_64.whl", hash = "sha256:e5e9f630c73a490b758bf14d859a39f375e6999aea5ddd2e2e9da89b9953486a"}, + {file = "ruamel_yaml_clib-0.2.15-cp312-cp312-win32.whl", hash = "sha256:f4421ab780c37210a07d138e56dd4b51f8642187cdfb433eb687fe8c11de0144"}, + {file = "ruamel_yaml_clib-0.2.15-cp312-cp312-win_amd64.whl", hash = "sha256:2b216904750889133d9222b7b873c199d48ecbb12912aca78970f84a5aa1a4bc"}, + {file = "ruamel_yaml_clib-0.2.15-cp313-cp313-macosx_10_13_x86_64.whl", hash = "sha256:4dcec721fddbb62e60c2801ba08c87010bd6b700054a09998c4d09c08147b8fb"}, + {file = "ruamel_yaml_clib-0.2.15-cp313-cp313-macosx_11_0_arm64.whl", hash = "sha256:65f48245279f9bb301d1276f9679b82e4c080a1ae25e679f682ac62446fac471"}, + {file = "ruamel_yaml_clib-0.2.15-cp313-cp313-manylinux1_i686.manylinux_2_28_i686.manylinux_2_5_i686.whl", hash = "sha256:46895c17ead5e22bea5e576f1db7e41cb273e8d062c04a6a49013d9f60996c25"}, + {file = "ruamel_yaml_clib-0.2.15-cp313-cp313-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:3eb199178b08956e5be6288ee0b05b2fb0b5c1f309725ad25d9c6ea7e27f962a"}, + {file = "ruamel_yaml_clib-0.2.15-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:4d1032919280ebc04a80e4fb1e93f7a738129857eaec9448310e638c8bccefcf"}, + {file = "ruamel_yaml_clib-0.2.15-cp313-cp313-musllinux_1_2_aarch64.whl", hash = "sha256:ab0df0648d86a7ecbd9c632e8f8d6b21bb21b5fc9d9e095c796cacf32a728d2d"}, + {file = "ruamel_yaml_clib-0.2.15-cp313-cp313-musllinux_1_2_i686.whl", hash = "sha256:331fb180858dd8534f0e61aa243b944f25e73a4dae9962bd44c46d1761126bbf"}, + {file = "ruamel_yaml_clib-0.2.15-cp313-cp313-musllinux_1_2_x86_64.whl", hash = "sha256:fd4c928ddf6bce586285daa6d90680b9c291cfd045fc40aad34e445d57b1bf51"}, + {file = "ruamel_yaml_clib-0.2.15-cp313-cp313-win32.whl", hash = "sha256:bf0846d629e160223805db9fe8cc7aec16aaa11a07310c50c8c7164efa440aec"}, + {file = "ruamel_yaml_clib-0.2.15-cp313-cp313-win_amd64.whl", hash = "sha256:45702dfbea1420ba3450bb3dd9a80b33f0badd57539c6aac09f42584303e0db6"}, + {file = "ruamel_yaml_clib-0.2.15-cp314-cp314-macosx_10_15_x86_64.whl", hash = "sha256:753faf20b3a5906faf1fc50e4ddb8c074cb9b251e00b14c18b28492f933ac8ef"}, + {file = "ruamel_yaml_clib-0.2.15-cp314-cp314-macosx_11_0_arm64.whl", hash = "sha256:480894aee0b29752560a9de46c0e5f84a82602f2bc5c6cde8db9a345319acfdf"}, + {file = "ruamel_yaml_clib-0.2.15-cp314-cp314-manylinux1_i686.manylinux_2_28_i686.manylinux_2_5_i686.whl", hash = "sha256:4d3b58ab2454b4747442ac76fab66739c72b1e2bb9bd173d7694b9f9dbc9c000"}, + {file = "ruamel_yaml_clib-0.2.15-cp314-cp314-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:bfd309b316228acecfa30670c3887dcedf9b7a44ea39e2101e75d2654522acd4"}, + {file = "ruamel_yaml_clib-0.2.15-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:2812ff359ec1f30129b62372e5f22a52936fac13d5d21e70373dbca5d64bb97c"}, + {file = "ruamel_yaml_clib-0.2.15-cp314-cp314-musllinux_1_2_aarch64.whl", hash = "sha256:7e74ea87307303ba91073b63e67f2c667e93f05a8c63079ee5b7a5c8d0d7b043"}, + {file = "ruamel_yaml_clib-0.2.15-cp314-cp314-musllinux_1_2_i686.whl", hash = "sha256:713cd68af9dfbe0bb588e144a61aad8dcc00ef92a82d2e87183ca662d242f524"}, + {file = "ruamel_yaml_clib-0.2.15-cp314-cp314-musllinux_1_2_x86_64.whl", hash = "sha256:542d77b72786a35563f97069b9379ce762944e67055bea293480f7734b2c7e5e"}, + {file = "ruamel_yaml_clib-0.2.15-cp314-cp314-win32.whl", hash = "sha256:424ead8cef3939d690c4b5c85ef5b52155a231ff8b252961b6516ed7cf05f6aa"}, + {file = "ruamel_yaml_clib-0.2.15-cp314-cp314-win_amd64.whl", hash = "sha256:ac9b8d5fa4bb7fd2917ab5027f60d4234345fd366fe39aa711d5dca090aa1467"}, + {file = "ruamel_yaml_clib-0.2.15-cp39-cp39-macosx_10_9_x86_64.whl", hash = "sha256:923816815974425fbb1f1bf57e85eca6e14d8adc313c66db21c094927ad01815"}, + {file = "ruamel_yaml_clib-0.2.15-cp39-cp39-macosx_11_0_arm64.whl", hash = "sha256:dcc7f3162d3711fd5d52e2267e44636e3e566d1e5675a5f0b30e98f2c4af7974"}, + {file = "ruamel_yaml_clib-0.2.15-cp39-cp39-manylinux1_i686.manylinux_2_28_i686.manylinux_2_5_i686.whl", hash = "sha256:5d3c9210219cbc0f22706f19b154c9a798ff65a6beeafbf77fc9c057ec806f7d"}, + {file = "ruamel_yaml_clib-0.2.15-cp39-cp39-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:1bb7b728fd9f405aa00b4a0b17ba3f3b810d0ccc5f77f7373162e9b5f0ff75d5"}, + {file = "ruamel_yaml_clib-0.2.15-cp39-cp39-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:3cb75a3c14f1d6c3c2a94631e362802f70e83e20d1f2b2ef3026c05b415c4900"}, + {file = "ruamel_yaml_clib-0.2.15-cp39-cp39-musllinux_1_2_aarch64.whl", hash = "sha256:badd1d7283f3e5894779a6ea8944cc765138b96804496c91812b2829f70e18a7"}, + {file = "ruamel_yaml_clib-0.2.15-cp39-cp39-musllinux_1_2_i686.whl", hash = "sha256:0ba6604bbc3dfcef844631932d06a1a4dcac3fee904efccf582261948431628a"}, + {file = "ruamel_yaml_clib-0.2.15-cp39-cp39-musllinux_1_2_x86_64.whl", hash = "sha256:a8220fd4c6f98485e97aea65e1df76d4fed1678ede1fe1d0eed2957230d287c4"}, + {file = "ruamel_yaml_clib-0.2.15-cp39-cp39-win32.whl", hash = "sha256:04d21dc9c57d9608225da28285900762befbb0165ae48482c15d8d4989d4af14"}, + {file = "ruamel_yaml_clib-0.2.15-cp39-cp39-win_amd64.whl", hash = "sha256:27dc656e84396e6d687f97c6e65fb284d100483628f02d95464fd731743a4afe"}, + {file = "ruamel_yaml_clib-0.2.15.tar.gz", hash = "sha256:46e4cc8c43ef6a94885f72512094e482114a8a706d3c555a34ed4b0d20200600"}, +] + +[[package]] +name = "ruff" +version = "0.17.3" +description = "An extremely fast Python linter and code formatter, written in Rust." +optional = false +python-versions = ">=3.7" +groups = ["dev"] +files = [ + {file = "ruff-0.17.3-py3-none-linux_armv6l.whl", hash = "sha256:ab86b84cd9d05e56d329742c0cabdcbe2d1d56c98f4a04aab9b5b66196febfb4"}, + {file = "ruff-0.17.3-py3-none-macosx_10_12_x86_64.whl", hash = "sha256:e407b4a6852853a9a2cdce96963e94ad9775578bb44e7561a0f2829f3cc04b11"}, + {file = "ruff-0.17.3-py3-none-macosx_11_0_arm64.whl", hash = "sha256:b75cc113b3aca388933d8296b667b1ac401a80317ea3074c9b0a66541199da29"}, + {file = "ruff-0.17.3-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:ab5fa7ed5ce18554ea69d850ad162cbaa0c85fc47c08d83a569f806681c9a6ad"}, + {file = "ruff-0.17.3-py3-none-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:c673c170e27834d1262e3a9c1e97bdc9b9c3b180c63d89173ce69747232474c1"}, + {file = "ruff-0.17.3-py3-none-manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:6a754c0341bc9ca151b7b335988b3263921c08859f84ca219b70cee825571b6f"}, + {file = "ruff-0.17.3-py3-none-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:4590b9d86c0b1543ebb09094254e191cd43fe8edaf330baa035aab03da251532"}, + {file = "ruff-0.17.3-py3-none-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:bbde18beccd98c29a227c63c99022e3443b99258e08326df6425dfe0443705ef"}, + {file = "ruff-0.17.3-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:7fa4b0f931cdbcb8a52044f370a3eb429fb42eb88807b0e800fcaa98dd42b24e"}, + {file = "ruff-0.17.3-py3-none-manylinux_2_31_riscv64.whl", hash = "sha256:39405f341deec934b4289634324ad695f72df2970f9121582872eaf5b722f0f5"}, + {file = "ruff-0.17.3-py3-none-musllinux_1_2_aarch64.whl", hash = "sha256:68074094f55ca3d29ab19491f640282f7a84061c4c7977aa5094b968ca457e9a"}, + {file = "ruff-0.17.3-py3-none-musllinux_1_2_armv7l.whl", hash = "sha256:27bb571ac78fef178fe5118d2827df95c2aae74a48fff2b15f6acd03af5ff5bb"}, + {file = "ruff-0.17.3-py3-none-musllinux_1_2_i686.whl", hash = "sha256:029c37cbd3de48ca37aa12117bead2d065f15da17d66bf84e0d4274013705db2"}, + {file = "ruff-0.17.3-py3-none-musllinux_1_2_x86_64.whl", hash = "sha256:692fdb46d0fd9c71e380947dc5fc72f2c3f881073bc1f739e4579376231affbe"}, + {file = "ruff-0.17.3-py3-none-win32.whl", hash = "sha256:46539cb031b01fd29ae0a02142483c46640b233ee12bb1c1b690bffa1555d5ca"}, + {file = "ruff-0.17.3-py3-none-win_amd64.whl", hash = "sha256:56b8ffb3f3b3cbccb8b76b71b38261b7ed982ef5d20f5b447b1b61a7f4949b24"}, + {file = "ruff-0.17.3-py3-none-win_arm64.whl", hash = "sha256:81fce74bcd6be0e9da7c67c7553fd0f1f12964a2052cd728ef917b937a5ca1d9"}, + {file = "ruff-0.17.3.tar.gz", hash = "sha256:a57049c8addb3662073859bb55a7ad35aa54f8a7c9ed268911d1333c5aadaaa9"}, +] + +[[package]] +name = "subprocess-tee" +version = "0.4.2" +description = "subprocess-tee" +optional = false +python-versions = ">=3.8" +groups = ["dev"] +files = [ + {file = "subprocess_tee-0.4.2-py3-none-any.whl", hash = "sha256:21942e976715af4a19a526918adb03a8a27a8edab959f2d075b777e3d78f532d"}, + {file = "subprocess_tee-0.4.2.tar.gz", hash = "sha256:91b2b4da3aae9a7088d84acaf2ea0abee3f4fd9c0d2eae69a9b9122a71476590"}, +] + +[package.extras] +docs = ["argparse-manpage", "cairosvg", "markdown-include", "mkdocs", "mkdocs-git-revision-date-localized-plugin", "mkdocs-material", "mkdocs-material-extensions", "mkdocstrings", "mkdocstrings-python", "pillow", "pymdown-extensions"] +test = ["enrich (>=1.2.6)", "molecule (>=3.4.0)", "pytest (>=6.2.5)", "pytest-cov (>=2.12.1)", "pytest-plus (>=0.2)", "pytest-xdist (>=2.3.0)"] + +[[package]] +name = "typing-extensions" +version = "4.16.0" +description = "Backported and Experimental Type Hints for Python 3.9+" +optional = false +python-versions = ">=3.9" +groups = ["dev"] +files = [ + {file = "typing_extensions-4.16.0-py3-none-any.whl", hash = "sha256:481caa481374e813c1b176ada14e97f1f67a4539ce9cfeb3f350d78d6370c2e8"}, + {file = "typing_extensions-4.16.0.tar.gz", hash = "sha256:dc983d19a509c94dba722ee6abd33940f7c05a89e243c47e907eb4db6f1a43e5"}, +] + +[[package]] +name = "virtualenv" +version = "21.15.6" +description = "Virtual Python Environment builder" +optional = false +python-versions = ">=3.9" +groups = ["dev"] +files = [ + {file = "virtualenv-21.15.6-py3-none-any.whl", hash = "sha256:4b99ab9b95ff11043939bb4cad2fa0449175fa5a478372864f62160cb6c1a7ed"}, + {file = "virtualenv-21.15.6.tar.gz", hash = "sha256:c8b65c7d206ca6ef18de9352c160b3f04f66fff9f6b0ec8fd31f14688eba52c4"}, +] + +[package.dependencies] +distlib = ">=0.4.3,<1" +filelock = {version = ">=3.24.2,<5", markers = "python_version >= \"3.10\""} +packaging = ">=26.3" +platformdirs = ">=4.4,<5" +python-discovery = ">=1.6.1" + +[[package]] +name = "wcmatch" +version = "11.2" +description = "Wildcard/glob file name matcher." +optional = false +python-versions = ">=3.11" +groups = ["dev"] +files = [ + {file = "wcmatch-11.2-py3-none-any.whl", hash = "sha256:952bab83c75c233836007a69550a3a8753cf06964c1eb3b84ff11ad5bff1c272"}, + {file = "wcmatch-11.2.tar.gz", hash = "sha256:1605bde546fbde4c063628c7d89eda486909fd4337e51d73a873987fbcb2554c"}, +] + +[package.dependencies] +bracex = ">=3.2" + +[[package]] +name = "yamllint" +version = "1.38.0" +description = "A linter for YAML files." +optional = false +python-versions = ">=3.10" +groups = ["dev"] +files = [ + {file = "yamllint-1.38.0-py3-none-any.whl", hash = "sha256:fc394a5b3be980a4062607b8fdddc0843f4fa394152b6da21722f5d59013c220"}, + {file = "yamllint-1.38.0.tar.gz", hash = "sha256:09e5f29531daab93366bb061e76019d5e91691ef0a40328f04c927387d1d364d"}, +] + +[package.dependencies] +pathspec = ">=1.0.0" +pyyaml = "*" + +[package.extras] +dev = ["doc8", "flake8", "flake8-import-order", "rstcheck[sphinx]", "ruff", "sphinx"] + +[metadata] +lock-version = "2.1" +python-versions = ">=3.12,<4.0" +content-hash = "564d89b4c924865a441880300c9af5c856be4d27691f8906ca0381fc6e5dce8c" diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..8c68d1e --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,72 @@ +[project] +name = "compose2role" +version = "0.1.0" +description = "Generate Ansible roles from Docker Compose projects" +authors = [ + { name = "Marco D'Aleo", email = "marco@marcodaleo.com" } +] +license = "GPL-3.0-or-later" +readme = "README.md" +requires-python = ">=3.12,<4.0" +dependencies = [ + "click>=8.1.8,<9.0", + "jinja2>=3.1.6,<4.0", + "ruamel.yaml>=0.18.16,<0.19", +] +keywords = [ + "ansible", + "docker", + "docker-compose", + "sops", + "ssh", + "cli", + "sysadmin", +] +classifiers = [ + "Environment :: Console", + "Intended Audience :: System Administrators", + "Operating System :: POSIX :: Linux", + "Programming Language :: Python :: 3", + "Programming Language :: Python :: 3.12", + "Programming Language :: Python :: 3.13", + "Programming Language :: Python :: 3.14", + "Topic :: System :: Systems Administration", +] + +[project.urls] +Homepage = "https://git.sysmd.uk/mdaleo404/compose2role" +Repository = "https://git.sysmd.uk/mdaleo404/compose2role" + +[project.scripts] +compose2role = "compose2role.cli:main" + +[build-system] +requires = ["poetry-core>=2.0.0,<3.0.0"] +build-backend = "poetry.core.masonry.api" + +[tool.poetry] +packages = [{ include = "compose2role", from = "src" }] + +[dependency-groups] +dev = [ + "ansible-lint>=26.10.1,<27.0", + "mypy>=1.15,<2.0", + "pytest>=8.3,<9.0", + "ruff>=0.11,<1.0", + "pre-commit (>=4.6,<5.0)", +] + +[tool.pytest.ini_options] +testpaths = ["tests"] + +[tool.ruff] +target-version = "py312" +line-length = 100 + +[tool.ruff.lint] +select = ["E", "F", "I", "UP"] + +[tool.mypy] +files = ["src"] +python_version = "3.12" +strict = true diff --git a/src/compose2role/__init__.py b/src/compose2role/__init__.py new file mode 100644 index 0000000..a37cc88 --- /dev/null +++ b/src/compose2role/__init__.py @@ -0,0 +1 @@ +"""Compose projects to Ansible roles.""" diff --git a/src/compose2role/cli.py b/src/compose2role/cli.py new file mode 100644 index 0000000..e9cd0f4 --- /dev/null +++ b/src/compose2role/cli.py @@ -0,0 +1,775 @@ +"""The compose2role command-line interface.""" + +from __future__ import annotations + +import json +import os +import sys +from dataclasses import replace +from difflib import unified_diff +from pathlib import Path + +import click + +from compose2role.config import HostProfile, load_repository_config +from compose2role.discover import discover_projects, validate_docker_context +from compose2role.doctor import DoctorCheck, DoctorReport, diagnose, target_for +from compose2role.drift import inspect_drift +from compose2role.emit import external_prerequisites, normalize_remote_role_name +from compose2role.errors import ( + Compose2RoleError, + ConfigurationError, + DockerContextError, + GenerationError, +) +from compose2role.findings import Finding, Severity +from compose2role.layout import AnsibleLayout, discover_layout +from compose2role.remote import resolve_remote_projects +from compose2role.remote_generate import generate_remote_projects +from compose2role.resolve import ( + AuditOutcome, + AuditResult, + GeneratedProject, + GenerationOutcome, + GenerationResult, + PlanDiff, + ProjectDrift, + ProjectFailure, + ResolvedProject, +) +from compose2role.secret_policy import SecretPolicy, load_secret_policy + + +class _Command(click.Command): + """Click command that lists its full short help instead of an ellipsis.""" + + def get_short_help_str(self, limit: int = 45) -> str: + help_text = self.help or "" + paragraph_end = help_text.find("\n\n") + if paragraph_end != -1: + help_text = help_text[:paragraph_end] + return " ".join(help_text.split()) + + +class _Group(click.Group): + """Click group that renders every command description without truncation.""" + + command_class = _Command + + +@click.group(cls=_Group, context_settings={"max_content_width": 100}) +@click.version_option(package_name="compose2role", message="%(prog)s %(version)s") +def main() -> None: + """Generate Ansible roles that reproduce remote Docker Compose projects. + + Captures a project over SSH and writes a role that restores its exact Compose files, + dotenv inputs, and file-backed secrets, then applies them with docker compose. Remote-only: + generate and audit require --host plus exactly one of --project or --all. + """ + + +def _color_enabled() -> bool: + return sys.stdout.isatty() and "NO_COLOR" not in os.environ + + +class _ConfigurationClickError(click.ClickException): + """Report configuration and layout failures as usage errors.""" + + exit_code = 2 + + +def _layout( + config_path: Path | None, + *, + explicit_output: Path | None = None, + explicit_sops_config: Path | None = None, + explicit_secret_policy: Path | None = None, + host: str | None = None, +) -> AnsibleLayout: + config = load_repository_config(config_path, start=Path.cwd()) + return discover_layout( + config, + start=Path.cwd(), + explicit_output=explicit_output, + explicit_sops_config=explicit_sops_config, + explicit_secret_policy=explicit_secret_policy, + host=host, + ) + + +def _configured_sudo(explicit: bool | None, profile: HostProfile) -> bool: + if explicit is not None: + return explicit + return bool(profile.sudo) + + +def _docker_context(explicit: str | None, profile: HostProfile) -> str | None: + selected = explicit if explicit is not None else profile.docker_context + if selected is None: + return None + try: + return validate_docker_context(selected) + except DockerContextError as error: + raise _ConfigurationClickError(str(error)) from error + + +def _status(ok: bool, message: str) -> None: + glyph = "✓" if ok else "✗" + click.secho(f"{glyph} {message}", fg="green" if ok else "red", color=_color_enabled()) + + +def _visible_findings(findings: tuple[Finding, ...], verbose: bool) -> tuple[Finding, ...]: + if verbose: + return findings + return tuple(finding for finding in findings if finding.severity == Severity.ERROR) + + +def _echo_findings(findings: tuple[Finding, ...], verbose: bool, *, indent: str = "") -> None: + for finding in _visible_findings(findings, verbose): + click.echo( + f"{indent}{finding.location}: {finding.severity.upper()} " + f"{finding.id}: {finding.message}" + ) + + +def _echo_summary(ok_count: int, noun: str, failed_count: int) -> None: + summary = f"{ok_count} {noun}" + if failed_count: + summary = f"{summary}, {failed_count} failed" + click.echo() + click.echo(summary) + + +@main.command() +@click.option( + "--output", + "output_directory", + "-o", + type=click.Path(path_type=Path), + help="Roles directory to write into (default: resolved from --config or ansible.cfg).", +) +@click.option( + "--config", + "config_path", + type=click.Path(path_type=Path), + help="Repository configuration file to load.", +) +@click.option( + "--check", + "check_only", + is_flag=True, + help="List the planned files without writing or encrypting anything.", +) +@click.option( + "--diff", + "diff_only", + is_flag=True, + help="Show value-free changes; write only together with --force.", +) +@click.option( + "--force", + "force", + is_flag=True, + help="Replace a differing role after backing it up under .compose2role/backups/.", +) +@click.option( + "--host", + help="Remote OpenSSH alias or host:port destination (required).", +) +@click.option( + "--sudo", + "use_sudo", + is_flag=True, + default=None, + help="Run remote commands with sudo -n.", +) +@click.option( + "--docker-context", + "docker_context", + help="Remote Docker context to use (default: the remote CLI default).", +) +@click.option( + "--project", + "project_name", + help="Raw Compose project name to generate (exactly one project).", +) +@click.option( + "--all", + "all_projects", + is_flag=True, + help="Generate every discovered project.", +) +@click.option( + "--secret-policy", + "secret_policy_path", + type=click.Path(path_type=Path), + help="TOML secret-classification overrides.", +) +@click.option( + "--sops-config", + "sops_config_path", + type=click.Path(path_type=Path), + help="SOPS creation-rule file for encrypting secrets (or COMPOSE2ROLE_SOPS_CONFIG).", +) +@click.option( + "--verbose", + "-v", + is_flag=True, + help="Also show warning and info findings.", +) +@click.option( + "--format", + "output_format", + type=click.Choice(("text", "json")), + default="text", + help="Output format (default: text).", +) +def generate( + output_directory: Path | None, + config_path: Path | None, + check_only: bool, + diff_only: bool, + force: bool, + host: str | None, + use_sudo: bool | None, + docker_context: str | None, + project_name: str | None, + all_projects: bool, + secret_policy_path: Path | None, + sops_config_path: Path | None, + verbose: bool, + output_format: str, +) -> None: + """Generate Ansible roles from remote Compose projects. + + Requires --host and exactly one of --project or --all. Every run, including --check, needs an + existing SOPS configuration and the sops executable. A byte-identical role is left alone; a + differing or unmanaged role is refused unless --force is given. + """ + if host is None: + raise click.UsageError( + "local generation was removed; use --host with exactly one of --project or --all" + ) + if check_only and diff_only: + raise click.UsageError("--check and --diff are mutually exclusive") + if project_name is None and not all_projects: + raise click.UsageError("remote generate requires exactly one of --project or --all") + if project_name is not None and all_projects: + raise click.UsageError("remote generate accepts only one of --project or --all") + + text_output = output_format == "text" + if text_output: + click.echo(f"Discovering projects on {host}...") + try: + layout = _layout( + config_path, + explicit_output=output_directory, + explicit_sops_config=sops_config_path, + explicit_secret_policy=secret_policy_path, + host=host, + ) + secret_policy = _load_secret_policy(layout.secret_policy) + result = generate_remote_projects( + host, + layout.roles_directory, + project_name=project_name, + all_projects=all_projects, + use_sudo=_configured_sudo(use_sudo, layout.host), + docker_context=_docker_context(docker_context, layout.host), + secret_policy=secret_policy, + sops_config_path=layout.sops_config, + check_only=check_only, + diff_only=diff_only, + force=force, + on_outcome=( + (lambda outcome: _echo_generation_outcome(outcome, verbose)) + if text_output + else None + ), + ) + except ConfigurationError as error: + raise _ConfigurationClickError(str(error)) from error + except Compose2RoleError as error: + raise click.ClickException(str(error)) from error + + if output_format == "json": + click.echo(json.dumps(result.as_json())) + else: + _echo_generation_summary(result) + if result.failures: + raise click.exceptions.Exit(1) + + +def _echo_plan_diff(diff: PlanDiff) -> None: + for file_diff in diff.files: + if file_diff.state == "unchanged": + continue + if file_diff.withheld: + reason = ( + f": {file_diff.withheld_reason}" + if file_diff.withheld_reason is not None + else "" + ) + click.echo( + f" {file_diff.path}: {file_diff.change} (content withheld{reason})" + ) + continue + old_lines = (file_diff.old_text or "").splitlines() + new_lines = (file_diff.new_text or "").splitlines() + from_file = "/dev/null" if file_diff.change == "added" else f"a/{file_diff.path}" + rendered = list( + unified_diff( + old_lines, + new_lines, + fromfile=from_file, + tofile=f"b/{file_diff.path}", + lineterm="", + ) + ) + for line in rendered: + click.echo(f" {line}") + if file_diff.change == "changed" and not any( + line.startswith(("+", "-")) for line in rendered + ): + click.echo(f" {file_diff.path}: changed") + + +def _echo_generation_outcome(outcome: GenerationOutcome, verbose: bool) -> None: + if isinstance(outcome, GeneratedProject): + if outcome.checked: + action = "checked" + elif outcome.diff is not None and outcome.diff_only: + action = "unchanged" if outcome.diff.identical else "diff" + elif outcome.unchanged: + action = "unchanged" + else: + action = "generated" + _status(True, f"{outcome.name} -> {outcome.role_name} ({action})") + _echo_prerequisites(outcome.project) + _echo_findings(outcome.project.findings, verbose, indent=" ") + if outcome.diff is not None: + _echo_plan_diff(outcome.diff) + if outcome.checked: + for path in outcome.files: + click.echo(f" {path}") + return + role = f" -> {outcome.role_name}" if outcome.role_name is not None else "" + _status(False, f"{outcome.name}{role}: FAILED ({outcome.category}): {outcome.message}") + _echo_findings(outcome.findings, verbose, indent=" ") + + +def _echo_prerequisites(project: ResolvedProject) -> None: + prerequisites = external_prerequisites(project.canonical_document) + if prerequisites: + rendered = ", ".join(f"external {kind} {name}" for kind, name in prerequisites) + click.echo(f" prerequisites: {rendered}") + + +def _echo_generation_summary(result: GenerationResult) -> None: + generated = tuple( + outcome for outcome in result.outcomes if isinstance(outcome, GeneratedProject) + ) + if any(outcome.checked for outcome in generated): + noun = "checked" + elif generated and all(outcome.diff is not None and outcome.diff_only for outcome in generated): + noun = "unchanged" if all(outcome.unchanged for outcome in generated) else "diff" + elif generated and all(outcome.unchanged for outcome in generated): + noun = "unchanged" + else: + noun = "generated" + _echo_summary(len(generated), noun, len(result.failures)) + + +@main.command() +@click.option( + "--format", + "output_format", + type=click.Choice(("text", "json")), + default="text", + help="Output format (default: text).", +) +@click.option( + "--fail-on", + type=click.Choice(("error", "warning")), + help="Exit 3 when a finding at this severity or above exists.", +) +@click.option( + "--verbose", + "-v", + is_flag=True, + help="Also show warning and info findings.", +) +@click.option( + "--host", + help="Remote OpenSSH alias or host:port destination (required).", +) +@click.option( + "--sudo", + "use_sudo", + is_flag=True, + default=None, + help="Run remote commands with sudo -n.", +) +@click.option( + "--docker-context", + "docker_context", + help="Remote Docker context to use (default: the remote CLI default).", +) +@click.option( + "--project", + "project_name", + help="Raw Compose project name to audit (exactly one project).", +) +@click.option( + "--all", + "all_projects", + is_flag=True, + help="Audit every discovered project.", +) +@click.option( + "--secret-policy", + "secret_policy_path", + type=click.Path(path_type=Path), + help="TOML secret-classification overrides.", +) +@click.option( + "--config", + "config_path", + type=click.Path(path_type=Path), + help="Repository configuration file to load.", +) +def audit( + output_format: str, + fail_on: str | None, + verbose: bool, + host: str | None, + use_sudo: bool | None, + docker_context: str | None, + project_name: str | None, + all_projects: bool, + secret_policy_path: Path | None, + config_path: Path | None, +) -> None: + """Audit remote Compose projects without writing anything. + + Uses the same pipeline as generate and compares the captured source with an existing role's + provenance. Requires --host and exactly one of --project or --all. Text output shows errors + only; add -v for more, or use --format json. + """ + if host is None: + raise click.UsageError( + "local audit was removed; use --host with exactly one of --project or --all" + ) + if project_name is None and not all_projects: + raise click.UsageError("remote audit requires exactly one of --project or --all") + if project_name is not None and all_projects: + raise click.UsageError("remote audit accepts only one of --project or --all") + + text_output = output_format == "text" + if text_output: + click.echo(f"Discovering projects on {host}...") + try: + layout = _layout(config_path, explicit_secret_policy=secret_policy_path, host=host) + secret_policy = _load_secret_policy(layout.secret_policy) + result = resolve_remote_projects( + host, + project_name=project_name, + all_projects=all_projects, + use_sudo=_configured_sudo(use_sudo, layout.host), + secret_policy=secret_policy, + docker_context=_docker_context(docker_context, layout.host), + on_outcome=( + ( + lambda outcome: _echo_audit_outcome( + _outcome_with_drift(outcome, layout.roles_directory), verbose + ) + ) + if text_output + else None + ), + ) + result = _audit_result_with_drift(result, layout.roles_directory) + except ConfigurationError as error: + raise _ConfigurationClickError(str(error)) from error + except Compose2RoleError as error: + raise click.ClickException(str(error)) from error + + if output_format == "json": + click.echo(json.dumps(result.as_json())) + else: + drifted = sum( + 1 + for project in result.projects + if project.drift is not None and project.drift.state == "source changed" + ) + noun = f"ok, {drifted} drift" if all_projects and drifted else "ok" + _echo_summary(len(result.projects), noun, len(result.failures)) + if result.failures: + raise click.exceptions.Exit(1) + findings = tuple(finding for project in result.projects for finding in project.findings) + if fail_on is not None and _has_failure(findings, fail_on): + raise click.exceptions.Exit(3) + + +def _load_secret_policy(secret_policy_path: Path | None) -> SecretPolicy | None: + selected_path = secret_policy_path + if selected_path is None: + environment_path = os.environ.get("COMPOSE2ROLE_SECRET_POLICY") + selected_path = Path(environment_path) if environment_path else None + return load_secret_policy(selected_path) if selected_path is not None else None + + +def _echo_audit_status(name: str, drift: ProjectDrift) -> None: + _status(True, f"{name} ({_drift_label(drift)})") + + +def _drift_label(drift: ProjectDrift) -> str: + if drift.changed: + return f"{drift.state}: {', '.join(drift.changed)}" + return drift.state + + +def _outcome_with_drift(outcome: AuditOutcome, roles_directory: Path) -> AuditOutcome: + if not isinstance(outcome, ResolvedProject): + return outcome + try: + role_name = normalize_remote_role_name(outcome.project.name) + except GenerationError: + drift = ProjectDrift( + "missing", + role=outcome.project.name, + reason="project name cannot be normalized into an Ansible role name", + ) + else: + drift = inspect_drift(outcome, roles_directory / role_name) + return replace(outcome, drift=drift) + + +def _audit_result_with_drift(result: AuditResult, roles_directory: Path) -> AuditResult: + outcomes = tuple( + _outcome_with_drift(outcome, roles_directory) for outcome in result.outcomes + ) + return AuditResult( + projects=tuple( + outcome for outcome in outcomes if isinstance(outcome, ResolvedProject) + ), + failures=tuple( + outcome for outcome in outcomes if isinstance(outcome, ProjectFailure) + ), + outcomes=outcomes, + ) + + +def _echo_audit_outcome(outcome: AuditOutcome, verbose: bool) -> None: + if isinstance(outcome, ResolvedProject): + if outcome.drift is not None: + _echo_audit_status(outcome.project.name, outcome.drift) + else: + _status(True, outcome.project.name) + _echo_findings(outcome.findings, verbose, indent=" ") + return + _status(False, f"{outcome.name}: FAILED ({outcome.category}): {outcome.message}") + + +def _has_failure(findings: tuple[Finding, ...], fail_on: str) -> bool: + failure_severities = { + "error": frozenset({Severity.ERROR}), + "warning": frozenset({Severity.ERROR, Severity.WARNING}), + } + return any(finding.severity in failure_severities[fail_on] for finding in findings) + + +@main.command() +@click.option( + "--host", + required=True, + help="Remote OpenSSH alias or host:port destination.", +) +@click.option( + "--sudo", + "use_sudo", + is_flag=True, + default=None, + help="Run the remote Docker command with sudo -n.", +) +@click.option( + "--docker-context", + "docker_context", + help="Remote Docker context to use (default: the remote CLI default).", +) +@click.option( + "--format", + "output_format", + type=click.Choice(("text", "table", "json")), + default="text", + help="Output format (table is an alias for text).", +) +@click.option( + "--config", + "config_path", + type=click.Path(path_type=Path), + help="Repository configuration file to load.", +) +def discover( + host: str, + use_sudo: bool | None, + docker_context: str | None, + output_format: str, + config_path: Path | None, +) -> None: + """List Compose projects running on a remote host. + + Prints raw project names, statuses, and config files; copy a raw name for use with --project. + Discovery only reads Docker metadata and never changes the host. + """ + if output_format in {"text", "table"}: + click.echo(f"Discovering projects on {host}...") + try: + layout = _layout(config_path, host=host) + projects = discover_projects( + host, + use_sudo=_configured_sudo(use_sudo, layout.host), + docker_context=_docker_context(docker_context, layout.host), + ) + except ConfigurationError as error: + raise _ConfigurationClickError(str(error)) from error + except Compose2RoleError as error: + raise click.ClickException(str(error)) from error + + if output_format == "json": + click.echo(json.dumps({"projects": [project.as_json() for project in projects]})) + return + + for project in projects: + _status(True, f"{project.name} {project.status} {', '.join(project.config_files)}") + _echo_summary(len(projects), "found", 0) + + +@main.command() +@click.option( + "--host", + help="Also check this remote host (the path discover takes: SSH, daemon, Compose).", +) +@click.option( + "--sudo", + "use_sudo", + is_flag=True, + default=None, + help="Run remote check commands with sudo -n (requires --host).", +) +@click.option( + "--docker-context", + "docker_context", + help="Remote Docker context to use (default: the remote CLI default).", +) +@click.option( + "--config", + "config_path", + type=click.Path(path_type=Path), + help="Repository configuration file to load.", +) +@click.option( + "--output", + "output_directory", + type=click.Path(path_type=Path), + help="Roles directory to inspect (default: resolved layout).", +) +@click.option( + "--secret-policy", + "secret_policy_path", + type=click.Path(path_type=Path), + help="TOML secret-classification overrides.", +) +@click.option( + "--sops-config", + "sops_config_path", + type=click.Path(path_type=Path), + help="SOPS creation-rule file to validate (or COMPOSE2ROLE_SOPS_CONFIG).", +) +@click.option( + "--format", + "output_format", + type=click.Choice(("text", "json")), + default="text", + help="Output format (default: text).", +) +def doctor( + host: str | None, + use_sudo: bool | None, + docker_context: str | None, + config_path: Path | None, + output_directory: Path | None, + secret_policy_path: Path | None, + sops_config_path: Path | None, + output_format: str, +) -> None: + """Check the toolchain, configuration, and generated roles. + + Without --host this checks the controller and the generated roles; with --host it checks only + the host path discover takes: SSH resolution, the selected daemon, and Docker Compose. Warnings + never fail the run, a failed required check exits 1, and a configuration error exits 2. + """ + if host is None and use_sudo: + raise click.UsageError("--sudo requires --host") + if host is None and docker_context is not None: + raise click.UsageError("--docker-context requires --host") + target = target_for(host) + configuration_failure = False + try: + layout = _layout( + config_path, + explicit_output=output_directory, + explicit_sops_config=sops_config_path, + explicit_secret_policy=secret_policy_path, + host=host, + ) + selected_context = _docker_context(docker_context, layout.host) + except ConfigurationError as error: + configuration_failure = True + report = DoctorReport( + target=target, + checks=( + DoctorCheck( + "config", + "error", + str(error), + hint="fix the configuration file or pass --config", + required=True, + ), + ), + ) + else: + report = diagnose( + layout, + host=host, + use_sudo=_configured_sudo(use_sudo, layout.host), + docker_context=selected_context, + environ=os.environ, + ) + if output_format == "json": + click.echo(json.dumps(report.as_json())) + else: + _echo_doctor_report(report) + if configuration_failure: + raise click.exceptions.Exit(2) + if report.failed: + raise click.exceptions.Exit(1) + + +def _echo_doctor_report(report: DoctorReport) -> None: + statuses = { + "ok": ("✓", "green"), + "warning": ("!", "yellow"), + "error": ("✗", "red"), + } + for check in report.checks: + glyph, color = statuses[check.status] + line = f"{glyph} {check.name}: {check.detail}" + if check.hint is not None: + line = f"{line} — {check.hint}" + click.secho(line, fg=color, color=_color_enabled()) + summary = report.summary() + click.echo() + click.echo( + f"{summary['ok']} ok, {summary['warnings']} warnings, {summary['errors']} errors" + ) diff --git a/src/compose2role/config.py b/src/compose2role/config.py new file mode 100644 index 0000000..a2e550f --- /dev/null +++ b/src/compose2role/config.py @@ -0,0 +1,206 @@ +"""Discover and validate the repository configuration file.""" + +from __future__ import annotations + +import os +import tomllib +from collections.abc import Mapping +from dataclasses import dataclass, field +from pathlib import Path + +from compose2role.errors import ConfigurationError + +CONFIG_FILENAME = ".compose2role.toml" +ANSIBLE_CONFIG_FILENAME = "ansible.cfg" + +_DEFAULT_KEYS = frozenset( + { + "output", + "sops_config", + "secret_policy", + } +) +_HOST_KEYS = frozenset({"docker_context", "sudo"}) + + +@dataclass(frozen=True) +class HostProfile: + """Per-host settings that override repository defaults.""" + + sudo: bool | None = None + docker_context: str | None = None + + +@dataclass(frozen=True) +class RepositoryConfig: + """A validated repository configuration rooted at a directory.""" + + root: Path + path: Path | None = None + output: Path | None = None + sops_config: Path | None = None + secret_policy: Path | None = None + hosts: Mapping[str, HostProfile] = field(default_factory=dict) + + def host(self, name: str | None) -> HostProfile: + """Return the selected host profile, or an empty profile.""" + if name is None: + return HostProfile() + return self.hosts.get(name, HostProfile()) + + +def find_repository_config(start: Path | None = None) -> Path | None: + """Find the nearest `.compose2role.toml`, then the nearest `ansible.cfg`.""" + for filename in (CONFIG_FILENAME, ANSIBLE_CONFIG_FILENAME): + found = find_file_upwards(start, filename) + if found is not None: + return found + return None + + +def find_file_upwards(start: Path | None, filename: str) -> Path | None: + """Find FILENAME in START or its nearest ancestor directory.""" + for directory in ancestor_directories(start): + candidate = directory / filename + if candidate.is_file(): + return candidate + return None + + +def ancestor_directories(start: Path | None = None) -> tuple[Path, ...]: + """Return START and each ancestor, treating a non-existent path as a directory.""" + anchor = (start if start is not None else Path.cwd()).expanduser().resolve() + if anchor.is_file(): + anchor = anchor.parent + directories: list[Path] = [] + current = anchor + while True: + directories.append(current) + if current == current.parent: + return tuple(directories) + current = current.parent + + +def load_repository_config( + explicit_path: Path | None = None, + *, + start: Path | None = None, + environ: Mapping[str, str] | None = None, +) -> RepositoryConfig: + """Load the selected configuration, falling back to a discovered config root.""" + environment = os.environ if environ is None else environ + selected_path = explicit_path + if selected_path is None: + environment_path = environment.get("COMPOSE2ROLE_CONFIG") + selected_path = Path(environment_path) if environment_path else None + if selected_path is None: + selected_path = find_repository_config(start) + if selected_path is None: + root = (start if start is not None else Path.cwd()).expanduser().resolve() + return RepositoryConfig(root=root) + + selected_path = selected_path.expanduser() + if not selected_path.is_file(): + raise ConfigurationError(f"configuration file is unreadable: {selected_path}") + if selected_path.name == ANSIBLE_CONFIG_FILENAME: + return RepositoryConfig(root=selected_path.parent.resolve(), path=selected_path) + return _load_toml(selected_path) + + +def _load_toml(path: Path) -> RepositoryConfig: + try: + content = path.read_text(encoding="utf-8") + except (OSError, UnicodeDecodeError) as error: + raise ConfigurationError(f"configuration file is unreadable: {path}") from error + try: + document: object = tomllib.loads(content) + except tomllib.TOMLDecodeError as error: + raise ConfigurationError(f"configuration file is not valid TOML: {path}") from error + + root = path.parent.resolve() + settings = _mapping(document, f"configuration file must be a table: {path}") + unknown = sorted(set(settings) - {"defaults", "hosts"}) + if unknown: + raise ConfigurationError(f"unknown configuration key in {path}: {unknown[0]}") + + defaults = _mapping( + settings.get("defaults", {}), f"configuration defaults must be a table: {path}" + ) + unknown_defaults = sorted(set(defaults) - _DEFAULT_KEYS) + if unknown_defaults: + raise ConfigurationError( + f"unknown configuration key in {path}: {unknown_defaults[0]}" + ) + + return RepositoryConfig( + root=root, + path=path, + output=_path_setting(defaults, "output", root, path), + sops_config=_path_setting(defaults, "sops_config", root, path), + secret_policy=_path_setting(defaults, "secret_policy", root, path), + hosts=_host_profiles(settings.get("hosts"), path), + ) + + +def _host_profiles(value: object, path: Path) -> Mapping[str, HostProfile]: + if value is None: + return {} + hosts = _mapping(value, f"configuration hosts must be a table: {path}") + profiles: dict[str, HostProfile] = {} + for name, host_value in hosts.items(): + settings = _mapping(host_value, f"host profile must be a table: {path}") + unknown = sorted(set(settings) - _HOST_KEYS) + if unknown: + raise ConfigurationError(f"unknown host key in {path}: {unknown[0]}") + profiles[name] = HostProfile( + sudo=_bool_setting(settings, "sudo", path), + docker_context=_string_setting(settings, "docker_context", path), + ) + return profiles + + +def _mapping(value: object, message: str) -> Mapping[str, object]: + if not isinstance(value, dict): + raise ConfigurationError(message) + result: dict[str, object] = {} + for key, item in value.items(): + if not isinstance(key, str): + raise ConfigurationError(message) + result[key] = item + return result + + +def _string_setting( + settings: Mapping[str, object], key: str, path: Path +) -> str | None: + value = settings.get(key) + if value is None: + return None + if not isinstance(value, str): + raise ConfigurationError(f"configuration key {key!r} must be a string: {path}") + return value + + +def _bool_setting(settings: Mapping[str, object], key: str, path: Path) -> bool | None: + value = settings.get(key) + if value is None: + return None + if not isinstance(value, bool): + raise ConfigurationError(f"configuration key {key!r} must be a boolean: {path}") + return value + + +def _path_setting( + settings: Mapping[str, object], key: str, root: Path, path: Path +) -> Path | None: + value = _string_setting(settings, key, path) + if value is None: + return None + return _resolve_path(value, root) + + +def _resolve_path(value: str, root: Path) -> Path: + path = Path(value).expanduser() + if path.is_absolute(): + return path + return root / path diff --git a/src/compose2role/discover.py b/src/compose2role/discover.py new file mode 100644 index 0000000..677740f --- /dev/null +++ b/src/compose2role/discover.py @@ -0,0 +1,277 @@ +"""Discover Compose projects on a remote Docker host through OpenSSH.""" + +from __future__ import annotations + +import json +import re +from dataclasses import dataclass, replace +from json import JSONDecodeError +from subprocess import TimeoutExpired, run + +from compose2role.errors import DockerContextError, RemoteCommandError + +_SSH_TIMEOUT_SECONDS = 30 +_DOCKER_CONTEXT_NAME = re.compile(r"[A-Za-z0-9][A-Za-z0-9_.-]*") +_WORKING_DIRECTORY_FORMAT = ( + "'{{.Label \"com.docker.compose.project\"}}\t" + "{{.Label \"com.docker.compose.project.working_dir\"}}'" +) + + +@dataclass(frozen=True) +class DiscoveredProject: + """The remote Compose project details Docker exposes without a filesystem scan.""" + + name: str + status: str + config_files: tuple[str, ...] + working_directories: tuple[str, ...] = () + + def as_json(self) -> dict[str, str | list[str]]: + """Return the stable JSON form emitted by the CLI.""" + return { + "name": self.name, + "status": self.status, + "config_files": list(self.config_files), + } + + +def discover_projects( + host: str, + *, + use_sudo: bool = False, + include_working_directories: bool = False, + docker_context: str | None = None, +) -> tuple[DiscoveredProject, ...]: + """Ask Docker Compose on HOST for its known project config files.""" + remote_command = docker_command( + ("compose", "ls", "--all", "--format", "json"), docker_context=docker_context + ) + if use_sudo: + remote_command = ("sudo", "-n", *remote_command) + command = ssh_command(host, remote_command) + try: + result = run( + command, + check=False, + capture_output=True, + text=True, + timeout=_SSH_TIMEOUT_SECONDS, + ) + except FileNotFoundError as error: + raise RemoteCommandError( + "OpenSSH client executable 'ssh' was not found", category="ssh-client" + ) from error + except TimeoutExpired as error: + raise RemoteCommandError( + f"SSH discovery timed out for host {host!r}", category="ssh-timeout" + ) from error + + if result.returncode != 0: + if result.returncode == 255: + raise RemoteCommandError( + _error_message( + f"SSH connection or remote command failed for host {host!r} (exit 255)", + result.stderr, + ), + category="ssh", + ) + if use_sudo: + raise RemoteCommandError( + _error_message( + f"remote Docker discovery failed for host {host!r}; " + "ensure the SSH user has passwordless sudo access to Docker", + result.stderr, + ), + category="remote-discovery", + ) + raise RemoteCommandError( + _error_message( + f"remote Docker discovery failed for host {host!r}; " + "the SSH user may not be able to reach the daemon: " + "join the docker group, configure passwordless sudo, " + "or name a Docker context with --docker-context", + result.stderr, + ), + category="remote-discovery", + ) + projects = _parse_projects(result.stdout) + if not include_working_directories: + return projects + working_directories = _discover_working_directories( + host, use_sudo=use_sudo, docker_context=docker_context + ) + return tuple( + replace(project, working_directories=working_directories.get(project.name, ())) + for project in projects + ) + + +def _discover_working_directories( + host: str, *, use_sudo: bool, docker_context: str | None +) -> dict[str, tuple[str, ...]]: + remote_command: tuple[str, ...] = docker_command( + ("ps", "--all", "--format", _WORKING_DIRECTORY_FORMAT), + docker_context=docker_context, + ) + if use_sudo: + remote_command = ("sudo", "-n", *remote_command) + try: + result = run( + ssh_command(host, remote_command), + check=False, + capture_output=True, + text=True, + timeout=_SSH_TIMEOUT_SECONDS, + ) + except (FileNotFoundError, TimeoutExpired): + return {} + if result.returncode != 0: + return {} + return _parse_working_directories(result.stdout) + + +def _parse_working_directories(output: str) -> dict[str, tuple[str, ...]]: + try: + json_payload: object = json.loads(output) + except JSONDecodeError: + json_payload = None + if isinstance(json_payload, list): + # A JSON listing cannot carry the tab-separated label format, so labels are unavailable. + return {} + directories: dict[str, set[str]] = {} + for line in output.splitlines(): + if not line: + continue + if line.count("\t") != 1: + raise RemoteCommandError( + "remote Docker returned invalid Compose project working-directory labels", + category="invalid-discovery", + ) + project, directory = line.split("\t") + if not project and not directory: + continue + if ( + not project + or not directory + or _has_control_characters(project) + or _has_control_characters(directory) + ): + raise RemoteCommandError( + "remote Docker returned invalid Compose project working-directory labels", + category="invalid-discovery", + ) + directories.setdefault(project, set()).add(directory) + return {project: tuple(sorted(values)) for project, values in directories.items()} + + +def validate_docker_context(context: str) -> str: + """Return CONTEXT when it is one safe argv word, otherwise reject it.""" + if not _DOCKER_CONTEXT_NAME.fullmatch(context): + raise DockerContextError( + f"invalid Docker context name: {context!r}; " + "use a letter or digit followed by letters, digits, '_', '.', or '-'" + ) + return context + + +def docker_command( + command: tuple[str, ...], *, docker_context: str | None +) -> tuple[str, ...]: + """Build a remote Docker argv, naming the context when one is selected.""" + if docker_context is None: + return ("docker", *command) + return ("docker", "--context", docker_context, *command) + + +def ssh_command(host: str, remote_command: tuple[str, ...]) -> list[str]: + """Build an OpenSSH argv without changing SSH's configured connection behavior.""" + destination, port = _parse_destination(host) + command = ["ssh"] + if port is not None: + command.extend(("-p", port)) + command.extend(("--", destination, *remote_command)) + return command + + +def _error_message(message: str, stderr: str) -> str: + detail = stderr.strip() + return f"{message}: {detail}" if detail else message + + +def _parse_projects(output: str) -> tuple[DiscoveredProject, ...]: + try: + payload: object = json.loads(output) + except JSONDecodeError as error: + raise RemoteCommandError( + "remote Docker Compose returned invalid JSON", category="invalid-json" + ) from error + + if not isinstance(payload, list): + raise RemoteCommandError( + "remote Docker Compose returned an invalid project list", category="invalid-discovery" + ) + + projects: list[DiscoveredProject] = [] + for item in payload: + if not isinstance(item, dict): + raise RemoteCommandError( + "remote Docker Compose returned an invalid project entry", + category="invalid-discovery", + ) + name: object = item.get("Name") + status: object = item.get("Status", "") + config_files: object = item.get("ConfigFiles", "") + if not isinstance(name, str) or not name or _has_control_characters(name): + raise RemoteCommandError( + "remote Docker Compose returned a project without a name", + category="invalid-discovery", + ) + if not isinstance(status, str) or _has_control_characters(status): + raise RemoteCommandError( + "remote Docker Compose returned an invalid project status", + category="invalid-discovery", + ) + projects.append( + DiscoveredProject( + name=name, + status=status, + config_files=_config_files(config_files), + ) + ) + return tuple(projects) + + +def _config_files(value: object) -> tuple[str, ...]: + if isinstance(value, str): + paths = tuple(path for path in value.split(",") if path) + elif isinstance(value, list) and all(isinstance(path, str) for path in value): + paths = tuple(value) + else: + raise RemoteCommandError( + "remote Docker Compose returned invalid config file paths", category="invalid-discovery" + ) + if any(_has_control_characters(path) for path in paths): + raise RemoteCommandError( + "remote Docker Compose returned invalid config file paths", category="invalid-discovery" + ) + return paths + + +def _has_control_characters(value: str) -> bool: + return any(ord(character) < 32 or ord(character) == 127 for character in value) + + +def _parse_destination(host: str) -> tuple[str, str | None]: + if not host: + raise RemoteCommandError("SSH host must not be empty") + if host.count(":") != 1: + return host, None + + destination, possible_port = host.rsplit(":", maxsplit=1) + if not possible_port.isascii() or not possible_port.isdecimal(): + return host, None + port_number = int(possible_port) + if not destination or not 1 <= port_number <= 65535: + raise RemoteCommandError(f"SSH port is invalid for host {host!r}") + return destination, possible_port diff --git a/src/compose2role/doctor.py b/src/compose2role/doctor.py new file mode 100644 index 0000000..ce13f48 --- /dev/null +++ b/src/compose2role/doctor.py @@ -0,0 +1,807 @@ +"""Preflight checks for tools, repository configuration, and generated roles.""" + +from __future__ import annotations + +import hashlib +import json +import os +import re +import shutil +from collections.abc import Mapping +from dataclasses import dataclass +from json import JSONDecodeError +from pathlib import Path, PurePosixPath +from stat import S_IMODE +from subprocess import CompletedProcess, TimeoutExpired, run +from typing import Literal + +from compose2role.discover import docker_command, ssh_command +from compose2role.errors import GenerationError, SecretPolicyError +from compose2role.layout import AnsibleLayout +from compose2role.remote_emit import ( + normalized_compose_hash, + normalized_dotenv_hash, + normalized_file_secret_hash, +) +from compose2role.secret_policy import load_secret_policy +from compose2role.sops import SOPS_TARGET, validate_sops_config +from compose2role.source import FileMetadata + +DoctorStatus = Literal["ok", "warning", "error"] +DistroFamily = Literal["debian", "rhel"] | None +RoleDriftState = Literal["unchanged", "source changed", "source unavailable", "invalid"] + +OS_RELEASE_PATH = Path("/etc/os-release") +_PROVENANCE_FILE = ".compose2role.json" +_BACKUP_DIRECTORY = Path(".compose2role") / "backups" +_REQUIRED_COLLECTIONS = ("community.docker", "community.sops") +_TIMEOUT_SECONDS = 30 +# Single-quoted because OpenSSH joins the remote argv with spaces and the remote shell re-splits it. +_DOCKER_INFO_FORMAT = "'{{json .}}'" + + +@dataclass(frozen=True) +class DoctorCheck: + """One value-free diagnostic result with an optional remediation hint.""" + + name: str + status: DoctorStatus + detail: str + hint: str | None = None + required: bool = False + + def as_json(self) -> dict[str, object]: + """Return the stable JSON form of one check.""" + payload: dict[str, object] = { + "detail": self.detail, + "name": self.name, + "required": self.required, + "status": self.status, + } + if self.hint is not None: + payload["hint"] = self.hint + return payload + + +@dataclass(frozen=True) +class DoctorReport: + """Every check for one selected mode and its exit-relevant summary.""" + + target: dict[str, str] + checks: tuple[DoctorCheck, ...] + + @property + def failed(self) -> bool: + """Return whether a check required for the selected mode failed.""" + return any(check.status == "error" and check.required for check in self.checks) + + def as_json(self) -> dict[str, object]: + """Return the value-free doctor envelope.""" + return { + "checks": [check.as_json() for check in self.checks], + "summary": self.summary(), + "target": dict(self.target), + } + + def summary(self) -> dict[str, object]: + """Return the status counts shared by text and JSON output.""" + return { + "errors": sum(1 for check in self.checks if check.status == "error"), + "failed": self.failed, + "ok": sum(1 for check in self.checks if check.status == "ok"), + "warnings": sum(1 for check in self.checks if check.status == "warning"), + } + + +def target_for(host: str | None) -> dict[str, str]: + """Return the selected mode's value-free JSON target.""" + if host is None: + return {"kind": "local"} + return {"kind": "remote", "host": host} + + +@dataclass(frozen=True) +class _Distro: + family: DistroFamily + + +def diagnose( + layout: AnsibleLayout, + *, + host: str | None = None, + use_sudo: bool = False, + docker_context: str | None = None, + environ: Mapping[str, str] | None = None, + os_release_path: Path | None = None, +) -> DoctorReport: + """Run every check required by the selected local or remote mode.""" + environment = os.environ if environ is None else environ + distro = _read_distro(OS_RELEASE_PATH if os_release_path is None else os_release_path) + checks: list[DoctorCheck] = [] + if host is None: + checks.extend((_config_check(layout), _layout_check(layout))) + checks.append(_secret_policy_check(layout)) + checks.append(_output_check(layout.roles_directory)) + checks.extend( + ( + _sops_check(environment, distro), + _gpg_check(environment, distro), + _sops_rule_check(layout.sops_config), + ) + ) + galaxy = _resolve_ansible_tool("ansible-galaxy", environment) + checks.extend( + ( + _ansible_tool_check("ansible-galaxy", galaxy, distro), + _collections_check(galaxy, layout, environment, distro), + _ansible_tool_check( + "ansible-lint", + _resolve_ansible_tool("ansible-lint", environment), + distro, + ), + _ansible_tool_check( + "ansible-playbook", + _resolve_ansible_tool("ansible-playbook", environment), + distro, + ), + _roles_check(layout.roles_directory), + _drift_check(layout.roles_directory), + _backups_check(layout.roles_directory), + ) + ) + else: + ssh = _resolve_executable("ssh", "COMPOSE2ROLE_SSH_BIN", environment) + checks.extend( + _remote_checks(ssh, host, use_sudo, docker_context, environment, distro) + ) + target = target_for(host) + return DoctorReport(target=target, checks=tuple(checks)) + + +def _config_check(layout: AnsibleLayout) -> DoctorCheck: + if layout.config.path is not None: + detail = f"repository configuration: {layout.config.path}" + else: + detail = f"no repository configuration discovered; using {layout.root}" + return DoctorCheck("config", "ok", detail) + + +def _layout_check(layout: AnsibleLayout) -> DoctorCheck: + detail = f"roles {layout.roles_directory}; inventory {len(layout.inventory)} path(s)" + return DoctorCheck("layout", "ok", detail) + + +def _secret_policy_check(layout: AnsibleLayout) -> DoctorCheck: + if layout.secret_policy is None: + return DoctorCheck("secret policy", "ok", "no secret policy configured") + try: + load_secret_policy(layout.secret_policy) + except SecretPolicyError as error: + return DoctorCheck( + "secret policy", + "warning", + str(error), + hint=f"fix the secret policy at {layout.secret_policy}", + ) + return DoctorCheck("secret policy", "ok", f"secret policy loaded: {layout.secret_policy}") + + +def _output_check(roles_directory: Path) -> DoctorCheck: + writable, checked = _writable_directory(roles_directory) + if writable: + return DoctorCheck("output", "ok", f"roles directory is writable: {checked}") + return DoctorCheck( + "output", + "error", + f"roles directory is not writable: {checked}", + hint=f"make {checked} writable or pass --output", + required=True, + ) + + +def _remote_checks( + ssh: str | None, + host: str, + use_sudo: bool, + docker_context: str | None, + environment: Mapping[str, str], + distro: _Distro, +) -> tuple[DoctorCheck, ...]: + if ssh is None: + return ( + DoctorCheck( + "ssh", + "error", + "OpenSSH client executable 'ssh' was not found", + hint=_package_hint(_openssh_package(distro), distro), + required=True, + ), + ) + checks = [ + DoctorCheck("ssh", "ok", f"OpenSSH client executable: {ssh}"), + _ssh_resolution_check(ssh, host, environment), + _remote_docker_check(ssh, host, use_sudo, docker_context, environment), + _remote_compose_check(ssh, host, use_sudo, docker_context, environment), + ] + return tuple(checks) + + +def _ssh_resolution_check( + ssh: str, host: str, environment: Mapping[str, str] +) -> DoctorCheck: + command = ssh_command(host, ()) + argv = [ssh, "-G", *command[1:]] + result = _run(argv, environment) + if result is None or result.returncode != 0: + return DoctorCheck( + "ssh resolution", + "error", + f"ssh -G {host} failed", + hint="check the SSH host alias and OpenSSH configuration", + required=True, + ) + fields = _ssh_config_fields(result.stdout) + detail = ( + f"ssh -G {host} resolved hostname {fields.get('hostname', 'unknown')}, " + f"user {fields.get('user', 'unknown')}, port {fields.get('port', 'unknown')}" + ) + return DoctorCheck("ssh resolution", "ok", detail) + + +def _remote_docker_check( + ssh: str, + host: str, + use_sudo: bool, + docker_context: str | None, + environment: Mapping[str, str], +) -> DoctorCheck: + info = _run_remote( + ssh, + host, + docker_command( + ("info", "--format", _DOCKER_INFO_FORMAT), docker_context=docker_context + ), + use_sudo=use_sudo, + environment=environment, + ) + if info is None or info.returncode != 0: + return DoctorCheck( + "remote docker", + "error", + f"remote Docker daemon is not reachable on {host}", + hint=( + "join the docker group, configure passwordless sudo, " + "or name a context with --docker-context" + ), + required=True, + ) + mode = _daemon_mode(info.stdout) + projects = _remote_project_count(ssh, host, use_sudo, docker_context, environment) + detail = ( + f"remote Docker daemon is {mode}" + if mode is not None + else "remote Docker daemon is reachable" + ) + if projects is None: + detail = f"{detail}; visible Compose projects unavailable" + else: + detail = f"{detail}; {projects} visible Compose project(s)" + return DoctorCheck("remote docker", "ok", detail) + + +def _remote_project_count( + ssh: str, + host: str, + use_sudo: bool, + docker_context: str | None, + environment: Mapping[str, str], +) -> int | None: + result = _run_remote( + ssh, + host, + docker_command( + ("compose", "ls", "--all", "--format", "json"), docker_context=docker_context + ), + use_sudo=use_sudo, + environment=environment, + ) + if result is None or result.returncode != 0: + return None + try: + payload: object = json.loads(result.stdout) + except JSONDecodeError: + return None + return len(payload) if isinstance(payload, list) else None + + +def _run_remote( + ssh: str, + host: str, + remote_command: tuple[str, ...], + *, + use_sudo: bool, + environment: Mapping[str, str], +) -> CompletedProcess[str] | None: + if use_sudo: + remote_command = ("sudo", "-n", *remote_command) + command = ssh_command(host, remote_command) + return _run([ssh, *command[1:]], environment) + + +def _remote_compose_check( + ssh: str, + host: str, + use_sudo: bool, + docker_context: str | None, + environment: Mapping[str, str], +) -> DoctorCheck: + result = _run_remote( + ssh, + host, + docker_command(("compose", "version"), docker_context=docker_context), + use_sudo=use_sudo, + environment=environment, + ) + if result is None or result.returncode != 0: + return DoctorCheck( + "remote compose", + "error", + f"remote docker compose version failed on {host}", + hint="verify SSH access and install the Docker Compose v2 or newer plugin", + required=True, + ) + version = _compose_version(result.stdout) + major = _compose_major(version) + if major is None: + return DoctorCheck( + "remote compose", + "error", + f"remote Docker Compose version is unreadable: {version}", + hint="install the Docker Compose v2 or newer plugin on the host", + required=True, + ) + if major < 2: + return DoctorCheck( + "remote compose", + "error", + f"remote Docker Compose is {version}; compose2role requires v2 or newer", + hint="install the Docker Compose v2 or newer plugin on the host", + required=True, + ) + return DoctorCheck("remote compose", "ok", f"remote docker compose {version}") + + +def _sops_check(environment: Mapping[str, str], distro: _Distro) -> DoctorCheck: + executable = _resolve_executable("sops", "COMPOSE2ROLE_SOPS_BIN", environment) + if executable is None: + return DoctorCheck( + "sops", + "warning", + "sops executable was not found", + hint=_package_hint("sops", distro), + ) + result = _run([executable, "--version"], environment) + if result is None or result.returncode != 0: + return DoctorCheck( + "sops", + "warning", + "sops --version failed", + hint="verify the sops installation", + ) + return DoctorCheck("sops", "ok", _first_line(result.stdout) or f"sops: {executable}") + + +def _gpg_check(environment: Mapping[str, str], distro: _Distro) -> DoctorCheck: + executable = _resolve_executable("gpg", None, environment) + if executable is None: + package = "gnupg2" if distro.family == "rhel" else "gnupg" + return DoctorCheck( + "gpg", + "warning", + "gpg executable was not found", + hint=_package_hint(package, distro), + ) + return DoctorCheck("gpg", "ok", f"gpg executable: {executable}") + + +def _sops_rule_check(path: Path | None) -> DoctorCheck: + if path is None: + return DoctorCheck("sops rule", "ok", "no SOPS configuration configured") + try: + validate_sops_config(path) + except GenerationError as error: + return DoctorCheck( + "sops rule", + "error", + str(error), + hint=f"define a creation rule for {SOPS_TARGET} in {path}", + required=True, + ) + return DoctorCheck("sops rule", "ok", f"creation rule covers {SOPS_TARGET}: {path}") + + +def _ansible_tool_check(name: str, executable: str | None, distro: _Distro) -> DoctorCheck: + if executable is None: + package = "ansible-lint" if name == "ansible-lint" else _ansible_package(distro) + return DoctorCheck( + name, + "warning", + f"{name} executable was not found", + hint=_package_hint(package, distro), + ) + return DoctorCheck(name, "ok", f"{name} executable: {executable}") + + +def _collections_check( + galaxy: str | None, + layout: AnsibleLayout, + environment: Mapping[str, str], + distro: _Distro, +) -> DoctorCheck: + if galaxy is None: + return DoctorCheck( + "ansible collections", + "warning", + "installed collections were not checked because ansible-galaxy was not found", + hint=_package_hint(_ansible_package(distro), distro), + ) + result = _run([galaxy, "collection", "list", "--format", "json"], environment) + if result is None or result.returncode != 0: + return DoctorCheck( + "ansible collections", + "warning", + "ansible-galaxy collection list failed", + hint="verify the ansible-galaxy installation", + ) + installed = _installed_collections(result.stdout) + if installed is None: + return DoctorCheck( + "ansible collections", + "warning", + "ansible-galaxy returned invalid collection data", + ) + for name, version in _local_collections(layout, environment).items(): + installed.setdefault(name, version) + missing = [name for name in _REQUIRED_COLLECTIONS if name not in installed] + if missing: + return DoctorCheck( + "ansible collections", + "warning", + f"missing collections: {', '.join(missing)}", + hint=f"install with: ansible-galaxy collection install {' '.join(missing)}", + ) + found = ", ".join( + f"{name} {installed[name]}".rstrip() for name in _REQUIRED_COLLECTIONS + ) + return DoctorCheck("ansible collections", "ok", f"found {found}") + + +def _roles_check(roles_directory: Path) -> DoctorCheck: + roles = _managed_roles(roles_directory) + if not roles: + return DoctorCheck("roles", "ok", "no generated roles yet") + return DoctorCheck("roles", "ok", f"{len(roles)} generated role(s)") + + +def _drift_check(roles_directory: Path) -> DoctorCheck: + managed = _managed_roles(roles_directory) + if not managed: + return DoctorCheck("drift", "ok", "no managed roles to compare") + counts: dict[RoleDriftState, int] = { + "unchanged": 0, + "source changed": 0, + "source unavailable": 0, + "invalid": 0, + } + for role_directory in managed: + counts[_role_drift(role_directory)] += 1 + detail = ( + f"{counts['unchanged']} unchanged, {counts['source changed']} source changed, " + f"{counts['source unavailable']} source unavailable" + ) + if counts["invalid"]: + detail = f"{detail}, {counts['invalid']} unreadable provenance" + if counts["source changed"] or counts["invalid"]: + return DoctorCheck( + "drift", + "warning", + detail, + hint="run compose2role audit for an authoritative drift report", + ) + return DoctorCheck("drift", "ok", detail) + + +def _backups_check(roles_directory: Path) -> DoctorCheck: + directory = roles_directory / _BACKUP_DIRECTORY + if not directory.is_dir(): + return DoctorCheck("backups", "ok", "no backups") + try: + count = sum(1 for entry in directory.iterdir() if entry.is_dir()) + except OSError: + count = 0 + total = _directory_size(directory) + hint = f"prune with: rm -r {directory}" if count else None + return DoctorCheck( + "backups", + "ok", + f"{count} backup(s), {_format_size(total)}", + hint=hint, + ) + + +def _managed_roles(roles_directory: Path) -> tuple[Path, ...]: + try: + entries = sorted(roles_directory.iterdir()) + except OSError: + return () + return tuple( + entry + for entry in entries + if entry.is_dir() + and not entry.is_symlink() + and not entry.name.startswith(".") + and (entry / _PROVENANCE_FILE).is_file() + ) + + +def _role_drift(role_directory: Path) -> RoleDriftState: + """Compare recorded provenance hashes without re-resolving the project. + + ``audit`` remains the authoritative check because it re-captures the source. + Doctor only needs a cheap, offline signal for each existing role. + """ + try: + document: object = json.loads( + (role_directory / _PROVENANCE_FILE).read_text(encoding="utf-8") + ) + except (OSError, UnicodeDecodeError, JSONDecodeError): + return "invalid" + if not isinstance(document, dict): + return "invalid" + source = document.get("source") + files = document.get("files") + if not isinstance(source, dict) or not isinstance(files, dict): + return "invalid" + root_value = source.get("root") + if not isinstance(root_value, str) or not root_value: + return "invalid" + root = Path(root_value) + if not root.is_dir(): + return "source unavailable" + for path, entry in files.items(): + if not isinstance(path, str) or not isinstance(entry, dict): + return "invalid" + if not _recorded_file_matches(root, path, entry): + return "source changed" + return "unchanged" + + +def _recorded_file_matches( + root: Path, relative_path: str, entry: Mapping[str, object] +) -> bool: + expected = entry.get("sha256") + if not isinstance(expected, str): + return False + relative = PurePosixPath(relative_path) + if relative.is_absolute() or ".." in relative.parts: + return False + local_path = root.joinpath(*relative.parts) + try: + local_path.resolve().relative_to(root.resolve()) + metadata = local_path.stat() + content = local_path.read_bytes() + except (OSError, ValueError): + return False + if hashlib.sha256(content).hexdigest() == expected: + return True + file_metadata = FileMetadata( + mode=S_IMODE(metadata.st_mode), uid=metadata.st_uid, gid=metadata.st_gid + ) + return expected in { + normalized_compose_hash(content), + normalized_dotenv_hash(content), + normalized_file_secret_hash(file_metadata), + } + + +def _writable_directory(path: Path) -> tuple[bool, Path]: + candidate = path + while not candidate.exists() and candidate != candidate.parent: + candidate = candidate.parent + if not candidate.is_dir(): + return False, candidate + return os.access(candidate, os.W_OK | os.X_OK), candidate + + +def _resolve_executable( + name: str, override_name: str | None, environment: Mapping[str, str] +) -> str | None: + search_path = environment.get("PATH", os.defpath) + if override_name is not None: + override = environment.get(override_name) + if override: + return shutil.which(override, path=search_path) + return shutil.which(name, path=search_path) + + +def _resolve_ansible_tool(name: str, environment: Mapping[str, str]) -> str | None: + override = environment.get("COMPOSE2ROLE_ANSIBLE_BIN") + if override: + candidate = Path(override).expanduser() / name + if candidate.is_file() and os.access(candidate, os.X_OK): + return str(candidate) + return None + return shutil.which(name, path=environment.get("PATH", os.defpath)) + + +def _installed_collections(output: str) -> dict[str, str] | None: + try: + payload: object = json.loads(output) + except JSONDecodeError: + return None + if not isinstance(payload, dict): + return None + found: dict[str, str] = {} + for path_value in payload.values(): + if not isinstance(path_value, dict): + continue + for name, version_value in path_value.items(): + if not isinstance(name, str): + continue + version = "" + if isinstance(version_value, dict): + raw_version = version_value.get("version") + if isinstance(raw_version, str): + version = raw_version + found[name] = version + return found + + +def _run(argv: list[str], environment: Mapping[str, str]) -> CompletedProcess[str] | None: + try: + return run( + argv, + check=False, + capture_output=True, + env=dict(environment), + text=True, + timeout=_TIMEOUT_SECONDS, + ) + except (FileNotFoundError, OSError, TimeoutExpired): + return None + + +def _read_distro(path: Path) -> _Distro: + try: + content = path.read_text(encoding="utf-8") + except (OSError, UnicodeDecodeError): + return _Distro(None) + values: dict[str, str] = {} + for line in content.splitlines(): + stripped = line.strip() + if not stripped or stripped.startswith("#") or "=" not in stripped: + continue + key, _, value = stripped.partition("=") + value = value.strip() + if len(value) >= 2 and value[0] == value[-1] and value[0] in "\"'": + value = value[1:-1] + values[key.strip()] = value + tokens = {values.get("ID", "").lower(), *values.get("ID_LIKE", "").lower().split()} + if tokens & {"debian", "ubuntu"}: + return _Distro("debian") + if tokens & {"rhel", "fedora", "centos", "rocky", "almalinux", "ol"}: + return _Distro("rhel") + return _Distro(None) + + +def _package_hint(package: str, distro: _Distro) -> str: + if distro.family == "debian": + return f"install with: sudo apt-get install {package}" + if distro.family == "rhel": + suffix = " (EPEL)" if package == "sops" else "" + return f"install with: sudo dnf install {package}{suffix}" + return f"install {package} with your package manager" + + +def _ansible_package(distro: _Distro) -> str: + return "ansible-core" if distro.family == "rhel" else "ansible" + + +def _openssh_package(distro: _Distro) -> str: + return "openssh-clients" if distro.family == "rhel" else "openssh-client" + + +def _directory_size(path: Path) -> int: + total = 0 + for root, _directories, files in os.walk(path, followlinks=False): + for name in files: + try: + total += (Path(root) / name).stat().st_size + except OSError: + continue + return total + + +def _format_size(size: int) -> str: + if size < 1024: + return f"{size} B" + value = size / 1024 + for unit in ("KiB", "MiB"): + if value < 1024: + return f"{value:.1f} {unit}" + value /= 1024 + return f"{value:.1f} GiB" + + +def _compose_version(text: str) -> str: + version = _first_line(text) or "version unknown" + prefix = "Docker Compose version " + if version.startswith(prefix): + version = version[len(prefix) :] + return version + + +def _compose_major(version: str) -> int | None: + match = re.match(r"v?(\d+)", version) + return int(match.group(1)) if match is not None else None + + +def _ssh_config_fields(output: str) -> dict[str, str]: + fields: dict[str, str] = {} + for line in output.splitlines(): + key, separator, value = line.partition(" ") + if separator and key and value: + fields.setdefault(key, value.strip()) + return fields + + +def _daemon_mode(output: str) -> str | None: + """Return rootless or rootful from `docker info`; None when the payload is unusable.""" + try: + payload: object = json.loads(output) + except JSONDecodeError: + return None + if not isinstance(payload, dict): + return None + options = payload.get("SecurityOptions") + if not isinstance(options, list): + return None + return "rootless" if "name=rootless" in options else "rootful" + + +def _local_collections( + layout: AnsibleLayout, environment: Mapping[str, str] +) -> dict[str, str]: + """Find collections installed beside the repository or on ANSIBLE_COLLECTIONS_PATH.""" + roots: list[Path] = [ + Path(entry).expanduser() + for entry in environment.get("ANSIBLE_COLLECTIONS_PATH", "").split(os.pathsep) + if entry + ] + roots.append(layout.root / "collections") + found: dict[str, str] = {} + for root in roots: + for base in (root / "ansible_collections", root): + try: + namespaces = sorted(base.iterdir()) + except OSError: + continue + for namespace in namespaces: + if not namespace.is_dir(): + continue + try: + collections = sorted(namespace.iterdir()) + except OSError: + continue + for collection in collections: + if collection.is_dir(): + found.setdefault(f"{namespace.name}.{collection.name}", "") + return found + + +def _first_line(text: str) -> str: + for line in text.splitlines(): + stripped = line.strip() + if stripped: + return stripped[:120] + return "" diff --git a/src/compose2role/drift.py b/src/compose2role/drift.py new file mode 100644 index 0000000..de8dc89 --- /dev/null +++ b/src/compose2role/drift.py @@ -0,0 +1,98 @@ +"""Compare current captured source hashes with generated-role provenance.""" + +from __future__ import annotations + +import hashlib +import json +from pathlib import Path, PurePosixPath + +from compose2role.remote_emit import ( + normalized_compose_hash, + normalized_dotenv_hash, + normalized_file_secret_hash, +) +from compose2role.resolve import ProjectDrift, ProjectSnapshot, ResolvedProject + +_PROVENANCE_FILE = ".compose2role.json" + + +def inspect_drift(project: ResolvedProject, role_directory: Path) -> ProjectDrift: + """Return the value-free drift state for one project and its role directory.""" + role = role_directory.name + if not role_directory.exists(): + return ProjectDrift("missing", role=role, reason="role has not been generated") + if not role_directory.is_dir(): + return ProjectDrift("unmanaged", role=role, reason="role path is not a directory") + provenance_path = role_directory / _PROVENANCE_FILE + if not provenance_path.is_file(): + return ProjectDrift( + "unmanaged", role=role, reason="role has no .compose2role.json provenance" + ) + + try: + document: object = json.loads(provenance_path.read_text(encoding="utf-8")) + except (OSError, UnicodeDecodeError, json.JSONDecodeError): + return ProjectDrift( + "unmanaged", role=role, reason="role provenance is unreadable" + ) + if not isinstance(document, dict): + return ProjectDrift( + "unmanaged", role=role, reason="role provenance is unreadable" + ) + files = document.get("files") + if not isinstance(files, dict): + return ProjectDrift( + "unmanaged", role=role, reason="role provenance has no file hashes" + ) + + changed: set[str] = set() + recorded: set[str] = set() + for path, entry in files.items(): + if not isinstance(path, str) or not isinstance(entry, dict): + return ProjectDrift( + "unmanaged", role=role, reason="role provenance has invalid file hashes" + ) + expected = entry.get("sha256") + if not isinstance(expected, str): + return ProjectDrift( + "unmanaged", role=role, reason="role provenance has invalid file hashes" + ) + recorded.add(path) + actual = _current_hash(project.source, PurePosixPath(path), entry.get("redacted") is True) + if actual != expected: + changed.add(path) + + changed.update(_source_paths(project.source) - recorded) + if changed: + return ProjectDrift( + "source changed", role=role, changed=tuple(sorted(changed)) + ) + return ProjectDrift("unchanged", role=role) + + +def _current_hash( + snapshot: ProjectSnapshot, relative_path: PurePosixPath, redacted: bool +) -> str | None: + for source in snapshot.files: + if source.relative_path == relative_path: + if redacted: + return normalized_compose_hash(source.content) + return hashlib.sha256(source.content).hexdigest() + for source in snapshot.dotenv_files: + if source.relative_path == relative_path: + if redacted: + return normalized_dotenv_hash(source.content) + return hashlib.sha256(source.content).hexdigest() + for file_secret in snapshot.file_secrets: + if file_secret.source.relative_path == relative_path: + if redacted: + return normalized_file_secret_hash(file_secret.source.metadata) + return hashlib.sha256(file_secret.source.content).hexdigest() + return None + + +def _source_paths(snapshot: ProjectSnapshot) -> set[str]: + paths = {str(source.relative_path) for source in snapshot.files} + paths.update(str(source.relative_path) for source in snapshot.dotenv_files) + paths.update(str(file_secret.source.relative_path) for file_secret in snapshot.file_secrets) + return paths diff --git a/src/compose2role/emit.py b/src/compose2role/emit.py new file mode 100644 index 0000000..f4d1cae --- /dev/null +++ b/src/compose2role/emit.py @@ -0,0 +1,38 @@ +"""The source-faithful role emitter for remote generation. + +This module is the stable import surface; the rendering implementation lives in +:mod:`compose2role.remote_emit`. +""" + +from __future__ import annotations + +from compose2role.remote_emit import ( + GeneratedFile, + PlanComparison, + RolePlan, + RoleWriteResult, + compare_plan, + diff_plan, + external_prerequisites, + normalize_remote_role_name, + preview_remote_role, + with_encrypted_secrets, + write_role, +) +from compose2role.resolve import FileDiff, PlanDiff + +__all__ = [ + "FileDiff", + "GeneratedFile", + "PlanComparison", + "PlanDiff", + "RolePlan", + "RoleWriteResult", + "compare_plan", + "diff_plan", + "external_prerequisites", + "normalize_remote_role_name", + "preview_remote_role", + "with_encrypted_secrets", + "write_role", +] diff --git a/src/compose2role/errors.py b/src/compose2role/errors.py new file mode 100644 index 0000000..c643a70 --- /dev/null +++ b/src/compose2role/errors.py @@ -0,0 +1,33 @@ +"""Domain errors reported by the command-line interface.""" + + +class Compose2RoleError(Exception): + """Base class for expected compose2role failures.""" + + +class ConfigurationError(Compose2RoleError): + """The repository configuration or Ansible layout is invalid or unreadable.""" + + +class DockerContextError(Compose2RoleError): + """The selected Docker context name is not a valid one.""" + + +class ComposeParseError(Compose2RoleError): + """The compose document is invalid or outside the supported subset.""" + + +class GenerationError(Compose2RoleError): + """The target Ansible role cannot be generated safely.""" + + +class RemoteCommandError(Compose2RoleError): + """An SSH-mediated Docker Compose command failed.""" + + def __init__(self, message: str, category: str = "remote-command") -> None: + super().__init__(message) + self.category = category + + +class SecretPolicyError(Compose2RoleError): + """The controller-side secret-classification policy is invalid.""" diff --git a/src/compose2role/findings.py b/src/compose2role/findings.py new file mode 100644 index 0000000..5fcb57a --- /dev/null +++ b/src/compose2role/findings.py @@ -0,0 +1,131 @@ +"""Audit findings derived from the typed Compose model.""" + +from __future__ import annotations + +from dataclasses import dataclass +from enum import StrEnum + +from compose2role.model import SourcePosition, Stack + + +class Severity(StrEnum): + """The impact level assigned to a finding.""" + + ERROR = "error" + WARNING = "warning" + INFO = "info" + + +@dataclass(frozen=True) +class Finding: + """One stable, machine-readable diagnostic for a compose project.""" + + id: str + severity: Severity + message: str + location: SourcePosition + service: str | None = None + + def as_json(self) -> dict[str, str | None]: + """Return the stable JSON representation used by the CLI.""" + return { + "id": self.id, + "severity": self.severity.value, + "message": self.message, + "location": str(self.location), + "service": self.service, + } + + +def audit_stack(stack: Stack) -> tuple[Finding, ...]: + """Return all audit findings supported by the current ruleset.""" + findings: list[Finding] = [] + for service in stack.services: + if not service.image.is_pinned: + findings.append( + Finding( + id="C2R001", + severity=Severity.WARNING, + message="image is not pinned to a tag or digest", + location=service.location, + service=service.name, + ) + ) + for environment in service.environment: + if environment.is_secret: + findings.append( + Finding( + id="C2R002", + severity=Severity.ERROR, + message=( + f"{environment.name} is an inline secret-shaped environment variable; " + "move it to SOPS" + ), + location=environment.location, + service=service.name, + ) + ) + if service.is_privileged: + findings.append( + Finding( + id="C2R009", + severity=Severity.WARNING, + message="service enables privileged mode", + location=service.location, + service=service.name, + ) + ) + if service.uses_host_network: + findings.append( + Finding( + id="C2R010", + severity=Severity.WARNING, + message="service uses host networking", + location=service.location, + service=service.name, + ) + ) + if service.runs_as_root: + findings.append( + Finding( + id="C2R011", + severity=Severity.WARNING, + message="service runs as root", + location=service.location, + service=service.name, + ) + ) + if not service.has_healthcheck: + findings.append( + Finding( + id="C2R012", + severity=Severity.INFO, + message="service has no healthcheck", + location=service.location, + service=service.name, + ) + ) + if not service.has_restart_policy: + findings.append( + Finding( + id="C2R016", + severity=Severity.WARNING, + message="service has no restart policy", + location=service.location, + service=service.name, + ) + ) + return tuple(findings) + + +def classified_secret_finding( + service: str, key: str, reason: str, location: SourcePosition +) -> Finding: + """Report a remote secret classification without exposing its effective value.""" + return Finding( + id="C2R025", + severity=Severity.INFO, + message=f"{key} is classified as a secret ({reason})", + location=location, + service=service, + ) diff --git a/src/compose2role/layout.py b/src/compose2role/layout.py new file mode 100644 index 0000000..91ed67b --- /dev/null +++ b/src/compose2role/layout.py @@ -0,0 +1,159 @@ +"""Discover the Ansible repository layout.""" + +from __future__ import annotations + +import configparser +import os +from collections.abc import Mapping +from dataclasses import dataclass +from pathlib import Path + +from compose2role.config import HostProfile, RepositoryConfig, find_file_upwards +from compose2role.errors import ConfigurationError + + +@dataclass(frozen=True) +class AnsibleConfig: + """The documented subset of `ansible.cfg` used by the tool.""" + + path: Path + roles_path: tuple[Path, ...] = () + collections_path: tuple[Path, ...] = () + inventory: tuple[Path, ...] = () + + +@dataclass(frozen=True) +class AnsibleLayout: + """Repository paths derived from configuration and Ansible conventions.""" + + root: Path + config: RepositoryConfig + ansible_config: AnsibleConfig | None + roles_directory: Path + inventory: tuple[Path, ...] + collections_path: tuple[Path, ...] + sops_config: Path | None + secret_policy: Path | None + host: HostProfile + + +def find_ansible_config(start: Path | None = None) -> Path | None: + """Find the nearest `ansible.cfg` walking up from START.""" + return find_file_upwards(start, "ansible.cfg") + + +def load_ansible_config(path: Path) -> AnsibleConfig: + """Read only `[defaults]` `roles_path`, `collections_path`, and `inventory`.""" + parser = configparser.ConfigParser(interpolation=None) + try: + with path.open(encoding="utf-8") as handle: + parser.read_file(handle) + except (OSError, UnicodeDecodeError, configparser.Error) as error: + raise ConfigurationError(f"Ansible configuration is unreadable: {path}") from error + + if not parser.has_section("defaults"): + return AnsibleConfig(path=path) + return AnsibleConfig( + path=path, + roles_path=_split_config_paths( + parser.get("defaults", "roles_path", fallback=""), path.parent + ), + collections_path=_split_config_paths( + parser.get("defaults", "collections_path", fallback=""), path.parent + ), + inventory=_split_config_paths( + parser.get("defaults", "inventory", fallback=""), path.parent + ), + ) + + +def discover_layout( + config: RepositoryConfig, + *, + start: Path | None = None, + explicit_output: Path | None = None, + explicit_sops_config: Path | None = None, + explicit_secret_policy: Path | None = None, + host: str | None = None, + environ: Mapping[str, str] | None = None, +) -> AnsibleLayout: + """Resolve repository paths with flags first, then environment, then config.""" + environment = os.environ if environ is None else environ + root = config.root + anchor = (start if start is not None else root).expanduser().resolve() + ansible_path = find_ansible_config(anchor) + ansible_config = load_ansible_config(ansible_path) if ansible_path is not None else None + + roles_directory = ( + _selected_path(explicit_output, environment, None, anchor) + or config.output + or (ansible_config.roles_path[0] if ansible_config and ansible_config.roles_path else None) + or _first_existing((root / "ansible" / "roles", root / "roles")) + or root / "roles" + ) + sops_config = ( + _selected_path(explicit_sops_config, environment, None, anchor) + or _selected_path(None, environment, "COMPOSE2ROLE_SOPS_CONFIG", anchor) + or config.sops_config + ) + secret_policy = ( + _selected_path(explicit_secret_policy, environment, None, anchor) + or _selected_path(None, environment, "COMPOSE2ROLE_SECRET_POLICY", anchor) + or config.secret_policy + ) + + if ansible_config is not None and ansible_config.inventory: + inventory = ansible_config.inventory + else: + discovered_inventory = _first_existing( + (root / "inventory", root / "ansible" / "inventory") + ) + inventory = () if discovered_inventory is None else (discovered_inventory,) + + return AnsibleLayout( + root=root, + config=config, + ansible_config=ansible_config, + roles_directory=roles_directory, + inventory=inventory, + collections_path=ansible_config.collections_path if ansible_config else (), + sops_config=sops_config, + secret_policy=secret_policy, + host=config.host(host), + ) + + +def _selected_path( + explicit: Path | None, + environment: Mapping[str, str], + environment_name: str | None, + base: Path, +) -> Path | None: + if explicit is not None: + path = explicit.expanduser() + return path if path.is_absolute() else base / path + if environment_name is None: + return None + value = environment.get(environment_name) + if not value: + return None + path = Path(value).expanduser() + return path if path.is_absolute() else base / path + + +def _first_existing(candidates: tuple[Path, ...]) -> Path | None: + for candidate in candidates: + if candidate.exists(): + return candidate + return None + + +def _split_config_paths(value: str, base: Path) -> tuple[Path, ...]: + paths: list[Path] = [] + for entry in value.replace(",", os.pathsep).split(os.pathsep): + entry = entry.strip() + if not entry: + continue + path = Path(entry).expanduser() + paths.append(path.resolve() if path.is_absolute() else (base / path).resolve()) + return tuple(paths) diff --git a/src/compose2role/model.py b/src/compose2role/model.py new file mode 100644 index 0000000..2b61223 --- /dev/null +++ b/src/compose2role/model.py @@ -0,0 +1,247 @@ +"""The typed Compose model used by remote audit.""" + +from __future__ import annotations + +from collections.abc import Mapping +from dataclasses import dataclass +from pathlib import PurePosixPath + +from compose2role.errors import ComposeParseError + + +@dataclass(frozen=True) +class SourcePosition: + """A one-based location in a source compose file.""" + + path: PurePosixPath + line: int + column: int + + def __str__(self) -> str: + return f"{self.path}:{self.line}:{self.column}" + + +@dataclass(frozen=True) +class Image: + """An image repository and its tag or digest.""" + + source: str + repository: str | None + reference: str | None + separator: str | None + + @property + def uses_interpolation(self) -> bool: + """Whether Compose must resolve the image expression itself.""" + return "${" in self.source + + @property + def is_pinned(self) -> bool: + """Whether the image has a tag other than ``latest`` or a digest.""" + if self.uses_interpolation: + return True + return self.separator == "@" or self.reference != "latest" + + +@dataclass(frozen=True) +class Port: + """A resolved port mapping.""" + + target: str + published: str | None + location: SourcePosition + + +@dataclass(frozen=True) +class EnvironmentVariable: + """An inline, mapping-form environment variable from a Compose service.""" + + name: str + value: str + is_secret: bool + location: SourcePosition + + +@dataclass(frozen=True) +class Service: + """A Compose service.""" + + name: str + image: Image + ports: tuple[Port, ...] + environment: tuple[EnvironmentVariable, ...] + is_privileged: bool + uses_host_network: bool + runs_as_root: bool + has_healthcheck: bool + has_restart_policy: bool + option_names: frozenset[str] + location: SourcePosition + + +@dataclass(frozen=True) +class Stack: + """A complete Compose project.""" + + services: tuple[Service, ...] + option_names: frozenset[str] + name: str | None = None + + +def parse_canonical_stack(document: object, *, source_path: PurePosixPath) -> Stack: + """Parse Docker Compose's canonical JSON model for remote audit. + + Canonical JSON has no useful source positions, so every derived value uses the supplied + synthetic source position. Remote secret classification is intentionally performed separately + from this parser. + """ + document_mapping = _canonical_mapping(document, "remote Docker Compose returned invalid JSON") + services_value = document_mapping.get("services") + services_mapping = _canonical_mapping( + services_value, "remote Docker Compose returned no services" + ) + if not services_mapping: + raise ComposeParseError("remote Docker Compose returned no services") + + services = tuple( + _parse_canonical_service(source_path, service_name, service_value) + for service_name, service_value in services_mapping.items() + ) + name_value = document_mapping.get("name") + if name_value is not None and not isinstance(name_value, str): + raise ComposeParseError("remote Docker Compose returned an invalid project name") + return Stack( + services=services, + option_names=frozenset(document_mapping), + name=name_value, + ) + + +def _canonical_mapping(value: object, message: str) -> dict[str, object]: + if not isinstance(value, dict): + raise ComposeParseError(message) + mapping: dict[str, object] = {} + for key, item in value.items(): + if not isinstance(key, str): + raise ComposeParseError(message) + mapping[key] = item + return mapping + + +def _parse_canonical_service( + source_path: PurePosixPath, service_name: str, service_value: object +) -> Service: + service = _canonical_mapping(service_value, "remote Docker Compose returned an invalid service") + position = SourcePosition(path=source_path, line=1, column=1) + image_value = service.get("image") + if not isinstance(image_value, str) or not image_value: + raise ComposeParseError("remote Docker Compose returned a service without an image") + + ports_value = service.get("ports", []) + if not isinstance(ports_value, list): + raise ComposeParseError("remote Docker Compose returned invalid service ports") + environment_value = _canonical_mapping( + service.get("environment", {}), "remote Docker Compose returned invalid service environment" + ) + environment: list[EnvironmentVariable] = [] + for name, value in environment_value.items(): + if not name or not isinstance(value, str) or _has_unresolved_interpolation(value): + raise ComposeParseError("remote Docker Compose returned unresolved environment values") + environment.append( + EnvironmentVariable( + name=name, + value=value, + is_secret=False, + location=position, + ) + ) + + return Service( + name=service_name, + image=_parse_canonical_image(image_value), + ports=tuple(_parse_canonical_port(value, position) for value in ports_value), + environment=tuple(environment), + is_privileged=service.get("privileged") is True, + uses_host_network=service.get("network_mode") == "host", + runs_as_root=_runs_as_root(service.get("user")), + has_healthcheck=_has_healthcheck(service), + has_restart_policy=_has_restart_policy(service.get("restart")), + option_names=frozenset(service), + location=position, + ) + + +def _parse_canonical_image(image: str) -> Image: + if _has_unresolved_interpolation(image): + raise ComposeParseError("remote Docker Compose returned an unresolved image reference") + if "@" in image: + repository, reference = image.split("@", maxsplit=1) + if repository and reference: + return Image(source=image, repository=repository, reference=reference, separator="@") + raise ComposeParseError("remote Docker Compose returned an invalid image reference") + + last_slash = image.rfind("/") + last_colon = image.rfind(":") + if last_colon > last_slash: + repository, reference = image.rsplit(":", maxsplit=1) + if repository and reference: + return Image(source=image, repository=repository, reference=reference, separator=":") + raise ComposeParseError("remote Docker Compose returned an invalid image reference") + return Image(source=image, repository=image, reference="latest", separator=":") + + +def _has_unresolved_interpolation(value: str) -> bool: + """Return whether a canonical value still contains an active Compose interpolation.""" + index = 0 + while index < len(value): + if value[index] != "$": + index += 1 + continue + if index + 1 == len(value): + return False + following = value[index + 1] + if following == "$": + index += 2 + continue + if following == "{" or following == "_" or ( + following.isascii() and following.isalpha() + ): + return True + index += 1 + return False + + +def _parse_canonical_port(value: object, position: SourcePosition) -> Port: + port = _canonical_mapping(value, "remote Docker Compose returned an invalid port mapping") + target = port.get("target") + published = port.get("published") + if isinstance(target, bool) or not isinstance(target, (int, str)): + raise ComposeParseError("remote Docker Compose returned an invalid port mapping") + if isinstance(published, bool) or not isinstance(published, (int, str, type(None))): + raise ComposeParseError("remote Docker Compose returned an invalid port mapping") + return Port( + target=str(target), + published=None if published is None else str(published), + location=position, + ) + + +def _runs_as_root(user: object) -> bool: + return user is None or user == "" or user == "0" or user == 0 + + +def _has_healthcheck(service: Mapping[str, object]) -> bool: + if "healthcheck" not in service: + return False + + healthcheck: object = service.get("healthcheck") + if not isinstance(healthcheck, Mapping): + return True + if healthcheck.get("disable") is True: + return False + test: object = healthcheck.get("test") + return test != "NONE" + + +def _has_restart_policy(restart: object) -> bool: + return isinstance(restart, str) and restart not in {"", "no"} diff --git a/src/compose2role/naming.py b/src/compose2role/naming.py new file mode 100644 index 0000000..4ec134a --- /dev/null +++ b/src/compose2role/naming.py @@ -0,0 +1,25 @@ +"""Transport-neutral role and Compose project name normalization.""" + +from __future__ import annotations + +import re + + +def normalize_role_name(raw: str) -> str: + """Derive a deterministic valid Ansible role name.""" + normalized = re.sub(r"[^a-z0-9_]+", "_", raw.lower()).strip("_") + if not normalized: + return "compose_project" + if not normalized[0].isalpha(): + return f"compose_{normalized}" + return normalized + + +def normalize_compose_project_name(raw: str) -> str: + """Derive a deterministic Compose-compatible project name.""" + normalized = re.sub(r"[^a-z0-9_-]+", "", raw.lower()).strip("_-") + if not normalized: + return "compose" + if not normalized[0].isalnum(): + return f"compose_{normalized}" + return normalized diff --git a/src/compose2role/remote.py b/src/compose2role/remote.py new file mode 100644 index 0000000..68ec00f --- /dev/null +++ b/src/compose2role/remote.py @@ -0,0 +1,601 @@ +"""Resolve remote Docker Compose projects without persisting their source data.""" + +from __future__ import annotations + +import base64 +import binascii +import json +import os +import shlex +from collections.abc import Callable +from json import JSONDecodeError +from pathlib import PurePosixPath +from select import select +from subprocess import DEVNULL, PIPE, Popen, TimeoutExpired, run +from time import monotonic +from typing import IO, NoReturn + +from compose2role.discover import DiscoveredProject, discover_projects, ssh_command +from compose2role.errors import RemoteCommandError +from compose2role.resolve import ( + AuditOutcome, + AuditResult, + Canonicalizer, + CanonicalResult, + ProjectCapture, + ProjectFailure, + ProjectResolutionError, + ResolvedProject, + SourceFile, + relative_source_path, + resolve_project, +) +from compose2role.secret_policy import SecretPolicy +from compose2role.source import FileMetadata + +_SSH_TIMEOUT_SECONDS = 30 +_DISCOVERY_MESSAGES = { + "ssh": "SSH discovery failed", + "ssh-client": "OpenSSH client executable 'ssh' was not found", + "ssh-timeout": "SSH discovery timed out", + "remote-discovery": "remote Docker Compose discovery failed", + "invalid-json": "remote Docker Compose discovery returned invalid JSON", + "invalid-discovery": "remote Docker Compose discovery returned invalid project data", +} +_REMOTE_OPERATION_PREFIX = """\ +set -eu +main() { + base64 -d | { + IFS= read -r operation || exit 64 + case "$operation" in + config) + IFS= read -r project || exit 64 + IFS= read -r root || exit 64 + set -- + while IFS= read -r file; do + [ -n "$file" ] || exit 64 + set -- "$@" -f "$file" + done + exec env -i PATH="$PATH" %(docker)s compose --project-name "$project" \\ + --project-directory "$root" "$@" config --format json + ;; + *) + exit 64 + ;; + esac + } +} +main <<'__C2R_PAYLOAD__' +""" +_REMOTE_OPERATION_SUFFIX = """\ +__C2R_PAYLOAD__ +""" +_REMOTE_SOURCE_SESSION_SCRIPT = """\ +# compose2role-source-session-v1 +set -eu +source_state() { + source_path=$1 + source_root=$2 + root_physical=$(CDPATH= cd -P "$source_root" && pwd -P) || return 65 + parent_physical=$(CDPATH= cd -P "$(dirname "$source_path")" && pwd -P) || parent_physical= + if [ "$root_physical" != / ] && [ -n "$parent_physical" ] \ + && [ "$parent_physical" != "$root_physical" ] \ + && [ "${parent_physical#"$root_physical"/}" = "$parent_physical" ]; then + printf '%s\\n' escape + elif [ -L "$source_path" ]; then + printf '%s\\n' symlink + elif [ -f "$source_path" ]; then + printf '%s\\n' file + elif [ -e "$source_path" ]; then + printf '%s\\n' other + else + printf '%s\\n' missing + fi +} +main() { + printf '%s\\n' ready + while IFS=' ' read -r marker request_id request_operation extra; do + [ "$marker" = request ] || exit 64 + [ -n "$request_id" ] && [ -z "$extra" ] || exit 64 + case "$request_id" in + ''|*[!0-9]*) exit 64 ;; + esac + IFS= read -r path || exit 64 + IFS= read -r root || exit 64 + IFS= read -r trailer || exit 64 + [ "$trailer" = "end $request_id" ] || exit 64 + if ! state=$(source_state "$path" "$root"); then + printf 'result %s error\\nend %s\\n' "$request_id" "$request_id" + continue + fi + case "$request_operation" in + capture) + case "$state" in + file) + if ! metadata=$(stat -c '%a %u %g' -- "$path"); then + printf 'result %s error\\nend %s\\n' "$request_id" "$request_id" + continue + fi + if ! encoded=$(base64 < "$path"); then + printf 'result %s error\\nend %s\\n' "$request_id" "$request_id" + continue + fi + printf 'result %s file %s\\n' "$request_id" "$metadata" + printf '%s' "$encoded" | tr -d '\\n' + printf '\\nend %s\\n' "$request_id" + ;; + escape|symlink|other|missing) + printf 'result %s %s\\nend %s\\n' "$request_id" "$state" "$request_id" + ;; + *) exit 65 ;; + esac + ;; + stat) + printf 'result %s %s\\nend %s\\n' "$request_id" "$state" "$request_id" + ;; + *) exit 64 ;; + esac + done +} +# compose2role-source-session-ready +main +""" + + +class _RemoteSourceSession: + """One framed SSH session for all source reads needed by a remote project.""" + + def __init__(self, host: str, *, use_sudo: bool) -> None: + remote_command: tuple[str, ...] = ("sh", "-s") + if use_sudo: + remote_command = ("sudo", "-n", *remote_command) + try: + process: Popen[bytes] = Popen( + ssh_command(host, remote_command), + stdin=PIPE, + stdout=PIPE, + stderr=DEVNULL, + ) + except FileNotFoundError as error: + raise RemoteProjectError( + "ssh-client", "OpenSSH client executable 'ssh' was not found" + ) from error + if process.stdin is None or process.stdout is None: + process.kill() + raise RemoteProjectError("source", "remote source session could not be started") + self._process = process + self._stdin: IO[bytes] = process.stdin + self._stdout: IO[bytes] = process.stdout + self._line_buffer = b"" + self._next_request_id = 0 + self._closed = False + try: + self._write(_REMOTE_SOURCE_SESSION_SCRIPT) + if self._read_line() != "ready": + raise RemoteProjectError("source", "remote source session response is invalid") + except RemoteProjectError: + self.abort() + raise + + def capture(self, root: PurePosixPath, path: PurePosixPath) -> SourceFile | str: + """Capture PATH and its metadata, or return its source-boundary state.""" + return self._request("capture", root, path) + + def stat(self, root: PurePosixPath, path: PurePosixPath) -> str: + """Return the source-boundary state of PATH without reading its bytes.""" + state = self._request("stat", root, path) + if not isinstance(state, str): + raise RemoteProjectError("source", "remote source session response is invalid") + return state + + def close(self) -> None: + """Finish the session and reject an abnormal remote exit or trailing output.""" + if self._closed: + return + try: + self._stdin.close() + except OSError: + pass + trailing = self._line_buffer + self._read_to_eof() + try: + returncode = self._process.wait(timeout=_SSH_TIMEOUT_SECONDS) + except TimeoutExpired as error: + self.abort() + raise RemoteProjectError("timeout", "remote operation timed out") from error + self._closed = True + if returncode == 255: + raise RemoteProjectError("ssh", "SSH connection or remote command failed") + if returncode != 0: + raise RemoteProjectError("source", "remote source validation failed") + if trailing: + raise RemoteProjectError("source", "remote source session response is invalid") + + def abort(self) -> None: + """Close a failed session without replacing its original value-free error.""" + if self._closed: + return + self._closed = True + try: + self._stdin.close() + except OSError: + pass + if self._process.poll() is None: + self._process.terminate() + try: + self._process.wait(timeout=_SSH_TIMEOUT_SECONDS) + except TimeoutExpired: + self._process.kill() + self._process.wait() + + def _request( + self, operation: str, root: PurePosixPath, path: PurePosixPath + ) -> SourceFile | str: + request_id = self._next_request_id + self._next_request_id += 1 + self._write(f"request {request_id} {operation}\n{path}\n{root}\nend {request_id}\n") + header = self._read_line().split() + if len(header) < 3 or header[:2] != ["result", str(request_id)]: + raise RemoteProjectError("source", "remote source session response is invalid") + state = header[2] + if state not in {"error", "escape", "file", "missing", "other", "symlink"}: + raise RemoteProjectError("source", "remote source session response is invalid") + if state == "error": + self._expect_end(request_id) + raise RemoteProjectError("source", "remote source request failed") + if operation == "stat": + if len(header) != 3: + raise RemoteProjectError("source", "remote source session response is invalid") + self._expect_end(request_id) + return state + if operation != "capture": + raise RemoteProjectError("source", "remote source session response is invalid") + if state != "file": + if len(header) != 3: + raise RemoteProjectError("source", "remote source session response is invalid") + self._expect_end(request_id) + return state + if len(header) != 6: + raise RemoteProjectError("source", "remote source session response is invalid") + encoded_content = self._read_line() + self._expect_end(request_id) + metadata, content = _parse_captured_source_file( + f"file {' '.join(header[3:])}\n{encoded_content}" + ) + return SourceFile( + relative_path=relative_source_path(root, path), + content=content, + metadata=metadata, + ) + + def _expect_end(self, request_id: int) -> None: + if self._read_line() != f"end {request_id}": + raise RemoteProjectError("source", "remote source session response is invalid") + + def _write(self, value: str) -> None: + try: + self._stdin.write(value.encode("utf-8")) + self._stdin.flush() + except (BrokenPipeError, OSError) as error: + self._raise_closed(error) + + def _read_line(self) -> str: + deadline = monotonic() + _SSH_TIMEOUT_SECONDS + while True: + separator = self._line_buffer.find(b"\n") + if separator != -1: + line = self._line_buffer[:separator] + self._line_buffer = self._line_buffer[separator + 1 :] + try: + return line.decode("utf-8") + except UnicodeDecodeError as error: + raise RemoteProjectError( + "source", "remote response was not valid UTF-8" + ) from error + chunk = self._read_chunk(deadline) + if not chunk: + self._raise_closed() + self._line_buffer += chunk + + def _read_to_eof(self) -> bytes: + deadline = monotonic() + _SSH_TIMEOUT_SECONDS + chunks = bytearray() + while True: + chunk = self._read_chunk(deadline) + if not chunk: + return bytes(chunks) + chunks += chunk + + def _read_chunk(self, deadline: float) -> bytes: + timeout = deadline - monotonic() + if timeout <= 0: + self.abort() + raise RemoteProjectError("timeout", "remote operation timed out") + readable, _, _ = select((self._stdout,), (), (), timeout) + if not readable: + self.abort() + raise RemoteProjectError("timeout", "remote operation timed out") + try: + return os.read(self._stdout.fileno(), 8192) + except OSError as error: + self._raise_closed(error) + + def _raise_closed(self, error: OSError | None = None) -> NoReturn: + try: + returncode = self._process.wait(timeout=_SSH_TIMEOUT_SECONDS) + except TimeoutExpired: + self.abort() + returncode = None + if returncode == 255: + message = ("ssh", "SSH connection or remote command failed") + else: + message = ("source", "remote source session closed unexpectedly") + if error is None: + raise RemoteProjectError(*message) + raise RemoteProjectError(*message) from error + + +RemoteProjectError = ProjectResolutionError + + +class RemoteCapture(ProjectCapture): + """Adapt the framed SSH source session to the shared capture seam.""" + + def __init__(self, host: str, *, use_sudo: bool) -> None: + self._session = _RemoteSourceSession(host, use_sudo=use_sudo) + + def capture(self, root: PurePosixPath, path: PurePosixPath) -> SourceFile | str: + """Capture PATH and its metadata, or return its source-boundary state.""" + return self._session.capture(root, path) + + def stat(self, root: PurePosixPath, path: PurePosixPath) -> str: + """Return the source-boundary state of PATH without reading its bytes.""" + return self._session.stat(root, path) + + def close(self) -> None: + """Finish the session and reject an abnormal remote exit.""" + self._session.close() + + def abort(self) -> None: + """Discard the session without replacing the original value-free error.""" + self._session.abort() + + +class RemoteComposeCanonicalizer(Canonicalizer): + """Resolve a remote project through Docker Compose's canonical JSON model.""" + + def __init__( + self, host: str, *, use_sudo: bool, docker_context: str | None = None + ) -> None: + self._host = host + self._use_sudo = use_sudo + self._docker_context = docker_context + + def canonicalize( + self, + identity: DiscoveredProject, + root: PurePosixPath, + files: tuple[PurePosixPath, ...], + ) -> CanonicalResult: + """Return the effective Compose document for the captured project.""" + config_files = tuple(str(root.joinpath(*file.parts)) for file in files) + payload = "\n".join(("config", identity.name, str(root), *config_files)) + "\n" + output = _run_remote_config( + self._host, + identity.name, + payload, + use_sudo=self._use_sudo, + docker_context=self._docker_context, + ) + try: + document: object = json.loads(output) + except JSONDecodeError as error: + raise RemoteProjectError( + "invalid-json", "remote Docker Compose returned invalid canonical JSON" + ) from error + return CanonicalResult(document=_canonical_document(document)) + + +def resolve_remote_projects( + host: str, + *, + project_name: str | None, + all_projects: bool, + use_sudo: bool, + secret_policy: SecretPolicy | None, + docker_context: str | None = None, + on_outcome: Callable[[AuditOutcome], None] | None = None, +) -> AuditResult: + """Resolve a selected remote project or all discovered projects in isolation.""" + try: + projects = discover_projects( + host, + use_sudo=use_sudo, + include_working_directories=True, + docker_context=docker_context, + ) + except RemoteCommandError as error: + raise RemoteProjectError( + error.category, + _DISCOVERY_MESSAGES.get(error.category, "remote Docker Compose discovery failed"), + ) from error + if project_name is not None: + selected_projects = tuple(project for project in projects if project.name == project_name) + if not selected_projects: + failure = ProjectFailure( + name=project_name, + category="unknown-project", + message="the requested remote Compose project was not discovered", + ) + if on_outcome is not None: + on_outcome(failure) + return AuditResult( + projects=(), + failures=(failure,), + outcomes=(failure,), + ) + elif all_projects: + selected_projects = projects + else: + raise ValueError("remote project selection is required") + + resolved: list[ResolvedProject] = [] + failures: list[ProjectFailure] = [] + outcomes: list[AuditOutcome] = [] + for project in selected_projects: + try: + resolution = _resolve_remote_project( + host, + project, + use_sudo=use_sudo, + secret_policy=secret_policy, + docker_context=docker_context, + ) + resolved.append(resolution) + outcome: AuditOutcome = resolution + except RemoteProjectError as error: + failure = ProjectFailure( + name=project.name, + category=error.category, + message=str(error), + ) + failures.append(failure) + outcome = failure + outcomes.append(outcome) + if on_outcome is not None: + on_outcome(outcome) + return AuditResult( + projects=tuple(resolved), + failures=tuple(failures), + outcomes=tuple(outcomes), + ) + + +def _resolve_remote_project( + host: str, + project: DiscoveredProject, + *, + use_sudo: bool, + secret_policy: SecretPolicy | None, + docker_context: str | None, +) -> ResolvedProject: + return resolve_project( + RemoteCapture(host, use_sudo=use_sudo), + RemoteComposeCanonicalizer( + host, use_sudo=use_sudo, docker_context=docker_context + ), + project, + secret_policy, + ) + + +def _canonical_document(value: object) -> dict[str, object]: + if not isinstance(value, dict): + raise RemoteProjectError( + "invalid-model", "remote Docker Compose returned an unsupported effective model" + ) + document: dict[str, object] = {} + for key, item in value.items(): + if not isinstance(key, str): + raise RemoteProjectError( + "invalid-model", "remote Docker Compose returned an unsupported effective model" + ) + document[key] = item + return document + + +def _parse_captured_source_file(output: str) -> tuple[FileMetadata, bytes]: + """Decode one captured file header and its base64 content line.""" + header, separator, encoded_content = output.partition("\n") + if not separator: + raise RemoteProjectError("source", "remote source metadata is invalid") + + fields = header.split() + if len(fields) != 4 or fields[0] != "file": + raise RemoteProjectError("source", "remote source metadata is invalid") + mode, uid, gid = fields[1:] + if ( + not mode + or len(mode) > 4 + or any(character not in "01234567" for character in mode) + or len(uid) > 10 + or not uid.isascii() + or not uid.isdecimal() + or len(gid) > 10 + or not gid.isascii() + or not gid.isdecimal() + ): + raise RemoteProjectError("source", "remote source metadata is invalid") + try: + content = base64.b64decode(encoded_content.strip(), validate=True) + except (binascii.Error, ValueError) as error: + raise RemoteProjectError("source", "remote source content is invalid") from error + return FileMetadata(mode=int(mode, 8), uid=int(uid), gid=int(gid)), content + + +def _run_remote_config( + host: str, + project_name: str, + payload: str, + *, + use_sudo: bool, + docker_context: str | None, +) -> str: + return _run_remote_operation( + host, + project_name, + payload, + use_sudo=use_sudo, + docker_context=docker_context, + category="remote-compose", + failure_message=f"remote Compose resolution failed for project {project_name!r}", + ) + + +def _run_remote_operation( + host: str, + project_name: str, + payload: str, + *, + use_sudo: bool, + category: str, + failure_message: str, + docker_context: str | None = None, +) -> str: + remote_command: tuple[str, ...] = ("sh", "-s") + if use_sudo: + remote_command = ("sudo", "-n", *remote_command) + try: + result = run( + ssh_command(host, remote_command), + check=False, + capture_output=True, + input=_operation_script(payload, docker_context), + text=True, + encoding="utf-8", + timeout=_SSH_TIMEOUT_SECONDS, + ) + except FileNotFoundError as error: + raise RemoteProjectError( + "ssh-client", "OpenSSH client executable 'ssh' was not found" + ) from error + except TimeoutExpired as error: + raise RemoteProjectError("timeout", "remote operation timed out") from error + except UnicodeDecodeError as error: + raise RemoteProjectError(category, "remote response was not valid UTF-8") from error + + if result.returncode == 255: + raise RemoteProjectError("ssh", "SSH connection or remote command failed") + if result.returncode != 0: + raise RemoteProjectError(category, failure_message) + return result.stdout + + +def _operation_script(payload: str, docker_context: str | None = None) -> str: + encoded_payload = base64.b64encode(payload.encode("utf-8")).decode("ascii") + docker = ( + "docker" + if docker_context is None + else f"docker --context {shlex.quote(docker_context)}" + ) + prefix = _REMOTE_OPERATION_PREFIX % {"docker": docker} + return f"{prefix}{encoded_payload}\n{_REMOTE_OPERATION_SUFFIX}" diff --git a/src/compose2role/remote_emit.py b/src/compose2role/remote_emit.py new file mode 100644 index 0000000..c47f6d2 --- /dev/null +++ b/src/compose2role/remote_emit.py @@ -0,0 +1,807 @@ +"""Render and atomically write source-faithful Compose roles.""" + +from __future__ import annotations + +import hashlib +import json +import os +import re +import shutil +import tempfile +from dataclasses import dataclass +from datetime import UTC, datetime +from importlib.metadata import version +from pathlib import Path, PurePosixPath +from typing import Literal + +from ruamel.yaml import YAML +from ruamel.yaml.error import YAMLError + +from compose2role.errors import GenerationError +from compose2role.naming import normalize_role_name +from compose2role.resolve import FileDiff, PlanDiff, ResolvedProject +from compose2role.source import FileMetadata +from compose2role.source_fidelity import ( + SourceRoleMaterialization, + SourceTemplate, + materialize_source_role, +) + +_ROLE_NAME = re.compile(r"[a-z][a-z0-9_]*") +_WriteState = Literal["generated", "unchanged"] +_PROVENANCE_FILE = PurePosixPath(".compose2role.json") + + +@dataclass(frozen=True) +class GeneratedFile: + """One text or binary file in a generated role.""" + + relative_path: PurePosixPath + content: str | bytes + classified_secret: bool = False + + +@dataclass(frozen=True) +class RolePlan: + """A fully rendered role that has not been written yet.""" + + role_name: str + files: tuple[GeneratedFile, ...] + secret_values: dict[str, str] + + +@dataclass(frozen=True) +class PlanComparison: + """The value-free difference between a rendered plan and an existing role.""" + + role_exists: bool + unmanaged: bool + added: tuple[str, ...] + changed: tuple[str, ...] + + @property + def identical(self) -> bool: + """Return whether the existing role matches every planned file byte for byte.""" + return self.role_exists and not self.added and not self.changed + + +@dataclass(frozen=True) +class RoleWriteResult: + """The outcome of placing one rendered role.""" + + role_directory: Path + state: _WriteState + backup_directory: Path | None = None + + +def normalize_remote_role_name(project_name: str) -> str: + """Derive a deterministic valid Ansible role name from a raw Compose project name.""" + role_name = normalize_role_name(project_name) + if not _ROLE_NAME.fullmatch(role_name): + raise GenerationError("remote project name cannot be normalized into an Ansible role name") + return role_name + + +def preview_remote_role( + project: ResolvedProject, role_name: str, *, docker_context: str | None = None +) -> RolePlan: + """Render a source-faithful remote role without writing it.""" + _validate_role_name(role_name) + materialization = materialize_source_role(project, role_name) + return _preview_role(project, role_name, materialization, docker_context) + + +def _preview_role( + project: ResolvedProject, + role_name: str, + materialization: SourceRoleMaterialization, + docker_context: str | None, +) -> RolePlan: + has_secrets = bool(materialization.secret_values) + prerequisites = external_prerequisites(project.canonical_document) + files: list[GeneratedFile] = [ + GeneratedFile( + PurePosixPath("defaults/main.yml"), + _render_source_defaults( + role_name, + materialization, + str(project.source.root), + project.project.name, + docker_context, + ), + ), + GeneratedFile( + PurePosixPath("tasks/main.yml"), + _render_source_tasks( + role_name, + materialization, + tuple(source.relative_path for source in project.source.files), + has_secrets, + docker_context, + ), + ), + GeneratedFile(PurePosixPath("meta/main.yml"), _render_meta(has_secrets)), + GeneratedFile( + PurePosixPath("meta/requirements.yml"), _render_requirements(has_secrets) + ), + GeneratedFile( + PurePosixPath(".compose2role.json"), + _render_provenance(project, role_name, prerequisites, materialization), + ), + ] + files.extend( + GeneratedFile( + PurePosixPath("templates").joinpath(*template.relative_template_path.parts), + template.content, + classified_secret=template.has_secrets, + ) + for template in materialization.templates + ) + return RolePlan( + role_name=role_name, + files=tuple(files), + secret_values=materialization.secret_values, + ) + + +def with_encrypted_secrets(plan: RolePlan, encrypted_secrets: bytes) -> RolePlan: + """Attach real encrypted SOPS output to a rendered remote role plan.""" + if not plan.secret_values: + raise GenerationError("remote role has no secrets to encrypt") + return RolePlan( + role_name=plan.role_name, + files=( + *plan.files, + GeneratedFile( + PurePosixPath("vars/secrets.sops.yml"), + encrypted_secrets, + classified_secret=True, + ), + ), + secret_values=plan.secret_values, + ) + + +def compare_plan(plan: RolePlan, role_directory: Path) -> PlanComparison: + """Compare a rendered plan with an existing role without writing anything.""" + if role_directory.is_symlink() or ( + role_directory.exists() and not role_directory.is_dir() + ): + raise GenerationError(f"existing role path must be a directory: {role_directory}") + planned = _planned_files(plan) + if not role_directory.exists(): + return PlanComparison( + role_exists=False, + unmanaged=False, + added=tuple(sorted(str(path) for path in planned)), + changed=(), + ) + existing = _existing_role_files(role_directory) + return PlanComparison( + role_exists=True, + unmanaged=_PROVENANCE_FILE not in existing, + added=tuple(sorted(str(path) for path in planned if path not in existing)), + changed=tuple( + sorted( + str(path) + for path, content in planned.items() + if path in existing and existing[path] != content + ) + ), + ) + + +def diff_plan(plan: RolePlan, role_directory: Path) -> PlanDiff: + """Compare a rendered plan with an existing role for value-free review.""" + if role_directory.is_symlink() or ( + role_directory.exists() and not role_directory.is_dir() + ): + raise GenerationError(f"existing role path must be a directory: {role_directory}") + planned = { + generated_file.relative_path: generated_file for generated_file in plan.files + } + if not role_directory.exists(): + existing: dict[PurePosixPath, bytes | None] = {} + role_exists = False + unmanaged = False + else: + existing = _existing_role_files(role_directory) + role_exists = True + unmanaged = _PROVENANCE_FILE not in existing + + files: list[FileDiff] = [] + for path in sorted(planned, key=str): + generated_file = planned[path] + content = _generated_file_bytes(generated_file) + if path not in existing: + change: Literal["added", "changed", "unchanged"] = "added" + previous = b"" + elif existing[path] == content: + change = "unchanged" + previous = b"" + else: + change = "changed" + previous = existing[path] or b"" + withheld, withheld_reason = _withheld(generated_file) + if unmanaged: + withheld, withheld_reason = True, None + if withheld: + files.append( + FileDiff( + path=str(path), + change=change, + withheld=True, + withheld_reason=withheld_reason, + ) + ) + elif change == "unchanged": + files.append(FileDiff(path=str(path), change=change)) + else: + files.append( + FileDiff( + path=str(path), + change=change, + old_text=_decode_diff_text(previous), + new_text=_decode_diff_text(content), + ) + ) + return PlanDiff(role_exists=role_exists, unmanaged=unmanaged, files=tuple(files)) + + +_DIFF_SAFE_PATHS = frozenset( + { + "defaults/main.yml", + "tasks/main.yml", + ".compose2role.json", + "README.md", + "site.yml", + "site.yaml", + } +) + + +def _is_safe_diff_path(path: PurePosixPath) -> bool: + if str(path) in _DIFF_SAFE_PATHS: + return True + return bool(path.parts) and path.parts[0] == "meta" + + +def _withheld(generated_file: GeneratedFile) -> tuple[bool, str | None]: + if generated_file.classified_secret: + return True, "classified secret" + if not _is_safe_diff_path(generated_file.relative_path): + return True, None + return False, None + + +def _decode_diff_text(content: bytes) -> str: + return content.decode("utf-8", errors="replace") + + +def write_role( + plan: RolePlan, output_directory: Path, *, force: bool = False +) -> RoleWriteResult: + """Place a rendered role, refusing an existing differing role unless forced.""" + if output_directory.is_symlink() or ( + output_directory.exists() and not output_directory.is_dir() + ): + raise GenerationError("role output must be a directory") + role_directory = output_directory / plan.role_name + if role_directory.is_symlink() or ( + role_directory.exists() and not role_directory.is_dir() + ): + raise GenerationError(f"refusing to overwrite non-directory role path: {role_directory}") + + if not role_directory.exists(): + try: + output_directory.mkdir(parents=True, exist_ok=True) + except OSError as error: + raise GenerationError("role output could not be written") from error + _install_new_role(plan, output_directory, role_directory) + return RoleWriteResult(role_directory, "generated") + + comparison = compare_plan(plan, role_directory) + if comparison.identical: + return RoleWriteResult(role_directory, "unchanged") + if not force: + raise GenerationError(_refusal_message(comparison, role_directory)) + backup_directory = _replace_role(plan, output_directory, role_directory) + return RoleWriteResult(role_directory, "generated", backup_directory=backup_directory) + + +def _refusal_message(comparison: PlanComparison, role_directory: Path) -> str: + if comparison.unmanaged: + return ( + f"refusing to overwrite unmanaged role {role_directory} " + "(no .compose2role.json); use --force to replace it" + ) + return ( + f"refusing to overwrite role {role_directory}: {_describe_changes(comparison)}; " + "use --diff to review or --force to replace it" + ) + + +def _describe_changes(comparison: PlanComparison) -> str: + changes: list[str] = [] + if comparison.added: + changes.append(f"added: {', '.join(comparison.added)}") + if comparison.changed: + changes.append(f"changed: {', '.join(comparison.changed)}") + return "; ".join(changes) if changes else "no file differences" + + +def _planned_files(plan: RolePlan) -> dict[PurePosixPath, bytes]: + return { + generated_file.relative_path: _generated_file_bytes(generated_file) + for generated_file in plan.files + } + + +def _generated_file_bytes(generated_file: GeneratedFile) -> bytes: + if isinstance(generated_file.content, bytes): + return generated_file.content + return generated_file.content.encode("utf-8") + + +def _existing_role_files(role_directory: Path) -> dict[PurePosixPath, bytes | None]: + files: dict[PurePosixPath, bytes | None] = {} + for directory, _, names in os.walk(role_directory, followlinks=False): + for name in names: + path = Path(directory, name) + relative_path = PurePosixPath(path.relative_to(role_directory).as_posix()) + files[relative_path] = _read_existing_file(path) + return files + + +def _read_existing_file(path: Path) -> bytes | None: + try: + if path.is_symlink() or not path.is_file(): + return None + return path.read_bytes() + except OSError: + return None + + +def _install_new_role( + plan: RolePlan, output_directory: Path, role_directory: Path +) -> None: + staging_directory = _stage_plan(plan, output_directory) + try: + if role_directory.exists() or role_directory.is_symlink(): + raise GenerationError(f"refusing to overwrite existing role: {role_directory}") + os.replace(staging_directory, role_directory) + except OSError as error: + raise GenerationError("role output could not be written") from error + finally: + _discard_staging(staging_directory) + + +def _replace_role( + plan: RolePlan, output_directory: Path, role_directory: Path +) -> Path: + staging_directory = _stage_plan(plan, output_directory) + backups_directory = output_directory / ".compose2role" / "backups" + try: + backups_directory.mkdir(parents=True, exist_ok=True) + except OSError as error: + _discard_staging(staging_directory) + raise GenerationError("role output could not be written") from error + backup_directory = backups_directory / f"{plan.role_name}-{_utc_timestamp()}" + try: + os.replace(role_directory, backup_directory) + except OSError as error: + _discard_staging(staging_directory) + raise GenerationError("role output could not be written") from error + try: + os.replace(staging_directory, role_directory) + except OSError as error: + try: + os.replace(backup_directory, role_directory) + except OSError as restore_error: + raise GenerationError( + "role output could not be written and the previous role could not be " + f"restored; backup retained at {backup_directory}" + ) from restore_error + raise GenerationError( + "role output could not be written; previous role restored" + ) from error + finally: + _discard_staging(staging_directory) + return backup_directory + + +def _stage_plan(plan: RolePlan, output_directory: Path) -> Path: + try: + staging_directory = Path( + tempfile.mkdtemp(prefix=f".{plan.role_name}.", dir=output_directory) + ) + except OSError as error: + raise GenerationError("role output could not be written") from error + try: + for generated_file in plan.files: + destination = staging_directory.joinpath(*generated_file.relative_path.parts) + destination.parent.mkdir(parents=True, exist_ok=True) + if isinstance(generated_file.content, bytes): + destination.write_bytes(generated_file.content) + else: + destination.write_text(generated_file.content, encoding="utf-8") + except OSError as error: + _discard_staging(staging_directory) + raise GenerationError("role output could not be written") from error + return staging_directory + + +def _discard_staging(staging_directory: Path) -> None: + if staging_directory.exists(): + shutil.rmtree(staging_directory, ignore_errors=True) + + +def _utc_timestamp() -> str: + return datetime.now(UTC).strftime("%Y%m%dT%H%M%S%fZ") + + +def _validate_role_name(role_name: str) -> None: + if not _ROLE_NAME.fullmatch(role_name): + raise GenerationError("remote role name is invalid") + + +def _render_source_defaults( + role_name: str, + materialization: SourceRoleMaterialization, + project_dir: str, + compose_project_name: str, + docker_context: str | None, +) -> str: + lines = [ + "---", + f"{role_name}_project_dir: {json.dumps(project_dir)}", + f"{role_name}_compose_project_name: {json.dumps(compose_project_name)}", + ] + if docker_context is not None: + lines.append(f"{role_name}_docker_context: {json.dumps(docker_context)}") + for variable in materialization.variables: + if not variable.is_secret: + lines.append(f"{variable.name}: {json.dumps(variable.value)}") + secret_names = [variable.name for variable in materialization.variables if variable.is_secret] + if secret_names: + lines.append(f"{role_name}_required_secrets:") + lines.extend(f" - {name}" for name in secret_names) + lines.append(f'{role_name}_secrets_file: "{{{{ role_path }}}}/vars/secrets.sops.yml"') + return "\n".join(lines) + "\n" + + +def _render_source_tasks( + role_name: str, + materialization: SourceRoleMaterialization, + compose_files: tuple[PurePosixPath, ...], + has_secrets: bool, + docker_context: str | None, +) -> str: + source_files = ( + *(template.source for template in materialization.templates), + *(file_secret.source for file_secret in materialization.file_secrets), + ) + directories = sorted( + { + source.relative_path.parent + for source in source_files + if source.relative_path.parent != PurePosixPath(".") + }, + key=str, + ) + directory_tasks = "" + if directories: + rendered_directories = "\n".join( + f' - "{{{{ {role_name}_project_dir }}}}/{directory}"' for directory in directories + ) + directory_tasks = f"""\ +- name: Create {role_name} source directories + tags: + - source + ansible.builtin.file: + path: "{{{{ item }}}}" + state: directory + mode: "0755" + loop: +{rendered_directories} + +""" + rendered_sources = "\n\n".join( + _render_source_file_task(role_name, template) for template in materialization.templates + ) + rendered_compose_files = "\n".join( + f" - {json.dumps(str(compose_file))}" for compose_file in compose_files + ) + secret_tasks = _render_source_secret_tasks(role_name) if has_secrets else "" + file_secret_tasks = _render_file_secret_tasks(role_name, materialization) + context_line = ( + f' cli_context: "{{{{ {role_name}_docker_context }}}}"\n' + if docker_context is not None + else "" + ) + return f"""\ +--- +- name: Ensure {role_name} Compose project directory exists + tags: + - source + ansible.builtin.file: + path: "{{{{ {role_name}_project_dir }}}}" + state: directory + mode: "0755" + +{secret_tasks}{directory_tasks}{rendered_sources} + +{file_secret_tasks}- name: Deploy {role_name} Compose project + tags: + - compose + community.docker.docker_compose_v2: + project_src: "{{{{ {role_name}_project_dir }}}}" + project_name: "{{{{ {role_name}_compose_project_name }}}}" +{context_line} files: +{rendered_compose_files} + state: present + pull: missing + recreate: auto + remove_orphans: false +""" + + +def _render_source_file_task(role_name: str, template: SourceTemplate) -> str: + """Render one captured source file through a literal template task.""" + relative_path = template.source.relative_path + metadata = template.source.metadata + destination = f"{{{{ {role_name}_project_dir }}}}/{relative_path}" + return f"""\ +- name: {json.dumps(f"Render {relative_path}")} + tags: + - source + ansible.builtin.template: + src: {json.dumps(str(template.relative_template_path))} + dest: {json.dumps(destination)} + owner: "{metadata.uid}" + group: "{metadata.gid}" + mode: "{metadata.mode:04o}" + newline_sequence: {json.dumps(template.newline_sequence)} +""" + + +def _render_source_secret_tasks(role_name: str) -> str: + return f"""\ +- name: Load {role_name} SOPS secrets + tags: + - secrets + community.sops.load_vars: + file: "{{{{ {role_name}_secrets_file }}}}" + name: {role_name}_secrets + no_log: true + +- name: Assert {role_name} required secrets are present + tags: + - secrets + ansible.builtin.assert: + that: item in {role_name}_secrets + fail_msg: "Missing secret: {{{{ item }}}}" + loop: "{{{{ {role_name}_required_secrets }}}}" + loop_control: + label: "{{{{ item }}}}" + +""" + + +def _render_file_secret_tasks( + role_name: str, materialization: SourceRoleMaterialization +) -> str: + if not materialization.file_secrets: + return "" + rendered_secrets = "\n".join( + ( + f' - path: {json.dumps(str(file_secret.source.relative_path))}\n' + f' key: {json.dumps(file_secret.variable_name)}\n' + f' owner: {json.dumps(str(file_secret.source.metadata.uid))}\n' + f' group: {json.dumps(str(file_secret.source.metadata.gid))}\n' + f' mode: "{file_secret.source.metadata.mode:04o}"' + ) + for file_secret in materialization.file_secrets + ) + return f"""\ +- name: Render {role_name} file-backed Compose secrets + tags: + - secrets + ansible.builtin.shell: + cmd: | + set -eu + target="{{{{ ({role_name}_project_dir ~ '/' ~ item.path) | quote }}}}" + owner="{{{{ item.owner }}}}" + group="{{{{ item.group }}}}" + mode="{{{{ item.mode }}}}" + if [ -L "$target" ] || [ -d "$target" ]; then + printf '%s\\n' 'unsafe file-secret target' >&2 + exit 4 + fi + temporary=$(mktemp "$target.compose2role.XXXXXX") + trap 'rm -f "$temporary"' EXIT + base64 --decode > "$temporary" + target_state="$(stat -c '%a:%u:%g' "$target" 2>/dev/null || true)" + expected_mode="$(printf '%o' "$((0$mode))")" + expected_state="$expected_mode:$owner:$group" + if [ -f "$target" ] && cmp -s "$temporary" "$target"; then + if [ "$target_state" = "$expected_state" ]; then + exit 0 + fi + fi + install -o "$owner" -g "$group" -m "$mode" "$temporary" "$target" + exit 3 + stdin: "{{{{ {role_name}_secrets[item.key] }}}}" + stdin_add_newline: false + register: {role_name}_file_secret_result + changed_when: {role_name}_file_secret_result.rc == 3 + failed_when: {role_name}_file_secret_result.rc not in [0, 3] + no_log: true + loop: +{rendered_secrets} + loop_control: + label: "{{{{ item.path }}}}" + +""" + + +def _render_meta(has_secrets: bool) -> str: + collections = [" - community.docker"] + if has_secrets: + collections.append(" - community.sops") + return "\n".join(("---", "collections:", *collections, "dependencies: []", "")) + + +def _render_requirements(has_secrets: bool) -> str: + collections = [" - name: community.docker", " version: 5.4.0"] + if has_secrets: + collections.extend((" - name: community.sops", " version: 2.5.0")) + return "\n".join(("---", "collections:", *collections, "")) + + +def _render_provenance( + project: ResolvedProject, + role_name: str, + prerequisites: tuple[tuple[str, str], ...], + materialization: SourceRoleMaterialization, +) -> str: + redacted_document = _redact_environment_values(project.canonical_document) + canonical_json = json.dumps(redacted_document, sort_keys=True, separators=(",", ":")) + config_files = list(project.project.config_files) + source_root = str(project.source.root) + provenance = { + "external_prerequisites": [ + {"kind": kind, "name": name} for kind, name in prerequisites + ], + "files": _source_file_hashes(project, materialization), + "generator": "compose2role", + "mode": "remote", + "project": project.project.name, + "role": role_name, + "source": {"config_files": config_files, "root": source_root}, + "structural_hash": hashlib.sha256(canonical_json.encode("utf-8")).hexdigest(), + "version": version("compose2role"), + } + return json.dumps(provenance, indent=2, sort_keys=True) + "\n" + + +def _source_file_hashes( + project: ResolvedProject, materialization: SourceRoleMaterialization +) -> dict[str, dict[str, object]]: + secret_paths = { + template.source.relative_path + for template in materialization.templates + if template.has_secrets + } + entries: dict[str, dict[str, object]] = {} + for source in project.source.files: + if source.relative_path in secret_paths: + entries[str(source.relative_path)] = { + "redacted": True, + "sha256": normalized_compose_hash(source.content), + } + else: + entries[str(source.relative_path)] = { + "redacted": False, + "sha256": hashlib.sha256(source.content).hexdigest(), + } + for source in project.source.dotenv_files: + if source.relative_path in secret_paths: + entries[str(source.relative_path)] = { + "redacted": True, + "sha256": normalized_dotenv_hash(source.content), + } + else: + entries[str(source.relative_path)] = { + "redacted": False, + "sha256": hashlib.sha256(source.content).hexdigest(), + } + for file_secret in project.source.file_secrets: + entries[str(file_secret.source.relative_path)] = { + "redacted": True, + "sha256": normalized_file_secret_hash(file_secret.source.metadata), + } + return entries + + +def normalized_compose_hash(content: bytes) -> str: + try: + document = YAML(typ="rt").load(content.decode("utf-8")) + except (UnicodeDecodeError, YAMLError): + return _hash_json({"redacted": "compose"}) + return _hash_json(_redact_environment_values(document)) + + +def normalized_dotenv_hash(content: bytes) -> str: + try: + text = content.decode("utf-8") + except UnicodeDecodeError: + return _hash_json({"redacted": "dotenv"}) + keys = [ + match.group(1) + for line in text.splitlines() + if (match := _DOTENV_KEY.match(line)) is not None + ] + return _hash_json({"dotenv_keys": keys}) + + +def normalized_file_secret_hash(metadata: FileMetadata) -> str: + return _hash_json( + { + "content": "", + "gid": metadata.gid, + "mode": metadata.mode, + "uid": metadata.uid, + } + ) + + +def _hash_json(value: object) -> str: + encoded = json.dumps( + value, sort_keys=True, separators=(",", ":"), default=str + ).encode("utf-8") + return hashlib.sha256(encoded).hexdigest() + + +_DOTENV_KEY = re.compile(r"[ \t]*(?:export[ \t]+)?([A-Za-z_][A-Za-z0-9_]*)[ \t]*=") + + +def _redact_environment_values(value: object, *, redact_values: bool = False) -> object: + if isinstance(value, dict): + return { + key: "" + if redact_values + else _redact_environment_values(item, redact_values=key == "environment") + for key, item in value.items() + } + if isinstance(value, list): + if redact_values: + return ["" for _ in value] + return [_redact_environment_values(item) for item in value] + if redact_values: + return "" + return value + + +def external_prerequisites(document: dict[str, object]) -> tuple[tuple[str, str], ...]: + """Return external Compose configs and secrets declared by the canonical model.""" + prerequisites: list[tuple[str, str]] = [] + for kind, key in (("config", "configs"), ("secret", "secrets")): + value = document.get(key, {}) + definitions = _mapping(value, "remote Compose model has invalid external resources") + for name, definition in definitions.items(): + definition_mapping = _mapping( + definition, "remote Compose model has invalid external resources" + ) + if definition_mapping.get("external") is True: + prerequisites.append((kind, name)) + return tuple(prerequisites) + + +def _mapping(value: object, message: str) -> dict[str, object]: + if not isinstance(value, dict): + raise GenerationError(message) + for key in value: + if not isinstance(key, str): + raise GenerationError(message) + return value diff --git a/src/compose2role/remote_generate.py b/src/compose2role/remote_generate.py new file mode 100644 index 0000000..9533b34 --- /dev/null +++ b/src/compose2role/remote_generate.py @@ -0,0 +1,215 @@ +"""Orchestrate safe, isolated remote role generation.""" + +from __future__ import annotations + +from collections.abc import Callable +from dataclasses import dataclass +from pathlib import Path + +from compose2role.emit import ( + RolePlan, + diff_plan, + normalize_remote_role_name, + preview_remote_role, + with_encrypted_secrets, + write_role, +) +from compose2role.errors import GenerationError +from compose2role.remote import resolve_remote_projects +from compose2role.resolve import ( + GeneratedProject, + GenerationFailure, + GenerationOutcome, + GenerationResult, + PlanDiff, + ProjectFailure, + ResolvedProject, +) +from compose2role.secret_policy import SecretPolicy +from compose2role.sops import encrypt_secret_values, resolve_sops_configuration + + +def generate_remote_projects( + host: str, + output_directory: Path, + *, + project_name: str | None, + all_projects: bool, + use_sudo: bool, + secret_policy: SecretPolicy | None, + sops_config_path: Path | None, + check_only: bool, + diff_only: bool = False, + force: bool = False, + docker_context: str | None = None, + on_outcome: Callable[[GenerationOutcome], None] | None = None, +) -> GenerationResult: + """Generate selected remote projects while isolating individual project failures.""" + sops_configuration = resolve_sops_configuration(sops_config_path) + resolution = resolve_remote_projects( + host, + project_name=project_name, + all_projects=all_projects, + use_sudo=use_sudo, + secret_policy=secret_policy, + docker_context=docker_context, + ) + prepared = _prepare_projects(resolution.projects, docker_context) + _mark_output_conflicts(prepared, output_directory) + + outcomes: list[GenerationOutcome] = [] + + def record(outcome: GenerationOutcome) -> None: + outcomes.append(outcome) + if on_outcome is not None: + on_outcome(outcome) + + for outcome in resolution.outcomes: + if isinstance(outcome, ProjectFailure): + record( + GenerationFailure( + name=outcome.name, + role_name=None, + category=outcome.category, + message=outcome.message, + ) + ) + continue + prepared_project = prepared[outcome.project.name] + if isinstance(prepared_project, GenerationFailure): + record(prepared_project) + continue + if check_only: + record( + GeneratedProject( + project=outcome, + role_name=prepared_project.role_name, + checked=True, + files=_plan_files(prepared_project.plan), + ) + ) + continue + plan = prepared_project.plan + if plan.secret_values: + try: + encrypted_secrets = encrypt_secret_values(sops_configuration, plan.secret_values) + plan = with_encrypted_secrets(plan, encrypted_secrets) + except GenerationError as error: + record( + _generation_failure(outcome, prepared_project.role_name, "sops", str(error)) + ) + continue + plan_diff: PlanDiff | None = None + if diff_only: + plan_diff = diff_plan(plan, output_directory / prepared_project.role_name) + if not force: + record( + GeneratedProject( + project=outcome, + role_name=prepared_project.role_name, + checked=False, + files=_plan_files(plan), + unchanged=plan_diff.identical, + diff=plan_diff, + diff_only=True, + ) + ) + continue + try: + write_result = write_role(plan, output_directory, force=force) + except GenerationError as error: + record( + _generation_failure(outcome, prepared_project.role_name, "output", str(error)) + ) + continue + record( + GeneratedProject( + project=outcome, + role_name=prepared_project.role_name, + checked=False, + files=_plan_files(plan), + unchanged=write_result.state == "unchanged", + diff=plan_diff, + ) + ) + return GenerationResult(outcomes=tuple(outcomes)) + + +@dataclass(frozen=True) +class _PreparedProject: + """A role plan ready for collision checking and optional output.""" + + project: ResolvedProject + role_name: str + plan: RolePlan + + +def _plan_files(plan: RolePlan) -> tuple[str, ...]: + return tuple(str(generated_file.relative_path) for generated_file in plan.files) + + +def _prepare_projects( + projects: tuple[ResolvedProject, ...], docker_context: str | None +) -> dict[str, _PreparedProject | GenerationFailure]: + prepared: dict[str, _PreparedProject | GenerationFailure] = {} + for project in projects: + try: + role_name = normalize_remote_role_name(project.project.name) + plan = preview_remote_role( + project, role_name, docker_context=docker_context + ) + except GenerationError as error: + prepared[project.project.name] = _generation_failure( + project, None, "generation", str(error) + ) + else: + prepared[project.project.name] = _PreparedProject(project, role_name, plan) + return prepared + + +def _mark_output_conflicts( + prepared: dict[str, _PreparedProject | GenerationFailure], output_directory: Path +) -> None: + if output_directory.is_symlink() or ( + output_directory.exists() and not output_directory.is_dir() + ): + for project_name, value in prepared.items(): + if isinstance(value, _PreparedProject): + prepared[project_name] = _generation_failure( + value.project, + value.role_name, + "output", + "remote role output must be a directory", + ) + return + + by_role_name: dict[str, list[_PreparedProject]] = {} + for value in prepared.values(): + if isinstance(value, _PreparedProject): + by_role_name.setdefault(value.role_name, []).append(value) + for candidates in by_role_name.values(): + if len(candidates) > 1: + for candidate in candidates: + prepared[candidate.project.project.name] = _generation_failure( + candidate.project, + candidate.role_name, + "output", + "normalized remote role name collides with another selected project", + ) + continue + + +def _generation_failure( + project: ResolvedProject, + role_name: str | None, + category: str, + message: str, +) -> GenerationFailure: + return GenerationFailure( + name=project.project.name, + role_name=role_name, + category=category, + message=message, + findings=project.findings, + classifications=project.classifications, + ) diff --git a/src/compose2role/resolve.py b/src/compose2role/resolve.py new file mode 100644 index 0000000..efaf7e8 --- /dev/null +++ b/src/compose2role/resolve.py @@ -0,0 +1,862 @@ +"""Shared project model, capture seam, and resolution pipeline.""" + +from __future__ import annotations + +from dataclasses import dataclass, field +from pathlib import PurePosixPath +from typing import Literal, Protocol + +from ruamel.yaml import YAML +from ruamel.yaml.comments import CommentedMap, CommentedSeq +from ruamel.yaml.error import YAMLError + +from compose2role.discover import DiscoveredProject +from compose2role.errors import Compose2RoleError, ComposeParseError +from compose2role.findings import Finding, audit_stack, classified_secret_finding +from compose2role.model import Stack, parse_canonical_stack +from compose2role.secret_policy import SecretPolicy +from compose2role.secrets import EnvironmentClassification, classify_environment, is_secret_key +from compose2role.source import FileMetadata + +_UNSUPPORTED_DOCUMENT_FEATURES = { + "include": "C2R008: remote Compose include files are not supported", +} +_UNSUPPORTED_SERVICE_FEATURES = { + "build": "C2R005: remote build contexts are not supported", + "extends": "C2R006: remote service extends declarations are not supported", + "profiles": "C2R007: remote service profiles are not supported", +} + + +@dataclass(frozen=True) +class SourceFile: + """One raw, root-relative source file retained only in controller memory.""" + + relative_path: PurePosixPath + content: bytes = field(repr=False) + metadata: FileMetadata + + +@dataclass(frozen=True) +class FileSecret: + """One file-backed Compose secret retained only in controller memory.""" + + name: str + source: SourceFile = field(repr=False) + + +@dataclass(frozen=True) +class ProjectSnapshot: + """An ordered source snapshot for a root-contained Compose project.""" + + root: PurePosixPath + files: tuple[SourceFile, ...] = field(repr=False) + dotenv_files: tuple[SourceFile, ...] = field(default=(), repr=False) + file_secrets: tuple[FileSecret, ...] = field(default=(), repr=False) + + +@dataclass(frozen=True) +class ResolvedProject: + """One project resolved to canonical and source-faithful representations.""" + + project: DiscoveredProject + stack: Stack + canonical_document: dict[str, object] + findings: tuple[Finding, ...] + classifications: tuple[EnvironmentClassification, ...] + source: ProjectSnapshot = field(repr=False) + secret_policy: SecretPolicy | None = field(default=None, repr=False) + drift: ProjectDrift | None = field(default=None, repr=False) + + def as_json(self) -> dict[str, object]: + """Return the value-free audit JSON envelope for a successful project.""" + return { + "classifications": [ + classification.as_json() + for classification in self.classifications + if classification.is_secret + ], + "drift": None if self.drift is None else self.drift.as_json(), + "error": None, + "findings": [finding.as_json() for finding in self.findings], + "name": self.project.name, + "role": None, + "status": "ok", + } + + +@dataclass(frozen=True) +class ProjectFailure: + """A value-free operational failure for one project.""" + + name: str + category: str + message: str + + def as_json(self) -> dict[str, object]: + """Return the value-free audit JSON envelope for a failed project.""" + return { + "classifications": [], + "drift": None, + "error": self.message, + "findings": [], + "name": self.name, + "role": None, + "status": "failed", + } + + +type AuditOutcome = ResolvedProject | ProjectFailure + + +@dataclass(frozen=True) +class AuditResult: + """Audit successes and isolated per-project failures.""" + + projects: tuple[ResolvedProject, ...] + failures: tuple[ProjectFailure, ...] + outcomes: tuple[AuditOutcome, ...] + + def as_json(self) -> dict[str, list[dict[str, object]]]: + """Return the batch JSON envelope used by audit.""" + return {"projects": [outcome.as_json() for outcome in self.outcomes]} + + +type DiffState = Literal["added", "changed", "unchanged", "withheld"] + +type DriftState = Literal["unchanged", "source changed", "unmanaged", "missing"] + + +@dataclass(frozen=True) +class ProjectDrift: + """The value-free drift state of one project against its generated role.""" + + state: DriftState + role: str + changed: tuple[str, ...] = () + reason: str | None = None + + def as_json(self) -> dict[str, object]: + """Return paths and reasons only, never source content.""" + payload: dict[str, object] = { + "changed": list(self.changed), + "role": self.role, + "state": self.state, + } + if self.reason is not None: + payload["reason"] = self.reason + return payload + + +@dataclass(frozen=True) +class FileDiff: + """One value-free comparison result for a planned role file.""" + + path: str + change: Literal["added", "changed", "unchanged"] + withheld: bool = False + withheld_reason: str | None = None + old_text: str | None = field(default=None, repr=False) + new_text: str | None = field(default=None, repr=False) + + @property + def state(self) -> DiffState: + """Return the reported state, preferring the withheld category.""" + if self.withheld and self.change != "unchanged": + return "withheld" + return self.change + + +@dataclass(frozen=True) +class PlanDiff: + """The value-free difference between a rendered plan and an existing role.""" + + role_exists: bool + unmanaged: bool + files: tuple[FileDiff, ...] + + @property + def identical(self) -> bool: + """Return whether every planned file is unchanged.""" + return self.role_exists and all(file.state == "unchanged" for file in self.files) + + def as_json(self) -> dict[str, list[str]]: + """Return path-only diff categories; content is never included.""" + grouped: dict[str, list[str]] = { + "added": [], + "changed": [], + "unchanged": [], + "withheld": [], + } + for file in self.files: + grouped[file.state].append(file.path) + return grouped + + +@dataclass(frozen=True) +class GeneratedProject: + """One project successfully generated or checked.""" + + project: ResolvedProject + role_name: str + checked: bool + files: tuple[str, ...] + unchanged: bool = False + diff: PlanDiff | None = None + diff_only: bool = False + + @property + def name(self) -> str: + """Return the raw Compose project name.""" + return self.project.project.name + + def as_json(self) -> dict[str, object]: + """Return a value-free generation summary for this project.""" + if self.checked: + status = "checked" + elif self.diff is not None and self.diff_only: + status = "unchanged" if self.diff.identical else "diff" + elif self.unchanged: + status = "unchanged" + else: + status = "generated" + summary: dict[str, object] = { + "classifications": [ + classification.as_json() + for classification in self.project.classifications + if classification.is_secret + ], + "error": None, + "findings": [finding.as_json() for finding in self.project.findings], + "name": self.name, + "role": self.role_name, + "status": status, + } + if self.checked: + summary["files"] = list(self.files) + if self.diff is not None: + summary["diff"] = self.diff.as_json() + return summary + + +@dataclass(frozen=True) +class GenerationFailure: + """A value-free generation failure for one project.""" + + name: str + role_name: str | None + category: str + message: str + findings: tuple[Finding, ...] = () + classifications: tuple[EnvironmentClassification, ...] = () + + def as_json(self) -> dict[str, object]: + """Return a value-free generation summary for this failed project.""" + return { + "classifications": [ + classification.as_json() + for classification in self.classifications + if classification.is_secret + ], + "error": self.message, + "findings": [finding.as_json() for finding in self.findings], + "name": self.name, + "role": self.role_name, + "status": "failed", + } + + +type GenerationOutcome = GeneratedProject | GenerationFailure + + +@dataclass(frozen=True) +class GenerationResult: + """Ordered generated and failed project outcomes.""" + + outcomes: tuple[GenerationOutcome, ...] + + @property + def failures(self) -> tuple[GenerationFailure, ...]: + """Return every project that did not generate successfully.""" + return tuple( + outcome for outcome in self.outcomes if isinstance(outcome, GenerationFailure) + ) + + def as_json(self) -> dict[str, list[dict[str, object]]]: + """Return the generation batch envelope.""" + return {"projects": [outcome.as_json() for outcome in self.outcomes]} + + +class ProjectResolutionError(Compose2RoleError): + """A value-free error while resolving a particular project.""" + + def __init__(self, category: str, message: str) -> None: + super().__init__(message) + self.category = category + + +class ProjectCapture(Protocol): + """A transport that captures root-contained source files.""" + + def capture(self, root: PurePosixPath, path: PurePosixPath) -> SourceFile | str: + """Capture PATH and its metadata, or return its source-boundary state.""" + ... + + def stat(self, root: PurePosixPath, path: PurePosixPath) -> str: + """Return the source-boundary state of PATH without reading its bytes.""" + ... + + def close(self) -> None: + """Finish the capture session.""" + ... + + def abort(self) -> None: + """Discard the capture session without replacing the original error.""" + ... + + +@dataclass(frozen=True) +class CanonicalResult: + """The effective Compose document plus an optional source-located typed stack.""" + + document: dict[str, object] + stack: Stack | None = None + + +class Canonicalizer(Protocol): + """A transport that produces the effective Compose document.""" + + def canonicalize( + self, + identity: DiscoveredProject, + root: PurePosixPath, + files: tuple[PurePosixPath, ...], + ) -> CanonicalResult: + """Return the canonical Compose document for the captured project.""" + ... + + +@dataclass(frozen=True) +class _EnvironmentFile: + """A service env_file reference that must remain within the project root.""" + + path: PurePosixPath + required: bool + + +@dataclass(frozen=True) +class _FileSecretInput: + """A contained source file referenced by one Compose secret declaration.""" + + name: str + path: PurePosixPath + + +def resolve_project( + capture: ProjectCapture, + canonicalizer: Canonicalizer, + identity: DiscoveredProject, + secret_policy: SecretPolicy | None, + *, + extra_dotenv_paths: tuple[PurePosixPath, ...] = (), +) -> ResolvedProject: + """Capture, canonicalize, classify, and audit one root-contained project.""" + root = project_root(identity) + try: + source = _capture_source_snapshot(capture, identity, root) + source_documents = _source_documents(source) + _validate_source_documents(source_documents) + environment_files = _environment_files(source_documents, root) + dotenv_files, file_secrets = _capture_dependent_inputs( + capture, + root, + environment_files, + _file_secret_inputs(source_documents, root), + extra_dotenv_paths, + ) + except Exception: + capture.abort() + raise + else: + capture.close() + source = ProjectSnapshot( + root=source.root, + files=source.files, + dotenv_files=dotenv_files, + file_secrets=file_secrets, + ) + canonical = canonicalizer.canonicalize( + identity, root, tuple(file.relative_path for file in source.files) + ) + canonical_document = canonical.document + if canonical.stack is not None: + stack = canonical.stack + else: + try: + stack = parse_canonical_stack( + canonical_document, + source_path=PurePosixPath("remote-compose.json"), + ) + except ComposeParseError as error: + raise ProjectResolutionError( + "invalid-model", "remote Docker Compose returned an unsupported effective model" + ) from error + _validate_canonical_topology(canonical_document) + classifications = tuple( + classify_environment( + identity.name, + service.name, + environment.name, + environment.value, + environment.location, + secret_policy, + ) + for service in stack.services + for environment in service.environment + ) + inline_secret_entries = { + (service.name, environment.name) + for service in stack.services + for environment in service.environment + if environment.is_secret + } + findings = ( + *audit_stack(stack), + *( + classified_secret_finding( + classification.service, + classification.key, + classification.reason, + classification.location, + ) + for classification in classifications + if classification.is_secret + and (classification.service, classification.key) not in inline_secret_entries + ), + ) + return ResolvedProject( + project=identity, + stack=stack, + canonical_document=canonical_document, + findings=findings, + classifications=classifications, + source=source, + secret_policy=secret_policy, + ) + + +def project_root(project: DiscoveredProject) -> PurePosixPath: + """Return the root that contains every declared Compose file.""" + if not project.config_files: + raise ProjectResolutionError("source", "the remote project has no Compose files") + _safe_line(project.name, "project name") + config_files = tuple(_safe_remote_path(path) for path in project.config_files) + if len(project.working_directories) > 1: + raise ProjectResolutionError( + "source", "remote Compose project has conflicting working-directory labels" + ) + root = ( + _safe_project_root(project.working_directories[0]) + if project.working_directories + else config_files[0].parent + ) + for path in config_files: + relative_source_path(root, path) + return root + + +def _validate_canonical_topology(document: dict[str, object]) -> None: + services = _canonical_mapping( + document.get("services"), "remote Docker Compose returned an unsupported effective model" + ) + for service in services.values(): + service_mapping = _canonical_mapping( + service, "remote Docker Compose returned an unsupported effective model" + ) + _canonical_volumes(service_mapping) + + +def _canonical_volumes(service: dict[str, object]) -> tuple[dict[str, object], ...]: + volumes = service.get("volumes", []) + if not isinstance(volumes, list): + raise ProjectResolutionError( + "invalid-model", "remote Docker Compose returned an unsupported effective model" + ) + return tuple( + _canonical_mapping( + volume, "remote Docker Compose returned an unsupported effective model" + ) + for volume in volumes + ) + + +def _canonical_mapping(value: object, message: str) -> dict[str, object]: + if not isinstance(value, dict): + raise ProjectResolutionError("invalid-model", message) + mapping: dict[str, object] = {} + for key, item in value.items(): + if not isinstance(key, str): + raise ProjectResolutionError("invalid-model", message) + mapping[key] = item + return mapping + + +def _safe_remote_path(path: str) -> PurePosixPath: + _safe_line(path, "Compose file path") + parsed_path = PurePosixPath(path) + if not parsed_path.is_absolute(): + raise ProjectResolutionError("source", "remote Compose file paths must be absolute") + if ".." in parsed_path.parts: + raise ProjectResolutionError("source", "remote Compose file path escapes the project root") + return parsed_path + + +def _safe_project_root(path: str) -> PurePosixPath: + _safe_line(path, "Compose project working directory") + parsed_path = PurePosixPath(path) + if not parsed_path.is_absolute(): + raise ProjectResolutionError( + "source", "remote Compose project working directory must be absolute" + ) + if ".." in parsed_path.parts: + raise ProjectResolutionError( + "source", "remote Compose project working directory escapes the project root" + ) + return parsed_path + + +def _safe_line(value: str, description: str) -> None: + if not value or any(ord(character) < 32 or ord(character) == 127 for character in value): + raise ProjectResolutionError( + "source", f"remote {description} contains unsupported characters" + ) + + +def relative_source_path(root: PurePosixPath, path: PurePosixPath) -> PurePosixPath: + """Return PATH relative to ROOT, rejecting an escape.""" + try: + return path.relative_to(root) + except ValueError as error: + raise ProjectResolutionError( + "source", "remote Compose file path escapes the project root" + ) from error + + +def _capture_source_snapshot( + capture: ProjectCapture, + project: DiscoveredProject, + root: PurePosixPath, +) -> ProjectSnapshot: + paths = tuple(_safe_remote_path(path) for path in project.config_files) + files = tuple(_required_source_file(capture.capture(root, path)) for path in paths) + return ProjectSnapshot(root=root, files=files) + + +def _required_source_file(capture: SourceFile | str) -> SourceFile: + if isinstance(capture, SourceFile): + return capture + if capture == "escape": + raise ProjectResolutionError("source", "remote Compose file path escapes the project root") + if capture == "symlink": + raise ProjectResolutionError("source", "remote Compose source file must not be a symlink") + raise ProjectResolutionError("source", "remote Compose source file is not a regular file") + + +def _captured_input_file(capture: SourceFile | str, message: str) -> SourceFile: + if isinstance(capture, SourceFile): + return capture + raise ProjectResolutionError("source", message) + + +def _source_documents(source: ProjectSnapshot) -> tuple[dict[str, object], ...]: + documents: list[dict[str, object]] = [] + for file in source.files: + try: + document: object = YAML(typ="rt").load(file.content.decode("utf-8")) + except (UnicodeDecodeError, YAMLError) as error: + raise ProjectResolutionError( + "source", "remote Compose source could not be parsed safely" + ) from error + _validate_source_tags(document) + documents.append(_source_mapping(document)) + return tuple(documents) + + +def _source_mapping(value: object) -> dict[str, object]: + if not isinstance(value, dict): + raise ProjectResolutionError("source", "remote Compose source must be a mapping") + mapping: dict[str, object] = {} + for key, item in value.items(): + if not isinstance(key, str): + raise ProjectResolutionError( + "source", "remote Compose source has invalid mapping keys" + ) + mapping[key] = item + return mapping + + +def _validate_source_tags(value: object) -> None: + tag = getattr(value, "tag", None) + tag_value = getattr(tag, "value", None) + if tag_value is not None and ( + tag_value not in {"!override", "!reset"} + or not isinstance(value, (CommentedMap, CommentedSeq)) + ): + raise ProjectResolutionError( + "source", "remote Compose source has an unsupported YAML tag" + ) + if isinstance(value, dict): + for key, item in value.items(): + _validate_source_tags(key) + _validate_source_tags(item) + elif isinstance(value, list): + for item in value: + _validate_source_tags(item) + + +def _environment_files( + documents: tuple[dict[str, object], ...], root: PurePosixPath +) -> tuple[_EnvironmentFile, ...]: + environment_files: list[_EnvironmentFile] = [] + for document in documents: + services = _source_mapping(document.get("services", {})) + for service in services.values(): + service_mapping = _source_mapping(service) + if "env_file" not in service_mapping: + continue + environment_files.extend( + _parse_environment_files(service_mapping["env_file"], root) + ) + return tuple(environment_files) + + +def _validate_source_documents(documents: tuple[dict[str, object], ...]) -> None: + for document in documents: + for feature, message in _UNSUPPORTED_DOCUMENT_FEATURES.items(): + if feature in document: + raise ProjectResolutionError("source", message) + _validate_file_backed_configs(document) + _reject_non_environment_secret_mapping_keys(document) + services = _source_mapping(document.get("services", {})) + for service in services.values(): + service_mapping = _source_mapping(service) + for feature, message in _UNSUPPORTED_SERVICE_FEATURES.items(): + if feature in service_mapping: + raise ProjectResolutionError("source", message) + + +def _validate_file_backed_configs(document: dict[str, object]) -> None: + definitions = _source_mapping(document.get("configs", {})) + for definition in definitions.values(): + definition_mapping = _source_mapping(definition) + if "file" in definition_mapping: + raise ProjectResolutionError( + "topology", "C2R027: remote file-backed Compose configs are not supported" + ) + + +def _reject_non_environment_secret_mapping_keys(document: dict[str, object]) -> None: + _walk_source_mappings(document, scan_keys=False) + + +def _walk_source_mappings(value: object, *, scan_keys: bool) -> None: + if isinstance(value, dict): + for key, item in value.items(): + if not isinstance(key, str): + raise ProjectResolutionError( + "source", "remote Compose source has invalid mapping keys" + ) + if scan_keys and is_secret_key(key): + raise ProjectResolutionError( + "topology", + "C2R029: remote non-environment secret-shaped mapping keys are not supported", + ) + if key == "environment": + continue + if key in {"services", "configs", "secrets", "networks", "volumes", "depends_on"}: + _walk_source_identity_mapping(item) + else: + _walk_source_mappings(item, scan_keys=True) + elif isinstance(value, list): + for item in value: + _walk_source_mappings(item, scan_keys=scan_keys) + + +def _walk_source_identity_mapping(value: object) -> None: + if not isinstance(value, dict): + _walk_source_mappings(value, scan_keys=True) + return + for key, item in value.items(): + if not isinstance(key, str): + raise ProjectResolutionError( + "source", "remote Compose source has invalid mapping keys" + ) + _walk_source_mappings(item, scan_keys=False) + + +def _parse_environment_files(value: object, root: PurePosixPath) -> tuple[_EnvironmentFile, ...]: + if isinstance(value, (str, dict)): + values: list[object] = [value] + elif isinstance(value, list): + values = value + else: + raise ProjectResolutionError("source", "remote service env_file entries are invalid") + + environment_files: list[_EnvironmentFile] = [] + for item in values: + required = True + if isinstance(item, str): + reference = item + elif isinstance(item, dict): + item_mapping = _source_mapping(item) + reference_value = item_mapping.get("path") + required_value = item_mapping.get("required", True) + if not isinstance(reference_value, str) or not isinstance(required_value, bool): + raise ProjectResolutionError( + "source", "remote service env_file entries are invalid" + ) + reference = reference_value + required = required_value + else: + raise ProjectResolutionError("source", "remote service env_file entries are invalid") + environment_files.append(_EnvironmentFile(_contained_path(root, reference), required)) + return tuple(environment_files) + + +def _contained_path( + root: PurePosixPath, reference: str, *, subject: str = "environment file" +) -> PurePosixPath: + _safe_line(reference, f"{subject} path") + relative_path = PurePosixPath(reference) + if relative_path.is_absolute() or ".." in relative_path.parts: + raise ProjectResolutionError("source", f"remote {subject} path escapes the project root") + path = root.joinpath(*relative_path.parts) + try: + path.relative_to(root) + except ValueError as error: + raise ProjectResolutionError( + "source", f"remote {subject} path escapes the project root" + ) from error + return path + + +def _file_secret_inputs( + documents: tuple[dict[str, object], ...], root: PurePosixPath +) -> tuple[_FileSecretInput, ...]: + inputs: list[_FileSecretInput] = [] + for document in documents: + definitions = _source_mapping(document.get("secrets", {})) + for name, definition in definitions.items(): + if not isinstance(name, str) or not name: + raise ProjectResolutionError("source", "remote Compose secret names are invalid") + definition_mapping = _source_mapping(definition) + file_value = definition_mapping.get("file") + if file_value is None: + continue + if not isinstance(file_value, str) or not file_value: + raise ProjectResolutionError( + "source", "remote file-backed Compose secret input is invalid" + ) + inputs.append( + _FileSecretInput( + name=name, + path=_contained_path( + root, file_value, subject="file-backed Compose secret" + ), + ) + ) + return tuple(inputs) + + +def _capture_dependent_inputs( + capture: ProjectCapture, + root: PurePosixPath, + environment_files: tuple[_EnvironmentFile, ...], + secret_inputs: tuple[_FileSecretInput, ...], + extra_dotenv_paths: tuple[PurePosixPath, ...], +) -> tuple[tuple[SourceFile, ...], tuple[FileSecret, ...]]: + root_dotenv_path = root / ".env" + dotenv_files: list[SourceFile] = [] + captured_dotenv_paths: set[PurePosixPath] = set() + root_dotenv_state = capture.stat(root, root_dotenv_path) + if root_dotenv_state == "file": + dotenv_files.append( + _captured_input_file( + capture.capture(root, root_dotenv_path), + "remote root dotenv input is not a regular file", + ) + ) + captured_dotenv_paths.add(root_dotenv_path) + elif root_dotenv_state == "escape": + raise ProjectResolutionError( + "source", "remote environment file path escapes the project root" + ) + elif root_dotenv_state != "missing": + raise ProjectResolutionError("source", "remote root dotenv input is not a regular file") + + for extra_path in extra_dotenv_paths: + absolute_path = root.joinpath(*extra_path.parts) + if absolute_path in captured_dotenv_paths: + continue + state = capture.stat(root, absolute_path) + if state == "file": + dotenv_files.append( + _captured_input_file( + capture.capture(root, absolute_path), + "remote environment file input is not a regular file", + ) + ) + captured_dotenv_paths.add(absolute_path) + continue + if state == "escape": + raise ProjectResolutionError( + "source", "remote environment file path escapes the project root" + ) + raise ProjectResolutionError( + "source", "remote environment file input is not a regular file" + ) + + for environment_file in environment_files: + state = capture.stat(root, environment_file.path) + if state == "file": + if environment_file.path not in captured_dotenv_paths: + dotenv_files.append( + _captured_input_file( + capture.capture(root, environment_file.path), + "remote service env_file input is not a regular file", + ) + ) + captured_dotenv_paths.add(environment_file.path) + continue + if state == "escape": + raise ProjectResolutionError( + "source", "remote environment file path escapes the project root" + ) + if state == "missing" and not environment_file.required: + continue + raise ProjectResolutionError( + "source", "remote service env_file input is not a regular file" + ) + + file_secrets: list[FileSecret] = [] + for secret_input in secret_inputs: + state = capture.stat(root, secret_input.path) + if state == "file": + file_secrets.append( + FileSecret( + name=secret_input.name, + source=_captured_input_file( + capture.capture(root, secret_input.path), + "remote file-backed Compose secret input is not a regular file", + ), + ) + ) + continue + if state == "escape": + raise ProjectResolutionError( + "source", "remote file-backed Compose secret path escapes the project root" + ) + raise ProjectResolutionError( + "source", "remote file-backed Compose secret input is not a regular file" + ) + return tuple(dotenv_files), tuple(file_secrets) diff --git a/src/compose2role/secret_policy.py b/src/compose2role/secret_policy.py new file mode 100644 index 0000000..c38c66d --- /dev/null +++ b/src/compose2role/secret_policy.py @@ -0,0 +1,175 @@ +"""Controller-side secret policy overrides for remote environment secret classification.""" + +from __future__ import annotations + +import tomllib +from dataclasses import dataclass +from pathlib import Path, PurePosixPath + +from compose2role.errors import SecretPolicyError + +_ENVIRONMENT_RULE_KEYS = frozenset({"project", "service", "key", "decision"}) +_DOTENV_RULE_KEYS = frozenset({"project", "source", "key", "decision"}) + + +@dataclass(frozen=True) +class SecretRule: + """One exact environment classification override with no value data.""" + + project: str + service: str + key: str + decision: str + + +@dataclass(frozen=True) +class DotenvSecretRule: + """One exact, value-free override for a copied dotenv entry.""" + + project: str + source: PurePosixPath + key: str + decision: str + + +@dataclass(frozen=True) +class SecretPolicy: + """Exact controller-side secret policy rules.""" + + rules: tuple[SecretRule, ...] + dotenv_rules: tuple[DotenvSecretRule, ...] = () + + def decision_for(self, project: str, service: str, key: str) -> str | None: + """Return the exact matching decision, if the secret policy has one.""" + for rule in self.rules: + if (rule.project, rule.service, rule.key) == (project, service, key): + return rule.decision + return None + + def dotenv_decision_for( + self, project: str, source: PurePosixPath, key: str + ) -> str | None: + """Return the exact matching copied-dotenv decision, if present.""" + for rule in self.dotenv_rules: + if (rule.project, rule.source, rule.key) == (project, source, key): + return rule.decision + return None + + +def load_secret_policy(path: Path) -> SecretPolicy: + """Load a secret policy without retaining arbitrary policy values in its interface.""" + try: + document: object = tomllib.loads(path.read_text(encoding="utf-8")) + except (OSError, tomllib.TOMLDecodeError) as error: + raise SecretPolicyError("secret policy could not be read") from error + if not isinstance(document, dict): + raise SecretPolicyError("secret policy is invalid") + + if frozenset(document) - {"rules", "dotenv_rules"}: + raise SecretPolicyError("secret policy must define only exact rule tables") + rules_value: object = document.get("rules", []) + dotenv_rules_value: object = document.get("dotenv_rules", []) + if not isinstance(rules_value, list) or not isinstance(dotenv_rules_value, list): + raise SecretPolicyError("secret policy must define rules") + if not rules_value and not dotenv_rules_value: + raise SecretPolicyError("secret policy must define rules") + + rules: list[SecretRule] = [] + decisions: dict[tuple[str, str, str], str] = {} + for rule_value in rules_value: + for rule in _parse_rule(rule_value): + identity = (rule.project, rule.service, rule.key) + previous_decision = decisions.get(identity) + if previous_decision is not None and previous_decision != rule.decision: + raise SecretPolicyError("secret policy has conflicting exact rules") + decisions[identity] = rule.decision + rules.append(rule) + dotenv_rules: list[DotenvSecretRule] = [] + dotenv_decisions: dict[tuple[str, PurePosixPath, str], str] = {} + for rule_value in dotenv_rules_value: + for dotenv_rule in _parse_dotenv_rule(rule_value): + dotenv_identity = (dotenv_rule.project, dotenv_rule.source, dotenv_rule.key) + previous_decision = dotenv_decisions.get(dotenv_identity) + if previous_decision is not None and previous_decision != dotenv_rule.decision: + raise SecretPolicyError("secret policy has conflicting exact rules") + dotenv_decisions[dotenv_identity] = dotenv_rule.decision + dotenv_rules.append(dotenv_rule) + return SecretPolicy(rules=tuple(rules), dotenv_rules=tuple(dotenv_rules)) + + +def _rule_keys(value: object) -> tuple[str, ...]: + if isinstance(value, str): + if not value: + raise SecretPolicyError("secret policy rule keys must be non-empty strings") + return (value,) + if isinstance(value, list) and value: + keys: list[str] = [] + for item in value: + if not isinstance(item, str) or not item: + raise SecretPolicyError("secret policy rule keys must be non-empty strings") + keys.append(item) + return tuple(keys) + raise SecretPolicyError("secret policy rule keys must be non-empty strings") + + +def _parse_rule(value: object) -> list[SecretRule]: + if not isinstance(value, dict): + raise SecretPolicyError("secret policy is invalid") + rule: dict[str, object] = {} + for key, item in value.items(): + if not isinstance(key, str): + raise SecretPolicyError("secret policy is invalid") + rule[key] = item + if frozenset(rule) != _ENVIRONMENT_RULE_KEYS: + raise SecretPolicyError("secret policy rules must contain only exact match fields") + + project = rule["project"] + service = rule["service"] + key = rule["key"] + decision = rule["decision"] + if ( + not isinstance(project, str) + or not project + or not isinstance(service, str) + or not service + or not isinstance(decision, str) + or decision not in {"secret", "plain"} + ): + raise SecretPolicyError("secret policy rules are invalid") + return [ + SecretRule(project=project, service=service, key=item, decision=decision) + for item in _rule_keys(key) + ] + + +def _parse_dotenv_rule(value: object) -> list[DotenvSecretRule]: + if not isinstance(value, dict): + raise SecretPolicyError("secret policy is invalid") + rule: dict[str, object] = {} + for key, item in value.items(): + if not isinstance(key, str): + raise SecretPolicyError("secret policy is invalid") + rule[key] = item + if frozenset(rule) != _DOTENV_RULE_KEYS: + raise SecretPolicyError("dotenv secret policy rules must contain only exact match fields") + + project = rule["project"] + source_value = rule["source"] + key = rule["key"] + decision = rule["decision"] + if ( + not isinstance(project, str) + or not project + or not isinstance(source_value, str) + or not source_value + or not isinstance(decision, str) + or decision not in {"secret", "plain"} + ): + raise SecretPolicyError("dotenv secret policy rules are invalid") + source = PurePosixPath(source_value) + if source.is_absolute() or ".." in source.parts or str(source) != source_value: + raise SecretPolicyError("dotenv secret policy source paths must be root-relative") + return [ + DotenvSecretRule(project=project, source=source, key=item, decision=decision) + for item in _rule_keys(key) + ] diff --git a/src/compose2role/secrets.py b/src/compose2role/secrets.py new file mode 100644 index 0000000..c23cb80 --- /dev/null +++ b/src/compose2role/secrets.py @@ -0,0 +1,143 @@ +"""Value-aware secret classification that never exposes effective values.""" + +from __future__ import annotations + +import re +from dataclasses import dataclass +from typing import TYPE_CHECKING + +if TYPE_CHECKING: + from compose2role.model import SourcePosition + from compose2role.secret_policy import SecretPolicy + +_SECRET_COMPONENTS = frozenset( + { + "CREDENTIAL", + "CREDENTIALS", + "DSN", + "PASS", + "PASSWORD", + "SECRET", + "TOKEN", + } +) +_CREDENTIAL_URI = re.compile(r"^[A-Za-z][A-Za-z0-9+.-]*://[^\s/:@]+:[^\s/@]+@") +_JWT = re.compile(r"^[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}$") +_OPAQUE_TOKEN = re.compile(r"^[A-Za-z0-9_-]{32,}$") +_OPAQUE_TOKEN_PREFIXES = ("sk-", "ghp_", "xoxb-") + + +@dataclass(frozen=True) +class EnvironmentClassification: + """A value-free secret decision for an effective service environment entry.""" + + service: str + key: str + is_secret: bool + reason: str + location: SourcePosition + + def as_json(self) -> dict[str, str]: + """Return the classification report without the effective value.""" + return { + "key": self.key, + "location": str(self.location), + "reason": self.reason, + "service": self.service, + } + + +def is_secret_key(key: str) -> bool: + """Return whether an environment-variable name likely carries a secret. + + ``KEY_PREFIX`` deliberately does not match: matching arbitrary ``KEY`` substrings + creates too many false positives. A standalone ``KEY`` or a ``*_KEY`` suffix does + match; later configuration can allow-list public keys explicitly. + """ + normalized = key.upper() + components = frozenset(normalized.split("_")) + return ( + bool(components & _SECRET_COMPONENTS) + or normalized == "KEY" + or normalized.endswith("_KEY") + ) + + +def classify_environment( + project: str, + service: str, + key: str, + value: str, + location: SourcePosition, + secret_policy: SecretPolicy | None = None, +) -> EnvironmentClassification: + """Classify an effective environment value without including it in the result.""" + secret_policy_decision = ( + secret_policy.decision_for(project, service, key) + if secret_policy is not None + else None + ) + if secret_policy_decision is not None: + return EnvironmentClassification( + service=service, + key=key, + is_secret=secret_policy_decision == "secret", + reason=f"secret-policy:forced-{secret_policy_decision}", + location=location, + ) + if is_secret_key(key): + return EnvironmentClassification( + service=service, + key=key, + is_secret=True, + reason="key-name", + location=location, + ) + if _CREDENTIAL_URI.match(value): + return EnvironmentClassification( + service=service, + key=key, + is_secret=True, + reason="value:credential-uri", + location=location, + ) + if "-----BEGIN" in value and "PRIVATE KEY-----" in value: + return EnvironmentClassification( + service=service, + key=key, + is_secret=True, + reason="value:private-key", + location=location, + ) + if _JWT.match(value): + return EnvironmentClassification( + service=service, + key=key, + is_secret=True, + reason="value:jwt", + location=location, + ) + if value.startswith(_OPAQUE_TOKEN_PREFIXES) or _is_mixed_opaque_token(value): + return EnvironmentClassification( + service=service, + key=key, + is_secret=True, + reason="value:opaque-token", + location=location, + ) + return EnvironmentClassification( + service=service, + key=key, + is_secret=False, + reason="not-secret", + location=location, + ) + + +def _is_mixed_opaque_token(value: str) -> bool: + return bool( + _OPAQUE_TOKEN.match(value) + and any(character.islower() for character in value) + and any(character.isupper() for character in value) + and any(character.isdigit() for character in value) + ) diff --git a/src/compose2role/sops.py b/src/compose2role/sops.py new file mode 100644 index 0000000..299bfa2 --- /dev/null +++ b/src/compose2role/sops.py @@ -0,0 +1,138 @@ +"""Validate and apply SOPS configuration for remote generation.""" + +from __future__ import annotations + +import json +import os +import re +import shutil +from collections.abc import Mapping +from dataclasses import dataclass +from pathlib import Path, PurePosixPath +from subprocess import TimeoutExpired, run + +from ruamel.yaml import YAML +from ruamel.yaml.error import YAMLError + +from compose2role.errors import GenerationError + +SOPS_TARGET = PurePosixPath("vars/secrets.sops.yml") + + +@dataclass(frozen=True) +class SopsConfiguration: + """A validated operator-owned SOPS config and executable.""" + + path: Path + executable: str + + +def validate_sops_config(path: Path) -> None: + """Validate operator-owned SOPS coverage without copying or encrypting anything.""" + if not path.is_file(): + raise GenerationError("SOPS config could not be read") + _validate_creation_rule(path) + + +def resolve_sops_configuration(explicit_path: Path | None) -> SopsConfiguration: + """Validate the selected SOPS configuration before remote acquisition.""" + selected_path = explicit_path + if selected_path is None: + environment_path = os.environ.get("COMPOSE2ROLE_SOPS_CONFIG") + selected_path = Path(environment_path) if environment_path else None + if selected_path is None or not selected_path.is_file(): + raise GenerationError( + "remote generation requires an existing SOPS config via --sops-config " + "or COMPOSE2ROLE_SOPS_CONFIG" + ) + + executable = shutil.which("sops") + if executable is None: + raise GenerationError("remote generation requires the SOPS executable") + _validate_creation_rule(selected_path) + return SopsConfiguration(path=selected_path, executable=executable) + + +def encrypt_secret_values(configuration: SopsConfiguration, values: Mapping[str, str]) -> bytes: + """Encrypt an in-memory secret mapping without creating a plaintext file.""" + plaintext = json.dumps(dict(values), sort_keys=True).encode("utf-8") + try: + result = run( + ( + configuration.executable, + "--config", + str(configuration.path), + "--encrypt", + "--input-type", + "yaml", + "--output-type", + "yaml", + "--filename-override", + str(SOPS_TARGET), + "/dev/stdin", + ), + check=False, + capture_output=True, + input=plaintext, + timeout=30, + ) + except (FileNotFoundError, TimeoutExpired, OSError) as error: + raise GenerationError("SOPS encryption failed") from error + if result.returncode != 0 or not result.stdout: + raise GenerationError("SOPS encryption failed") + _validate_encrypted_output(values, result.stdout) + return result.stdout + + +def _validate_encrypted_output(values: Mapping[str, str], output: bytes) -> None: + try: + document: object = YAML(typ="safe").load(output.decode("utf-8")) + except (UnicodeDecodeError, YAMLError) as error: + raise GenerationError("SOPS encryption output is invalid") from error + if not isinstance(document, dict): + raise GenerationError("SOPS encryption output is invalid") + plaintext_values = {value for value in values.values() if value} + for key, plaintext in values.items(): + encrypted_value = document.get(key) + if plaintext == "": + # SOPS deliberately leaves empty strings unencrypted; there is nothing to protect. + if encrypted_value != "": + raise GenerationError("SOPS encryption output is invalid") + continue + if not ( + isinstance(encrypted_value, str) + and encrypted_value.startswith("ENC[") + and encrypted_value.endswith("]") + ): + raise GenerationError("SOPS encryption output is invalid") + for key, value in document.items(): + if key != "sops" and isinstance(value, str) and value in plaintext_values: + raise GenerationError("SOPS encryption output is invalid") + + +def _validate_creation_rule(path: Path) -> None: + try: + document: object = YAML(typ="safe").load(path.read_text(encoding="utf-8")) + except (OSError, YAMLError, UnicodeDecodeError) as error: + raise GenerationError("SOPS config could not be read") from error + if not isinstance(document, dict): + raise GenerationError("SOPS config must define creation rules") + rules = document.get("creation_rules") + if not isinstance(rules, list): + raise GenerationError("SOPS config must define creation rules") + + target = str(SOPS_TARGET) + for rule in rules: + if not isinstance(rule, dict): + continue + path_regex = rule.get("path_regex") + if path_regex is None: + return + if not isinstance(path_regex, str): + continue + try: + if re.search(path_regex, target) is not None: + return + except re.error as error: + raise GenerationError("SOPS config contains an invalid path_regex") from error + raise GenerationError("SOPS config has no creation rule for vars/secrets.sops.yml") diff --git a/src/compose2role/source.py b/src/compose2role/source.py new file mode 100644 index 0000000..b6171ff --- /dev/null +++ b/src/compose2role/source.py @@ -0,0 +1,14 @@ +"""Captured Compose source metadata.""" + +from __future__ import annotations + +from dataclasses import dataclass + + +@dataclass(frozen=True) +class FileMetadata: + """Ownership and mode captured with one source file.""" + + mode: int + uid: int + gid: int diff --git a/src/compose2role/source_fidelity.py b/src/compose2role/source_fidelity.py new file mode 100644 index 0000000..d1875a6 --- /dev/null +++ b/src/compose2role/source_fidelity.py @@ -0,0 +1,1254 @@ +"""Prepare source-faithful remote Compose templates and parameter values.""" + +from __future__ import annotations + +import hashlib +import re +from base64 import b64encode +from collections.abc import Iterable +from dataclasses import dataclass, field +from pathlib import PurePosixPath + +from ruamel.yaml import YAML +from ruamel.yaml.comments import CommentedMap, CommentedSeq +from ruamel.yaml.error import YAMLError + +from compose2role.errors import GenerationError +from compose2role.model import Port, Service, SourcePosition +from compose2role.resolve import ResolvedProject, SourceFile +from compose2role.secret_policy import SecretPolicy +from compose2role.secrets import classify_environment + + +@dataclass(frozen=True) +class SourceTemplate: + """One source file rendered through Ansible without normalizing its layout.""" + + source: SourceFile + content: str + newline_sequence: str + has_secrets: bool = False + + @property + def relative_template_path(self) -> PurePosixPath: + """Return the role-relative template path for the captured source file.""" + return self.source.relative_path.with_name(f"{self.source.relative_path.name}.j2") + + +@dataclass(frozen=True) +class RoleVariable: + """One generated default or encrypted SOPS value.""" + + name: str + value: str = field(repr=False) + is_secret: bool + + +@dataclass(frozen=True) +class FileSecretRender: + """One encrypted file-backed secret restored by the generated role.""" + + compose_secret_name: str + source: SourceFile = field(repr=False) + variable_name: str + + +@dataclass(frozen=True) +class SourceRoleMaterialization: + """The source templates and values needed to render one adopted role.""" + + templates: tuple[SourceTemplate, ...] + variables: tuple[RoleVariable, ...] = field(repr=False) + file_secrets: tuple[FileSecretRender, ...] = field(default=(), repr=False) + + @property + def secret_values(self) -> dict[str, str]: + """Return the in-memory SOPS payload without exposing it through repr().""" + return {variable.name: variable.value for variable in self.variables if variable.is_secret} + + +@dataclass(frozen=True) +class _Candidate: + """A source value which becomes one role variable.""" + + identity: str + base_name: str + value: str = field(repr=False) + is_secret: bool + + +@dataclass(frozen=True) +class _Edit: + """One source span replaced by text and generated variables.""" + + start: int + end: int + parts: tuple[str | _Candidate, ...] + + +def materialize_source_role( + project: ResolvedProject, role_name: str +) -> SourceRoleMaterialization: + """Build byte-faithful templates and parameter values for captured Compose files. + + The public remote emitter consumes this one result. Parsing, source-span tracking, collision + handling, and Jinja escaping stay local so callers only need the emitted templates and values. + """ + candidates: dict[str, _Candidate] = {} + edits_by_path: dict[PurePosixPath, list[_Edit]] = {} + file_secret_candidates: list[tuple[str, SourceFile, _Candidate]] = [] + dotenv_environment_decisions: dict[str, set[bool]] = {} + services = {service.name: service for service in project.stack.services} + canonical_secret_classifications = { + (classification.service, classification.key): classification.is_secret + for classification in project.classifications + } + + for source in project.source.files: + content = _decode_source(source) + document = _load_round_trip_document(content) + _raise_for_extension_secret_environments( + document, source.relative_path, project.project.name, project.secret_policy + ) + source_edits = edits_by_path.setdefault(source.relative_path, []) + source_services = _mapping( + document.get("services"), "remote Compose source has invalid services" + ) + for service_name, source_service in source_services.items(): + if not isinstance(service_name, str): + raise GenerationError("remote Compose source has invalid service names") + service_mapping = _mapping( + source_service, "remote Compose source has an invalid service" + ) + service = services.get(service_name) + if service is None: + _raise_for_source_only_secret_environment( + service_mapping.get("environment"), + service_name, + source.relative_path, + project.project.name, + project.secret_policy, + ) + continue + _plan_image_edit( + candidates, + source_edits, + content, + source.relative_path, + service_mapping, + service, + role_name, + ) + _plan_port_edits( + candidates, + source_edits, + content, + source.relative_path, + service_mapping, + service, + role_name, + ) + _plan_environment_edits( + candidates, + source_edits, + content, + source.relative_path, + service_mapping, + service, + role_name, + project.project.name, + project.secret_policy, + canonical_secret_classifications, + ) + _record_dotenv_environment_decisions( + service_mapping, + service, + source.relative_path, + project.project.name, + project.secret_policy, + canonical_secret_classifications, + dotenv_environment_decisions, + ) + + for source in project.source.dotenv_files: + content = _decode_source(source) + source_edits = edits_by_path.setdefault(source.relative_path, []) + _plan_dotenv_edits( + candidates, + source_edits, + content, + source.relative_path, + project.project.name, + role_name, + project.secret_policy, + dotenv_environment_decisions, + ) + + for file_secret in project.source.file_secrets: + candidate = _add_candidate( + candidates, + identity=f"file-secret:{file_secret.name}:{file_secret.source.relative_path}", + base_name=f"{role_name}_secret_{_normalize_name(file_secret.name)}", + value=b64encode(file_secret.source.content).decode("ascii"), + is_secret=True, + ) + file_secret_candidates.append((file_secret.name, file_secret.source, candidate)) + + names = _variable_names( + candidates.values(), + { + f"{role_name}_compose_project_name", + f"{role_name}_project_dir", + f"{role_name}_required_secrets", + f"{role_name}_secrets_file", + }, + ) + source_files = (*project.source.files, *project.source.dotenv_files) + templates = tuple( + SourceTemplate( + source=source, + content=_apply_edits( + _decode_source(source), edits_by_path[source.relative_path], names, role_name + ), + newline_sequence=_newline_sequence(_decode_source(source)), + has_secrets=any( + isinstance(part, _Candidate) and part.is_secret + for edit in edits_by_path[source.relative_path] + for part in edit.parts + ), + ) + for source in source_files + ) + variables = tuple( + RoleVariable( + name=names[candidate.identity], + value=candidate.value, + is_secret=candidate.is_secret, + ) + for candidate in candidates.values() + ) + file_secrets = tuple( + FileSecretRender( + compose_secret_name=name, + source=source, + variable_name=names[candidate.identity], + ) + for name, source, candidate in file_secret_candidates + ) + return SourceRoleMaterialization( + templates=templates, + variables=variables, + file_secrets=file_secrets, + ) + + +def _decode_source(source: SourceFile) -> str: + try: + return source.content.decode("utf-8") + except UnicodeDecodeError as error: + raise GenerationError("remote Compose source must be UTF-8") from error + + +def _load_round_trip_document(content: str) -> CommentedMap: + yaml = YAML(typ="rt") + try: + document: object = yaml.load(content) + except YAMLError as error: + raise GenerationError("remote Compose source could not be parsed") from error + return _mapping(document, "remote Compose source must be a mapping") + + +def _mapping(value: object, message: str) -> CommentedMap: + if not isinstance(value, CommentedMap): + raise GenerationError(message) + return value + + +def _plan_image_edit( + candidates: dict[str, _Candidate], + edits: list[_Edit], + content: str, + relative_path: PurePosixPath, + source_service: CommentedMap, + service: Service, + role_name: str, +) -> None: + source_image = source_service.get("image") + if not isinstance(source_image, str) or "$" in source_image: + return + repository, reference, separator = _image_parts(service) + if source_image != service.image.source: + return + try: + start, end = _value_span(content, source_service, "image") + except GenerationError: + return + prefix, suffix = _scalar_wrapper(content[start:end]) + identity_prefix = f"compose:{relative_path}:{service.name}:image" + image = _add_candidate( + candidates, + identity=f"{identity_prefix}:repository", + base_name=f"{_service_prefix(role_name, service)}_image", + value=repository, + is_secret=False, + ) + if source_image == repository and reference == "latest" and separator == ":": + edits.append(_Edit(start, end, (prefix, image, suffix))) + return + tag = _add_candidate( + candidates, + identity=f"{identity_prefix}:reference", + base_name=f"{_service_prefix(role_name, service)}_image_tag", + value=reference, + is_secret=False, + ) + edits.append(_Edit(start, end, (prefix, image, separator, tag, suffix))) + + +def _plan_port_edits( + candidates: dict[str, _Candidate], + edits: list[_Edit], + content: str, + relative_path: PurePosixPath, + source_service: CommentedMap, + service: Service, + role_name: str, +) -> None: + if not _has_direct_source_value(source_service, "ports"): + return + ports_value = source_service.get("ports") + if not isinstance(ports_value, CommentedSeq): + return + published_ports = [port for port in service.ports if port.published is not None] + used: set[int] = set() + for source_index, source_port in enumerate(ports_value): + try: + source_item_span = _item_span(content, ports_value, source_index) + except GenerationError: + continue + if isinstance(source_port, CommentedMap): + parsed_long = _long_port(source_port) + if parsed_long is None: + continue + published, target = parsed_long + canonical_index = _matching_port(service.ports, published, target, used) + if canonical_index is None: + continue + used.add(canonical_index) + port = service.ports[canonical_index] + if port.published is None: + continue + try: + start, end = _value_span(content, source_port, "published") + except GenerationError: + continue + raw = content[start:end] + prefix, suffix = _scalar_wrapper(raw) + value = raw[len(prefix) : len(raw) - len(suffix)] if prefix else raw + published_index = published_ports.index(port) + 1 + variable = _add_candidate( + candidates, + identity=( + f"compose:{relative_path}:{service.name}:port:{source_index}:published" + ), + base_name=_published_port_variable( + _service_prefix(role_name, service), published_index, len(published_ports) + ), + value=value, + is_secret=False, + ) + edits.append( + _Edit(start, end, (variable,) if not prefix else (prefix, variable, suffix)) + ) + continue + if not isinstance(source_port, str): + continue + parsed = _short_port(source_port) + if parsed is None: + continue + published, target = parsed + canonical_index = _matching_port(service.ports, published, target, used) + if canonical_index is None: + continue + used.add(canonical_index) + port = service.ports[canonical_index] + if port.published is None: + continue + start, end = source_item_span + published_start, published_end = _short_port_published_span( + content, start, end, published + ) + published_index = published_ports.index(port) + 1 + variable = _add_candidate( + candidates, + identity=( + f"compose:{relative_path}:{service.name}:port:{source_index}:published" + ), + base_name=_published_port_variable( + _service_prefix(role_name, service), published_index, len(published_ports) + ), + value=published, + is_secret=False, + ) + edits.append(_Edit(published_start, published_end, (variable,))) + + +def _plan_environment_edits( + candidates: dict[str, _Candidate], + edits: list[_Edit], + content: str, + relative_path: PurePosixPath, + source_service: CommentedMap, + service: Service, + role_name: str, + project_name: str, + secret_policy: SecretPolicy | None, + canonical_secret_classifications: dict[tuple[str, str], bool], +) -> None: + environment_value = source_service.get("environment") + _raise_for_merged_secret_environment( + source_service, + service, + relative_path, + project_name, + secret_policy, + canonical_secret_classifications, + ) + if not _has_direct_source_value(source_service, "environment"): + _raise_for_uneditable_secret_environment( + environment_value, + service, + relative_path, + project_name, + secret_policy, + canonical_secret_classifications, + ) + return + if isinstance(environment_value, CommentedMap): + _plan_mapping_environment_edits( + candidates, + edits, + content, + relative_path, + environment_value, + service, + role_name, + project_name, + secret_policy, + canonical_secret_classifications, + ) + return + if isinstance(environment_value, CommentedSeq): + _plan_list_environment_edits( + candidates, + edits, + content, + relative_path, + environment_value, + service, + role_name, + project_name, + secret_policy, + canonical_secret_classifications, + ) + + +def _raise_for_merged_secret_environment( + source_service: CommentedMap, + service: Service, + relative_path: PurePosixPath, + project_name: str, + secret_policy: SecretPolicy | None, + canonical_secret_classifications: dict[tuple[str, str], bool], +) -> None: + for merged_service in source_service.merge: + if isinstance(merged_service, CommentedMap): + _raise_for_uneditable_secret_environment( + merged_service.get("environment"), + service, + relative_path, + project_name, + secret_policy, + canonical_secret_classifications, + ) + + +def _raise_for_uneditable_secret_environment( + environment: object, + service: Service, + relative_path: PurePosixPath, + project_name: str, + secret_policy: SecretPolicy | None, + canonical_secret_classifications: dict[tuple[str, str], bool], +) -> None: + if _environment_contains_secret_literal( + environment, + service, + relative_path, + project_name, + secret_policy, + canonical_secret_classifications, + ): + raise GenerationError("remote Compose source has an unsupported secret environment") + + +def _environment_contains_secret_literal( + environment: object, + service: Service, + relative_path: PurePosixPath, + project_name: str, + secret_policy: SecretPolicy | None, + canonical_secret_classifications: dict[tuple[str, str], bool], +) -> bool: + if isinstance(environment, CommentedMap): + return any( + isinstance(key, str) + and _is_secret_literal(value) + and _source_environment_secret( + service, + key, + value, + _mapping_source_position(environment, key, relative_path), + project_name, + secret_policy, + canonical_secret_classifications, + ) + for key, value in environment.items() + ) + if isinstance(environment, CommentedSeq): + for index, item in enumerate(environment): + if not isinstance(item, str): + continue + key, separator, value = item.partition("=") + if ( + separator + and _is_secret_literal(value) + and _source_environment_secret( + service, + key, + value, + _sequence_source_position(environment, index, relative_path), + project_name, + secret_policy, + canonical_secret_classifications, + ) + ): + return True + return False + + +def _is_secret_literal(value: object) -> bool: + return value is not None and value != "" and ( + not isinstance(value, str) or not _uses_interpolation(value) + ) + + +def _raise_for_source_only_secret_environment( + environment: object, + service_name: str, + relative_path: PurePosixPath, + project_name: str, + secret_policy: SecretPolicy | None, +) -> None: + if isinstance(environment, CommentedMap): + contains_secret = any( + isinstance(key, str) + and _is_secret_literal(value) + and _source_environment_secret_for_service( + service_name, + key, + value, + _mapping_source_position(environment, key, relative_path), + project_name, + secret_policy, + {}, + ) + for key, value in environment.items() + ) + elif isinstance(environment, CommentedSeq): + contains_secret = any( + isinstance(item, str) + and bool(separator) + and _is_secret_literal(value) + and _source_environment_secret_for_service( + service_name, + key, + value, + _sequence_source_position(environment, index, relative_path), + project_name, + secret_policy, + {}, + ) + for index, item in enumerate(environment) + for key, separator, value in (item.partition("="),) + ) + else: + contains_secret = False + if contains_secret: + raise GenerationError("remote Compose source has an unsupported secret environment") + + +def _raise_for_extension_secret_environments( + document: CommentedMap, + relative_path: PurePosixPath, + project_name: str, + secret_policy: SecretPolicy | None, +) -> None: + for key, value in document.items(): + if isinstance(key, str) and key.startswith("x-"): + _raise_for_nested_extension_secret_environments( + value, relative_path, project_name, secret_policy + ) + + +def _raise_for_nested_extension_secret_environments( + value: object, + relative_path: PurePosixPath, + project_name: str, + secret_policy: SecretPolicy | None, +) -> None: + if isinstance(value, CommentedMap): + if "environment" in value: + _raise_for_source_only_secret_environment( + value["environment"], "extension", relative_path, project_name, secret_policy + ) + for key, item in value.items(): + if key != "environment": + _raise_for_nested_extension_secret_environments( + item, relative_path, project_name, secret_policy + ) + elif isinstance(value, CommentedSeq): + for item in value: + _raise_for_nested_extension_secret_environments( + item, relative_path, project_name, secret_policy + ) + + +def _source_environment_secret( + service: Service, + key: str, + value: object, + location: SourcePosition, + project_name: str, + secret_policy: SecretPolicy | None, + canonical_secret_classifications: dict[tuple[str, str], bool], +) -> bool: + return _source_environment_secret_for_service( + service.name, + key, + value, + location, + project_name, + secret_policy, + canonical_secret_classifications, + ) + + +def _source_environment_secret_for_service( + service_name: str, + key: str, + value: object, + location: SourcePosition, + project_name: str, + secret_policy: SecretPolicy | None, + canonical_secret_classifications: dict[tuple[str, str], bool], +) -> bool: + source_classification = classify_environment( + project_name, + service_name, + key, + str(value), + location, + secret_policy, + ) + return source_classification.is_secret or canonical_secret_classifications.get( + (service_name, key), False + ) + + +def _mapping_source_position( + mapping: CommentedMap, key: str, relative_path: PurePosixPath +) -> SourcePosition: + return _source_position(mapping.lc.value(key), relative_path) + + +def _sequence_source_position( + sequence: CommentedSeq, index: int, relative_path: PurePosixPath +) -> SourcePosition: + return _source_position(sequence.lc.item(index), relative_path) + + +def _source_position( + position: tuple[int, int] | None, relative_path: PurePosixPath +) -> SourcePosition: + if position is None: + return SourcePosition(relative_path, 1, 1) + line, column = position + return SourcePosition(relative_path, line + 1, column + 1) + + +def _plan_mapping_environment_edits( + candidates: dict[str, _Candidate], + edits: list[_Edit], + content: str, + relative_path: PurePosixPath, + environment: CommentedMap, + service: Service, + role_name: str, + project_name: str, + secret_policy: SecretPolicy | None, + canonical_secret_classifications: dict[tuple[str, str], bool], +) -> None: + for key, value in environment.items(): + if not isinstance(key, str) or value is None: + continue + is_secret = _source_environment_secret( + service, + key, + value, + _mapping_source_position(environment, key, relative_path), + project_name, + secret_policy, + canonical_secret_classifications, + ) + if isinstance(value, str) and _uses_interpolation(value): + continue + if not isinstance(value, (str, int, float, bool)): + if is_secret: + raise GenerationError("remote Compose source has an unsupported secret environment") + continue + try: + start, end = _value_span(content, environment, key) + except GenerationError: + if is_secret: + raise + continue + raw = content[start:end] + prefix, suffix = _scalar_wrapper(raw) + value = raw[len(prefix) : len(raw) - len(suffix)] if prefix else raw + variable = _add_candidate( + candidates, + identity=f"compose:{relative_path}:{service.name}:environment:{key}", + base_name=f"{_service_prefix(role_name, service)}_{_normalize_name(key)}", + value=value, + is_secret=is_secret, + ) + edits.append( + _Edit(start, end, (variable,) if not prefix else (prefix, variable, suffix)) + ) + + +def _plan_list_environment_edits( + candidates: dict[str, _Candidate], + edits: list[_Edit], + content: str, + relative_path: PurePosixPath, + environment: CommentedSeq, + service: Service, + role_name: str, + project_name: str, + secret_policy: SecretPolicy | None, + canonical_secret_classifications: dict[tuple[str, str], bool], +) -> None: + for index, item in enumerate(environment): + if not isinstance(item, str): + continue + key, separator, value = item.partition("=") + if not key or not separator or _uses_interpolation(value): + continue + is_secret = _source_environment_secret( + service, + key, + value, + _sequence_source_position(environment, index, relative_path), + project_name, + secret_policy, + canonical_secret_classifications, + ) + try: + start, end = _item_span(content, environment, index) + value_start, value_end = _list_environment_value_span(content, start, end, key) + except GenerationError: + if is_secret: + raise + continue + raw = content[value_start:value_end] + prefix, suffix = _scalar_wrapper(raw) + value = raw[len(prefix) : len(raw) - len(suffix)] if prefix else raw + variable = _add_candidate( + candidates, + identity=f"compose:{relative_path}:{service.name}:environment-list:{index}:{key}", + base_name=f"{_service_prefix(role_name, service)}_{_normalize_name(key)}", + value=value, + is_secret=is_secret, + ) + edits.append( + _Edit( + value_start, + value_end, + (variable,) if not prefix else (prefix, variable, suffix), + ) + ) + + +def _list_environment_value_span(content: str, start: int, end: int, key: str) -> tuple[int, int]: + item = content[start:end] + prefix, suffix = _scalar_wrapper(item) + inner_start = start + len(prefix) + inner_end = end - len(suffix) + separator = content.find("=", inner_start, inner_end) + if separator == -1 or content[inner_start:separator] != key: + raise GenerationError("remote Compose source has an unsupported environment item") + return separator + 1, inner_end + + +def _record_dotenv_environment_decisions( + source_service: CommentedMap, + service: Service, + relative_path: PurePosixPath, + project_name: str, + secret_policy: SecretPolicy | None, + canonical_secret_classifications: dict[tuple[str, str], bool], + decisions: dict[str, set[bool]], +) -> None: + environment = source_service.get("environment") + if isinstance(environment, CommentedMap): + for key, value in environment.items(): + if isinstance(key, str) and isinstance(value, str): + _record_dotenv_environment_decision( + key, + value, + _source_environment_secret( + service, + key, + value, + _mapping_source_position(environment, key, relative_path), + project_name, + secret_policy, + canonical_secret_classifications, + ), + decisions, + ) + return + if isinstance(environment, CommentedSeq): + for index, item in enumerate(environment): + if not isinstance(item, str): + continue + key, separator, value = item.partition("=") + if key and separator: + _record_dotenv_environment_decision( + key, + value, + _source_environment_secret( + service, + key, + value, + _sequence_source_position(environment, index, relative_path), + project_name, + secret_policy, + canonical_secret_classifications, + ), + decisions, + ) + + +def _plan_dotenv_edits( + candidates: dict[str, _Candidate], + edits: list[_Edit], + content: str, + relative_path: PurePosixPath, + project_name: str, + role_name: str, + secret_policy: SecretPolicy | None, + environment_decisions: dict[str, set[bool]], +) -> None: + for index, entry in enumerate(_dotenv_entries(content)): + secret_policy_decision = _dotenv_secret_policy_decision( + secret_policy, project_name, relative_path, entry.key + ) + if secret_policy_decision is not None: + is_secret = secret_policy_decision == "secret" + else: + decisions = environment_decisions.get(entry.key, set()) + if decisions: + is_secret = True in decisions + else: + classification = classify_environment( + project_name, + f"dotenv:{relative_path}", + entry.key, + entry.semantic_value, + SourcePosition(relative_path, entry.line, entry.column), + ) + is_secret = classification.is_secret + raw = content[entry.start : entry.end] + prefix, suffix = _scalar_wrapper(raw) + value = raw[len(prefix) : len(raw) - len(suffix)] if prefix else raw + variable = _add_candidate( + candidates, + identity=f"dotenv:{relative_path}:{index}:{entry.key}", + base_name=( + f"{role_name}_dotenv_{_source_path_name(relative_path)}_" + f"{_normalize_name(entry.key)}" + ), + value=value, + is_secret=is_secret, + ) + edits.append( + _Edit( + entry.start, + entry.end, + (variable,) if not prefix else (prefix, variable, suffix), + ) + ) + + +def _dotenv_secret_policy_decision( + secret_policy: SecretPolicy | None, project: str, source: PurePosixPath, key: str +) -> str | None: + if secret_policy is None: + return None + result = secret_policy.dotenv_decision_for(project, source, key) + if result is not None and result not in {"secret", "plain"}: + raise GenerationError("dotenv secret policy returned an invalid decision") + return result + + +_SIMPLE_INTERPOLATION = re.compile(r"^\$(?:\{([A-Za-z_][A-Za-z0-9_]*)\}|([A-Za-z_][A-Za-z0-9_]*))$") + + +def _record_dotenv_environment_decision( + key: str, + value: str, + is_secret: bool, + decisions: dict[str, set[bool]], +) -> None: + match = _SIMPLE_INTERPOLATION.fullmatch(value) + if match is None: + return + dotenv_key = match.group(1) or match.group(2) + if dotenv_key is not None: + decisions.setdefault(dotenv_key, set()).add(is_secret) + + +@dataclass(frozen=True) +class _DotenvEntry: + """One dotenv assignment whose lexical value can be substituted exactly.""" + + key: str + semantic_value: str + start: int + end: int + line: int + column: int + + +_DOTENV_ASSIGNMENT = re.compile(r"[ \t]*(?:export[ \t]+)?([A-Za-z_][A-Za-z0-9_]*)[ \t]*=") + + +def _dotenv_entries(content: str) -> tuple[_DotenvEntry, ...]: + entries: list[_DotenvEntry] = [] + offset = 0 + for line_number, line in enumerate(content.splitlines(keepends=True), start=1): + body = line.rstrip("\r\n") + match = _DOTENV_ASSIGNMENT.match(body) + if match is None: + offset += len(line) + continue + value_start = offset + match.end() + while value_start < offset + len(body) and content[value_start] in " \t": + value_start += 1 + value_end = _dotenv_value_end(content, value_start, offset + len(body)) + lexical_value = content[value_start:value_end] + entries.append( + _DotenvEntry( + key=match.group(1), + semantic_value=_dotenv_semantic_value(lexical_value), + start=value_start, + end=value_end, + line=line_number, + column=value_start - offset + 1, + ) + ) + offset += len(line) + return tuple(entries) + + +def _dotenv_value_end(content: str, start: int, line_end: int) -> int: + if start == line_end: + return start + if content[start] in {"'", '"'}: + return _quoted_scalar_end(content, start) + fragment = content[start:line_end] + comment = re.search(r"\s+#", fragment) + end = start + (comment.start() if comment is not None else len(fragment)) + while end > start and content[end - 1] in " \t": + end -= 1 + return end + + +def _dotenv_semantic_value(value: str) -> str: + if len(value) >= 2 and value[0] == value[-1] and value[0] in {"'", '"'}: + return value[1:-1] + return value + + +def _image_parts(service: Service) -> tuple[str, str, str]: + image = service.image + if image.repository is None or image.reference is None or image.separator is None: + raise GenerationError("remote Compose model has an invalid image") + return image.repository, image.reference, image.separator + + +def _long_port(source_port: CommentedMap) -> tuple[str, str] | None: + published = source_port.get("published") + target = source_port.get("target") + if isinstance(published, bool) or not isinstance(published, (str, int)): + return None + if isinstance(target, bool) or not isinstance(target, (str, int)): + return None + return str(published), str(target) + + +def _short_port(value: str) -> tuple[str, str] | None: + before, separator, target_with_protocol = value.rpartition(":") + if not separator or not before or not target_with_protocol: + return None + target = target_with_protocol.split("/", maxsplit=1)[0] + published = before.rpartition(":")[2] or before + if not published or not target: + return None + return published, target + + +def _short_port_published_span( + content: str, start: int, end: int, published: str +) -> tuple[int, int]: + prefix, suffix = _scalar_wrapper(content[start:end]) + inner_start = start + len(prefix) + inner_end = end - len(suffix) + inner = content[inner_start:inner_end] + for match in re.finditer(re.escape(published), inner): + before = inner[: match.start()] + after = inner[match.end() :] + if (not before or before.endswith(":")) and ( + after.startswith(":") or after.startswith("/") + ): + return inner_start + match.start(), inner_start + match.end() + raise GenerationError("remote Compose source has an unsupported port") + + +def _matching_port( + ports: tuple[Port, ...], published: str, target: str, used: set[int] +) -> int | None: + for index, port in enumerate(ports): + if index not in used and (port.published, port.target) == (published, target): + return index + return None + + +def _add_candidate( + candidates: dict[str, _Candidate], + *, + identity: str, + base_name: str, + value: str, + is_secret: bool, +) -> _Candidate: + candidate = _Candidate(identity, base_name, value, is_secret) + previous = candidates.setdefault(identity, candidate) + if previous != candidate: + raise GenerationError("remote source parameterization is ambiguous") + return previous + + +def _variable_names(candidates: Iterable[_Candidate], reserved_names: set[str]) -> dict[str, str]: + values = tuple(candidates) + by_base: dict[str, list[_Candidate]] = {} + for candidate in values: + by_base.setdefault(candidate.base_name, []).append(candidate) + names: dict[str, str] = {} + for base_name, group in by_base.items(): + if len(group) == 1 and base_name not in reserved_names: + names[group[0].identity] = base_name + continue + for candidate in group: + digest = hashlib.sha256(candidate.identity.encode("utf-8")).hexdigest()[:8] + names[candidate.identity] = f"{base_name}_{digest}" + return names + + +def _value_span(content: str, mapping: CommentedMap, key: str) -> tuple[int, int]: + if not _has_direct_source_value(mapping, key): + raise GenerationError("remote Compose source has an unsupported scalar") + position = mapping.lc.value(key) + if position is None: + raise GenerationError("remote Compose source has an unsupported scalar") + return _scalar_span(content, _offset_for_position(content, position)) + + +def _has_direct_source_value(mapping: CommentedMap, key: str) -> bool: + """Return whether a mapping owns a source span for the named value. + + ruamel resolves merge keys and aliases when reading mappings, but its locations for inherited + or aliased values point at the defining anchor (or are absent). Those locations are not safe + to edit for the consuming service. + """ + if not any(item_key == key for item_key, _ in mapping.non_merged_items()): + return False + key_position = mapping.lc.key(key) + value_position = mapping.lc.value(key) + return ( + key_position is not None + and value_position is not None + and value_position >= key_position + ) + + +def _item_span(content: str, sequence: CommentedSeq, index: int) -> tuple[int, int]: + position = sequence.lc.item(index) + if position is None: + raise GenerationError("remote Compose source has an unsupported scalar") + return _scalar_span(content, _offset_for_position(content, position)) + + +def _offset_for_position(content: str, position: tuple[int, int]) -> int: + line, column = position + offsets = [0] + offsets.extend(index + 1 for index, character in enumerate(content) if character == "\n") + try: + return offsets[line] + column + except IndexError as error: + raise GenerationError("remote Compose source has invalid scalar locations") from error + + +def _scalar_span(content: str, start: int) -> tuple[int, int]: + if start >= len(content) or content[start] in "|>&*!": + raise GenerationError("remote Compose source has an unsupported scalar") + if content[start] in {"'", '"'}: + return start, _quoted_scalar_end(content, start) + line_end = content.find("\n", start) + if line_end == -1: + line_end = len(content) + if line_end > start and content[line_end - 1] == "\r": + line_end -= 1 + fragment = content[start:line_end] + comment = re.search(r"\s+#", fragment) + value_end = start + (comment.start() if comment is not None else len(fragment)) + while value_end > start and content[value_end - 1].isspace(): + value_end -= 1 + if value_end == start: + raise GenerationError("remote Compose source has an unsupported scalar") + return start, value_end + + +def _quoted_scalar_end(content: str, start: int) -> int: + quote = content[start] + index = start + 1 + while index < len(content): + if content[index] != quote: + index += 1 + continue + if quote == '"' and _is_escaped_double_quote(content, start, index): + index += 1 + continue + if quote == "'" and index + 1 < len(content) and content[index + 1] == quote: + index += 2 + continue + return index + 1 + raise GenerationError("remote Compose source has an unterminated scalar") + + +def _is_escaped_double_quote(content: str, start: int, index: int) -> bool: + backslash_count = 0 + cursor = index - 1 + while cursor > start and content[cursor] == "\\": + backslash_count += 1 + cursor -= 1 + return backslash_count % 2 == 1 + + +def _scalar_wrapper(value: str) -> tuple[str, str]: + if value.startswith(("'", '"')): + return value[0], value[-1] + return "", "" + + +def _uses_interpolation(value: str) -> bool: + """Return whether Compose would treat part of this value as interpolation. + + A dollar sign followed by neither a name nor an opening brace is literal, and ``$$`` + escapes a literal dollar. Such values can be parameterized safely; active expressions + must stay verbatim so Compose resolves them at deployment time. + """ + index = 0 + while index < len(value): + if value[index] != "$": + index += 1 + continue + if index + 1 == len(value): + return False + following = value[index + 1] + if following == "$": + index += 2 + continue + if following == "{" or following == "_" or ( + following.isascii() and following.isalpha() + ): + return True + index += 1 + return False + + +def _apply_edits( + content: str, edits: list[_Edit], names: dict[str, str], role_name: str +) -> str: + result: list[str] = [] + offset = 0 + for edit in sorted(edits, key=lambda value: value.start): + if edit.start < offset or edit.end < edit.start: + raise GenerationError("remote source parameterization overlaps") + result.append(_escape_jinja(content[offset : edit.start])) + for part in edit.parts: + if isinstance(part, _Candidate): + variable_name = names[part.identity] + if part.is_secret: + result.append(f"{{{{ {role_name}_secrets['{variable_name}'] }}}}") + else: + result.append(f"{{{{ {variable_name} }}}}") + else: + result.append(part) + offset = edit.end + result.append(_escape_jinja(content[offset:])) + return "".join(result) + + +def _escape_jinja(value: str) -> str: + substitutions = {"{{": "{{ '{{' }}", "{%": "{{ '{%' }}", "{#": "{{ '{#' }}"} + return re.sub(r"\{\{|\{%|\{#", lambda match: substitutions[match.group()], value) + + +def _newline_sequence(content: str) -> str: + without_crlf = content.replace("\r\n", "") + has_crlf = "\r\n" in content + if has_crlf and "\n" in without_crlf: + raise GenerationError("remote Compose source has mixed line endings") + return "\r\n" if has_crlf else "\n" + + +def _service_prefix(role_name: str, service: Service) -> str: + return f"{role_name}_{_normalize_name(service.name)}" + + +def _published_port_variable(prefix: str, index: int, count: int) -> str: + if count == 1: + return f"{prefix}_published_port" + return f"{prefix}_published_port_{index}" + + +def _normalize_name(value: str) -> str: + return re.sub(r"[^a-zA-Z0-9_]", "_", value).lower() + + +def _source_path_name(path: PurePosixPath) -> str: + return _normalize_name(str(path)).strip("_") or "source" diff --git a/tests/requirements.yml b/tests/requirements.yml new file mode 100644 index 0000000..11f0dac --- /dev/null +++ b/tests/requirements.yml @@ -0,0 +1,6 @@ +--- +collections: + - name: community.docker + version: 5.4.0 + - name: community.sops + version: 2.5.0 diff --git a/tests/ssh_stub.py b/tests/ssh_stub.py new file mode 100644 index 0000000..78edc3b --- /dev/null +++ b/tests/ssh_stub.py @@ -0,0 +1,69 @@ +"""Adapters that let one-shot SSH test stubs serve the framed source session.""" + +from __future__ import annotations + +from pathlib import Path +from shlex import quote + + +def legacy_source_session_bridge(legacy_ssh: Path) -> str: + """Wrap a one-shot SSH fake so the resolver can keep one real source session open.""" + return f"""\ +#!/bin/sh +legacy={quote(str(legacy_ssh))} + +case "$*" in + *"sh -s") + IFS= read -r first_line || exit 97 + if [ "$first_line" != '# compose2role-source-session-v1' ]; then + (printf '%s\\n' "$first_line"; cat) | "$legacy" "$@" + exit $? + fi + while IFS= read -r line; do + [ "$line" = '# compose2role-source-session-ready' ] && break + done + IFS= read -r invocation || exit 97 + [ "$invocation" = main ] || exit 97 + printf '%s\\n' ready + while IFS=' ' read -r marker request_id operation extra; do + [ "$marker" = request ] || exit 97 + [ -n "$request_id" ] && [ -z "$extra" ] || exit 97 + IFS= read -r path || exit 97 + IFS= read -r root || exit 97 + IFS= read -r trailer || exit 97 + [ "$trailer" = "end $request_id" ] || exit 97 + encoded=$(printf '%s\\n%s\\n%s\\n' "$operation" "$path" "$root" | base64 | tr -d '\\n') + response=$(printf '%s\\n%s\\n%s\\n' x "$encoded" y | "$legacy" "$@") + status=$? + [ "$status" -eq 0 ] || exit "$status" + case "$operation" in + stat) + case "$response" in + escape|file|missing|other|symlink) + printf 'result %s %s\\nend %s\\n' "$request_id" "$response" "$request_id" + ;; + *) exit 97 ;; + esac + ;; + capture) + header=$(printf '%s\\n' "$response" | sed -n '1p') + case "$header" in + file[[:space:]]*) + content=$(printf '%s\\n' "$response" | sed '1d') + printf 'result %s %s\\n%s\\nend %s\\n' \ + "$request_id" "$header" "$content" "$request_id" + ;; + escape|missing|other|symlink) + printf 'result %s %s\\nend %s\\n' "$request_id" "$header" "$request_id" + ;; + *) exit 97 ;; + esac + ;; + *) exit 97 ;; + esac + done + exit 0 + ;; + *) exec "$legacy" "$@" ;; +esac +""" diff --git a/tests/test_cli_remote_only.py b/tests/test_cli_remote_only.py new file mode 100644 index 0000000..23eebb9 --- /dev/null +++ b/tests/test_cli_remote_only.py @@ -0,0 +1,60 @@ +from __future__ import annotations + +from pathlib import Path + +import pytest +from click.testing import CliRunner + +from compose2role.cli import main + + +@pytest.mark.parametrize("command", ("generate", "audit")) +def test_local_invocation_exits_with_a_usage_error(command: str) -> None: + result = CliRunner().invoke(main, [command]) + + assert result.exit_code == 2 + assert "was removed" in result.output + assert "--host" in result.output + + +@pytest.mark.parametrize("command", ("generate", "audit")) +def test_local_source_is_rejected(command: str, tmp_path: Path) -> None: + source = tmp_path / "compose.yaml" + source.write_text("services: {}\n", encoding="utf-8") + + result = CliRunner().invoke(main, [command, str(source)]) + + assert result.exit_code == 2 + assert "Got unexpected extra argument" in result.output + + +@pytest.mark.parametrize("command", ("generate", "audit")) +def test_help_lists_only_remote_options(command: str) -> None: + result = CliRunner().invoke(main, [command, "--help"]) + + assert result.exit_code == 0 + for option in ( + "--file", + "--project-dir", + "--env-file", + "--resolve-with", + "--name", + "--sops-pgp", + ): + assert option not in result.output + + +@pytest.mark.parametrize( + "arguments", + ( + ("--playbook",), + ("--playbook-dir", "playbooks"), + ), +) +def test_generate_no_longer_offers_playbook_options(arguments: tuple[str, ...]) -> None: + result = CliRunner().invoke( + main, ["generate", "--host", "web1", "--project", "my-app", *arguments] + ) + + assert result.exit_code == 2 + assert "No such option" in result.output diff --git a/tests/test_config.py b/tests/test_config.py new file mode 100644 index 0000000..34f5128 --- /dev/null +++ b/tests/test_config.py @@ -0,0 +1,145 @@ +from __future__ import annotations + +from pathlib import Path + +import pytest + +from compose2role.config import ( + HostProfile, + find_repository_config, + load_repository_config, +) +from compose2role.errors import ConfigurationError + + +def _write(path: Path, content: str) -> Path: + path.parent.mkdir(parents=True, exist_ok=True) + path.write_text(content, encoding="utf-8") + return path + + +def test_explicit_config_wins_over_environment_and_discovery( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + repository = tmp_path / "repository" + _write(repository / ".compose2role.toml", "[defaults]\noutput = 'discovered'\n") + environment_config = _write( + tmp_path / "environment.toml", "[defaults]\noutput = 'environment'\n" + ) + explicit_config = _write(tmp_path / "explicit.toml", "[defaults]\noutput = 'explicit'\n") + monkeypatch.setenv("COMPOSE2ROLE_CONFIG", str(environment_config)) + + config = load_repository_config(explicit_config, start=repository) + + assert config.path == explicit_config + assert config.output == explicit_config.parent / "explicit" + + +def test_environment_config_wins_over_discovered_config( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + repository = tmp_path / "repository" + _write(repository / ".compose2role.toml", "[defaults]\noutput = 'discovered'\n") + environment_config = _write( + tmp_path / "environment.toml", "[defaults]\noutput = 'environment'\n" + ) + monkeypatch.setenv("COMPOSE2ROLE_CONFIG", str(environment_config)) + + config = load_repository_config(start=repository) + + assert config.path == environment_config + assert config.output == environment_config.parent / "environment" + + +def test_nearest_compose2role_config_is_discovered_walking_up(tmp_path: Path) -> None: + repository = tmp_path / "repository" + discovered = _write( + repository / ".compose2role.toml", "[defaults]\noutput = 'ansible/roles'\n" + ) + nested = repository / "stacks" / "web" + + config = load_repository_config(start=nested, environ={}) + + assert config.path == discovered + assert config.output == repository / "ansible" / "roles" + + +def test_ansible_cfg_is_the_fallback_configuration_root(tmp_path: Path) -> None: + repository = tmp_path / "repository" + ansible_config = _write(repository / "ansible.cfg", "[defaults]\n") + nested = repository / "stacks" / "web" + + config = load_repository_config(start=nested, environ={}) + + assert config.path == ansible_config + assert config.root == repository + + +def test_compose2role_config_beats_a_closer_ansible_cfg(tmp_path: Path) -> None: + _write(tmp_path / "ansible.cfg", "[defaults]\n") + discovered = _write(tmp_path / "stacks" / ".compose2role.toml", "[defaults]\n") + + assert find_repository_config(tmp_path / "stacks" / "web") == discovered + + +def test_defaults_and_host_profiles_are_parsed(tmp_path: Path) -> None: + config_path = _write( + tmp_path / ".compose2role.toml", + """\ +[defaults] +output = "ansible/roles" +sops_config = "ansible/.sops.yaml" +secret_policy = "ansible/policy.toml" + +[hosts.web1] +sudo = true +docker_context = "rootless" +""", + ) + + config = load_repository_config(config_path, environ={}) + + assert config.output == tmp_path / "ansible" / "roles" + assert config.sops_config == tmp_path / "ansible" / ".sops.yaml" + assert config.secret_policy == tmp_path / "ansible" / "policy.toml" + assert config.host("web1") == HostProfile(sudo=True, docker_context="rootless") + assert config.host("unknown") == HostProfile() + + +def test_host_docker_context_is_parsed(tmp_path: Path) -> None: + config_path = _write( + tmp_path / ".compose2role.toml", + '[hosts."web1"]\ndocker_context = "rootless"\n', + ) + + config = load_repository_config(config_path, environ={}) + + assert config.host("web1").docker_context == "rootless" + assert config.host("web1").sudo is None + + +@pytest.mark.parametrize( + "content", + ( + "[defaults\n", + "[unknown]\nvalue = true\n", + "[defaults]\nbogus = true\n", + "[defaults]\nplaybook = 'yes'\n", + "[defaults]\nplaybook = true\n", + "[defaults]\nplaybook_dir = 'playbooks'\n", + "[defaults]\noutput = 1\n", + "[hosts.web1]\nbogus = true\n", + "[hosts.web1]\nsudo = 'yes'\n", + "[hosts.web1]\ndocker_context = 5\n", + ), +) +def test_invalid_configuration_is_a_configuration_error(tmp_path: Path, content: str) -> None: + config_path = _write(tmp_path / ".compose2role.toml", content) + + with pytest.raises(ConfigurationError): + load_repository_config(config_path, environ={}) + + +def test_missing_explicit_configuration_is_a_configuration_error(tmp_path: Path) -> None: + with pytest.raises(ConfigurationError): + load_repository_config(tmp_path / "missing.toml", environ={}) diff --git a/tests/test_docker_context.py b/tests/test_docker_context.py new file mode 100644 index 0000000..e222bec --- /dev/null +++ b/tests/test_docker_context.py @@ -0,0 +1,384 @@ +"""Docker context selection at the command-line and generated-role seams.""" + +from __future__ import annotations + +import base64 +import json +import os +from pathlib import Path + +import pytest +from click.testing import CliRunner +from ruamel.yaml import YAML +from ssh_stub import legacy_source_session_bridge + +from compose2role.cli import main + +_SOURCE_DOCUMENT = """\ +services: + web: + image: "web:1" +""" + +_CANONICAL_DOCUMENT = {"services": {"web": {"image": "web:1"}}} + +_SOPS_CONFIG = """\ +creation_rules: + - path_regex: vars/secrets\\.sops\\.yml$ +""" + +_SSH_STUB = """\ +#!/bin/sh +request=$(cat) + +if [ -n "${C2R_SSH_LOG:-}" ]; then + printf '%s\\n' "$*" >> "$C2R_SSH_LOG" + case "$request" in + *"docker --context rootless compose --project-name"*) + printf 'canonicalized\\n' >> "$C2R_SSH_LOG" + ;; + esac +fi + +case "$*" in + *"compose ls --all --format json"*) + printf '[{"Name":"my-app","Status":"running(1)","ConfigFiles":"/srv/my-app/compose.yaml"}]\\n' + exit 0 + ;; + *" ps --all --format "*) + printf '%s' "${C2R_WORKING_DIRECTORY_LABEL:-}" + exit 0 + ;; +esac + +encoded=$(printf '%s\\n' "$request" | tail -n 2 | head -n 1) +payload=$(printf '%s' "$encoded" | base64 -d) +operation=$(printf '%s\\n' "$payload" | head -n 1) +case "$operation" in + capture) + printf '%s\\n' 'file 640 0 0' + printf '%s\\n' "$C2R_SOURCE_DOCUMENT" + ;; + stat) + printf '%s\\n' missing + ;; + config) + printf '%s\\n' "$C2R_CANONICAL_DOCUMENT" + ;; + *) + exit 97 + ;; +esac +""" + + +def _install(bin_directory: Path, name: str, script: str) -> Path: + executable = bin_directory / name + executable.write_text(script, encoding="utf-8") + executable.chmod(0o755) + return executable + + +def _environment( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch, *, hosts: str = "" +) -> tuple[Path, Path]: + """Return the repository and SSH log for one isolated run.""" + bin_directory = tmp_path / "bin" + bin_directory.mkdir() + legacy_ssh = _install(bin_directory, "ssh-legacy", _SSH_STUB) + _install(bin_directory, "ssh", legacy_source_session_bridge(legacy_ssh)) + _install(bin_directory, "sops", "#!/bin/sh\nexit 99\n") + monkeypatch.setenv("PATH", f"{bin_directory}:{os.environ['PATH']}") + ssh_log = tmp_path / "ssh.log" + monkeypatch.setenv("C2R_SSH_LOG", str(ssh_log)) + monkeypatch.setenv( + "C2R_SOURCE_DOCUMENT", + base64.b64encode(_SOURCE_DOCUMENT.encode("utf-8")).decode("ascii"), + ) + monkeypatch.setenv("C2R_CANONICAL_DOCUMENT", json.dumps(_CANONICAL_DOCUMENT)) + sops_config = tmp_path / "sops.yaml" + sops_config.write_text(_SOPS_CONFIG, encoding="utf-8") + monkeypatch.setenv("COMPOSE2ROLE_SOPS_CONFIG", str(sops_config)) + repository = tmp_path / "repository" + repository.mkdir() + (repository / ".compose2role.toml").write_text( + f'[defaults]\noutput = "roles"\n\n{hosts}', encoding="utf-8" + ) + return repository, ssh_log + + +def _lines(ssh_log: Path) -> list[str]: + if not ssh_log.exists(): + return [] + return ssh_log.read_text(encoding="utf-8").splitlines() + + +def test_discover_passes_the_context_to_the_discovery_command( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + repository, ssh_log = _environment(tmp_path, monkeypatch) + + result = CliRunner().invoke( + main, + [ + "discover", + "--host", + "web1", + "--docker-context", + "rootless", + "--config", + str(repository / ".compose2role.toml"), + ], + ) + + assert result.exit_code == 0, result.output + assert "-- web1 docker --context rootless compose ls --all --format json" in _lines( + ssh_log + ) + + +def test_discover_without_a_context_keeps_the_plain_argv( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + repository, ssh_log = _environment(tmp_path, monkeypatch) + + result = CliRunner().invoke( + main, + ["discover", "--host", "web1", "--config", str(repository / ".compose2role.toml")], + ) + + assert result.exit_code == 0, result.output + assert "-- web1 docker compose ls --all --format json" in _lines(ssh_log) + + +def test_the_configured_context_applies_when_the_flag_is_absent( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + repository, ssh_log = _environment( + tmp_path, monkeypatch, hosts='[hosts."web1"]\ndocker_context = "rootless"\n' + ) + + result = CliRunner().invoke( + main, + ["discover", "--host", "web1", "--config", str(repository / ".compose2role.toml")], + ) + + assert result.exit_code == 0, result.output + assert "-- web1 docker --context rootless compose ls --all --format json" in _lines( + ssh_log + ) + + +def test_the_flag_wins_over_the_configured_context( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + repository, ssh_log = _environment( + tmp_path, monkeypatch, hosts='[hosts."web1"]\ndocker_context = "production"\n' + ) + + result = CliRunner().invoke( + main, + [ + "discover", + "--host", + "web1", + "--docker-context", + "rootless", + "--config", + str(repository / ".compose2role.toml"), + ], + ) + + assert result.exit_code == 0, result.output + lines = _lines(ssh_log) + assert "-- web1 docker --context rootless compose ls --all --format json" in lines + assert not any("production" in line for line in lines) + + +def test_audit_uses_the_context_for_the_working_directory_listing( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + repository, ssh_log = _environment(tmp_path, monkeypatch) + + result = CliRunner().invoke( + main, + [ + "audit", + "--host", + "web1", + "--all", + "--docker-context", + "rootless", + "--config", + str(repository / ".compose2role.toml"), + ], + ) + + assert result.exit_code == 0, result.output + lines = _lines(ssh_log) + assert "-- web1 docker --context rootless compose ls --all --format json" in lines + assert any("docker --context rootless ps --all --format " in line for line in lines) + + +def test_generate_uses_the_context_for_discovery_and_canonicalization( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + repository, ssh_log = _environment(tmp_path, monkeypatch) + + result = CliRunner().invoke( + main, + [ + "generate", + "--host", + "web1", + "--project", + "my-app", + "--docker-context", + "rootless", + "--config", + str(repository / ".compose2role.toml"), + ], + ) + + assert result.exit_code == 0, result.output + lines = _lines(ssh_log) + assert "-- web1 docker --context rootless compose ls --all --format json" in lines + assert "canonicalized" in lines + + +def test_generate_without_a_context_uses_the_plain_argv( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + repository, ssh_log = _environment(tmp_path, monkeypatch) + + result = CliRunner().invoke( + main, + [ + "generate", + "--host", + "web1", + "--project", + "my-app", + "--config", + str(repository / ".compose2role.toml"), + ], + ) + + assert result.exit_code == 0, result.output + lines = _lines(ssh_log) + assert "-- web1 docker compose ls --all --format json" in lines + assert "canonicalized" not in lines + + +@pytest.mark.parametrize( + "arguments", + ( + ("discover", "--host", "web1"), + ("audit", "--host", "web1", "--all"), + ("generate", "--host", "web1", "--project", "my-app"), + ("doctor", "--host", "web1"), + ), +) +def test_an_invalid_context_name_is_a_usage_error_before_ssh( + arguments: tuple[str, ...], tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + repository, ssh_log = _environment(tmp_path, monkeypatch) + + result = CliRunner().invoke( + main, + [ + *arguments, + "--docker-context", + "not a context", + "--config", + str(repository / ".compose2role.toml"), + ], + ) + + assert result.exit_code == 2, result.output + assert "not a context" in result.output + assert _lines(ssh_log) == [] + + +def _generate(tmp_path: Path, repository: Path, output: str, *extra: str) -> object: + return CliRunner().invoke( + main, + [ + "generate", + "--host", + "web1", + "--project", + "my-app", + "--output", + str(tmp_path / output), + "--config", + str(repository / ".compose2role.toml"), + *extra, + ], + ) + + +def test_a_generation_records_the_context_in_the_role( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + repository, _ssh_log = _environment(tmp_path, monkeypatch) + + result = _generate(tmp_path, repository, "context-roles", "--docker-context", "rootless") + + assert result.exit_code == 0, result.output + role = tmp_path / "context-roles" / "my_app" + defaults = YAML(typ="safe").load( + (role / "defaults" / "main.yml").read_text(encoding="utf-8") + ) + tasks = YAML(typ="safe").load( + (role / "tasks" / "main.yml").read_text(encoding="utf-8") + ) + assert defaults["my_app_docker_context"] == "rootless" + compose = next(task for task in tasks if task["name"] == "Deploy my_app Compose project") + assert compose["community.docker.docker_compose_v2"]["cli_context"] == ( + "{{ my_app_docker_context }}" + ) + assert "docker_context" not in (role / ".compose2role.json").read_text(encoding="utf-8") + + +def test_a_context_free_generation_leaves_every_other_file_unchanged( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + repository, _ssh_log = _environment(tmp_path, monkeypatch) + plain = _generate(tmp_path, repository, "plain-roles") + contextual = _generate( + tmp_path, repository, "context-roles", "--docker-context", "rootless" + ) + assert plain.exit_code == 0, plain.output + assert contextual.exit_code == 0, contextual.output + + plain_role = tmp_path / "plain-roles" / "my_app" + context_role = tmp_path / "context-roles" / "my_app" + plain_defaults = (plain_role / "defaults" / "main.yml").read_text(encoding="utf-8") + plain_tasks = (plain_role / "tasks" / "main.yml").read_text(encoding="utf-8") + assert plain_defaults == ( + "---\n" + 'my_app_project_dir: "/srv/my-app"\n' + 'my_app_compose_project_name: "my-app"\n' + 'my_app_web_image: "web"\n' + 'my_app_web_image_tag: "1"\n' + ) + assert "docker_context" not in plain_defaults + assert "cli_context" not in plain_tasks + + plain_files = { + path.relative_to(plain_role): path.read_bytes() + for path in plain_role.rglob("*") + if path.is_file() + } + context_files = { + path.relative_to(context_role): path.read_bytes() + for path in context_role.rglob("*") + if path.is_file() + } + assert set(plain_files) == set(context_files) + for relative, content in plain_files.items(): + if str(relative) in {"defaults/main.yml", "tasks/main.yml"}: + assert context_files[relative] != content + continue + assert context_files[relative] == content, relative diff --git a/tests/test_doctor.py b/tests/test_doctor.py new file mode 100644 index 0000000..0a6d4b2 --- /dev/null +++ b/tests/test_doctor.py @@ -0,0 +1,606 @@ +"""Doctor diagnostics at the command-line seam.""" + +from __future__ import annotations + +import hashlib +import json +from pathlib import Path + +import pytest +from click.testing import CliRunner + +from compose2role import doctor as doctor_module +from compose2role.cli import main + +_SOPS_STUB = """\ +#!/bin/sh +printf 'sops 3.9.0\\n' +exit 0 +""" + +_GPG_STUB = """\ +#!/bin/sh +exit 0 +""" + +_GALAXY_STUB = """\ +#!/bin/sh +if [ "$*" = "collection list --format json" ]; then + printf '%s' '{"collections": {' + printf '%s' '"community.docker": {"version": "5.4.0"}, ' + printf '%s' '"community.sops": {"version": "2.5.0"}}}' + exit 0 +fi +exit 1 +""" + +_EMPTY_STUB = """\ +#!/bin/sh +exit 0 +""" + +_SSH_STUB = """\ +#!/bin/sh +if [ -n "${DOCTOR_SSH_LOG:-}" ]; then + printf '%s\\n' "$*" >> "$DOCTOR_SSH_LOG" +fi +case "$*" in + -G*) + printf 'hostname web1\\nuser operator\\nport 2222\\n' + exit 0 + ;; + *" info --format "*) + printf '%s\\n' "${DOCTOR_DOCKER_INFO:-\"\"}" + exit "${DOCTOR_DOCKER_INFO_STATUS:-0}" + ;; + *"compose ls --all --format json"*) + printf '%s\\n' "${DOCTOR_COMPOSE_LS:-[]}" + exit 0 + ;; + *"compose version"*) + printf 'Docker Compose version %s\\n' "${DOCTOR_COMPOSE_VERSION:-v2.30.0}" + exit 0 + ;; +esac +exit 1 +""" + +_SOPS_CONFIG = """\ +creation_rules: + - path_regex: vars/secrets\\.sops\\.yml$ +""" + + +def _install(bin_directory: Path, name: str, script: str) -> Path: + executable = bin_directory / name + executable.write_text(script, encoding="utf-8") + executable.chmod(0o755) + return executable + + +def _install_ansible_tools(bin_directory: Path) -> None: + _install(bin_directory, "ansible-galaxy", _GALAXY_STUB) + _install(bin_directory, "ansible-lint", _EMPTY_STUB) + _install(bin_directory, "ansible-playbook", _EMPTY_STUB) + + +def _isolate(monkeypatch: pytest.MonkeyPatch, bin_directory: Path) -> None: + monkeypatch.setenv("PATH", str(bin_directory)) + for name in ( + "COMPOSE2ROLE_SSH_BIN", + "COMPOSE2ROLE_SOPS_BIN", + "COMPOSE2ROLE_ANSIBLE_BIN", + "COMPOSE2ROLE_SSH_LOG", + "DOCTOR_SSH_LOG", + "DOCTOR_DOCKER_INFO", + "DOCTOR_DOCKER_INFO_STATUS", + "DOCTOR_COMPOSE_LS", + "DOCTOR_COMPOSE_VERSION", + "ANSIBLE_COLLECTIONS_PATH", + ): + monkeypatch.delenv(name, raising=False) + + +def _set_os_release(monkeypatch: pytest.MonkeyPatch, tmp_path: Path, content: str) -> None: + path = tmp_path / "os-release" + path.write_text(content, encoding="utf-8") + monkeypatch.setattr(doctor_module, "OS_RELEASE_PATH", path) + + +def _repository(tmp_path: Path, output: str = "roles", extra: str = "") -> Path: + repository = tmp_path / "repository" + repository.mkdir() + (repository / ".compose2role.toml").write_text( + f'[defaults]\noutput = "{output}"\n{extra}', encoding="utf-8" + ) + return repository + + +def _invoke(arguments: list[str]) -> object: + return CliRunner().invoke(main, ["doctor", *arguments]) + + +def _checks(result: object) -> dict[str, dict[str, object]]: + assert getattr(result, "exit_code") in {0, 1} + payload = json.loads(getattr(result, "output")) + return {check["name"]: check for check in payload["checks"]} + + +def test_doctor_local_reports_every_check( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + repository = _repository(tmp_path) + (repository / "roles").mkdir() + bin_directory = tmp_path / "bin" + bin_directory.mkdir() + _install(bin_directory, "sops", _SOPS_STUB) + _install(bin_directory, "gpg", _GPG_STUB) + _install_ansible_tools(bin_directory) + _isolate(monkeypatch, bin_directory) + _set_os_release(monkeypatch, tmp_path, "ID=debian\nID_LIKE=debian\n") + + result = _invoke(["--config", str(repository / ".compose2role.toml")]) + + assert getattr(result, "exit_code") == 0 + output = getattr(result, "output") + assert "✓ config:" in output + assert "✓ layout:" in output + assert "✓ sops: sops 3.9.0" in output + assert "✓ ansible collections: found" in output + assert "0 warnings, 0 errors" in output + + +def test_doctor_controller_mode_keeps_its_check_set( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + repository = _repository(tmp_path) + (repository / "roles").mkdir() + bin_directory = tmp_path / "bin" + bin_directory.mkdir() + _install(bin_directory, "sops", _SOPS_STUB) + _install(bin_directory, "gpg", _GPG_STUB) + _install_ansible_tools(bin_directory) + _isolate(monkeypatch, bin_directory) + _set_os_release(monkeypatch, tmp_path, "ID=debian\n") + + result = _invoke( + ["--config", str(repository / ".compose2role.toml"), "--format", "json"] + ) + + assert getattr(result, "exit_code") == 0, getattr(result, "output") + payload = json.loads(getattr(result, "output")) + assert [check["name"] for check in payload["checks"]] == [ + "config", + "layout", + "secret policy", + "output", + "sops", + "gpg", + "sops rule", + "ansible-galaxy", + "ansible collections", + "ansible-lint", + "ansible-playbook", + "roles", + "drift", + "backups", + ] + + +def test_doctor_host_mode_requires_ssh_and_reports_no_controller_checks( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + repository = _repository(tmp_path) + (repository / "roles").mkdir() + bin_directory = tmp_path / "bin" + bin_directory.mkdir() + _isolate(monkeypatch, bin_directory) + _set_os_release(monkeypatch, tmp_path, 'ID="rhel"\nID_LIKE="fedora"\n') + + result = _invoke( + ["--host", "web1", "--config", str(repository / ".compose2role.toml"), "--format", "json"] + ) + + checks = _checks(result) + assert getattr(result, "exit_code") == 1 + assert set(checks) == {"ssh"} + assert checks["ssh"]["status"] == "error" + assert checks["ssh"]["required"] is True + assert "sudo dnf install openssh-clients" in checks["ssh"]["hint"] + assert json.loads(getattr(result, "output"))["summary"]["failed"] is True + + +def test_doctor_remote_resolves_ssh_and_checks_remote_compose( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + repository = _repository(tmp_path) + (repository / "roles").mkdir() + ssh_log = tmp_path / "ssh.log" + bin_directory = tmp_path / "bin" + bin_directory.mkdir() + _install(bin_directory, "ssh", _SSH_STUB) + _isolate(monkeypatch, bin_directory) + monkeypatch.setenv("DOCTOR_SSH_LOG", str(ssh_log)) + _set_os_release(monkeypatch, tmp_path, "ID=debian\n") + + result = _invoke( + ["--host", "web1", "--config", str(repository / ".compose2role.toml")] + ) + + assert getattr(result, "exit_code") == 0 + output = getattr(result, "output") + assert "✓ ssh resolution: ssh -G web1 resolved hostname web1, user operator, port 2222" in ( + output + ) + assert "✓ remote docker:" in output + assert "✓ remote compose: remote docker compose v2.30.0" in output + assert "sops" not in output + invocations = ssh_log.read_text(encoding="utf-8").splitlines() + assert "-G -- web1" in invocations + assert "-- web1 docker compose version" in invocations + + +def _host_environment( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch, **environment: str +) -> Path: + """Install the host-mode stubs and return the repository root.""" + repository = _repository(tmp_path) + (repository / "roles").mkdir() + bin_directory = tmp_path / "bin" + bin_directory.mkdir() + _install(bin_directory, "ssh", _SSH_STUB) + _install(bin_directory, "sops", _SOPS_STUB) + _install(bin_directory, "gpg", _GPG_STUB) + _install_ansible_tools(bin_directory) + _isolate(monkeypatch, bin_directory) + for name, value in environment.items(): + monkeypatch.setenv(name, value) + _set_os_release(monkeypatch, tmp_path, "ID=debian\n") + return repository + + +def test_doctor_remote_sudo_prefixes_every_remote_command( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + repository = _host_environment(tmp_path, monkeypatch, DOCTOR_SSH_LOG=str(tmp_path / "ssh.log")) + + result = _invoke( + ["--host", "web1", "--sudo", "--config", str(repository / ".compose2role.toml")] + ) + + assert getattr(result, "exit_code") == 0, getattr(result, "output") + invocations = (tmp_path / "ssh.log").read_text(encoding="utf-8") + assert "-- web1 sudo -n docker info" in invocations + assert "-- web1 sudo -n docker compose version" in invocations + + +def test_doctor_host_mode_applies_the_docker_context( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + repository = _host_environment( + tmp_path, monkeypatch, DOCTOR_SSH_LOG=str(tmp_path / "ssh.log") + ) + + result = _invoke( + [ + "--host", + "web1", + "--docker-context", + "rootless", + "--config", + str(repository / ".compose2role.toml"), + ] + ) + + assert getattr(result, "exit_code") == 0, getattr(result, "output") + invocations = (tmp_path / "ssh.log").read_text(encoding="utf-8") + assert "-- web1 docker --context rootless info" in invocations + assert "-- web1 docker --context rootless compose version" in invocations + + +def test_doctor_quotes_the_docker_info_format_for_the_remote_shell( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + repository = _host_environment( + tmp_path, monkeypatch, DOCTOR_SSH_LOG=str(tmp_path / "ssh.log") + ) + + result = _invoke( + ["--host", "web1", "--config", str(repository / ".compose2role.toml")] + ) + + assert getattr(result, "exit_code") == 0, getattr(result, "output") + invocations = (tmp_path / "ssh.log").read_text(encoding="utf-8") + assert "-- web1 docker info --format '{{json .}}'" in invocations + + +def test_doctor_host_mode_reports_only_the_host_check_set( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + repository = _host_environment(tmp_path, monkeypatch) + + result = _invoke( + ["--host", "web1", "--format", "json", "--config", str(repository / ".compose2role.toml")] + ) + + assert getattr(result, "exit_code") == 0, getattr(result, "output") + payload = json.loads(getattr(result, "output")) + assert [check["name"] for check in payload["checks"]] == [ + "ssh", + "ssh resolution", + "remote docker", + "remote compose", + ] + + +def test_doctor_host_mode_fails_when_the_daemon_is_unreachable( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + repository = _host_environment(tmp_path, monkeypatch, DOCTOR_DOCKER_INFO_STATUS="1") + + result = _invoke( + ["--host", "web1", "--format", "json", "--config", str(repository / ".compose2role.toml")] + ) + + checks = _checks(result) + assert getattr(result, "exit_code") == 1 + assert checks["remote docker"]["status"] == "error" + assert checks["remote docker"]["required"] is True + assert "not reachable" in str(checks["remote docker"]["detail"]) + + +def test_doctor_host_mode_reports_the_daemon_mode_and_project_count( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + repository = _host_environment( + tmp_path, + monkeypatch, + DOCTOR_DOCKER_INFO='{"SecurityOptions":["name=rootless"]}', + DOCTOR_COMPOSE_LS='[{"Name":"one"},{"Name":"two"}]', + ) + + result = _invoke( + ["--host", "web1", "--format", "json", "--config", str(repository / ".compose2role.toml")] + ) + + check = _checks(result)["remote docker"] + assert check["status"] == "ok" + assert "rootless" in str(check["detail"]) + assert "2 visible Compose project(s)" in str(check["detail"]) + + +def test_doctor_host_mode_accepts_compose_v5( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + repository = _host_environment(tmp_path, monkeypatch, DOCTOR_COMPOSE_VERSION="v5.7.0") + + result = _invoke( + ["--host", "web1", "--format", "json", "--config", str(repository / ".compose2role.toml")] + ) + + checks = _checks(result) + assert getattr(result, "exit_code") == 0 + assert checks["remote compose"]["status"] == "ok" + assert "v5.7.0" in str(checks["remote compose"]["detail"]) + + +def test_doctor_host_mode_rejects_compose_v1( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + repository = _host_environment(tmp_path, monkeypatch, DOCTOR_COMPOSE_VERSION="v1.29.2") + + result = _invoke( + ["--host", "web1", "--format", "json", "--config", str(repository / ".compose2role.toml")] + ) + + checks = _checks(result) + assert getattr(result, "exit_code") == 1 + assert checks["remote compose"]["status"] == "error" + assert "v1.29.2" in str(checks["remote compose"]["detail"]) + + +def test_doctor_finds_repo_local_collections( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + repository = _repository(tmp_path) + (repository / "roles").mkdir() + snapshot = repository / "collections" / "ansible_collections" + for name in ("community/docker", "community/sops"): + collection = snapshot / name + collection.mkdir(parents=True) + (collection / "galaxy.yml").write_text("namespace: community\n", encoding="utf-8") + bin_directory = tmp_path / "bin" + bin_directory.mkdir() + _install( + bin_directory, + "ansible-galaxy", + "#!/bin/sh\nif [ \"$*\" = 'collection list --format json' ]; then\n" + " printf '%s' '{}'\n exit 0\nfi\nexit 1\n", + ) + _install(bin_directory, "ansible-lint", _EMPTY_STUB) + _install(bin_directory, "ansible-playbook", _EMPTY_STUB) + _isolate(monkeypatch, bin_directory) + _set_os_release(monkeypatch, tmp_path, "ID=debian\n") + + result = _invoke(["--config", str(repository / ".compose2role.toml")]) + + assert getattr(result, "exit_code") == 0, getattr(result, "output") + assert "✓ ansible collections: found community.docker, community.sops" in ( + getattr(result, "output") + ) + + +def test_doctor_honours_the_ansible_collections_path( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + repository = _repository(tmp_path) + (repository / "roles").mkdir() + external = tmp_path / "external-collections" + for name in ("community/docker", "community/sops"): + collection = external / "ansible_collections" / name + collection.mkdir(parents=True) + (collection / "galaxy.yml").write_text("namespace: community\n", encoding="utf-8") + bin_directory = tmp_path / "bin" + bin_directory.mkdir() + _install( + bin_directory, + "ansible-galaxy", + "#!/bin/sh\nif [ \"$*\" = 'collection list --format json' ]; then\n" + " printf '%s' '{}'\n exit 0\nfi\nexit 1\n", + ) + _install(bin_directory, "ansible-lint", _EMPTY_STUB) + _install(bin_directory, "ansible-playbook", _EMPTY_STUB) + _isolate(monkeypatch, bin_directory) + monkeypatch.setenv("ANSIBLE_COLLECTIONS_PATH", str(external)) + _set_os_release(monkeypatch, tmp_path, "ID=debian\n") + + result = _invoke(["--config", str(repository / ".compose2role.toml")]) + + assert getattr(result, "exit_code") == 0, getattr(result, "output") + assert "✓ ansible collections: found" in getattr(result, "output") + + +def test_doctor_distro_hints_name_the_package_manager( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + repository = _repository(tmp_path) + (repository / "roles").mkdir() + bin_directory = tmp_path / "bin" + bin_directory.mkdir() + _isolate(monkeypatch, bin_directory) + _set_os_release(monkeypatch, tmp_path, 'ID="rocky"\nID_LIKE="rhel centos fedora"\n') + + result = _invoke(["--config", str(repository / ".compose2role.toml")]) + + output = getattr(result, "output") + assert "sudo dnf install sops (EPEL)" in output + assert "sudo dnf install gnupg2" in output + + +def test_doctor_reports_configuration_and_secret_policy_failures( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + bin_directory = tmp_path / "bin" + bin_directory.mkdir() + _isolate(monkeypatch, bin_directory) + _set_os_release(monkeypatch, tmp_path, "ID=debian\n") + + invalid_config = tmp_path / "invalid.toml" + invalid_config.write_text("this is not = toml", encoding="utf-8") + config_result = _invoke(["--config", str(invalid_config)]) + assert getattr(config_result, "exit_code") == 2 + assert "✗ config:" in getattr(config_result, "output") + + repository = _repository(tmp_path, extra='secret_policy = "policy.toml"\n') + (repository / "roles").mkdir() + (repository / "policy.toml").write_text("rules = 5\n", encoding="utf-8") + secret_policy_result = _invoke(["--config", str(repository / ".compose2role.toml")]) + assert getattr(secret_policy_result, "exit_code") == 0 + assert "! secret policy: secret policy must define rules" in getattr( + secret_policy_result, "output" + ) + + +def test_doctor_reports_an_unwritable_output_directory( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + repository = _repository(tmp_path, output="blocked/roles") + (repository / "blocked").write_text("not a directory", encoding="utf-8") + bin_directory = tmp_path / "bin" + bin_directory.mkdir() + _isolate(monkeypatch, bin_directory) + _set_os_release(monkeypatch, tmp_path, "ID=debian\n") + + result = _invoke(["--config", str(repository / ".compose2role.toml")]) + + assert getattr(result, "exit_code") == 1 + assert "✗ output: roles directory is not writable" in getattr(result, "output") + + +def test_doctor_validates_a_configured_sops_rule( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + repository = _repository(tmp_path) + (repository / "roles").mkdir() + invalid_config = tmp_path / "invalid-sops.yaml" + invalid_config.write_text("creation_rules: []\n", encoding="utf-8") + bin_directory = tmp_path / "bin" + bin_directory.mkdir() + _install(bin_directory, "sops", _SOPS_STUB) + _isolate(monkeypatch, bin_directory) + _set_os_release(monkeypatch, tmp_path, "ID=debian\n") + + result = _invoke( + [ + "--config", + str(repository / ".compose2role.toml"), + "--sops-config", + str(invalid_config), + ] + ) + + assert getattr(result, "exit_code") == 1 + assert "✗ sops rule:" in getattr(result, "output") + + +def _write_role(roles_directory: Path, project_root: Path, content: bytes) -> Path: + project_root.mkdir() + (project_root / "compose.yaml").write_bytes(content) + role_directory = roles_directory / "my_app" + role_directory.mkdir() + provenance = { + "files": { + "compose.yaml": { + "redacted": False, + "sha256": hashlib.sha256(content).hexdigest(), + } + }, + "generator": "compose2role", + "mode": "local", + "project": "my-app", + "role": "my_app", + "source": { + "config_files": [str(project_root / "compose.yaml")], + "root": str(project_root), + }, + "structural_hash": "fixture", + "version": "0.1.0", + } + (role_directory / ".compose2role.json").write_text( + json.dumps(provenance), encoding="utf-8" + ) + return role_directory + + +def test_doctor_reports_roles_drift_and_backups( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + repository = _repository(tmp_path) + roles_directory = repository / "roles" + roles_directory.mkdir() + project_root = tmp_path / "project" + _write_role(roles_directory, project_root, b"services:\n web:\n image: web:1\n") + backup = roles_directory / ".compose2role" / "backups" / "my_app-20260101T000000Z" + backup.mkdir(parents=True) + (backup / "compose.yaml").write_bytes(b"x" * 2048) + bin_directory = tmp_path / "bin" + bin_directory.mkdir() + _isolate(monkeypatch, bin_directory) + _set_os_release(monkeypatch, tmp_path, "ID=debian\n") + + result = _invoke(["--config", str(repository / ".compose2role.toml")]) + + assert getattr(result, "exit_code") == 0 + output = getattr(result, "output") + assert "✓ roles: 1 generated role(s)" in output + assert "✓ drift: 1 unchanged, 0 source changed, 0 source unavailable" in output + assert "✓ backups: 1 backup(s), 2.0 KiB" in output + assert "prune with: rm -r" in output + + (project_root / "compose.yaml").write_bytes(b"services:\n web:\n image: web:2\n") + drifted = _invoke(["--config", str(repository / ".compose2role.toml")]) + + assert getattr(drifted, "exit_code") == 0 + assert "! drift: 0 unchanged, 1 source changed, 0 source unavailable" in getattr( + drifted, "output" + ) diff --git a/tests/test_generate.py b/tests/test_generate.py new file mode 100644 index 0000000..d6b06ae --- /dev/null +++ b/tests/test_generate.py @@ -0,0 +1,218 @@ +from __future__ import annotations + +import json +import os +from importlib.metadata import version as package_version +from pathlib import Path + +import pytest +from click.testing import CliRunner + +from compose2role.cli import main + + +def test_version_reports_the_installed_package_version() -> None: + result = CliRunner().invoke(main, ["--version"], prog_name="compose2role") + + assert result.exit_code == 0, result.output + assert result.output == f"compose2role {package_version('compose2role')}\n" + + +def _install_stub_ssh(tmp_path: Path, script: str) -> Path: + bin_directory = tmp_path / "bin" + bin_directory.mkdir() + ssh = bin_directory / "ssh" + ssh.write_text(script, encoding="utf-8") + ssh.chmod(0o755) + return bin_directory + + +def _ssh_environment(bin_directory: Path, arguments_file: Path | None = None) -> dict[str, str]: + environment = {"PATH": f"{bin_directory}:{os.environ['PATH']}"} + if arguments_file is not None: + environment["C2R_SSH_ARGUMENTS"] = str(arguments_file) + return environment + + +def test_discover_lists_remote_compose_projects_as_json(tmp_path: Path) -> None: + bin_directory = _install_stub_ssh( + tmp_path, + """\ +#!/bin/sh +printf '%s\\n' '[{"Name":"myapp","Status":"running(1)","ConfigFiles":"/srv/myapp/compose.yaml"}]' +""", + ) + + result = CliRunner().invoke( + main, + ["discover", "--host", "web1", "--format", "json"], + env=_ssh_environment(bin_directory), + ) + + assert result.exit_code == 0, result.output + assert json.loads(result.output) == { + "projects": [ + { + "config_files": ["/srv/myapp/compose.yaml"], + "name": "myapp", + "status": "running(1)", + } + ] + } + + +def test_discover_prints_one_status_line_per_project(tmp_path: Path) -> None: + """Text discovery streams a status line per project and closes with a count.""" + bin_directory = _install_stub_ssh( + tmp_path, + """\ +#!/bin/sh +printf '%s\\n' '[{"Name":"myapp","Status":"running(1)",' +printf '%s\\n' '"ConfigFiles":"/srv/myapp/compose.yaml"}]' +""", + ) + + result = CliRunner().invoke( + main, + ["discover", "--host", "web1"], + env=_ssh_environment(bin_directory), + ) + + assert result.exit_code == 0, result.output + assert result.output.splitlines() == [ + "Discovering projects on web1...", + "✓ myapp running(1) /srv/myapp/compose.yaml", + "", + "1 found", + ] + + +@pytest.mark.parametrize( + ("arguments", "expected_ssh_arguments"), + ( + ( + ["discover", "--host", "admin@web1.example:2222", "--format", "json"], + [ + "-p", + "2222", + "--", + "admin@web1.example", + "docker", + "compose", + "ls", + "--all", + "--format", + "json", + ], + ), + ( + [ + "discover", + "--host", + "admin@web1.example:2222", + "--sudo", + "--format", + "json", + ], + [ + "-p", + "2222", + "--", + "admin@web1.example", + "sudo", + "-n", + "docker", + "compose", + "ls", + "--all", + "--format", + "json", + ], + ), + ), +) +def test_discover_passes_fixed_ssh_arguments( + tmp_path: Path, arguments: list[str], expected_ssh_arguments: list[str] +) -> None: + arguments_file = tmp_path / "ssh-arguments" + bin_directory = _install_stub_ssh( + tmp_path, + """\ +#!/bin/sh +printf '%s\\n' "$@" > "$C2R_SSH_ARGUMENTS" +printf '%s\\n' '[]' +""", + ) + + result = CliRunner().invoke( + main, + arguments, + env=_ssh_environment(bin_directory, arguments_file), + ) + + assert result.exit_code == 0, result.output + assert arguments_file.read_text(encoding="utf-8").splitlines() == expected_ssh_arguments + + +@pytest.mark.parametrize( + ("arguments", "expected_message"), + ( + ( + ["discover", "--host", "web1", "--sudo"], + "ensure the SSH user has passwordless sudo access to Docker", + ), + ( + ["discover", "--host", "web1"], + "join the docker group", + ), + ), +) +def test_discover_explains_remote_docker_access_failures( + tmp_path: Path, arguments: list[str], expected_message: str +) -> None: + bin_directory = _install_stub_ssh(tmp_path, "#!/bin/sh\nexit 1\n") + + result = CliRunner().invoke( + main, + arguments, + env=_ssh_environment(bin_directory), + ) + + assert result.exit_code == 1 + assert expected_message in result.output + + +def test_discover_enumerates_the_access_remedies_without_recommending_one( + tmp_path: Path, +) -> None: + bin_directory = _install_stub_ssh(tmp_path, "#!/bin/sh\nexit 1\n") + + result = CliRunner().invoke( + main, + ["discover", "--host", "web1"], + env=_ssh_environment(bin_directory), + ) + + assert result.exit_code == 1 + assert "join the docker group" in result.output + assert "configure passwordless sudo" in result.output + assert "--docker-context" in result.output + assert "--sudo" not in result.output + + +def test_discover_reports_ssh_connection_failures_without_a_sudo_hint(tmp_path: Path) -> None: + bin_directory = _install_stub_ssh( + tmp_path, + "#!/bin/sh\nprintf '%s\\n' 'Host key verification failed.' >&2\nexit 255\n", + ) + + result = CliRunner().invoke( + main, + ["discover", "--host", "web1", "--sudo"], + env=_ssh_environment(bin_directory), + ) + + assert result.exit_code == 1 + assert "SSH connection or remote command failed" in result.output + assert "Host key verification failed." in result.output + assert "passwordless sudo" not in result.output diff --git a/tests/test_layout.py b/tests/test_layout.py new file mode 100644 index 0000000..a64406a --- /dev/null +++ b/tests/test_layout.py @@ -0,0 +1,200 @@ +from __future__ import annotations + +from pathlib import Path + +import pytest +from click.testing import CliRunner + +from compose2role.cli import main +from compose2role.config import RepositoryConfig, load_repository_config +from compose2role.layout import discover_layout, find_ansible_config, load_ansible_config +from compose2role.naming import normalize_compose_project_name, normalize_role_name + + +def _write(path: Path, content: str) -> Path: + path.parent.mkdir(parents=True, exist_ok=True) + path.write_text(content, encoding="utf-8") + return path + + +def _config(tmp_path: Path, content: str) -> RepositoryConfig: + return load_repository_config(_write(tmp_path / ".compose2role.toml", content), environ={}) + + +def test_roles_directory_prefers_explicit_output(tmp_path: Path) -> None: + layout = discover_layout( + RepositoryConfig(root=tmp_path), + start=tmp_path, + explicit_output=tmp_path / "explicit", + environ={}, + ) + + assert layout.roles_directory == tmp_path / "explicit" + + +def test_roles_directory_uses_config_defaults_before_ansible_cfg(tmp_path: Path) -> None: + _write(tmp_path / "ansible.cfg", "[defaults]\nroles_path = ansible/roles\n") + config = _config(tmp_path, "[defaults]\noutput = 'configured/roles'\n") + + layout = discover_layout(config, start=tmp_path, environ={}) + + assert layout.roles_directory == tmp_path / "configured" / "roles" + + +def test_roles_directory_uses_the_first_ansible_cfg_roles_path_entry(tmp_path: Path) -> None: + _write( + tmp_path / "ansible.cfg", + "[defaults]\nroles_path = ansible/roles:extra/roles\n", + ) + + layout = discover_layout(RepositoryConfig(root=tmp_path), start=tmp_path, environ={}) + + assert layout.roles_directory == tmp_path / "ansible" / "roles" + + +def test_roles_directory_falls_back_to_existing_ansible_roles(tmp_path: Path) -> None: + (tmp_path / "ansible" / "roles").mkdir(parents=True) + + layout = discover_layout(RepositoryConfig(root=tmp_path), start=tmp_path, environ={}) + + assert layout.roles_directory == tmp_path / "ansible" / "roles" + + +def test_roles_directory_falls_back_to_existing_roles(tmp_path: Path) -> None: + (tmp_path / "roles").mkdir() + + layout = discover_layout(RepositoryConfig(root=tmp_path), start=tmp_path, environ={}) + + assert layout.roles_directory == tmp_path / "roles" + + +def test_roles_directory_falls_back_to_config_root_roles(tmp_path: Path) -> None: + layout = discover_layout(RepositoryConfig(root=tmp_path), start=tmp_path, environ={}) + + assert layout.roles_directory == tmp_path / "roles" + + +def test_ansible_cfg_only_reads_the_documented_defaults_keys(tmp_path: Path) -> None: + _write( + tmp_path / "ansible.cfg", + """\ +[defaults] +roles_path = documented/roles +unknown = ignored + +[other] +roles_path = ignored/roles +""", + ) + + layout = discover_layout(RepositoryConfig(root=tmp_path), start=tmp_path, environ={}) + + assert layout.roles_directory == tmp_path / "documented" / "roles" + + +def test_ansible_cfg_paths_resolve_relative_to_the_config_file(tmp_path: Path) -> None: + ansible_config = _write( + tmp_path / "ansible.cfg", + """\ +[defaults] +roles_path = roles +collections_path = collections:more-collections +inventory = inventory/hosts.ini:inventory/extra.ini +""", + ) + + parsed = load_ansible_config(ansible_config) + + assert parsed.roles_path == (tmp_path / "roles",) + assert parsed.collections_path == (tmp_path / "collections", tmp_path / "more-collections") + assert parsed.inventory == ( + tmp_path / "inventory" / "hosts.ini", + tmp_path / "inventory" / "extra.ini", + ) + + +def test_inventory_directory_is_discovered_without_ansible_cfg(tmp_path: Path) -> None: + inventory = tmp_path / "inventory" + inventory.mkdir() + + layout = discover_layout(RepositoryConfig(root=tmp_path), start=tmp_path, environ={}) + + assert layout.inventory == (inventory,) + + +def test_find_ansible_config_walks_up(tmp_path: Path) -> None: + ansible_config = _write(tmp_path / "ansible.cfg", "[defaults]\n") + nested = tmp_path / "stacks" / "web" + + assert find_ansible_config(nested) == ansible_config + + +def test_sops_config_precedence(tmp_path: Path) -> None: + config = RepositoryConfig(root=tmp_path, sops_config=tmp_path / "configured.yaml") + + explicit = discover_layout( + config, + start=tmp_path, + explicit_sops_config=tmp_path / "explicit.yaml", + environ={}, + ) + environment = discover_layout( + config, + start=tmp_path, + environ={"COMPOSE2ROLE_SOPS_CONFIG": str(tmp_path / "environment.yaml")}, + ) + configured = discover_layout(config, start=tmp_path, environ={}) + + assert explicit.sops_config == tmp_path / "explicit.yaml" + assert environment.sops_config == tmp_path / "environment.yaml" + assert configured.sops_config == tmp_path / "configured.yaml" + + +def test_secret_policy_precedence(tmp_path: Path) -> None: + config = RepositoryConfig(root=tmp_path, secret_policy=tmp_path / "configured.toml") + + explicit = discover_layout( + config, + start=tmp_path, + explicit_secret_policy=tmp_path / "explicit.toml", + environ={}, + ) + environment = discover_layout( + config, + start=tmp_path, + environ={"COMPOSE2ROLE_SECRET_POLICY": str(tmp_path / "environment.toml")}, + ) + configured = discover_layout(config, start=tmp_path, environ={}) + + assert explicit.secret_policy == tmp_path / "explicit.toml" + assert environment.secret_policy == tmp_path / "environment.toml" + assert configured.secret_policy == tmp_path / "configured.toml" + + +def test_host_profile_is_selected_for_the_target_host(tmp_path: Path) -> None: + config = _config(tmp_path, "[hosts.web1]\nsudo = true\n") + + layout = discover_layout(config, start=tmp_path, host="web1", environ={}) + other = discover_layout(config, start=tmp_path, host="web2", environ={}) + + assert layout.host.sudo is True + assert other.host.sudo is None + + +def test_normalizers_produce_valid_names() -> None: + assert normalize_role_name("My App") == "my_app" + assert normalize_role_name("123") == "compose_123" + assert normalize_compose_project_name("My App") == "myapp" + assert normalize_compose_project_name("---") == "compose" + + +def test_invalid_configuration_exits_with_usage_status( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + _write(tmp_path / ".compose2role.toml", "[defaults\n") + monkeypatch.chdir(tmp_path) + + result = CliRunner().invoke(main, ["discover", "--host", "web1"]) + + assert result.exit_code == 2, result.output + assert "configuration" in result.output.lower() diff --git a/tests/test_remote_audit.py b/tests/test_remote_audit.py new file mode 100644 index 0000000..5ac5eea --- /dev/null +++ b/tests/test_remote_audit.py @@ -0,0 +1,2431 @@ +"""Remote audit behavior at the resolver and command-line seams.""" + +from __future__ import annotations + +import base64 +import hashlib +import json +import os +from pathlib import Path, PurePosixPath + +import pytest +from click.testing import CliRunner +from ssh_stub import legacy_source_session_bridge + +from compose2role.cli import main +from compose2role.remote import resolve_remote_projects + + +def _install_stub_ssh(tmp_path: Path, script: str, *, source_session: bool = False) -> Path: + bin_directory = tmp_path / "bin" + bin_directory.mkdir() + ssh = bin_directory / "ssh" + if source_session: + ssh.write_text(script, encoding="utf-8") + ssh.chmod(0o755) + return bin_directory + legacy_ssh = bin_directory / "ssh-legacy" + legacy_ssh.write_text(script, encoding="utf-8") + legacy_ssh.chmod(0o755) + ssh.write_text(legacy_source_session_bridge(legacy_ssh), encoding="utf-8") + ssh.chmod(0o755) + return bin_directory + + +def _ssh_environment(bin_directory: Path, log_file: Path) -> dict[str, str]: + return { + "C2R_SSH_LOG": str(log_file), + "PATH": f"{bin_directory}:{os.environ['PATH']}", + } + + +_DRIFT_STUB = """\ +#!/bin/sh +request=$(cat) + +if [ "$*" = '-- web1 docker compose ls --all --format json' ]; then + printf '%s\\n' '[{"Name":"myapp","Status":"running(1)","ConfigFiles":"/srv/myapp/compose.yaml"}]' + exit 0 +fi + +if [ "$*" = '-- web1 sh -s' ]; then + encoded=$(printf '%s\\n' "$request" | tail -n 2 | head -n 1) + payload=$(printf '%s' "$encoded" | base64 -d) + operation=$(printf '%s\\n' "$payload" | head -n 1) + case "$operation" in + capture) + printf '%s\n' 'file 640 0 0' + base64 <<'EOF' | tr -d '\n' +name: myapp +services: + web: + image: ghcr.io/example/web:IMAGE_TAG +EOF + printf '\n' + ;; + stat) + printf '%s\\n' 'missing' + ;; + config) + printf '%s\\n' '{"name":"myapp","services":{"web":{"image":"ghcr.io/example/web:IMAGE_TAG"}}}' + ;; + *) + exit 97 + ;; + esac + exit 0 +fi + +exit 97 +""" + + +def _drift_stub(image_tag: str) -> str: + return _DRIFT_STUB.replace("IMAGE_TAG", image_tag) + + +def test_audit_reports_remote_drift_and_all_summary( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + monkeypatch.chdir(tmp_path) + source_text = """\ +name: myapp +services: + web: + image: ghcr.io/example/web:1.2.3 +""" + role_directory = tmp_path / "roles" / "myapp" + role_directory.mkdir(parents=True) + (role_directory / ".compose2role.json").write_text( + json.dumps( + { + "files": { + "compose.yaml": { + "sha256": hashlib.sha256(source_text.encode("utf-8")).hexdigest() + } + } + } + ), + encoding="utf-8", + ) + + unchanged_root = tmp_path / "unchanged" + unchanged_root.mkdir() + unchanged_bin = _install_stub_ssh(unchanged_root, _drift_stub("1.2.3")) + unchanged = CliRunner().invoke( + main, + ["audit", "--host", "web1", "--project", "myapp", "--format", "json"], + env=_ssh_environment(unchanged_bin, tmp_path / "unchanged-ssh-log"), + ) + + assert unchanged.exit_code == 0, unchanged.output + assert json.loads(unchanged.output)["projects"][0]["drift"] == { + "changed": [], + "role": "myapp", + "state": "unchanged", + } + + changed_root = tmp_path / "changed" + changed_root.mkdir() + changed_bin = _install_stub_ssh(changed_root, _drift_stub("1.2.4")) + changed = CliRunner().invoke( + main, + ["audit", "--host", "web1", "--all", "--format", "json"], + env=_ssh_environment(changed_bin, tmp_path / "changed-ssh-log"), + ) + + assert changed.exit_code == 0, changed.output + assert json.loads(changed.output)["projects"][0]["drift"] == { + "changed": ["compose.yaml"], + "role": "myapp", + "state": "source changed", + } + + text_result = CliRunner().invoke( + main, + ["audit", "--host", "web1", "--all"], + env=_ssh_environment(changed_bin, tmp_path / "text-ssh-log"), + ) + + assert text_result.exit_code == 0, text_result.output + assert text_result.output.splitlines() == [ + "Discovering projects on web1...", + "✓ myapp (source changed: compose.yaml)", + "", + "1 ok, 1 drift", + ] + + +def test_audit_resolves_one_remote_project_without_exposing_source_paths_in_ssh_argv( + tmp_path: Path, +) -> None: + log_file = tmp_path / "ssh-log" + bin_directory = _install_stub_ssh( + tmp_path, + """\ +#!/bin/sh +request=$(cat) +{ + printf '%s\\n' 'ARGV' + printf '%s\\n' "$@" + printf '%s\\n' 'STDIN' + printf '%s\\n' "$request" + printf '%s\\n' 'END' +} >> "$C2R_SSH_LOG" + +if [ "$*" = '-- web1 docker compose ls --all --format json' ]; then + printf '%s\\n' '[{"Name":"myapp","Status":"running(1)","ConfigFiles":"/srv/myapp/compose.yaml"}]' + exit 0 +fi + +if [ "$*" = '-- web1 sh -s' ]; then + encoded=$(printf '%s\\n' "$request" | tail -n 2 | head -n 1) + payload=$(printf '%s' "$encoded" | base64 -d) + operation=$(printf '%s\\n' "$payload" | head -n 1) + case "$operation" in + capture) + printf '%s\n' 'file 640 0 0' + base64 <<'EOF' | tr -d '\n' +services: + web: + image: ghcr.io/example/web:1.2.3 +EOF + printf '\n' + ;; + stat) + printf '%s\\n' 'missing' + ;; + config) + printf '%s\\n' '{"services":{"web":{"image":"ghcr.io/example/web:1.2.3",' + printf '%s\\n' '"user":"1000:1000","healthcheck":{"test":["CMD","true"]},' + printf '%s\\n' '"restart":"unless-stopped","environment":{}}}}' + ;; + *) + exit 97 + ;; + esac + exit 0 +fi + +exit 97 +""", + ) + + result = CliRunner().invoke( + main, + ["audit", "--host", "web1", "--project", "myapp", "--format", "json"], + env=_ssh_environment(bin_directory, log_file), + ) + + assert result.exit_code == 0, result.output + assert json.loads(result.output) == { + "projects": [ + { + "classifications": [], + "drift": { + "changed": [], + "reason": "role has not been generated", + "role": "myapp", + "state": "missing", + }, + "error": None, + "findings": [], + "name": "myapp", + "role": None, + "status": "ok", + } + ] + } + log = log_file.read_text(encoding="utf-8") + assert "/srv/myapp/compose.yaml" not in log.split("STDIN", maxsplit=1)[0] + assert "ARGV\n--\nweb1\nsh\n-s\nSTDIN" in log + expected_payload = base64.b64encode( + b"config\nmyapp\n/srv/myapp\n/srv/myapp/compose.yaml\n" + ).decode("ascii") + assert expected_payload in log + + +@pytest.mark.parametrize( + ("label_output", "expected_root", "expected_error", "forbidden_label_value"), + ( + ("myapp\t/srv/stacks/myapp\n", "/srv/stacks/myapp", None, None), + ("", "/srv/stacks/myapp/compose", None, None), + ( + "myapp\t/srv/stacks/myapp\nmyapp\t/srv/other\n", + None, + "remote Compose project has conflicting working-directory labels", + "/srv/other", + ), + ( + "myapp\tsource/relative\n", + None, + "remote Compose project working directory must be absolute", + "source/relative", + ), + ( + "myapp\t/srv/stacks/myapp/../other\n", + None, + "remote Compose project working directory escapes the project root", + "/srv/stacks/myapp/../other", + ), + ), + ids=("label", "fallback", "conflict", "relative", "escape"), +) +def test_audit_uses_the_labeled_compose_project_directory_when_available( + tmp_path: Path, + label_output: str, + expected_root: str | None, + expected_error: str | None, + forbidden_label_value: str | None, +) -> None: + """The reported Compose working directory controls remote config and source containment.""" + payload_log = tmp_path / "payload-log" + bin_directory = _install_stub_ssh( + tmp_path, + """\ +#!/bin/sh +request=$(cat) + +if [ "$*" = '-- web1 docker compose ls --all --format json' ]; then + printf '%s\\n' '[{"Name":"myapp","Status":"running(1)",' + printf '%s\\n' '"ConfigFiles":"/srv/stacks/myapp/compose/compose.yaml"}]' + exit 0 +fi + +case "$*" in + *"docker ps"*) + printf '%s' "$C2R_LABEL_OUTPUT" + exit 0 + ;; +esac + +if [ "$*" = '-- web1 sh -s' ]; then + encoded=$(printf '%s\\n' "$request" | tail -n 2 | head -n 1) + payload=$(printf '%s' "$encoded" | base64 -d) + printf '%s\\n' "$payload" >> "$C2R_PAYLOAD_LOG" + operation=$(printf '%s\\n' "$payload" | head -n 1) + case "$operation" in + capture) + printf '%s\\n' 'file 640 0 0' + base64 <<'EOF' | tr -d '\\n' +services: + web: + image: ghcr.io/example/web:1.2.3 +EOF + printf '\\n' + ;; + stat) + printf '%s\\n' missing + ;; + config) + printf '%s\\n' '{"services":{"web":{"environment":{},' + printf '%s\\n' '"healthcheck":{"test":["CMD","true"]},' + printf '%s\\n' '"image":"ghcr.io/example/web:1.2.3",' + printf '%s\\n' '"restart":"unless-stopped","user":"1000:1000"}}}' + ;; + *) exit 97 ;; + esac + exit 0 +fi + +exit 97 +""", + ) + + result = CliRunner().invoke( + main, + ["audit", "--host", "web1", "--project", "myapp"], + env={ + "C2R_LABEL_OUTPUT": label_output, + "C2R_PAYLOAD_LOG": str(payload_log), + "PATH": f"{bin_directory}:{os.environ['PATH']}", + }, + ) + + if expected_error is not None: + assert result.exit_code == 1, result.output + assert expected_error in result.output + assert forbidden_label_value is not None + assert forbidden_label_value not in result.output + assert not payload_log.exists() + return + + assert result.exit_code == 0, result.output + assert expected_root is not None + payloads = payload_log.read_text(encoding="utf-8") + assert f"capture\n/srv/stacks/myapp/compose/compose.yaml\n{expected_root}\n" in payloads + assert f"config\nmyapp\n{expected_root}\n" in payloads + + +def test_audit_rejects_compose_files_outside_a_usable_labeled_root(tmp_path: Path) -> None: + """A label root must contain the reported Compose files before any source is read.""" + operation_called = tmp_path / "operation-called" + bin_directory = _install_stub_ssh( + tmp_path, + """\ +#!/bin/sh +if [ "$*" = '-- web1 docker compose ls --all --format json' ]; then + printf '%s\\n' '[{"Name":"myapp","Status":"running(1)",' + printf '%s\\n' '"ConfigFiles":"/srv/other/compose.yaml"}]' + exit 0 +fi + +case "$*" in + *"docker ps"*) + printf 'myapp\\t/srv/stacks/myapp\\n' + exit 0 + ;; + *"sh -s") + : > "$C2R_OPERATION_CALLED" + exit 97 + ;; +esac + +exit 97 +""", + ) + + result = CliRunner().invoke( + main, + ["audit", "--host", "web1", "--project", "myapp", "--format", "json"], + env={ + "C2R_OPERATION_CALLED": str(operation_called), + "PATH": f"{bin_directory}:{os.environ['PATH']}", + }, + ) + + assert result.exit_code == 1, result.output + project = json.loads(result.output)["projects"][0] + assert project["error"] == "remote Compose file path escapes the project root" + assert "/srv/other/compose.yaml" not in result.output + assert not operation_called.exists() + + +def test_audit_batches_multifile_source_and_dependent_input_capture(tmp_path: Path) -> None: + """One source session retains all inputs without one SSH operation per file.""" + source_calls = tmp_path / "source-calls" + primary_source = b"""\ +services: + web: + image: ghcr.io/example/web:1.2.3 + environment: + DATABASE_URL: ${DATABASE_URL} + env_file: env/web.env +secrets: + app_secret: + file: ./secrets/app.bin +""" + override_source = b"""\ +services: + web: + environment: + LOG_LEVEL: debug +""" + root_dotenv = b"DATABASE_URL=postgres://operator:batch-source-secret@db/app\n" + service_dotenv = b"LOG_LEVEL=debug\n" + file_secret = b"\x00batch-file-secret\n" + bin_directory = _install_stub_ssh( + tmp_path, + """\ +#!/bin/sh +if [ "$*" = '-- web1 docker compose ls --all --format json' ]; then + printf '%s\\n' '[{"Name":"myapp","Status":"running(1)",' + printf '%s\\n' '"ConfigFiles":["/srv/myapp/compose.yaml",' + printf '%s\\n' '"/srv/myapp/compose.override.yaml"]}]' + exit 0 +fi + +if [ "$*" = '-- web1 sh -s' ]; then + IFS= read -r first_line || exit 97 + if [ "$first_line" = '# compose2role-source-session-v1' ]; then + while IFS= read -r line; do + [ "$line" = '# compose2role-source-session-ready' ] && break + done + IFS= read -r invocation || exit 97 + [ "$invocation" = main ] || exit 97 + printf '%s\\n' session >> "$C2R_SOURCE_CALLS" + printf '%s\\n' ready + while IFS= read -r header; do + set -- $header + [ "$#" -eq 3 ] || exit 97 + [ "$1" = request ] || exit 97 + request_id=$2 + operation=$3 + IFS= read -r path || exit 97 + IFS= read -r root || exit 97 + IFS= read -r trailer || exit 97 + [ "$trailer" = "end $request_id" ] || exit 97 + printf '%s %s\\n' "$operation" "$path" >> "$C2R_SOURCE_CALLS" + case "$operation" in + capture) + printf 'result %s file 640 0 0\\n' "$request_id" + case "$path" in + /srv/myapp/compose.yaml) printf '%s\\n' "$C2R_PRIMARY_SOURCE" ;; + /srv/myapp/compose.override.yaml) printf '%s\\n' "$C2R_OVERRIDE_SOURCE" ;; + /srv/myapp/.env) printf '%s\\n' "$C2R_ROOT_DOTENV" ;; + /srv/myapp/env/web.env) printf '%s\\n' "$C2R_SERVICE_DOTENV" ;; + /srv/myapp/secrets/app.bin) printf '%s\\n' "$C2R_FILE_SECRET" ;; + *) exit 98 ;; + esac + ;; + stat) + case "$path" in + /srv/myapp/.env|/srv/myapp/env/web.env|/srv/myapp/secrets/app.bin) + printf 'result %s file\\n' "$request_id" + ;; + *) exit 98 ;; + esac + ;; + *) exit 97 ;; + esac + printf 'end %s\\n' "$request_id" + done + exit 0 + fi + request=$( { printf '%s\\n' "$first_line"; cat; } ) + encoded=$(printf '%s\\n' "$request" | tail -n 2 | head -n 1) + payload=$(printf '%s' "$encoded" | base64 -d) + operation=$(printf '%s\\n' "$payload" | head -n 1) + case "$operation" in + batch) + printf '%s\\n' "$payload" >> "$C2R_SOURCE_CALLS" + count=$(printf '%s\\n' "$payload" | sed -n '2p') + index=0 + while [ "$index" -lt "$count" ]; do + line=$((4 + index * 3)) + path=$(printf '%s\\n' "$payload" | sed -n "${line}p") + printf 'result %s file 640 0 0\\n' "$index" + case "$path" in + /srv/myapp/compose.yaml) printf '%s\\n' "$C2R_PRIMARY_SOURCE" ;; + /srv/myapp/compose.override.yaml) printf '%s\\n' "$C2R_OVERRIDE_SOURCE" ;; + /srv/myapp/.env) printf '%s\\n' "$C2R_ROOT_DOTENV" ;; + /srv/myapp/env/web.env) printf '%s\\n' "$C2R_SERVICE_DOTENV" ;; + /srv/myapp/secrets/app.bin) printf '%s\\n' "$C2R_FILE_SECRET" ;; + *) exit 98 ;; + esac + index=$((index + 1)) + done + ;; + config) + printf '%s\\n' "$C2R_CANONICAL_DOCUMENT" + ;; + *) exit 97 ;; + esac + exit 0 +fi + +exit 97 +""", + source_session=True, + ) + canonical_document = { + "services": { + "web": { + "environment": { + "DATABASE_URL": "postgres://operator:batch-source-secret@db/app", + "LOG_LEVEL": "debug", + }, + "healthcheck": {"test": ["CMD", "true"]}, + "image": "ghcr.io/example/web:1.2.3", + "restart": "unless-stopped", + "user": "1000:1000", + } + } + } + + result = CliRunner().invoke( + main, + ["audit", "--host", "web1", "--project", "myapp", "--format", "json"], + env={ + "C2R_CANONICAL_DOCUMENT": json.dumps(canonical_document), + "C2R_FILE_SECRET": base64.b64encode(file_secret).decode("ascii"), + "C2R_OVERRIDE_SOURCE": base64.b64encode(override_source).decode("ascii"), + "C2R_PRIMARY_SOURCE": base64.b64encode(primary_source).decode("ascii"), + "C2R_ROOT_DOTENV": base64.b64encode(root_dotenv).decode("ascii"), + "C2R_SERVICE_DOTENV": base64.b64encode(service_dotenv).decode("ascii"), + "C2R_SOURCE_CALLS": str(source_calls), + "PATH": f"{bin_directory}:{os.environ['PATH']}", + }, + ) + + assert result.exit_code == 0, result.output + assert "batch-source-secret" not in result.output + calls = source_calls.read_text(encoding="utf-8") + assert calls.count("session\n") == 1 + assert "capture /srv/myapp/compose.yaml" in calls + assert "capture /srv/myapp/compose.override.yaml" in calls + assert "capture /srv/myapp/.env" in calls + assert "capture /srv/myapp/env/web.env" in calls + assert "capture /srv/myapp/secrets/app.bin" in calls + + +def test_audit_keeps_a_failed_source_session_request_value_free(tmp_path: Path) -> None: + """A request error fails only its project after prior source requests succeeded.""" + secret_value = "session-request-secret-must-not-escape" + source_calls = tmp_path / "source-calls" + bin_directory = _install_stub_ssh( + tmp_path, + """\ +#!/bin/sh +if [ "$*" = '-- web1 docker compose ls --all --format json' ]; then + printf '%s\\n' '[{"Name":"myapp","Status":"running(1)",' + printf '%s\\n' '"ConfigFiles":"/srv/myapp/compose.yaml"}]' + exit 0 +fi + +if [ "$*" = '-- web1 sh -s' ]; then + IFS= read -r first_line || exit 97 + if [ "$first_line" = '# compose2role-source-session-v1' ]; then + while IFS= read -r line; do + [ "$line" = '# compose2role-source-session-ready' ] && break + done + IFS= read -r invocation || exit 97 + [ "$invocation" = main ] || exit 97 + printf '%s\\n' session >> "$C2R_SOURCE_CALLS" + printf '%s\\n' ready + while IFS= read -r header; do + set -- $header + request_id=$2 + operation=$3 + IFS= read -r path || exit 97 + IFS= read -r root || exit 97 + IFS= read -r trailer || exit 97 + [ "$trailer" = "end $request_id" ] || exit 97 + printf '%s %s\\n' "$operation" "$path" >> "$C2R_SOURCE_CALLS" + case "$operation:$path" in + capture:/srv/myapp/compose.yaml) + printf 'result %s file 640 0 0\\n' "$request_id" + base64 <<'EOF' | tr -d '\\n' +services: + web: + image: ghcr.io/example/web:1.2.3 + env_file: env/web.env +EOF + printf '\\n' + ;; + stat:/srv/myapp/.env) + printf 'result %s missing\\n' "$request_id" + ;; + stat:/srv/myapp/env/web.env) + printf 'result %s error\\n' "$request_id" + ;; + *) exit 98 ;; + esac + printf 'end %s\\n' "$request_id" + done + exit 0 + fi + printf '%s\\n' config >> "$C2R_SOURCE_CALLS" + exit 98 +fi + +exit 97 +""", + source_session=True, + ) + + result = CliRunner().invoke( + main, + ["audit", "--host", "web1", "--project", "myapp", "--format", "json"], + env={ + "C2R_SOURCE_CALLS": str(source_calls), + "PATH": f"{bin_directory}:{os.environ['PATH']}", + }, + ) + + assert result.exit_code == 1, result.output + assert json.loads(result.output)["projects"][0]["error"] == "remote source request failed" + assert secret_value not in result.output + calls = source_calls.read_text(encoding="utf-8") + assert calls.count("session\n") == 1 + assert "capture /srv/myapp/compose.yaml" in calls + assert "stat /srv/myapp/.env" in calls + assert "stat /srv/myapp/env/web.env" in calls + assert "config\n" not in calls + + +def test_audit_reports_key_and_value_secret_classifications_without_values(tmp_path: Path) -> None: + secret_value = "postgres://operator:never-print-this@db.example/app" + log_file = tmp_path / "ssh-log" + bin_directory = _install_stub_ssh( + tmp_path, + f"""\ +#!/bin/sh +request=$(cat) +printf '%s\\n' "$request" >> "$C2R_SSH_LOG" + +if [ "$*" = '-- web1 docker compose ls --all --format json' ]; then + printf '%s\\n' '[{{"Name":"myapp","Status":"running(1)",' + printf '%s\\n' '"ConfigFiles":"/srv/myapp/compose.yaml"}}]' + exit 0 +fi + +if [ "$*" = '-- web1 sh -s' ]; then + encoded=$(printf '%s\\n' "$request" | tail -n 2 | head -n 1) + payload=$(printf '%s' "$encoded" | base64 -d) + operation=$(printf '%s\\n' "$payload" | head -n 1) + case "$operation" in + capture) + printf '%s\n' 'file 640 0 0' + base64 <<'EOF' | tr -d '\n' +services: + web: + image: ghcr.io/example/web:1.2.3 +EOF + printf '\n' + ;; + stat) + printf '%s\\n' 'missing' + ;; + config) + printf '%s\\n' '{{"services":{{"web":{{"image":"ghcr.io/example/web:1.2.3",' + printf '%s\\n' '"user":"1000:1000","healthcheck":{{"test":["CMD","true"]}},' + printf '%s\\n' '"restart":"unless-stopped","environment":{{"API_TOKEN":"ordinary-looking",' + printf '%s\\n' '"DATABASE_URL":"{secret_value}"}}}}}}}}' + ;; + *) + exit 97 + ;; + esac + exit 0 +fi + +exit 97 +""", + ) + + result = CliRunner().invoke( + main, + ["audit", "--host", "web1", "--project", "myapp", "--format", "json"], + env=_ssh_environment(bin_directory, log_file), + ) + + assert result.exit_code == 0, result.output + project = json.loads(result.output)["projects"][0] + assert project["classifications"] == [ + { + "key": "API_TOKEN", + "location": "remote-compose.json:1:1", + "reason": "key-name", + "service": "web", + }, + { + "key": "DATABASE_URL", + "location": "remote-compose.json:1:1", + "reason": "value:credential-uri", + "service": "web", + }, + ] + assert [finding["id"] for finding in project["findings"]] == ["C2R025", "C2R025"] + assert secret_value not in result.output + assert secret_value not in log_file.read_text(encoding="utf-8") + + text_result = CliRunner().invoke( + main, + ["audit", "--host", "web1", "--project", "myapp", "--verbose"], + env=_ssh_environment(bin_directory, tmp_path / "text-ssh-log"), + ) + + assert text_result.exit_code == 0, text_result.output + assert text_result.output.splitlines() == [ + "Discovering projects on web1...", + "✓ myapp (missing)", + " remote-compose.json:1:1: INFO C2R025: API_TOKEN is classified as a secret (key-name)", + " remote-compose.json:1:1: INFO C2R025: DATABASE_URL is classified as a secret " + "(value:credential-uri)", + "", + "1 ok", + ] + assert secret_value not in text_result.output + + quiet_result = CliRunner().invoke( + main, + ["audit", "--host", "web1", "--project", "myapp"], + env=_ssh_environment(bin_directory, tmp_path / "quiet-ssh-log"), + ) + + assert quiet_result.exit_code == 0, quiet_result.output + assert "C2R025" not in quiet_result.output + assert "✓ myapp" in quiet_result.output + + +def test_audit_resolves_ordered_compose_files_and_contained_environment_inputs( + tmp_path: Path, +) -> None: + payload_log = tmp_path / "ssh-payloads" + bin_directory = _install_stub_ssh( + tmp_path, + """\ +#!/bin/sh +request=$(cat) + +if [ "$*" = '-- web1 docker compose ls --all --format json' ]; then + printf '%s\\n' '[{"Name":"myapp","Status":"running(1)",' + printf '%s\\n' '"ConfigFiles":["/srv/myapp/compose.yaml",' + printf '%s\\n' '"/srv/myapp/compose.override.yaml"]}]' + exit 0 +fi + +encoded=$(printf '%s\\n' "$request" | tail -n 2 | head -n 1) +payload=$(printf '%s' "$encoded" | base64 -d) +{ + printf '%s\\n' 'REQUEST' + printf '%s\\n' "$payload" + printf '%s\\n' 'END' +} >> "$C2R_SSH_PAYLOADS" +operation=$(printf '%s\\n' "$payload" | head -n 1) +path=$(printf '%s\\n' "$payload" | sed -n '2p') + +case "$operation" in + capture) + case "$path" in + /srv/myapp/compose.yaml) + printf '%s\n' 'file 640 0 0' + base64 <<'EOF' | tr -d '\n' +services: + web: + image: ghcr.io/example/web:1.2.3 + env_file: app.env + environment: + LOG_LEVEL: info +EOF + ;; + /srv/myapp/compose.override.yaml) + printf '%s\n' 'file 640 0 0' + base64 <<'EOF' | tr -d '\n' +services: + web: + environment: + LOG_LEVEL: debug +EOF + ;; + /srv/myapp/.env) + printf '%s\n' 'file 640 0 0' + base64 <<'EOF' | tr -d '\n' +COMPOSE_PROJECT_NAME=myapp +EOF + ;; + /srv/myapp/app.env) + printf '%s\n' 'file 640 0 0' + base64 <<'EOF' | tr -d '\n' +APP_MODE=production +EOF + ;; + *) + exit 98 + ;; + esac + printf '\n' + ;; + stat) + case "$path" in + /srv/myapp/.env|/srv/myapp/app.env) + printf '%s\\n' 'file' + ;; + *) + exit 98 + ;; + esac + ;; + config) + printf '%s\\n' '{"services":{"web":{"image":"ghcr.io/example/web:1.2.3",' + printf '%s\\n' '"user":"1000:1000","healthcheck":{"test":["CMD","true"]},' + printf '%s\\n' '"restart":"unless-stopped","environment":{"LOG_LEVEL":"debug"}}}}' + ;; + *) + exit 97 + ;; +esac +""", + ) + + result = CliRunner().invoke( + main, + ["audit", "--host", "web1", "--project", "myapp", "--format", "json"], + env={ + **_ssh_environment(bin_directory, tmp_path / "ssh-log"), + "C2R_SSH_PAYLOADS": str(payload_log), + }, + ) + + assert result.exit_code == 0, result.output + payloads = payload_log.read_text(encoding="utf-8") + assert payloads.index("capture\n/srv/myapp/compose.yaml") < payloads.index( + "capture\n/srv/myapp/compose.override.yaml" + ) + assert "stat\n/srv/myapp/.env" in payloads + assert "stat\n/srv/myapp/app.env" in payloads + assert "capture\n/srv/myapp/.env" in payloads + assert "capture\n/srv/myapp/app.env" in payloads + assert "config\nmyapp\n/srv/myapp\n/srv/myapp/compose.yaml" in payloads + assert "/srv/myapp/compose.override.yaml\nEND" in payloads + + +def test_remote_resolver_retains_an_ordered_source_snapshot_for_adoption( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + primary_source = b"""\ +# preserve this source marker\r +services:\r + web:\r + image: ghcr.io/example/web:1.2.3\r + volumes:\r + - /srv/web-data:/var/lib/web\r +secrets:\r + app_secret:\r + file: ./secret.txt\r +""" + override_source = b"""\ +# preserve the relative layout +services: + web: + environment: + LOG_LEVEL: debug +""" + bin_directory = _install_stub_ssh( + tmp_path, + """\ +#!/bin/sh +request=$(cat) + +if [ "$*" = '-- web1 docker compose ls --all --format json' ]; then + printf '%s\\n' '[{"Name":"myapp","Status":"running(1)",' + printf '%s\\n' '"ConfigFiles":["/srv/myapp/docker-compose.yml",' + printf '%s\\n' '"/srv/myapp/compose/override.yaml"]}]' + exit 0 +fi + +encoded=$(printf '%s\\n' "$request" | tail -n 2 | head -n 1) +payload=$(printf '%s' "$encoded" | base64 -d) +operation=$(printf '%s\\n' "$payload" | head -n 1) +path=$(printf '%s\\n' "$payload" | sed -n '2p') + +case "$operation" in + capture) + case "$path" in + /srv/myapp/docker-compose.yml) + printf '%s\\n' 'file 640 1000 1001' + printf '%s\\n' "$C2R_PRIMARY_SOURCE" + ;; + /srv/myapp/compose/override.yaml) + printf '%s\\n' 'file 644 1002 1003' + printf '%s\\n' "$C2R_OVERRIDE_SOURCE" + ;; + /srv/myapp/secret.txt) + printf '%s\\n' 'file 600 1004 1005' + printf '%s\\n' "$C2R_SECRET_SOURCE" + ;; + *) + exit 98 + ;; + esac + ;; + stat) + if [ "$path" = /srv/myapp/secret.txt ]; then + printf '%s\\n' file + else + printf '%s\\n' missing + fi + ;; + config) + printf '%s\\n' '{"services":{"web":{"environment":{"LOG_LEVEL":"debug"},' + printf '%s\\n' '"healthcheck":{"test":["CMD","true"]},' + printf '%s\\n' '"image":"ghcr.io/example/web:1.2.3",' + printf '%s\\n' '"restart":"unless-stopped","user":"1000:1000",' + printf '%s\\n' '"volumes":[{"source":"/srv/web-data","target":"/var/lib/web",' + printf '%s\\n' '"type":"bind"}]}}}' + ;; + *) + exit 97 + ;; +esac +""", + ) + monkeypatch.setenv("C2R_PRIMARY_SOURCE", base64.b64encode(primary_source).decode("ascii")) + monkeypatch.setenv("C2R_OVERRIDE_SOURCE", base64.b64encode(override_source).decode("ascii")) + monkeypatch.setenv("C2R_SECRET_SOURCE", base64.b64encode(b"captured-secret\n").decode("ascii")) + monkeypatch.setenv("PATH", f"{bin_directory}:{os.environ['PATH']}") + + result = resolve_remote_projects( + "web1", + project_name="myapp", + all_projects=False, + use_sudo=False, + secret_policy=None, + ) + + assert not result.failures + project = result.projects[0] + assert project.source.root == PurePosixPath("/srv/myapp") + assert [source.relative_path for source in project.source.files] == [ + PurePosixPath("docker-compose.yml"), + PurePosixPath("compose/override.yaml"), + ] + assert [source.content for source in project.source.files] == [primary_source, override_source] + assert [ + (source.metadata.mode, source.metadata.uid, source.metadata.gid) + for source in project.source.files + ] == [(0o640, 1000, 1001), (0o644, 1002, 1003)] + assert [secret.name for secret in project.source.file_secrets] == ["app_secret"] + assert project.source.file_secrets[0].source.relative_path == PurePosixPath("secret.txt") + assert project.source.file_secrets[0].source.content == b"captured-secret\n" + assert ( + project.source.file_secrets[0].source.metadata.mode, + project.source.file_secrets[0].source.metadata.uid, + project.source.file_secrets[0].source.metadata.gid, + ) == (0o600, 1004, 1005) + assert "preserve this source marker" not in json.dumps(result.as_json()) + assert "preserve this source marker" not in repr(project) + + +def test_remote_resolver_round_trips_wrapped_source_bytes_through_the_capture_protocol( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + root = tmp_path / "project" + root.mkdir() + source_path = root / "compose.yaml" + source_content = ( + b"# " + b"n" * 90 + b"\r\n" + b"services:\r\n" + b" web:\r\n" + b" image: ghcr.io/example/web:1.2.3\r\n" + b" labels:\r\n" + b" description: " + b"n" * 120 + b"\r\n" + ) + source_path.write_bytes(source_content) + source_path.chmod(0o640) + bin_directory = _install_stub_ssh( + tmp_path, + """\ +#!/bin/sh +if [ "$1" = -- ] && [ "$2" = web1 ] && [ "$3" = docker ]; then + printf '%s\\n' "$C2R_DISCOVERY_JSON" + exit 0 +fi +if [ "$1" = -- ] && [ "$2" = web1 ] && [ "$3" = sh ] && [ "$4" = -s ]; then + shift 2 + exec "$@" +fi +exit 99 +""", + source_session=True, + ) + docker = bin_directory / "docker" + docker.write_text( + """\ +#!/bin/sh +printf '%s\\n' '{"services":{"web":{"environment":{},' +printf '%s\\n' '"healthcheck":{"test":["CMD","true"]},' +printf '%s\\n' '"image":"ghcr.io/example/web:1.2.3",' +printf '%s\\n' '"restart":"unless-stopped","user":"1000:1000"}}}' +""", + encoding="utf-8", + ) + docker.chmod(0o755) + monkeypatch.setenv("PATH", f"{bin_directory}:{os.environ['PATH']}") + monkeypatch.setenv( + "C2R_DISCOVERY_JSON", + json.dumps([{"Name": "myapp", "Status": "running(1)", "ConfigFiles": str(source_path)}]), + ) + + result = resolve_remote_projects( + "web1", + project_name="myapp", + all_projects=False, + use_sudo=False, + secret_policy=None, + ) + + assert not result.failures + captured_source = result.projects[0].source.files[0] + assert captured_source.content == source_content + assert captured_source.metadata.mode == 0o640 + + +def test_audit_accepts_bind_mount_and_file_backed_secret_source_declarations( + tmp_path: Path, +) -> None: + source_marker = "source-content-must-not-be-reported" + source_document = f"""\ +# {source_marker} +services: + web: + image: ghcr.io/example/web:1.2.3 + volumes: + - /srv/web-data:/var/lib/web +secrets: + app_secret: + file: ./secret.txt +""" + bin_directory = _install_stub_ssh( + tmp_path, + """\ +#!/bin/sh +request=$(cat) + +if [ "$*" = '-- web1 docker compose ls --all --format json' ]; then + printf '%s\\n' '[{"Name":"myapp","Status":"running(1)",' + printf '%s\\n' '"ConfigFiles":"/srv/myapp/compose.yaml"}]' + exit 0 +fi + +encoded=$(printf '%s\\n' "$request" | tail -n 2 | head -n 1) +payload=$(printf '%s' "$encoded" | base64 -d) +operation=$(printf '%s\\n' "$payload" | head -n 1) +path=$(printf '%s\\n' "$payload" | sed -n '2p') +case "$operation" in + capture) + case "$path" in + /srv/myapp/compose.yaml) + printf '%s\\n' 'file 640 1000 1001' + printf '%s\\n' "$C2R_SOURCE_DOCUMENT" + ;; + /srv/myapp/secret.txt) + printf '%s\\n' 'file 600 1000 1001' + printf '%s\\n' "$C2R_SECRET_SOURCE" + ;; + *) exit 98 ;; + esac + ;; + stat) + if [ "$path" = /srv/myapp/secret.txt ]; then + printf '%s\\n' file + else + printf '%s\\n' missing + fi + ;; + config) + printf '%s\\n' '{"services":{"web":{"environment":{},' + printf '%s\\n' '"healthcheck":{"test":["CMD","true"]},' + printf '%s\\n' '"image":"ghcr.io/example/web:1.2.3",' + printf '%s\\n' '"restart":"unless-stopped","user":"1000:1000",' + printf '%s\\n' '"volumes":[{"source":"/srv/web-data","target":"/var/lib/web",' + printf '%s\\n' '"type":"bind"}]}}}' + ;; + *) + exit 97 + ;; +esac +""", + ) + + result = CliRunner().invoke( + main, + ["audit", "--host", "web1", "--project", "myapp", "--format", "json"], + env={ + **_ssh_environment(bin_directory, tmp_path / "ssh-log"), + "C2R_SOURCE_DOCUMENT": base64.b64encode(source_document.encode("utf-8")).decode( + "ascii" + ), + "C2R_SECRET_SOURCE": base64.b64encode(b"audit-only-secret\n").decode("ascii"), + }, + ) + + assert result.exit_code == 0, result.output + assert json.loads(result.output)["projects"][0]["status"] == "ok" + assert "C2R013" not in result.output + assert "C2R028" not in result.output + assert source_marker not in result.output + + +def test_audit_refuses_a_symlinked_compose_source_before_canonical_resolution( + tmp_path: Path, +) -> None: + root = tmp_path / "project" + root.mkdir() + target = root / "source.yaml" + target.write_text("services: {}\n", encoding="utf-8") + compose_path = root / "compose.yaml" + compose_path.symlink_to(target) + bin_directory = _install_stub_ssh( + tmp_path, + """\ +#!/bin/sh +if [ "$1" = -- ] && [ "$2" = web1 ] && [ "$3" = docker ]; then + printf '%s\\n' "$C2R_DISCOVERY_JSON" + exit 0 +fi +if [ "$1" = -- ] && [ "$2" = web1 ] && [ "$3" = sh ] && [ "$4" = -s ]; then + shift 2 + exec "$@" +fi +exit 99 +""", + source_session=True, + ) + + result = CliRunner().invoke( + main, + ["audit", "--host", "web1", "--project", "myapp", "--format", "json"], + env={ + **_ssh_environment(bin_directory, tmp_path / "ssh-log"), + "C2R_DISCOVERY_JSON": json.dumps( + [{"Name": "myapp", "Status": "running(1)", "ConfigFiles": str(compose_path)}] + ), + }, + ) + + assert result.exit_code == 1, result.output + assert json.loads(result.output)["projects"][0]["error"] == ( + "remote Compose source file must not be a symlink" + ) + + +def test_audit_refuses_a_compose_source_with_a_parent_symlink_escape(tmp_path: Path) -> None: + root = tmp_path / "project" + outside = tmp_path / "outside" + root.mkdir() + outside.mkdir() + (root / "compose.yaml").write_text("services: {}\n", encoding="utf-8") + (outside / "override.yaml").write_text("services: {}\n", encoding="utf-8") + (root / "nested").symlink_to(outside, target_is_directory=True) + bin_directory = _install_stub_ssh( + tmp_path, + """\ +#!/bin/sh +if [ "$1" = -- ] && [ "$2" = web1 ] && [ "$3" = docker ]; then + printf '%s\\n' "$C2R_DISCOVERY_JSON" + exit 0 +fi +if [ "$1" = -- ] && [ "$2" = web1 ] && [ "$3" = sh ] && [ "$4" = -s ]; then + shift 2 + exec "$@" +fi +exit 99 +""", + source_session=True, + ) + + result = CliRunner().invoke( + main, + ["audit", "--host", "web1", "--project", "myapp", "--format", "json"], + env={ + **_ssh_environment(bin_directory, tmp_path / "ssh-log"), + "C2R_DISCOVERY_JSON": json.dumps( + [ + { + "Name": "myapp", + "Status": "running(1)", + "ConfigFiles": [ + str(root / "compose.yaml"), + str(root / "nested" / "override.yaml"), + ], + } + ] + ), + }, + ) + + assert result.exit_code == 1, result.output + assert json.loads(result.output)["projects"][0]["error"] == ( + "remote Compose file path escapes the project root" + ) + + +def test_audit_refuses_compose_files_outside_the_discovered_source_root(tmp_path: Path) -> None: + root = tmp_path / "project" + remote_operation_called = tmp_path / "remote-operation-called" + bin_directory = _install_stub_ssh( + tmp_path, + """\ +#!/bin/sh +if [ "$*" = '-- web1 docker compose ls --all --format json' ]; then + printf '%s\\n' "$C2R_DISCOVERY_JSON" + exit 0 +fi +if [ "$*" = '-- web1 sh -s' ]; then + : > "$C2R_REMOTE_OPERATION_CALLED" +fi +exit 99 +""", + ) + + result = CliRunner().invoke( + main, + ["audit", "--host", "web1", "--project", "myapp", "--format", "json"], + env={ + **_ssh_environment(bin_directory, tmp_path / "ssh-log"), + "C2R_DISCOVERY_JSON": json.dumps( + [ + { + "Name": "myapp", + "Status": "running(1)", + "ConfigFiles": [str(root / "compose.yaml"), "/etc/override.yaml"], + } + ] + ), + "C2R_REMOTE_OPERATION_CALLED": str(remote_operation_called), + }, + ) + + assert result.exit_code == 1, result.output + assert json.loads(result.output)["projects"][0]["error"] == ( + "remote Compose file path escapes the project root" + ) + assert not remote_operation_called.exists() + + +def test_audit_quarantines_invalid_source_metadata(tmp_path: Path) -> None: + invalid_uid = "9" * 5_000 + bin_directory = _install_stub_ssh( + tmp_path, + """\ +#!/bin/sh +request=$(cat) + +if [ "$*" = '-- web1 docker compose ls --all --format json' ]; then + printf '%s\\n' '[{"Name":"myapp","Status":"running(1)",' + printf '%s\\n' '"ConfigFiles":"/srv/myapp/compose.yaml"}]' + exit 0 +fi + +encoded=$(printf '%s\\n' "$request" | tail -n 2 | head -n 1) +payload=$(printf '%s' "$encoded" | base64 -d) +operation=$(printf '%s\\n' "$payload" | head -n 1) +case "$operation" in + capture) + printf 'file 640 %s 0\\n' "$C2R_INVALID_UID" + printf '%s\\n' "$C2R_SOURCE_DOCUMENT" + ;; + *) + exit 97 + ;; +esac +""", + ) + + result = CliRunner().invoke( + main, + ["audit", "--host", "web1", "--project", "myapp", "--format", "json"], + env={ + **_ssh_environment(bin_directory, tmp_path / "ssh-log"), + "C2R_INVALID_UID": invalid_uid, + "C2R_SOURCE_DOCUMENT": base64.b64encode(b"services: {}\n").decode("ascii"), + }, + ) + + assert result.exit_code == 1, result.output + assert json.loads(result.output)["projects"][0]["error"] == "remote source metadata is invalid" + assert invalid_uid not in result.output + + +@pytest.mark.parametrize( + ("capture_response", "expected_error", "unexposed_value"), + ( + ("missing\n", "remote Compose source file is not a regular file", "missing"), + ("other\n", "remote Compose source file is not a regular file", "other"), + ( + "file 640 1000 1001\nnot-valid-base64-payload\n", + "remote source content is invalid", + "not-valid-base64-payload", + ), + ), +) +def test_audit_quarantines_invalid_capture_responses( + tmp_path: Path, + capture_response: str, + expected_error: str, + unexposed_value: str, +) -> None: + bin_directory = _install_stub_ssh( + tmp_path, + """\ +#!/bin/sh +request=$(cat) + +if [ "$*" = '-- web1 docker compose ls --all --format json' ]; then + printf '%s\\n' '[{"Name":"myapp","Status":"running(1)",' + printf '%s\\n' '"ConfigFiles":"/srv/myapp/compose.yaml"}]' + exit 0 +fi + +encoded=$(printf '%s\\n' "$request" | tail -n 2 | head -n 1) +payload=$(printf '%s' "$encoded" | base64 -d) +operation=$(printf '%s\\n' "$payload" | head -n 1) +case "$operation" in + capture) + printf '%s' "$C2R_CAPTURE_RESPONSE" + ;; + *) + exit 97 + ;; +esac +""", + ) + + result = CliRunner().invoke( + main, + ["audit", "--host", "web1", "--project", "myapp", "--format", "json"], + env={ + **_ssh_environment(bin_directory, tmp_path / "ssh-log"), + "C2R_CAPTURE_RESPONSE": capture_response, + }, + ) + + assert result.exit_code == 1, result.output + assert json.loads(result.output)["projects"][0]["error"] == expected_error + assert unexposed_value not in result.output + + +def test_audit_refuses_remote_build_sources_before_composing(tmp_path: Path) -> None: + config_called = tmp_path / "config-called" + bin_directory = _install_stub_ssh( + tmp_path, + """\ +#!/bin/sh +request=$(cat) + +if [ "$*" = '-- web1 docker compose ls --all --format json' ]; then + printf '%s\\n' '[{"Name":"unsafe","Status":"running(1)",' + printf '%s\\n' '"ConfigFiles":"/srv/unsafe/compose.yaml"}]' + exit 0 +fi + +encoded=$(printf '%s\\n' "$request" | tail -n 2 | head -n 1) +payload=$(printf '%s' "$encoded" | base64 -d) +operation=$(printf '%s\\n' "$payload" | head -n 1) +case "$operation" in + capture) + printf '%s\n' 'file 640 0 0' + base64 <<'EOF' | tr -d '\n' +services: + web: + image: ghcr.io/example/web:1.2.3 + build: . +EOF + printf '\n' + ;; + stat) + printf '%s\\n' 'missing' + ;; + config) + : > "$C2R_CONFIG_CALLED" + printf '%s\\n' '{"services":{"web":{"image":"ghcr.io/example/web:1.2.3"}}}' + ;; + *) + exit 97 + ;; +esac +""", + ) + + result = CliRunner().invoke( + main, + ["audit", "--host", "web1", "--project", "unsafe", "--format", "json"], + env={ + **_ssh_environment(bin_directory, tmp_path / "ssh-log"), + "C2R_CONFIG_CALLED": str(config_called), + }, + ) + + assert result.exit_code == 1, result.output + project = json.loads(result.output)["projects"][0] + assert project["status"] == "failed" + assert project["error"] == "C2R005: remote build contexts are not supported" + assert not config_called.exists() + + +def test_audit_secret_policy_overrides_automatic_secret_classification(tmp_path: Path) -> None: + secret_policy_path = tmp_path / "policy.toml" + secret_policy_path.write_text( + """\ +[[rules]] +project = "myapp" +service = "web" +key = "API_TOKEN" +decision = "plain" + +[[rules]] +project = "myapp" +service = "web" +key = "LOG_LEVEL" +decision = "secret" +""", + encoding="utf-8", + ) + environment_secret_policy_path = tmp_path / "environment-secret-policy.toml" + environment_secret_policy_path.write_text( + """\ +[[rules]] +project = "myapp" +service = "web" +key = "API_TOKEN" +decision = "secret" + +[[rules]] +project = "myapp" +service = "web" +key = "LOG_LEVEL" +decision = "plain" +""", + encoding="utf-8", + ) + bin_directory = _install_stub_ssh( + tmp_path, + """\ +#!/bin/sh +request=$(cat) + +if [ "$*" = '-- web1 docker compose ls --all --format json' ]; then + printf '%s\\n' '[{"Name":"myapp","Status":"running(1)",' + printf '%s\\n' '"ConfigFiles":"/srv/myapp/compose.yaml"}]' + exit 0 +fi + +encoded=$(printf '%s\\n' "$request" | tail -n 2 | head -n 1) +payload=$(printf '%s' "$encoded" | base64 -d) +operation=$(printf '%s\\n' "$payload" | head -n 1) +case "$operation" in + capture) + printf '%s\n' 'file 640 0 0' + base64 <<'EOF' | tr -d '\n' +services: + web: + image: ghcr.io/example/web:1.2.3 +EOF + printf '\n' + ;; + stat) + printf '%s\\n' 'missing' + ;; + config) + printf '%s\\n' '{"services":{"web":{"image":"ghcr.io/example/web:1.2.3",' + printf '%s\\n' '"user":"1000:1000","healthcheck":{"test":["CMD","true"]},' + printf '%s\\n' '"restart":"unless-stopped","environment":{' + printf '%s\\n' '"API_TOKEN":"automatic-secret","LOG_LEVEL":"info"}}}}' + ;; + *) + exit 97 + ;; +esac +""", + ) + + result = CliRunner().invoke( + main, + [ + "audit", + "--host", + "web1", + "--project", + "myapp", + "--secret-policy", + str(secret_policy_path), + "--format", + "json", + ], + env={ + **_ssh_environment(bin_directory, tmp_path / "ssh-log"), + "COMPOSE2ROLE_SECRET_POLICY": str(environment_secret_policy_path), + }, + ) + + assert result.exit_code == 0, result.output + project = json.loads(result.output)["projects"][0] + assert project["classifications"] == [ + { + "key": "LOG_LEVEL", + "location": "remote-compose.json:1:1", + "reason": "secret-policy:forced-secret", + "service": "web", + } + ] + assert [finding["message"] for finding in project["findings"]] == [ + "LOG_LEVEL is classified as a secret (secret-policy:forced-secret)" + ] + + fallback_result = CliRunner().invoke( + main, + ["audit", "--host", "web1", "--project", "myapp", "--format", "json"], + env={ + **_ssh_environment(bin_directory, tmp_path / "fallback-ssh-log"), + "COMPOSE2ROLE_SECRET_POLICY": str(environment_secret_policy_path), + }, + ) + + assert fallback_result.exit_code == 0, fallback_result.output + fallback_classifications = json.loads(fallback_result.output)["projects"][0]["classifications"] + assert [(item["key"], item["reason"]) for item in fallback_classifications] == [ + ("API_TOKEN", "secret-policy:forced-secret"), + ] + + +def test_audit_rejects_conflicting_secret_policy_rules_before_remote_discovery( + tmp_path: Path, +) -> None: + secret_policy_path = tmp_path / "conflicting-secret-policy.toml" + secret_policy_path.write_text( + """\ +[[rules]] +project = "myapp" +service = "web" +key = "API_TOKEN" +decision = "secret" + +[[rules]] +project = "myapp" +service = "web" +key = "API_TOKEN" +decision = "plain" +""", + encoding="utf-8", + ) + ssh_log = tmp_path / "ssh-log" + bin_directory = _install_stub_ssh( + tmp_path, + "#!/bin/sh\nprintf '%s\\n' invoked >> \"$C2R_SSH_LOG\"\nexit 99\n", + ) + + result = CliRunner().invoke( + main, + [ + "audit", + "--host", + "web1", + "--project", + "myapp", + "--secret-policy", + str(secret_policy_path), + ], + env=_ssh_environment(bin_directory, ssh_log), + ) + + assert result.exit_code == 1 + assert "secret policy has conflicting exact rules" in result.output + assert not ssh_log.exists() + + +def test_audit_rejects_nonstring_secret_policy_decisions_without_remote_discovery( + tmp_path: Path, +) -> None: + secret_policy_path = tmp_path / "invalid-secret-policy.toml" + secret_policy_path.write_text( + """\ +[[rules]] +project = "myapp" +service = "web" +key = "API_TOKEN" +decision = ["secret"] +""", + encoding="utf-8", + ) + ssh_log = tmp_path / "ssh-log" + bin_directory = _install_stub_ssh( + tmp_path, + "#!/bin/sh\nprintf '%s\\n' invoked >> \"$C2R_SSH_LOG\"\nexit 99\n", + ) + + result = CliRunner().invoke( + main, + [ + "audit", + "--host", + "web1", + "--project", + "myapp", + "--secret-policy", + str(secret_policy_path), + ], + env=_ssh_environment(bin_directory, ssh_log), + ) + + assert result.exit_code == 1 + assert "secret policy rules are invalid" in result.output + assert not ssh_log.exists() + + +def test_audit_reports_high_confidence_value_secret_signals(tmp_path: Path) -> None: + private_key = "-----BEGIN PRIVATE KEY-----\nnot-a-real-key\n-----END PRIVATE KEY-----" + jwt = "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ1c2VyIn0.signaturetoken" + token = "ghp_abcdefghijklmnopqrstuvwxyzABCDEF1234567890" + long_token = "Abcdefghijklmnopqrstuvwxyz1234567890Z" + bin_directory = _install_stub_ssh( + tmp_path, + """\ +#!/bin/sh +request=$(cat) + +if [ "$*" = '-- web1 docker compose ls --all --format json' ]; then + printf '%s\\n' '[{"Name":"myapp","Status":"running(1)",' + printf '%s\\n' '"ConfigFiles":"/srv/myapp/compose.yaml"}]' + exit 0 +fi + +encoded=$(printf '%s\\n' "$request" | tail -n 2 | head -n 1) +payload=$(printf '%s' "$encoded" | base64 -d) +operation=$(printf '%s\\n' "$payload" | head -n 1) +case "$operation" in + capture) + printf '%s\n' 'file 640 0 0' + base64 <<'EOF' | tr -d '\n' +services: + web: + image: ghcr.io/example/web:1.2.3 +EOF + printf '\n' + ;; + stat) + printf '%s\\n' 'missing' + ;; + config) + printf '%s\\n' '{"services":{"web":{"image":"ghcr.io/example/web:1.2.3",' + printf '%s\\n' '"user":"1000:1000","healthcheck":{"test":["CMD","true"]},' + printf '%s\\n' '"restart":"unless-stopped","environment":{' + printf '%s\\n' '"TLS_MATERIAL":"-----BEGIN PRIVATE KEY-----\\nnot-a-real-key",' + printf '%s\\n' '"JWT_MATERIAL":"eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ1c2VyIn0.signaturetoken",' + printf '%s\\n' '"CLIENT_ID":"ghp_abcdefghijklmnopqrstuvwxyzABCDEF1234567890",' + printf '%s\\n' '"OPAQUE_MATERIAL":"Abcdefghijklmnopqrstuvwxyz1234567890Z"}}}}' + ;; + *) + exit 97 + ;; +esac +""", + ) + + result = CliRunner().invoke( + main, + ["audit", "--host", "web1", "--project", "myapp", "--format", "json"], + env=_ssh_environment(bin_directory, tmp_path / "ssh-log"), + ) + + assert result.exit_code == 0, result.output + classifications = json.loads(result.output)["projects"][0]["classifications"] + assert [(item["key"], item["reason"]) for item in classifications] == [ + ("TLS_MATERIAL", "value:private-key"), + ("JWT_MATERIAL", "value:jwt"), + ("CLIENT_ID", "value:opaque-token"), + ("OPAQUE_MATERIAL", "value:opaque-token"), + ] + assert private_key not in result.output + assert jwt not in result.output + assert token not in result.output + assert long_token not in result.output + + +def test_audit_all_keeps_successful_projects_when_another_project_fails(tmp_path: Path) -> None: + bin_directory = _install_stub_ssh( + tmp_path, + """\ +#!/bin/sh +request=$(cat) + +if [ "$*" = '-- web1 docker compose ls --all --format json' ]; then + printf '%s\\n' '[{"Name":"bad","Status":"running(1)",' + printf '%s\\n' '"ConfigFiles":"/srv/bad/compose.yaml"},' + printf '%s\\n' '{"Name":"good","Status":"running(1)",' + printf '%s\\n' '"ConfigFiles":"/srv/good/compose.yaml"}]' + exit 0 +fi + +encoded=$(printf '%s\\n' "$request" | tail -n 2 | head -n 1) +payload=$(printf '%s' "$encoded" | base64 -d) +operation=$(printf '%s\\n' "$payload" | head -n 1) +path=$(printf '%s\\n' "$payload" | sed -n '2p') +case "$operation" in + capture) + case "$path" in + /srv/bad/compose.yaml) + printf '%s\n' 'file 640 0 0' + base64 <<'EOF' | tr -d '\n' +services: + web: + image: ghcr.io/example/web:1.2.3 + build: . +EOF + ;; + /srv/good/compose.yaml) + printf '%s\n' 'file 640 0 0' + base64 <<'EOF' | tr -d '\n' +services: + web: + image: ghcr.io/example/web:1.2.3 +EOF + ;; + *) + exit 98 + ;; + esac + printf '\n' + ;; + stat) + printf '%s\\n' 'missing' + ;; + config) + printf '%s\\n' '{"services":{"web":{"image":"ghcr.io/example/web:1.2.3",' + printf '%s\\n' '"user":"1000:1000","healthcheck":{"test":["CMD","true"]},' + printf '%s\\n' '"restart":"unless-stopped","environment":{}}}}' + ;; + *) + exit 97 + ;; +esac +""", + ) + + result = CliRunner().invoke( + main, + ["audit", "--host", "web1", "--all", "--format", "json"], + env=_ssh_environment(bin_directory, tmp_path / "ssh-log"), + ) + + assert result.exit_code == 1, result.output + projects = json.loads(result.output)["projects"] + assert [(project["name"], project["status"]) for project in projects] == [ + ("bad", "failed"), + ("good", "ok"), + ] + assert projects[0]["error"] == "C2R005: remote build contexts are not supported" + + text_result = CliRunner().invoke( + main, + ["audit", "--host", "web1", "--all"], + env=_ssh_environment(bin_directory, tmp_path / "text-ssh-log"), + ) + + assert text_result.exit_code == 1, text_result.output + assert text_result.output.splitlines() == [ + "Discovering projects on web1...", + "✗ bad: FAILED (source): C2R005: remote build contexts are not supported", + "✓ good (missing)", + "", + "1 ok, 1 failed", + ] + + +def test_audit_all_reports_an_empty_discovery_batch(tmp_path: Path) -> None: + bin_directory = _install_stub_ssh( + tmp_path, + """\ +#!/bin/sh +if [ "$*" = '-- web1 docker compose ls --all --format json' ]; then + printf '%s\\n' '[]' + exit 0 +fi +exit 99 +""", + ) + + result = CliRunner().invoke( + main, + ["audit", "--host", "web1", "--all", "--format", "json"], + env=_ssh_environment(bin_directory, tmp_path / "ssh-log"), + ) + + assert result.exit_code == 0, result.output + assert json.loads(result.output) == {"projects": []} + + +def test_audit_reports_an_unknown_remote_project_without_resolving_source(tmp_path: Path) -> None: + bin_directory = _install_stub_ssh( + tmp_path, + """\ +#!/bin/sh +if [ "$*" = '-- web1 docker compose ls --all --format json' ]; then + printf '%s\\n' '[{"Name":"known","Status":"running(1)",' + printf '%s\\n' '"ConfigFiles":"/srv/known/compose.yaml"}]' + exit 0 +fi +exit 97 +""", + ) + + result = CliRunner().invoke( + main, + ["audit", "--host", "web1", "--project", "missing", "--format", "json"], + env=_ssh_environment(bin_directory, tmp_path / "ssh-log"), + ) + + assert result.exit_code == 1, result.output + assert json.loads(result.output) == { + "projects": [ + { + "classifications": [], + "drift": None, + "error": "the requested remote Compose project was not discovered", + "findings": [], + "name": "missing", + "role": None, + "status": "failed", + } + ] + } + + +@pytest.mark.parametrize( + ("failure_mode", "expected_error"), + ( + ("ssh", "SSH connection or remote command failed"), + ("compose", "remote Compose resolution failed for project 'myapp'"), + ), +) +def test_audit_quarantines_ssh_and_remote_compose_error_output( + tmp_path: Path, failure_mode: str, expected_error: str +) -> None: + secret_value = "remote-error-value-must-not-print" + bin_directory = _install_stub_ssh( + tmp_path, + f"""\ +#!/bin/sh +request=$(cat) + +if [ "$*" = '-- web1 docker compose ls --all --format json' ]; then + printf '%s\\n' '[{{"Name":"myapp","Status":"running(1)",' + printf '%s\\n' '"ConfigFiles":"/srv/myapp/compose.yaml"}}]' + exit 0 +fi + +if [ "{failure_mode}" = ssh ]; then + printf '%s\\n' '{secret_value}' >&2 + exit 255 +fi + +encoded=$(printf '%s\\n' "$request" | tail -n 2 | head -n 1) +payload=$(printf '%s' "$encoded" | base64 -d) +operation=$(printf '%s\\n' "$payload" | head -n 1) +case "$operation" in + capture) + printf '%s\n' 'file 640 0 0' + base64 <<'EOF' | tr -d '\n' +services: + web: + image: ghcr.io/example/web:1.2.3 +EOF + printf '\n' + ;; + stat) + printf '%s\\n' 'missing' + ;; + config) + printf '%s\\n' '{secret_value}' >&2 + exit 1 + ;; + *) + exit 97 + ;; +esac +""", + ) + + result = CliRunner().invoke( + main, + ["audit", "--host", "web1", "--project", "myapp", "--format", "json"], + env=_ssh_environment(bin_directory, tmp_path / "ssh-log"), + ) + + assert result.exit_code == 1, result.output + project = json.loads(result.output)["projects"][0] + assert project["error"] == expected_error + assert secret_value not in result.output + + +def test_audit_quarantines_discovery_error_output(tmp_path: Path) -> None: + secret_value = "discovery-error-value-must-not-print" + bin_directory = _install_stub_ssh( + tmp_path, + f"#!/bin/sh\nprintf '%s\\n' '{secret_value}' >&2\nexit 255\n", + ) + + result = CliRunner().invoke( + main, + ["audit", "--host", "web1", "--project", "myapp"], + env=_ssh_environment(bin_directory, tmp_path / "ssh-log"), + ) + + assert result.exit_code == 1 + assert "SSH discovery failed" in result.output + assert secret_value not in result.output + + +def test_audit_rejects_control_characters_in_discovered_project_identity(tmp_path: Path) -> None: + bin_directory = _install_stub_ssh( + tmp_path, + """\ +#!/bin/sh +if [ "$*" = '-- web1 docker compose ls --all --format json' ]; then + printf '%s\\n' '[{"Name":"bad\\nproject","Status":"running(1)",' + printf '%s\\n' '"ConfigFiles":"/srv/bad/compose.yaml"}]' + exit 0 +fi +exit 99 +""", + ) + + result = CliRunner().invoke( + main, + ["audit", "--host", "web1", "--all"], + env=_ssh_environment(bin_directory, tmp_path / "ssh-log"), + ) + + assert result.exit_code == 1 + assert "remote Docker Compose discovery returned invalid project data" in result.output + assert "bad" not in result.output + + +def test_audit_keeps_remote_plaintext_only_in_memory(tmp_path: Path) -> None: + secret_value = "quarantine-value-must-not-persist" + capture_file = tmp_path / "ssh-capture" + bin_directory = _install_stub_ssh( + tmp_path, + """\ +#!/bin/sh +request=$(cat) +printf '%s\\n' "$request" >> "$C2R_SSH_CAPTURE" + +if [ "$*" = '-- web1 docker compose ls --all --format json' ]; then + printf '%s\\n' '[{"Name":"myapp","Status":"running(1)",' + printf '%s\\n' '"ConfigFiles":"/srv/myapp/compose.yaml"}]' + exit 0 +fi + +encoded=$(printf '%s\\n' "$request" | tail -n 2 | head -n 1) +payload=$(printf '%s' "$encoded" | base64 -d) +operation=$(printf '%s\\n' "$payload" | head -n 1) +case "$operation" in + capture) + printf '%s\n' 'file 640 0 0' + base64 < None: + closing = "}}}}" + bin_directory = _install_stub_ssh( + tmp_path, + f"""\ +#!/bin/sh +request=$(cat) + +if [ "$*" = '-- web1 docker compose ls --all --format json' ]; then + printf '%s\\n' '[{{"Name":"myapp","Status":"running(1)",' + printf '%s\\n' '"ConfigFiles":"/srv/myapp/compose.yaml"}}]' + exit 0 +fi + +encoded=$(printf '%s\\n' "$request" | tail -n 2 | head -n 1) +payload=$(printf '%s' "$encoded" | base64 -d) +operation=$(printf '%s\\n' "$payload" | head -n 1) +case "$operation" in + capture) + printf '%s\n' 'file 640 0 0' + base64 <<'EOF' | tr -d '\n' +services: + web: + image: ghcr.io/example/web:1.2.3 +EOF + printf '\n' + ;; + stat) + printf '%s\\n' 'missing' + ;; + config) + printf '%s' '{{"services":{{"web":{{"image":"ghcr.io/example/web:1.2.3",' + printf '%s' '"user":"1000:1000","healthcheck":{{"test":["CMD","true"]}},' + printf '%s' '"restart":"unless-stopped","environment":{{"FROM_HOST":' + printf '%s\\n' '{effective_value}{closing}' + ;; + *) + exit 97 + ;; +esac +""", + ) + + result = CliRunner().invoke( + main, + ["audit", "--host", "web1", "--project", "myapp", "--format", "json"], + env=_ssh_environment(bin_directory, tmp_path / "ssh-log"), + ) + + assert result.exit_code == 1, result.output + project = json.loads(result.output)["projects"][0] + assert project["error"] == "remote Docker Compose returned an unsupported effective model" + assert "FROM_HOST" not in result.output + + +def test_audit_all_applies_fail_on_to_aggregated_remote_findings(tmp_path: Path) -> None: + bin_directory = _install_stub_ssh( + tmp_path, + """\ +#!/bin/sh +request=$(cat) + +if [ "$*" = '-- web1 docker compose ls --all --format json' ]; then + printf '%s\\n' '[{"Name":"warn","Status":"running(1)",' + printf '%s\\n' '"ConfigFiles":"/srv/warn/compose.yaml"},' + printf '%s\\n' '{"Name":"clean","Status":"running(1)",' + printf '%s\\n' '"ConfigFiles":"/srv/clean/compose.yaml"}]' + exit 0 +fi + +encoded=$(printf '%s\\n' "$request" | tail -n 2 | head -n 1) +payload=$(printf '%s' "$encoded" | base64 -d) +operation=$(printf '%s\\n' "$payload" | head -n 1) +project=$(printf '%s\\n' "$payload" | sed -n '2p') +case "$operation" in + capture) + printf '%s\n' 'file 640 0 0' + base64 <<'EOF' | tr -d '\n' +services: + web: + image: ghcr.io/example/web:1.2.3 +EOF + printf '\n' + ;; + stat) + printf '%s\\n' 'missing' + ;; + config) + if [ "$project" = warn ]; then + printf '%s\\n' '{"services":{"web":{"image":"ghcr.io/example/web",' + else + printf '%s\\n' '{"services":{"web":{"image":"ghcr.io/example/web:1.2.3",' + fi + printf '%s\\n' '"user":"1000:1000","healthcheck":{"test":["CMD","true"]},' + printf '%s\\n' '"restart":"unless-stopped","environment":{}}}}' + ;; + *) + exit 97 + ;; +esac +""", + ) + + result = CliRunner().invoke( + main, + [ + "audit", + "--host", + "web1", + "--all", + "--format", + "json", + "--fail-on", + "warning", + ], + env=_ssh_environment(bin_directory, tmp_path / "ssh-log"), + ) + + assert result.exit_code == 3, result.output + projects = json.loads(result.output)["projects"] + assert [project["name"] for project in projects] == ["warn", "clean"] + assert projects[0]["findings"][0]["id"] == "C2R001" + + error_result = CliRunner().invoke( + main, + [ + "audit", + "--host", + "web1", + "--all", + "--format", + "json", + "--fail-on", + "error", + ], + env=_ssh_environment(bin_directory, tmp_path / "error-ssh-log"), + ) + + assert error_result.exit_code == 0, error_result.output + + +@pytest.mark.parametrize( + ("env_file", "state", "expected_error"), + ( + ( + "/etc/remote.env", + "missing", + "remote environment file path escapes the project root", + ), + ( + "../outside.env", + "missing", + "remote environment file path escapes the project root", + ), + ( + "linked.env", + "symlink", + "remote service env_file input is not a regular file", + ), + ( + "missing.env", + "missing", + "remote service env_file input is not a regular file", + ), + ( + "nested/linked.env", + "escape", + "remote environment file path escapes the project root", + ), + ), +) +def test_audit_refuses_environment_inputs_outside_or_linked_from_project_root( + tmp_path: Path, + env_file: str, + state: str, + expected_error: str, +) -> None: + config_called = tmp_path / "config-called" + bin_directory = _install_stub_ssh( + tmp_path, + f"""\ +#!/bin/sh +request=$(cat) + +if [ "$*" = '-- web1 docker compose ls --all --format json' ]; then + printf '%s\\n' '[{{"Name":"myapp","Status":"running(1)",' + printf '%s\\n' '"ConfigFiles":"/srv/myapp/compose.yaml"}}]' + exit 0 +fi + +encoded=$(printf '%s\\n' "$request" | tail -n 2 | head -n 1) +payload=$(printf '%s' "$encoded" | base64 -d) +operation=$(printf '%s\\n' "$payload" | head -n 1) +path=$(printf '%s\\n' "$payload" | sed -n '2p') +case "$operation" in + capture) + printf '%s\n' 'file 640 0 0' + base64 <<'EOF' | tr -d '\n' +services: + web: + image: ghcr.io/example/web:1.2.3 + env_file: {env_file} +EOF + printf '\n' + ;; + stat) + if [ "$path" = /srv/myapp/.env ]; then + printf '%s\\n' 'missing' + else + printf '%s\\n' '{state}' + fi + ;; + config) + : > "$C2R_CONFIG_CALLED" + exit 99 + ;; + *) + exit 97 + ;; +esac +""", + ) + + result = CliRunner().invoke( + main, + ["audit", "--host", "web1", "--project", "myapp", "--format", "json"], + env={ + **_ssh_environment(bin_directory, tmp_path / "ssh-log"), + "C2R_CONFIG_CALLED": str(config_called), + }, + ) + + assert result.exit_code == 1, result.output + project = json.loads(result.output)["projects"][0] + assert project["error"] == expected_error + assert not config_called.exists() + + +def test_audit_rejects_parent_symlink_escape_from_a_glob_named_root(tmp_path: Path) -> None: + root = tmp_path / "my[ab]" + root.mkdir() + outside = tmp_path / "mya" + outside.mkdir() + (outside / "linked.env").write_text("LOG_LEVEL=debug\n", encoding="utf-8") + (root / "nested").symlink_to(outside, target_is_directory=True) + compose_path = root / "compose.yaml" + compose_path.write_text( + """\ +services: + web: + image: ghcr.io/example/web:1.2.3 + env_file: nested/linked.env +""", + encoding="utf-8", + ) + docker_called = tmp_path / "docker-called" + bin_directory = _install_stub_ssh( + tmp_path, + """\ +#!/bin/sh +if [ "$1" = -- ] && [ "$2" = web1 ] && [ "$3" = docker ]; then + printf '%s\\n' "$C2R_DISCOVERY_JSON" + exit 0 +fi +if [ "$1" = -- ] && [ "$2" = web1 ] && [ "$3" = sh ] && [ "$4" = -s ]; then + shift 2 + exec "$@" +fi +exit 99 +""", + source_session=True, + ) + docker = bin_directory / "docker" + docker.write_text( + """\ +#!/bin/sh +: > "$C2R_DOCKER_CALLED" +printf '%s\\n' "$C2R_CANONICAL_JSON" +""", + encoding="utf-8", + ) + docker.chmod(0o755) + + result = CliRunner().invoke( + main, + ["audit", "--host", "web1", "--project", "myapp", "--format", "json"], + env={ + **_ssh_environment(bin_directory, tmp_path / "ssh-log"), + "C2R_CONFIG_PATH": str(compose_path), + "C2R_DISCOVERY_JSON": json.dumps( + [{"Name": "myapp", "Status": "running(1)", "ConfigFiles": str(compose_path)}] + ), + "C2R_DOCKER_CALLED": str(docker_called), + "C2R_CANONICAL_JSON": json.dumps( + { + "services": { + "web": { + "environment": {}, + "healthcheck": {"test": ["CMD", "true"]}, + "image": "ghcr.io/example/web:1.2.3", + "restart": "unless-stopped", + "user": "1000:1000", + } + } + } + ), + }, + ) + + assert result.exit_code == 1, result.output + project = json.loads(result.output)["projects"][0] + assert project["error"] == "remote environment file path escapes the project root" + assert not docker_called.exists() + + +def test_audit_uses_noninteractive_sudo_for_every_remote_operation(tmp_path: Path) -> None: + log_file = tmp_path / "ssh-log" + bin_directory = _install_stub_ssh( + tmp_path, + """\ +#!/bin/sh +request=$(cat) +{ + printf '%s\\n' 'ARGV' + printf '%s\\n' "$@" + printf '%s\\n' 'END' +} >> "$C2R_SSH_LOG" + +if [ "$*" = '-- web1 sudo -n docker compose ls --all --format json' ]; then + printf '%s\\n' '[{"Name":"myapp","Status":"running(1)",' + printf '%s\\n' '"ConfigFiles":"/srv/myapp/compose.yaml"}]' + exit 0 +fi + +encoded=$(printf '%s\\n' "$request" | tail -n 2 | head -n 1) +payload=$(printf '%s' "$encoded" | base64 -d) +operation=$(printf '%s\\n' "$payload" | head -n 1) +case "$operation" in + capture) + printf '%s\n' 'file 640 0 0' + base64 <<'EOF' | tr -d '\n' +services: + web: + image: ghcr.io/example/web:1.2.3 +EOF + printf '\n' + ;; + stat) + printf '%s\\n' 'missing' + ;; + config) + printf '%s\\n' '{"services":{"web":{"image":"ghcr.io/example/web:1.2.3",' + printf '%s\\n' '"user":"1000:1000","healthcheck":{"test":["CMD","true"]},' + printf '%s\\n' '"restart":"unless-stopped","environment":{}}}}' + ;; + *) + exit 97 + ;; +esac +""", + ) + + result = CliRunner().invoke( + main, + ["audit", "--host", "web1", "--sudo", "--project", "myapp", "--format", "json"], + env=_ssh_environment(bin_directory, log_file), + ) + + assert result.exit_code == 0, result.output + log = log_file.read_text(encoding="utf-8") + assert "ARGV\n--\nweb1\nsudo\n-n\ndocker\ncompose\nls\n--all\n--format\njson" in log + assert log.count("ARGV\n--\nweb1\nsudo\n-n\nsh\n-s\nEND") == 3 + assert "StrictHostKeyChecking" not in log + + +@pytest.mark.parametrize( + "arguments", + ( + ["audit", "--host", "web1"], + ["audit", "--host", "web1", "--project", "myapp", "--all"], + ), +) +def test_audit_requires_one_remote_selection( + tmp_path: Path, arguments: list[str] +) -> None: + ssh_log = tmp_path / "ssh-log" + bin_directory = _install_stub_ssh( + tmp_path, + "#!/bin/sh\nprintf '%s\\n' invoked >> \"$C2R_SSH_LOG\"\nexit 99\n", + ) + + result = CliRunner().invoke( + main, + arguments, + env=_ssh_environment(bin_directory, ssh_log), + ) + + assert result.exit_code == 2, result.output + assert not ssh_log.exists() diff --git a/tests/test_remote_generate.py b/tests/test_remote_generate.py new file mode 100644 index 0000000..9bb8def --- /dev/null +++ b/tests/test_remote_generate.py @@ -0,0 +1,4109 @@ +"""Remote generation behavior at the command-line and emitted-role seams.""" + +from __future__ import annotations + +import base64 +import hashlib +import json +import os +import subprocess +from pathlib import Path + +import pytest +from click.testing import CliRunner +from jinja2 import Environment +from ruamel.yaml import YAML +from ssh_stub import legacy_source_session_bridge + +from compose2role.cli import main + + +def _install_stub_command(bin_directory: Path, name: str, script: str) -> None: + command = bin_directory / name + if name == "ssh": + legacy_ssh = bin_directory / "ssh-legacy" + legacy_ssh.write_text(script, encoding="utf-8") + legacy_ssh.chmod(0o755) + command.write_text(legacy_source_session_bridge(legacy_ssh), encoding="utf-8") + command.chmod(0o755) + return + command.write_text(script, encoding="utf-8") + command.chmod(0o755) + + +def _encrypted_sops_output(*keys: str) -> str: + return "\n".join( + ( + *(f"{key}: ENC[AES256_GCM,data:fixture]" for key in keys), + "sops:", + " mac: ENC[AES256_GCM,data:fixture]", + ) + ) + + +def _ansible_executable(name: str) -> str: + configured_bin = os.environ.get("COMPOSE2ROLE_ANSIBLE_BIN") + return str(Path(configured_bin) / name) if configured_bin else name + + +def _assert_generated_role_validates( + tmp_path: Path, output_directory: Path, role_name: str +) -> None: + """Lint and syntax-check one generated role against the pinned collections.""" + role_directory = output_directory / role_name + environment = { + **os.environ, + "ANSIBLE_COLLECTIONS_PATH": str(Path(__file__).parents[1] / "collections"), + "ANSIBLE_ROLES_PATH": str(output_directory), + } + lint_result = subprocess.run( + [ + _ansible_executable("ansible-lint"), + "--exclude", + str(role_directory / "vars" / "secrets.sops.yml"), + str(role_directory), + ], + check=False, + capture_output=True, + # ansible-lint treats the working directory as the project and skips an + # out-of-tree target, so lint from the roles directory, not the repository. + cwd=output_directory, + env=environment, + text=True, + ) + assert lint_result.returncode == 0, lint_result.stdout + lint_result.stderr + playbook = tmp_path / "syntax-check.yml" + playbook.write_text( + f"""\ +--- +- name: Validate generated role syntax + hosts: localhost + connection: local + gather_facts: false + roles: + - {role_name} +""", + encoding="utf-8", + ) + syntax_result = subprocess.run( + [ + _ansible_executable("ansible-playbook"), + "--inventory", + "localhost,", + "--connection", + "local", + "--syntax-check", + str(playbook), + ], + check=False, + capture_output=True, + env=environment, + text=True, + ) + assert syntax_result.returncode == 0, syntax_result.stdout + syntax_result.stderr + + +def _remote_environment( + tmp_path: Path, source_document: str, canonical_document: dict[str, object] +) -> tuple[dict[str, str], Path]: + bin_directory = tmp_path / "bin" + bin_directory.mkdir() + _install_stub_command( + bin_directory, + "ssh", + """\ +#!/bin/sh +request=$(cat) + +if [ "$*" = '-- web1 docker compose ls --all --format json' ]; then + config_path=${C2R_CONFIG_PATH:-/srv/my-app/compose.yaml} + printf '[{"Name":"my-app","Status":"running(1)","ConfigFiles":"%s"}]\\n' "$config_path" + exit 0 +fi + +case "$*" in + *"docker ps"*) + printf '%s' "${C2R_WORKING_DIRECTORY_LABEL:-}" + exit 0 + ;; +esac + +encoded=$(printf '%s\\n' "$request" | tail -n 2 | head -n 1) +payload=$(printf '%s' "$encoded" | base64 -d) +operation=$(printf '%s\\n' "$payload" | head -n 1) +case "$operation" in + capture) + printf '%s\\n' 'file 640 0 0' + printf '%s\\n' "$C2R_SOURCE_DOCUMENT" + ;; + stat) + printf '%s\\n' missing + ;; + config) + printf '%s\\n' "$C2R_CANONICAL_DOCUMENT" + ;; + *) + exit 97 + ;; +esac +""", + ) + _install_stub_command(bin_directory, "sops", "#!/bin/sh\nexit 99\n") + sops_config = tmp_path / "sops.yaml" + sops_config.write_text( + "creation_rules:\n - path_regex: vars/secrets\\.sops\\.yml$\n", + encoding="utf-8", + ) + return ( + { + "C2R_CANONICAL_DOCUMENT": json.dumps(canonical_document), + "C2R_SOURCE_DOCUMENT": base64.b64encode(source_document.encode("utf-8")).decode( + "ascii" + ), + "PATH": f"{bin_directory}:{os.environ['PATH']}", + }, + sops_config, + ) + + +def test_generate_creates_a_secret_free_remote_role(tmp_path: Path) -> None: + """A source-fidelity role adopts one remote Compose file in place.""" + source_document = ( + "# Preserve comments, quotes, ordering, and CRLF.\r\n" + "services:\r\n" + " web:\r\n" + " image: \"ghcr.io/example/web:1.2.3\"\r\n" + " ports:\r\n" + " - \"8080:80\"\r\n" + " environment:\r\n" + " LOG_LEVEL: info\r\n" + ) + canonical_document = { + "services": { + "web": { + "environment": {"LOG_LEVEL": "info"}, + "healthcheck": {"test": ["CMD", "true"]}, + "image": "ghcr.io/example/web:1.2.3", + "ports": [{"target": 80, "published": "8080", "protocol": "tcp"}], + "restart": "unless-stopped", + "user": "1000:1000", + } + } + } + environment, sops_config = _remote_environment(tmp_path, source_document, canonical_document) + output_directory = tmp_path / "roles" + + result = CliRunner().invoke( + main, + [ + "generate", + "--host", + "web1", + "--project", + "my-app", + "--output", + str(output_directory), + "--sops-config", + str(sops_config), + ], + env=environment, + ) + + assert result.exit_code == 0, result.output + assert result.output.splitlines() == [ + "Discovering projects on web1...", + "✓ my-app -> my_app (generated)", + "", + "1 generated", + ] + role_directory = output_directory / "my_app" + defaults = (role_directory / "defaults" / "main.yml").read_text(encoding="utf-8") + assert 'my_app_compose_project_name: "my-app"' in defaults + assert 'my_app_project_dir: "/srv/my-app"' in defaults + assert 'my_app_web_image: "ghcr.io/example/web"' in defaults + assert 'my_app_web_image_tag: "1.2.3"' in defaults + assert 'my_app_web_published_port: "8080"' in defaults + assert 'my_app_web_log_level: "info"' in defaults + compose_template = ( + role_directory / "templates" / "compose.yaml.j2" + ).read_bytes().decode("utf-8") + assert compose_template.startswith("# Preserve comments, quotes, ordering, and CRLF.\r\n") + assert 'image: "{{ my_app_web_image }}:{{ my_app_web_image_tag }}"' in compose_template + assert '"{{ my_app_web_published_port }}:80"' in compose_template + assert "LOG_LEVEL: {{ my_app_web_log_level }}" in compose_template + generated_defaults = YAML(typ="safe").load(defaults) + assert isinstance(generated_defaults, dict) + rendered_source = Environment( + keep_trailing_newline=True, newline_sequence="\r\n" + ).from_string(compose_template).render(**generated_defaults) + assert rendered_source.encode("utf-8") == source_document.encode("utf-8") + tasks = (role_directory / "tasks" / "main.yml").read_text(encoding="utf-8") + assert 'project_name: "{{ my_app_compose_project_name }}"' in tasks + assert 'dest: "{{ my_app_project_dir }}/compose.yaml"' in tasks + assert 'src: "compose.yaml.j2"' in tasks + assert 'owner: "0"' in tasks + assert 'mode: "0640"' in tasks + assert 'newline_sequence: "\\r\\n"' in tasks + assert 'files:\n - "compose.yaml"' in tasks + assert "compose.yml.j2" not in tasks + assert "- name: \"Render compose.yaml\"\n tags:\n - source\n" in tasks + assert "- name: Deploy my_app Compose project\n tags:\n - compose\n" in tasks + assert not (role_directory / "templates" / "compose.yml.j2").exists() + assert not (role_directory / ".sops.yaml").exists() + assert not (role_directory / "vars" / "secrets.example.yml").exists() + assert not (role_directory / "vars" / "secrets.sops.yml").exists() + + +def test_generate_reports_a_byte_identical_remote_role_as_unchanged(tmp_path: Path) -> None: + """Re-running remote generation against the same source is a no-op.""" + source_document = """\ +services: + web: + image: ghcr.io/example/web:1.2.3 + environment: + LOG_LEVEL: info +""" + canonical_document = { + "services": { + "web": { + "environment": {"LOG_LEVEL": "info"}, + "healthcheck": {"test": ["CMD", "true"]}, + "image": "ghcr.io/example/web:1.2.3", + "restart": "unless-stopped", + "user": "1000:1000", + } + } + } + environment, sops_config = _remote_environment(tmp_path, source_document, canonical_document) + output_directory = tmp_path / "roles" + arguments = [ + "generate", + "--host", + "web1", + "--project", + "my-app", + "--output", + str(output_directory), + "--sops-config", + str(sops_config), + ] + + first = CliRunner().invoke(main, arguments, env=environment) + assert first.exit_code == 0, first.output + role_directory = output_directory / "my_app" + before = { + str(path.relative_to(role_directory)): path.read_bytes() + for path in sorted(role_directory.rglob("*")) + if path.is_file() + } + + second = CliRunner().invoke(main, arguments, env=environment) + + assert second.exit_code == 0, second.output + assert second.output.splitlines() == [ + "Discovering projects on web1...", + "✓ my-app -> my_app (unchanged)", + "", + "1 unchanged", + ] + after = { + str(path.relative_to(role_directory)): path.read_bytes() + for path in sorted(role_directory.rglob("*")) + if path.is_file() + } + assert after == before + assert not (output_directory / ".compose2role").exists() + + json_result = CliRunner().invoke(main, [*arguments, "--format", "json"], env=environment) + assert json_result.exit_code == 0, json_result.output + assert json.loads(json_result.output)["projects"][0]["status"] == "unchanged" + + +def test_generate_remote_diff_reports_path_states_without_writing(tmp_path: Path) -> None: + """Remote --diff reviews the rendered plan without writing role content.""" + source_document = """\ +services: + web: + image: ghcr.io/example/web:1.2.3 + environment: + LOG_LEVEL: info +""" + canonical_document = { + "services": { + "web": { + "environment": {"LOG_LEVEL": "info"}, + "image": "ghcr.io/example/web:1.2.3", + } + } + } + environment, sops_config = _remote_environment(tmp_path, source_document, canonical_document) + output_directory = tmp_path / "roles" + arguments = [ + "generate", + "--host", + "web1", + "--project", + "my-app", + "--output", + str(output_directory), + "--sops-config", + str(sops_config), + ] + + first = CliRunner().invoke(main, arguments, env=environment) + assert first.exit_code == 0, first.output + role_directory = output_directory / "my_app" + before = { + str(path.relative_to(role_directory)): path.read_bytes() + for path in sorted(role_directory.rglob("*")) + if path.is_file() + } + + changed_source = source_document + " command: [\"serve\"]\n" + environment["C2R_SOURCE_DOCUMENT"] = base64.b64encode( + changed_source.encode("utf-8") + ).decode("ascii") + environment["C2R_CANONICAL_DOCUMENT"] = json.dumps( + { + "services": { + "web": { + "command": ["serve"], + "environment": {"LOG_LEVEL": "info"}, + "image": "ghcr.io/example/web:1.2.3", + } + } + } + ) + result = CliRunner().invoke( + main, [*arguments, "--diff", "--format", "json"], env=environment + ) + + assert result.exit_code == 0, result.output + payload = json.loads(result.output) + assert payload["projects"][0]["status"] == "diff" + diff = payload["projects"][0]["diff"] + assert ".compose2role.json" in diff["changed"] + assert "templates/compose.yaml.j2" in diff["withheld"] + assert "defaults/main.yml" in diff["unchanged"] + assert "1.2.3" not in json.dumps(diff) + after = { + str(path.relative_to(role_directory)): path.read_bytes() + for path in sorted(role_directory.rglob("*")) + if path.is_file() + } + assert after == before + assert not (output_directory / ".compose2role").exists() + + +def test_generate_hides_warning_findings_unless_verbose(tmp_path: Path) -> None: + """Default text generation shows errors only; --verbose adds warnings.""" + source_document = """\ +services: + web: + image: ghcr.io/example/web +""" + canonical_document = { + "services": { + "web": { + "environment": {}, + "healthcheck": {"test": ["CMD", "true"]}, + "image": "ghcr.io/example/web", + "restart": "unless-stopped", + "user": "1000:1000", + } + } + } + environment, sops_config = _remote_environment(tmp_path, source_document, canonical_document) + base_arguments = [ + "generate", + "--host", + "web1", + "--project", + "my-app", + "--sops-config", + str(sops_config), + ] + + quiet = CliRunner().invoke( + main, + [*base_arguments, "--output", str(tmp_path / "quiet-roles")], + env=environment, + ) + verbose = CliRunner().invoke( + main, + [*base_arguments, "--output", str(tmp_path / "verbose-roles"), "--verbose"], + env=environment, + ) + + assert quiet.exit_code == 0, quiet.output + assert "C2R001" not in quiet.output + assert "✓ my-app -> my_app (generated)" in quiet.output + assert verbose.exit_code == 0, verbose.output + assert "C2R001" in verbose.output + assert "WARNING" in verbose.output + + +def test_generate_uses_a_labeled_compose_project_directory_for_defaults_and_provenance( + tmp_path: Path, +) -> None: + """A valid container label supersedes the first Compose file's parent directory.""" + source_document = """\ +services: + web: + image: ghcr.io/example/web:1.2.3 +""" + canonical_document = { + "services": { + "web": { + "environment": {}, + "healthcheck": {"test": ["CMD", "true"]}, + "image": "ghcr.io/example/web:1.2.3", + "restart": "unless-stopped", + "user": "1000:1000", + } + } + } + environment, sops_config = _remote_environment(tmp_path, source_document, canonical_document) + output_directory = tmp_path / "roles" + + result = CliRunner().invoke( + main, + [ + "generate", + "--host", + "web1", + "--project", + "my-app", + "--output", + str(output_directory), + "--sops-config", + str(sops_config), + ], + env={ + **environment, + "C2R_CONFIG_PATH": "/srv/stacks/my-app/compose/compose.yaml", + "C2R_WORKING_DIRECTORY_LABEL": "my-app\t/srv/stacks/my-app\n", + }, + ) + + assert result.exit_code == 0, result.output + role_directory = output_directory / "my_app" + defaults = (role_directory / "defaults" / "main.yml").read_text(encoding="utf-8") + assert 'my_app_project_dir: "/srv/stacks/my-app"' in defaults + assert (role_directory / "templates" / "compose" / "compose.yaml.j2").is_file() + provenance = json.loads((role_directory / ".compose2role.json").read_text(encoding="utf-8")) + assert provenance["source"]["root"] == "/srv/stacks/my-app" + + +def test_generate_adopts_merge_keys_and_aliases_without_duplicate_edits(tmp_path: Path) -> None: + """Inherited and aliased values stay literal while direct values remain configurable.""" + source_document = """\ +x-common: &common + image: &common_image ghcr.io/example/shared:1.2.3 + ports: &common_ports + - &common_port "8080:80" + environment: &common_environment + LOG_LEVEL: &common_log info +services: + merged_one: + <<: *common + merged_two: + <<: *common + direct: + <<: *common + image: ghcr.io/example/direct:2.0.0 + ports: + - "8081:81" + environment: + LOG_LEVEL: debug + aliases: + image: *common_image + ports: *common_ports + environment: *common_environment +""" + canonical_document = { + "services": { + "aliases": { + "environment": {"LOG_LEVEL": "info"}, + "healthcheck": {"test": ["CMD", "true"]}, + "image": "ghcr.io/example/shared:1.2.3", + "ports": [{"target": 80, "published": "8080", "protocol": "tcp"}], + "restart": "unless-stopped", + "user": "1000:1000", + }, + "direct": { + "environment": {"LOG_LEVEL": "debug"}, + "healthcheck": {"test": ["CMD", "true"]}, + "image": "ghcr.io/example/direct:2.0.0", + "ports": [{"target": 81, "published": "8081", "protocol": "tcp"}], + "restart": "unless-stopped", + "user": "1000:1000", + }, + "merged_one": { + "environment": {"LOG_LEVEL": "info"}, + "healthcheck": {"test": ["CMD", "true"]}, + "image": "ghcr.io/example/shared:1.2.3", + "ports": [{"target": 80, "published": "8080", "protocol": "tcp"}], + "restart": "unless-stopped", + "user": "1000:1000", + }, + "merged_two": { + "environment": {"LOG_LEVEL": "info"}, + "healthcheck": {"test": ["CMD", "true"]}, + "image": "ghcr.io/example/shared:1.2.3", + "ports": [{"target": 80, "published": "8080", "protocol": "tcp"}], + "restart": "unless-stopped", + "user": "1000:1000", + }, + } + } + environment, sops_config = _remote_environment(tmp_path, source_document, canonical_document) + output_directory = tmp_path / "roles" + + result = CliRunner().invoke( + main, + [ + "generate", + "--host", + "web1", + "--project", + "my-app", + "--output", + str(output_directory), + "--sops-config", + str(sops_config), + ], + env=environment, + ) + + assert result.exit_code == 0, result.output + role_directory = output_directory / "my_app" + defaults = (role_directory / "defaults" / "main.yml").read_text(encoding="utf-8") + assert 'my_app_direct_image: "ghcr.io/example/direct"' in defaults + assert 'my_app_direct_image_tag: "2.0.0"' in defaults + assert 'my_app_direct_published_port: "8081"' in defaults + assert 'my_app_direct_log_level: "debug"' in defaults + assert "my_app_merged_one_" not in defaults + assert "my_app_merged_two_" not in defaults + assert "my_app_aliases_" not in defaults + compose_template = (role_directory / "templates" / "compose.yaml.j2").read_text( + encoding="utf-8" + ) + assert compose_template.count(" <<: *common\n") == 3 + assert " image: &common_image ghcr.io/example/shared:1.2.3\n" in compose_template + assert ' - &common_port "8080:80"\n' in compose_template + assert " LOG_LEVEL: &common_log info\n" in compose_template + assert " image: *common_image\n" in compose_template + assert " ports: *common_ports\n" in compose_template + assert " environment: *common_environment\n" in compose_template + assert "image: {{ my_app_direct_image }}:{{ my_app_direct_image_tag }}" in compose_template + assert '"{{ my_app_direct_published_port }}:81"' in compose_template + assert "LOG_LEVEL: {{ my_app_direct_log_level }}" in compose_template + rendered_source = Environment(keep_trailing_newline=True).from_string(compose_template).render( + **YAML(typ="safe").load(defaults) + ) + assert rendered_source.encode("utf-8") == source_document.encode("utf-8") + + +@pytest.mark.parametrize( + "source_document_template", + ( + """\ +x-common: &common + image: ghcr.io/example/web:1.2.3 + environment: + API_TOKEN: {secret_value} +services: + web: + <<: *common +""", + """\ +x-common: &common + environment: &secret_environment + API_TOKEN: {secret_value} +services: + web: + image: ghcr.io/example/web:1.2.3 + environment: *secret_environment +""", + ), + ids=("merge", "alias"), +) +def test_generate_rejects_secret_environment_without_direct_source_span( + tmp_path: Path, source_document_template: str +) -> None: + """A secret without a direct source span never reaches a generated role.""" + secret_value = "merged-secret-must-not-persist" + source_document = source_document_template.format(secret_value=secret_value) + canonical_document = { + "services": { + "web": { + "environment": {"API_TOKEN": secret_value}, + "healthcheck": {"test": ["CMD", "true"]}, + "image": "ghcr.io/example/web:1.2.3", + "restart": "unless-stopped", + "user": "1000:1000", + } + } + } + environment, sops_config = _remote_environment(tmp_path, source_document, canonical_document) + output_directory = tmp_path / "roles" + + result = CliRunner().invoke( + main, + [ + "generate", + "--host", + "web1", + "--project", + "my-app", + "--output", + str(output_directory), + "--sops-config", + str(sops_config), + ], + env=environment, + ) + + assert result.exit_code == 1, result.output + assert "remote Compose source has an unsupported secret environment" in result.output + assert secret_value not in result.output + assert not output_directory.exists() + + +def test_generate_rejects_secret_inherited_by_a_partial_environment_override( + tmp_path: Path, +) -> None: + """A direct override cannot leave an inherited secret in the template.""" + secret_value = "partial-override-secret-must-not-persist" + source_document = f"""\ +x-common: &common + image: ghcr.io/example/web:1.2.3 + environment: + API_TOKEN: {secret_value} +services: + web: + <<: *common + environment: + LOG_LEVEL: debug +""" + canonical_document = { + "services": { + "web": { + "environment": {"API_TOKEN": secret_value, "LOG_LEVEL": "debug"}, + "healthcheck": {"test": ["CMD", "true"]}, + "image": "ghcr.io/example/web:1.2.3", + "restart": "unless-stopped", + "user": "1000:1000", + } + } + } + environment, sops_config = _remote_environment(tmp_path, source_document, canonical_document) + output_directory = tmp_path / "roles" + + result = CliRunner().invoke( + main, + [ + "generate", + "--host", + "web1", + "--project", + "my-app", + "--output", + str(output_directory), + "--sops-config", + str(sops_config), + ], + env=environment, + ) + + assert result.exit_code == 1, result.output + assert "remote Compose source has an unsupported secret environment" in result.output + assert secret_value not in result.output + assert not output_directory.exists() + + +def test_generate_lifts_list_form_environment_secrets_without_plaintext(tmp_path: Path) -> None: + """A classified list-form environment value is rendered from SOPS, not source plaintext.""" + secret_value = "fixture-$1-secret" + source_document = f"""\ +services: + web: + image: ghcr.io/example/web:1.2.3 + environment: + - LOG_LEVEL=info + - API_TOKEN={secret_value} +""" + canonical_document = { + "services": { + "web": { + "environment": {"API_TOKEN": secret_value, "LOG_LEVEL": "info"}, + "healthcheck": {"test": ["CMD", "true"]}, + "image": "ghcr.io/example/web:1.2.3", + "restart": "unless-stopped", + "user": "1000:1000", + } + } + } + environment, sops_config = _remote_environment(tmp_path, source_document, canonical_document) + bin_directory = Path(environment["PATH"].split(":", maxsplit=1)[0]) + _install_stub_command( + bin_directory, + "sops", + "#!/bin/sh\ncat >/dev/null\nprintf '%s\\n' \"$C2R_SOPS_OUTPUT\"\n", + ) + output_directory = tmp_path / "roles" + + result = CliRunner().invoke( + main, + [ + "generate", + "--host", + "web1", + "--project", + "my-app", + "--output", + str(output_directory), + "--sops-config", + str(sops_config), + ], + env={ + **environment, + "C2R_SOPS_OUTPUT": _encrypted_sops_output("my_app_web_api_token"), + }, + ) + + assert result.exit_code == 0, result.output + role_directory = output_directory / "my_app" + generated_role_contents = b"".join( + path.read_bytes() for path in role_directory.rglob("*") if path.is_file() + ) + assert secret_value.encode("utf-8") not in generated_role_contents + template = (role_directory / "templates" / "compose.yaml.j2").read_text(encoding="utf-8") + assert "- LOG_LEVEL={{ my_app_web_log_level }}" in template + assert "API_TOKEN={{ my_app_secrets['my_app_web_api_token'] }}" in template + tasks = (role_directory / "tasks" / "main.yml").read_text(encoding="utf-8") + assert "community.sops.load_vars:" in tasks + assert "- name: Load my_app SOPS secrets\n tags:\n - secrets\n" in tasks + defaults = (role_directory / "defaults" / "main.yml").read_text(encoding="utf-8") + assert 'my_app_web_log_level: "info"' in defaults + assert " - my_app_web_api_token" in defaults + rendered = Environment(keep_trailing_newline=True).from_string(template).render( + **YAML(typ="safe").load(defaults), + my_app_secrets={"my_app_web_api_token": secret_value}, + ) + assert rendered.encode("utf-8") == source_document.encode("utf-8") + + +def test_generate_lifts_a_quoted_secret_value_ending_in_a_backslash(tmp_path: Path) -> None: + """A quoted secret ending in a literal backslash remains source-faithful.""" + effective_secret_value = "fixture\\" + source_secret_value = r"fixture\\" + source_document = r"""services: + web: + image: ghcr.io/example/web:1.2.3 + environment: + API_TOKEN: "fixture\\" +""" + canonical_document = { + "services": { + "web": { + "environment": {"API_TOKEN": effective_secret_value}, + "healthcheck": {"test": ["CMD", "true"]}, + "image": "ghcr.io/example/web:1.2.3", + "restart": "unless-stopped", + "user": "1000:1000", + } + } + } + environment, sops_config = _remote_environment(tmp_path, source_document, canonical_document) + bin_directory = Path(environment["PATH"].split(":", maxsplit=1)[0]) + sops_payload = tmp_path / "sops-payload.json" + _install_stub_command( + bin_directory, + "sops", + "#!/bin/sh\ncat > \"$C2R_SOPS_PAYLOAD\"\nprintf '%s\\n' \"$C2R_SOPS_OUTPUT\"\n", + ) + output_directory = tmp_path / "roles" + + result = CliRunner().invoke( + main, + [ + "generate", + "--host", + "web1", + "--project", + "my-app", + "--output", + str(output_directory), + "--sops-config", + str(sops_config), + ], + env={ + **environment, + "C2R_SOPS_OUTPUT": _encrypted_sops_output("my_app_web_api_token"), + "C2R_SOPS_PAYLOAD": str(sops_payload), + }, + ) + + assert result.exit_code == 0, result.output + assert json.loads(sops_payload.read_text(encoding="utf-8")) == { + "my_app_web_api_token": source_secret_value + } + role_directory = output_directory / "my_app" + generated_role_contents = b"".join( + path.read_bytes() for path in role_directory.rglob("*") if path.is_file() + ) + assert source_secret_value.encode("utf-8") not in generated_role_contents + assert effective_secret_value.encode("utf-8") not in generated_role_contents + template = (role_directory / "templates" / "compose.yaml.j2").read_text(encoding="utf-8") + assert 'API_TOKEN: "{{ my_app_secrets[\'my_app_web_api_token\'] }}"' in template + defaults = YAML(typ="safe").load( + (role_directory / "defaults" / "main.yml").read_text(encoding="utf-8") + ) + assert isinstance(defaults, dict) + rendered = Environment(keep_trailing_newline=True).from_string(template).render( + **defaults, + my_app_secrets={"my_app_web_api_token": source_secret_value}, + ) + assert rendered.encode("utf-8") == source_document.encode("utf-8") + + +def test_generate_preserves_plain_block_scalar_environment_values(tmp_path: Path) -> None: + """An unparameterizable plain scalar stays literal instead of failing adoption.""" + source_document = """\ +services: + web: + image: ghcr.io/example/web:1.2.3 + environment: + LOG_LEVEL: info + CONFIG_TEXT: >- + hello + world +""" + canonical_document = { + "services": { + "web": { + "environment": {"CONFIG_TEXT": "hello world", "LOG_LEVEL": "info"}, + "healthcheck": {"test": ["CMD", "true"]}, + "image": "ghcr.io/example/web:1.2.3", + "restart": "unless-stopped", + "user": "1000:1000", + } + } + } + environment, sops_config = _remote_environment(tmp_path, source_document, canonical_document) + output_directory = tmp_path / "roles" + + result = CliRunner().invoke( + main, + [ + "generate", + "--host", + "web1", + "--project", + "my-app", + "--output", + str(output_directory), + "--sops-config", + str(sops_config), + ], + env=environment, + ) + + assert result.exit_code == 0, result.output + role_directory = output_directory / "my_app" + template = (role_directory / "templates" / "compose.yaml.j2").read_text(encoding="utf-8") + assert "CONFIG_TEXT: >-\n hello\n world\n" in template + defaults = (role_directory / "defaults" / "main.yml").read_text(encoding="utf-8") + assert 'my_app_web_log_level: "info"' in defaults + assert "my_app_web_config_text" not in defaults + + +def test_generate_parameterizes_short_and_long_form_published_ports(tmp_path: Path) -> None: + """Host-IP, protocol, and long-form port declarations retain their source and knob.""" + source_document = """\ +services: + web: + image: ghcr.io/example/web:1.2.3 + ports: + - "127.0.0.1:8080:80/udp" + - target: 443 + published: "8443" + protocol: tcp +""" + canonical_document = { + "services": { + "web": { + "environment": {}, + "healthcheck": {"test": ["CMD", "true"]}, + "image": "ghcr.io/example/web:1.2.3", + "ports": [ + {"published": "8080", "target": 80, "protocol": "udp"}, + {"published": "8443", "target": 443, "protocol": "tcp"}, + ], + "restart": "unless-stopped", + "user": "1000:1000", + } + } + } + environment, sops_config = _remote_environment(tmp_path, source_document, canonical_document) + output_directory = tmp_path / "roles" + + result = CliRunner().invoke( + main, + [ + "generate", + "--host", + "web1", + "--project", + "my-app", + "--output", + str(output_directory), + "--sops-config", + str(sops_config), + ], + env=environment, + ) + + assert result.exit_code == 0, result.output + role_directory = output_directory / "my_app" + defaults = (role_directory / "defaults" / "main.yml").read_text(encoding="utf-8") + assert 'my_app_web_published_port_1: "8080"' in defaults + assert 'my_app_web_published_port_2: "8443"' in defaults + template = (role_directory / "templates" / "compose.yaml.j2").read_text(encoding="utf-8") + assert '"127.0.0.1:{{ my_app_web_published_port_1 }}:80/udp"' in template + assert 'published: "{{ my_app_web_published_port_2 }}"' in template + rendered = Environment(keep_trailing_newline=True).from_string(template).render( + **YAML(typ="safe").load(defaults) + ) + assert rendered.encode("utf-8") == source_document.encode("utf-8") + + +def test_generated_role_adopts_a_matching_source_tree_without_changes(tmp_path: Path) -> None: + """Matching source is idempotent and unsafe file-secret targets are refused.""" + source_document = b"""\ +# Existing source remains in place. +services: + web: + image: "ghcr.io/example/web:1.2.3" +secrets: + app_secret: + file: ./secrets/app.bin +""" + secret_bytes = b"\x00adopted-secret\n" + canonical_document = { + "services": { + "web": { + "environment": {}, + "healthcheck": {"test": ["CMD", "true"]}, + "image": "ghcr.io/example/web:1.2.3", + "restart": "unless-stopped", + "user": "1000:1000", + } + } + } + bin_directory = tmp_path / "bin" + bin_directory.mkdir() + _install_stub_command( + bin_directory, + "ssh", + """\ +#!/bin/sh +request=$(cat) + +if [ "$*" = '-- web1 docker compose ls --all --format json' ]; then + printf '%s\\n' '[{"Name":"my-app","Status":"running(1)",' + printf '%s\\n' '"ConfigFiles":"/srv/my-app/compose.yaml"}]' + exit 0 +fi + +encoded=$(printf '%s\\n' "$request" | tail -n 2 | head -n 1) +payload=$(printf '%s' "$encoded" | base64 -d) +operation=$(printf '%s\\n' "$payload" | head -n 1) +path=$(printf '%s\\n' "$payload" | sed -n '2p') +case "$operation" in + capture) + case "$path" in + /srv/my-app/compose.yaml) + printf 'file %s %s %s\\n' "$C2R_SOURCE_MODE" "$C2R_SOURCE_UID" "$C2R_SOURCE_GID" + printf '%s\\n' "$C2R_SOURCE_DOCUMENT" + ;; + /srv/my-app/secrets/app.bin) + printf 'file 600 %s %s\\n' "$C2R_SECRET_UID" "$C2R_SECRET_GID" + printf '%s\\n' "$C2R_SECRET_SOURCE" + ;; + *) exit 98 ;; + esac + ;; + stat) + case "$path" in + /srv/my-app/.env) printf '%s\\n' missing ;; + /srv/my-app/secrets/app.bin) printf '%s\\n' file ;; + *) exit 98 ;; + esac + ;; + config) + printf '%s\\n' "$C2R_CANONICAL_DOCUMENT" + ;; + *) exit 97 ;; +esac +""", + ) + _install_stub_command( + bin_directory, + "sops", + "#!/bin/sh\ncat >/dev/null\nprintf '%s\\n' \"$C2R_SOPS_OUTPUT\"\n", + ) + sops_config = tmp_path / "sops.yaml" + sops_config.write_text( + "creation_rules:\n - path_regex: vars/secrets\\.sops\\.yml$\n", + encoding="utf-8", + ) + output_directory = tmp_path / "roles" + result = CliRunner().invoke( + main, + [ + "generate", + "--host", + "web1", + "--project", + "my-app", + "--output", + str(output_directory), + "--sops-config", + str(sops_config), + ], + env={ + "C2R_CANONICAL_DOCUMENT": json.dumps(canonical_document), + "C2R_SECRET_GID": str(os.getgid()), + "C2R_SECRET_SOURCE": base64.b64encode(secret_bytes).decode("ascii"), + "C2R_SECRET_UID": str(os.getuid()), + "C2R_SOURCE_DOCUMENT": base64.b64encode(source_document).decode("ascii"), + "C2R_SOURCE_GID": str(os.getgid()), + "C2R_SOURCE_MODE": "640", + "C2R_SOURCE_UID": str(os.getuid()), + "C2R_SOPS_OUTPUT": _encrypted_sops_output("my_app_secret_app_secret"), + "PATH": f"{bin_directory}:{os.environ['PATH']}", + }, + ) + + assert result.exit_code == 0, result.output + role_directory = output_directory / "my_app" + assert not (role_directory / "README.md").exists() + tasks = (role_directory / "tasks" / "main.yml").read_text(encoding="utf-8") + assert "my_app_secret_app_secret" in tasks + assert "secrets/app.bin" in tasks + source_tree = tmp_path / "matching-source" + source_tree.mkdir() + compose_file = source_tree / "compose.yaml" + compose_file.write_bytes(source_document) + compose_file.chmod(0o640) + secret_file = source_tree / "secrets" / "app.bin" + secret_file.parent.mkdir() + secret_file.write_bytes(secret_bytes) + secret_file.chmod(0o600) + original_checksum = hashlib.sha256(compose_file.read_bytes()).digest() + original_secret_checksum = hashlib.sha256(secret_file.read_bytes()).digest() + fake_collection_root = tmp_path / "fake-collections" + module_directory = ( + fake_collection_root + / "ansible_collections" + / "community" + / "docker" + / "plugins" + / "modules" + ) + module_directory.mkdir(parents=True) + (module_directory / "docker_compose_v2.py").write_text( + """\ +from ansible.module_utils.basic import AnsibleModule + +module = AnsibleModule( + argument_spec={ + "files": {"type": "list", "elements": "str", "required": True}, + "project_name": {"type": "str", "required": True}, + "project_src": {"type": "str", "required": True}, + "pull": {"type": "str", "required": True}, + "recreate": {"type": "str", "required": True}, + "remove_orphans": {"type": "bool", "required": True}, + "state": {"type": "str", "required": True}, + } +) +if module.params["files"] != ["compose.yaml"] or module.params["project_name"] != "my-app": + module.fail_json(msg="unexpected source-fidelity Compose invocation") +module.exit_json(changed=False) +""", + encoding="utf-8", + ) + sops_action_directory = ( + fake_collection_root + / "ansible_collections" + / "community" + / "sops" + / "plugins" + / "action" + ) + sops_action_directory.mkdir(parents=True) + (sops_action_directory / "load_vars.py").write_text( + """\ +from __future__ import annotations + +import os + +from ansible.plugins.action import ActionBase + + +class ActionModule(ActionBase): + def run(self, tmp=None, task_vars=None): + result = super().run(tmp, task_vars) + result["ansible_facts"] = { + self._task.args["name"]: { + "my_app_secret_app_secret": os.environ["C2R_TEST_SECRET_BASE64"], + } + } + return result +""", + encoding="utf-8", + ) + playbook = tmp_path / "adopt.yml" + playbook.write_text( + f"""\ +--- +- name: Adopt a matching source tree + hosts: localhost + connection: local + gather_facts: false + vars: + my_app_project_dir: {json.dumps(str(source_tree))} + roles: + - my_app +""", + encoding="utf-8", + ) + configured_bin = os.environ.get("COMPOSE2ROLE_ANSIBLE_BIN") + ansible_playbook = ( + str(Path(configured_bin) / "ansible-playbook") if configured_bin else "ansible-playbook" + ) + validation_environment = { + **os.environ, + "ANSIBLE_COLLECTIONS_PATH": ( + f"{fake_collection_root}:{Path(__file__).parents[1] / 'collections'}" + ), + "ANSIBLE_ROLES_PATH": str(output_directory), + "C2R_TEST_SECRET_BASE64": base64.b64encode(secret_bytes).decode("ascii"), + } + for _ in range(2): + apply_result = subprocess.run( + [ansible_playbook, "--inventory", "localhost,", str(playbook)], + check=False, + capture_output=True, + env=validation_environment, + text=True, + ) + assert apply_result.returncode == 0, apply_result.stdout + apply_result.stderr + assert "changed=0" in apply_result.stdout + assert hashlib.sha256(compose_file.read_bytes()).digest() == original_checksum + assert hashlib.sha256(secret_file.read_bytes()).digest() == original_secret_checksum + + def apply_unsafe_target(project_directory: Path) -> subprocess.CompletedProcess[str]: + unsafe_playbook = tmp_path / f"{project_directory.name}.yml" + unsafe_playbook.write_text( + f"""\ +--- +- name: Refuse an unsafe file-secret target + hosts: localhost + connection: local + gather_facts: false + vars: + my_app_project_dir: {json.dumps(str(project_directory))} + roles: + - my_app +""", + encoding="utf-8", + ) + return subprocess.run( + [ansible_playbook, "--inventory", "localhost,", str(unsafe_playbook)], + check=False, + capture_output=True, + env=validation_environment, + text=True, + ) + + symlink_project = tmp_path / "symlink-target" + symlink_target = symlink_project / "secrets" / "app.bin" + symlink_target.parent.mkdir(parents=True) + outside_target = tmp_path / "outside-secret" + outside_target.write_bytes(b"outside-target") + symlink_target.symlink_to(outside_target) + symlink_result = apply_unsafe_target(symlink_project) + + assert symlink_result.returncode != 0 + assert outside_target.read_bytes() == b"outside-target" + assert base64.b64encode(secret_bytes).decode("ascii") not in ( + symlink_result.stdout + symlink_result.stderr + ) + + directory_project = tmp_path / "directory-target" + directory_target = directory_project / "secrets" / "app.bin" + directory_target.mkdir(parents=True) + directory_result = apply_unsafe_target(directory_project) + + assert directory_result.returncode != 0 + assert not tuple(directory_target.iterdir()) + assert base64.b64encode(secret_bytes).decode("ascii") not in ( + directory_result.stdout + directory_result.stderr + ) + + +def test_generate_preserves_external_compose_prerequisites(tmp_path: Path) -> None: + """External config and secret declarations stay visible to the deployment operator.""" + source_document = """\ +services: + web: + image: ghcr.io/example/web:1.2.3 +configs: + app_config: + external: true +secrets: + db_password: + external: true +""" + canonical_document = { + "configs": {"app_config": {"external": True, "name": "shared_config"}}, + "secrets": {"db_password": {"external": True, "name": "shared_db_password"}}, + "services": { + "web": { + "configs": [{"source": "app_config", "target": "/etc/app/config.yml"}], + "environment": {}, + "healthcheck": {"test": ["CMD", "true"]}, + "image": "ghcr.io/example/web:1.2.3", + "restart": "unless-stopped", + "secrets": [{"source": "db_password", "target": "db_password"}], + "user": "1000:1000", + } + }, + } + environment, sops_config = _remote_environment(tmp_path, source_document, canonical_document) + output_directory = tmp_path / "roles" + + result = CliRunner().invoke( + main, + [ + "generate", + "--host", + "web1", + "--project", + "my-app", + "--output", + str(output_directory), + "--sops-config", + str(sops_config), + ], + env=environment, + ) + + assert result.exit_code == 0, result.output + role_directory = output_directory / "my_app" + compose_template = (role_directory / "templates" / "compose.yaml.j2").read_text( + encoding="utf-8" + ) + assert "app_config:" in compose_template + assert "db_password:" in compose_template + assert "external: true" in compose_template + assert not (role_directory / "README.md").exists() + assert ( + " prerequisites: external config app_config, external secret db_password" + in result.output + ) + + +def test_generate_adopts_dotenv_files_and_preserves_bind_mounts(tmp_path: Path) -> None: + """Adoption templates approved dotenv inputs but never captures bind-mounted data.""" + compose_source = """\ +# Keep this hand-maintained bind declaration. +services: + web: + image: ghcr.io/example/web:1.2.3 + env_file: env/web.env + environment: + - LOG_LEVEL=${LOG_LEVEL} + - API_TOKEN=${APP_VALUE} + volumes: + - ${UPLOAD_LOCATION}:/var/lib/web +networks: + internal: + shared: + external: true +""" + root_dotenv = """\ +# Keep dotenv comments and ordering. +LOG_LEVEL=info +APP_VALUE=fixture-dotenv-secret +UPLOAD_LOCATION=/srv/web-data +UNUSED_TOKEN=unused-secret-value +""" + service_dotenv = """\ +DATABASE_URL=postgres://operator:dotenv-password@db.example/web +""" + canonical_document = { + "networks": {"internal": {}, "shared": {"external": True}}, + "services": { + "web": { + "environment": { + "API_TOKEN": "fixture-dotenv-secret", + "DATABASE_URL": "postgres://operator:dotenv-password@db.example/web", + "LOG_LEVEL": "info", + }, + "healthcheck": {"test": ["CMD", "true"]}, + "image": "ghcr.io/example/web:1.2.3", + "restart": "unless-stopped", + "user": "1000:1000", + "volumes": [ + {"source": "/srv/web-data", "target": "/var/lib/web", "type": "bind"} + ], + } + }, + } + bin_directory = tmp_path / "bin" + bin_directory.mkdir() + _install_stub_command( + bin_directory, + "ssh", + """\ +#!/bin/sh +request=$(cat) + +if [ "$*" = '-- web1 docker compose ls --all --format json' ]; then + printf '%s\\n' '[{"Name":"my-app","Status":"running(1)",' + printf '%s\\n' '"ConfigFiles":"/srv/my-app/docker-compose.yml"}]' + exit 0 +fi + +encoded=$(printf '%s\\n' "$request" | tail -n 2 | head -n 1) +payload=$(printf '%s' "$encoded" | base64 -d) +printf '%s\\n' "$payload" >> "$C2R_PAYLOAD_LOG" +operation=$(printf '%s\\n' "$payload" | head -n 1) +path=$(printf '%s\\n' "$payload" | sed -n '2p') +case "$operation" in + capture) + printf '%s\\n' 'file 640 1000 1001' + case "$path" in + /srv/my-app/docker-compose.yml) printf '%s\\n' "$C2R_COMPOSE_SOURCE" ;; + /srv/my-app/.env) printf '%s\\n' "$C2R_ROOT_DOTENV" ;; + /srv/my-app/env/web.env) printf '%s\\n' "$C2R_SERVICE_DOTENV" ;; + *) exit 98 ;; + esac + ;; + stat) + case "$path" in + /srv/my-app/.env|/srv/my-app/env/web.env) printf '%s\\n' file ;; + *) exit 98 ;; + esac + ;; + config) + printf '%s\\n' "$C2R_CANONICAL_DOCUMENT" + ;; + *) + exit 97 + ;; +esac +""", + ) + _install_stub_command( + bin_directory, + "sops", + """\ +#!/bin/sh +cat > "$C2R_SOPS_PAYLOAD" +cat <<'EOF' +sops: + mac: ENC[AES256_GCM,data:fixture] +my_app_dotenv_env_app_value: ENC[AES256_GCM,data:fixture] +my_app_dotenv_env_log_level: ENC[AES256_GCM,data:fixture] +my_app_dotenv_env_web_env_database_url: ENC[AES256_GCM,data:fixture] +EOF +""", + ) + sops_config = tmp_path / "sops.yaml" + sops_config.write_text( + "creation_rules:\n - path_regex: vars/secrets\\.sops\\.yml$\n", + encoding="utf-8", + ) + output_directory = tmp_path / "roles" + payload_log = tmp_path / "payloads" + sops_payload = tmp_path / "sops-payload.json" + secret_policy_path = tmp_path / "policy.toml" + secret_policy_path.write_text( + """\ +[[dotenv_rules]] +project = "my-app" +source = ".env" +key = "LOG_LEVEL" +decision = "secret" + +[[dotenv_rules]] +project = "my-app" +source = ".env" +key = "UNUSED_TOKEN" +decision = "plain" +""", + encoding="utf-8", + ) + + result = CliRunner().invoke( + main, + [ + "generate", + "--host", + "web1", + "--project", + "my-app", + "--output", + str(output_directory), + "--secret-policy", + str(secret_policy_path), + "--sops-config", + str(sops_config), + ], + env={ + "C2R_CANONICAL_DOCUMENT": json.dumps(canonical_document), + "C2R_COMPOSE_SOURCE": base64.b64encode(compose_source.encode("utf-8")).decode("ascii"), + "C2R_ROOT_DOTENV": base64.b64encode(root_dotenv.encode("utf-8")).decode("ascii"), + "C2R_SERVICE_DOTENV": base64.b64encode(service_dotenv.encode("utf-8")).decode( + "ascii" + ), + "C2R_PAYLOAD_LOG": str(payload_log), + "C2R_SOPS_PAYLOAD": str(sops_payload), + "PATH": f"{bin_directory}:{os.environ['PATH']}", + }, + ) + + assert result.exit_code == 0, result.output + assert json.loads(sops_payload.read_text(encoding="utf-8")) == { + "my_app_dotenv_env_app_value": "fixture-dotenv-secret", + "my_app_dotenv_env_log_level": "info", + "my_app_dotenv_env_web_env_database_url": ( + "postgres://operator:dotenv-password@db.example/web" + ), + } + role_directory = output_directory / "my_app" + assert (role_directory / "templates" / "docker-compose.yml.j2").is_file() + assert (role_directory / "templates" / ".env.j2").is_file() + assert (role_directory / "templates" / "env" / "web.env.j2").is_file() + compose_template = (role_directory / "templates" / "docker-compose.yml.j2").read_text( + encoding="utf-8" + ) + assert "${UPLOAD_LOCATION}:/var/lib/web" in compose_template + assert "internal:" in compose_template + assert "external: true" in compose_template + defaults = (role_directory / "defaults" / "main.yml").read_text(encoding="utf-8") + assert "my_app_dotenv_env_app_value:" not in defaults + assert "my_app_dotenv_env_log_level:" not in defaults + assert 'my_app_dotenv_env_upload_location: "/srv/web-data"' in defaults + assert 'my_app_dotenv_env_unused_token: "unused-secret-value"' in defaults + assert "my_app_dotenv_env_web_env_database_url" in defaults + generated_role_contents = b"".join( + path.read_bytes() for path in role_directory.rglob("*") if path.is_file() + ) + assert b"fixture-dotenv-secret" not in generated_role_contents + assert b"dotenv-password" not in generated_role_contents + payloads = payload_log.read_text(encoding="utf-8") + assert "capture\n/srv/my-app/docker-compose.yml" in payloads + assert "capture\n/srv/my-app/.env" in payloads + assert "capture\n/srv/my-app/env/web.env" in payloads + assert "/srv/web-data" not in payloads + + +def test_generate_adopts_ordered_multifile_source_tree(tmp_path: Path) -> None: + """Ordered Compose files retain their relative layout and render their exact source bytes.""" + primary_source = b"""\ +# Primary Compose source.\r +services:\r + web:\r + image: \"ghcr.io/example/web:1.2.3\"\r + ports: !override\r + - "8080:80"\r + environment: !override\r + LOG_LEVEL: info\r +""" + override_source = b"""\ +# Override source stays separate. +services: + web: + ports: !reset + - "9080:80" + environment: !reset + LOG_LEVEL: debug +""" + canonical_document = { + "services": { + "web": { + "environment": {"LOG_LEVEL": "debug"}, + "healthcheck": {"test": ["CMD", "true"]}, + "image": "ghcr.io/example/web:1.2.3", + "ports": [{"target": 80, "published": "9080", "protocol": "tcp"}], + "restart": "unless-stopped", + "user": "1000:1000", + } + } + } + bin_directory = tmp_path / "bin" + bin_directory.mkdir() + _install_stub_command( + bin_directory, + "ssh", + """\ +#!/bin/sh +request=$(cat) + +if [ "$*" = '-- web1 docker compose ls --all --format json' ]; then + printf '%s\\n' '[{"Name":"my-app","Status":"running(1)",' + printf '%s\\n' '"ConfigFiles":["/srv/my-app/docker-compose.yml",' + printf '%s\\n' '"/srv/my-app/compose/override.yml"]}]' + exit 0 +fi + +encoded=$(printf '%s\\n' "$request" | tail -n 2 | head -n 1) +payload=$(printf '%s' "$encoded" | base64 -d) +printf '%s\\n' "$payload" >> "$C2R_PAYLOAD_LOG" +operation=$(printf '%s\\n' "$payload" | head -n 1) +path=$(printf '%s\\n' "$payload" | sed -n '2p') +case "$operation" in + capture) + printf '%s\\n' 'file 640 1000 1001' + case "$path" in + /srv/my-app/docker-compose.yml) printf '%s\\n' "$C2R_PRIMARY_SOURCE" ;; + /srv/my-app/compose/override.yml) printf '%s\\n' "$C2R_OVERRIDE_SOURCE" ;; + *) exit 98 ;; + esac + ;; + stat) + [ "$path" = /srv/my-app/.env ] || exit 98 + printf '%s\\n' missing + ;; + config) + printf '%s\\n' "$C2R_CANONICAL_DOCUMENT" + ;; + *) exit 97 ;; +esac +""", + ) + _install_stub_command(bin_directory, "sops", "#!/bin/sh\nexit 99\n") + sops_config = tmp_path / "sops.yaml" + sops_config.write_text( + "creation_rules:\n - path_regex: vars/secrets\\.sops\\.yml$\n", + encoding="utf-8", + ) + output_directory = tmp_path / "roles" + payload_log = tmp_path / "payloads" + + result = CliRunner().invoke( + main, + [ + "generate", + "--host", + "web1", + "--project", + "my-app", + "--output", + str(output_directory), + "--sops-config", + str(sops_config), + ], + env={ + "C2R_CANONICAL_DOCUMENT": json.dumps(canonical_document), + "C2R_OVERRIDE_SOURCE": base64.b64encode(override_source).decode("ascii"), + "C2R_PAYLOAD_LOG": str(payload_log), + "C2R_PRIMARY_SOURCE": base64.b64encode(primary_source).decode("ascii"), + "PATH": f"{bin_directory}:{os.environ['PATH']}", + }, + ) + + assert result.exit_code == 0, result.output + role_directory = output_directory / "my_app" + defaults = YAML(typ="safe").load( + (role_directory / "defaults" / "main.yml").read_text(encoding="utf-8") + ) + assert isinstance(defaults, dict) + for relative_path, source, newline_sequence in ( + ("docker-compose.yml", primary_source, "\r\n"), + ("compose/override.yml", override_source, "\n"), + ): + template = ( + role_directory / "templates" / f"{relative_path}.j2" + ).read_bytes().decode("utf-8") + rendered = Environment( + keep_trailing_newline=True, newline_sequence=newline_sequence + ).from_string(template).render(**defaults) + assert rendered.encode("utf-8") == source + tasks = (role_directory / "tasks" / "main.yml").read_text(encoding="utf-8") + assert 'files:\n - "docker-compose.yml"\n - "compose/override.yml"' in tasks + payloads = payload_log.read_text(encoding="utf-8") + assert payloads.index("capture\n/srv/my-app/docker-compose.yml") < payloads.index( + "capture\n/srv/my-app/compose/override.yml" + ) + _assert_generated_role_validates(tmp_path, output_directory, "my_app") + + +@pytest.mark.parametrize( + "source_document", + ( + """\ +services: + web: + image: !override fixture-secret-value +""", + """\ +services: + web: + image: ghcr.io/example/web:1.2.3 + environment: !unsupported + API_TOKEN: fixture-secret-value +""", + ), +) +def test_generate_rejects_unsupported_yaml_tags(tmp_path: Path, source_document: str) -> None: + """Only Compose reset and override collection tags are accepted from source.""" + canonical_document = { + "services": { + "web": { + "environment": {}, + "healthcheck": {"test": ["CMD", "true"]}, + "image": "ghcr.io/example/web:1.2.3", + "restart": "unless-stopped", + "user": "1000:1000", + } + } + } + environment, sops_config = _remote_environment(tmp_path, source_document, canonical_document) + output_directory = tmp_path / "roles" + + result = CliRunner().invoke( + main, + [ + "generate", + "--host", + "web1", + "--project", + "my-app", + "--output", + str(output_directory), + "--sops-config", + str(sops_config), + ], + env=environment, + ) + + assert result.exit_code == 1, result.output + assert "remote Compose source has an unsupported YAML tag" in result.output + assert "fixture-secret-value" not in result.output + assert not output_directory.exists() + + +@pytest.mark.parametrize( + ("primary_environment", "override_environment", "canonical_environment", "plain_value"), + ( + ( + " environment:\n DATABASE_URL: postgres://operator:fixture-source-secret@db/app\n", + " environment:\n DATABASE_URL: postgres://plain@db/app\n", + {"DATABASE_URL": "postgres://plain@db/app"}, + "postgres://plain@db/app", + ), + ( + " environment:\n - DATABASE_URL=postgres://operator:fixture-source-secret@db/app\n", + " environment:\n - DATABASE_URL=postgres://plain@db/app\n", + {"DATABASE_URL": "postgres://plain@db/app"}, + "postgres://plain@db/app", + ), + ( + " environment:\n DATABASE_URL: postgres://operator:fixture-source-secret@db/app\n", + " environment: !reset []\n", + {}, + None, + ), + ), + ids=("mapping-override", "list-override", "reset-removed"), +) +def test_generate_classifies_each_multifile_environment_occurrence( + tmp_path: Path, + primary_environment: str, + override_environment: str, + canonical_environment: dict[str, str], + plain_value: str | None, +) -> None: + """A source-only secret never becomes a plaintext default after an override.""" + secret_value = "postgres://operator:fixture-source-secret@db/app" + source_identity = ( + "compose:compose.yaml:web:environment-list:0:DATABASE_URL" + if "- DATABASE_URL" in primary_environment + else "compose:compose.yaml:web:environment:DATABASE_URL" + ) + secret_key = "my_app_web_database_url" + if plain_value is not None: + source_digest = hashlib.sha256(source_identity.encode()).hexdigest()[:8] + secret_key = f"{secret_key}_{source_digest}" + primary_source = ( + "services:\n" + " web:\n" + " image: ghcr.io/example/web:1.2.3\n" + f"{primary_environment}" + ).encode() + override_source = ( + "services:\n" + " web:\n" + f"{override_environment}" + ).encode() + canonical_document = { + "services": { + "web": { + "environment": canonical_environment, + "healthcheck": {"test": ["CMD", "true"]}, + "image": "ghcr.io/example/web:1.2.3", + "restart": "unless-stopped", + "user": "1000:1000", + } + } + } + bin_directory = tmp_path / "bin" + bin_directory.mkdir() + _install_stub_command( + bin_directory, + "ssh", + """\ +#!/bin/sh +request=$(cat) + +if [ "$*" = '-- web1 docker compose ls --all --format json' ]; then + printf '%s\\n' '[{"Name":"my-app","Status":"running(1)",' + printf '%s\\n' '"ConfigFiles":["/srv/my-app/compose.yaml",' + printf '%s\\n' '"/srv/my-app/compose.override.yaml"]}]' + exit 0 +fi + +encoded=$(printf '%s\\n' "$request" | tail -n 2 | head -n 1) +payload=$(printf '%s' "$encoded" | base64 -d) +operation=$(printf '%s\\n' "$payload" | head -n 1) +path=$(printf '%s\\n' "$payload" | sed -n '2p') +case "$operation" in + capture) + printf '%s\\n' 'file 640 1000 1001' + case "$path" in + /srv/my-app/compose.yaml) printf '%s\\n' "$C2R_PRIMARY_SOURCE" ;; + /srv/my-app/compose.override.yaml) printf '%s\\n' "$C2R_OVERRIDE_SOURCE" ;; + *) exit 98 ;; + esac + ;; + stat) + [ "$path" = /srv/my-app/.env ] || exit 98 + printf '%s\\n' missing + ;; + config) + printf '%s\\n' "$C2R_CANONICAL_DOCUMENT" + ;; + *) exit 97 ;; +esac +""", + ) + _install_stub_command( + bin_directory, + "sops", + "#!/bin/sh\ncat >/dev/null\nprintf '%s\\n' \"$C2R_SOPS_OUTPUT\"\n", + ) + sops_config = tmp_path / "sops.yaml" + sops_config.write_text( + "creation_rules:\n - path_regex: vars/secrets\\.sops\\.yml$\n", + encoding="utf-8", + ) + output_directory = tmp_path / "roles" + + result = CliRunner().invoke( + main, + [ + "generate", + "--host", + "web1", + "--project", + "my-app", + "--output", + str(output_directory), + "--sops-config", + str(sops_config), + ], + env={ + "C2R_CANONICAL_DOCUMENT": json.dumps(canonical_document), + "C2R_OVERRIDE_SOURCE": base64.b64encode(override_source).decode("ascii"), + "C2R_PRIMARY_SOURCE": base64.b64encode(primary_source).decode("ascii"), + "C2R_SOPS_OUTPUT": _encrypted_sops_output(secret_key), + "PATH": f"{bin_directory}:{os.environ['PATH']}", + }, + ) + + assert result.exit_code == 0, result.output + role_directory = output_directory / "my_app" + generated_role_contents = b"".join( + path.read_bytes() for path in role_directory.rglob("*") if path.is_file() + ) + assert secret_value.encode("utf-8") not in generated_role_contents + primary_template = (role_directory / "templates" / "compose.yaml.j2").read_text( + encoding="utf-8" + ) + assert "my_app_secrets[" in primary_template + defaults = (role_directory / "defaults" / "main.yml").read_text(encoding="utf-8") + if plain_value is not None: + assert plain_value in defaults + + +def test_generate_refuses_a_source_only_service_with_a_secret_environment(tmp_path: Path) -> None: + """A service removed from the canonical model cannot leave a source secret literal behind.""" + secret_value = "postgres://operator:source-only-secret@db/app" + source_document = f"""\ +services: + stale: + image: ghcr.io/example/stale:1.2.3 + environment: + DATABASE_URL: {secret_value} +""" + canonical_document = { + "services": { + "web": { + "environment": {}, + "healthcheck": {"test": ["CMD", "true"]}, + "image": "ghcr.io/example/web:1.2.3", + "restart": "unless-stopped", + "user": "1000:1000", + } + } + } + environment, sops_config = _remote_environment(tmp_path, source_document, canonical_document) + output_directory = tmp_path / "roles" + + result = CliRunner().invoke( + main, + [ + "generate", + "--host", + "web1", + "--project", + "my-app", + "--output", + str(output_directory), + "--sops-config", + str(sops_config), + ], + env=environment, + ) + + assert result.exit_code == 1, result.output + assert "remote Compose source has an unsupported secret environment" in result.output + assert secret_value not in result.output + assert not output_directory.exists() + + +def test_generate_refuses_an_unused_extension_anchor_with_a_secret_environment( + tmp_path: Path, +) -> None: + """A captured extension anchor cannot preserve a secret the role cannot parameterize.""" + secret_value = "postgres://operator:unused-anchor-secret@db/app" + source_document = f"""\ +x-common: + environment: + DATABASE_URL: {secret_value} +services: + web: + image: ghcr.io/example/web:1.2.3 +""" + canonical_document = { + "services": { + "web": { + "environment": {}, + "healthcheck": {"test": ["CMD", "true"]}, + "image": "ghcr.io/example/web:1.2.3", + "restart": "unless-stopped", + "user": "1000:1000", + } + } + } + environment, sops_config = _remote_environment(tmp_path, source_document, canonical_document) + output_directory = tmp_path / "roles" + + result = CliRunner().invoke( + main, + [ + "generate", + "--host", + "web1", + "--project", + "my-app", + "--output", + str(output_directory), + "--sops-config", + str(sops_config), + ], + env=environment, + ) + + assert result.exit_code == 1, result.output + assert "remote Compose source has an unsupported secret environment" in result.output + assert secret_value not in result.output + assert not output_directory.exists() + + +def test_generate_refuses_file_backed_remote_configs(tmp_path: Path) -> None: + """A role does not claim to carry a remote file-backed Compose config.""" + source_document = """\ +services: + web: + image: ghcr.io/example/web:1.2.3 +configs: + app_config: + file: ./app.conf +""" + canonical_document = { + "services": { + "web": { + "environment": {}, + "healthcheck": {"test": ["CMD", "true"]}, + "image": "ghcr.io/example/web:1.2.3", + "restart": "unless-stopped", + "user": "1000:1000", + } + } + } + environment, sops_config = _remote_environment(tmp_path, source_document, canonical_document) + output_directory = tmp_path / "roles" + + result = CliRunner().invoke( + main, + [ + "generate", + "--host", + "web1", + "--project", + "my-app", + "--output", + str(output_directory), + "--sops-config", + str(sops_config), + ], + env=environment, + ) + + assert result.exit_code == 1, result.output + assert "C2R027: remote file-backed Compose configs are not supported" in result.output + assert not output_directory.exists() + + +def test_generate_imports_binary_file_backed_compose_secrets(tmp_path: Path) -> None: + """A contained file secret is encrypted and restored at its original path and metadata.""" + source_document = """\ +services: + web: + image: ghcr.io/example/web:1.2.3 +secrets: + app_secret: + file: ./secrets/app.bin +""" + secret_bytes = b"\x00binary\xffsecret\n" + canonical_document = { + "services": { + "web": { + "environment": {}, + "healthcheck": {"test": ["CMD", "true"]}, + "image": "ghcr.io/example/web:1.2.3", + "restart": "unless-stopped", + "user": "1000:1000", + } + } + } + bin_directory = tmp_path / "bin" + bin_directory.mkdir() + _install_stub_command( + bin_directory, + "ssh", + """\ +#!/bin/sh +request=$(cat) + +if [ "$*" = '-- web1 docker compose ls --all --format json' ]; then + printf '%s\\n' '[{"Name":"my-app","Status":"running(1)",' + printf '%s\\n' '"ConfigFiles":"/srv/my-app/docker-compose.yml"}]' + exit 0 +fi + +encoded=$(printf '%s\\n' "$request" | tail -n 2 | head -n 1) +payload=$(printf '%s' "$encoded" | base64 -d) +operation=$(printf '%s\\n' "$payload" | head -n 1) +path=$(printf '%s\\n' "$payload" | sed -n '2p') +case "$operation" in + capture) + case "$path" in + /srv/my-app/docker-compose.yml) + printf '%s\\n' 'file 640 1000 1001' + printf '%s\\n' "$C2R_COMPOSE_SOURCE" + ;; + /srv/my-app/secrets/app.bin) + printf '%s\\n' 'file 600 1002 1003' + printf '%s\\n' "$C2R_SECRET_SOURCE" + ;; + *) exit 98 ;; + esac + ;; + stat) + case "$path" in + /srv/my-app/.env) printf '%s\\n' missing ;; + /srv/my-app/secrets/app.bin) printf '%s\\n' file ;; + *) exit 98 ;; + esac + ;; + config) + printf '%s\\n' "$C2R_CANONICAL_DOCUMENT" + ;; + *) exit 97 ;; +esac +""", + ) + _install_stub_command( + bin_directory, + "sops", + """\ +#!/bin/sh +payload=$(cat) +case "$payload" in + *"$C2R_EXPECTED_BASE64"*) ;; + *) exit 98 ;; +esac +cat <<'EOF' +sops: + mac: ENC[AES256_GCM,data:fixture] +my_app_secret_app_secret: ENC[AES256_GCM,data:fixture] +EOF +""", + ) + sops_config = tmp_path / "sops.yaml" + sops_config.write_text( + "creation_rules:\n - path_regex: vars/secrets\\.sops\\.yml$\n", + encoding="utf-8", + ) + output_directory = tmp_path / "roles" + + result = CliRunner().invoke( + main, + [ + "generate", + "--host", + "web1", + "--project", + "my-app", + "--output", + str(output_directory), + "--sops-config", + str(sops_config), + ], + env={ + "C2R_CANONICAL_DOCUMENT": json.dumps(canonical_document), + "C2R_COMPOSE_SOURCE": base64.b64encode(source_document.encode("utf-8")).decode( + "ascii" + ), + "C2R_EXPECTED_BASE64": base64.b64encode(secret_bytes).decode("ascii"), + "C2R_SECRET_SOURCE": base64.b64encode(secret_bytes).decode("ascii"), + "PATH": f"{bin_directory}:{os.environ['PATH']}", + }, + ) + + assert result.exit_code == 0, result.output + role_directory = output_directory / "my_app" + tasks = (role_directory / "tasks" / "main.yml").read_text(encoding="utf-8") + assert "my_app_secret_app_secret" in tasks + assert "secrets/app.bin" in tasks + assert 'mode: "0600"' in tasks + assert "base64 --decode" in tasks + assert "file: ./secrets/app.bin" in ( + role_directory / "templates" / "docker-compose.yml.j2" + ).read_text(encoding="utf-8") + generated_role_contents = b"".join( + path.read_bytes() for path in role_directory.rglob("*") if path.is_file() + ) + assert secret_bytes not in generated_role_contents + assert base64.b64encode(secret_bytes) not in generated_role_contents + _assert_generated_role_validates(tmp_path, output_directory, "my_app") + + +def test_generate_accepts_empty_secrets_left_unencrypted_by_sops(tmp_path: Path) -> None: + """SOPS leaves empty values plaintext; an empty secret carries nothing to protect.""" + source_document = """\ +services: + web: + image: ghcr.io/example/web:1.2.3 + environment: + API_TOKEN: "" +secrets: + app_secret: + file: ./secrets/empty.bin +""" + canonical_document = { + "services": { + "web": { + "environment": {"API_TOKEN": ""}, + "healthcheck": {"test": ["CMD", "true"]}, + "image": "ghcr.io/example/web:1.2.3", + "restart": "unless-stopped", + "user": "1000:1000", + } + } + } + bin_directory = tmp_path / "bin" + bin_directory.mkdir() + _install_stub_command( + bin_directory, + "ssh", + """\ +#!/bin/sh +request=$(cat) + +if [ "$*" = '-- web1 docker compose ls --all --format json' ]; then + printf '%s\\n' '[{"Name":"my-app","Status":"running(1)",' + printf '%s\\n' '"ConfigFiles":"/srv/my-app/docker-compose.yml"}]' + exit 0 +fi + +encoded=$(printf '%s\\n' "$request" | tail -n 2 | head -n 1) +payload=$(printf '%s' "$encoded" | base64 -d) +operation=$(printf '%s\\n' "$payload" | head -n 1) +path=$(printf '%s\\n' "$payload" | sed -n '2p') +case "$operation" in + capture) + case "$path" in + /srv/my-app/docker-compose.yml) + printf '%s\\n' 'file 640 1000 1001' + printf '%s\\n' "$C2R_COMPOSE_SOURCE" + ;; + /srv/my-app/secrets/empty.bin) + printf '%s\\n' 'file 600 1002 1003' + printf '\\n' + ;; + *) exit 98 ;; + esac + ;; + stat) + case "$path" in + /srv/my-app/.env) printf '%s\\n' missing ;; + /srv/my-app/secrets/empty.bin) printf '%s\\n' file ;; + *) exit 98 ;; + esac + ;; + config) + printf '%s\\n' "$C2R_CANONICAL_DOCUMENT" + ;; + *) exit 97 ;; +esac +""", + ) + _install_stub_command( + bin_directory, + "sops", + """\ +#!/bin/sh +cat >/dev/null +cat <<'EOF' +sops: + mac: ENC[AES256_GCM,data:fixture] +my_app_secret_app_secret: "" +my_app_web_api_token: "" +EOF +""", + ) + sops_config = tmp_path / "sops.yaml" + sops_config.write_text( + "creation_rules:\n - path_regex: vars/secrets\\.sops\\.yml$\n", + encoding="utf-8", + ) + output_directory = tmp_path / "roles" + + result = CliRunner().invoke( + main, + [ + "generate", + "--host", + "web1", + "--project", + "my-app", + "--output", + str(output_directory), + "--sops-config", + str(sops_config), + ], + env={ + "C2R_CANONICAL_DOCUMENT": json.dumps(canonical_document), + "C2R_COMPOSE_SOURCE": base64.b64encode(source_document.encode("utf-8")).decode( + "ascii" + ), + "PATH": f"{bin_directory}:{os.environ['PATH']}", + }, + ) + + assert result.exit_code == 0, result.output + role_directory = output_directory / "my_app" + secrets = (role_directory / "vars" / "secrets.sops.yml").read_text(encoding="utf-8") + assert 'my_app_secret_app_secret: ""' in secrets + assert 'my_app_web_api_token: ""' in secrets + tasks = (role_directory / "tasks" / "main.yml").read_text(encoding="utf-8") + assert "my_app_secret_app_secret" in tasks + assert "secrets/empty.bin" in tasks + assert "base64 --decode" in tasks + + +def test_generate_accepts_short_secrets_that_appear_inside_ciphertext(tmp_path: Path) -> None: + """A plaintext value can coincide with ciphertext characters without a leak.""" + secret_value = "x" + source_document = f"""\ +services: + web: + image: ghcr.io/example/web:1.2.3 + environment: + API_TOKEN: {secret_value} +""" + canonical_document = { + "services": { + "web": { + "environment": {"API_TOKEN": secret_value}, + "healthcheck": {"test": ["CMD", "true"]}, + "image": "ghcr.io/example/web:1.2.3", + "restart": "unless-stopped", + "user": "1000:1000", + } + } + } + environment, sops_config = _remote_environment(tmp_path, source_document, canonical_document) + bin_directory = Path(environment["PATH"].split(":", maxsplit=1)[0]) + _install_stub_command( + bin_directory, + "sops", + """\ +#!/bin/sh +cat >/dev/null +cat <<'EOF' +my_app_web_api_token: ENC[AES256_GCM,data:x==,iv:x==,tag:x==,type:str] +sops: + mac: ENC[AES256_GCM,data:x==] +EOF +""", + ) + output_directory = tmp_path / "roles" + + result = CliRunner().invoke( + main, + [ + "generate", + "--host", + "web1", + "--project", + "my-app", + "--output", + str(output_directory), + "--sops-config", + str(sops_config), + ], + env=environment, + ) + + assert result.exit_code == 0, result.output + secrets = (output_directory / "my_app" / "vars" / "secrets.sops.yml").read_text( + encoding="utf-8" + ) + assert "my_app_web_api_token: ENC[" in secrets + + +def test_generate_refuses_escaping_file_backed_compose_secret_paths(tmp_path: Path) -> None: + """A secret input outside the project root fails without naming or copying it.""" + canonical_document = { + "services": { + "web": { + "environment": {}, + "healthcheck": {"test": ["CMD", "true"]}, + "image": "ghcr.io/example/web:1.2.3", + "restart": "unless-stopped", + "user": "1000:1000", + } + } + } + for name, reference in ( + ("parent", "../outside.txt"), + ("absolute", "/etc/passwd"), + ): + case_directory = tmp_path / name + case_directory.mkdir() + source_document = f"""\ +services: + web: + image: ghcr.io/example/web:1.2.3 +secrets: + app_secret: + file: {reference} +""" + environment, sops_config = _remote_environment( + case_directory, source_document, canonical_document + ) + output_directory = case_directory / "roles" + + result = CliRunner().invoke( + main, + [ + "generate", + "--host", + "web1", + "--project", + "my-app", + "--output", + str(output_directory), + "--sops-config", + str(sops_config), + ], + env=environment, + ) + + assert result.exit_code == 1, result.output + assert "remote file-backed Compose secret path escapes the project root" in result.output + assert reference not in result.output + assert not output_directory.exists() + + +def test_generate_disambiguates_colliding_file_secret_sops_keys(tmp_path: Path) -> None: + """Colliding normalized Compose secret names receive stable distinct SOPS keys.""" + source_document = """\ +services: + web: + image: ghcr.io/example/web:1.2.3 +secrets: + app-token: + file: ./secrets/first.bin + app_token: + file: ./secrets/second.bin +""" + first_secret = b"\x00first-secret\n" + second_secret = b"\xffsecond-secret\n" + canonical_document = { + "services": { + "web": { + "environment": {}, + "healthcheck": {"test": ["CMD", "true"]}, + "image": "ghcr.io/example/web:1.2.3", + "restart": "unless-stopped", + "user": "1000:1000", + } + } + } + bin_directory = tmp_path / "bin" + bin_directory.mkdir() + _install_stub_command( + bin_directory, + "ssh", + """\ +#!/bin/sh +request=$(cat) + +if [ "$*" = '-- web1 docker compose ls --all --format json' ]; then + printf '%s\\n' '[{"Name":"my-app","Status":"running(1)",' + printf '%s\\n' '"ConfigFiles":"/srv/my-app/compose.yaml"}]' + exit 0 +fi + +encoded=$(printf '%s\\n' "$request" | tail -n 2 | head -n 1) +payload=$(printf '%s' "$encoded" | base64 -d) +operation=$(printf '%s\\n' "$payload" | head -n 1) +path=$(printf '%s\\n' "$payload" | sed -n '2p') +case "$operation" in + capture) + case "$path" in + /srv/my-app/compose.yaml) + printf '%s\\n' 'file 640 0 0' + printf '%s\\n' "$C2R_COMPOSE_SOURCE" + ;; + /srv/my-app/secrets/first.bin) + printf '%s\\n' 'file 600 0 0' + printf '%s\\n' "$C2R_FIRST_SECRET_SOURCE" + ;; + /srv/my-app/secrets/second.bin) + printf '%s\\n' 'file 600 0 0' + printf '%s\\n' "$C2R_SECOND_SECRET_SOURCE" + ;; + *) exit 98 ;; + esac + ;; + stat) + case "$path" in + /srv/my-app/.env) printf '%s\\n' missing ;; + /srv/my-app/secrets/first.bin|/srv/my-app/secrets/second.bin) printf '%s\\n' file ;; + *) exit 98 ;; + esac + ;; + config) + printf '%s\\n' "$C2R_CANONICAL_DOCUMENT" + ;; + *) exit 97 ;; +esac +""", + ) + _install_stub_command( + bin_directory, + "sops", + """\ +#!/bin/sh +cat > "$C2R_SOPS_PAYLOAD" +printf '%s\\n' "$C2R_SOPS_OUTPUT" +""", + ) + sops_config = tmp_path / "sops.yaml" + sops_config.write_text( + "creation_rules:\n - path_regex: vars/secrets\\.sops\\.yml$\n", + encoding="utf-8", + ) + first_payload_path = tmp_path / "first-sops-payload.json" + environment = { + "C2R_CANONICAL_DOCUMENT": json.dumps(canonical_document), + "C2R_COMPOSE_SOURCE": base64.b64encode(source_document.encode("utf-8")).decode("ascii"), + "C2R_FIRST_SECRET_SOURCE": base64.b64encode(first_secret).decode("ascii"), + "C2R_SECOND_SECRET_SOURCE": base64.b64encode(second_secret).decode("ascii"), + "C2R_SOPS_OUTPUT": _encrypted_sops_output( + *( + "my_app_secret_app_token_" + + hashlib.sha256(identity.encode("utf-8")).hexdigest()[:8] + for identity in ( + "file-secret:app-token:secrets/first.bin", + "file-secret:app_token:secrets/second.bin", + ) + ) + ), + "C2R_SOPS_PAYLOAD": str(first_payload_path), + "PATH": f"{bin_directory}:{os.environ['PATH']}", + } + arguments = [ + "generate", + "--host", + "web1", + "--project", + "my-app", + "--sops-config", + str(sops_config), + ] + output_directory = tmp_path / "roles" + + first_result = CliRunner().invoke( + main, + [*arguments, "--output", str(output_directory)], + env=environment, + ) + + assert first_result.exit_code == 0, first_result.output + second_payload_path = tmp_path / "second-sops-payload.json" + second_result = CliRunner().invoke( + main, + [*arguments, "--output", str(tmp_path / "roles-again")], + env={**environment, "C2R_SOPS_PAYLOAD": str(second_payload_path)}, + ) + + assert second_result.exit_code == 0, second_result.output + first_payload = json.loads(first_payload_path.read_text(encoding="utf-8")) + assert first_payload == json.loads(second_payload_path.read_text(encoding="utf-8")) + assert set(first_payload.values()) == { + base64.b64encode(first_secret).decode("ascii"), + base64.b64encode(second_secret).decode("ascii"), + } + keys = set(first_payload) + prefix = "my_app_secret_app_token_" + assert len(keys) == 2 + assert all( + key.startswith(prefix) + and len(key) == len(prefix) + 8 + and all(character in "0123456789abcdef" for character in key[len(prefix) :]) + for key in keys + ) + tasks = (output_directory / "my_app" / "tasks" / "main.yml").read_text(encoding="utf-8") + assert all(f'key: "{key}"' in tasks for key in keys) + generated_role_contents = b"".join( + path.read_bytes() for path in (output_directory / "my_app").rglob("*") if path.is_file() + ) + assert first_secret not in generated_role_contents + assert second_secret not in generated_role_contents + assert base64.b64encode(first_secret) not in generated_role_contents + assert base64.b64encode(second_secret) not in generated_role_contents + + +def test_generate_refuses_secret_shaped_non_environment_keys(tmp_path: Path) -> None: + """A secret-shaped label is refused rather than rendered as an unprotected value.""" + secret_value = "do-not-render-this-label-value" + source_document = f"""\ +services: + web: + image: ghcr.io/example/web:1.2.3 + labels: + API_TOKEN: {secret_value} +""" + canonical_document = { + "services": { + "web": { + "environment": {}, + "healthcheck": {"test": ["CMD", "true"]}, + "image": "ghcr.io/example/web:1.2.3", + "labels": {"API_TOKEN": secret_value}, + "restart": "unless-stopped", + "user": "1000:1000", + } + } + } + environment, sops_config = _remote_environment(tmp_path, source_document, canonical_document) + output_directory = tmp_path / "roles" + + result = CliRunner().invoke( + main, + [ + "generate", + "--host", + "web1", + "--project", + "my-app", + "--output", + str(output_directory), + "--sops-config", + str(sops_config), + ], + env=environment, + ) + + assert result.exit_code == 1, result.output + assert ( + "C2R029: remote non-environment secret-shaped mapping keys are not supported" + in result.output + ) + assert secret_value not in result.output + assert not output_directory.exists() + + +def test_generate_preserves_canonical_no_file_topology(tmp_path: Path) -> None: + """Ordinary source topology survives source-faithful remote role generation.""" + source_document = """\ +networks: + backplane: + name: my-app_backplane +services: + db: + image: "postgres:16" + healthcheck: + test: ["CMD-SHELL", "pg_isready"] + networks: + - backplane + restart: unless-stopped + user: "1000:1000" + volumes: + - type: volume + source: db_data + target: /var/lib/postgresql/data + web: + image: "ghcr.io/example/web:1.2.3" + command: ["serve", "--mode", "production"] + depends_on: + db: + condition: service_healthy + healthcheck: + test: ["CMD", "true"] + labels: + org.example.component: web + networks: + - backplane + restart: unless-stopped + user: "1000:1000" +volumes: + db_data: + name: my-app_db_data +""" + canonical_document = { + "networks": {"backplane": {"name": "my-app_backplane"}}, + "services": { + "db": { + "environment": {}, + "healthcheck": {"test": ["CMD-SHELL", "pg_isready"]}, + "image": "postgres:16", + "networks": ["backplane"], + "restart": "unless-stopped", + "user": "1000:1000", + "volumes": [ + {"source": "db_data", "target": "/var/lib/postgresql/data", "type": "volume"} + ], + }, + "web": { + "command": ["serve", "--mode", "production"], + "depends_on": {"db": {"condition": "service_healthy"}}, + "environment": {}, + "healthcheck": {"test": ["CMD", "true"]}, + "image": "ghcr.io/example/web:1.2.3", + "labels": {"org.example.component": "web"}, + "networks": ["backplane"], + "restart": "unless-stopped", + "user": "1000:1000", + }, + }, + "volumes": {"db_data": {"name": "my-app_db_data"}}, + } + environment, sops_config = _remote_environment(tmp_path, source_document, canonical_document) + output_directory = tmp_path / "roles" + + result = CliRunner().invoke( + main, + [ + "generate", + "--host", + "web1", + "--project", + "my-app", + "--output", + str(output_directory), + "--sops-config", + str(sops_config), + ], + env=environment, + ) + + assert result.exit_code == 0, result.output + template = YAML(typ="safe").load( + (output_directory / "my_app" / "templates" / "compose.yaml.j2").read_text(encoding="utf-8") + ) + assert template["networks"] == canonical_document["networks"] + assert template["volumes"] == canonical_document["volumes"] + assert template["services"]["web"]["command"] == ["serve", "--mode", "production"] + assert template["services"]["web"]["depends_on"] == { + "db": {"condition": "service_healthy"} + } + assert template["services"]["web"]["labels"] == {"org.example.component": "web"} + assert template["services"]["db"]["healthcheck"] == { + "test": ["CMD-SHELL", "pg_isready"] + } + + +def test_generate_encrypts_remote_environment_secrets_without_plaintext_files( + tmp_path: Path, +) -> None: + """Classified remote environment values reach SOPS only over stdin.""" + secret_value = "remote-secret-must-not-persist" + source_document = f"""\ +services: + web: + image: ghcr.io/example/web:1.2.3 + environment: + API_TOKEN: {secret_value} + LOG_LEVEL: info +""" + canonical_document = { + "services": { + "web": { + "environment": {"API_TOKEN": secret_value, "LOG_LEVEL": "info"}, + "healthcheck": {"test": ["CMD", "true"]}, + "image": "ghcr.io/example/web:1.2.3", + "restart": "unless-stopped", + "user": "1000:1000", + } + } + } + environment, sops_config = _remote_environment(tmp_path, source_document, canonical_document) + bin_directory = Path(environment["PATH"].split(":", maxsplit=1)[0]) + sops_marker = tmp_path / "sops-marker" + sops_arguments = tmp_path / "sops-arguments" + _install_stub_command( + bin_directory, + "sops", + """\ +#!/bin/sh +payload=$(cat) +case "$payload" in + *"$C2R_EXPECTED_SECRET"*) + ;; + *) + exit 98 + ;; +esac +case "$*" in + *"$C2R_EXPECTED_SECRET"*) + exit 97 + ;; +esac +printf '%s\\n' "$@" > "$C2R_SOPS_ARGUMENTS" +printf '%s\\n' stdin-secret-received >> "$C2R_SOPS_MARKER" +cat <<'EOF' +sops: + mac: ENC[AES256_GCM,data:fixture] +my_app_web_api_token: ENC[AES256_GCM,data:fixture] +EOF +""", + ) + output_directory = tmp_path / "roles" + + result = CliRunner().invoke( + main, + [ + "generate", + "--host", + "web1", + "--project", + "my-app", + "--output", + str(output_directory), + "--sops-config", + str(sops_config), + ], + env={ + **environment, + "C2R_EXPECTED_SECRET": secret_value, + "C2R_SOPS_ARGUMENTS": str(sops_arguments), + "C2R_SOPS_MARKER": str(sops_marker), + }, + ) + + assert result.exit_code == 0, result.output + role_directory = output_directory / "my_app" + assert sops_marker.read_text(encoding="utf-8") == "stdin-secret-received\n" + assert secret_value not in sops_arguments.read_text(encoding="utf-8") + assert "--filename-override" in sops_arguments.read_text(encoding="utf-8") + encrypted_secrets = (role_directory / "vars" / "secrets.sops.yml").read_text( + encoding="utf-8" + ) + assert "ENC[AES256_GCM,data:fixture]" in encrypted_secrets + assert secret_value not in encrypted_secrets + assert not (role_directory / ".sops.yaml").exists() + assert not (role_directory / "vars" / "secrets.example.yml").exists() + assert 'my_app_secrets_file: "{{ role_path }}/vars/secrets.sops.yml"' in ( + role_directory / "defaults" / "main.yml" + ).read_text(encoding="utf-8") + assert "community.sops.load_vars:" in (role_directory / "tasks" / "main.yml").read_text( + encoding="utf-8" + ) + assert "community.sops" in (role_directory / "meta" / "requirements.yml").read_text( + encoding="utf-8" + ) + compose_template = (role_directory / "templates" / "compose.yaml.j2").read_text( + encoding="utf-8" + ) + assert "API_TOKEN: {{ my_app_secrets['my_app_web_api_token'] }}" in compose_template + assert "LOG_LEVEL: {{ my_app_web_log_level }}" in compose_template + for path in tmp_path.rglob("*"): + if path.is_file(): + assert secret_value.encode("utf-8") not in path.read_bytes() + assert secret_value not in result.output + + +def test_generated_remote_secret_role_passes_ansible_validation(tmp_path: Path) -> None: + """The role wiring for encrypted remote environment values is lint- and syntax-valid.""" + secret_value = "ansible-validation-secret" + source_document = f"""\ +services: + web: + image: ghcr.io/example/web:1.2.3 + environment: + API_TOKEN: {secret_value} +""" + canonical_document = { + "services": { + "web": { + "environment": {"API_TOKEN": secret_value}, + "healthcheck": {"test": ["CMD", "true"]}, + "image": "ghcr.io/example/web:1.2.3", + "restart": "unless-stopped", + "user": "1000:1000", + } + } + } + environment, sops_config = _remote_environment(tmp_path, source_document, canonical_document) + bin_directory = Path(environment["PATH"].split(":", maxsplit=1)[0]) + _install_stub_command( + bin_directory, + "sops", + """\ +#!/bin/sh +cat >/dev/null +cat <<'EOF' +sops: + mac: "ENC[AES256_GCM,data:fixture]" +my_app_web_api_token: "ENC[AES256_GCM,data:fixture]" +EOF +""", + ) + output_directory = tmp_path / "roles" + + result = CliRunner().invoke( + main, + [ + "generate", + "--host", + "web1", + "--project", + "my-app", + "--output", + str(output_directory), + "--sops-config", + str(sops_config), + ], + env=environment, + ) + + assert result.exit_code == 0, result.output + _assert_generated_role_validates(tmp_path, output_directory, "my_app") + + +def test_generate_writes_redacted_remote_provenance(tmp_path: Path) -> None: + """Remote role provenance identifies structure without retaining environment values.""" + secret_value = "provenance-must-not-contain-this" + source_document = f"""\ +services: + web: + image: ghcr.io/example/web:1.2.3 + environment: + API_TOKEN: {secret_value} +""" + canonical_document = { + "services": { + "web": { + "environment": {"API_TOKEN": secret_value}, + "healthcheck": {"test": ["CMD", "true"]}, + "image": "ghcr.io/example/web:1.2.3", + "restart": "unless-stopped", + "user": "1000:1000", + } + } + } + environment, sops_config = _remote_environment(tmp_path, source_document, canonical_document) + bin_directory = Path(environment["PATH"].split(":", maxsplit=1)[0]) + _install_stub_command( + bin_directory, + "sops", + "#!/bin/sh\ncat >/dev/null\nprintf '%s\\n' \"$C2R_SOPS_OUTPUT\"\n", + ) + output_directory = tmp_path / "roles" + + result = CliRunner().invoke( + main, + [ + "generate", + "--host", + "web1", + "--project", + "my-app", + "--output", + str(output_directory), + "--sops-config", + str(sops_config), + ], + env={ + **environment, + "C2R_SOPS_OUTPUT": _encrypted_sops_output("my_app_web_api_token"), + }, + ) + + assert result.exit_code == 0, result.output + provenance_text = (output_directory / "my_app" / ".compose2role.json").read_text( + encoding="utf-8" + ) + provenance = json.loads(provenance_text) + assert provenance["generator"] == "compose2role" + assert provenance["mode"] == "remote" + assert provenance["project"] == "my-app" + assert provenance["role"] == "my_app" + assert provenance["source"] == { + "config_files": ["/srv/my-app/compose.yaml"], + "root": "/srv/my-app", + } + assert len(provenance["structural_hash"]) == 64 + assert secret_value not in provenance_text + + +def test_remote_provenance_hash_is_stable_when_only_environment_values_change( + tmp_path: Path, +) -> None: + """Effective environment values do not influence the structural provenance hash.""" + hashes: list[str] = [] + for index, log_level in enumerate(("info", "debug"), start=1): + case_directory = tmp_path / str(index) + case_directory.mkdir() + source_document = f"""\ +services: + web: + image: ghcr.io/example/web:1.2.3 + environment: + LOG_LEVEL: {log_level} +""" + canonical_document = { + "services": { + "web": { + "environment": {"LOG_LEVEL": log_level}, + "healthcheck": {"test": ["CMD", "true"]}, + "image": "ghcr.io/example/web:1.2.3", + "restart": "unless-stopped", + "user": "1000:1000", + } + } + } + environment, sops_config = _remote_environment( + case_directory, source_document, canonical_document + ) + output_directory = case_directory / "roles" + + result = CliRunner().invoke( + main, + [ + "generate", + "--host", + "web1", + "--project", + "my-app", + "--output", + str(output_directory), + "--sops-config", + str(sops_config), + ], + env=environment, + ) + + assert result.exit_code == 0, result.output + provenance = json.loads( + (output_directory / "my_app" / ".compose2role.json").read_text(encoding="utf-8") + ) + hashes.append(provenance["structural_hash"]) + + assert hashes[0] == hashes[1] + + +def test_generate_check_validates_remote_secrets_without_writing_or_encrypting( + tmp_path: Path, +) -> None: + """Remote check mode reaches validation but leaves no role or SOPS artifact behind.""" + secret_value = "check-mode-secret" + source_document = f"""\ +services: + web: + image: ghcr.io/example/web:1.2.3 + environment: + API_TOKEN: {secret_value} +""" + canonical_document = { + "services": { + "web": { + "environment": {"API_TOKEN": secret_value}, + "healthcheck": {"test": ["CMD", "true"]}, + "image": "ghcr.io/example/web:1.2.3", + "restart": "unless-stopped", + "user": "1000:1000", + } + } + } + environment, sops_config = _remote_environment(tmp_path, source_document, canonical_document) + bin_directory = Path(environment["PATH"].split(":", maxsplit=1)[0]) + sops_called = tmp_path / "sops-called" + _install_stub_command(bin_directory, "sops", "#!/bin/sh\n: > \"$C2R_SOPS_CALLED\"\nexit 99\n") + output_directory = tmp_path / "roles" + + result = CliRunner().invoke( + main, + [ + "generate", + "--host", + "web1", + "--project", + "my-app", + "--output", + str(output_directory), + "--sops-config", + str(sops_config), + "--check", + ], + env={**environment, "C2R_SOPS_CALLED": str(sops_called)}, + ) + + assert result.exit_code == 0, result.output + assert "✓ my-app -> my_app (checked)" in result.output + assert " templates/compose.yaml.j2" in result.output + assert secret_value not in result.output + assert not output_directory.exists() + assert not sops_called.exists() + + json_result = CliRunner().invoke( + main, + [ + "generate", + "--host", + "web1", + "--project", + "my-app", + "--output", + str(output_directory), + "--sops-config", + str(sops_config), + "--check", + "--format", + "json", + ], + env={**environment, "C2R_SOPS_CALLED": str(sops_called)}, + ) + + assert json_result.exit_code == 0, json_result.output + checked_project = json.loads(json_result.output)["projects"][0] + assert checked_project["role"] == "my_app" + assert "templates/compose.yaml.j2" in checked_project["files"] + assert secret_value not in json_result.output + assert not output_directory.exists() + assert not sops_called.exists() + + +def test_generate_all_keeps_successful_roles_after_an_isolated_failure(tmp_path: Path) -> None: + """Bulk generation keeps a good role when another project is not portable.""" + bin_directory = tmp_path / "bin" + bin_directory.mkdir() + _install_stub_command( + bin_directory, + "ssh", + """\ +#!/bin/sh +request=$(cat) + +if [ "$*" = '-- web1 docker compose ls --all --format json' ]; then + printf '%s\\n' '[{"Name":"bad","Status":"running(1)",' + printf '%s\\n' '"ConfigFiles":"/srv/bad/compose.yaml"},' + printf '%s\\n' '{"Name":"good","Status":"running(1)",' + printf '%s\\n' '"ConfigFiles":"/srv/good/compose.yaml"}]' + exit 0 +fi + +encoded=$(printf '%s\\n' "$request" | tail -n 2 | head -n 1) +payload=$(printf '%s' "$encoded" | base64 -d) +operation=$(printf '%s\\n' "$payload" | head -n 1) +path=$(printf '%s\\n' "$payload" | sed -n '2p') +case "$operation" in + capture) + if [ "$path" = /srv/bad/compose.yaml ]; then + printf '%s\\n' 'file 640 0 0' + base64 <<'EOF' | tr -d '\\n' +services: + web: + image: ghcr.io/example/web:1.2.3 + build: . +EOF + else + printf '%s\\n' 'file 640 0 0' + base64 <<'EOF' | tr -d '\\n' +services: + web: + image: ghcr.io/example/web:1.2.3 +EOF + fi + printf '\\n' + ;; + stat) + printf '%s\\n' missing + ;; + config) + printf '%s\\n' '{"services":{"web":{"environment":{},' + printf '%s\\n' '"healthcheck":{"test":["CMD","true"]},' + printf '%s\\n' '"image":"ghcr.io/example/web:1.2.3",' + printf '%s\\n' '"restart":"unless-stopped","user":"1000:1000"}}}' + ;; + *) + exit 97 + ;; +esac +""", + ) + _install_stub_command(bin_directory, "sops", "#!/bin/sh\nexit 99\n") + sops_config = tmp_path / "sops.yaml" + sops_config.write_text( + "creation_rules:\n - path_regex: vars/secrets\\.sops\\.yml$\n", + encoding="utf-8", + ) + output_directory = tmp_path / "roles" + + result = CliRunner().invoke( + main, + [ + "generate", + "--host", + "web1", + "--all", + "--output", + str(output_directory), + "--sops-config", + str(sops_config), + "--format", + "json", + ], + env={"PATH": f"{bin_directory}:{os.environ['PATH']}"}, + ) + + assert result.exit_code == 1, result.output + summary = json.loads(result.output)["projects"] + assert [(project["name"], project["role"], project["status"]) for project in summary] == [ + ("bad", None, "failed"), + ("good", "good", "generated"), + ] + assert summary[0]["error"] == "C2R005: remote build contexts are not supported" + assert (output_directory / "good").is_dir() + assert not (output_directory / "bad").exists() + + +def test_generate_all_adopts_multifile_sources_and_isolates_an_unsupported_project( + tmp_path: Path, +) -> None: + """Bulk generation retains a source-faithful role without persisting its secrets.""" + secret_value = "postgres://operator:bulk-source-secret@db/app" + primary_source = ( + b"# Primary source remains byte-faithful.\r\n" + b"services:\r\n" + b" web:\r\n" + b" image: ghcr.io/example/web:1.2.3\r\n" + b" environment:\r\n" + b" DATABASE_URL: ${DATABASE_URL}\r\n" + b" env_file: ./env/web.env\r\n" + b"secrets:\r\n" + b" app_secret:\r\n" + b" file: ./secrets/app.bin\r\n" + ) + override_source = b"""\ +services: + web: + environment: + LOG_LEVEL: debug +""" + root_dotenv = f"DATABASE_URL={secret_value}\n".encode() + service_dotenv = b"LOG_LEVEL=debug\n" + file_secret = b"\x00bulk-file-secret\n" + bin_directory = tmp_path / "bin" + bin_directory.mkdir() + _install_stub_command( + bin_directory, + "ssh", + """\ +#!/bin/sh +request=$(cat) + +if [ "$*" = '-- web1 docker compose ls --all --format json' ]; then + printf '%s\\n' '[{"Name":"good-app","Status":"running(1)",' + printf '%s\\n' '"ConfigFiles":["/srv/good/compose.yaml",' + printf '%s\\n' '"/srv/good/compose.override.yaml"]},' + printf '%s\\n' '{"Name":"bad-app","Status":"running(1)",' + printf '%s\\n' '"ConfigFiles":"/srv/bad/compose.yaml"}]' + exit 0 +fi + +encoded=$(printf '%s\\n' "$request" | tail -n 2 | head -n 1) +payload=$(printf '%s' "$encoded" | base64 -d) +operation=$(printf '%s\\n' "$payload" | head -n 1) +path=$(printf '%s\\n' "$payload" | sed -n '2p') +case "$operation" in + capture) + printf '%s\\n' 'file 640 0 0' + case "$path" in + /srv/good/compose.yaml) printf '%s\\n' "$C2R_PRIMARY_SOURCE" ;; + /srv/good/compose.override.yaml) printf '%s\\n' "$C2R_OVERRIDE_SOURCE" ;; + /srv/good/.env) printf '%s\\n' "$C2R_ROOT_DOTENV" ;; + /srv/good/env/web.env) printf '%s\\n' "$C2R_SERVICE_DOTENV" ;; + /srv/good/secrets/app.bin) printf '%s\\n' "$C2R_FILE_SECRET" ;; + /srv/bad/compose.yaml) + base64 <<'EOF' | tr -d '\\n' +services: + web: + image: ghcr.io/example/web:1.2.3 + build: . +EOF + printf '\\n' + ;; + *) exit 98 ;; + esac + ;; + stat) + case "$path" in + /srv/good/.env|/srv/good/env/web.env|/srv/good/secrets/app.bin) printf '%s\\n' file ;; + /srv/bad/.env) printf '%s\\n' missing ;; + *) exit 98 ;; + esac + ;; + config) + printf '%s\\n' "$C2R_CANONICAL_DOCUMENT" + ;; + *) exit 97 ;; +esac +""", + ) + _install_stub_command( + bin_directory, + "sops", + """\ +#!/bin/sh +printf '%s\\n' called >> "$C2R_SOPS_CALLS" +cat >/dev/null +printf '%s\\n' "$C2R_SOPS_OUTPUT" +""", + ) + sops_config = tmp_path / "sops.yaml" + sops_config.write_text( + "creation_rules:\n - path_regex: vars/secrets\\.sops\\.yml$\n", + encoding="utf-8", + ) + canonical_document = { + "services": { + "web": { + "environment": {"DATABASE_URL": secret_value, "LOG_LEVEL": "debug"}, + "healthcheck": {"test": ["CMD", "true"]}, + "image": "ghcr.io/example/web:1.2.3", + "restart": "unless-stopped", + "user": "1000:1000", + } + } + } + sops_calls = tmp_path / "sops-calls" + output_directory = tmp_path / "roles" + environment = { + "C2R_CANONICAL_DOCUMENT": json.dumps(canonical_document), + "C2R_FILE_SECRET": base64.b64encode(file_secret).decode("ascii"), + "C2R_OVERRIDE_SOURCE": base64.b64encode(override_source).decode("ascii"), + "C2R_PRIMARY_SOURCE": base64.b64encode(primary_source).decode("ascii"), + "C2R_ROOT_DOTENV": base64.b64encode(root_dotenv).decode("ascii"), + "C2R_SERVICE_DOTENV": base64.b64encode(service_dotenv).decode("ascii"), + "C2R_SOPS_CALLS": str(sops_calls), + "C2R_SOPS_OUTPUT": _encrypted_sops_output( + "good_app_dotenv_env_database_url", "good_app_secret_app_secret" + ), + "PATH": f"{bin_directory}:{os.environ['PATH']}", + } + arguments = [ + "generate", + "--host", + "web1", + "--all", + "--output", + str(output_directory), + "--sops-config", + str(sops_config), + "--format", + "json", + ] + + result = CliRunner().invoke(main, arguments, env=environment) + + assert result.exit_code == 1, result.output + summary = json.loads(result.output)["projects"] + assert [(item["name"], item["role"], item["status"]) for item in summary] == [ + ("good-app", "good_app", "generated"), + ("bad-app", None, "failed"), + ] + assert summary[1]["error"] == "C2R005: remote build contexts are not supported" + assert secret_value not in result.output + assert (output_directory / "good_app").is_dir() + assert not (output_directory / "bad_app").exists() + assert sops_calls.read_text(encoding="utf-8").splitlines() == ["called"] + + role_directory = output_directory / "good_app" + defaults = YAML(typ="safe").load( + (role_directory / "defaults" / "main.yml").read_text(encoding="utf-8") + ) + assert isinstance(defaults, dict) + for relative_path, source, newline_sequence in ( + ("compose.yaml", primary_source, "\r\n"), + ("compose.override.yaml", override_source, "\n"), + (".env", root_dotenv, "\n"), + ("env/web.env", service_dotenv, "\n"), + ): + template = (role_directory / "templates" / f"{relative_path}.j2").read_text( + encoding="utf-8" + ) + rendered = Environment( + keep_trailing_newline=True, newline_sequence=newline_sequence + ).from_string(template).render( + **defaults, + good_app_secrets={"good_app_dotenv_env_database_url": secret_value}, + ) + assert rendered.encode() == source + role_bytes = b"".join( + path.read_bytes() for path in role_directory.rglob("*") if path.is_file() + ) + assert secret_value.encode() not in role_bytes + assert base64.b64encode(file_secret) not in role_bytes + assert secret_value not in (role_directory / ".compose2role.json").read_text(encoding="utf-8") + + check_output = tmp_path / "check-roles" + check_result = CliRunner().invoke( + main, + [ + "generate", + "--host", + "web1", + "--all", + "--output", + str(check_output), + "--sops-config", + str(sops_config), + "--check", + "--format", + "json", + ], + env=environment, + ) + + assert check_result.exit_code == 1, check_result.output + checked_summary = json.loads(check_result.output)["projects"] + assert [(item["name"], item["role"], item["status"]) for item in checked_summary] == [ + ("good-app", "good_app", "checked"), + ("bad-app", None, "failed"), + ] + assert "templates/compose.yaml.j2" in checked_summary[0]["files"] + assert secret_value not in check_result.output + assert not check_output.exists() + assert sops_calls.read_text(encoding="utf-8").splitlines() == ["called"] + + +def test_generate_reports_secret_role_output_failures_as_output_failures(tmp_path: Path) -> None: + """A secret role's output failure is not misreported as an SOPS failure.""" + secret_value = "output-category-secret" + source_document = f"""\ +services: + web: + image: ghcr.io/example/web:1.2.3 + environment: + API_TOKEN: {secret_value} +""" + canonical_document = { + "services": { + "web": { + "environment": {"API_TOKEN": secret_value}, + "healthcheck": {"test": ["CMD", "true"]}, + "image": "ghcr.io/example/web:1.2.3", + "restart": "unless-stopped", + "user": "1000:1000", + } + } + } + environment, sops_config = _remote_environment(tmp_path, source_document, canonical_document) + bin_directory = Path(environment["PATH"].split(":", maxsplit=1)[0]) + _install_stub_command( + bin_directory, + "sops", + "#!/bin/sh\ncat >/dev/null\nprintf '%s\\n' \"$C2R_SOPS_OUTPUT\"\n", + ) + output_directory = tmp_path / "roles" + output_directory.write_text("not a directory", encoding="utf-8") + + result = CliRunner().invoke( + main, + [ + "generate", + "--host", + "web1", + "--project", + "my-app", + "--output", + str(output_directory), + "--sops-config", + str(sops_config), + ], + env={ + **environment, + "C2R_SOPS_OUTPUT": _encrypted_sops_output("my_app_web_api_token"), + }, + ) + + assert result.exit_code == 1, result.output + assert ( + "my-app -> my_app: FAILED (output): remote role output must be a directory" + in result.output + ) + assert secret_value not in result.output + + +def test_generate_keeps_secret_role_unwritten_when_sops_encryption_fails(tmp_path: Path) -> None: + """SOPS failure exposes no stderr value and leaves no partial role behind.""" + secret_value = "sops-failure-secret" + source_document = f"""\ +services: + web: + image: ghcr.io/example/web:1.2.3 + environment: + API_TOKEN: {secret_value} +""" + canonical_document = { + "services": { + "web": { + "environment": {"API_TOKEN": secret_value}, + "healthcheck": {"test": ["CMD", "true"]}, + "image": "ghcr.io/example/web:1.2.3", + "restart": "unless-stopped", + "user": "1000:1000", + } + } + } + environment, sops_config = _remote_environment(tmp_path, source_document, canonical_document) + bin_directory = Path(environment["PATH"].split(":", maxsplit=1)[0]) + _install_stub_command( + bin_directory, + "sops", + "#!/bin/sh\nprintf '%s\\n' \"$C2R_EXPECTED_SECRET\" >&2\nexit 91\n", + ) + output_directory = tmp_path / "roles" + + result = CliRunner().invoke( + main, + [ + "generate", + "--host", + "web1", + "--project", + "my-app", + "--output", + str(output_directory), + "--sops-config", + str(sops_config), + ], + env={**environment, "C2R_EXPECTED_SECRET": secret_value}, + ) + + assert result.exit_code == 1, result.output + assert "my-app -> my_app: FAILED (sops): SOPS encryption failed" in result.output + assert secret_value not in result.output + assert not output_directory.exists() + + +@pytest.mark.parametrize( + "sops_output", + ( + "not: [valid", + "sops:\n mac: ENC[AES256_GCM,data:fixture]", + "my_app_web_api_token: sops-output-secret\nsops:\n mac: ENC[AES256_GCM,data:fixture]", + ), + ids=("invalid-yaml", "missing-secret", "plaintext-secret"), +) +def test_generate_keeps_secret_role_unwritten_when_sops_output_is_invalid( + tmp_path: Path, sops_output: str +) -> None: + """Malformed, incomplete, or plaintext SOPS output never reaches a role directory.""" + secret_value = "sops-output-secret" + source_document = f"""\ +services: + web: + image: ghcr.io/example/web:1.2.3 + environment: + API_TOKEN: {secret_value} +""" + canonical_document = { + "services": { + "web": { + "environment": {"API_TOKEN": secret_value}, + "healthcheck": {"test": ["CMD", "true"]}, + "image": "ghcr.io/example/web:1.2.3", + "restart": "unless-stopped", + "user": "1000:1000", + } + } + } + environment, sops_config = _remote_environment(tmp_path, source_document, canonical_document) + bin_directory = Path(environment["PATH"].split(":", maxsplit=1)[0]) + _install_stub_command( + bin_directory, + "sops", + "#!/bin/sh\ncat >/dev/null\nprintf '%s\\n' \"$C2R_SOPS_OUTPUT\"\n", + ) + output_directory = tmp_path / "roles" + + result = CliRunner().invoke( + main, + [ + "generate", + "--host", + "web1", + "--project", + "my-app", + "--output", + str(output_directory), + "--sops-config", + str(sops_config), + ], + env={**environment, "C2R_SOPS_OUTPUT": sops_output}, + ) + + assert result.exit_code == 1, result.output + assert "my-app -> my_app: FAILED (sops): SOPS encryption output is invalid" in result.output + assert secret_value not in result.output + assert not output_directory.exists() + + +def test_generate_all_rejects_normalized_role_name_collisions_before_writing( + tmp_path: Path, +) -> None: + """Raw project names that normalize equally leave no ambiguous role output.""" + bin_directory = tmp_path / "bin" + bin_directory.mkdir() + _install_stub_command( + bin_directory, + "ssh", + """\ +#!/bin/sh +request=$(cat) + +if [ "$*" = '-- web1 docker compose ls --all --format json' ]; then + printf '%s\\n' '[{"Name":"my-app","Status":"running(1)",' + printf '%s\\n' '"ConfigFiles":"/srv/my-app/compose.yaml"},' + printf '%s\\n' '{"Name":"my_app","Status":"running(1)",' + printf '%s\\n' '"ConfigFiles":"/srv/my_app/compose.yaml"}]' + exit 0 +fi + +encoded=$(printf '%s\\n' "$request" | tail -n 2 | head -n 1) +payload=$(printf '%s' "$encoded" | base64 -d) +operation=$(printf '%s\\n' "$payload" | head -n 1) +case "$operation" in + capture) + printf '%s\\n' 'file 640 0 0' + base64 <<'EOF' | tr -d '\\n' +services: + web: + image: ghcr.io/example/web:1.2.3 +EOF + printf '\\n' + ;; + stat) + printf '%s\\n' missing + ;; + config) + printf '%s\\n' '{"services":{"web":{"environment":{},' + printf '%s\\n' '"healthcheck":{"test":["CMD","true"]},' + printf '%s\\n' '"image":"ghcr.io/example/web:1.2.3",' + printf '%s\\n' '"restart":"unless-stopped","user":"1000:1000"}}}' + ;; + *) + exit 97 + ;; +esac +""", + ) + _install_stub_command(bin_directory, "sops", "#!/bin/sh\nexit 99\n") + sops_config = tmp_path / "sops.yaml" + sops_config.write_text( + "creation_rules:\n - path_regex: vars/secrets\\.sops\\.yml$\n", + encoding="utf-8", + ) + output_directory = tmp_path / "roles" + + result = CliRunner().invoke( + main, + [ + "generate", + "--host", + "web1", + "--all", + "--output", + str(output_directory), + "--sops-config", + str(sops_config), + "--format", + "json", + ], + env={"PATH": f"{bin_directory}:{os.environ['PATH']}"}, + ) + + assert result.exit_code == 1, result.output + projects = json.loads(result.output)["projects"] + assert [(project["name"], project["role"], project["status"]) for project in projects] == [ + ("my-app", "my_app", "failed"), + ("my_app", "my_app", "failed"), + ] + assert all( + project["error"] == "normalized remote role name collides with another selected project" + for project in projects + ) + assert not output_directory.exists() + + +def test_generate_check_reports_planned_files_for_pre_existing_role_without_modifying_it( + tmp_path: Path, +) -> None: + """Check mode lists planned files without validating or altering existing output.""" + source_document = """\ +services: + web: + image: ghcr.io/example/web:1.2.3 +""" + canonical_document = { + "services": { + "web": { + "environment": {}, + "healthcheck": {"test": ["CMD", "true"]}, + "image": "ghcr.io/example/web:1.2.3", + "restart": "unless-stopped", + "user": "1000:1000", + } + } + } + environment, sops_config = _remote_environment(tmp_path, source_document, canonical_document) + output_directory = tmp_path / "roles" + role_directory = output_directory / "my_app" + role_directory.mkdir(parents=True) + sentinel = role_directory / "operator-file" + sentinel.write_text("do not alter", encoding="utf-8") + + result = CliRunner().invoke( + main, + [ + "generate", + "--host", + "web1", + "--project", + "my-app", + "--output", + str(output_directory), + "--sops-config", + str(sops_config), + "--check", + ], + env=environment, + ) + + assert result.exit_code == 0, result.output + assert "my-app -> my_app (checked)" in result.output + assert " .compose2role.json" in result.output + assert sentinel.read_text(encoding="utf-8") == "do not alter" + assert not (role_directory / ".compose2role.json").exists() + + +def test_generate_check_rejects_a_symlinked_output_directory(tmp_path: Path) -> None: + """Check mode applies the writer's output-directory safety rule before reporting success.""" + source_document = """\ +services: + web: + image: ghcr.io/example/web:1.2.3 +""" + canonical_document = { + "services": { + "web": { + "environment": {}, + "healthcheck": {"test": ["CMD", "true"]}, + "image": "ghcr.io/example/web:1.2.3", + "restart": "unless-stopped", + "user": "1000:1000", + } + } + } + environment, sops_config = _remote_environment(tmp_path, source_document, canonical_document) + target_directory = tmp_path / "operator-output" + target_directory.mkdir() + output_directory = tmp_path / "roles" + output_directory.symlink_to(target_directory, target_is_directory=True) + + result = CliRunner().invoke( + main, + [ + "generate", + "--host", + "web1", + "--project", + "my-app", + "--output", + str(output_directory), + "--sops-config", + str(sops_config), + "--check", + ], + env=environment, + ) + + assert result.exit_code == 1, result.output + assert ( + "my-app -> my_app: FAILED (output): remote role output must be a directory" + in result.output + ) + assert not (target_directory / "my_app").exists() + + +def test_generate_prefers_explicit_sops_config_and_falls_back_to_environment( + tmp_path: Path, +) -> None: + """The selected SOPS config has deterministic option-over-environment precedence.""" + source_document = """\ +services: + web: + image: ghcr.io/example/web:1.2.3 +""" + canonical_document = { + "services": { + "web": { + "environment": {}, + "healthcheck": {"test": ["CMD", "true"]}, + "image": "ghcr.io/example/web:1.2.3", + "restart": "unless-stopped", + "user": "1000:1000", + } + } + } + environment, explicit_config = _remote_environment( + tmp_path, source_document, canonical_document + ) + environment_config = tmp_path / "environment-sops.yaml" + environment_config.write_text( + "creation_rules:\n - path_regex: vars/secrets\\.sops\\.yml$\n", + encoding="utf-8", + ) + missing_config = tmp_path / "not-used.yaml" + + explicit_result = CliRunner().invoke( + main, + [ + "generate", + "--host", + "web1", + "--project", + "my-app", + "--output", + str(tmp_path / "explicit-roles"), + "--sops-config", + str(explicit_config), + "--check", + ], + env={**environment, "COMPOSE2ROLE_SOPS_CONFIG": str(missing_config)}, + ) + environment_result = CliRunner().invoke( + main, + [ + "generate", + "--host", + "web1", + "--project", + "my-app", + "--output", + str(tmp_path / "environment-roles"), + "--check", + ], + env={**environment, "COMPOSE2ROLE_SOPS_CONFIG": str(environment_config)}, + ) + + assert explicit_result.exit_code == 0, explicit_result.output + assert environment_result.exit_code == 0, environment_result.output + + +def test_generate_rejects_missing_sops_config_before_remote_acquisition(tmp_path: Path) -> None: + """Missing configuration stops before the SSH resolver can acquire remote values.""" + bin_directory = tmp_path / "bin" + bin_directory.mkdir() + _install_stub_command( + bin_directory, + "ssh", + "#!/bin/sh\n: > \"$C2R_SSH_MARKER\"\nexit 97\n", + ) + ssh_marker = tmp_path / "ssh-called" + + result = CliRunner().invoke( + main, + [ + "generate", + "--host", + "web1", + "--project", + "my-app", + "--output", + str(tmp_path / "roles"), + ], + env={ + "C2R_SSH_MARKER": str(ssh_marker), + "COMPOSE2ROLE_SOPS_CONFIG": "", + "PATH": f"{bin_directory}:{os.environ['PATH']}", + }, + ) + + assert result.exit_code == 1 + assert "remote generation requires an existing SOPS config" in result.output + assert not ssh_marker.exists() + + +def test_generate_rejects_missing_sops_executable_before_remote_acquisition(tmp_path: Path) -> None: + """A valid config does not permit SSH acquisition when SOPS is unavailable.""" + bin_directory = tmp_path / "bin" + bin_directory.mkdir() + _install_stub_command( + bin_directory, + "ssh", + "#!/bin/sh\n: > \"$C2R_SSH_MARKER\"\nexit 97\n", + ) + sops_config = tmp_path / "sops.yaml" + sops_config.write_text( + "creation_rules:\n - path_regex: vars/secrets\\.sops\\.yml$\n", + encoding="utf-8", + ) + ssh_marker = tmp_path / "ssh-called" + + result = CliRunner().invoke( + main, + [ + "generate", + "--host", + "web1", + "--project", + "my-app", + "--output", + str(tmp_path / "roles"), + "--sops-config", + str(sops_config), + ], + env={"C2R_SSH_MARKER": str(ssh_marker), "PATH": str(bin_directory)}, + ) + + assert result.exit_code == 1 + assert "remote generation requires the SOPS executable" in result.output + assert not ssh_marker.exists() + + +def test_generate_check_validates_the_sops_creation_rule_before_remote_acquisition( + tmp_path: Path, +) -> None: + """Check mode validates an applicable creation rule without running SOPS or SSH.""" + bin_directory = tmp_path / "bin" + bin_directory.mkdir() + _install_stub_command( + bin_directory, + "ssh", + "#!/bin/sh\n: > \"$C2R_SSH_MARKER\"\nexit 97\n", + ) + _install_stub_command(bin_directory, "sops", "#!/bin/sh\nexit 97\n") + sops_config = tmp_path / "sops.yaml" + sops_config.write_text( + "creation_rules:\n - path_regex: other-output\\.yml$\n", + encoding="utf-8", + ) + ssh_marker = tmp_path / "ssh-called" + + result = CliRunner().invoke( + main, + [ + "generate", + "--host", + "web1", + "--project", + "my-app", + "--output", + str(tmp_path / "roles"), + "--sops-config", + str(sops_config), + "--check", + ], + env={"C2R_SSH_MARKER": str(ssh_marker), "PATH": str(bin_directory)}, + ) + + assert result.exit_code == 1 + assert "SOPS config has no creation rule for vars/secrets.sops.yml" in result.output + assert not ssh_marker.exists() + + +def test_generate_applies_secret_policy_overrides_to_remote_environment( + tmp_path: Path, +) -> None: + """Exact secret policy decisions override automatic remote environment classification.""" + source_document = """\ +services: + web: + image: ghcr.io/example/web:1.2.3 + environment: + API_TOKEN: operator-approved-plain + LOG_LEVEL: info +""" + canonical_document = { + "services": { + "web": { + "environment": { + "API_TOKEN": "operator-approved-plain", + "LOG_LEVEL": "info", + }, + "healthcheck": {"test": ["CMD", "true"]}, + "image": "ghcr.io/example/web:1.2.3", + "restart": "unless-stopped", + "user": "1000:1000", + } + } + } + environment, sops_config = _remote_environment(tmp_path, source_document, canonical_document) + bin_directory = Path(environment["PATH"].split(":", maxsplit=1)[0]) + _install_stub_command( + bin_directory, + "sops", + "#!/bin/sh\ncat >/dev/null\nprintf '%s\\n' \"$C2R_SOPS_OUTPUT\"\n", + ) + secret_policy_path = tmp_path / "policy.toml" + secret_policy_path.write_text( + """\ +[[rules]] +project = "my-app" +service = "web" +key = "API_TOKEN" +decision = "plain" + +[[rules]] +project = "my-app" +service = "web" +key = "LOG_LEVEL" +decision = "secret" +""", + encoding="utf-8", + ) + output_directory = tmp_path / "roles" + + result = CliRunner().invoke( + main, + [ + "generate", + "--host", + "web1", + "--project", + "my-app", + "--output", + str(output_directory), + "--secret-policy", + str(secret_policy_path), + "--sops-config", + str(sops_config), + "--format", + "json", + ], + env={ + **environment, + "C2R_SOPS_OUTPUT": _encrypted_sops_output("my_app_web_log_level"), + }, + ) + + assert result.exit_code == 0, result.output + project = json.loads(result.output)["projects"][0] + assert project["classifications"] == [ + { + "key": "LOG_LEVEL", + "location": "remote-compose.json:1:1", + "reason": "secret-policy:forced-secret", + "service": "web", + } + ] + defaults = (output_directory / "my_app" / "defaults" / "main.yml").read_text(encoding="utf-8") + compose_template = (output_directory / "my_app" / "templates" / "compose.yaml.j2").read_text( + encoding="utf-8" + ) + assert 'my_app_web_api_token: "operator-approved-plain"' in defaults + assert "my_app_web_log_level:" not in defaults + assert "API_TOKEN: {{ my_app_web_api_token }}" in compose_template + assert "LOG_LEVEL: {{ my_app_secrets['my_app_web_log_level'] }}" in compose_template + + +def test_generate_refuses_other_nonportable_compose_features(tmp_path: Path) -> None: + """Selected generation retains the resolver's build/extends/profiles/include refusals.""" + cases = ( + ( + "build", + """\ +services: + web: + image: ghcr.io/example/web:1.2.3 + build: . +""", + "C2R005: remote build contexts are not supported", + ), + ( + "extends", + """\ +services: + web: + image: ghcr.io/example/web:1.2.3 + extends: base +""", + "C2R006: remote service extends declarations are not supported", + ), + ( + "profiles", + """\ +services: + web: + image: ghcr.io/example/web:1.2.3 + profiles: [optional] +""", + "C2R007: remote service profiles are not supported", + ), + ( + "include", + """\ +include: other.compose.yml +services: + web: + image: ghcr.io/example/web:1.2.3 +""", + "C2R008: remote Compose include files are not supported", + ), + ) + canonical_document = { + "services": { + "web": { + "environment": {}, + "healthcheck": {"test": ["CMD", "true"]}, + "image": "ghcr.io/example/web:1.2.3", + "restart": "unless-stopped", + "user": "1000:1000", + } + } + } + + for name, source_document, expected_message in cases: + case_directory = tmp_path / name + case_directory.mkdir() + environment, sops_config = _remote_environment( + case_directory, source_document, canonical_document + ) + output_directory = case_directory / "roles" + result = CliRunner().invoke( + main, + [ + "generate", + "--host", + "web1", + "--project", + "my-app", + "--output", + str(output_directory), + "--sops-config", + str(sops_config), + ], + env=environment, + ) + + assert result.exit_code == 1, result.output + assert expected_message in result.output + assert not output_directory.exists() + + +def test_generate_all_writes_roles_and_no_playbook(tmp_path: Path) -> None: + """Generation is roles-only: --all writes one role per project and nothing else.""" + source_document = "services:\n web:\n image: ghcr.io/example/web:1.2.3\n" + canonical_document = { + "services": { + "web": { + "environment": {}, + "healthcheck": {"test": ["CMD", "true"]}, + "image": "ghcr.io/example/web:1.2.3", + "restart": "unless-stopped", + "user": "1000:1000", + } + } + } + environment, sops_config = _remote_environment(tmp_path, source_document, canonical_document) + + result = CliRunner().invoke( + main, + [ + "generate", + "--host", + "web1", + "--all", + "--output", + str(tmp_path / "roles"), + "--sops-config", + str(sops_config), + ], + env=environment, + ) + + assert result.exit_code == 0, result.output + assert (tmp_path / "roles" / "my_app" / "tasks" / "main.yml").is_file() + assert sorted(path.name for path in tmp_path.iterdir() if path.is_dir()) == [ + "bin", + "roles", + ] diff --git a/tests/test_secret_policy.py b/tests/test_secret_policy.py new file mode 100644 index 0000000..cd94578 --- /dev/null +++ b/tests/test_secret_policy.py @@ -0,0 +1,136 @@ +from __future__ import annotations + +from pathlib import Path, PurePosixPath + +import pytest + +from compose2role.errors import SecretPolicyError +from compose2role.secret_policy import load_secret_policy + + +def _write_policy(tmp_path: Path, content: str) -> Path: + path = tmp_path / "secret-policy.toml" + path.write_text(content, encoding="utf-8") + return path + + +def test_secret_policy_key_list_expands_to_one_exact_rule_per_key(tmp_path: Path) -> None: + policy = load_secret_policy( + _write_policy( + tmp_path, + """\ +[[rules]] +project = "myapp" +service = "web" +key = ["API_TOKEN", "DB_PASSWORD"] +decision = "plain" +""", + ) + ) + + assert policy.decision_for("myapp", "web", "API_TOKEN") == "plain" + assert policy.decision_for("myapp", "web", "DB_PASSWORD") == "plain" + assert policy.decision_for("myapp", "web", "OTHER_KEY") is None + assert policy.decision_for("myapp", "worker", "API_TOKEN") is None + + +def test_secret_policy_dotenv_key_list_expands_to_one_exact_rule_per_key(tmp_path: Path) -> None: + policy = load_secret_policy( + _write_policy( + tmp_path, + """\ +[[dotenv_rules]] +project = "myapp" +source = ".env" +key = ["MAIL_PASSWORD", "SMTP_TOKEN"] +decision = "secret" +""", + ) + ) + + assert policy.dotenv_decision_for("myapp", PurePosixPath(".env"), "MAIL_PASSWORD") == "secret" + assert policy.dotenv_decision_for("myapp", PurePosixPath(".env"), "SMTP_TOKEN") == "secret" + assert policy.dotenv_decision_for("myapp", PurePosixPath(".env"), "OTHER_KEY") is None + assert policy.dotenv_decision_for("myapp", PurePosixPath(".env.local"), "SMTP_TOKEN") is None + + +def test_secret_policy_single_key_rule_still_matches_exactly(tmp_path: Path) -> None: + policy = load_secret_policy( + _write_policy( + tmp_path, + """\ +[[rules]] +project = "myapp" +service = "web" +key = "API_TOKEN" +decision = "secret" +""", + ) + ) + + assert policy.decision_for("myapp", "web", "API_TOKEN") == "secret" + assert policy.decision_for("myapp", "web", "API_TOKEN_2") is None + + +@pytest.mark.parametrize( + "key_value", + ['[]', '["API_TOKEN", 7]', '["API_TOKEN", ""]', '""', "7"], +) +def test_secret_policy_rejects_invalid_environment_key_values( + tmp_path: Path, key_value: str +) -> None: + policy_path = _write_policy( + tmp_path, + f"""\ +[[rules]] +project = "myapp" +service = "web" +key = {key_value} +decision = "secret" +""", + ) + + with pytest.raises(SecretPolicyError, match="keys must be non-empty strings"): + load_secret_policy(policy_path) + + +@pytest.mark.parametrize( + "key_value", + ['[]', '["MAIL_PASSWORD", 7]', '["MAIL_PASSWORD", ""]', '""', "7"], +) +def test_secret_policy_rejects_invalid_dotenv_key_values(tmp_path: Path, key_value: str) -> None: + policy_path = _write_policy( + tmp_path, + f"""\ +[[dotenv_rules]] +project = "myapp" +source = ".env" +key = {key_value} +decision = "secret" +""", + ) + + with pytest.raises(SecretPolicyError, match="keys must be non-empty strings"): + load_secret_policy(policy_path) + + +def test_secret_policy_rejects_conflicts_between_list_and_single_rules(tmp_path: Path) -> None: + policy_path = _write_policy( + tmp_path, + """\ +[[rules]] +project = "myapp" +service = "web" +key = ["API_TOKEN", "LOG_LEVEL"] +decision = "secret" + +[[rules]] +project = "myapp" +service = "web" +key = "LOG_LEVEL" +decision = "plain" +""", + ) + + with pytest.raises(SecretPolicyError, match="conflicting exact rules"): + load_secret_policy(policy_path)