Skip to content

Commit 616bc22

Browse files
committed
update entrypoint
1 parent 3519865 commit 616bc22

File tree

1 file changed

+50
-11
lines changed

1 file changed

+50
-11
lines changed

entrypoint.sh

Lines changed: 50 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22
set -eu
33

44
# If arguments are provided, run bw with them (passthrough mode)
5+
# This keeps the image usable as a plain CLI.
56
if [ "$#" -gt 0 ]; then
67
exec bw "$@"
78
fi
89

9-
# Server mode
10+
# --- Server mode (for bw serve) ---
1011

1112
if [ -z "${BW_HOST:-}" ]; then
1213
echo "BW_HOST is required" >&2
@@ -16,25 +17,63 @@ fi
1617
# Point CLI to the desired server (US/EU/self-hosted)
1718
bw config server "${BW_HOST}"
1819

19-
# Login/unlock
20-
if [ -n "${BW_CLIENTID:-}" ] && [ -n "${BW_CLIENTSECRET:-}" ]; then
20+
login_with_apikey() {
2121
echo "Using API key to log in"
2222
# bw reads BW_CLIENTID and BW_CLIENTSECRET from env
23-
bw login --apikey --raw >/dev/null 2>&1 || true
24-
if [ -n "${BW_PASSWORD:-}" ]; then
25-
export BW_SESSION="$(bw unlock --passwordenv BW_PASSWORD --raw)"
26-
fi
27-
else
23+
bw login --apikey --raw >/dev/null 2>&1
24+
}
25+
26+
login_with_user() {
2827
if [ -z "${BW_USER:-}" ] || [ -z "${BW_PASSWORD:-}" ]; then
2928
echo "BW_USER and BW_PASSWORD are required when not using API key" >&2
3029
exit 2
3130
fi
3231
echo "Using username/password to log in"
33-
export BW_SESSION="$(bw login "${BW_USER}" --passwordenv BW_PASSWORD --raw)"
32+
BW_SESSION="$(bw login "${BW_USER}" --passwordenv BW_PASSWORD --raw)"
33+
export BW_SESSION
34+
}
35+
36+
unlock_with_password() {
37+
if [ -z "${BW_PASSWORD:-}" ]; then
38+
echo "BW_PASSWORD is required to unlock vault" >&2
39+
return 1
40+
fi
41+
BW_SESSION="$(bw unlock --passwordenv BW_PASSWORD --raw)"
42+
export BW_SESSION
43+
}
44+
45+
# Initial login + unlock so the vault starts in an unlocked state.
46+
if [ -n "${BW_CLIENTID:-}" ] && [ -n "${BW_CLIENTSECRET:-}" ]; then
47+
login_with_apikey
48+
# Use password to derive a session so CLI/server can operate without prompts.
49+
unlock_with_password || {
50+
echo "Initial unlock failed; Bitwarden vault remains locked." >&2
51+
}
52+
else
53+
login_with_user
3454
fi
3555

36-
# Ensure session is valid if possible
37-
bw unlock --check >/dev/null 2>&1 || true
56+
# Background watchdog: keep the vault unlocked even when Bitwarden/Vaultwarden
57+
# applies short auto-lock timeouts (no \"never\" option).
58+
keep_unlocked() {
59+
# We intentionally ignore transient errors here; ESO will surface issues
60+
# and the pod can be restarted by Kubernetes if needed.
61+
while true; do
62+
if ! bw unlock --check >/dev/null 2>&1; then
63+
echo "Vault appears locked; attempting re-unlock..." >&2
64+
if [ -n "${BW_CLIENTID:-}" ] && [ -n "${BW_CLIENTSECRET:-}" ]; then
65+
bw login --apikey --raw >/dev/null 2>&1 || true
66+
fi
67+
unlock_with_password || true
68+
fi
69+
sleep "${BW_UNLOCK_INTERVAL:-300}"
70+
done
71+
}
72+
73+
# After initial login/unlock, we don't want set -e to kill the container
74+
# because of a transient failure inside keep_unlocked.
75+
set +e
76+
keep_unlocked &
3877

3978
echo 'Running `bw serve` on 0.0.0.0:8087'
4079
exec bw serve --hostname 0.0.0.0

0 commit comments

Comments
 (0)