Skip to content

Commit 4693dbf

Browse files
committed
MB-34149: Handle master password correctly.
With the new erlang distribution mechanism implemented in cluster manager, the master password command in the CLI stopped working. This change passes in the necessary arguments to the erlang VM that will be started by the CLI to set the master password. Earlier the CLI was scraping the stdout of the sub-process to determine the success/failure of the command. Now, the CLI will use the discrete exit codes returned by babysitter's helper API to determine success/failure of the command. This change is dependent on the following change: http://review.couchbase.org/#/c/111937/ Change-Id: I7b531bf26c385f0632ec3793a5ef22f958c26aec Reviewed-on: http://review.couchbase.org/111938 Reviewed-by: Patrick Varley <patrick@couchbase.com> Tested-by: Ajit Yagaty <ajit.yagaty@couchbase.com> Tested-by: Build Bot <build@couchbase.com>
1 parent 4b05d52 commit 4693dbf

1 file changed

Lines changed: 24 additions & 19 deletions

File tree

cbmgr.py

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535

3636
CB_BIN_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..", "bin"))
3737
CB_ETC_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..", "etc", "couchbase"))
38+
CB_LIB_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..", "lib"))
3839

3940
# On MacOS the config is store in the users home directory
4041
if platform.system() == "Darwin":
@@ -1404,37 +1405,41 @@ def execute(self, opts):
14041405
nodefile = os.path.join(opts.config_path, "couchbase-server.babysitter.node")
14051406
node = _exit_on_file_read_failure(nodefile).rstrip()
14061407

1407-
self.prompt_for_master_pwd(node, cookie, opts.send_password)
1408+
self.prompt_for_master_pwd(node, cookie, opts.send_password, opts.config_path)
14081409
else :
14091410
_exitIfErrors(["No parameters set"])
14101411

1411-
def prompt_for_master_pwd(self, node, cookie, password):
1412+
def prompt_for_master_pwd(self, node, cookie, password, cb_cfg_path):
1413+
ns_server_ebin_path = os.path.join(CB_LIB_PATH, "ns_server", "erlang", "lib", "ns_server", "ebin")
1414+
babystr_ebin_path = os.path.join(CB_LIB_PATH, "ns_server", "erlang", "lib", "ns_babysitter", "ebin")
1415+
inetrc_file = os.path.join(CB_ETC_PATH, "hosts.cfg")
1416+
dist_cfg_file = os.path.join(cb_cfg_path, "config", "dist_cfg")
1417+
14121418
if password == '':
14131419
password = getpass.getpass("\nEnter master password:")
1414-
password = "\"" + password.replace("\\", "\\\\").replace("\"", "\\\"") + "\""
1415-
1416-
randChars = ''.join(random.choice(string.ascii_letters) for i in range(20))
1417-
name = f'cb-{randChars}@127.0.0.1'
14181420

1419-
instr = "Res = rpc:call('" + node + "', encryption_service, set_password, [" \
1420-
+ password + "]), io:format(\"~p~n\", [Res])."
1421-
args = ["-noinput", "-name", name, "-setcookie", cookie, "-eval", \
1422-
instr, "-run", "init", "stop"]
1421+
name = f'executioner@cb.local'
1422+
args = ['-pa', ns_server_ebin_path, babystr_ebin_path, '-noinput', '-name', name, \
1423+
'-proto_dist', 'cb', '-epmd_module', 'cb_epmd', \
1424+
'-kernel', 'inetrc', f'"{inetrc_file}"', 'dist_config_file', f'"{dist_cfg_file}"', \
1425+
'-setcookie', cookie, \
1426+
'-run', 'encryption_service', 'remote_set_password', node, password]
14231427

1424-
res, error = self.run_process("erl", args)
1425-
res = res.decode().strip()
1428+
rc, out, err = self.run_process("erl", args)
14261429

1427-
if res == "ok":
1430+
if rc == 0:
14281431
print("SUCCESS: Password accepted. Node started booting.")
1429-
elif res == "retry":
1432+
elif rc == 1:
14301433
print("Incorrect password.")
1431-
self.prompt_for_master_pwd(node, cookie, '')
1432-
elif res == "{error,not_allowed}":
1434+
self.prompt_for_master_pwd(node, cookie, '', cb_cfg_path)
1435+
elif rc == 2:
14331436
_exitIfErrors(["Password was already supplied"])
1434-
elif res == "{badrpc,nodedown}":
1437+
elif rc == 3:
14351438
_exitIfErrors(["The node is down"])
1436-
else:
1439+
elif rc == 4:
14371440
_exitIfErrors(["Incorrect password. Node shuts down."])
1441+
else:
1442+
_exitIfErrors([f'Unknown error: {out}, {err}'])
14381443

14391444
def run_process(self, name, args):
14401445
try:
@@ -1447,7 +1452,7 @@ def run_process(self, name, args):
14471452
error = p.stderr.read()
14481453
p.wait()
14491454
rc = p.returncode
1450-
return output, error
1455+
return rc, output, error
14511456
except OSError:
14521457
_exitIfErrors([f'Could not locate the {name} executable'])
14531458

0 commit comments

Comments
 (0)