-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
73a4cff
commit 804a04e
Showing
12 changed files
with
136 additions
and
14 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
[package] | ||
name = "services-lib" | ||
version = "0.0.1" | ||
edition = "2021" | ||
|
||
[lib] | ||
doctest = false | ||
|
||
[dependencies] | ||
anyhow = "1.0" | ||
base64 = "0.21" | ||
tokio = { version = "1", features = ["full"] } | ||
tokio-postgres = { version = "0.7", features = ["with-chrono-0_4"] } | ||
tokio-postgres-rustls = "0.9.0" | ||
postgres-types = { version = "0.2", features = ["array-impls", "derive", "with-chrono-0_4"] } | ||
postgres-native-tls = "0.5" | ||
native-tls = "0.2" | ||
rustls = "0.20.8" | ||
postgres_query = { git = "https://github.com/nolanderc/rust-postgres-query", rev = "b4422051c8a31fbba4a35f88004c1cefb1878dd5" } | ||
tracing = { version = "0.1", features = ["log"] } | ||
serde = { version = "1.0.188", features = ["derive"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
use serde::{Deserialize, Deserializer}; | ||
use std::env; | ||
|
||
/// Get a string content, or the content of an Env variable it the string start with $ | ||
/// | ||
/// Example: | ||
/// - "abc" -> "abc" | ||
/// - "$something" -> read env variable named something and return it's content | ||
/// | ||
/// *WARNING*: May kill the program if we are asking for anv environment variable that does not exist | ||
pub fn string_or_env<'de, D>(deserializer: D) -> Result<String, D::Error> | ||
where | ||
D: Deserializer<'de>, | ||
{ | ||
let value_or_env = String::deserialize(deserializer)?; | ||
let value = match &value_or_env.chars().next().unwrap() { | ||
'$' => env::var(&value_or_env[1..]).expect("reading from env"), | ||
_ => value_or_env, | ||
}; | ||
Ok(value) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
pub mod env_helper; | ||
pub mod postgres_configuration; | ||
pub mod postgres_connection; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
use crate::env_helper::string_or_env; | ||
use serde::Deserialize; | ||
|
||
#[derive(Clone, Debug, Deserialize, Default)] | ||
pub struct PostgresConfiguration { | ||
#[serde(deserialize_with = "string_or_env")] | ||
pub connection_string: String, | ||
pub allow_invalid_certs: bool, | ||
pub tls: Option<PostgresTlsConfig>, | ||
} | ||
|
||
#[derive(Clone, Debug, Deserialize)] | ||
pub struct PostgresTlsConfig { | ||
/// CA Cert file or env var | ||
pub ca_cert_path: String, | ||
/// PKCS12 client cert path | ||
pub client_key_path: String, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
use crate::postgres_configuration::PostgresConfiguration; | ||
use native_tls::{Certificate, Identity, TlsConnector}; | ||
use postgres_native_tls::MakeTlsConnector; | ||
use std::{env, fs}; | ||
use tokio::task::JoinHandle; | ||
use tokio_postgres::Client; | ||
|
||
pub async fn connect( | ||
config: &PostgresConfiguration, | ||
) -> anyhow::Result<(Client, JoinHandle<Result<(), tokio_postgres::Error>>)> { | ||
// openssl pkcs12 -export -in client.cer -inkey client-key.cer -out client.pks | ||
// base64 -i ca.cer -o ca.cer.b64 && base64 -i client.pks -o client.pks.b64 | ||
// fly secrets set PG_CA_CERT=- < ./ca.cer.b64 -a mango-fills | ||
// fly secrets set PG_CLIENT_KEY=- < ./client.pks.b64 -a mango-fills | ||
let tls = match &config.tls { | ||
Some(tls) => { | ||
use base64::{engine::general_purpose, Engine as _}; | ||
let ca_cert = match &tls.ca_cert_path.chars().next().unwrap() { | ||
'$' => general_purpose::STANDARD | ||
.decode( | ||
env::var(&tls.ca_cert_path[1..]) | ||
.expect("reading client cert from env") | ||
.into_bytes(), | ||
) | ||
.expect("decoding client cert"), | ||
_ => fs::read(&tls.ca_cert_path).expect("reading client cert from file"), | ||
}; | ||
let client_key = match &tls.client_key_path.chars().next().unwrap() { | ||
'$' => general_purpose::STANDARD | ||
.decode( | ||
env::var(&tls.client_key_path[1..]) | ||
.expect("reading client key from env") | ||
.into_bytes(), | ||
) | ||
.expect("decoding client key"), | ||
_ => fs::read(&tls.client_key_path).expect("reading client key from file"), | ||
}; | ||
MakeTlsConnector::new( | ||
TlsConnector::builder() | ||
.add_root_certificate(Certificate::from_pem(&ca_cert)?) | ||
.identity(Identity::from_pkcs12(&client_key, "pass")?) | ||
.danger_accept_invalid_certs(config.allow_invalid_certs) | ||
.build()?, | ||
) | ||
} | ||
None => MakeTlsConnector::new( | ||
TlsConnector::builder() | ||
.danger_accept_invalid_certs(config.allow_invalid_certs) | ||
.build()?, | ||
), | ||
}; | ||
|
||
let config = config.clone(); | ||
|
||
let (client, connection) = tokio_postgres::connect(&config.connection_string, tls).await?; | ||
|
||
let handle = tokio::spawn(async move { connection.await }); | ||
|
||
Ok((client, handle)) | ||
} |