formatting pass
This commit is contained in:
1 parent
d976eebbbc
commit
e4cfa53bce
21 files changed
+333
-194
No files matched your search
+25
-16
@@ -1,7 +1,7 @@
|
||||
use super::Db;
|
||||
use crate::credentials::AwsCredential;
|
||||
use duckdb::{Error, params};
|
||||
use uuid::Uuid;
|
||||
use crate::credentials::AwsCredential;
|
||||
use super::Db;
|
||||
|
||||
impl Db {
|
||||
pub fn load_aws_credential(&self, name: &str) -> Result<AwsCredential, Error> {
|
||||
@@ -16,13 +16,15 @@ impl Db {
|
||||
FROM aws_credentials
|
||||
WHERE name = ?",
|
||||
params![name],
|
||||
|row| Ok(AwsCredential {
|
||||
id: row.get(0)?,
|
||||
name: row.get(1)?,
|
||||
access_key_id: row.get(2)?,
|
||||
secret_access_key: row.get(3)?,
|
||||
is_default: row.get(4)?,
|
||||
})
|
||||
|row| {
|
||||
Ok(AwsCredential {
|
||||
id: row.get(0)?,
|
||||
name: row.get(1)?,
|
||||
access_key_id: row.get(2)?,
|
||||
secret_access_key: row.get(3)?,
|
||||
is_default: row.get(4)?,
|
||||
})
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
@@ -53,7 +55,13 @@ impl Db {
|
||||
access_key_id = EXCLUDED.access_key_id,
|
||||
secret_access_key = EXCLUDED.secret_access_key,
|
||||
is_default = EXCLUDED.is_default",
|
||||
params![cred.id, cred.name, cred.access_key_id, cred.secret_access_key, cred.is_default],
|
||||
params![
|
||||
cred.id,
|
||||
cred.name,
|
||||
cred.access_key_id,
|
||||
cred.secret_access_key,
|
||||
cred.is_default
|
||||
],
|
||||
)?;
|
||||
tx.commit()
|
||||
}
|
||||
@@ -74,25 +82,26 @@ impl Db {
|
||||
secret_access_key,
|
||||
is_default
|
||||
FROM aws_credentials
|
||||
ORDER BY created_at"
|
||||
ORDER BY created_at",
|
||||
)?;
|
||||
stmt.query_map([], |row| {
|
||||
Ok(AwsCredential{
|
||||
Ok(AwsCredential {
|
||||
id: row.get(0)?,
|
||||
name: row.get(1)?,
|
||||
access_key_id: row.get(2)?,
|
||||
secret_access_key: row.get(3)?,
|
||||
is_default: row.get(4)?,
|
||||
})
|
||||
})?.collect()
|
||||
})?
|
||||
.collect()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use testresult::TestResult;
|
||||
use crate::tests::TestEnv;
|
||||
use super::*;
|
||||
use crate::tests::TestEnv;
|
||||
use testresult::TestResult;
|
||||
|
||||
fn test_cred(name: &str, access_key_id: &str) -> AwsCredential {
|
||||
AwsCredential {
|
||||
@@ -150,7 +159,7 @@ mod tests {
|
||||
let (created_at, updated_at): (i64, i64) = conn.query_row(
|
||||
"SELECT created_at, updated_at FROM aws_credentials WHERE name = 'renamed'",
|
||||
[],
|
||||
|row| Ok((row.get(0)?, row.get(1)?))
|
||||
|row| Ok((row.get(0)?, row.get(1)?)),
|
||||
)?;
|
||||
|
||||
assert!(updated_at > created_at);
|
||||
|
||||
+4
-5
@@ -1,6 +1,6 @@
|
||||
use super::Db;
|
||||
use crate::config::PrivateConfig;
|
||||
use crate::error::CreddyError;
|
||||
use super::Db;
|
||||
|
||||
impl Db {
|
||||
pub fn load_private_config(&self) -> Result<PrivateConfig, CreddyError> {
|
||||
@@ -21,11 +21,10 @@ impl Db {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use testresult::TestResult;
|
||||
use tempfile::TempDir;
|
||||
use crate::config::AppConfig;
|
||||
use super::*;
|
||||
|
||||
use crate::config::AppConfig;
|
||||
use tempfile::TempDir;
|
||||
use testresult::TestResult;
|
||||
|
||||
fn test_db() -> Result<(Db, TempDir), CreddyError> {
|
||||
let data_dir = tempfile::tempdir()?;
|
||||
|
||||
+14
-8
@@ -3,16 +3,13 @@ use include_dir::{Dir, include_dir};
|
||||
|
||||
use crate::error::CreddyError;
|
||||
|
||||
|
||||
static MIGRATIONS_DIR: Dir = include_dir!("$CARGO_MANIFEST_DIR/migrations");
|
||||
|
||||
|
||||
struct Migration {
|
||||
id: &'static str,
|
||||
sql: &'static str,
|
||||
}
|
||||
|
||||
|
||||
/// Run all unapplied database migrations.
|
||||
pub fn migrate(conn: &mut Connection) -> Result<(), CreddyError> {
|
||||
apply_migrations(conn, &embedded_migrations())
|
||||
@@ -25,7 +22,11 @@ fn embedded_migrations() -> Vec<Migration> {
|
||||
.filter(|f| f.path().extension().is_some_and(|ext| ext == "sql"))
|
||||
.map(|f| Migration {
|
||||
// these are baked in at compile time, so a bad file is a build problem, not a runtime one
|
||||
id: f.path().file_name().and_then(|n| n.to_str()).expect("migration filename is not UTF-8"),
|
||||
id: f
|
||||
.path()
|
||||
.file_name()
|
||||
.and_then(|n| n.to_str())
|
||||
.expect("migration filename is not UTF-8"),
|
||||
sql: f.contents_utf8().expect("migration file is not UTF-8"),
|
||||
})
|
||||
.collect();
|
||||
@@ -81,11 +82,10 @@ fn list_applied_migrations(conn: &Connection) -> Result<Vec<String>, CreddyError
|
||||
Ok(ids)
|
||||
}
|
||||
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use testresult::TestResult;
|
||||
use super::*;
|
||||
use testresult::TestResult;
|
||||
|
||||
const FIRST: Migration = Migration {
|
||||
id: "0001_first.sql",
|
||||
@@ -110,7 +110,10 @@ mod tests {
|
||||
migrate(&mut conn)?;
|
||||
|
||||
let applied = list_applied_migrations(&conn)?;
|
||||
let expected: Vec<_> = embedded_migrations().iter().map(|m| m.id.to_string()).collect();
|
||||
let expected: Vec<_> = embedded_migrations()
|
||||
.iter()
|
||||
.map(|m| m.id.to_string())
|
||||
.collect();
|
||||
assert_eq!(applied, expected);
|
||||
Ok(())
|
||||
}
|
||||
@@ -123,7 +126,10 @@ mod tests {
|
||||
|
||||
// already-applied migrations are skipped, so re-running FIRST would fail if it weren't
|
||||
apply_migrations(&mut conn, &[FIRST, SECOND])?;
|
||||
assert_eq!(list_applied_migrations(&conn)?, ["0001_first.sql", "0002_second.sql"]);
|
||||
assert_eq!(
|
||||
list_applied_migrations(&conn)?,
|
||||
["0001_first.sql", "0002_second.sql"]
|
||||
);
|
||||
assert!(table_exists(&conn, "b")?);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
+17
-20
@@ -1,14 +1,11 @@
|
||||
use argon2::{Algorithm, Argon2, Error as Argon2Error, ParamsBuilder, Version};
|
||||
use base64ct::{Base64, Encoding};
|
||||
use duckdb::{Connection, params};
|
||||
use std::fs;
|
||||
use std::sync::Mutex;
|
||||
use argon2::{Algorithm, Argon2, Version, Error as Argon2Error, ParamsBuilder};
|
||||
use base64ct::{Base64, Encoding};
|
||||
use duckdb::{
|
||||
Connection,
|
||||
params,
|
||||
};
|
||||
|
||||
use crate::error::CreddyError;
|
||||
use crate::config::AppConfig;
|
||||
use crate::error::CreddyError;
|
||||
|
||||
mod aws;
|
||||
mod config;
|
||||
@@ -29,7 +26,6 @@ const TIME_COST: u32 = 1;
|
||||
#[cfg(debug_assertions)]
|
||||
const PARALLELISM_COST: u32 = 1;
|
||||
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Db {
|
||||
connection: Mutex<Connection>,
|
||||
@@ -42,21 +38,21 @@ impl Db {
|
||||
// since we are about to make use of the salt stored in `config`, we need to save it now
|
||||
config.public_config().save(&config.public_config_path())?;
|
||||
|
||||
let path = config.db_path()
|
||||
let path = config
|
||||
.db_path()
|
||||
.into_os_string()
|
||||
.into_string()
|
||||
.map_err(|s| CreddyError::InvalidPath(s))?;
|
||||
let key = Db::derive_key(passphrase, &config.db_salt)?;
|
||||
|
||||
let mut conn = Connection::open_in_memory()?;
|
||||
conn.execute(
|
||||
"ATTACH ? AS creddy (ENCRYPTION_KEY ?);",
|
||||
params![path, key],
|
||||
)?;
|
||||
conn.execute("ATTACH ? AS creddy (ENCRYPTION_KEY ?);", params![path, key])?;
|
||||
// must come before migrating, or the tables end up in the in-memory db
|
||||
conn.execute("USE creddy;", [])?;
|
||||
migrations::migrate(&mut conn)?;
|
||||
Ok(Self{ connection: Mutex::new(conn) })
|
||||
Ok(Self {
|
||||
connection: Mutex::new(conn),
|
||||
})
|
||||
}
|
||||
|
||||
/// Hash the given passphrase/salt with argon2, then encode to base64 for DuckDB
|
||||
@@ -77,12 +73,11 @@ impl Db {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use testresult::TestResult;
|
||||
use duckdb::Error;
|
||||
use super::*;
|
||||
use duckdb::Error;
|
||||
use testresult::TestResult;
|
||||
|
||||
const TEST_PASSPHRASE: &'static str = "correct horse battery staple";
|
||||
const TEST_SALT: [u8; 32] = [0; 32];
|
||||
@@ -103,9 +98,11 @@ mod tests {
|
||||
Err(Error::DuckDBFailure(error, _desc)) => {
|
||||
assert!(error.code == duckdb::ErrorCode::Unknown);
|
||||
assert!(error.extended_code == 1);
|
||||
},
|
||||
}
|
||||
Err(e) => panic!("Unexpected error type: {e:?}"),
|
||||
Ok(_) => panic!("Should not be able to open newly-created database without encryption key"),
|
||||
Ok(_) => {
|
||||
panic!("Should not be able to open newly-created database without encryption key")
|
||||
}
|
||||
};
|
||||
|
||||
// confirm that using a different passphrase won't work
|
||||
@@ -115,7 +112,7 @@ mod tests {
|
||||
assert!(error.code == duckdb::ErrorCode::Unknown);
|
||||
assert!(error.extended_code == 1);
|
||||
assert!(desc.contains("Wrong encryption key"));
|
||||
},
|
||||
}
|
||||
Err(e) => panic!("Unexpected error type: {e:?}"),
|
||||
Ok(_) => panic!("Should not be able to open database with a different passphrase"),
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user