Skip to content

Commit 7813075

Browse files
committed
feat: database encryption
1 parent b8afb37 commit 7813075

3 files changed

Lines changed: 22 additions & 4 deletions

File tree

backend/Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

backend/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ axum-login = "0.16.0"
2525
http = "1.0.0"
2626
password-auth = { version = "1.0.0", default-features = false, features = ["argon2"] }
2727
serde = "1.0.0"
28-
sqlx = { version = "0.8.1", default-features = false, features = ["json"] }
28+
sqlx = { version = "0.8.1", default-features = false, features = ["json", "sqlite"] }
29+
libsqlite3-sys = { version = "0.30.1", default-features = false, features = ["bundled-sqlcipher"] }
2930
time = { version = "0.3.30", default-features = false }
3031
tokio = { workspace = true }
3132
futures-util = { workspace = true }

backend/src/web/app.rs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ use super::{ImageContainer, MdnsChannelMessage};
6060
// TODO: Maybe use `std::future::pending::<()>();` instead of sleeping forever
6161

6262
// TODO: Change default admin and guest hashes, remember to search and update where they're hardcoded
63-
const SQLITE_URL: &str = "sqlite://data.db";
63+
const SQLITE_PROD_URL: &str = "sqlite://oko.db";
64+
const SQLITE_DEV_URL: &str = "sqlite://data.db";
6465
const VIDEO_PATH: &str = "./videos/";
6566
const DEFAULT_ADMIN_USERNAME: &str = "admin";
6667
const DEFAULT_ADMIN_PASS_HASH: &str = "$argon2id$v=19$m=19456,t=2,p=1$VE0e3g7DalWHgDwou3nuRA$uC6TER156UQpk0lNQ5+jHM0l5poVjPA1he/Tyn9J4Zw";
@@ -94,10 +95,25 @@ pub struct App {
9495
}
9596

9697
impl App {
98+
#[allow(clippy::cognitive_complexity)]
9799
#[allow(clippy::similar_names)]
98100
pub async fn new() -> Result<Self, Box<dyn std::error::Error + Send + Sync>> {
99-
let sqlite_connect_options =
100-
SqliteConnectOptions::from_str(SQLITE_URL)?.create_if_missing(true);
101+
let sqlite_connect_options = if cfg!(debug_assertions) {
102+
SqliteConnectOptions::from_str(SQLITE_DEV_URL)?.create_if_missing(true)
103+
} else {
104+
let Ok(password) = std::env::var("OKO_DB_PASSWORD") else {
105+
error!("No password provided for database. Please provide a password using the OKO_DB_PASSWORD environment variable.");
106+
return Err("No password provided for database. Please provide a password using the OKO_DB_PASSWORD environment variable.".into());
107+
};
108+
109+
SqliteConnectOptions::from_str(SQLITE_PROD_URL)?
110+
.create_if_missing(true)
111+
.pragma("key", password)
112+
.pragma("cipher_page_size", "1024")
113+
.pragma("kdf_iter", "64000")
114+
.pragma("cipher_hmac_algorithm", "HMAC_SHA1")
115+
.pragma("cipher_kdf_algorithm", "PBKDF2_HMAC_SHA1")
116+
};
101117

102118
let db = SqlitePool::connect_with(sqlite_connect_options).await?;
103119

0 commit comments

Comments
 (0)