Remove rocksdb

This commit is contained in:
Jeremy Wall 2022-09-01 14:26:55 -04:00
parent 26a98ae89a
commit 0cc411590e
5 changed files with 13 additions and 311 deletions

View File

@ -3,7 +3,4 @@
"macro-error" "macro-error"
], ],
"rust-analyzer.cargo.noDefaultFeatures": false, "rust-analyzer.cargo.noDefaultFeatures": false,
"rust-analyzer.cargo.features": [
"sqlite"
],
} }

149
Cargo.lock generated
View File

@ -340,25 +340,6 @@ dependencies = [
"serde", "serde",
] ]
[[package]]
name = "bindgen"
version = "0.60.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "062dddbc1ba4aca46de6338e2bf87771414c335f7b2f2036e8f3e9befebf88e6"
dependencies = [
"bitflags",
"cexpr",
"clang-sys",
"lazy_static",
"lazycell",
"peeking_take_while",
"proc-macro2",
"quote",
"regex",
"rustc-hash",
"shlex",
]
[[package]] [[package]]
name = "bitflags" name = "bitflags"
version = "1.3.2" version = "1.3.2"
@ -451,17 +432,6 @@ version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f0b3de4a0c5e67e16066a0715723abd91edc2f9001d09c46e1dca929351e130e" checksum = "f0b3de4a0c5e67e16066a0715723abd91edc2f9001d09c46e1dca929351e130e"
[[package]]
name = "bzip2-sys"
version = "0.1.11+1.0.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "736a955f3fa7875102d57c82b8cac37ec45224a07fd32d58f9f7a186b6cd4cdc"
dependencies = [
"cc",
"libc",
"pkg-config",
]
[[package]] [[package]]
name = "cache-padded" name = "cache-padded"
version = "1.2.0" version = "1.2.0"
@ -473,18 +443,6 @@ name = "cc"
version = "1.0.73" version = "1.0.73"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2fff2a6927b3bb87f9595d67196a70493f627687a71d87a0d692242c33f58c11" checksum = "2fff2a6927b3bb87f9595d67196a70493f627687a71d87a0d692242c33f58c11"
dependencies = [
"jobserver",
]
[[package]]
name = "cexpr"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6fac387a98bb7c37292057cffc56d62ecb629900026402633ae9160df93a8766"
dependencies = [
"nom",
]
[[package]] [[package]]
name = "cfg-if" name = "cfg-if"
@ -539,17 +497,6 @@ dependencies = [
"half", "half",
] ]
[[package]]
name = "clang-sys"
version = "1.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5a050e2153c5be08febd6734e29298e844fdb0fa21aeddd63b4eb7baa106c69b"
dependencies = [
"glob",
"libc",
"libloading",
]
[[package]] [[package]]
name = "clap" name = "clap"
version = "3.2.16" version = "3.2.16"
@ -934,12 +881,6 @@ dependencies = [
"wasi 0.11.0+wasi-snapshot-preview1", "wasi 0.11.0+wasi-snapshot-preview1",
] ]
[[package]]
name = "glob"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9b919933a397b79c37e33b77bb2aa3dc8eb6e165ad809e58ff75bc7db2e34574"
[[package]] [[package]]
name = "gloo-net" name = "gloo-net"
version = "0.1.0" version = "0.1.0"
@ -1200,15 +1141,6 @@ version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "112c678d4050afce233f4f2852bb2eb519230b3cf12f33585275537d7e41578d" checksum = "112c678d4050afce233f4f2852bb2eb519230b3cf12f33585275537d7e41578d"
[[package]]
name = "jobserver"
version = "0.1.24"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "af25a77299a7f711a01975c35a6a424eb6862092cc2d6c72c4ed6cbc56dfc1fa"
dependencies = [
"libc",
]
[[package]] [[package]]
name = "js-sys" name = "js-sys"
version = "0.3.58" version = "0.3.58"
@ -1235,7 +1167,6 @@ dependencies = [
"mime_guess", "mime_guess",
"recipe-store", "recipe-store",
"recipes", "recipes",
"rocksdb",
"rust-embed", "rust-embed",
"secrecy", "secrecy",
"serde", "serde",
@ -1280,12 +1211,6 @@ version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]]
name = "lazycell"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "830d08ce1d1d941e6b30645f1a0eb5643013d835ce3779a5fc208261dbe10f55"
[[package]] [[package]]
name = "lexical" name = "lexical"
version = "6.1.1" version = "6.1.1"
@ -1365,31 +1290,6 @@ version = "0.2.126"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "349d5a591cd28b49e1d1037471617a32ddcda5731b99419008085f72d5a53836" checksum = "349d5a591cd28b49e1d1037471617a32ddcda5731b99419008085f72d5a53836"
[[package]]
name = "libloading"
version = "0.7.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "efbc0f03f9a775e9f6aed295c6a1ba2253c5757a9e03d55c6caa46a681abcddd"
dependencies = [
"cfg-if 1.0.0",
"winapi",
]
[[package]]
name = "librocksdb-sys"
version = "0.8.0+7.4.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "611804e4666a25136fcc5f8cf425ab4d26c7f74ea245ffe92ea23b85b6420b5d"
dependencies = [
"bindgen",
"bzip2-sys",
"cc",
"glob",
"libc",
"libz-sys",
"zstd-sys",
]
[[package]] [[package]]
name = "libsqlite3-sys" name = "libsqlite3-sys"
version = "0.24.2" version = "0.24.2"
@ -1401,17 +1301,6 @@ dependencies = [
"vcpkg", "vcpkg",
] ]
[[package]]
name = "libz-sys"
version = "1.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9702761c3935f8cc2f101793272e202c72b99da8f4224a19ddcf1279a6450bbf"
dependencies = [
"cc",
"pkg-config",
"vcpkg",
]
[[package]] [[package]]
name = "lock_api" name = "lock_api"
version = "0.4.8" version = "0.4.8"
@ -1621,12 +1510,6 @@ version = "1.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0c520e05135d6e763148b6426a837e239041653ba7becd2e538c076c738025fc" checksum = "0c520e05135d6e763148b6426a837e239041653ba7becd2e538c076c738025fc"
[[package]]
name = "peeking_take_while"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "19b17cddbe7ec3f8bc800887bab5e717348c95ea2ca0b1bf0837fb964dc67099"
[[package]] [[package]]
name = "percent-encoding" name = "percent-encoding"
version = "2.1.0" version = "2.1.0"
@ -1828,16 +1711,6 @@ dependencies = [
"winapi", "winapi",
] ]
[[package]]
name = "rocksdb"
version = "0.19.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7e9562ea1d70c0cc63a34a22d977753b50cca91cc6b6527750463bd5dd8697bc"
dependencies = [
"libc",
"librocksdb-sys",
]
[[package]] [[package]]
name = "rust-embed" name = "rust-embed"
version = "6.4.0" version = "6.4.0"
@ -1872,12 +1745,6 @@ dependencies = [
"walkdir", "walkdir",
] ]
[[package]]
name = "rustc-hash"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2"
[[package]] [[package]]
name = "rustls" name = "rustls"
version = "0.20.6" version = "0.20.6"
@ -2027,12 +1894,6 @@ dependencies = [
"lazy_static", "lazy_static",
] ]
[[package]]
name = "shlex"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "43b2853a4d09f215c24cc5489c992ce46052d359b5109343cbafbf26bc62f8a3"
[[package]] [[package]]
name = "signal-hook" name = "signal-hook"
version = "0.3.14" version = "0.3.14"
@ -2854,13 +2715,3 @@ name = "zeroize"
version = "1.5.7" version = "1.5.7"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c394b5bd0c6f669e7275d9c20aa90ae064cb22e75a1cad54e1b34088034b149f" checksum = "c394b5bd0c6f669e7275d9c20aa90ae064cb22e75a1cad54e1b34088034b149f"
[[package]]
name = "zstd-sys"
version = "2.0.1+zstd.1.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9fd07cbbc53846d9145dbffdf6dd09a7a0aa52be46741825f5c97bdd4f73f12b"
dependencies = [
"cc",
"libc",
]

View File

@ -12,9 +12,6 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
sqlx-prepare: wasm kitches/src/*.rs
cd kitchen; cargo sqlx-prepare
kitchen: wasm kitchen/src/*.rs kitchen: wasm kitchen/src/*.rs
cd kitchen; cargo build cd kitchen; cargo build
@ -38,4 +35,7 @@ wasm-dist: web/src/*.rs web/src/components/*.rs
clean: clean:
rm -rf web/dist/* rm -rf web/dist/*
cargo clean cargo clean
sqlx-prepare: wasm kitches/src/*.rs
cd kitchen; cargo sqlx-prepare

View File

@ -54,14 +54,4 @@ features = ["tokio1"]
[dependencies.sqlx] [dependencies.sqlx]
version = "0.6.1" version = "0.6.1"
features = ["sqlite", "runtime-async-std-rustls", "offline"] features = ["sqlite", "runtime-async-std-rustls", "offline"]
optional = true
[dependencies.rocksdb]
version = "0.19.0"
optional = true
[features]
sqlite = ["dep:sqlx"]
rocksdb = ["dep:rocksdb"]
default = ["sqlite"]

View File

@ -13,6 +13,7 @@
// limitations under the License. // limitations under the License.
use async_std::sync::Arc; use async_std::sync::Arc;
use std::path::Path; use std::path::Path;
use std::str::FromStr;
use argon2::{ use argon2::{
password_hash::{rand_core::OsRng, PasswordHash, PasswordHasher, PasswordVerifier, SaltString}, password_hash::{rand_core::OsRng, PasswordHash, PasswordHasher, PasswordVerifier, SaltString},
@ -26,18 +27,15 @@ use axum::{
http::StatusCode, http::StatusCode,
}; };
use ciborium; use ciborium;
use cookie::{Cookie as CookieParse, SameSite};
#[cfg(feature = "rocksdb")]
use rocksdb::{
BoundColumnFamily, ColumnFamilyDescriptor, DBWithThreadMode, MultiThreaded, Options,
};
use secrecy::{ExposeSecret, Secret}; use secrecy::{ExposeSecret, Secret};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use sqlx::{
self,
sqlite::{SqliteConnectOptions, SqliteJournalMode},
SqlitePool,
};
use tracing::{debug, error, info, instrument}; use tracing::{debug, error, info, instrument};
const SESSION_CF: &'static str = "kitchen_session";
const USER_CF: &'static str = "kitchen_users";
pub const AXUM_SESSION_COOKIE_NAME: &'static str = "kitchen-session-cookie"; pub const AXUM_SESSION_COOKIE_NAME: &'static str = "kitchen-session-cookie";
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
@ -86,129 +84,6 @@ pub trait AuthStore: SessionStore {
async fn store_user_creds(&self, user_creds: UserCreds) -> async_session::Result<()>; async fn store_user_creds(&self, user_creds: UserCreds) -> async_session::Result<()>;
} }
#[cfg(feature = "rocksdb")]
#[derive(Clone, Debug)]
pub struct RocksdbStore {
db: Arc<DBWithThreadMode<MultiThreaded>>,
}
#[cfg(feature = "rocksdb")]
impl RocksdbStore {
pub fn new<P: AsRef<Path>>(name: P) -> Result<Self, rocksdb::Error> {
let session_cf_opts = Options::default();
let session_cf = ColumnFamilyDescriptor::new(SESSION_CF, session_cf_opts);
let user_cf_opts = Options::default();
let user_cf = ColumnFamilyDescriptor::new(USER_CF, user_cf_opts);
let mut opts = Options::default();
opts.create_missing_column_families(true);
opts.create_if_missing(true);
Ok(Self {
db: Arc::new(DBWithThreadMode::open_cf_descriptors(
&opts,
name,
vec![session_cf, user_cf],
)?),
})
}
fn get_session_column_family_handle(&self) -> Option<Arc<BoundColumnFamily>> {
self.db.cf_handle(SESSION_CF)
}
fn get_users_column_family_handle(&self) -> Option<Arc<BoundColumnFamily>> {
self.db.cf_handle(USER_CF)
}
}
#[cfg(feature = "rocksdb")]
#[async_trait]
impl SessionStore for RocksdbStore {
#[instrument]
async fn load_session(&self, cookie_value: String) -> async_session::Result<Option<Session>> {
let id = make_id_key(&cookie_value)?;
let cf_handle = self
.get_session_column_family_handle()
.expect(&format!("column family {} is missing", SESSION_CF));
if let Some(payload) = self.db.get_cf(&cf_handle, id.as_bytes())? {
let session: Session = ciborium::de::from_reader(payload.as_slice())?;
return Ok(Some(session));
}
Ok(None)
}
#[instrument]
async fn store_session(&self, session: Session) -> async_session::Result<Option<String>> {
let id = session.id();
let mut payload: Vec<u8> = Vec::new();
let cf_handle = self
.get_session_column_family_handle()
.expect(&format!("column family {} is missing", SESSION_CF));
ciborium::ser::into_writer(&session, &mut payload)?;
self.db
.put_cf(&cf_handle, id.as_bytes(), payload.as_slice())?;
Ok(session.into_cookie_value())
}
#[instrument]
async fn destroy_session(&self, session: Session) -> async_session::Result {
let id = session.id();
let cf_handle = self
.get_session_column_family_handle()
.expect(&format!("column family {} is missing", SESSION_CF));
self.db.delete_cf(&cf_handle, id.as_bytes())?;
Ok(())
}
#[instrument]
async fn clear_store(&self) -> async_session::Result {
self.db.drop_cf(SESSION_CF)?;
Ok(())
}
}
#[cfg(feature = "rocksdb")]
#[async_trait]
impl AuthStore for RocksdbStore {
#[instrument(fields(user=%user_creds.id.0), skip_all)]
async fn check_user_creds(&self, user_creds: &UserCreds) -> async_session::Result<bool> {
// TODO(jwall): Make this function asynchronous.
info!("checking credentials for user");
let cf_handle = self
.get_users_column_family_handle()
.expect(&format!("column family {} is missing", USER_CF));
if let Some(payload) = self
.db
.get_cf(&cf_handle, user_creds.user_id().as_bytes())?
{
debug!("Found user in credential store");
let payload = String::from_utf8_lossy(payload.as_slice()).to_string();
return Ok(check_pass(&payload, &user_creds.pass));
}
Ok(false)
}
// TODO(jwall): Make this function asynchronous.
#[instrument(fields(user=%user_creds.id.0), skip_all)]
async fn store_user_creds(&self, user_creds: UserCreds) -> async_session::Result<()> {
// TODO(jwall): Enforce a password length?
// TODO(jwall): Make this function asynchronous.
info!("storing credentials for user {}", user_creds.id.0);
let cf_handle = self
.get_users_column_family_handle()
.expect(&format!("column family {} is missing", USER_CF));
let salt = SaltString::generate(&mut OsRng);
let password_hash = Argon2::default()
.hash_password(user_creds.pass.expose_secret().as_bytes(), &salt)
.expect("failed to hash password");
self.db.put_cf(
&cf_handle,
user_creds.id.0.as_bytes(),
password_hash.to_string().as_bytes(),
)?;
Ok(())
}
}
#[async_trait] #[async_trait]
impl<B> FromRequest<B> for UserIdFromSession impl<B> FromRequest<B> for UserIdFromSession
where where
@ -224,6 +99,7 @@ where
let cookies = Option::<TypedHeader<Cookie>>::from_request(req) let cookies = Option::<TypedHeader<Cookie>>::from_request(req)
.await .await
.unwrap(); .unwrap();
// TODO(jwall): We should really validate the expiration and such on this cookie.
if let Some(session_cookie) = cookies if let Some(session_cookie) = cookies
.as_ref() .as_ref()
.and_then(|c| c.get(AXUM_SESSION_COOKIE_NAME)) .and_then(|c| c.get(AXUM_SESSION_COOKIE_NAME))
@ -232,6 +108,7 @@ where
match session_store.load_session(session_cookie.to_owned()).await { match session_store.load_session(session_cookie.to_owned()).await {
Ok(Some(session)) => { Ok(Some(session)) => {
if let Some(user_id) = session.get::<UserId>("user_id") { if let Some(user_id) = session.get::<UserId>("user_id") {
info!(user_id = user_id.0, "Found Authenticated session");
return Ok(Self::FoundUserId(user_id)); return Ok(Self::FoundUserId(user_id));
} else { } else {
error!("No user id found in session"); error!("No user id found in session");
@ -254,23 +131,12 @@ where
} }
} }
#[cfg(feature = "sqlite")]
use sqlx::{
self,
sqlite::{SqliteConnectOptions, SqliteJournalMode},
SqlitePool,
};
#[cfg(feature = "sqlite")]
use std::str::FromStr;
#[cfg(feature = "sqlite")]
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct SqliteStore { pub struct SqliteStore {
pool: Arc<SqlitePool>, pool: Arc<SqlitePool>,
url: String, url: String,
} }
#[cfg(feature = "sqlite")]
impl SqliteStore { impl SqliteStore {
pub async fn new<P: AsRef<Path>>(path: P) -> sqlx::Result<Self> { pub async fn new<P: AsRef<Path>>(path: P) -> sqlx::Result<Self> {
let url = format!("sqlite://{}/store.db", path.as_ref().to_string_lossy()); let url = format!("sqlite://{}/store.db", path.as_ref().to_string_lossy());
@ -280,7 +146,6 @@ impl SqliteStore {
} }
} }
#[cfg(feature = "sqlite")]
#[async_trait] #[async_trait]
impl SessionStore for SqliteStore { impl SessionStore for SqliteStore {
#[instrument(fields(conn_string=self.url), skip_all)] #[instrument(fields(conn_string=self.url), skip_all)]
@ -333,7 +198,6 @@ impl SessionStore for SqliteStore {
} }
} }
#[cfg(feature = "sqlite")]
#[async_trait] #[async_trait]
impl AuthStore for SqliteStore { impl AuthStore for SqliteStore {
#[instrument(fields(user=%user_creds.id.0, conn_string=self.url), skip_all)] #[instrument(fields(user=%user_creds.id.0, conn_string=self.url), skip_all)]