Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

vault: Use tokio synchronization #845

Merged
merged 1 commit into from
Sep 24, 2024
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
15 changes: 8 additions & 7 deletions flecs-core/src/fsm/console_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ struct SessionIdMiddleware {
vault: Arc<Vault>,
}

impl Default for SessionIdMiddleware {
fn default() -> Self {
impl SessionIdMiddleware {
pub async fn default() -> Self {
Self {
vault: crate::lore::vault::default(),
vault: crate::lore::vault::default().await,
}
}
}

pub fn create_default_client_with_middleware() -> ClientWithMiddleware {
pub async fn create_default_client_with_middleware() -> ClientWithMiddleware {
ClientBuilder::new(reqwest::Client::new())
.with(SessionIdMiddleware::default())
.with(SessionIdMiddleware::default().await)
.build()
}

Expand All @@ -36,7 +36,7 @@ impl SessionIdMiddleware {
debug!("{request:?}");
}

fn handle_response(&self, response: Result<Response>) -> Result<Response> {
async fn handle_response(&self, response: Result<Response>) -> Result<Response> {
debug!("{response:?}");
if let Ok(response) = response {
if let Some(session) = response.headers().get("x-session-id") {
Expand All @@ -48,6 +48,7 @@ impl SessionIdMiddleware {
.reservation()
.reserve_secret_pouch_mut()
.grab()
.await
.secret_pouch_mut
.as_mut()
.unwrap()
Expand Down Expand Up @@ -80,6 +81,6 @@ impl Middleware for SessionIdMiddleware {
) -> Result<Response> {
self.handle_request(&mut req);
let res = next.run(req, extensions).await;
self.handle_response(res)
self.handle_response(res).await
}
}
10 changes: 7 additions & 3 deletions flecs-core/src/fsm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ pub fn init_tracing() {
info!("Tracing initialized");
}

fn create_service() -> IntoMakeServiceWithConnectInfo<Router, UdsConnectInfo> {
let server = server_impl::ServerImpl::default();
async fn create_service() -> IntoMakeServiceWithConnectInfo<Router, UdsConnectInfo> {
let server = server_impl::ServerImpl::default().await;
let app = flecsd_axum_server::server::new(Arc::new(server)).layer(
tower_http::trace::TraceLayer::new_for_http()
.make_span_with(|request: &Request<_>| {
Expand Down Expand Up @@ -137,7 +137,11 @@ async fn serve(
}

pub async fn server(socket_path: PathBuf) -> Result<()> {
serve(create_unix_socket(socket_path).await?, create_service()).await;
serve(
create_unix_socket(socket_path).await?,
create_service().await,
)
.await;
Ok(())
}

Expand Down
14 changes: 7 additions & 7 deletions flecs-core/src/fsm/server_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ pub struct ServerImpl {
vault: Arc<Vault>,
}

impl Default for ServerImpl {
fn default() -> Self {
impl ServerImpl {
pub async fn default() -> Self {
Self {
vault: crate::lore::vault::default(),
vault: crate::lore::vault::default().await,
}
}
}
Expand Down Expand Up @@ -161,7 +161,7 @@ impl Console for ServerImpl {
_host: Host,
_cookies: CookieJar,
) -> Result<ConsoleAuthenticationDeleteResponse, String> {
crate::sorcerer::authmancer::delete_authentication(&self.vault);
crate::sorcerer::authmancer::delete_authentication(&self.vault).await;
Ok(ConsoleAuthenticationDeleteResponse::Status204_NoContent)
}

Expand All @@ -172,7 +172,7 @@ impl Console for ServerImpl {
_cookies: CookieJar,
body: AuthResponseData,
) -> Result<ConsoleAuthenticationPutResponse, String> {
crate::sorcerer::authmancer::store_authentication(body, &self.vault);
crate::sorcerer::authmancer::store_authentication(body, &self.vault).await;
Ok(ConsoleAuthenticationPutResponse::Status204_NoContent)
}
}
Expand All @@ -187,7 +187,7 @@ impl Device for ServerImpl {
) -> Result<DeviceLicenseActivationPostResponse, String> {
match crate::sorcerer::licenso::activate_license(
&self.vault,
crate::lore::console_client_config::default(),
crate::lore::console_client_config::default().await,
)
.await
{
Expand Down Expand Up @@ -224,7 +224,7 @@ impl Device for ServerImpl {
_host: Host,
_cookies: CookieJar,
) -> Result<DeviceLicenseInfoGetResponse, String> {
let secrets = self.vault.get_secrets();
let secrets = self.vault.get_secrets().await;
Ok(DeviceLicenseInfoGetResponse::Status200_Success(
DeviceLicenseInfoGet200Response {
// TODO: Use correct type, as soon as serial numbers are implemented
Expand Down
32 changes: 19 additions & 13 deletions flecs-core/src/lore/mod.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1,40 @@
pub mod console_client_config {
use crate::fsm::console_client::create_default_client_with_middleware;
use flecs_console_client::apis::configuration::Configuration;
use std::sync::OnceLock;
use tokio::sync::OnceCell;

pub fn default() -> &'static Configuration {
static CONSOLE_CLIENT_CONFIG: OnceLock<Configuration> = OnceLock::new();
pub async fn default() -> &'static Configuration {
static CONSOLE_CLIENT_CONFIG: OnceCell<Configuration> = OnceCell::const_new();
#[cfg(debug_assertions)]
const BASE_PATH: &str = "https://console-dev.flecs.tech";
#[cfg(not(debug_assertions))]
const BASE_PATH: &str = "https://console.flecs.tech";
CONSOLE_CLIENT_CONFIG.get_or_init(|| Configuration {
base_path: BASE_PATH.to_owned(),
client: create_default_client_with_middleware(),
..Configuration::default()
})
CONSOLE_CLIENT_CONFIG
.get_or_init(|| async {
Configuration {
base_path: BASE_PATH.to_owned(),
client: create_default_client_with_middleware().await,
..Configuration::default()
}
})
.await
}
}

pub mod vault {
use crate::vault::{Vault, VaultConfig};
use std::sync::{Arc, OnceLock};
use std::sync::Arc;
use tokio::sync::OnceCell;

pub fn default() -> Arc<Vault> {
static DEFAULT_VAULT: OnceLock<Arc<Vault>> = OnceLock::new();
pub async fn default() -> Arc<Vault> {
static DEFAULT_VAULT: OnceCell<Arc<Vault>> = OnceCell::const_new();
DEFAULT_VAULT
.get_or_init(|| {
.get_or_init(|| async {
let vault = Vault::new(VaultConfig::default());
vault.open();
vault.open().await;
Arc::new(vault)
})
.await
.clone()
}
}
Expand Down
25 changes: 15 additions & 10 deletions flecs-core/src/sorcerer/authmancer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use flecs_console_client::apis::configuration::Configuration;
use flecs_console_client::models::PostApiV2Tokens200ResponseData;
use flecsd_axum_server::models::AuthResponseData;

pub fn delete_authentication(vault: &Vault) {
let mut grabbed_pouches = vault.reservation().reserve_secret_pouch_mut().grab();
pub async fn delete_authentication(vault: &Vault) {
let mut grabbed_pouches = vault.reservation().reserve_secret_pouch_mut().grab().await;
spell::auth::delete_authentication(
grabbed_pouches
.secret_pouch_mut
Expand All @@ -17,8 +17,8 @@ pub fn delete_authentication(vault: &Vault) {
)
}

pub fn store_authentication(auth: AuthResponseData, vault: &Vault) {
let mut grabbed_pouches = vault.reservation().reserve_secret_pouch_mut().grab();
pub async fn store_authentication(auth: AuthResponseData, vault: &Vault) {
let mut grabbed_pouches = vault.reservation().reserve_secret_pouch_mut().grab().await;
spell::auth::store_authentication(
auth,
grabbed_pouches
Expand All @@ -39,6 +39,7 @@ pub async fn acquire_download_token(
.reservation()
.reserve_secret_pouch()
.grab()
.await
.secret_pouch
.as_ref()
.unwrap()
Expand Down Expand Up @@ -79,25 +80,27 @@ mod tests {
}
}

#[test]
fn delete_authentication_test() {
#[tokio::test]
async fn delete_authentication_test() {
let test_path = Path::new(TEST_PATH).join("delete_authentication");
fs::create_dir_all(&test_path).unwrap();
let vault = Vault::new(VaultConfig { path: test_path });
vault
.reservation()
.reserve_secret_pouch_mut()
.grab()
.await
.secret_pouch_mut
.as_mut()
.unwrap()
.gems_mut()
.authentication = Some(create_test_auth());
delete_authentication(&vault);
delete_authentication(&vault).await;
assert!(vault
.reservation()
.reserve_secret_pouch()
.grab()
.await
.secret_pouch
.as_ref()
.unwrap()
Expand All @@ -106,28 +109,30 @@ mod tests {
.is_none())
}

#[test]
fn store_authentication_test() {
#[tokio::test]
async fn store_authentication_test() {
let test_path = Path::new(TEST_PATH).join("store_authentication");
fs::create_dir_all(&test_path).unwrap();
let vault = Vault::new(VaultConfig { path: test_path });
assert!(vault
.reservation()
.reserve_secret_pouch()
.grab()
.await
.secret_pouch
.as_ref()
.unwrap()
.gems()
.authentication
.is_none());
store_authentication(create_test_auth(), &vault);
store_authentication(create_test_auth(), &vault).await;
assert_eq!(
Some(create_test_auth()),
vault
.reservation()
.reserve_secret_pouch()
.grab()
.await
.secret_pouch
.as_ref()
.unwrap()
Expand Down
Loading