Skip to content

Commit

Permalink
ch4: wrap secret data with secrecy::Secret
Browse files Browse the repository at this point in the history
Signed-off-by: Jin Dong <jin.dong@databricks.com>
  • Loading branch information
djdongjin committed Mar 27, 2024
1 parent 8eb978d commit 2c59e1a
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 17 deletions.
11 changes: 11 additions & 0 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Expand Up @@ -19,6 +19,7 @@ actix-web = "4.5.1"
chrono = { version = "0.4.35", default-features = false, features = ["clock"] }
config = "0.14.0"
log = "0.4.21"
secrecy = { version = "0.8.0", features = ["serde"] }
serde = { version = "1.0.197", features = ["derive"] }
tokio = { version = "1.36.0", features = ["macros", "rt-multi-thread"] }
tracing = { version = "0.1.40", features = ["log"] }
Expand All @@ -41,4 +42,4 @@ features = [

[dev-dependencies]
reqwest = "0.12.1"
once_cell = "1"
once_cell = "1"
27 changes: 18 additions & 9 deletions src/configuration.rs
@@ -1,3 +1,5 @@
use secrecy::{ExposeSecret, Secret};

#[derive(serde::Deserialize)]
pub struct Settings {
pub database: DatabseSettings,
Expand All @@ -7,25 +9,32 @@ pub struct Settings {
#[derive(serde::Deserialize)]
pub struct DatabseSettings {
pub username: String,
pub password: String,
pub password: Secret<String>,
pub host: String,
pub port: u16,
pub database_name: String,
}

impl DatabseSettings {
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
))
}
}

Expand Down
8 changes: 5 additions & 3 deletions src/main.rs
@@ -1,3 +1,4 @@
use secrecy::ExposeSecret;
use sqlx::PgPool;
use std::net::TcpListener;
use zero2prod::configuration::get_configuration;
Expand All @@ -12,9 +13,10 @@ async fn main() -> Result<(), std::io::Error> {
let configuration = get_configuration().expect("Failed to read configuration");
let address = format!("127.0.0.1:{}", configuration.application_port);
let listener = TcpListener::bind(address)?;
let connection_pool = PgPool::connect(&configuration.database.connection_string())
.await
.expect("Failed to connect to Postgres.");
let connection_pool =
PgPool::connect(&configuration.database.connection_string().expose_secret())
.await
.expect("Failed to connect to Postgres.");

run(listener, connection_pool)?.await
}
10 changes: 6 additions & 4 deletions tests/health_check.rs
@@ -1,6 +1,7 @@
use std::net::TcpListener;

use once_cell::sync::Lazy;
use secrecy::ExposeSecret;
use sqlx::{Connection, Executor, PgConnection, PgPool};
use uuid::Uuid;
use zero2prod::configuration::{get_configuration, DatabseSettings};
Expand Down Expand Up @@ -50,17 +51,18 @@ async fn spawn_app() -> TestApp {

async fn configure_database(config: &DatabseSettings) -> PgPool {
// Create database
let mut connection = PgConnection::connect(&config.connection_string_without_db())
.await
.expect("Failed to connect to Postgres.");
let mut connection =
PgConnection::connect(&config.connection_string_without_db().expose_secret())
.await
.expect("Failed to connect to Postgres.");

// Migrate database
connection
.execute(format!(r#"CREATE DATABASE "{}";"#, config.database_name).as_str())
.await
.expect("Failed to create database.");

let connection_pool = PgPool::connect(&config.connection_string())
let connection_pool = PgPool::connect(&config.connection_string().expose_secret())
.await
.expect("Failed to connect to Postgres.");

Expand Down

0 comments on commit 2c59e1a

Please sign in to comment.