Skip to content

Fix the issue in test_ssh_limit.py due to password rotation#12255

Merged
wangxin merged 11 commits intosonic-net:masterfrom
yutongzhang-microsoft:yutongzhang/fix_ssh_limit
Apr 3, 2024
Merged

Fix the issue in test_ssh_limit.py due to password rotation#12255
wangxin merged 11 commits intosonic-net:masterfrom
yutongzhang-microsoft:yutongzhang/fix_ssh_limit

Conversation

@yutongzhang-microsoft
Copy link
Contributor

@yutongzhang-microsoft yutongzhang-microsoft commented Apr 1, 2024

Description of PR

Fix the issue in test_ssh_limit.py due to password rotation

In this PR, we provide a public function connecting to device through paramiko. It can try either several potential passwords or a single password. We will use this new function to connect device.

Summary:
Fixes # (issue)

Type of change

  • Bug fix
  • Testbed and Framework(new/improvement)
  • Test case(new/improvement)

Back port request

  • 201911
  • 202012
  • 202205
  • 202305
  • 202311

Approach

What is the motivation for this PR?

Fix the issue in test_ssh_limit.py due to password rotation
In this PR, we provide a public function connecting to device through paramiko. It can try either several potential passwords or a single password. We will use this new function to connect device.

How did you do it?

How did you verify/test it?

Any platform specific information?

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

Documentation

@yejianquan
Copy link
Collaborator

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

remote_ip, username=remote_username, password=remote_password, allow_agent=False,
look_for_keys=False, auth_timeout=TIMEOUT_LIMIT)
return ssh
def ssh_connect_remote(duthost, remote_ip, remote_username, remote_passwords):
Copy link
Collaborator

Choose a reason for hiding this comment

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

This function is shared by tests in different folders. Why not move it to the common folder?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Another thing is that the duthost_ssh function can be more generic. The duthost argument is not mandatory for it. The function name can be more generic. Arguments do not need to have "sonic_" prefix.

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(ipaddr, username=username, password=password,
allow_agent=False, look_for_keys=False, timeout=10)
Copy link
Collaborator

Choose a reason for hiding this comment

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

There is code repetition here. It would be easier to workaround it with code like below:

if isinstance(passwords, str):
    candidate_passwords = [passwords]
elif isinstance(passwords, list):
    candidate_passwords = passwords
else:
    raise Exception("The passwords argument must be either a string or a list of string."# Then loop through the candidate_passwords to try connection.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good idea!


def duthost_ssh(duthost, sonic_username, sonic_passwords, sonic_ip):
for password in sonic_passwords:
def paramiko_ssh(username, passwords, ipaddr):
Copy link
Collaborator

Choose a reason for hiding this comment

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

This would be a common function to be used by lots of other tests. Add docstring as a document is important. Can you add docstring for it?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Name ipaddr may be conflicting with a library also named ipaddr.
The convention is to have ip address as the first argument. Then username and password.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

Copy link
Contributor

@xwjiang-ms xwjiang-ms left a comment

Choose a reason for hiding this comment

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

LGTM

@wangxin wangxin merged commit 7530c00 into sonic-net:master Apr 3, 2024
@yutongzhang-microsoft yutongzhang-microsoft deleted the yutongzhang/fix_ssh_limit branch April 7, 2024 07:54
wangxin pushed a commit that referenced this pull request Apr 9, 2024
This PR is a combination of #12253, #12255 and #12264 
Backport to 202305 branch
wangxin pushed a commit that referenced this pull request Apr 9, 2024
wangxin pushed a commit that referenced this pull request Apr 9, 2024
wangxin pushed a commit that referenced this pull request Apr 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants