Skip to content

Commit

Permalink
feat: Wrap db password in a Secret
Browse files Browse the repository at this point in the history
  • Loading branch information
Yag000 committed Aug 30, 2023
1 parent b7a7d3d commit 7ea02d6
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 16 deletions.
17 changes: 17 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ tracing-subscriber = { version = "0.3", features = ["registry", "env-filter"] }
tracing-bunyan-formatter = "0.3"
tracing-log = "0.1"
once_cell = "1"
secrecy = { version = "0.8", features = ["serde"] }

[dev-dependencies]
reqwest = { version = "0.11", features = ["json"] }
27 changes: 18 additions & 9 deletions src/configurations.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use secrecy::{ExposeSecret, Secret};

#[derive(serde::Deserialize)]
pub struct Settings {
pub database: DatabaseSettings,
Expand All @@ -7,25 +9,32 @@ 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,
}

impl DatabaseSettings {
pub fn get_connnection_string(&self) -> String {
format!(
pub fn get_connnection_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 get_connnection_string_without_db(&self) -> String {
format!(
pub fn get_connnection_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
))
}
}

Expand Down
12 changes: 9 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use secrecy::ExposeSecret;
use sqlx::PgPool;
use std::net::TcpListener;
use tracing::subscriber::set_global_default;
Expand All @@ -14,9 +15,14 @@ async fn main() -> Result<(), std::io::Error> {

// We want to panic if we cannot read the configuration
let configuration = get_configuration().expect("Failed to read configurations");
let connection = PgPool::connect(&configuration.database.get_connnection_string())
.await
.expect("Failed to connect to Postgresf");
let connection = PgPool::connect(
&configuration
.database
.get_connnection_string()
.expose_secret(),
)
.await
.expect("Failed to connect to Postgresf");
// Bind the TCP listener socket address with the configuration port
let address = format!("127.0.0.1:{}", configuration.application_port);
let listener = TcpListener::bind(address).expect("Failed to bind random port");
Expand Down
10 changes: 6 additions & 4 deletions tests/health_check.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use once_cell::sync::Lazy;
use secrecy::ExposeSecret;
use sqlx::{Connection, Executor, PgConnection, PgPool};
use std::net::TcpListener;
use uuid::Uuid;
Expand Down Expand Up @@ -53,16 +54,17 @@ async fn spawn_app() -> TestApp {
/// This is important in order to avoid polluting an existing database with test data.
pub async fn configure_database(config: &zero2prod::configurations::DatabaseSettings) -> PgPool {
// Create database
let mut connection = PgConnection::connect(&config.get_connnection_string_without_db())
.await
.expect("Failed to connect to Postgres");
let mut connection =
PgConnection::connect(&config.get_connnection_string_without_db().expose_secret())
.await
.expect("Failed to connect to Postgres");
connection
.execute(format!(r#"CREATE DATABASE "{}";"#, config.database_name).as_str())
.await
.expect("Failed to create database");

// Migrate database
let connection_pool = PgPool::connect(&config.get_connnection_string())
let connection_pool = PgPool::connect(&config.get_connnection_string().expose_secret())
.await
.expect("Failed to connect to Postgres");
sqlx::migrate!("./migrations")
Expand Down

0 comments on commit 7ea02d6

Please sign in to comment.