Skip to content

Commit

Permalink
inline services-mango-lib
Browse files Browse the repository at this point in the history
  • Loading branch information
grooviegermanikus committed Jan 23, 2025
1 parent 73a4cff commit 804a04e
Show file tree
Hide file tree
Showing 12 changed files with 136 additions and 14 deletions.
9 changes: 4 additions & 5 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion bin/comparer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ router-config-lib = { path = "../../lib/router-config-lib" }
router-lib = { path = "../../lib/router-lib/", version = "0.0.1" }
base64 = "0.21.7"
bincode = "1.3.3"
services-mango-lib = { git = "https://github.com/blockworks-foundation/mango-v4.git" }
services-lib = { path = "../../lib/services-lib" }
tokio-postgres = { version = "0.7", features = ["with-chrono-0_4"] }
tokio-postgres-rustls = "0.9.0"
postgres_query = { git = "https://github.com/nolanderc/rust-postgres-query", rev = "b4422051c8a31fbba4a35f88004c1cefb1878dd5" }
Expand Down
4 changes: 2 additions & 2 deletions bin/comparer/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use services_mango_lib::env_helper::string_or_env as serde_string_or_env;
use services_mango_lib::postgres_configuration::PostgresConfiguration;
use services_lib::env_helper::string_or_env as serde_string_or_env;
use services_lib::postgres_configuration::PostgresConfiguration;

#[derive(Clone, Debug, Default, serde_derive::Deserialize)]
pub struct Config {
Expand Down
4 changes: 2 additions & 2 deletions bin/comparer/src/persister.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::config::Config;
use async_channel::Receiver;
use services_mango_lib::postgres_configuration::PostgresConfiguration;
use services_mango_lib::postgres_connection;
use services_lib::postgres_configuration::PostgresConfiguration;
use services_lib::postgres_connection;
use solana_program::pubkey::Pubkey;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
Expand Down
2 changes: 1 addition & 1 deletion bin/indexer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ router-feed-lib = { path = "../../lib/router-feed-lib" }
router-config-lib = { path = "../../lib/router-config-lib" }
base64 = "0.21.7"
bincode = "1.3.3"
services-mango-lib = { git = "https://github.com/blockworks-foundation/mango-v4.git" }
services-lib = { path = "../../lib/services-lib" }
tokio-postgres = { version = "0.7", features = ["with-chrono-0_4"] }
tokio-postgres-rustls = "0.9.0"
postgres_query = { git = "https://github.com/nolanderc/rust-postgres-query", rev = "b4422051c8a31fbba4a35f88004c1cefb1878dd5" }
Expand Down
2 changes: 1 addition & 1 deletion bin/indexer/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use router_config_lib::AccountDataSourceConfig;
use services_mango_lib::postgres_configuration::PostgresConfiguration;
use services_lib::postgres_configuration::PostgresConfiguration;

#[derive(Clone, Debug, Default, serde_derive::Deserialize)]
pub struct Config {
Expand Down
4 changes: 2 additions & 2 deletions bin/indexer/src/persister.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::config::MetricsConfig;
use async_channel::Receiver;
use services_mango_lib::postgres_configuration::PostgresConfiguration;
use services_mango_lib::postgres_connection;
use services_lib::postgres_configuration::PostgresConfiguration;
use services_lib::postgres_connection;
use solana_sdk::signature::Signature;
use std::collections::HashMap;
use std::sync::atomic::{AtomicBool, Ordering};
Expand Down
21 changes: 21 additions & 0 deletions lib/services-lib/Cargo.toml
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"] }
21 changes: 21 additions & 0 deletions lib/services-lib/src/env_helper.rs
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)
}
3 changes: 3 additions & 0 deletions lib/services-lib/src/lib.rs
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;
18 changes: 18 additions & 0 deletions lib/services-lib/src/postgres_configuration.rs
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,
}
60 changes: 60 additions & 0 deletions lib/services-lib/src/postgres_connection.rs
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))
}

0 comments on commit 804a04e

Please sign in to comment.