Skip to content

Commit 98e4e41

Browse files
committed
fix docker backend and secrets setup
1 parent bff7c0f commit 98e4e41

File tree

6 files changed

+94
-42
lines changed

6 files changed

+94
-42
lines changed

backend/Dockerfile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ FROM python:3.13-slim-bookworm AS runner
2929

3030
WORKDIR /app
3131

32+
# Install necessary runtime dependencies
33+
RUN apt-get update && apt-get install -y --no-install-recommends \
34+
libmariadb3 && \
35+
rm -rf /var/lib/apt/lists/*
36+
3237
# Copy necessary files from the builder stage
3338
COPY --from=builder /app /app
3439

backend/lifehub/config/checks.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
from lifehub.config.constants import cfg
55
from lifehub.config.providers import setup_providers
66
from lifehub.config.util.schemas import * # noqa: F401,F403
7-
from lifehub.config.vault import setup_vault
87
from lifehub.core.common.base.db_model import BaseModel
98
from lifehub.core.common.database_service import get_engine, get_session
109
from lifehub.core.provider.repository.provider import ProviderRepository

docker-compose.yml

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
version: "3.8"
2-
31
services:
42
lifehub-backend:
53
build:
@@ -8,15 +6,16 @@ services:
86
environment:
97
- ENVIRONMENT=production
108
- UVICORN_HOST=0.0.0.0
11-
- DB_HOST=lifehub-mariadb
9+
- DB_HOST=host.docker.internal
1210
- DB_NAME=lifehub
1311
- FRONTEND_URL=http://lifehub-frontend
14-
- VAULT_ADDR=http://192.168.100.1:8200
12+
- VAULT_ADDR=http://host.docker.internal:8200
1513
- VAULT_APPROLE_ROLE_ID=${VAULT_APPROLE_ROLE_ID}
1614
- VAULT_APPROLE_SECRET_ID=${VAULT_APPROLE_SECRET_ID}
1715
networks:
18-
- vault-net
1916
- lifehub-net
17+
extra_hosts:
18+
- "host.docker.internal:host-gateway"
2019

2120
lifehub-frontend:
2221
build:
@@ -34,11 +33,5 @@ services:
3433
- lifehub-net
3534

3635
networks:
37-
vault-net:
38-
external: true
3936
lifehub-net:
4037
driver: bridge
41-
42-
volumes:
43-
dbdata:
44-
driver: local

load-secrets.sh

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/bin/bash
2+
3+
# Load the .env file
4+
set -a
5+
. backend/.env
6+
set +a
7+
8+
set -e # Exit on error
9+
set -u # Treat unset variables as errors
10+
set -o pipefail # Catch pipeline errors
11+
12+
# Write to kv/lifehub/metadata
13+
vault kv put kv/lifehub/metadata \
14+
AUTH_SECRET_KEY="$AUTH_SECRET_KEY" \
15+
ADMIN_PASSWORD="$ADMIN_PASSWORD"
16+
17+
vault kv put kv/lifehub/api-tokens \
18+
GOCARDLESS_CLIENT_ID="$GOCARDLESS_CLIENT_ID" \
19+
GOCARDLESS_CLIENT_SECRET="$GOCARDLESS_CLIENT_SECRET" \
20+
GOOGLE_CALENDAR_CLIENT_ID="$GOOGLE_CALENDAR_CLIENT_ID" \
21+
GOOGLE_CALENDAR_CLIENT_SECRET="$GOOGLE_CALENDAR_CLIENT_SECRET" \
22+
GOOGLE_TASKS_CLIENT_ID="$GOOGLE_TASKS_CLIENT_ID" \
23+
GOOGLE_TASKS_CLIENT_SECRET="$GOOGLE_TASKS_CLIENT_SECRET" \
24+
SPOTIFY_CLIENT_ID="$SPOTIFY_CLIENT_ID" \
25+
SPOTIFY_CLIENT_SECRET="$SPOTIFY_CLIENT_SECRET" \
26+
STRAVA_CLIENT_ID="$STRAVA_CLIENT_ID" \
27+
STRAVA_CLIENT_SECRET="$STRAVA_CLIENT_SECRET" \
28+
YNAB_CLIENT_ID="$YNAB_CLIENT_ID" \
29+
YNAB_CLIENT_SECRET="$YNAB_CLIENT_SECRET"

mariadb/setup-db.sh

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,76 @@
11
#!/bin/bash
22

3-
db_name=lifehub
3+
set -e # Exit on error
4+
set -u # Treat unset variables as errors
5+
set -o pipefail # Catch pipeline errors
46

5-
db_user=lifehub
6-
db_password=testing
7+
VAULT_ADDR="http://localhost:8200"
8+
export VAULT_ADDR
9+
10+
11+
db_name="lifehub"
12+
db_user="lifehub"
13+
db_password="testing" # TODO: Read from env or vault
14+
db_host="localhost"
715

816
vault_user="vault"
9-
vault_password="vault-testing"
17+
vault_password=$(vault kv get -field=password kv/vault-metadata/mariadb)
18+
vault_host="localhost"
19+
#vault_host="bernardo-arch"
1020

1121
# Must be root to run this script
12-
if [ "$(id -u)" -ne 0 ]; then
13-
echo "Error: This script must be run as root"
14-
exit 1
15-
fi
22+
#if [ "$(id -u)" -ne 0 ]; then
23+
# echo "Error: This script must be run as root"
24+
# exit 1
25+
#fi
1626

1727
# Check if mariadb is running
1828
echo "Checking if MariaDB is running..."
19-
if ! mariadb-admin ping -h localhost --silent; then
29+
if ! sudo mariadb-admin ping -h localhost --silent; then
2030
echo "Error: MariaDB is not running"
2131
exit 1
2232
fi
2333

2434
# Setup the database and user for the application
2535
echo "Setting up database and user..."
26-
mariadb -u root <<EOF
36+
sudo mariadb -u root <<EOF
2737
CREATE DATABASE IF NOT EXISTS ${db_name};
28-
CREATE USER IF NOT EXISTS '${db_user}'@'%' IDENTIFIED BY '${db_password}';
29-
GRANT ALL PRIVILEGES ON ${db_name}.* TO '${db_user}'@'%';
38+
CREATE USER IF NOT EXISTS '${db_user}'@'${vault_host}' IDENTIFIED BY '${db_password}';
39+
GRANT ALL PRIVILEGES ON ${db_name}.* TO '${db_user}'@'${vault_host}';
3040
FLUSH PRIVILEGES;
3141
EOF
3242

3343
# Check if the database was created
34-
if ! mariadb -u root -e "USE ${db_name}"; then
44+
if ! sudo mariadb -u root -e "USE ${db_name}"; then
3545
echo "Error: Failed to create database"
3646
exit 1
3747
fi
3848

3949
# Check if the user was created
40-
if ! mariadb -u root -e "SELECT User FROM mysql.user WHERE User='${db_user}'"; then
50+
if ! sudo mariadb -u root -e "SELECT User FROM mysql.user WHERE User='${db_user}'"; then
4151
echo "Error: Failed to create user"
4252
exit 1
4353
fi
4454

4555
# Setup Vault user and grant privileges
4656
# This user is used by the Vault server to create ephemeral users for applications
4757
echo "Setting up Vault user and granting privileges..."
48-
mariadb -u root <<EOF
49-
CREATE USER IF NOT EXISTS '${vault_user}'@'%' IDENTIFIED BY '${vault_password}';
50-
GRANT CREATE USER, DROP, SELECT, INSERT, UPDATE, DELETE, ALTER, EXECUTE ON *.* TO '${vault_user}'@'%';
51-
GRANT ALL PRIVILEGES ON mysql.* TO '${vault_user}'@'%';
52-
GRANT ALL PRIVILEGES ON ${db_name}.* TO '${vault_user}'@'%' WITH GRANT OPTION;
58+
sudo mariadb -u root <<EOF
59+
CREATE USER IF NOT EXISTS '${vault_user}'@'${vault_host}' IDENTIFIED BY '${vault_password}';
60+
GRANT CREATE USER, DROP, SELECT, INSERT, UPDATE, DELETE, ALTER, EXECUTE ON *.* TO '${vault_user}'@'${vault_host}';
61+
GRANT ALL PRIVILEGES ON mysql.* TO '${vault_user}'@'${vault_host}';
62+
GRANT ALL PRIVILEGES ON ${db_name}.* TO '${vault_user}'@'${vault_host}' WITH GRANT OPTION;
5363
FLUSH PRIVILEGES;
5464
EOF
5565

5666
# Check if the Vault user was created
57-
if ! mariadb -u root -e "SELECT User FROM mysql.user WHERE User='${vault_user}'"; then
67+
if ! sudo mariadb -u root -e "SELECT User FROM mysql.user WHERE User='${vault_user}'"; then
5868
echo "Error: Failed to create Vault user"
5969
exit 1
6070
fi
6171

6272
# Check if the Vault user has the correct privileges
63-
if ! mariadb -u root -e "SHOW GRANTS FOR '${vault_user}'@'%'"; then
73+
if ! sudo mariadb -u root -e "SHOW GRANTS FOR '${vault_user}'@'${vault_host}'"; then
6474
echo "Error: Failed to grant privileges to Vault user"
6575
exit 1
6676
fi

setup-vault.sh

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
#!/bin/bash
22

3-
set -e # Exit on error
3+
# set -e # Exit on error
44
set -u # Treat unset variables as errors
55
set -o pipefail # Catch pipeline errors
66

77
# Vault address and login
8-
VAULT_ADDR="http://192.168.100.1:8200"
8+
VAULT_ADDR="http://localhost:8200"
99
export VAULT_ADDR
1010

1111
# Always make sure these are synced with the backend config
1212
DB_NAME="lifehub"
13-
DB_HOST="192.168.100.1"
13+
DB_HOST="localhost"
1414
VAULT_DB_USER="vault"
1515
VAULT_DB_ROLE="lifehub-app"
1616
VAULT_DB_ADMIN_ROLE="lifehub-admin"
@@ -70,8 +70,8 @@ else
7070
fi
7171

7272
# --- Setup Vault Policies ---
73-
echo "Applying user-keystore policy..."
74-
vault policy write user-keystore - <<EOF
73+
echo "Applying lifehub-user policy..."
74+
vault policy write lifehub-user - <<EOF
7575
# Allow users to encrypt data using only their own KEK
7676
path "transit/lifehub/encrypt/user-{{identity.entity.id}}" {
7777
capabilities = ["update"]
@@ -88,8 +88,8 @@ path "kv/lifehub/user-keys/{{identity.entity.id}}" {
8888
}
8989
EOF
9090

91-
echo "Applying backend-keystore policy..."
92-
vault policy write backend-keystore - <<EOF
91+
echo "Applying lifehub-app policy..."
92+
vault policy write lifehub-app - <<EOF
9393
# Backend can encrypt data using any user's KEK
9494
path "transit/lifehub/encrypt/user-*" {
9595
capabilities = ["update"]
@@ -105,10 +105,26 @@ path "transit/lifehub/keys/user-*" {
105105
capabilities = ["create", "update", "read", "delete"]
106106
}
107107
108-
# Backend has full access to all user KEK metadata in Vault KV
109-
path "kv/lifehub/user-keys/*" {
108+
# Backend has full access to all data in the lifehub KV store
109+
path "kv/lifehub/*" {
110110
capabilities = ["create", "update", "read", "delete"]
111111
}
112+
113+
# Backend can read and write to the lifehub database
114+
path "database/lifehub/*" {
115+
capabilities = ["create", "read", "update", "delete"]
116+
}
112117
EOF
113118

119+
# --- Setup Vault Auth Methods ---
120+
echo "Enabling AppRole authentication..."
121+
vault auth enable approle &> /dev/null || echo "AppRole authentication already enabled."
122+
123+
echo "Creating AppRole for lifehub-app..."
124+
vault write auth/approle/role/lifehub-app \
125+
token_policies="lifehub-app" \
126+
token_ttl="10m" \
127+
token_max_ttl="30m" \
128+
secret_id_num_uses="1" &> /dev/null
129+
114130
echo "Vault setup completed successfully!"

0 commit comments

Comments
 (0)