|
| 1 | +name: Build Rust native binaries |
| 2 | + |
| 3 | +# Reusable workflow that compiles the varlock-local-encrypt Rust binary |
| 4 | +# for Linux and Windows targets. |
| 5 | +# |
| 6 | +# Builds are cached by Cargo.lock + source hash. Each platform builds |
| 7 | +# natively on its own runner for maximum compatibility. |
| 8 | +# |
| 9 | +# Output: native binaries uploaded as artifacts, ready to be bundled |
| 10 | +# into the varlock npm package and CLI release archives. |
| 11 | + |
| 12 | +permissions: |
| 13 | + contents: read |
| 14 | + |
| 15 | +on: |
| 16 | + workflow_call: |
| 17 | + inputs: |
| 18 | + artifact-name: |
| 19 | + description: 'Base name for uploaded artifacts (suffixed with platform)' |
| 20 | + type: string |
| 21 | + default: 'native-bin-rust' |
| 22 | + |
| 23 | +jobs: |
| 24 | + build: |
| 25 | + strategy: |
| 26 | + matrix: |
| 27 | + include: |
| 28 | + - os: ubuntu-latest |
| 29 | + target: x86_64-unknown-linux-gnu |
| 30 | + native-bin-subdir: linux-x64 |
| 31 | + binary-name: varlock-local-encrypt |
| 32 | + test-interop: true # only need interop on one platform — crypto is identical |
| 33 | + - os: ubuntu-24.04-arm |
| 34 | + target: aarch64-unknown-linux-gnu |
| 35 | + native-bin-subdir: linux-arm64 |
| 36 | + binary-name: varlock-local-encrypt |
| 37 | + test-interop: false |
| 38 | + - os: windows-latest |
| 39 | + target: x86_64-pc-windows-msvc |
| 40 | + native-bin-subdir: win32-x64 |
| 41 | + binary-name: varlock-local-encrypt.exe |
| 42 | + test-interop: false |
| 43 | + |
| 44 | + runs-on: ${{ matrix.os }} |
| 45 | + name: Build ${{ matrix.native-bin-subdir }} |
| 46 | + |
| 47 | + steps: |
| 48 | + - uses: actions/checkout@v6 |
| 49 | + |
| 50 | + - name: Install Rust toolchain |
| 51 | + uses: dtolnay/rust-toolchain@stable |
| 52 | + with: |
| 53 | + targets: ${{ matrix.target }} |
| 54 | + |
| 55 | + # Cache Cargo registry + build artifacts by lockfile hash |
| 56 | + - name: Cache Cargo |
| 57 | + uses: actions/cache@v5 |
| 58 | + with: |
| 59 | + path: | |
| 60 | + ~/.cargo/registry |
| 61 | + ~/.cargo/git |
| 62 | + packages/encryption-binary-rust/target |
| 63 | + key: rust-${{ matrix.target }}-${{ hashFiles('packages/encryption-binary-rust/Cargo.lock') }}-${{ hashFiles('packages/encryption-binary-rust/src/**') }} |
| 64 | + restore-keys: | |
| 65 | + rust-${{ matrix.target }}-${{ hashFiles('packages/encryption-binary-rust/Cargo.lock') }}- |
| 66 | + rust-${{ matrix.target }}- |
| 67 | +
|
| 68 | + - name: Install UPX |
| 69 | + shell: bash |
| 70 | + run: | |
| 71 | + if [[ "${{ matrix.os }}" == *"ubuntu"* ]]; then |
| 72 | + sudo apt-get update && sudo apt-get install -y upx-ucl |
| 73 | + elif [[ "${{ matrix.os }}" == *"windows"* ]]; then |
| 74 | + choco install upx --yes |
| 75 | + fi |
| 76 | +
|
| 77 | + - name: Build release binary |
| 78 | + working-directory: packages/encryption-binary-rust |
| 79 | + run: cargo build --release --target ${{ matrix.target }} |
| 80 | + |
| 81 | + - name: Prepare artifact |
| 82 | + shell: bash |
| 83 | + run: | |
| 84 | + ARTIFACT_DIR="native-bins/${{ matrix.native-bin-subdir }}" |
| 85 | + mkdir -p "$ARTIFACT_DIR" |
| 86 | + cp "packages/encryption-binary-rust/target/${{ matrix.target }}/release/${{ matrix.binary-name }}" "$ARTIFACT_DIR/" |
| 87 | + echo "=== Binary info (before UPX) ===" |
| 88 | + ls -la "$ARTIFACT_DIR/${{ matrix.binary-name }}" |
| 89 | + file "$ARTIFACT_DIR/${{ matrix.binary-name }}" || true |
| 90 | + echo "=== UPX compress ===" |
| 91 | + upx --best "$ARTIFACT_DIR/${{ matrix.binary-name }}" |
| 92 | + echo "=== Binary info (after UPX) ===" |
| 93 | + ls -la "$ARTIFACT_DIR/${{ matrix.binary-name }}" |
| 94 | +
|
| 95 | + # Run Rust unit tests |
| 96 | + - name: Run unit tests |
| 97 | + working-directory: packages/encryption-binary-rust |
| 98 | + run: cargo test --release --target ${{ matrix.target }} |
| 99 | + |
| 100 | + # Test the built binary end-to-end (one-shot commands, no biometric) |
| 101 | + # Uses python (not python3) for Windows compat; printf for reliable base64 input |
| 102 | + - name: Test binary - status |
| 103 | + shell: bash |
| 104 | + run: | |
| 105 | + BIN="native-bins/${{ matrix.native-bin-subdir }}/${{ matrix.binary-name }}" |
| 106 | + PY=$(command -v python3 || command -v python) |
| 107 | + echo "=== status ===" |
| 108 | + $BIN status |
| 109 | + $BIN status | $PY -c "import sys,json; d=json.load(sys.stdin); assert d['ok'], 'status not ok'" |
| 110 | +
|
| 111 | + - name: Test binary - key lifecycle + encrypt/decrypt roundtrip |
| 112 | + shell: bash |
| 113 | + run: | |
| 114 | + BIN="native-bins/${{ matrix.native-bin-subdir }}/${{ matrix.binary-name }}" |
| 115 | + PY=$(command -v python3 || command -v python) |
| 116 | +
|
| 117 | + echo "=== generate-key ===" |
| 118 | + $BIN generate-key --key-id ci-test |
| 119 | + $BIN key-exists --key-id ci-test | $PY -c "import sys,json; d=json.load(sys.stdin); assert d['exists']" |
| 120 | +
|
| 121 | + echo "=== encrypt ===" |
| 122 | + PLAINTEXT=$($PY -c "import base64; print(base64.b64encode(b'hello from CI').decode())") |
| 123 | + CIPHERTEXT=$($BIN encrypt --key-id ci-test --data "$PLAINTEXT" | $PY -c "import sys,json; print(json.load(sys.stdin)['ciphertext'])") |
| 124 | + echo "Ciphertext: ${CIPHERTEXT:0:40}..." |
| 125 | +
|
| 126 | + echo "=== decrypt ===" |
| 127 | + DECRYPTED=$($BIN decrypt --key-id ci-test --data "$CIPHERTEXT" | $PY -c "import sys,json; print(json.load(sys.stdin)['plaintext'])") |
| 128 | + echo "Decrypted: $DECRYPTED" |
| 129 | +
|
| 130 | + if [ "$DECRYPTED" != "hello from CI" ]; then |
| 131 | + echo "::error::Roundtrip failed! Expected 'hello from CI', got '$DECRYPTED'" |
| 132 | + exit 1 |
| 133 | + fi |
| 134 | +
|
| 135 | + echo "=== delete-key ===" |
| 136 | + $BIN delete-key --key-id ci-test |
| 137 | +
|
| 138 | + echo "All binary tests passed" |
| 139 | +
|
| 140 | + # Cross-platform interop: encrypt with Rust, decrypt with JS, and vice versa |
| 141 | + # Only on platforms where Bun is available and keys aren't DPAPI-protected |
| 142 | + - name: Setup Bun (for interop test) |
| 143 | + if: matrix.test-interop |
| 144 | + uses: oven-sh/setup-bun@v2 |
| 145 | + |
| 146 | + - name: Install JS deps (for interop test) |
| 147 | + if: matrix.test-interop |
| 148 | + run: bun install |
| 149 | + |
| 150 | + # Bidirectional Rust↔JS interop test (Linux x64 only — crypto is platform-independent) |
| 151 | + - name: Setup Bun (for interop test) |
| 152 | + if: matrix.test-interop |
| 153 | + uses: oven-sh/setup-bun@v2 |
| 154 | + |
| 155 | + - name: Install JS deps (for interop test) |
| 156 | + if: matrix.test-interop |
| 157 | + run: bun install |
| 158 | + |
| 159 | + - name: Test Rust↔JS interop |
| 160 | + if: matrix.test-interop |
| 161 | + shell: bash |
| 162 | + run: | |
| 163 | + BIN="native-bins/${{ matrix.native-bin-subdir }}/${{ matrix.binary-name }}" |
| 164 | +
|
| 165 | + # Generate key with Rust (no DPAPI on Linux = plaintext PKCS8) |
| 166 | + $BIN generate-key --key-id interop-test |
| 167 | + KEY_FILE="$HOME/.config/varlock/local-encrypt/keys/interop-test.json" |
| 168 | + PUBLIC_KEY=$(python3 -c "import json; print(json.load(open('$KEY_FILE'))['publicKey'])") |
| 169 | + PRIVATE_KEY=$(python3 -c "import json; print(json.load(open('$KEY_FILE'))['protectedPrivateKey'])") |
| 170 | +
|
| 171 | + # Rust→JS: encrypt with Rust, decrypt with JS |
| 172 | + PLAINTEXT_B64=$(python3 -c "import base64; print(base64.b64encode(b'rust to javascript').decode())") |
| 173 | + CIPHERTEXT=$($BIN encrypt --key-id interop-test --data "$PLAINTEXT_B64" | python3 -c "import sys,json; print(json.load(sys.stdin)['ciphertext'])") |
| 174 | + RESULT=$(bun -e " |
| 175 | + const { decrypt } = await import('./packages/varlock/src/lib/local-encrypt/crypto.ts'); |
| 176 | + const result = await decrypt('$PRIVATE_KEY', '$PUBLIC_KEY', '$CIPHERTEXT'); |
| 177 | + process.stdout.write(result); |
| 178 | + ") |
| 179 | + [ "$RESULT" = "rust to javascript" ] || { echo "::error::Rust→JS failed! Got: $RESULT"; exit 1; } |
| 180 | + echo "Rust→JS: '$RESULT'" |
| 181 | +
|
| 182 | + # JS→Rust: encrypt with JS, decrypt with Rust |
| 183 | + CIPHERTEXT=$(bun -e " |
| 184 | + const { encrypt } = await import('./packages/varlock/src/lib/local-encrypt/crypto.ts'); |
| 185 | + const result = await encrypt('$PUBLIC_KEY', 'javascript to rust'); |
| 186 | + process.stdout.write(result); |
| 187 | + ") |
| 188 | + RESULT=$($BIN decrypt --key-id interop-test --data "$CIPHERTEXT" | python3 -c "import sys,json; print(json.load(sys.stdin)['plaintext'])") |
| 189 | + [ "$RESULT" = "javascript to rust" ] || { echo "::error::JS→Rust failed! Got: $RESULT"; exit 1; } |
| 190 | + echo "JS→Rust: '$RESULT'" |
| 191 | +
|
| 192 | + $BIN delete-key --key-id interop-test |
| 193 | + echo "All interop tests passed" |
| 194 | +
|
| 195 | + - name: Upload artifact |
| 196 | + uses: actions/upload-artifact@v7 |
| 197 | + with: |
| 198 | + name: ${{ inputs.artifact-name }}-${{ matrix.native-bin-subdir }} |
| 199 | + path: native-bins/${{ matrix.native-bin-subdir }}/ |
| 200 | + retention-days: 7 |
| 201 | + |
| 202 | + # Cache the built binary so preview releases on other runners can restore it |
| 203 | + - name: Cache built binary |
| 204 | + uses: actions/cache/save@v5 |
| 205 | + with: |
| 206 | + path: native-bins/${{ matrix.native-bin-subdir }}/ |
| 207 | + key: native-bin-rust-${{ matrix.native-bin-subdir }}-${{ hashFiles('packages/encryption-binary-rust/Cargo.lock', 'packages/encryption-binary-rust/src/**') }} |
0 commit comments