Skip to content

Retry from the first provided password if none of the passwords work#16870

Merged
bingwang-ms merged 2 commits intosonic-net:masterfrom
saiarcot895:make-alt-passwords-a-loop
Feb 12, 2025
Merged

Retry from the first provided password if none of the passwords work#16870
bingwang-ms merged 2 commits intosonic-net:masterfrom
saiarcot895:make-alt-passwords-a-loop

Conversation

@saiarcot895
Copy link
Contributor

Description of PR

Summary:
Fixes # (issue)

Type of change

  • Bug fix
  • Testbed and Framework(new/improvement)
  • New Test case
    • Skipped for non-supported platforms
  • Test case improvement

Back port request

  • 202012
  • 202205
  • 202305
  • 202311
  • 202405
  • 202411

Approach

What is the motivation for this PR?

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.

How did you do it?

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.

How did you verify/test it?

Any platform specific information?

Supported testbed topology if it's a new test case?

Documentation

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>
@mssonicbld
Copy link
Collaborator

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

Signed-off-by: Saikrishna Arcot <sarcot@microsoft.com>
@mssonicbld
Copy link
Collaborator

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

self.alt_password_index = 0
self.passwords = [password]
if alt_password:
self.passwords += alt_password
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$ python
Python 3.8.10 (default, Jan 17 2025, 14:40:23) 
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 
>>> 
>>> s = ["pass"]
>>> s += "pass2"
>>> s
['pass', 'p', 'a', 's', 's', '2']
>>> 

I don't think this is what you need. Please check?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alt_password is expected to be a list of strings.

The new behavior is the same as the old behavior; if someone passes in just a string, then each individual character of that string will get used as the password. I can add a check/handling here to accept only lists, or if a string is passed in, either error out or wrap it into a list.

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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where will this be retried from and what's the limit of retry?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a @retry decorator for this function that retries 4 times upon getting an AuthenticationException

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. The retry count should be a function of len(passwords)? So that your fix can take effect - every password can be tried at least twice?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally yes, but these variables are not defined when the @retry decorator is evaluated, so it can't be used. It actually might be a bit easier to do this without using the @retry decorator and doing the retry logic ourselves.

Copy link
Contributor

@vaibhavhd vaibhavhd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved with non blocking suggestions

@saiarcot895
Copy link
Contributor Author

/azpw run

@mssonicbld
Copy link
Collaborator

/AzurePipelines run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@bingwang-ms bingwang-ms merged commit 8108de2 into sonic-net:master Feb 12, 2025
17 checks passed
@saiarcot895 saiarcot895 deleted the make-alt-passwords-a-loop branch February 14, 2025 19:27
nnelluri-cisco pushed a commit to nnelluri-cisco/sonic-mgmt that referenced this pull request Mar 15, 2025
…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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants