Skip to content

Commit

Permalink
Add bitwarden-cli-persistence feature
Browse files Browse the repository at this point in the history
  • Loading branch information
flokoe committed Sep 28, 2023
1 parent fcc59c4 commit 613fcdc
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/bitwarden-cli-persistence/devcontainer-feature.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"id": "bitwarden-cli-persistence",
"version": "0.1.0",
"name": "Bitwarden CLI Persistence",
"description": "Avoid extra logins from the Bitwarden CLI by preserving the `~/.config/Bitwarden CLI` folder across container instances.",
"documentationURL": "https://github.com/flokoe/devcontainer-features/tree/main/src/bitwarden-cli-persistence",
"licenseURL": "https://github.com/flokoe/devcontainer-features/blob/main/LICENSE",
"options": {},
"mounts": [
{
"source": "${devcontainerId}-bitwarden-cli",
"target": "/dc/bitwarden-cli",
"type": "volume"
}
],
"installsAfter": [
"ghcr.io/devcontainers-contrib/features/bitwarden-cli"
]
}
35 changes: 35 additions & 0 deletions src/bitwarden-cli-persistence/install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/usr/bin/env bash

set -e

if [ -z "$_REMOTE_USER" ] || [ -z "$_REMOTE_USER_HOME" ]; then
echo "***********************************************************************************"
echo "*** Require _REMOTE_USER and _REMOTE_USER_HOME to be set (by dev container CLI) ***"
echo "***********************************************************************************"
exit 1
fi

# Ensure ~/.config exists to prevent tests failing if root user
mkdir -p "$_REMOTE_USER_HOME/.config"

# Create /dc/bitwarden-cli folder if doesn't exist.
mkdir -p "/dc/bitwarden-cli"

# As to why we move around the folder, check `github-cli-persistence/install.sh` @github.com/joshuanianji/devcontainer-features
if [ -d "$_REMOTE_USER_HOME/.config/Bitwarden CLI" ]; then
echo "Moving existing '~/.config/Bitwarden CLI' folder to '~/.config/Bitwarden CLI OLD'"
mv "$_REMOTE_USER_HOME/.config/Bitwarden CLI" "$_REMOTE_USER_HOME/.config/Bitwarden CLI OLD"
fi

ln -s /dc/bitwarden-cli "$_REMOTE_USER_HOME/.config/Bitwarden CLI"

# Change owner of folder.
chown -R "${_REMOTE_USER}:${_REMOTE_USER}" "$_REMOTE_USER_HOME/.config/Bitwarden CLI"

# chown mount (only attached on startup)
cat <<EOF >>"$_REMOTE_USER_HOME/.bashrc"
sudo chown -R "${_REMOTE_USER}:${_REMOTE_USER}" /dc/bitwarden-cli
EOF
chown -R "$_REMOTE_USER" "$_REMOTE_USER_HOME/.bashrc"

echo "done!"
9 changes: 9 additions & 0 deletions test/bitwarden-cli-persistence/scenarios.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"with_vscode": {
"image": "mcr.microsoft.com/devcontainers/base:bookworm",
"features": {
"bitwarden-cli-persistence": {},
"ghcr.io/devcontainers-contrib/features/bitwarden-cli": {}
}
}
}
28 changes: 28 additions & 0 deletions test/bitwarden-cli-persistence/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/usr/bin/env bash

set -e

# Optional: Import test library bundled with the devcontainer CLI
# See https://github.com/devcontainers/cli/blob/HEAD/docs/features/test.md#dev-container-features-test-lib
# Provides the 'check' and 'reportResults' commands.
source dev-container-features-test-lib

# Feature-specific tests
# The 'check' command comes from the dev-container-features-test-lib. Syntax is...
# check <LABEL> <cmd> [args...]

# shellcheck disable=SC2016
check '`~/.config/Bitwarden CLI` existence' test -d "$HOME/.config/Bitwarden CLI"

# shellcheck disable=SC2016
check '`/dc/bitwarden-cli` existence' test -d /dc/bitwarden-cli

# shellcheck disable=SC2016
check '`~/.config/Bitwarden CLI` is a symlink' test -L "$HOME/.config/Bitwarden CLI"

# shellcheck disable=SC2016
check '`~/.config/Bitwarden CLI` is writable' test -w "$HOME/.config/Bitwarden CLI"

# Report results
# If any of the checks above exited with a non-zero exit code, the test will fail.
reportResults
29 changes: 29 additions & 0 deletions test/bitwarden-cli-persistence/with_vscode.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/env bash

set -e

# Optional: Import test library bundled with the devcontainer CLI
# See https://github.com/devcontainers/cli/blob/HEAD/docs/features/test.md#dev-container-features-test-lib
# Provides the 'check' and 'reportResults' commands.
source dev-container-features-test-lib

# Feature-specific tests
# The 'check' command comes from the dev-container-features-test-lib. Syntax is...
# check <LABEL> <cmd> [args...]

# shellcheck disable=SC2016
check '`~/.config/Bitwarden CLI` existence' test -d "$HOME/.config/Bitwarden CLI"

# shellcheck disable=SC2016
check '`/dc/bitwarden-cli` existence' test -d /dc/bitwarden-cli

# shellcheck disable=SC2016
check '`~/.config/Bitwarden CLI` is a symlink' test -L "$HOME/.config/Bitwarden CLI"

check 'Bitwarden CLI is working' bw --version

check 'Bitwarden CLI data file exists' test -f /dc/bitwarden-cli/data.json

# Report results
# If any of the checks above exited with a non-zero exit code, the test will fail.
reportResults

0 comments on commit 613fcdc

Please sign in to comment.