|
8 | 8 | import platform |
9 | 9 | import random |
10 | 10 | import re |
| 11 | +import socket |
11 | 12 | import string |
12 | 13 | import subprocess |
13 | 14 | import sys |
@@ -1681,55 +1682,47 @@ def __init__(self): |
1681 | 1682 |
|
1682 | 1683 | def execute(self, opts): |
1683 | 1684 | if opts.send_password is not None: |
1684 | | - path = [CB_BIN_PATH, os.environ['PATH']] |
1685 | | - if os.name == 'posix': |
1686 | | - os.environ['PATH'] = ':'.join(path) |
1687 | | - else: |
1688 | | - os.environ['PATH'] = ';'.join(path) |
1689 | | - |
1690 | | - cookiefile = os.path.join(opts.config_path, "couchbase-server.babysitter.cookie") |
1691 | | - if not os.path.isfile(cookiefile): |
| 1685 | + portfile = os.path.join(opts.config_path, "couchbase-server.babysitter.smport") |
| 1686 | + if not os.path.isfile(portfile): |
1692 | 1687 | _exit_if_errors(["The node is down"]) |
1693 | | - cookie = _exit_on_file_read_failure(cookiefile, "Insufficient privileges to send master password - Please" |
1694 | | - " execute this command as a operating system user who has" |
1695 | | - " file system read permission on the Couchbase Server " |
1696 | | - " configuration").rstrip() |
1697 | | - |
1698 | | - nodefile = os.path.join(opts.config_path, "couchbase-server.babysitter.node") |
1699 | | - node = _exit_on_file_read_failure(nodefile).rstrip() |
1700 | | - |
1701 | | - self.prompt_for_master_pwd(node, cookie, opts.send_password, opts.config_path) |
| 1688 | + familyport = _exit_on_file_read_failure( |
| 1689 | + portfile, "Insufficient privileges to send master password - Please" |
| 1690 | + " execute this command as an operating system user who has" |
| 1691 | + " file system read permission on the Couchbase Server " |
| 1692 | + " configuration").rstrip() |
| 1693 | + [afamilystr, portstr] = familyport.split() |
| 1694 | + port = int(portstr) |
| 1695 | + afamily = socket.AF_INET6 if afamilystr == "inet6" else socket.AF_INET |
| 1696 | + self.prompt_for_master_pwd(afamily, port, opts.send_password) |
1702 | 1697 | else: |
1703 | 1698 | _exit_if_errors(["No parameters set"]) |
1704 | 1699 |
|
1705 | | - def prompt_for_master_pwd(self, node, cookie, password, cb_cfg_path): |
1706 | | - dist_cfg_file = os.path.join(cb_cfg_path, "config", "dist_cfg") |
1707 | | - |
| 1700 | + def prompt_for_master_pwd(self, afamily, port, password): |
1708 | 1701 | if password == '': |
1709 | 1702 | password = getpass.getpass("\nEnter master password:") |
1710 | 1703 |
|
1711 | | - name = 'executioner@cb.local' |
1712 | | - args = ['-pa', CB_NS_EBIN_PATH, CB_BABYSITTER_EBIN_PATH, '-noinput', '-name', name, '-proto_dist', 'cb', |
1713 | | - '-eval', 'erlang:set_cookie(node(), list_to_atom(os:getenv("CB_COOKIE"))).', '-epmd_module', 'cb_epmd', |
1714 | | - '-kernel'] + CB_INETRC_OPT + \ |
1715 | | - ['dist_config_file', f'"{dist_cfg_file}"', '-run', 'encryption_service', |
1716 | | - 'remote_set_password', node] |
1717 | | - |
1718 | | - rc, out, err = self.run_process("erl", args, extra_env={'SETPASSWORD': password, 'CB_COOKIE': cookie}) |
1719 | | - |
1720 | | - if rc == 0: |
| 1704 | + sock = socket.socket(afamily, socket.SOCK_DGRAM) |
| 1705 | + try: |
| 1706 | + sock.settimeout(5) |
| 1707 | + addr = "::1" if afamily == socket.AF_INET6 else "127.0.0.1" |
| 1708 | + sock.sendto(password.encode('utf-8'), (addr, port)) |
| 1709 | + (result, _) = sock.recvfrom(128) |
| 1710 | + except socket.timeout: |
| 1711 | + result = b'timeout' |
| 1712 | + finally: |
| 1713 | + sock.close() |
| 1714 | + |
| 1715 | + if result == b'ok': |
1721 | 1716 | print("SUCCESS: Password accepted. Node started booting.") |
1722 | | - elif rc == 101: |
| 1717 | + elif result == b'retry': |
1723 | 1718 | print("Incorrect password.") |
1724 | | - self.prompt_for_master_pwd(node, cookie, '', cb_cfg_path) |
1725 | | - elif rc == 102: |
1726 | | - _exit_if_errors(["Password was already supplied"]) |
1727 | | - elif rc == 103: |
1728 | | - _exit_if_errors(["The node is down"]) |
1729 | | - elif rc == 104: |
| 1719 | + self.prompt_for_master_pwd(afamily, port, '') |
| 1720 | + elif result == b'timeout': |
| 1721 | + _exit_if_errors(["Timeout"]) |
| 1722 | + elif result == b'auth_failure': |
1730 | 1723 | _exit_if_errors(["Incorrect password. Node shuts down."]) |
1731 | 1724 | else: |
1732 | | - _exit_if_errors([f'Unknown error: {rc} {out}, {err}']) |
| 1725 | + _exit_if_errors([f'Unknown error: {result}']) |
1733 | 1726 |
|
1734 | 1727 | def run_process(self, name, args, extra_env=None): |
1735 | 1728 | try: |
|
0 commit comments