Python library for handling containerized PHT trains
Project description
🚆 Train Container Library
Python library for validating and interacting with pht-train images/containers.
Installation
pip install pht-train-container-library
Setup development environment
Make sure you have poetry and pre-commit installed.
Install the dependencies and pre-commit hooks:
poetry install --with dev
poetry run pre-commit install
Run tests
poetry run pytest
Linting and formatting
These commands are also run as pre-commit hooks.
Linting with ruff:
poetry run ruff . --fix
Formatting with black:
poetry run black .
Security Protocol
The pht security protocol adapted from docs/Secure_PHT_latest__official.pdf
performs two main tasks:
- Before executing a train-image on the local machine, unless the station is the first station on the route, the
previous results need to be decrypted and the content of the image needs to be validated based on the configuration
of the individual train ->
pre-run
. - After executing the train the updated results need to be encrypted and the train configuration needs to be updated to
reflect the current state ->
post-run
.
Train image structure
To ensure the protocol is working correctly train docker images are required to keep the following structure:
/opt/train_config.json
: Stores the configuration file of the train./opt/pht_train/
: Stores all the files containing code or other things required for the train algorithm to run. The contents of this directory can never change and is validated by thepre-run
step./opt/pht_results/
: Stores the results of the train. Which will be decrypted in thepre-run
step and encrypted in thepost-run
step.
No files in the image outside the /opt/pht_results/
directory should change during the execution of the algorithm.
Usage - Python Script
To use the protocol in your own python application, after installing the library
with pip install pht-train-container-library
an instance of the protocol can be to validate docker images as follows:
from train_lib.security.protocol import SecurityProtocol
from train_lib.docker_util.docker_ops import extract_train_config
image_name = '<image-repo>:<image-tag>'
station_id = '<station-id>'
# Get the train configuration from the image
config = extract_train_config(image_name)
# Initialize the protocol with the extracted config and station_id
protocol = SecurityProtocol(station_id=station_id, config=config)
# execute one of the protocol steps
protocol.pre_run_protocol(image_name, private_key_path='<path-to-private-key>')
# protocol.post_run_protocol(image_name, private_key_path='<path-to-private-key>')
Usage - Container
A containerized version of the protocol is also available it can be used with the following command:
docker run -e STATION_ID=<station_id> -e PRIVATE_KEY_PATH=/opt/private_key.pem -v /var/run/docker.sock:/var/run/docker.sock -v <path_to_your_key>:/opt/private_key.pem ghcr.io/pht-medic/protocol <pre-run/post-run> <image-repo>:<image-tag>
STATION_ID
and PRIVATE_KEY_PATH
are required to be set in the environment variables. As well as passing the docker
socket /var/run/docker.sock
to the container as a volume to enable docker-in-docker functionality.
Pre-run protocol
The pre-run protocol consists of the following steps
- The train files are decrypted using an encrypted symmetric key that is decrypted using the provided station private key
- The hash of the immutable files (train definition) is verified making sure that the executable files did not change during the train definition.
- The build signature is verified ensuring that this image was build by the given user
- The digital signature is verified ensuring the correctness of the results at each stop of the train.
- The symmetric key is decrypted using the provided station private key
- The mutable files in
/opt/pht_results
are decrypted using the symmetric key obtained in the previous step - The decrypted files are hashed and the hash is compared to the one stored in the train configuration file.
Once these steps have been completed the image is ready to be executed.
Post-run protocol
- Calculate the hash of the newly generated results
- Sign the hash of the results using the provided
PRIVATE_KEY_PATH
- Update the the train signature using the session id that is randomly generated at each execution step
- Encrypt the resulting files using a newly generated symmetric key
- Encrypt the generated symmetric key with the public keys of the train participants
- Encrypt the train files using the symmetric key
- Update the train configuration file and train image
With the completion of these steps the train is ready to be pushed into the registry for further processing
Tests
Run the tests to validate the security protocol is working as intended. From this projects root directory run
poetry run pytest train_lib
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file pht_train_container_library-2.0.0a2.tar.gz
.
File metadata
- Download URL: pht_train_container_library-2.0.0a2.tar.gz
- Upload date:
- Size: 64.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.3.1 CPython/3.9.9 Windows/10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 215f161a20385eccff435041586ae78d7ca920ea29b65f79115fe7a6b2730eca |
|
MD5 | 5845fa31da254b5876b7601c4e84cdf1 |
|
BLAKE2b-256 | 84c9d7c74ab30437061f3f961294864e2777ce345e52d24f86a3686f3e57f947 |
File details
Details for the file pht_train_container_library-2.0.0a2-py3-none-any.whl
.
File metadata
- Download URL: pht_train_container_library-2.0.0a2-py3-none-any.whl
- Upload date:
- Size: 69.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.3.1 CPython/3.9.9 Windows/10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9beab54870652a8b04f531ad5a40df17a4d15ab23b6725222b631dd60b5d5197 |
|
MD5 | 76a22180d424a851ef865a04c11eb312 |
|
BLAKE2b-256 | b37e38ffc1613bc4fbe46f4456cd9d39062b25727b3ecf4583e95a5a44f53450 |