Skip to content

Commit

Permalink
Retry from the first provided password if none of the passwords work (s…
Browse files Browse the repository at this point in the history
…onic-net#16870)

* Retry from the first provided password if none of the passwords work

In the DeviceConnection class, if none of the passwords appear to work,
then try again from the first provided password. The reason for this is
if a a device initially needs some password specified in the alternate
passwords list, but then later needs to use some earlier-specified
password (because of some config change), then connection attempts will
fail until a new DeviceConnection object is instantiated.

Instead, work around that by trying the passwords in a loop (i.e. from
the beginning again). This also means that the class doesn't really need
to keep track of what password might be the "primary" password and what
password might be alternates.

Signed-off-by: Saikrishna Arcot <sarcot@microsoft.com>

* Remove extra self

Signed-off-by: Saikrishna Arcot <sarcot@microsoft.com>

---------

Signed-off-by: Saikrishna Arcot <sarcot@microsoft.com>
  • Loading branch information
saiarcot895 authored Feb 12, 2025
1 parent 023a8e8 commit 8108de2
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions ansible/roles/test/files/ptftests/device_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ def __init__(self, hostname, username, password=None, alt_password=None):
'''
self.hostname = hostname
self.username = username
self.password = password
self.alt_password = alt_password
self.alt_password_index = 0
self.passwords = [password]
if alt_password:
self.passwords += alt_password
self.password_index = 0

@retry(
stop_max_attempt_number=4,
Expand Down Expand Up @@ -62,18 +63,17 @@ def execCommand(self, cmd, timeout=DEFAULT_CMD_EXECUTION_TIMEOUT_SEC):
retValue = 1
try:
client.connect(self.hostname, username=self.username,
password=self.password, allow_agent=False)
password=self.passwords[self.password_index], allow_agent=False)
si, so, se = client.exec_command(cmd, timeout=timeout)
stdOut = so.readlines()
stdErr = se.readlines()
retValue = 0
except AuthenticationException as authenticationException:
logger.error('SSH Authentication failure with message: %s' %
authenticationException)
if self.alt_password is not None and self.alt_password_index < len(self.alt_password):
# attempt retry with alt_password
self.password = self.alt_password[self.alt_password_index]
self.alt_password_index += 1
if len(self.passwords) > 1:
# attempt retry with another password
self.password_index = (self.password_index + 1) % len(self.passwords)
raise AuthenticationException
except SSHException as sshException:
logger.error('SSH Command failed with message: %s' % sshException)
Expand Down

0 comments on commit 8108de2

Please sign in to comment.