From f74b98d4eeca1abad3cb70d4e1574ebf9befae03 Mon Sep 17 00:00:00 2001 From: Usman Saleem Date: Tue, 9 Jul 2024 18:06:37 +1000 Subject: [PATCH] feat: Add Dockerfile to test the plugin. (#10) * Dockerfile to test the plugin * Use constant for SECP256K1 curve * cleanup docs --- .dockerignore | 10 +++ .gitignore | 4 + Dockerfile | 44 ++++++++++ README.md | 57 ++++++++++-- docker/clean_volumes.sh | 3 + docker/scripts/entrypoint.sh | 88 +++++++++++++++++++ docker/volumes/config/besu-dev.toml | 19 ++++ docker/volumes/config/pkcs11-hsm-password.txt | 1 + docker/volumes/config/pkcs11-softhsm.cfg | 11 +++ .../besu/plugin/softhsm/Pkcs11HsmPlugin.java | 17 ++-- .../softhsm/Pkcs11PluginCliOptions.java | 2 +- .../softhsm/Pkcs11SecurityModuleService.java | 29 ++++-- .../usmans/besu/plugin/softhsm/SECP256K1.java | 29 ++++++ .../besu/plugin/softhsm/SignatureUtil.java | 35 ++++++-- 14 files changed, 321 insertions(+), 28 deletions(-) create mode 100644 .dockerignore create mode 100644 Dockerfile create mode 100755 docker/clean_volumes.sh create mode 100644 docker/scripts/entrypoint.sh create mode 100644 docker/volumes/config/besu-dev.toml create mode 100644 docker/volumes/config/pkcs11-hsm-password.txt create mode 100644 docker/volumes/config/pkcs11-softhsm.cfg create mode 100644 src/main/java/info/usmans/besu/plugin/softhsm/SECP256K1.java diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..e783d81 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,10 @@ +# Copyright 2024, Usman Saleem. +# SPDX-License-Identifier: (Apache-2.0 OR MIT) + +# Exclude everything +* + +# Include specific files and directories needed for the build +!docker/scripts/entrypoint.sh +!Dockerfile +!build/libs/ \ No newline at end of file diff --git a/.gitignore b/.gitignore index ab5e240..82718d3 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,7 @@ build .idea + +# Ignore data and tokens in volume directory +docker/volumes/data +docker/volumes/tokens diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..6c93d13 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,44 @@ +# syntax=docker/dockerfile:1 +# Copyright 2024, Usman Saleem. +# SPDX-License-Identifier: (Apache-2.0 OR MIT) + +# Start from the latest Hyperledger Besu image +FROM hyperledger/besu:latest + +# Switch to root to install packages +USER 0 + +# Install additional packages for SoftHSM2 and OpenSC +RUN apt-get update && \ + apt-get install -y --no-install-recommends \ + openssl \ + libssl3 \ + softhsm2 \ + opensc \ + gnutls-bin && \ + apt-get clean && \ + rm -rf /var/lib/apt/lists/* + +# Create a directory for SoftHSM2 tokens. This can be overridden using a volume mount to persist. +RUN mkdir -p /var/lib/tokens && chmod 755 /var/lib/tokens && chown besu:besu /var/lib/tokens + +# Switch back to the besu user +USER besu + +# Update workdir to Besu home directory +WORKDIR /opt/besu + +# Set environment variables for SoftHSM2 configuration +ENV SOFTHSM2_CONF=/opt/besu/softhsm2.conf + +# Copy the PKCS11 plugin JAR to the plugins directory +COPY --chown=besu:besu ./build/libs/besu-pkcs11-plugin-*.jar ./plugins/ + +# Copy the initialization script +COPY --chown=besu:besu --chmod=755 ./docker/scripts/entrypoint.sh ./entrypoint.sh + +# Create a custom SoftHSM2 configuration file in besu home directory +RUN echo "directories.tokendir = /var/lib/tokens" > ./softhsm2.conf + +# Set the entrypoint to our new script +ENTRYPOINT ["/opt/besu/entrypoint.sh"] \ No newline at end of file diff --git a/README.md b/README.md index 5846c0e..1ec150a 100644 --- a/README.md +++ b/README.md @@ -37,14 +37,61 @@ The plugin jar will be available at `build/libs/besu-pkcs11-plugin-.jar Drop the `besu-pkcs11-plugin-.jar` in the `/plugins` folder under Besu installation. This plugin will expose following additional cli options: -`TBA` +```shell +--plugin-pkcs11-hsm-config-path= + Path to the PKCS11 configuration file +--plugin-pkcs11-hsm-key-alias=