Skip to content

Commit 0be54d1

Browse files
dani-garciaHinton
andauthored
Publish WASM package to enable argon2 support on CLI (#691)
## Type of change ``` - [ ] Bug fix - [x] New feature development - [ ] Tech debt (refactoring, code cleanup, dependency upgrades, etc) - [x] Build/deploy pipeline (DevOps) - [ ] Other ``` ## Objective Continuation of #680 I've created workflows to publish the WASM builds to NPM, so that it can be used from the CLI client. The publishing step is based on the NAPI builds, and I've tested it against the Github NPM registry. We'll need to get a build going and published on the NPM registry to be able to use it on the CLI. --------- Co-authored-by: Hinton <[email protected]>
1 parent 6d45212 commit 0be54d1

File tree

6 files changed

+224
-1
lines changed

6 files changed

+224
-1
lines changed

.github/workflows/build-wasm.yml

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
---
2+
name: Build @bitwarden/sdk-wasm
3+
4+
on:
5+
pull_request:
6+
push:
7+
branches:
8+
- "main"
9+
- "rc"
10+
- "hotfix-rc"
11+
workflow_dispatch:
12+
13+
defaults:
14+
run:
15+
shell: bash
16+
working-directory: crates/bitwarden-wasm
17+
18+
jobs:
19+
build:
20+
name: Building @bitwarden/sdk-wasm
21+
runs-on: ubuntu-22.04
22+
23+
steps:
24+
- name: Checkout repo
25+
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
26+
27+
- name: Setup Node
28+
uses: actions/setup-node@60edb5dd545a775178f52524783378180af0d1f8 # v4.0.2
29+
with:
30+
node-version: 18
31+
cache: "npm"
32+
33+
- name: Install dependencies
34+
run: npm i -g binaryen
35+
36+
- name: Install rust
37+
uses: dtolnay/rust-toolchain@be73d7920c329f220ce78e0234b8f96b7ae60248 # stable
38+
with:
39+
toolchain: stable
40+
targets: wasm32-unknown-unknown
41+
42+
- name: Cache cargo registry
43+
uses: Swatinem/rust-cache@23bce251a8cd2ffc3c1075eaa2367cf899916d84 # v2.7.3
44+
with:
45+
key: wasm-cargo-cache
46+
47+
- name: Install wasm-bindgen-cli
48+
run: cargo install wasm-bindgen-cli
49+
50+
- name: Build
51+
run: ./build.sh -r
52+
53+
- name: Upload artifact
54+
uses: actions/upload-artifact@5d5d22a31266ced268874388b861e4b58bb5c2f3 # v4.3.1
55+
with:
56+
name: sdk-bitwarden-wasm
57+
path: ${{ github.workspace }}/languages/js/wasm/*
58+
if-no-files-found: error

.github/workflows/release-wasm.yml

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
---
2+
name: Release @bitwarden/sdk-wasm
3+
run-name: Release @bitwarden/sdk-wasm ${{ inputs.release_type }}
4+
5+
on:
6+
workflow_dispatch:
7+
inputs:
8+
release_type:
9+
description: "Release Options"
10+
required: true
11+
default: "Release"
12+
type: choice
13+
options:
14+
- Release
15+
- Dry Run
16+
npm_publish:
17+
description: "Publish to NPM registry"
18+
required: true
19+
default: true
20+
type: boolean
21+
22+
defaults:
23+
run:
24+
shell: bash
25+
working-directory: languages/js/wasm
26+
27+
jobs:
28+
setup:
29+
name: Setup
30+
runs-on: ubuntu-22.04
31+
outputs:
32+
release-version: ${{ steps.version.outputs.version }}
33+
steps:
34+
- name: Checkout repo
35+
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
36+
37+
- name: Branch check
38+
if: ${{ github.event.inputs.release_type != 'Dry Run' }}
39+
run: |
40+
if [[ "$GITHUB_REF" != "refs/heads/rc" ]] && [[ "$GITHUB_REF" != "refs/heads/hotfix-rc" ]]; then
41+
echo "==================================="
42+
echo "[!] Can only release from the 'rc' or 'hotfix-rc' branches"
43+
echo "==================================="
44+
exit 1
45+
fi
46+
47+
- name: Check Release Version
48+
id: version
49+
uses: bitwarden/gh-actions/release-version-check@main
50+
with:
51+
release-type: ${{ github.event.inputs.release_type }}
52+
project-type: ts
53+
file: languages/js/wasm/package.json
54+
monorepo: false
55+
56+
- name: Create GitHub deployment
57+
if: ${{ github.event.inputs.release_type != 'Dry Run' }}
58+
uses: chrnorm/deployment-action@55729fcebec3d284f60f5bcabbd8376437d696b1 # v2.0.7
59+
id: deployment
60+
with:
61+
token: "${{ secrets.GITHUB_TOKEN }}"
62+
initial-status: "in_progress"
63+
environment: "Bitwarden SDK WASM - Production"
64+
description: "Deployment ${{ steps.version.outputs.version }} from branch ${{ github.ref_name }}"
65+
task: release
66+
67+
- name: Update deployment status to Success
68+
if: ${{ github.event.inputs.release_type != 'Dry Run' && success() }}
69+
uses: chrnorm/deployment-status@2afb7d27101260f4a764219439564d954d10b5b0 # v2.0.1
70+
with:
71+
token: "${{ secrets.GITHUB_TOKEN }}"
72+
state: "success"
73+
deployment-id: ${{ steps.deployment.outputs.deployment_id }}
74+
75+
- name: Update deployment status to Failure
76+
if: ${{ github.event.inputs.release_type != 'Dry Run' && failure() }}
77+
uses: chrnorm/deployment-status@2afb7d27101260f4a764219439564d954d10b5b0 # v2.0.1
78+
with:
79+
token: "${{ secrets.GITHUB_TOKEN }}"
80+
state: "failure"
81+
deployment-id: ${{ steps.deployment.outputs.deployment_id }}
82+
83+
npm:
84+
name: Publish NPM
85+
runs-on: ubuntu-22.04
86+
needs: setup
87+
if: inputs.npm_publish
88+
env:
89+
_PKG_VERSION: ${{ needs.setup.outputs.release-version }}
90+
steps:
91+
- name: Checkout repo
92+
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
93+
94+
- name: Setup Node
95+
uses: actions/setup-node@60edb5dd545a775178f52524783378180af0d1f8 # v4.0.2
96+
with:
97+
node-version: 18
98+
cache: "npm"
99+
100+
- name: Login to Azure
101+
uses: Azure/login@e15b166166a8746d1a47596803bd8c1b595455cf # v1.6.0
102+
with:
103+
creds: ${{ secrets.AZURE_KV_CI_SERVICE_PRINCIPAL }}
104+
105+
- name: Retrieve secrets
106+
id: retrieve-secrets
107+
uses: bitwarden/gh-actions/get-keyvault-secrets@main
108+
with:
109+
keyvault: "bitwarden-ci"
110+
secrets: "npm-api-key"
111+
112+
- name: Download artifacts
113+
uses: bitwarden/gh-actions/download-artifacts@main
114+
with:
115+
workflow: build-wasm.yml
116+
path: ${{ github.workspace }}/languages/js/wasm
117+
workflow_conclusion: success
118+
branch: ${{ github.event.inputs.release_type == 'Dry Run' && 'main' || github.ref_name }}
119+
120+
- name: Setup NPM
121+
run: |
122+
echo 'registry="https://registry.npmjs.org/"' > ./.npmrc
123+
echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" >> ./.npmrc
124+
125+
echo 'registry="https://registry.npmjs.org/"' > ~/.npmrc
126+
echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" >> ~/.npmrc
127+
env:
128+
NPM_TOKEN: ${{ steps.retrieve-secrets.outputs.npm-api-key }}
129+
130+
- name: Publish NPM
131+
if: ${{ github.event.inputs.release_type != 'Dry Run' }}
132+
run: npm publish --access public --registry=https://registry.npmjs.org/ --userconfig=./.npmrc

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/bitwarden-wasm/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ keywords.workspace = true
1515
crate-type = ["cdylib"]
1616

1717
[dependencies]
18+
argon2 = { version = ">=0.5.0, <0.6", features = [
19+
"alloc",
20+
"zeroize",
21+
], default-features = false }
1822
bitwarden-json = { path = "../bitwarden-json", features = [
1923
"secrets",
2024
"internal",

crates/bitwarden-wasm/src/client.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
extern crate console_error_panic_hook;
22
use std::rc::Rc;
33

4+
use argon2::{Algorithm, Argon2, Params, Version};
45
use bitwarden_json::client::Client as JsonClient;
56
use js_sys::Promise;
67
use log::Level;
@@ -54,3 +55,27 @@ impl BitwardenClient {
5455
})
5556
}
5657
}
58+
59+
#[wasm_bindgen]
60+
pub fn argon2(
61+
password: &[u8],
62+
salt: &[u8],
63+
iterations: u32,
64+
memory: u32,
65+
parallelism: u32,
66+
) -> Result<Vec<u8>, JsError> {
67+
let argon = Argon2::new(
68+
Algorithm::Argon2id,
69+
Version::V0x13,
70+
Params::new(
71+
memory * 1024, // Convert MiB to KiB
72+
iterations,
73+
parallelism,
74+
Some(32),
75+
)?,
76+
);
77+
78+
let mut hash = [0u8; 32];
79+
argon.hash_password_into(password, salt, &mut hash)?;
80+
Ok(hash.to_vec())
81+
}

languages/js/wasm/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,20 @@
44
"files": [
55
"bitwarden_wasm_bg.js",
66
"bitwarden_wasm_bg.wasm",
7+
"bitwarden_wasm_bg.wasm.d.ts",
8+
"bitwarden_wasm_bg.wasm.js",
79
"bitwarden_wasm.d.ts",
810
"bitwarden_wasm.js",
911
"index.js",
10-
"node/bitwarden_wasm_bg.wasm.d.ts",
1112
"node/bitwarden_wasm_bg.wasm",
13+
"node/bitwarden_wasm_bg.wasm.d.ts",
1214
"node/bitwarden_wasm.d.ts",
1315
"node/bitwarden_wasm.js"
1416
],
1517
"main": "node/bitwarden_wasm.js",
1618
"module": "index.js",
1719
"types": "bitwarden_wasm.d.ts",
20+
"scripts": {},
1821
"sideEffects": [
1922
"./bitwarden_wasm.js",
2023
"./snippets/*"

0 commit comments

Comments
 (0)