Skip to content
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

add tls connection envs #95

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,9 @@ You can also configure martin using environment variables
| DATABASE_URL | postgres://postgres@localhost/db | postgres database connection |
| WATCH_MODE | true | scan for new sources |
| DANGER_ACCEPT_INVALID_CERTS | false | Trust invalid certificates |
| CA_ROOT_FILE | /certs/ca_root.crt | Server root certificate file |
| CLIENT_PKCS12_FILE | /certs/identity.pfx | Client certificate file |
| CLIENT_PKCS12_PASS | secret | Client certificate password |

## Configuration File

Expand Down
49 changes: 46 additions & 3 deletions src/db.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use std::io;
use std::str::FromStr;
use std::{env, io};

use native_tls::TlsConnector;
use native_tls::{Certificate, Identity, TlsConnector};
use postgres_native_tls::MakeTlsConnector;
use r2d2::PooledConnection;
use r2d2_postgres::PostgresConnectionManager;
use semver::Version;
use semver::VersionReq;
use std::fs::File;
use std::io::Read;

use crate::utils::prettify_error;

Expand All @@ -15,7 +17,48 @@ pub type Pool = r2d2::Pool<ConnectionManager>;
pub type Connection = PooledConnection<ConnectionManager>;

fn make_tls_connector(danger_accept_invalid_certs: bool) -> io::Result<MakeTlsConnector> {
let connector = TlsConnector::builder()
let key = "CA_ROOT_FILE";
let ca_file = match env::var_os(key) {
stepankuzmin marked this conversation as resolved.
Show resolved Hide resolved
Some(s) => s.into_string().unwrap(),
stepankuzmin marked this conversation as resolved.
Show resolved Hide resolved
None => {
println!("{} is not defined in the environment", key);
String::default()
}
};
let key = "CLIENT_PKCS12_FILE";
let client_identity_file = match env::var_os(key) {
Some(s) => s.into_string().unwrap(),
None => {
println!("{} is not defined in the environment", key);
String::default()
}
};
let key = "CLIENT_PKCS12_PASS";
let client_identity_pass = match env::var_os(key) {
Some(s) => s.into_string().unwrap(),
None => {
println!("{} is not defined in the environment", key);
String::default()
}
};

let mut builder = TlsConnector::builder();

if !client_identity_file.is_empty() {
let mut file = File::open(&client_identity_file).unwrap();
stepankuzmin marked this conversation as resolved.
Show resolved Hide resolved
let mut identity = vec![];
file.read_to_end(&mut identity).unwrap();
let identity = Identity::from_pkcs12(&identity, &client_identity_pass).unwrap();
builder.identity(identity);
}
if !ca_file.is_empty() {
let mut ca = File::open(&ca_file).unwrap();
let mut buf = Vec::new();
ca.read_to_end(&mut buf).unwrap();
let cert = Certificate::from_pem(&buf).unwrap();
builder.add_root_certificate(cert);
}
let connector = builder
.danger_accept_invalid_certs(danger_accept_invalid_certs)
.build()
.map_err(prettify_error("Can't build TLS connection"))?;
Expand Down