Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

fix(filemanager): avoid using to_url_lossy when loading database credentials #385

Merged
merged 1 commit into from
Jul 1, 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
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ use crate::database::aws::ingester::Ingester;
use crate::database::aws::ingester_paired::IngesterPaired;
use crate::env::Config;
use async_trait::async_trait;
use sea_orm::{ConnectOptions, Database, DatabaseConnection, SqlxPostgresConnector};
use sea_orm::{DatabaseConnection, SqlxPostgresConnector};
use sqlx::postgres::PgConnectOptions;
use sqlx::{ConnectOptions as SqlxConnectOptions, PgPool};
use sqlx::PgPool;
use tracing::debug;

use crate::error::Result;
Expand Down Expand Up @@ -49,28 +49,16 @@ impl Client {
generator: Option<impl CredentialGenerator>,
config: &Config,
) -> Result<Self> {
Ok(Self::new(Self::create_pool(generator, config).await?))
Ok(Self::from_pool(Self::create_pool(generator, config).await?))
}

/// Create a database connection pool using credential loading logic defined in
/// `Self::connect_options`.
pub async fn create_pool(
generator: Option<impl CredentialGenerator>,
config: &Config,
) -> Result<DatabaseConnection> {
Ok(Database::connect(Self::connect_options(generator, config).await?).await?)
}

/// Create database connect options using a series of credential loading logic.
pub async fn connect_options(
generator: Option<impl CredentialGenerator>,
config: &Config,
) -> Result<ConnectOptions> {
Ok(ConnectOptions::new(
Self::pg_connect_options(generator, config)
.await?
.to_url_lossy(),
))
) -> Result<PgPool> {
Ok(PgPool::connect_with(Self::pg_connect_options(generator, config).await?).await?)
}

/// Create database connect options using a series of credential loading logic.
Expand Down Expand Up @@ -117,6 +105,11 @@ impl Client {
pub fn connection_ref(&self) -> &DatabaseConnection {
&self.connection
}

/// Get the inner database connection.
pub fn into_inner(self) -> DatabaseConnection {
self.connection
}
}

#[async_trait]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,13 @@ pub async fn ingest_s3_inventory(

/// Create a postgres database pool using an IAM credential generator.
pub async fn create_database_pool(env_config: &EnvConfig) -> Result<DatabaseConnection, Error> {
Ok(Client::create_pool(
let client = Client::from_generator(
Some(IamGeneratorBuilder::default().build(env_config).await?),
env_config,
)
.await?)
.await?;

Ok(client.into_inner())
}

/// Update connection options with new credentials.
Expand Down