Space Bot commited on
Commit
8d2dc53
·
1 Parent(s): 36905d3

Add database encryption system with initial backup

Browse files
Files changed (4) hide show
  1. .gitignore +2 -0
  2. .space/Dockerfile +0 -14
  3. backend/scripts/db_crypt.sh +100 -0
  4. backend/start.sh +5 -0
.gitignore CHANGED
@@ -307,3 +307,5 @@ dist
307
  cypress/videos
308
  cypress/screenshots
309
  .vscode/settings.json
 
 
 
307
  cypress/videos
308
  cypress/screenshots
309
  .vscode/settings.json
310
+
311
+ webui.db
.space/Dockerfile DELETED
@@ -1,14 +0,0 @@
1
- FROM ghcr.io/open-webui/open-webui:main
2
-
3
- # Install gpg
4
- USER root
5
- RUN apt-get update && \
6
- apt-get install -y --no-install-recommends gpg && \
7
- rm -rf /var/lib/apt/lists/* && \
8
- gpg --version # Verify installation
9
-
10
- # Set correct permissions
11
- RUN mkdir -p /app/backend/db_backup && \
12
- chown -R 1000:1000 /app/backend/db_backup
13
-
14
- USER 1000
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
backend/scripts/db_crypt.sh ADDED
@@ -0,0 +1,100 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env bash
2
+
3
+ SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
4
+ BACKEND_DIR="$(realpath "$SCRIPT_DIR/..")"
5
+ SPACE_NAME="thryyyyy/open-webui"
6
+
7
+ function check_requirements() {
8
+ if ! command -v gpg >/dev/null; then
9
+ echo "Error: gpg is not installed"
10
+ return 1
11
+ fi
12
+ }
13
+
14
+ function validate_secrets() {
15
+ if [ -z "$BACKUP_PASSPHRASE" ]; then
16
+ echo "Error: BACKUP_PASSPHRASE secret not set"
17
+ return 1
18
+ fi
19
+
20
+ if [ -z "$HF_TOKEN" ]; then
21
+ echo "Error: HF_TOKEN secret not set"
22
+ return 1
23
+ fi
24
+ }
25
+
26
+ function decrypt_database() {
27
+ validate_secrets || return 1
28
+
29
+ mkdir -p "$BACKEND_DIR/data"
30
+
31
+ if [ -f "$BACKEND_DIR/db_backup/webui.db.gpg" ]; then
32
+ echo "Decrypting database backup..."
33
+ gpg --batch --yes --passphrase "$BACKUP_PASSPHRASE" -d \
34
+ -o "$BACKEND_DIR/data/webui.db" "$BACKEND_DIR/db_backup/webui.db.gpg"
35
+
36
+ if [ $? -eq 0 ]; then
37
+ echo "Database decrypted successfully"
38
+ return 0
39
+ else
40
+ echo "Failed to decrypt database"
41
+ return 1
42
+ fi
43
+ else
44
+ echo "No encrypted backup found at db_backup/webui.db.gpg"
45
+ # Not an error, might be first run
46
+ return 0
47
+ fi
48
+ }
49
+
50
+ function encrypt_database() {
51
+ validate_secrets || return 1
52
+
53
+ if [ ! -f "$BACKEND_DIR/data/webui.db" ]; then
54
+ echo "Database not found at data/webui.db"
55
+ return 1
56
+ fi
57
+
58
+ mkdir -p "$BACKEND_DIR/db_backup"
59
+
60
+ echo "Encrypting database..."
61
+ gpg --batch --yes --passphrase "$BACKUP_PASSPHRASE" -c --cipher-algo AES256 \
62
+ -o "$BACKEND_DIR/db_backup/webui.db.gpg" "$BACKEND_DIR/data/webui.db"
63
+
64
+ if [ $? -eq 0 ]; then
65
+ echo "Database encrypted successfully"
66
+ cd "$BACKEND_DIR" || exit 1
67
+
68
+ # Configure Git for this operation
69
+ git config --local user.email "[email protected]"
70
+ git config --local user.name "Space Bot"
71
+
72
+ echo "Committing and pushing changes..."
73
+ git add db_backup/webui.db.gpg
74
+ git commit -m "Update encrypted database backup"
75
+
76
+ # Push using the token
77
+ REPO_URL="https://user:[email protected]/spaces/$SPACE_NAME"
78
+ if git push "$REPO_URL" main; then
79
+ echo "Successfully pushed backup to repository"
80
+ cd - >/dev/null
81
+ return 0
82
+ else
83
+ echo "Failed to push to repository"
84
+ cd - >/dev/null
85
+ return 1
86
+ fi
87
+ else
88
+ echo "Failed to encrypt database"
89
+ return 1
90
+ fi
91
+ }
92
+
93
+ # Check requirements first
94
+ check_requirements || exit 1
95
+
96
+ # If script is run directly, default to encryption
97
+ # (equivalent to fish's: if test (status filename) = (status -f))
98
+ if [ "${BASH_SOURCE[0]}" = "$0" ]; then
99
+ encrypt_database
100
+ fi
backend/start.sh CHANGED
@@ -2,6 +2,11 @@
2
 
3
  SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
4
  cd "$SCRIPT_DIR" || exit
 
 
 
 
 
5
 
6
  KEY_FILE=.webui_secret_key
7
 
 
2
 
3
  SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
4
  cd "$SCRIPT_DIR" || exit
5
+ # Source encryption functions
6
+ source scripts/db_crypt.sh
7
+
8
+ # Check requirements and decrypt database if backup exists
9
+ check_requirements && decrypt_database
10
 
11
  KEY_FILE=.webui_secret_key
12