Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 16 additions & 15 deletions src/backend/database_settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,22 @@ pub(crate) fn get_setting(conn: &Connection, key: &str) -> InventoryResult<Optio
pub(crate) fn get_or_create_credential_store_profile_id(
conn: &Connection,
) -> InventoryResult<String> {
let transaction = conn.unchecked_transaction()?;
let profile_id = match get_setting(&transaction, CREDENTIAL_STORE_PROFILE_ID_SETTING_KEY)? {
Some(profile_id) => validate_credential_store_profile_id(profile_id)?,
None => {
let profile_id = new_credential_store_profile_id();
set_setting(
&transaction,
CREDENTIAL_STORE_PROFILE_ID_SETTING_KEY,
&profile_id,
)?;
profile_id
}
};
transaction.commit()?;
Ok(profile_id)
if let Some(profile_id) = get_setting(conn, CREDENTIAL_STORE_PROFILE_ID_SETTING_KEY)? {
return validate_credential_store_profile_id(profile_id);
}
conn.execute(
"INSERT INTO settings (key, value) VALUES (?1, ?2) ON CONFLICT(key) DO NOTHING",
params![
CREDENTIAL_STORE_PROFILE_ID_SETTING_KEY,
new_credential_store_profile_id()
],
)?;
let stored = conn.query_row(
"SELECT value FROM settings WHERE key = ?1",
[CREDENTIAL_STORE_PROFILE_ID_SETTING_KEY],
|row| row.get(0),
)?;
validate_credential_store_profile_id(stored)
}

pub(crate) fn credential_store_profile_migration_completed(
Expand Down
23 changes: 23 additions & 0 deletions src/backend/filament_database_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5900,3 +5900,26 @@ fn trusted_lan_refresh_respects_reconfigured_and_disabled_interfaces() {
assert!(refresh("192.0.2.2").is_none());
assert!(!db.get_trusted_lan_settings().unwrap().enabled);
}

#[test]
fn simultaneous_credential_profile_initialization_returns_one_identity() {
let path = temp_db_path("concurrent-credential-profile");
let db = FilamentDatabase::open(&path).unwrap();
db.apply_schema().unwrap();
let barrier = std::sync::Barrier::new(8);
std::thread::scope(|scope| {
let threads: Vec<_> = (0..8)
.map(|_| {
scope.spawn(|| {
let db = FilamentDatabase::open(&path).unwrap();
barrier.wait();
db.get_or_create_credential_store_profile_id().unwrap()
})
})
.collect();
let ids: Vec<_> = threads.into_iter().map(|t| t.join().unwrap()).collect();
assert!(ids.iter().all(|id| id == &ids[0]));
});
drop(db);
std::fs::remove_file(path).unwrap();
}
Loading