From ec27e85c72e29a2a11fb37e70260b5615d96b459 Mon Sep 17 00:00:00 2001 From: nb-tech5 <115477424+nb-tech5@users.noreply.github.com> Date: Mon, 6 Oct 2025 11:27:44 +0100 Subject: [PATCH 1/2] feat: adds cache on file object generation check --- .github/workflows/ci.yml | 8 +++- src/lib/common/log.h | 2 +- src/lib/object_store/Generation.cpp | 68 +++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 2e7ec8ab8..3e98c085c 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -56,7 +56,7 @@ jobs: linux_ossl_30: name: Linux with OpenSSL 3.0 - runs-on: ubuntu-24.04 + runs-on: ubuntu-20.04 steps: - uses: actions/checkout@v4 - name: Prepare @@ -74,6 +74,9 @@ jobs: - name: Test run: | make check || (find . -name test-suite.log -exec cat {} \; && false) + - uses: actions/upload-artifact@v4 + with: + path: src/lib/.lib/*.so macos: name: macOS (${{ matrix.backend }}) @@ -148,3 +151,6 @@ jobs: CTEST_OUTPUT_ON_FAILURE: 1 run: | cmake --build build --target RUN_TESTS + - uses: actions/upload-artifact@v4 + with: + path: build/src/lib/Debug diff --git a/src/lib/common/log.h b/src/lib/common/log.h index cf91aafb3..554add12e 100644 --- a/src/lib/common/log.h +++ b/src/lib/common/log.h @@ -47,7 +47,7 @@ /* #define SOFTHSM_LOG_FUNCTION_NAME */ /* Define this symbol (either here or in the build setup) to log to stderr */ -/* #define DEBUG_LOG_STDERR */ +#define DEBUG_LOG_STDERR /* Logging errors */ #ifndef _WIN32 diff --git a/src/lib/object_store/Generation.cpp b/src/lib/object_store/Generation.cpp index cb858796c..c09264335 100644 --- a/src/lib/object_store/Generation.cpp +++ b/src/lib/object_store/Generation.cpp @@ -34,6 +34,11 @@ #include "log.h" #include "Generation.h" +#include +#include +#include +#include + // Factory Generation* Generation::create(const std::string path, int umask, bool isToken) { @@ -85,6 +90,55 @@ bool Generation::sync(File &objectFile) return objectFile.seek(0L); } +std::string getenv_string(const char *name, const char *defaultValue /* = NULL */) +{ +#ifdef _WIN32 + size_t valueSize = 0; + char buffer[256]; + getenv_s(&valueSize, buffer, 256, name); + buffer[valueSize] = '\0'; + const char *pPath = buffer; +#else + const char *pPath = getenv(name); +#endif + if (pPath == nullptr || pPath[0] == '\0') + { + if(defaultValue == NULL) + { + std::stringstream msg("Missing env var: "); + msg << name; + throw std::logic_error(msg.str()); + } + else + { + return std::string(defaultValue); + } + } + return std::string(pPath); +} + +long stringToLong(const std::string value) +{ + std::istringstream str(value); + long result; + str >> result; + if (!str) { + if (result == std::numeric_limits::max()) { + throw std::logic_error("Overflow!"); + } else if (result == std::numeric_limits::min()) { + throw std::logic_error("Underflow!"); + } else { + throw std::logic_error("Some other parse error"); + } + } + return result; +} + +bool cacheGeneration = getenv_string("SOFTHSM2_OBJECT_GENERATION_CACHE_ENABLE", "false") == "true"; +std::chrono::milliseconds cacheGenerationPeriodMs(stringToLong(getenv_string("SOFTHSM2_OBJECT_GENERATION_CACHE_PERIOD", "1000"))); +std::chrono::milliseconds cacheGenerationLastUpdate(0); +std::mutex cacheGenerationMutex; + // Check if the target was updated bool Generation::wasUpdated() { @@ -118,6 +172,15 @@ bool Generation::wasUpdated() } else { + if (cacheGeneration) { + std::lock_guard lock(cacheGenerationMutex); + if (cacheGenerationLastUpdate > std::chrono::milliseconds(0) && + (std::chrono::duration_cast( + std::chrono::system_clock::now().time_since_epoch()) - cacheGenerationLastUpdate) < cacheGenerationPeriodMs) + { + return false; + } + } File objectFile(path, umask); if (!objectFile.isValid()) @@ -134,6 +197,11 @@ bool Generation::wasUpdated() return true; } + if (cacheGeneration) { + std::lock_guard lock(cacheGenerationMutex); + cacheGenerationLastUpdate = std::chrono::duration_cast( + std::chrono::system_clock::now().time_since_epoch()); + } return (onDisk != currentValue); } } From 26757040ade4b49c4eb2dcc6c925f4c9c23fcefa Mon Sep 17 00:00:00 2001 From: Nelson Branco Date: Fri, 27 Feb 2026 11:57:30 +0000 Subject: [PATCH 2/2] update ci workflow --- .github/workflows/ci.yml | 132 +++++++++++++-------------------------- 1 file changed, 45 insertions(+), 87 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 3e98c085c..274bc988f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -4,59 +4,17 @@ on: push: branches: - main + tags: ['*'] pull_request: jobs: - linux_botan: - name: Linux with Botan - runs-on: ubuntu-24.04 - steps: - - uses: actions/checkout@v4 - - name: Prepare - run: | - sudo apt-get update -qq - sudo apt-get install -y libcppunit-dev libbotan-2-dev p11-kit - - name: Build - env: - CXXFLAGS: -Werror -DBOTAN_NO_DEPRECATED_WARNINGS - run: | - ./autogen.sh - ./configure --with-crypto-backend=botan - make - - name: Test - run: | - make check || (find . -name test-suite.log -exec cat {} \; && false) - - linux_ossl_1: - name: Linux with OpenSSL 1.1.1 - runs-on: ubuntu-24.04 - steps: - - uses: actions/checkout@v4 - - name: Prepare - run: | - sudo apt-get update -qq - sudo apt-get install -y libcppunit-dev p11-kit - # Replace installed OpenSSL with the supported version 1.1.1 - curl -O http://security.ubuntu.com/ubuntu/pool/main/o/openssl/libssl1.1_1.1.1f-1ubuntu2.24_amd64.deb - curl -O http://security.ubuntu.com/ubuntu/pool/main/o/openssl/libssl-dev_1.1.1f-1ubuntu2.24_amd64.deb - curl -O http://security.ubuntu.com/ubuntu/pool/main/o/openssl/openssl_1.1.1f-1ubuntu2.24_amd64.deb - sudo dpkg -i --force-confnew openssl_1.1.1f-1ubuntu2.24_amd64.deb \ - libssl1.1_1.1.1f-1ubuntu2.24_amd64.deb \ - libssl-dev_1.1.1f-1ubuntu2.24_amd64.deb - - name: Build - env: - CXXFLAGS: -Werror - run: | - ./autogen.sh - ./configure --with-crypto-backend=openssl - make - - name: Test - run: | - make check || (find . -name test-suite.log -exec cat {} \; && false) - linux_ossl_30: name: Linux with OpenSSL 3.0 - runs-on: ubuntu-20.04 + strategy: + fail-fast: false + matrix: + os: [ubuntu-22.04, ubuntu-24.04] + runs-on: ${{ matrix.os }} steps: - uses: actions/checkout@v4 - name: Prepare @@ -68,42 +26,21 @@ jobs: # env: # CXXFLAGS: -Werror run: | + sudo chmod +x autogen.sh ./autogen.sh ./configure --with-crypto-backend=openssl make - name: Test run: | make check || (find . -name test-suite.log -exec cat {} \; && false) - - uses: actions/upload-artifact@v4 - with: - path: src/lib/.lib/*.so - - macos: - name: macOS (${{ matrix.backend }}) - runs-on: macos-14 - strategy: - fail-fast: false - matrix: - include: - - backend: openssl - extra-options: --with-openssl=$(brew --prefix openssl@1.1) - - backend: botan - extra-options: --with-botan=$(brew --prefix botan@2) - steps: - - uses: actions/checkout@v4 - - name: Prepare + - name: Prepare release run: | - brew install automake libtool cppunit botan@2 - - name: Build - env: - CXXFLAGS: -Werror -DBOTAN_NO_DEPRECATED_WARNINGS - run: | - ./autogen.sh - ./configure --with-crypto-backend=${{ matrix.backend }} ${{ matrix.extra-options }} - make - - name: Test - run: | - make check || (find . -name test-suite.log -exec cat {} \; && false) + tar -cvzf libsofthsm2-${{ matrix.os }}.tgz -C src/lib/.libs libsofthsm2.so + - name: Stage Artifact + uses: actions/upload-artifact@v4 + with: + name: binaries-linux-${{ matrix.os }} + path: libsofthsm2-${{ matrix.os }}.tgz windows: name: Windows (${{ matrix.arch }}, ${{ matrix.backend }}) @@ -116,14 +53,6 @@ jobs: backend: openssl target-platform: x64 build-options: - - arch: x64 - backend: botan - target-platform: x64 - build-options: -DENABLE_ECC=OFF -DENABLE_EDDSA=OFF - - arch: x86 - backend: openssl - target-platform: Win32 - build-options: -DENABLE_ECC=OFF -DENABLE_EDDSA=OFF steps: - uses: actions/checkout@v4 - uses: ilammy/msvc-dev-cmd@v1 @@ -151,6 +80,35 @@ jobs: CTEST_OUTPUT_ON_FAILURE: 1 run: | cmake --build build --target RUN_TESTS - - uses: actions/upload-artifact@v4 + - name: Prepare release + run: | + tar -cvzf libsofthsm2-win-${{ matrix.arch }}-${{ matrix.backend }}.tgz -C build/src/lib/Debug *.dll + - name: Stage Artifact + uses: actions/upload-artifact@v4 + with: + name: binaries-windows-${{ matrix.arch }}-${{ matrix.backend }} + path: libsofthsm2-win-${{ matrix.arch }}-${{ matrix.backend }}.tgz + + release: + needs: [linux_ossl_30, windows] + runs-on: ubuntu-latest + permissions: + contents: write + steps: + - name: Download all artifacts + uses: actions/download-artifact@v4 + with: + pattern: binaries-* + path: dist + merge-multiple: true # Flattens all 'binaries-*' into the 'dist' folder + + - name: Create GitHub Release + uses: softprops/action-gh-release@v2 with: - path: build/src/lib/Debug + tag_name: ${{ github.event_name == 'pull_request' && github.head_ref || github.ref_name }} + name: Release ${{ github.event_name == 'pull_request' && github.head_ref || github.ref_name }} for ${{ matrix.os }} + draft: false + prerelease: ${{ github.event_name == 'pull_request' }} # Mark as prerelease if from a PR + fail_on_unmatched_files: true + files: dist/* + generate_release_notes: true