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

Add verify ca and root cert path to settings #852

Merged
merged 1 commit into from
Aug 7, 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
9 changes: 8 additions & 1 deletion db_store/src/iam_auth_pool.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{error::invalid_configuration, Error, Result, Settings};
use sqlx::{
postgres::{PgConnectOptions, Postgres},
postgres::{PgConnectOptions, PgSslMode, Postgres},
Pool,
};

Expand All @@ -18,6 +18,13 @@ pub async fn connect(settings: &Settings) -> Result<Pool<Postgres>> {
let client = aws_sdk_sts::Client::new(&aws_config);
let connect_parameters = ConnectParameters::try_from(settings)?;
let connect_options = connect_parameters.connect_options(&client).await?;
let connect_options = if let Some(ref ca_path) = settings.ca_path {
connect_options
.ssl_mode(PgSslMode::VerifyCa)
.ssl_root_cert(ca_path)
} else {
connect_options
};

let pool = settings
.pool_options()
Expand Down
20 changes: 18 additions & 2 deletions db_store/src/settings.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
use std::path::PathBuf;

use crate::{iam_auth_pool, metric_tracker, Error, Result};
use serde::Deserialize;
use sqlx::{postgres::PgPoolOptions, Pool, Postgres};
use sqlx::{
postgres::{PgConnectOptions, PgPoolOptions, PgSslMode},
Pool, Postgres,
};

#[derive(Deserialize, Debug, Clone)]
#[serde(rename_all = "lowercase")]
Expand All @@ -17,6 +22,9 @@ pub struct Settings {
/// the auth_type is Postgres
pub url: Option<String>,

/// Optionally provided certificate authority
pub ca_path: Option<PathBuf>,

#[serde(default = "default_auth_type")]
auth_type: AuthType,

Expand Down Expand Up @@ -55,12 +63,20 @@ impl Settings {
}

async fn simple_connect(&self) -> Result<Pool<Postgres>> {
let connect_options = self
let connect_options: PgConnectOptions = self
.url
.as_ref()
.ok_or_else(|| Error::InvalidConfiguration("url is required".to_string()))?
.parse()?;

let connect_options = if let Some(ref ca_path) = self.ca_path {
connect_options
.ssl_mode(PgSslMode::VerifyCa)
.ssl_root_cert(ca_path)
} else {
connect_options
};

let pool = self.pool_options().connect_with(connect_options).await?;
Ok(pool)
}
Expand Down