oarepo-sensitive-common 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,11 @@
1
+ name: Release
2
+
3
+ on:
4
+ release:
5
+ types: [released, prereleased]
6
+
7
+ jobs:
8
+ bump-test-publish:
9
+ name: Bump, Test, and Publish the Release
10
+ uses: oarepo/oarepo/.github/workflows/release.yaml@main
11
+ secrets: inherit
@@ -0,0 +1,17 @@
1
+ # Copyright (c) 2025 CESNET z.s.p.o.
2
+ #
3
+ # OARepo is free software; you can redistribute it and/or modify
4
+ # it under the terms of the MIT License; see LICENSE file for more details.
5
+
6
+ name: Tests
7
+
8
+ on:
9
+ push: {}
10
+ schedule:
11
+ - cron: "0 1 * * 6" # Every Sunday at 01:00 UTC
12
+ workflow_dispatch: {}
13
+
14
+ jobs:
15
+ Tests:
16
+ uses: oarepo/oarepo/.github/workflows/test.yaml@main
17
+ secrets: inherit
@@ -0,0 +1,18 @@
1
+ .venv/
2
+ __pycache__/
3
+ .pytest_cache/
4
+ .ruff_cache/
5
+ dist/
6
+ .tools/
7
+ .env-services
8
+ coverage.json
9
+ htmlcov/
10
+ .coverage
11
+
12
+ # UV package management (added by oarepo-cli)
13
+ uv.lock
14
+ # Auto-generated docs scaffold (regenerated by `oarepo-cli library docs`)
15
+ /docs-build/
16
+ /mkdocs.yml
17
+ build/
18
+ .tmp-mkdocs/
@@ -0,0 +1,63 @@
1
+ target-version = "py314"
2
+ line-length = 120
3
+ indent-width = 4
4
+
5
+ extend-exclude = ["**/alembic"]
6
+
7
+ [lint]
8
+ select = [ "ALL" ]
9
+ ignore = [
10
+ "FIX002", # exclude TODO comments
11
+ "TD003", # Missing issue link for this TODO
12
+ "TD002", # Missing author in TODO
13
+ "N806", # Variable name should be lowercase as we dynamically create classes
14
+ "ANN204", # Missing return type annotation for __init__
15
+ "ANN401", # Any in *args/**kwargs
16
+ "TRY003", # Avoid long exception messages
17
+ "TRY300", # Do not force moving return to except/else block
18
+ "EM101", # Avoid using string literal in exception
19
+ "EM102", # Avoid using f-string literal in exception
20
+ "TRY301", # Avoid raising/catching the same exception type
21
+ "PLC0415", # Place imports to the top of the file
22
+ "PGH004", # Use specific noqa for pylint
23
+ "TID252", # Prefer absolute imports
24
+ "D203", # Using D211
25
+ "D213", # Using D212 (multi-line-summary-first-line) instead
26
+ "COM812",
27
+ "FBT001", # Avoid using boolean function parameters
28
+ "FBT002", # Avoid using boolean function parameters
29
+ ]
30
+
31
+ [lint.pep8-naming]
32
+ classmethod-decorators = [
33
+ "classmethod",
34
+ "declared_attr", # sqlalchemy declared_attr is a classmethod decorator
35
+ ]
36
+
37
+ [lint.per-file-ignores]
38
+ "__init__.py" = ["E402"]
39
+ "**/{tests,docs,tools}/*" = [
40
+ "E402",
41
+ "S101",
42
+ "ANN001",
43
+ "ANN002",
44
+ "ANN202",
45
+ "ARG001",
46
+ "ARG002",
47
+ "ANN003",
48
+ "SLF001",
49
+ "ARG005",
50
+ "D103",
51
+ "ANN201",
52
+ "D100",
53
+ "INP",
54
+ "PLR",
55
+ "PLC"
56
+ ]
57
+
58
+ [lint.flake8-copyright]
59
+ notice-rgx = "SPDX-License-Identifier: MIT"
60
+
61
+ [format]
62
+ docstring-code-format = true
63
+ docstring-code-line-length = 40
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (C) 2026 CESNET z.s.p.o.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
9
+ of the Software, and to permit persons to whom the Software is furnished to do
10
+ so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,107 @@
1
+ Metadata-Version: 2.5
2
+ Name: oarepo-sensitive-common
3
+ Version: 0.1.0
4
+ Summary: What the sensitive-data parts must do the same way: signed messages, their format, configuration helpers
5
+ Author: CESNET z.s.p.o.
6
+ License-Expression: MIT
7
+ License-File: LICENSE
8
+ Requires-Python: >=3.14
9
+ Requires-Dist: cryptography>=44
10
+ Requires-Dist: pyjwt<3,>=2.10
11
+ Provides-Extra: oarepo-ci
12
+ Requires-Dist: oarepo<15,>=14; extra == 'oarepo-ci'
13
+ Provides-Extra: tests
14
+ Requires-Dist: pytest>=8; extra == 'tests'
15
+ Description-Content-Type: text/markdown
16
+
17
+ # oarepo-sensitive-common
18
+
19
+ What the three parts of sensitive-data support must do the same way: the
20
+ repository (`oarepo-sensitive-data`), the ENCRES (`oarepo-encres`) and the HSM
21
+ gateway (`oarepo-c4gh-hsm-gateway`).
22
+
23
+ | Module | Contents |
24
+ |---|---|
25
+ | `jws` | `sign()` and `verify()`: every signed token, EdDSA compact JWS on PyJWT, with the shared rules (size cap, header fields, `typ`, `kid`, `aud`, `exp`, clock allowance) and fixed, loggable refusal reasons |
26
+ | `messages` | the format between the parts: message types (`typ`), audit ids, key-use grants (`grant_claims()`, `check_grant()`) |
27
+ | `config` | `Table` (one TOML table, unknown settings refused, errors naming the setting), `check_issuer_audience()`, `KEY_ID_RE`, Ed25519 PEM key files |
28
+
29
+ Dependencies: `cryptography` and `PyJWT` only, so the HSM gateway stays minimal.
30
+
31
+ ## Concepts
32
+
33
+ **The parts.** Sensitive files are stored encrypted with Crypt4GH, for the
34
+ repository's key.
35
+
36
+ - The **repository** (an Invenio extension) stores the files and decides who
37
+ may read them.
38
+ - The **ENCRES** (encryption and re-encryption service) checks each upload
39
+ (that it decrypts with the repository's key) and rebuilds a file's Crypt4GH
40
+ header for each requester. The data blocks stay as they are: the requester
41
+ downloads them from storage directly.
42
+ - The **HSM gateway** holds the repository's private keys and does only one
43
+ thing with them: the key agreement (ECDH) that opening a header needs.
44
+
45
+ They are separate services and trust each other only through signed tokens.
46
+
47
+ **Signed tokens.** Every token is a compact JWS signed with Ed25519 (`jws`), and
48
+ each kind has its own `typ` (`messages`):
49
+
50
+ | Token | From → to | `typ` |
51
+ |---|---|---|
52
+ | Key-use grant | repository → HSM gateway (carried by the ENCRES) | `hsm-grant+jwt` |
53
+ | Check request / result | repository → ENCRES / ENCRES → repository | `encres-check+jwt` / `encres-check-result+jwt` |
54
+ | Resolve request / resolution | ENCRES → repository / repository → ENCRES | `encres-resolve+jwt` / `encres-resolution+jwt` |
55
+
56
+ A receiver trusts a fixed set of the sender's public keys, chosen by `kid`;
57
+ two are configured while a key is being replaced.
58
+
59
+ **Key-use grants.** The HSM gateway does an ECDH only with a grant: a token from
60
+ the repository that names one operation (`rid`, its audit id) and the writer
61
+ points (`pts`) of the one file it is for, valid for a few minutes. So a
62
+ compromised ENCRES can't open the headers of other files. `grant_claims()`
63
+ builds one; `check_grant()` checks a verified one against the request.
64
+
65
+ **Audit ids** name one operation in the logs of all three parts:
66
+ `chk:<file id>` for an integrity check, `dl:<ticket id>` for a download,
67
+ `encres:public-key` for reading a key's public key. The ENCRES sends it to the
68
+ HSM gateway as `X-Request-Id`, and the grant must carry the same one.
69
+
70
+ **Identities** (`iss`, `aud`) are https URLs, one per part and instance, e.g.
71
+ `https://encres.example.org`. They are compared as exact strings, so
72
+ `check_issuer_audience()` accepts one spelling only: lowercase, no trailing
73
+ slash, query, fragment or user. A URL here names a party; nothing is fetched
74
+ from it.
75
+
76
+ **Key files.** Key material is only ever in files that a configuration names,
77
+ never pasted into it. Ed25519 keys are PEM files, as `openssl genpkey` and
78
+ `openssl pkey -pubout` write them.
79
+
80
+ **Versions.** The parts are deployed separately, so this package's version
81
+ says which of them understand each other. A change to `jws` or `messages`
82
+ changes the format between the parts: a new minor version, and all three are
83
+ updated together. An incompatible change is a new major version.
84
+
85
+ ## Develop
86
+
87
+ The same tooling as the other oarepo libraries (`run.sh`, which wraps `oarepo-cli library`):
88
+
89
+ ```sh
90
+ ./run.sh venv # .venv with the "tests" extra
91
+ ./run.sh test --skip-services # no services needed
92
+ ./run.sh lint # ruff (fix), ty, license headers; ./run.sh check doesn't fix
93
+ ./run.sh docs # API documentation into build/docs
94
+ ```
95
+
96
+ Keys in the tests are generated per run and deleted with their test.
97
+
98
+ ## Release
99
+
100
+ As for the other oarepo libraries: the workflows in `.github/workflows` call the
101
+ shared ones in `oarepo/oarepo`. Publishing a GitHub release `vX.Y.Z` runs the
102
+ tests and lint, writes the version into `pyproject.toml` and commits it, builds
103
+ the package and uploads it to PyPI. A new major version is refused there: change
104
+ it in `pyproject.toml` first.
105
+
106
+ The `oarepo-ci` extra in `pyproject.toml` only gives the shared workflows an
107
+ OARepo version for their matrix; it is never installed.
@@ -0,0 +1,91 @@
1
+ # oarepo-sensitive-common
2
+
3
+ What the three parts of sensitive-data support must do the same way: the
4
+ repository (`oarepo-sensitive-data`), the ENCRES (`oarepo-encres`) and the HSM
5
+ gateway (`oarepo-c4gh-hsm-gateway`).
6
+
7
+ | Module | Contents |
8
+ |---|---|
9
+ | `jws` | `sign()` and `verify()`: every signed token, EdDSA compact JWS on PyJWT, with the shared rules (size cap, header fields, `typ`, `kid`, `aud`, `exp`, clock allowance) and fixed, loggable refusal reasons |
10
+ | `messages` | the format between the parts: message types (`typ`), audit ids, key-use grants (`grant_claims()`, `check_grant()`) |
11
+ | `config` | `Table` (one TOML table, unknown settings refused, errors naming the setting), `check_issuer_audience()`, `KEY_ID_RE`, Ed25519 PEM key files |
12
+
13
+ Dependencies: `cryptography` and `PyJWT` only, so the HSM gateway stays minimal.
14
+
15
+ ## Concepts
16
+
17
+ **The parts.** Sensitive files are stored encrypted with Crypt4GH, for the
18
+ repository's key.
19
+
20
+ - The **repository** (an Invenio extension) stores the files and decides who
21
+ may read them.
22
+ - The **ENCRES** (encryption and re-encryption service) checks each upload
23
+ (that it decrypts with the repository's key) and rebuilds a file's Crypt4GH
24
+ header for each requester. The data blocks stay as they are: the requester
25
+ downloads them from storage directly.
26
+ - The **HSM gateway** holds the repository's private keys and does only one
27
+ thing with them: the key agreement (ECDH) that opening a header needs.
28
+
29
+ They are separate services and trust each other only through signed tokens.
30
+
31
+ **Signed tokens.** Every token is a compact JWS signed with Ed25519 (`jws`), and
32
+ each kind has its own `typ` (`messages`):
33
+
34
+ | Token | From → to | `typ` |
35
+ |---|---|---|
36
+ | Key-use grant | repository → HSM gateway (carried by the ENCRES) | `hsm-grant+jwt` |
37
+ | Check request / result | repository → ENCRES / ENCRES → repository | `encres-check+jwt` / `encres-check-result+jwt` |
38
+ | Resolve request / resolution | ENCRES → repository / repository → ENCRES | `encres-resolve+jwt` / `encres-resolution+jwt` |
39
+
40
+ A receiver trusts a fixed set of the sender's public keys, chosen by `kid`;
41
+ two are configured while a key is being replaced.
42
+
43
+ **Key-use grants.** The HSM gateway does an ECDH only with a grant: a token from
44
+ the repository that names one operation (`rid`, its audit id) and the writer
45
+ points (`pts`) of the one file it is for, valid for a few minutes. So a
46
+ compromised ENCRES can't open the headers of other files. `grant_claims()`
47
+ builds one; `check_grant()` checks a verified one against the request.
48
+
49
+ **Audit ids** name one operation in the logs of all three parts:
50
+ `chk:<file id>` for an integrity check, `dl:<ticket id>` for a download,
51
+ `encres:public-key` for reading a key's public key. The ENCRES sends it to the
52
+ HSM gateway as `X-Request-Id`, and the grant must carry the same one.
53
+
54
+ **Identities** (`iss`, `aud`) are https URLs, one per part and instance, e.g.
55
+ `https://encres.example.org`. They are compared as exact strings, so
56
+ `check_issuer_audience()` accepts one spelling only: lowercase, no trailing
57
+ slash, query, fragment or user. A URL here names a party; nothing is fetched
58
+ from it.
59
+
60
+ **Key files.** Key material is only ever in files that a configuration names,
61
+ never pasted into it. Ed25519 keys are PEM files, as `openssl genpkey` and
62
+ `openssl pkey -pubout` write them.
63
+
64
+ **Versions.** The parts are deployed separately, so this package's version
65
+ says which of them understand each other. A change to `jws` or `messages`
66
+ changes the format between the parts: a new minor version, and all three are
67
+ updated together. An incompatible change is a new major version.
68
+
69
+ ## Develop
70
+
71
+ The same tooling as the other oarepo libraries (`run.sh`, which wraps `oarepo-cli library`):
72
+
73
+ ```sh
74
+ ./run.sh venv # .venv with the "tests" extra
75
+ ./run.sh test --skip-services # no services needed
76
+ ./run.sh lint # ruff (fix), ty, license headers; ./run.sh check doesn't fix
77
+ ./run.sh docs # API documentation into build/docs
78
+ ```
79
+
80
+ Keys in the tests are generated per run and deleted with their test.
81
+
82
+ ## Release
83
+
84
+ As for the other oarepo libraries: the workflows in `.github/workflows` call the
85
+ shared ones in `oarepo/oarepo`. Publishing a GitHub release `vX.Y.Z` runs the
86
+ tests and lint, writes the version into `pyproject.toml` and commits it, builds
87
+ the package and uploads it to PyPI. A new major version is refused there: change
88
+ it in `pyproject.toml` first.
89
+
90
+ The `oarepo-ci` extra in `pyproject.toml` only gives the shared workflows an
91
+ OARepo version for their matrix; it is never installed.
@@ -0,0 +1,37 @@
1
+ [project]
2
+ name = "oarepo-sensitive-common"
3
+ version = "0.1.0"
4
+ description = "What the sensitive-data parts must do the same way: signed messages, their format, configuration helpers"
5
+ readme = "README.md"
6
+ license = "MIT"
7
+ license-files = ["LICENSE"]
8
+ authors = [{ name = "CESNET z.s.p.o." }]
9
+ requires-python = ">=3.14"
10
+ # Only these: the HSM gateway, the most sensitive part, depends on this package and stays minimal.
11
+ dependencies = [
12
+ "cryptography>=44",
13
+ "pyjwt>=2.10,<3",
14
+ ]
15
+
16
+ [project.optional-dependencies]
17
+ tests = [
18
+ "pytest>=8",
19
+ ]
20
+ # Only feeds the CI matrix of the shared oarepo workflows: oarepo-cli needs an
21
+ # OARepo version to work with, and finds it in an "oarepo" requirement. This
22
+ # package doesn't use OARepo, and ./run.sh never installs this extra (it
23
+ # installs only "dev", "tests" and "oarepo<N>"). Drop it once oarepo-cli
24
+ # supports libraries without OARepo.
25
+ oarepo-ci = [
26
+ "oarepo>=14,<15",
27
+ ]
28
+
29
+ [build-system]
30
+ requires = ["hatchling"]
31
+ build-backend = "hatchling.build"
32
+
33
+ [tool.hatch.build.targets.wheel]
34
+ packages = ["src/oarepo_sensitive_common"]
35
+
36
+ [tool.pytest.ini_options]
37
+ testpaths = ["tests"]
@@ -0,0 +1,70 @@
1
+ #!/usr/bin/env bash
2
+ # SPDX-FileCopyrightText: 2026 CESNET z.s.p.o.
3
+ # SPDX-License-Identifier: MIT
4
+ #
5
+ # Wrapper script for oarepo-cli library commands.
6
+ # Copy this file as "run.sh" to your library project root.
7
+ #
8
+ # This script:
9
+ # - Sets up a local .tools/venv (using uv) on first run
10
+ # - Installs oarepo-cli into that venv
11
+ # - Forwards all arguments to "oarepo-cli library <args>"
12
+ #
13
+ # Usage:
14
+ # ./run.sh venv # runs: oarepo-cli library venv
15
+ # ./run.sh test # runs: oarepo-cli library test
16
+ # ./run.sh self-update # removes .tools/venv and reinstalls oarepo-cli
17
+ #
18
+
19
+ set -euo pipefail
20
+
21
+ # OARepo packages and the CESNET-patched invenio-cli build (which oarepo-cli
22
+ # requires) live in the CESNET GitLab PyPI registry, not on public PyPI. uv
23
+ # gives an extra index higher priority than the default index, so invenio-cli
24
+ # resolves to the patched build here. Overridable via the environment.
25
+ export UV_EXTRA_INDEX_URL="${UV_EXTRA_INDEX_URL:-https://gitlab.cesnet.cz/api/v4/projects/1408/packages/pypi/simple}"
26
+ export PIP_EXTRA_INDEX_URL="${PIP_EXTRA_INDEX_URL:-https://gitlab.cesnet.cz/api/v4/projects/1408/packages/pypi/simple}"
27
+
28
+ SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
29
+ TOOLS_DIR="${SCRIPT_DIR}/.tools"
30
+ VENV_DIR="${TOOLS_DIR}/venv"
31
+ OAREPO_CLI="${VENV_DIR}/bin/oarepo-cli"
32
+
33
+ # Handle self-update: remove venv and reinstall
34
+ if [[ "${1:-}" == "self-update" ]]; then
35
+ echo "🔄 Self-updating oarepo-cli..." >&2
36
+ if [[ -d "${TOOLS_DIR}" ]]; then
37
+ echo " Removing ${TOOLS_DIR}..." >&2
38
+ rm -rf "${TOOLS_DIR}"
39
+ fi
40
+ shift # Remove "self-update" from arguments
41
+ fi
42
+
43
+ # Check if we need to set up the venv
44
+ if [[ ! -d "${TOOLS_DIR}" ]] || [[ ! -f "${OAREPO_CLI}" ]]; then
45
+ echo "🔧 Setting up oarepo-cli (first run or after self-update)..." >&2
46
+
47
+ # Check if uv is available
48
+ if ! command -v uv &> /dev/null; then
49
+ echo "❌ Error: 'uv' is not installed or not in PATH." >&2
50
+ echo " Install it with: pip install uv" >&2
51
+ echo " Or visit: https://github.com/astral-sh/uv" >&2
52
+ exit 1
53
+ fi
54
+
55
+ # Create tools directory if needed
56
+ mkdir -p "${TOOLS_DIR}"
57
+
58
+ # Create venv with uv
59
+ echo " Creating virtual environment in ${VENV_DIR}..." >&2
60
+ uv venv "${VENV_DIR}" --python 3.14
61
+
62
+ # Install oarepo-cli
63
+ echo " Installing oarepo-cli..." >&2
64
+ uv pip install --python "${VENV_DIR}/bin/python" oarepo-cli
65
+
66
+ echo "✅ oarepo-cli setup complete!" >&2
67
+ fi
68
+
69
+ # Run oarepo-cli with "library" subcommand and forwarded arguments
70
+ exec "${OAREPO_CLI}" library "$@"
@@ -0,0 +1,19 @@
1
+ # SPDX-FileCopyrightText: 2026 CESNET z.s.p.o.
2
+ # SPDX-License-Identifier: MIT
3
+
4
+ """Common tasks which the repository, the ENCRES (re-encryption service) and the HSM gateway must do the same way.
5
+
6
+ - :mod:`.jws`: signing and checking every signed token (EdDSA compact JWS);
7
+ - :mod:`.messages`: the format the parts agree on (types, audit ids, grants);
8
+ - :mod:`.config`: configuration helpers (TOML tables, identities, key files).
9
+
10
+ The three parts are separate services: the repository (an Invenio extension)
11
+ keeps the files and decides who may read them; the ENCRES re-encrypts the
12
+ Crypt4GH headers of uploaded files for each requester and checks uploads; the
13
+ HSM gateway holds the repository's private keys and does only the key agreement
14
+ (ECDH) for the ENCRES. They trust each other only through signed tokens, so the
15
+ rules for making and checking those tokens live here, once. The README explains
16
+ the terms.
17
+ """
18
+
19
+ from __future__ import annotations