-
Notifications
You must be signed in to change notification settings - Fork 31
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
inline services-mango-lib #48
Open
grooviegermanikus
wants to merge
1
commit into
main
Choose a base branch
from
improvement/inline-services-mango-lib-dependency
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.