Skip to content

Commit 86d18ed

Browse files
authored
[PM-8475] Add script to check for and import vault items from a specified file during vault setup (#345)
* add script to check for and import vault items from a specified file * tweak scripts for performance & stability * add import file generation guidance to example dotfile * cleanup
1 parent 750a55b commit 86d18ed

File tree

6 files changed

+58
-1
lines changed

6 files changed

+58
-1
lines changed

.env.example

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ VAULT_HOST_URL="https://localhost"
2727
# Used to match a remote server configuration.
2828
REMOTE_VAULT_CONFIG_MATCH="https://localhost:8443/api/config"
2929

30+
# (Optional) Specify the root directory filename for the Bitwarden vault JSON import
31+
# you'd like to use (e.g. "vault.json")
32+
# Mock data generation tool: https://www.passwordvaultgenerator.com/
33+
VAULT_IMPORT_FILE=""
34+
3035
# The extension that will have tests running against it
3136
EXTENSION_BUILD_PATH="clients/apps/browser/build"
3237

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Thumbs.db
66
.env
77
dev-server.local.pem
88
tmp-vault-seeder
9+
vault.json
910
ssl.crt
1011
ssl.key
1112

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
"seed:vault:account": "NODE_EXTRA_CA_CERTS=ssl.crt ts-node ./scripts/create-account.ts",
4343
"seed:vault:ciphers": "ts-node ./scripts/vault-seeder.ts",
4444
"seed:vault:ciphers:refresh": "./scripts/cli-serve-helper.sh && REFRESH=true ts-node ./scripts/vault-seeder.ts && kill $(ps -e | grep 'bw serve' | grep -v 'vault-seeder' | awk '{print $1}')",
45+
"seed:vault:import": "./scripts/vault-import.sh",
4546
"setup:all": "./scripts/first-time-setup.sh",
4647
"setup:extension": "rimraf clients && git clone https://github.com/bitwarden/clients.git clients && cd clients && npm ci",
4748
"setup:install": "ts-node ./scripts/generate-installation.ts",

scripts/cli-serve-helper.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ BW_COMMAND status
3333
# shellcheck disable=SC2086 # we want to pass the server host url as a single argument
3434
BW_COMMAND logout --quiet # In case there's an active outdated session (e.g. docker container was rebuilt)
3535
BW_COMMAND config server $VAULT_HOST || true # no error if already configured
36-
BW_COMMAND login "$VAULT_EMAIL" "$VAULT_PASSWORD" --nointeraction || true # no error if already logged in
36+
BW_COMMAND login "$VAULT_EMAIL" "$VAULT_PASSWORD" --nointeraction --quiet || true # no error if already logged in
3737
BW_COMMAND sync || true # no error if already synced
3838

3939
# Start Vault Management API

scripts/first-time-setup.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,6 @@ npm run seed:vault:account
1515
npm run start:cli
1616
npm run seed:vault:ciphers
1717
npm run stop:cli
18+
npm run seed:vault:import
1819

1920
npm run setup:test-site

scripts/vault-import.sh

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/usr/bin/env bash
2+
3+
ROOT_DIR=$(git rev-parse --show-toplevel)
4+
5+
# shellcheck source=.env
6+
set -o allexport
7+
. $ROOT_DIR/.env
8+
set +o allexport
9+
10+
export NODE_EXTRA_CA_CERTS=$ROOT_DIR/$BW_SSL_CERT
11+
12+
BW_COMMAND() {
13+
bw "$@"
14+
}
15+
16+
if [[ -z "${VAULT_IMPORT_FILE}" ]]; then
17+
printf "No vault import file was specified. Skipping vault import...\n\n"
18+
19+
exit 0
20+
fi
21+
22+
chmod +r $VAULT_IMPORT_FILE
23+
24+
export VAULT_HOST=$VAULT_HOST_URL:$VAULT_HOST_PORT
25+
26+
if [[ -z "${VAULT_HOST_URL:-}" ]]; then
27+
echo "VAULT_HOST_URL is not set, using local dev values"
28+
export VAULT_HOST='--api http://localhost:4000 --identity http://localhost:33656 --web-vault https://localhost:8080 --events http://localhost:46273'
29+
fi
30+
31+
BW_COMMAND status
32+
33+
# Login to the vault
34+
# shellcheck disable=SC2086 # we want to pass the server host url as a single argument
35+
BW_COMMAND logout --quiet # In case there's an active outdated session (e.g. docker container was rebuilt)
36+
BW_COMMAND config server $VAULT_HOST || true # no error if already configured
37+
38+
BW_COMMAND login "$VAULT_EMAIL" "$VAULT_PASSWORD" --nointeraction --quiet || true # no error if already logged in
39+
BW_COMMAND sync || true # no error if already synced
40+
41+
# Unlock and set session token
42+
export BW_SESSION=$(
43+
BW_COMMAND unlock --passwordenv VAULT_PASSWORD --raw --nointeraction
44+
)
45+
46+
printf "Importing...\n"
47+
BW_COMMAND import bitwardenjson "${VAULT_IMPORT_FILE}"
48+
49+
BW_COMMAND logout --quiet

0 commit comments

Comments
 (0)