Skip to content

Commit

Permalink
Fix db connection string, slight restructuring (#207)
Browse files Browse the repository at this point in the history
  • Loading branch information
kkohbrok authored Oct 7, 2024
1 parent 48001fc commit c03e6c4
Show file tree
Hide file tree
Showing 29 changed files with 192 additions and 204 deletions.
2 changes: 1 addition & 1 deletion backend/src/auth_service/client_record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use phnxtypes::{
};
use sqlx::{Connection, PgConnection};

use crate::persistence::StorageError;
use crate::errors::StorageError;

use super::queue::Queue;

Expand Down
2 changes: 1 addition & 1 deletion backend/src/auth_service/connection_package/persistence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use phnxtypes::{
use sqlx::{postgres::PgArguments, Arguments, Connection, PgConnection, PgExecutor};
use uuid::Uuid;

use crate::persistence::StorageError;
use crate::errors::StorageError;

use super::StorableConnectionPackage;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use phnxtypes::{
use serde::{Deserialize, Serialize};
use sqlx::{Connection, PgConnection};

use crate::persistence::StorageError;
use crate::errors::StorageError;

use super::{signing_key::StorableSigningKey, CredentialGenerationError};

Expand Down Expand Up @@ -102,7 +102,7 @@ mod persistence {
};
use sqlx::PgExecutor;

use crate::{auth_service::credentials::CredentialType, persistence::StorageError};
use crate::{auth_service::credentials::CredentialType, errors::StorageError};

use super::{IntermediateCredential, IntermediateSigningKey};

Expand Down
2 changes: 1 addition & 1 deletion backend/src/auth_service/credentials/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use phnxtypes::crypto::errors::KeyGenerationError;
use thiserror::Error;

use crate::persistence::StorageError;
use crate::errors::StorageError;

pub(in crate::auth_service) mod intermediate_signing_key;
pub(in crate::auth_service) mod signing_key;
Expand Down
2 changes: 1 addition & 1 deletion backend/src/auth_service/credentials/signing_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ impl StorableSigningKey {
mod persistence {
use phnxtypes::codec::PhnxCodec;

use crate::{auth_service::credentials::CredentialType, persistence::StorageError};
use crate::{auth_service::credentials::CredentialType, errors::StorageError};

use super::*;

Expand Down
5 changes: 4 additions & 1 deletion backend/src/auth_service/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ use thiserror::Error;
use tls_codec::{TlsSerialize, TlsSize};
use tokio::sync::Mutex;

use crate::persistence::{DatabaseError, InfraService, ServiceCreationError, StorageError};
use crate::{
errors::{DatabaseError, StorageError},
infra_service::{InfraService, ServiceCreationError},
};

pub mod client_api;
mod client_record;
Expand Down
2 changes: 1 addition & 1 deletion backend/src/auth_service/opaque.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use opaque_ke::{
use phnxtypes::crypto::OpaqueCiphersuite;
use sqlx::PgExecutor;

use crate::persistence::StorageError;
use crate::errors::StorageError;

pub(super) struct OpaqueSetup(ServerSetup<OpaqueCiphersuite>);

Expand Down
4 changes: 2 additions & 2 deletions backend/src/auth_service/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use phnxtypes::identifiers::AsClientId;
use sqlx::PgConnection;

use crate::persistence::StorageError;
use crate::errors::StorageError;

pub(super) struct Queue {
queue_id: AsClientId,
Expand All @@ -31,7 +31,7 @@ mod persistence {
use sqlx::{Connection, Row};
use uuid::Uuid;

use crate::persistence::QueueError;
use crate::errors::QueueError;

use super::*;

Expand Down
4 changes: 2 additions & 2 deletions backend/src/auth_service/user_record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use opaque_ke::ServerRegistration;
use phnxtypes::{crypto::OpaqueCiphersuite, identifiers::QualifiedUserName};

use crate::persistence::StorageError;
use crate::errors::StorageError;

#[derive(Debug, Clone)]
pub(super) struct UserRecord {
Expand Down Expand Up @@ -46,7 +46,7 @@ mod persistence {
};
use sqlx::PgExecutor;

use crate::persistence::StorageError;
use crate::errors::StorageError;

use super::UserRecord;

Expand Down
2 changes: 1 addition & 1 deletion backend/src/ds/group_state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use sqlx::PgExecutor;
use thiserror::Error;
use uuid::Uuid;

use crate::persistence::StorageError;
use crate::errors::StorageError;

use super::{process::ExternalCommitInfo, ReservedGroupId, GROUP_STATE_EXPIRATION};

Expand Down
4 changes: 2 additions & 2 deletions backend/src/ds/group_state/persistence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use sqlx::{
PgExecutor,
};

use crate::persistence::StorageError;
use crate::errors::StorageError;

use super::StorableDsGroupData;

Expand Down Expand Up @@ -108,7 +108,7 @@ mod test {
group_state::{EncryptedDsGroupState, StorableDsGroupData},
Ds,
},
persistence::InfraService,
infra_service::InfraService,
};

#[sqlx::test]
Expand Down
2 changes: 1 addition & 1 deletion backend/src/ds/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use sqlx::PgPool;
use tokio::sync::Mutex;
use uuid::Uuid;

use crate::persistence::{InfraService, ServiceCreationError};
use crate::infra_service::{InfraService, ServiceCreationError};

mod add_clients;
mod add_users;
Expand Down
2 changes: 1 addition & 1 deletion backend/src/ds/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,8 @@ use phnxtypes::{

use crate::{
ds::ReservedGroupId,
errors::StorageError,
messages::intra_backend::{DsFanOutMessage, DsFanOutPayload},
persistence::StorageError,
qs::QsConnector,
};

Expand Down
58 changes: 58 additions & 0 deletions backend/src/errors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// SPDX-FileCopyrightText: 2024 Phoenix R&D GmbH <[email protected]>
//
// SPDX-License-Identifier: AGPL-3.0-or-later

use thiserror::Error;

#[derive(Debug, Error)]
pub enum StorageError {
#[error(transparent)]
Database(#[from] DatabaseError),
#[error("Error deserializing column: {0}")]
Serde(#[from] phnxtypes::codec::Error),
}

impl From<sqlx::Error> for StorageError {
fn from(e: sqlx::Error) -> Self {
Self::Database(e.into())
}
}

impl From<Box<dyn std::error::Error + Send + Sync>> for StorageError {
fn from(e: Box<dyn std::error::Error + Send + Sync>) -> Self {
Self::Database(e.into())
}
}

#[derive(Debug, Error)]
pub enum DatabaseError {
#[error(transparent)]
Sqlx(#[from] sqlx::Error),
#[error(transparent)]
Dynamic(#[from] Box<dyn std::error::Error + Send + Sync>),
}

/// General error while accessing the requested queue.
#[derive(Error, Debug)]
pub(super) enum QueueError {
#[error(transparent)]
Storage(#[from] StorageError),
/// Mismatching sequence numbers.
#[error("Mismatching sequence numbers.")]
SequenceNumberMismatch,
/// Unrecoverable implementation error
#[error("Library Error")]
LibraryError,
}

impl From<sqlx::Error> for QueueError {
fn from(e: sqlx::Error) -> Self {
Self::Storage(e.into())
}
}

impl From<phnxtypes::codec::Error> for QueueError {
fn from(e: phnxtypes::codec::Error) -> Self {
Self::Storage(e.into())
}
}
64 changes: 6 additions & 58 deletions backend/src/persistence.rs → backend/src/infra_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,58 +7,7 @@ use phnxtypes::identifiers::Fqdn;
use sqlx::{Executor, PgPool};
use thiserror::Error;

#[derive(Debug, Error)]
pub enum StorageError {
#[error(transparent)]
Database(#[from] DatabaseError),
#[error("Error deserializing column: {0}")]
Serde(#[from] phnxtypes::codec::Error),
}

impl From<sqlx::Error> for StorageError {
fn from(e: sqlx::Error) -> Self {
Self::Database(e.into())
}
}

impl From<Box<dyn std::error::Error + Send + Sync>> for StorageError {
fn from(e: Box<dyn std::error::Error + Send + Sync>) -> Self {
Self::Database(e.into())
}
}

#[derive(Debug, Error)]
pub enum DatabaseError {
#[error(transparent)]
Sqlx(#[from] sqlx::Error),
#[error(transparent)]
Dynamic(#[from] Box<dyn std::error::Error + Send + Sync>),
}

/// General error while accessing the requested queue.
#[derive(Error, Debug)]
pub(super) enum QueueError {
#[error(transparent)]
Storage(#[from] StorageError),
/// Mismatching sequence numbers.
#[error("Mismatching sequence numbers.")]
SequenceNumberMismatch,
/// Unrecoverable implementation error
#[error("Library Error")]
LibraryError,
}

impl From<sqlx::Error> for QueueError {
fn from(e: sqlx::Error) -> Self {
Self::Storage(e.into())
}
}

impl From<phnxtypes::codec::Error> for QueueError {
fn from(e: phnxtypes::codec::Error) -> Self {
Self::Storage(e.into())
}
}
use crate::{errors::StorageError, settings::DatabaseSettings};

#[derive(Debug, Error)]
pub enum ServiceCreationError {
Expand All @@ -77,12 +26,13 @@ impl<T: Into<sqlx::Error>> From<T> for ServiceCreationError {
#[async_trait]
pub trait InfraService: Sized {
async fn new(
connection_string: &str,
db_name: &str,
database_settings: &DatabaseSettings,
domain: Fqdn,
) -> Result<Self, ServiceCreationError> {
let connection = PgPool::connect(connection_string).await?;
let connection =
PgPool::connect(&database_settings.connection_string_without_database()).await?;

let db_name = database_settings.name.as_str();
let db_exists = sqlx::query!(
"select exists (
SELECT datname FROM pg_catalog.pg_database WHERE datname = $1
Expand All @@ -98,11 +48,9 @@ pub trait InfraService: Sized {
.await?;
}

let connection_string_with_db = format!("{}/{}", connection_string, db_name);
tracing::info!("Successfully created database {}", db_name);
tracing::info!("Connecting to postgres {}", connection_string_with_db);

let db_pool = PgPool::connect(&connection_string_with_db).await?;
let db_pool = PgPool::connect(&database_settings.connection_string()).await?;

Self::new_from_pool(db_pool, domain).await
}
Expand Down
4 changes: 3 additions & 1 deletion backend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@

pub mod auth_service;
pub mod ds;
pub mod errors;
pub mod infra_service;
pub mod messages;
pub mod persistence;
pub mod qs;
pub mod settings;

pub use mls_assist::messages::{AssistedGroupInfo, AssistedMessageOut};
2 changes: 1 addition & 1 deletion backend/src/qs/add_package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ mod persistence {
};
use sqlx::{postgres::PgArguments, Arguments, Connection, PgConnection, PgExecutor};

use crate::persistence::StorageError;
use crate::errors::StorageError;

use super::*;
impl StorableEncryptedAddPackage {
Expand Down
2 changes: 1 addition & 1 deletion backend/src/qs/client_id_decryption_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl StorableClientIdDecryptionKey {
mod persistence {
use sqlx::PgExecutor;

use crate::persistence::StorageError;
use crate::errors::StorageError;

use super::StorableClientIdDecryptionKey;

Expand Down
4 changes: 2 additions & 2 deletions backend/src/qs/client_record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ use phnxtypes::{
};

use crate::{
errors::StorageError,
messages::intra_backend::DsFanOutPayload,
persistence::StorageError,
qs::{PushNotificationError, WsNotification},
};

Expand Down Expand Up @@ -96,7 +96,7 @@ mod persistence {

use super::*;

use crate::persistence::StorageError;
use crate::errors::StorageError;

impl QsClientRecord {
pub(super) async fn store(
Expand Down
2 changes: 1 addition & 1 deletion backend/src/qs/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// SPDX-License-Identifier: AGPL-3.0-or-later

use crate::persistence::StorageError;
use crate::errors::StorageError;

use super::network_provider_trait::NetworkProvider;
use phnxtypes::crypto::errors::{DecryptionError, KeyGenerationError};
Expand Down
3 changes: 2 additions & 1 deletion backend/src/qs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,9 @@ use sqlx::PgPool;
use thiserror::Error;

use crate::{
errors::StorageError,
infra_service::{InfraService, ServiceCreationError},
messages::intra_backend::DsFanOutMessage,
persistence::{InfraService, ServiceCreationError, StorageError},
};

mod add_package;
Expand Down
2 changes: 1 addition & 1 deletion backend/src/qs/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use phnxtypes::{codec::PhnxCodec, identifiers::QsClientId, messages::QueueMessage};
use sqlx::{Connection, PgConnection, PgExecutor};

use crate::persistence::{QueueError, StorageError};
use crate::errors::{QueueError, StorageError};

pub(super) struct Queue {
queue_id: QsClientId,
Expand Down
2 changes: 1 addition & 1 deletion backend/src/qs/signing_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl StorableQsSigningKey {
}

mod persistence {
use crate::persistence::StorageError;
use crate::errors::StorageError;

use super::*;

Expand Down
Loading

0 comments on commit c03e6c4

Please sign in to comment.