feat(api): wrap database password with Secret
This commit is contained in:
parent
3a0576ba48
commit
444e42351e
11
Cargo.lock
generated
11
Cargo.lock
generated
@ -615,6 +615,7 @@ dependencies = [
|
||||
"config",
|
||||
"once_cell",
|
||||
"reqwest",
|
||||
"secrecy",
|
||||
"serde",
|
||||
"sqlx",
|
||||
"tokio",
|
||||
@ -1930,6 +1931,16 @@ dependencies = [
|
||||
"untrusted",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "secrecy"
|
||||
version = "0.8.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9bd1c54ea06cfd2f6b63219704de0b9b4f72dcc2b8fdef820be6cd799780e91e"
|
||||
dependencies = [
|
||||
"serde",
|
||||
"zeroize",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "security-framework"
|
||||
version = "2.9.2"
|
||||
|
@ -27,6 +27,7 @@ tracing-subscriber = { version = "0.3.18", features = ["registry", "env-filter"]
|
||||
tracing-bunyan-formatter = "0.3.9"
|
||||
tracing-log = "0.2.0"
|
||||
once_cell = "1.19.0"
|
||||
secrecy = { version = "0.8.0", features = ["serde"] }
|
||||
|
||||
[dependencies.sqlx]
|
||||
version = "0.7"
|
||||
|
@ -1,3 +1,5 @@
|
||||
use secrecy::{ExposeSecret, Secret};
|
||||
|
||||
#[derive(serde::Deserialize)]
|
||||
pub struct Settings {
|
||||
pub database: DatabaseSettings,
|
||||
@ -7,7 +9,7 @@ pub struct Settings {
|
||||
#[derive(serde::Deserialize)]
|
||||
pub struct DatabaseSettings {
|
||||
pub username: String,
|
||||
pub password: String,
|
||||
pub password: Secret<String>,
|
||||
pub port: u16,
|
||||
pub host: String,
|
||||
pub database_name: String,
|
||||
@ -25,17 +27,24 @@ pub fn get_configuration() -> Result<Settings, config::ConfigError> {
|
||||
}
|
||||
|
||||
impl DatabaseSettings {
|
||||
pub fn connection_string(&self) -> String {
|
||||
format!(
|
||||
pub fn connection_string(&self) -> Secret<String> {
|
||||
Secret::new(format!(
|
||||
"postgres://{}:{}@{}:{}/{}",
|
||||
self.username, self.password, self.host, self.port, self.database_name
|
||||
)
|
||||
self.username,
|
||||
self.password.expose_secret(),
|
||||
self.host,
|
||||
self.port,
|
||||
self.database_name
|
||||
))
|
||||
}
|
||||
|
||||
pub fn connection_string_without_db(&self) -> String {
|
||||
format!(
|
||||
pub fn connection_string_without_db(&self) -> Secret<String> {
|
||||
Secret::new(format!(
|
||||
"postgres://{}:{}@{}:{}",
|
||||
self.username, self.password, self.host, self.port
|
||||
)
|
||||
self.username,
|
||||
self.password.expose_secret(),
|
||||
self.host,
|
||||
self.port
|
||||
))
|
||||
}
|
||||
}
|
||||
|
@ -2,6 +2,7 @@ use std::net::TcpListener;
|
||||
|
||||
use email_newsletter_api::telemetry::{get_subscriber, init_subscriber};
|
||||
use email_newsletter_api::{configuration::get_configuration, startup};
|
||||
use secrecy::ExposeSecret;
|
||||
use sqlx::PgPool;
|
||||
|
||||
#[tokio::main]
|
||||
@ -15,7 +16,7 @@ async fn main() -> Result<(), std::io::Error> {
|
||||
);
|
||||
init_subscriber(subscriber);
|
||||
|
||||
let db_conn = PgPool::connect(&configuration.database.connection_string())
|
||||
let db_conn = PgPool::connect(configuration.database.connection_string().expose_secret())
|
||||
.await
|
||||
.expect("Failed to connect to PostgreSQL");
|
||||
|
||||
|
@ -3,6 +3,7 @@ use email_newsletter_api::{
|
||||
telemetry::{get_subscriber, init_subscriber},
|
||||
};
|
||||
use once_cell::sync::Lazy;
|
||||
use secrecy::ExposeSecret;
|
||||
use sqlx::{Connection, Executor, PgConnection, PgPool};
|
||||
use std::net::TcpListener;
|
||||
use uuid::Uuid;
|
||||
@ -57,7 +58,8 @@ pub async fn spawn_app() -> TestApp {
|
||||
}
|
||||
|
||||
pub async fn configure_test_database(db_config: &DatabaseSettings) -> PgPool {
|
||||
let mut connection = PgConnection::connect(&db_config.connection_string_without_db())
|
||||
let mut connection =
|
||||
PgConnection::connect(db_config.connection_string_without_db().expose_secret())
|
||||
.await
|
||||
.expect("Failed to connect to Postgres");
|
||||
|
||||
@ -66,7 +68,7 @@ pub async fn configure_test_database(db_config: &DatabaseSettings) -> PgPool {
|
||||
.await
|
||||
.expect("Failed to create database");
|
||||
|
||||
let conn_pool = PgPool::connect(&db_config.connection_string())
|
||||
let conn_pool = PgPool::connect(db_config.connection_string().expose_secret())
|
||||
.await
|
||||
.expect("Failed to connect to PostgreSQL pool");
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user