Skip to content

Commit

Permalink
fix(mongo): remove the '--authenticationDatabase' option from mongo c…
Browse files Browse the repository at this point in the history
…ommands 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
  • Loading branch information
fabriceclementz authored Jun 10, 2022
1 parent 403c3c5 commit b6961c9
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 89 deletions.
25 changes: 5 additions & 20 deletions replibyte/src/commands/dump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?
Expand Down Expand Up @@ -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)?
Expand Down
29 changes: 5 additions & 24 deletions replibyte/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -223,7 +220,7 @@ impl SourceConfig {
#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
pub struct DestinationConfig {
pub connection_uri: String,
pub wipe_database: Option<bool>
pub wipe_database: Option<bool>,
}

impl DestinationConfig {
Expand Down Expand Up @@ -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<String, Error> {
Expand Down Expand Up @@ -456,17 +452,6 @@ fn get_database(url: &Url, default: Option<&str>) -> Result<String, Error> {
Ok(database.to_string())
}

fn get_mongodb_authentication_db(url: &Url) -> String {
let hash_query: HashMap<String, String> = 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<ConnectionUri, Error> {
let uri = substitute_env_var(uri)?;

Expand All @@ -493,11 +478,7 @@ fn parse_connection_uri(uri: &str) -> Result<ConnectionUri, Error> {
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(
Expand Down Expand Up @@ -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(),
)
)
}
Expand Down
28 changes: 5 additions & 23 deletions replibyte/src/destination/mongodb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}
}

Expand All @@ -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",
Expand All @@ -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()?;
Expand All @@ -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]
Expand Down
32 changes: 11 additions & 21 deletions replibyte/src/source/mongodb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}
}

Expand Down Expand Up @@ -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())
Expand All @@ -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()?;
Expand Down Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion replibyte/src/telemetry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ impl TelemetryClient {
match x.connection_uri()? {
ConnectionUri::Postgres(_, _, _, _, _) => "postgresql",
ConnectionUri::Mysql(_, _, _, _, _) => "mysql",
ConnectionUri::MongoDB(_, _, _) => "mongodb",
ConnectionUri::MongoDB(_, _) => "mongodb",
}
.to_string(),
);
Expand Down

0 comments on commit b6961c9

Please sign in to comment.