Metadata-Version: 2.4
Name: foca-plugin-tes-data-crypt4gh
Version: 0.1.1
Summary: FOCA gateway plugin that decrypts the Crypt4GH inputs of TES tasks in an extra executor
Author-email: ELIXIR On-Cloud <cloud-service@elixir-europe.org>
License-Expression: Apache-2.0
Project-URL: Homepage, https://github.com/elixir-cloud-aai/foca-plugins/tree/dev/plugins/tes/data/crypt4gh
Project-URL: Documentation, https://foca-plugins.readthedocs.io/
Project-URL: Changelog, https://github.com/elixir-cloud-aai/foca-plugins/blob/dev/plugins/tes/data/crypt4gh/CHANGELOG.md
Project-URL: Issues, https://github.com/elixir-cloud-aai/foca-plugins/issues
Keywords: foca,foca-plugin,gateway,ga4gh,tes,crypt4gh,encryption
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: System Administrators
Classifier: Natural Language :: English
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Programming Language :: Python :: 3.15
Classifier: Typing :: Typed
Requires-Python: >=3.11
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: foca<0.24,>=0.19.0
Requires-Dist: pydantic<3,>=2.7
Dynamic: license-file

# tes.data.crypt4gh

<!-- --8<-- [start:summary] -->
Decrypts the [Crypt4GH](https://www.ga4gh.org/news_item/crypt4gh-a-secure-method-for-sharing-human-genetic-data/)
inputs of TES tasks, which TES implementations can't read themselves: adds
an executor before the task's own ones that decrypts the Crypt4GH inputs
with the secret keys among the task's inputs to a volume of its own,
points the task's executors to the decrypted files, and adds an executor
after them that empties that volume again. The successor of the Crypt4GH
middleware of proTES.
<!-- --8<-- [end:summary] -->

| | |
| --- | --- |
| Plugin ID | `tes.data.crypt4gh` |
| Distribution | `foca-plugin-tes-data-crypt4gh` |
| Import path | `foca_plugins.tes.data.crypt4gh` |
| APIs | `tes` |
| Phases | `request` |
| Capabilities | `crypt4gh-decryption` |
| Maturity | experimental |
| Data transfers | none |
| Data stored | none (no plugin store) |
| FOCA | 0.19, 0.20, 0.21, 0.22, 0.23 (`>=0.19.0,<0.24`) |

## What it does

`request`:

1. Leaves tasks without executors and tasks whose `executors` or
   `volumes` have the wrong type as they are, without checking them: the
   gateway app rejects them.
2. Finds the Crypt4GH files and the secret keys among the task's `inputs`
   (files only, not `type: DIRECTORY`; a null `type` is a file), by the
   suffixes of their `path`s:
   `encrypted_suffixes` (`.c4gh`) and `key_suffixes` (`.sec`). The gateway
   can't read the inputs, so their names decide; the decryption executor
   checks the files when the task runs.
3. Leaves tasks without Crypt4GH inputs as they are, even if they have
   keys. Tasks that it changed already (they have `volume` once and, as
   their first executor, exactly the decryption executor that it would add)
   are checked as in step 4, except for that volume, and only their other
   executors are changed (steps 6 and 7), so that a task sent through the
   gateway twice gets one decryption executor and one teardown executor.
   This holds for gateways with the same settings only: chain gateways with
   this plugin only if their settings are the same.
4. Otherwise, it rejects the task with `422 Unprocessable Content` if:
   - it has no secret key among its inputs;
   - a Crypt4GH input or key has no absolute, normalized path (e.g.,
     `/in/./reads.fq.c4gh` or `//in/reads.fq.c4gh`), or another input, an
     output or a volume has a relative path;
   - a Crypt4GH input has no name besides its suffix (e.g., `/in/.c4gh`),
     or two decrypt to the same name (e.g., `/a/x.c4gh` and `/b/x.c4gh`);
   - an input, output or volume of the task is the `volume` of the
     decrypted files, inside it or one of its parent directories (e.g.,
     `/vol` or `/`), also in another spelling (e.g., `/vol/x/../crypt4gh`);
     for an output with wildcards, the directory before its first wildcard
     counts (e.g., `/` for `/v*/crypt4gh/*`);
   - an output has the path of a Crypt4GH input, also in another spelling,
     or has wildcards that match one (e.g., `/in/*.c4gh`).
5. Adds `volume` (`/vol/crypt4gh`) to the task's `volumes`, and, as the
   task's first executor, the decryption executor (`image`, `command`), which
   decrypts each Crypt4GH input to `<volume>/<name without suffix>`, e.g.,
   `/in/reads.fq.c4gh` to `/vol/crypt4gh/reads.fq`, with any of the keys.
   The paths are arguments of the executor, so a task's Crypt4GH inputs
   and keys are bounded by the argument limit of the backend's nodes
   (usually 2 MiB in all, i.e., thousands of paths).
6. Changes the arguments of the task's executors (`command`, and `stdin`)
   that are the path of a Crypt4GH input, in any spelling of the absolute
   path (e.g., `/in/./reads.fq.c4gh`), to the path of its decrypted file.
   Only whole, absolute arguments are changed: a relative path, or an
   argument such as
   `--in=/in/reads.fq.c4gh` keeps the encrypted file, as do `env` and
   `workdir`.
7. With `teardown` (the default), adds the teardown executor after the
   task's own executors, which empties the volume (see [The teardown
   executor](#the-teardown-executor)). Exact copies of it among the task's
   own executors are moved to the end, not kept where they are, so that it
   runs once, after all of them. With `teardown_on_error`, the task's own
   executors get `ignore_error: true`.

For example, a task with the inputs `/in/reads.fq.c4gh` and
`/keys/alice.sec` and the executor `align /in/reads.fq.c4gh` gets these
executors:

```json
[
  {
    "image": "<image>",
    "command": ["crypt4gh-decrypt", "--key", "/keys/alice.sec",
                "--file", "/in/reads.fq.c4gh", "/vol/crypt4gh/reads.fq"]
  },
  {"image": "aligner", "command": ["align", "/vol/crypt4gh/reads.fq"]},
  {
    "image": "<image>",
    "command": ["crypt4gh-decrypt", "--clear", "/vol/crypt4gh"]
  }
]
```

Annotations, counts only: `encrypted_inputs` and `key_inputs`, for all
tasks but those left to the app in step 1. Other
inputs (e.g., a reference genome) are left where they are.

### The decryption executor

The executor runs `crypt4gh-decrypt` (`decrypt.py` of the package) in the
decryption image. It reads the keys (Crypt4GH or OpenSSH ed25519 secret
keys, **without passphrase**), then decrypts each file with any of them,
writing it to a temporary file next to its target first. Decrypted files
are readable by all (0644, their directory 0755 where the executor may
change it), deliberately: the task's next executors may run as other users
than the decryption executor (e.g., images with a non-root user), and the
volume belongs to the task alone. Inputs
are only read, so they may be mounted read-only. If a key can't be read or
a file can't be decrypted (e.g., no key fits, or it is no Crypt4GH file),
the executor removes the files it decrypted and exits with status 1, so
that the task fails before its own executors run. Existing files at the
targets are replaced, not refused, so that a restarted executor (e.g., a
Kubernetes pod) runs again: the volume belongs to one task, whose
executors run once each, in order.

### The teardown executor

The teardown executor runs the same script, in the same image, with
`--clear` and the `volume`. It removes everything in the volume, but not
the volume itself (a mount point): the decrypted files, temporary files
that a stopped decryption may have left, and files that the task's
executors derived from the decrypted files there (e.g., an index written
next to its input). The volume is reserved for the plugin (no input,
output or volume of the task may be in it), so nothing there is the
task's result. Symbolic links in it are removed, not followed, and a
volume that is a link is refused. An empty or
missing volume is fine, so that a restarted executor runs again. If an
entry can't be removed, the executor removes the others and exits with
status 1, so that the task fails (`EXECUTOR_ERROR`) and the entries that
remain show in its logs; backends may then skip uploading the task's
outputs. Copies of the decrypted data that the task's executors write
outside the volume are not removed.

TES runs a task's executors in order and stops at the first that fails
(TES 1.1, `ignore_error`). So, by default, the teardown executor runs only
if all of the task's own executors succeed: after a failed executor, a
canceled task or a system error, the decrypted files stay in the volume
until the backend removes the task's working directory (see below). A
failed decryption removes its own files.

`teardown_on_error` sets `ignore_error: true` on the task's own executors
(not on the decryption and teardown executors), so that the teardown
executor runs also after one of them fails. It is off by default, as it
changes how the task runs and how it is reported, and not all backends
support it (as of the sources of Funnel and TESK in November 2026):

- The task's next executors run after one of them fails, also those that
  need the failed executor's results.
- The task may no longer end with `EXECUTOR_ERROR` when one of its own
  executors fails: TES says that the backend records the exit code and
  goes on. The final state differs between backends: the current sources
  of Funnel report the task as `COMPLETE` if the teardown succeeds, while
  Funnel 0.11.14 (the image that the [end-to-end test](#end-to-end-test)
  ran in November 2026) still ends it with `EXECUTOR_ERROR`, after running
  the teardown. Clients must check the exit codes in the executor logs.
- TESK accepts only `ignore_error: false` and refuses other tasks with
  `400 Bad Request`: don't set `teardown_on_error` with TESK backends.
- Backends that implement TES 1.0, which has no `ignore_error`, may ignore
  the field and stop at the first failed executor as before.
- Executors that set `ignore_error: false` themselves get `true`.

Without the teardown, or where it doesn't run, the decrypted files are
removed with the task's working directory, as far as the backend removes
it: Funnel removes it after the task unless its worker setting
`LeaveWorkDir` is set; TESK deletes the task's volume claim after the
task, unless `KEEP_FAILED_PVC` keeps that of tasks that don't complete.

### The decryption image

Each release of the plugin publishes its image on Docker Hub, as
[`elixircloud/foca-plugin-tes-data-crypt4gh-decrypt`](https://hub.docker.com/r/elixircloud/foca-plugin-tes-data-crypt4gh-decrypt),
tagged with the plugin's version (e.g., `0.1.0`; there is no `latest`).
Use the image of the plugin version that you install, and set `image` to
it by digest, which Docker Hub shows for the tag, as does:

```bash
docker buildx imagetools inspect docker.io/elixircloud/foca-plugin-tes-data-crypt4gh-decrypt:<version>
```

Before the release workflow signs the image (Sigstore keyless signing, with
the workflow's identity), attests its build provenance and tags it, it
scans the image for known vulnerabilities and runs its smoke test on it.
Verify the signature and the attestation before you use it:

```bash
image=docker.io/elixircloud/foca-plugin-tes-data-crypt4gh-decrypt@sha256:<digest>
cosign verify "${image}" \
  --certificate-identity-regexp '^https://github\.com/elixir-cloud-aai/foca-plugins/\.github/workflows/release\.yml@refs/tags/foca-plugin-tes-data-crypt4gh-v' \
  --certificate-oidc-issuer https://token.actions.githubusercontent.com
gh attestation verify "oci://${image}" --repo elixir-cloud-aai/foca-plugins
```

The image is built from [`image/Dockerfile`](image/Dockerfile) (Python 3.13,
`crypt4gh` from [`image/requirements.txt`](image/requirements.txt), pinned
with hashes, without pip, `linux/amd64`), with the plugin's directory as
the build context. To build it yourself (e.g., for a registry that your TES backends
can pull from, if they can't pull from Docker Hub), from the repository
root:

```bash
docker build -f plugins/tes/data/crypt4gh/image/Dockerfile \
  -t <registry>/<name>:<tag> plugins/tes/data/crypt4gh
```

CI builds the image for every pull request, runs
[`image/smoke.sh`](image/smoke.sh) on it and scans it. The image runs as
root, as some TES implementations (e.g., Funnel with host directories)
make the task's volumes writable for root only.

## Configuration

Admin settings, in the gateway app's configuration (FOCA's `plugins`
section), e.g., for proTES (overlays replace lists, so repeat the route
step of your pipeline; the decryption step comes first, as it changes the
task):

```yaml
plugins:
  available:
    tes.data.crypt4gh:
      distribution: foca-plugin-tes-data-crypt4gh
      config:
        # The image of the installed plugin version, by digest
        image: docker.io/elixircloud/foca-plugin-tes-data-crypt4gh-decrypt@sha256:<digest>
  pipeline:
    - id: decrypt
      alternatives:
        - plugin: tes.data.crypt4gh
    - id: route
      alternatives:
        - plugin: generic.routing.random
```

proTES's image doesn't contain the plugin: install it in an image derived
from that of proTES (see "Installing" in the docs, and proTES's "Plugin
images"). In proTES's Helm chart, set the same under `plugins.available`
and `plugins.pipeline`.

| Setting | Type | Default | Description |
| --- | --- | --- | --- |
| `config.image` | string, 1 to 512 characters, no spaces | none; required | image of the decryption executor (see [The decryption image](#the-decryption-image)) |
| `config.command` | 1 to 16 strings | `["crypt4gh-decrypt"]` | command of the decryption script in the image; the plugin appends its arguments |
| `config.volume` | absolute, normalized path below `/` | `/vol/crypt4gh` | volume of the decrypted files; tasks may not use it or paths in it |
| `config.encrypted_suffixes` | 1 to 16 suffixes (a dot and 1 to 32 characters, no `/` or spaces) | `[".c4gh"]` | suffixes of the names of Crypt4GH inputs; the longest that matches is removed from the decrypted file's name |
| `config.key_suffixes` | as `encrypted_suffixes`; none may end with one of them or the other way round (e.g., `.key.c4gh` and `.c4gh`) | `[".sec"]` | suffixes of the names of secret keys among the inputs |
| `config.teardown` | boolean | `true` | add the teardown executor, which empties the volume after the task's own executors (see [The teardown executor](#the-teardown-executor)) |
| `config.teardown_on_error` | boolean; needs `teardown` | `false` | set `ignore_error: true` on the task's own executors, so that the teardown executor runs after they fail; not for all backends (see [The teardown executor](#the-teardown-executor)) |

The plugin has no parameters.

## Keys and decrypted data

The plugin moves no data itself, but the tasks that it changes do:

- **Secret keys are inputs of the task**, as with the middleware: they are
  downloaded by the TES backend, available to all of the task's executors,
  and, if a client sends them as inline `content`, stored with the task by
  the gateway and the backend. Prefer keys made for a single task or
  dataset (re-encrypt the data for them), and pass them by URL from storage
  that only the task can read.
- **Decrypted files are written to the task's volume**, on the backend's
  disks. The teardown executor removes them, with everything else in the
  volume, after the task's own executors, if they all succeed (or with
  `teardown_on_error`, where the backend supports it); otherwise they are
  removed with the task's working directory, as far as the backend
  removes it (see [The teardown executor](#the-teardown-executor)). Removing a file doesn't overwrite
  its blocks on the disk, and copies that the task's executors make
  outside the volume stay.

Use the plugin only with TES backends that you trust with the decrypted
data and the keys, e.g., in a trusted research environment.

## Data transfers

None: the plugin calls no services, and the decryption executor, which
runs on the TES backend, needs no network.

## Changes from the middleware

The plugin replaces the Crypt4GH middleware of proTES
(`protes-middleware-crypt4gh`), which proTES can't load since it adopted
FOCA's plugin engine:

- Only inputs named as Crypt4GH files (`.c4gh`) are decrypted, and only
  keys named as keys (`.sec`) are passed to the executor; other inputs stay
  where they are, instead of being moved to the volume.
- Decrypted files lose their suffix (`reads.fq.c4gh` decrypts to
  `reads.fq`), are readable by all (0644), and are written to the volume;
  inputs are only read, so they may be mounted read-only.
- A failed decryption fails the task (exit status 1), instead of passing
  the encrypted file on.
- The volume is a setting (`/vol/crypt4gh`), not a random path per process,
  and tasks that use it are rejected (`422`), as are tasks with Crypt4GH
  inputs but no key.
- The image is a setting, built from this repository, instead of a fixed
  image.
- A teardown executor empties the volume after the task's own
  executors (`teardown`).

## Installation

```bash
pip install foca-plugin-tes-data-crypt4gh
```

See "Installing" in the docs for release trains and derived images.

## Development

From the repository root, with the development environment set up (see
"Contributing" in the docs):

```bash
uv run ruff check plugins/tes/data/crypt4gh
uv run python -m scripts.packages mypy plugins/tes/data/crypt4gh
uv run coverage run --source plugins/tes/data/crypt4gh/src \
  -m pytest plugins/tes/data/crypt4gh
uv run coverage report
```

The tests make their keys and Crypt4GH files with `crypt4gh` (a
development dependency), in temporary directories: the repository holds no
keys. With Docker, build the image and run its smoke test as CI does:

```bash
docker build -f plugins/tes/data/crypt4gh/image/Dockerfile \
  -t crypt4gh-decrypt:dev plugins/tes/data/crypt4gh
plugins/tes/data/crypt4gh/image/smoke.sh crypt4gh-decrypt:dev
```

### End-to-end test

[`e2e/check.sh`](e2e/check.sh) runs the plugin end to end, as CI does (job
`e2e`): proTES (a release image, with the plugin's wheel built from the
checkout) sends tasks to Funnel, a real TES backend, in Docker Compose
([`e2e/compose.yaml`](e2e/compose.yaml); the versions are pinned in
`e2e/Dockerfile.protes` and `e2e/Dockerfile.funnel`). Their inputs, a Crypt4GH
file and a secret key made in the test with the decryption image (also built
from the checkout), are served over HTTP, and Funnel downloads them as from
any server. It checks that:

1. a task completes, its executor reads the decrypted content, and the
   teardown executor leaves the volume of the decrypted files empty;
2. a failing decryption (no key for one of two files) fails the task before
   its own executors run, and leaves no decrypted files;
3. without `teardown_on_error`, a failing executor of the task stops it
   before the teardown, and the decrypted file stays in the volume (until
   the backend removes the task's working directory, see [The teardown
   executor](#the-teardown-executor));
4. with `teardown_on_error`, the teardown runs after the failing executor
   and empties the volume (the task's final state is the backend's, so
   either `EXECUTOR_ERROR` or `COMPLETE` passes).

Funnel keeps the tasks' working directories in the test, so that it can
check the volume on the host. TESK refuses tasks with `ignore_error: true`,
so `teardown_on_error` is tested with Funnel only, and the test has no TESK
variant. It needs Docker with Compose, uv, curl and jq (without them, or
with a bad work directory, it exits with status 2 before starting
anything), but no credentials:

```bash
plugins/tes/data/crypt4gh/e2e/check.sh
```

It removes the stack, the task containers and its work directory when it
ends; see "Contributing" in the docs for its settings.

## Changelog

See [`CHANGELOG.md`](CHANGELOG.md).
