Skip to content

Commit

Permalink
Fix #830: mlaunch: --auth does not quote --keyFile parameter (#843)
Browse files Browse the repository at this point in the history
* Fix #830: mlaunch: --auth does not quote --keyFile parameter

* Remove commented SystemExit and extra space around param
  • Loading branch information
stennie authored Nov 30, 2021
1 parent 9329ce9 commit 419f159
Showing 1 changed file with 31 additions and 12 deletions.
43 changes: 31 additions & 12 deletions mtools/mlaunch/mlaunch.py
Original file line number Diff line number Diff line change
Expand Up @@ -699,10 +699,22 @@ def init(self):
if self.args['auth'] and first_init:
if not os.path.exists(self.dir):
os.makedirs(self.dir)
keyfile = os.path.join(self.dir, "keyfile")
os.system('openssl rand -base64 753 > %s' % keyfile)
if os.name != 'nt':
os.system('chmod 600 %s' % keyfile)

if '--keyFile' in self.unknown_args:
# Check if keyfile is readable
keyfile = None
try:
keyfile_idx = self.unknown_args.index('--keyFile') + 1
keyfile_path = self.unknown_args[keyfile_idx]
keyfile = self._read_key_file(keyfile_path)
except:
print(f'\n WARNING: Specified keyFile does not appear readable: {keyfile_path}\n')
else:
keyfile = os.path.join(self.dir, "keyfile")
print(f'Generating keyfile: {keyfile}')
os.system('openssl rand -base64 753 > "%s"' % keyfile)
if os.name != 'nt':
os.system(f'chmod 600 "{keyfile}"')

# if not all ports are free, complain and suggest alternatives.
all_ports = self.get_tagged(['all'])
Expand Down Expand Up @@ -2128,8 +2140,10 @@ def _construct_mongod(self, dbpath, logpath, port, replset=None, extra=''):

auth_param = ''
if self.args['auth']:
key_path = os.path.abspath(os.path.join(self.dir, 'keyfile'))
auth_param = '--keyFile %s' % key_path
auth_param = '--auth'
if '--keyFile' not in self.unknown_args:
key_path = os.path.abspath(os.path.join(self.dir, 'keyfile'))
auth_param = f'{auth_param} --keyFile "{key_path}"'

if self.unknown_args:
config = '--configsvr' in extra
Expand Down Expand Up @@ -2181,8 +2195,10 @@ def _construct_mongos(self, logpath, port, configdb):

auth_param = ''
if self.args['auth']:
key_path = os.path.abspath(os.path.join(self.dir, 'keyfile'))
auth_param = '--keyFile %s' % key_path
auth_param = '--auth'
if '--keyFile' not in self.unknown_args:
key_path = os.path.abspath(os.path.join(self.dir, 'keyfile'))
auth_param = f'{auth_param} --keyFile "{key_path}"'

if self.unknown_args:
extra = self._filter_valid_arguments(self.unknown_args,
Expand All @@ -2205,10 +2221,13 @@ def _construct_mongos(self, logpath, port, configdb):
# store parameters in startup_info
self.startup_info[str(port)] = command_str

def _read_key_file(self):
with open(os.path.join(self.dir, 'keyfile'), 'rb') as f:
return ''.join(f.readlines())

def _read_key_file(self, keyfile=None):
if not keyfile:
with open(os.path.join(self.dir, 'keyfile'), 'rb') as f:
return ''.join(f.readlines())
else:
with open(keyfile, 'rb') as f:
return ''.join(f.readlines())

def main():
tool = MLaunchTool()
Expand Down

0 comments on commit 419f159

Please sign in to comment.