Skip to content

Commit b919328

Browse files
Merge pull request #133 from timvaillancourt/mongodump_no_cmdline_passwd
1.0.2: Fix to remove server password from mongodump commandline if mongodump >= 3.0.2
2 parents ba04d92 + 1ccbc24 commit b919328

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

mongodb_consistent_backup/Backup/Mongodump/MongodumpThread.py

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,12 @@ def __init__(self, state, uri, timer, user, password, authdb, base_dir, binary,
3030
self.dump_gzip = dump_gzip
3131
self.verbose = verbose
3232

33-
self.timer_name = "%s-%s" % (self.__class__.__name__, self.uri.replset)
34-
self.exit_code = 1
35-
self._command = None
33+
self.timer_name = "%s-%s" % (self.__class__.__name__, self.uri.replset)
34+
self.exit_code = 1
35+
self._command = None
36+
self.do_stdin_passwd = False
37+
self.stdin_passwd_sent = False
38+
3639
self.backup_dir = os.path.join(self.base_dir, self.uri.replset)
3740
self.dump_dir = os.path.join(self.backup_dir, "dump")
3841
self.oplog_file = os.path.join(self.dump_dir, "oplog.bson")
@@ -61,6 +64,18 @@ def parse_mongodump_line(self, line):
6164
except:
6265
return None
6366

67+
def is_password_prompt(self, line):
68+
if self.do_stdin_passwd and ("Enter Password:" in line or "reading password from standard input" in line):
69+
return True
70+
return False
71+
72+
def handle_password_prompt(self):
73+
if self.do_stdin_passwd and not self.stdin_passwd_sent:
74+
logging.debug("Received password prompt from mongodump, writing password to stdin")
75+
self._process.stdin.write(self.password + "\n")
76+
self._process.stdin.flush()
77+
self.stdin_passwd_sent = True
78+
6479
def wait(self):
6580
try:
6681
while self._process.stderr:
@@ -69,7 +84,11 @@ def wait(self):
6984
for fd in poll[0]:
7085
read = self._process.stderr.readline()
7186
line = self.parse_mongodump_line(read)
72-
if line:
87+
if not line:
88+
continue
89+
elif self.is_password_prompt(read):
90+
self.handle_password_prompt()
91+
else:
7392
logging.info(line)
7493
if self._process.poll() != None:
7594
break
@@ -92,7 +111,12 @@ def mongodump_cmd(self):
92111
logging.debug("Using database %s for authentication" % self.authdb)
93112
mongodump_flags.extend(["--authenticationDatabase", self.authdb])
94113
if self.user and self.password:
95-
mongodump_flags.extend(["-u", self.user, "-p", self.password])
114+
# >= 3.0.2 supports password input via stdin to mask from ps
115+
if tuple("3.0.2".split(".")) <= tuple(self.version.split(".")):
116+
mongodump_flags.extend(["-u", self.user, "-p", '""'])
117+
self.do_stdin_passwd = True
118+
else:
119+
mongodump_flags.extend(["-u", self.user, "-p", self.password])
96120
mongodump_cmd.extend(mongodump_flags)
97121
return mongodump_cmd
98122

@@ -109,7 +133,7 @@ def run(self):
109133
rmtree(self.dump_dir)
110134
os.makedirs(self.dump_dir)
111135
logging.debug("Running mongodump cmd: %s" % mongodump_cmd)
112-
self._process = Popen(mongodump_cmd, stderr=PIPE)
136+
self._process = Popen(mongodump_cmd, stdin=PIPE, stderr=PIPE)
113137
self.wait()
114138
self.exit_code = self._process.returncode
115139
if self.exit_code > 0:

scripts/build.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ if [ -d ${srcdir} ]; then
100100
if [ ! -d ${pexdir} ]; then
101101
mkdir -p ${pexdir}
102102
else
103-
find ${pexdir} -type f -name "${mod_name}-*.whl" -print -delete
103+
find ${pexdir} -type f -name "${mod_name}-*.whl" -delete
104104
fi
105105
[ ! -d ${bindir} ] && mkdir -p ${bindir}
106106
${venvdir}/bin/python2.7 ${venvdir}/bin/pex -o ${output_file} -m ${mod_name} -r ${require_file} --pex-root=${pexdir} ${builddir}

0 commit comments

Comments
 (0)