MCPcopy Create free account
hub / github.com/Keeper-Security/Commander / rotate_ssh

Function rotate_ssh

keepercommander/plugins/ssh/ssh.py:52–202  ·  view source on GitHub ↗

Rotate an SSH password host(str): SSH host port(int): SSH port user(str): SSH login name old_password(str): old password new_password(str): new password timeout(int): SSH connection timeout in seconds revert(bool): True to revert a previous rotation

(host, port, user, old_password, new_password, timeout=5, revert=False)

Source from the content-addressed store, hash-verified

50
51
52def rotate_ssh(host, port, user, old_password, new_password, timeout=5, revert=False):
53 """Rotate an SSH password
54
55 host(str): SSH host
56 port(int): SSH port
57 user(str): SSH login name
58 old_password(str): old password
59 new_password(str): new password
60 timeout(int): SSH connection timeout in seconds
61 revert(bool): True to revert a previous rotation
62 """
63 from ..commands import validate_shell_command_parameter
64
65 if not validate_shell_command_parameter(user, 'User login'):
66 return False
67 if not validate_shell_command_parameter(new_password, 'New password'):
68 return False
69
70 rotate_success = False
71 ssh_logger = logging.getLogger('paramiko')
72 ssh_logger.setLevel(logging.WARNING)
73 with paramiko.SSHClient() as ssh:
74 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
75 try:
76 ssh.connect(
77 hostname=host, port=port, username=user, password=old_password,
78 timeout=timeout, allow_agent=False, look_for_keys=False
79 )
80 except paramiko.ssh_exception.AuthenticationException:
81 if revert:
82 logging.error('SSH authentication was unsuccessful for revert of rotation.')
83 else:
84 logging.error('SSH authentication was unsuccessful using current password.')
85 return False
86 except socket.timeout:
87 logging.error('Connection to host timed out.')
88 return False
89 except Exception as e:
90 logging.error(f'Unrecognized connection error: {e}')
91 return False
92 stdin, stdout, stderr = ssh.exec_command('ver')
93 if ''.join(stdout.readlines()).strip().startswith('Microsoft Windows'):
94 try:
95 stdin, stdout, stderr = ssh.exec_command(
96 f'net user {user} {new_password}'
97 )
98 result = ''.join(stdout.readlines()).strip()
99 if result == 'The command completed successfully.':
100 rotate_success = True
101 logging.debug(result)
102 else:
103 logging.warning(f'Unrecognized result: "{result}"')
104 except Exception as e:
105 # Catch exception because password
106 # could have still been rotated and we need to verify
107 logging.error(str(e))
108 else:
109 stdin, stdout, stderr = ssh.exec_command('which passwd')

Callers 2

rotateMethod · 0.85
revertMethod · 0.85

Calls 7

warningMethod · 0.80
connectMethod · 0.45
errorMethod · 0.45
debugMethod · 0.45
sendMethod · 0.45
infoMethod · 0.45

Tested by

no test coverage detected