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

Remove pg dependency from CLI #123

Merged
merged 2 commits into from Nov 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -13,6 +13,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
([@mgrachev](https://github.com/mgrachev))

### ⚙️ Changed
- Remove the PostgreSQL dependency from the CLI application [#123](https://github.com/datanymizer/datanymizer/pull/123)
([@evgeniy-r](https://github.com/evgeniy-r))
- Remove arch-specific argument in Demo [#121](https://github.com/datanymizer/datanymizer/pull/121)
- Change edition to 2021 [#113](https://github.com/datanymizer/datanymizer/pull/113)
([@mgrachev](https://github.com/mgrachev))
Expand Down
56 changes: 28 additions & 28 deletions Cargo.lock

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

3 changes: 0 additions & 3 deletions cli/pg_datanymizer/Cargo.toml
Expand Up @@ -16,8 +16,5 @@ categories = ["command-line-utilities"]
anyhow = "1.0"
datanymizer_dumper = {path = "../../datanymizer_dumper"}
datanymizer_engine = {path = "../../datanymizer_engine"}
native-tls = "0.2.7"
postgres = "0.19.1"
postgres-native-tls = "0.5.0"
structopt = "0.3.20"
url = "2.2"
99 changes: 14 additions & 85 deletions cli/pg_datanymizer/src/app.rs
@@ -1,18 +1,14 @@
use anyhow::Result;
use native_tls::TlsConnector;
use postgres::{Client, IsolationLevel, NoTls};
use postgres_native_tls::MakeTlsConnector;
use std::borrow::Cow;
use url::Url;

use crate::options::{Options, TransactionConfig};

use datanymizer_dumper::{postgres::dumper::PgDumper, Dumper};
use datanymizer_dumper::{
postgres::{connector::Connector, dumper::PgDumper, IsolationLevel},
Dumper,
};
use datanymizer_engine::{Engine, Settings};

const SSL_MODE_PARAM: &str = "sslmode";
const NO_SSL_MODE: &str = "disable";

pub struct App {
options: Options,
database_url: Url,
Expand All @@ -29,12 +25,21 @@ impl App {
}

pub fn run(&self) -> Result<()> {
let mut client = self.client()?;
let mut dumper = self.dumper()?;
let mut client = self.connector().connect()?;

dumper.dump(&mut client)
}

fn connector(&self) -> Connector {
let options = &self.options;
Connector::new(
self.database_url.clone(),
options.accept_invalid_hostnames,
options.accept_invalid_certs,
)
}

fn dumper(&self) -> Result<PgDumper> {
let engine = self.engine()?;

Expand All @@ -52,48 +57,6 @@ impl App {
Ok(Engine::new(settings))
}

fn client(&self) -> Result<Client> {
let url = self.database_url.to_string();
let client = match self.tls_connector()? {
Some(c) => Client::connect(&url, c)?,
None => Client::connect(&url, NoTls)?,
};

Ok(client)
}

fn tls_connector(&self) -> Result<Option<MakeTlsConnector>> {
let ssl_mode = self
.database_url
.query_pairs()
.find_map(|(key, value)| {
if key == SSL_MODE_PARAM {
Some(value)
} else {
None
}
})
.unwrap_or(Cow::Borrowed(NO_SSL_MODE));

let connector = if ssl_mode == NO_SSL_MODE {
None
} else {
let mut builder = TlsConnector::builder();

if self.options.accept_invalid_hostnames {
builder.danger_accept_invalid_hostnames(true);
}
if self.options.accept_invalid_certs {
builder.danger_accept_invalid_certs(true);
}

let connector = builder.build()?;
Some(MakeTlsConnector::new(connector))
};

Ok(connector)
}

fn dump_isolation_level(&self) -> Option<IsolationLevel> {
match self.options.dump_transaction {
TransactionConfig::NoTransaction => None,
Expand All @@ -110,40 +73,6 @@ mod tests {
use super::*;
use structopt::StructOpt;

mod tls_connector {
use super::*;

fn connector(db_str: &str) -> Option<MakeTlsConnector> {
let options = Options::from_iter(vec!["DBNAME", db_str]);
let app = App::from_options(options).unwrap();
app.tls_connector().unwrap()
}

#[test]
fn default() {
let connector = connector("postgres://postgres@localhost/dbname");
assert!(connector.is_none());
}

#[test]
fn ssl_disable() {
let connector = connector("postgres://postgres@localhost/dbname?sslmode=disable");
assert!(connector.is_none());
}

#[test]
fn ssl_prefer() {
let connector = connector("postgres://postgres@localhost/dbname?sslmode=prefer");
assert!(connector.is_some());
}

#[test]
fn ssl_require() {
let connector = connector("postgres://postgres@localhost/dbname?sslmode=require");
assert!(connector.is_some());
}
}

mod isolation_level {
use super::*;

Expand Down
5 changes: 4 additions & 1 deletion datanymizer_dumper/Cargo.toml
Expand Up @@ -9,6 +9,9 @@ edition = "2021"
[dependencies]
datanymizer_engine = { path= "../datanymizer_engine" }
anyhow = "1.0"
indicatif = "0.15.0"
native-tls = "0.2.7"
postgres = "0.19.1"
postgres-native-tls = "0.5.0"
solvent = "0.8.2"
indicatif = "0.15.0"
url = "2.2"