From b6961c9673997a29ad1cee6305fb77f0e455d43e Mon Sep 17 00:00:00 2001 From: Fabrice Clementz Date: Fri, 10 Jun 2022 21:42:18 +0200 Subject: [PATCH] fix(mongo): remove the '--authenticationDatabase' option from mongo commands as we pass the '--uri' option (#163) * fix(mongo): remove the '--authenticationDatabase' option from mongo commands as we pass the '--uri' option * make tests pass --- replibyte/src/commands/dump.rs | 25 +++++----------------- replibyte/src/config.rs | 29 +++++-------------------- replibyte/src/destination/mongodb.rs | 28 +++++------------------- replibyte/src/source/mongodb.rs | 32 ++++++++++------------------ replibyte/src/telemetry.rs | 2 +- 5 files changed, 27 insertions(+), 89 deletions(-) diff --git a/replibyte/src/commands/dump.rs b/replibyte/src/commands/dump.rs index 1d7bf5c6..943b3466 100644 --- a/replibyte/src/commands/dump.rs +++ b/replibyte/src/commands/dump.rs @@ -138,16 +138,8 @@ where let task = FullDumpTask::new(mysql, datastore, options); task.run(progress_callback)? } - ConnectionUri::MongoDB( - uri, - database, - authentication_db, - ) => { - let mongodb = MongoDB::new( - uri.as_str(), - database.as_str(), - authentication_db.as_str(), - ); + ConnectionUri::MongoDB(uri, database) => { + let mongodb = MongoDB::new(uri.as_str(), database.as_str()); let task = FullDumpTask::new(mongodb, datastore, options); task.run(progress_callback)? @@ -452,16 +444,9 @@ where let task = FullRestoreTask::new(&mut mysql, datastore, options); task.run(progress_callback)?; } - ConnectionUri::MongoDB( - uri, - database, - authentication_db, - ) => { - let mut mongodb = destination::mongodb::MongoDB::new( - uri.as_str(), - database.as_str(), - authentication_db.as_str(), - ); + ConnectionUri::MongoDB(uri, database) => { + let mut mongodb = + destination::mongodb::MongoDB::new(uri.as_str(), database.as_str()); let task = FullRestoreTask::new(&mut mongodb, datastore, options); task.run(progress_callback)? diff --git a/replibyte/src/config.rs b/replibyte/src/config.rs index 2c8b5f1b..894f6f18 100644 --- a/replibyte/src/config.rs +++ b/replibyte/src/config.rs @@ -11,12 +11,9 @@ use crate::transformer::Transformer; use percent_encoding::percent_decode_str; use serde; use serde::{Deserialize, Serialize}; -use std::collections::HashMap; use std::io::{Error, ErrorKind}; use url::Url; -const DEFAULT_MONGODB_AUTH_DB: &str = "admin"; - #[derive(Debug, PartialEq, Serialize, Deserialize, Clone)] pub struct Config { // pub bind: Ipv4Addr, @@ -223,7 +220,7 @@ impl SourceConfig { #[derive(Debug, PartialEq, Serialize, Deserialize, Clone)] pub struct DestinationConfig { pub connection_uri: String, - pub wipe_database: Option + pub wipe_database: Option, } impl DestinationConfig { @@ -376,14 +373,13 @@ type Port = u16; type Username = String; type Password = String; type Database = String; -type AuthenticationDatabase = String; type Uri = String; #[derive(Debug, PartialEq, Clone)] pub enum ConnectionUri { Postgres(Host, Port, Username, Password, Database), Mysql(Host, Port, Username, Password, Database), - MongoDB(Uri, Database, AuthenticationDatabase), + MongoDB(Uri, Database), } fn get_host(url: &Url) -> Result { @@ -456,17 +452,6 @@ fn get_database(url: &Url, default: Option<&str>) -> Result { Ok(database.to_string()) } -fn get_mongodb_authentication_db(url: &Url) -> String { - let hash_query: HashMap = url.query_pairs().into_owned().collect(); - - let authentication_database = match hash_query.get("authSource") { - Some(authentication_db) => authentication_db.to_string(), - None => DEFAULT_MONGODB_AUTH_DB.to_string(), - }; - - authentication_database -} - fn parse_connection_uri(uri: &str) -> Result { let uri = substitute_env_var(uri)?; @@ -493,11 +478,7 @@ fn parse_connection_uri(uri: &str) -> Result { get_database(&url, None)?, ), scheme if scheme.to_lowercase() == "mongodb" || scheme.to_lowercase() == "mongodb+srv" => { - ConnectionUri::MongoDB( - url.to_string(), - get_database(&url, Some("test"))?, - get_mongodb_authentication_db(&url), - ) + ConnectionUri::MongoDB(url.to_string(), get_database(&url, Some("test"))?) } scheme => { return Err(Error::new( @@ -651,9 +632,9 @@ mod tests { assert_eq!( connection_uri, ConnectionUri::MongoDB( - "mongodb+srv://root:password@server.example.com/my_db?authSource=other_db".to_string(), + "mongodb+srv://root:password@server.example.com/my_db?authSource=other_db" + .to_string(), "my_db".to_string(), - "other_db".to_string(), ) ) } diff --git a/replibyte/src/destination/mongodb.rs b/replibyte/src/destination/mongodb.rs index af782852..d2a821fe 100644 --- a/replibyte/src/destination/mongodb.rs +++ b/replibyte/src/destination/mongodb.rs @@ -9,20 +9,11 @@ use crate::utils::{binary_exists, wait_for_command}; pub struct MongoDB<'a> { uri: &'a str, database: &'a str, - authentication_db: &'a str, } impl<'a> MongoDB<'a> { - pub fn new( - uri: &'a str, - database: &'a str, - authentication_db: &'a str, - ) -> Self { - MongoDB { - uri, - database, - authentication_db, - } + pub fn new(uri: &'a str, database: &'a str) -> Self { + MongoDB { uri, database } } } @@ -38,13 +29,10 @@ impl<'a> Connector for MongoDB<'a> { impl<'a> Destination for MongoDB<'a> { fn write(&self, data: Bytes) -> Result<(), Error> { - let mut process = Command::new("mongorestore") .args([ "--uri", self.uri, - "--authenticationDatabase", - self.authentication_db, format!("--nsFrom='{}.*'", self.database).as_str(), format!("--nsTo='{}.*'", self.database).as_str(), "--archive", @@ -64,19 +52,13 @@ impl<'a> Destination for MongoDB<'a> { } fn check_connection_status(db: &MongoDB) -> Result<(), Error> { - let mut echo_process = Command::new("echo") .arg(r#"'db.runCommand("ping").ok'"#) .stdout(Stdio::piped()) .spawn()?; let mut mongo_process = Command::new("mongosh") - .args([ - db.uri, - "--authenticationDatabase", - db.authentication_db, - "--quiet", - ]) + .args([db.uri, "--quiet"]) .stdin(echo_process.stdout.take().unwrap()) .stdout(Stdio::inherit()) .spawn()?; @@ -93,11 +75,11 @@ mod tests { use crate::destination::Destination; fn get_mongodb() -> MongoDB<'static> { - MongoDB::new("mongodb://root:password@localhost:27018", "test", "admin") + MongoDB::new("mongodb://root:password@localhost:27018", "test") } fn get_invalid_mongodb() -> MongoDB<'static> { - MongoDB::new("mongodb://root:wrongpassword@localhost:27018", "test", "admin") + MongoDB::new("mongodb://root:wrongpassword@localhost:27018", "test") } #[test] diff --git a/replibyte/src/source/mongodb.rs b/replibyte/src/source/mongodb.rs index a11ef93e..0fded4c0 100644 --- a/replibyte/src/source/mongodb.rs +++ b/replibyte/src/source/mongodb.rs @@ -15,20 +15,11 @@ use dump_parser::mongodb::Archive; pub struct MongoDB<'a> { uri: &'a str, database: &'a str, - authentication_db: &'a str, } impl<'a> MongoDB<'a> { - pub fn new( - uri: &'a str, - database: &'a str, - authentication_db: &'a str, - ) -> Self { - MongoDB { - uri, - database, - authentication_db - } + pub fn new(uri: &'a str, database: &'a str) -> Self { + MongoDB { uri, database } } } @@ -58,8 +49,6 @@ impl<'a> Source for MongoDB<'a> { self.uri, "--db", self.database, - "--authenticationDatabase", - self.authentication_db, "--archive", // dump to stdin ]) .stdout(Stdio::piped()) @@ -86,12 +75,7 @@ fn check_connection_status(db: &MongoDB) -> Result<(), Error> { .spawn()?; let mut mongo_process = Command::new("mongosh") - .args([ - db.uri, - "--authenticationDatabase", - db.authentication_db, - "--quiet" - ]) + .args([db.uri, "--quiet"]) .stdin(echo_process.stdout.take().unwrap()) .stdout(Stdio::inherit()) .spawn()?; @@ -299,11 +283,17 @@ mod tests { use super::recursively_transform_document; fn get_mongodb() -> MongoDB<'static> { - MongoDB::new("mongodb://root:password@localhost:27018/", "test", "admin") + MongoDB::new( + "mongodb://root:password@localhost:27018/test?authSource=admin", + "test", + ) } fn get_invalid_mongodb() -> MongoDB<'static> { - MongoDB::new("mongodb://root:wrongpassword@localhost:27018/", "test", "admin") + MongoDB::new( + "mongodb://root:wrongpassword@localhost:27018/test?authSource=admin", + "test", + ) } #[test] diff --git a/replibyte/src/telemetry.rs b/replibyte/src/telemetry.rs index 4ee15e75..c3b84a60 100644 --- a/replibyte/src/telemetry.rs +++ b/replibyte/src/telemetry.rs @@ -86,7 +86,7 @@ impl TelemetryClient { match x.connection_uri()? { ConnectionUri::Postgres(_, _, _, _, _) => "postgresql", ConnectionUri::Mysql(_, _, _, _, _) => "mysql", - ConnectionUri::MongoDB(_, _, _) => "mongodb", + ConnectionUri::MongoDB(_, _) => "mongodb", } .to_string(), );