8000 feat(anta)!: Use HTTP HEAD request instead of a TCP connection to check if device is online by dlobato · Pull Request #851 · aristanetworks/anta · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat(anta)!: Use HTTP HEAD request instead of a TCP connection to check if device is online #851

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

dlobato
Copy link
Contributor
@dlobato dlobato commented Oct 4, 2024

Description

Use HTTP HEAD request to check connection to a device instead of pure L3 check.
This allows to check connectivity on devices behind a reverse proxy, so rather than checking the reverse proxy is alive using HTTP the request will be forwarded to the right device.

The change is as close to the original as possible, but we could potentially check if the credentials are correct as well.

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my code
  • I have run pre-commit for code linting and typing (pre-commit run)
  • I have made corresponding changes to the documentation
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes (tox -e testenv)

@dlobato dlobato force-pushed the check-connection-with-head branch from 049202b to 6e61446 Compare October 4, 2024 16:13
@dlobato dlobato changed the title refactor(asynceapi): Use http head request to check_connection refactor(anta): Use http head request to check_connection Oct 4, 2024
mtache
mtache previously requested changes Oct 10, 2024
Copy link
Collaborator
@mtache mtache left a comment

Choose a reason for hiding this comment

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

Hi David,

This changes the current behaviour, hence changing the PR name.

I understand that your use case is to actually check that eAPI is reachable behind a reverse proxy which is not possible today with the simple call to asyncio.open_connection().

I checked the behaviour of a HEAD request on the base_url and here is the result:

$ curl -k --head https://172.20.1.3
HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Thu, 10 Oct 2024 15:34:22 GMT
Content-Type: text/html
Content-Length: 13
Connection: keep-alive
Location: https://172.20.1.3/eapi/

https://172.20.1.3/eapi/ is actually the eAPI Explorer.
So we can either do that, expect and 301 and test the redirect URL (https://www.python-httpx.org/quickstart/#redirection-and-history)

Or we can do a HEAD on https://{base_url}/command-api and expect a 401 if not authenticated:

$ curl -k --head https://172.20.1.3/command-api
HTTP/1.1 401 Unauthorized
Server: nginx
Date: Thu, 10 Oct 2024 15:38:59 GMT
Content-Type: text/plain
Content-Length: 30
Connection: keep-alive

I assume the code should also expect a 200 if the asynceapi.Device instance is authenticated.

Please update the code to handle that behaviour and write unit tests :)

Regarding login, I like the idea of checking the credentials before sending the "show version" eAPI request like we do today. I've opened #871 to track this.

Thanks !

@mtache mtache added this to the v2.0.0 milestone Oct 10, 2024
@mtache mtache changed the title refactor(anta): Use http head request to check_connection feat(anta)!: Use HTTP HEAD request instead of opening a TCP connection Oct 10, 2024
@mtache mtache changed the title feat(anta)!: Use HTTP HEAD request instead of opening a TCP connection feat(anta)!: Use HTTP HEAD request instead of a TCP connection to check if device is online Oct 10, 2024
@dlobato
Copy link
Contributor Author
dlobato commented Oct 11, 2024

Hi Matthieu,

I also think that requesting headers from command-api would be better. Since we have the credentials we can then check the status code and conclude if we can actually connect to the eAPI server.

I'll update the code and add the required tests.

David

@dlobato dlobato force-pushed the check-connection-with-head branch from 6bf8440 to 0235699 Compare October 11, 2024 15:05
Copy link

Copy link
codspeed-hq bot commented Oct 11, 2024

CodSpeed Performance Report

Merging #851 will not alter performance

Comparing dlobato:check-connection-with-head (3127789) with main (311b107)

Summary

✅ 22 untouched benchmarks

Copy link
Contributor

This pull request has conflicts, please resolve those before we can evaluate the pull request.

Copy link
Contributor

Conflicts have been resolved. A maintainer will review the pull request shortly.

Copy link

Copy link
Contributor
github-actions bot commented Jan 2, 2025

This pull request has conflicts, please resolve those before we can evaluate the pull request.

@dlobato dlobato force-pushed the check-connection-with-head branch from b2b46e7 to d9b4b61 Compare February 21, 2025 09:19
Copy link
Contributor

Conflicts have been resolved. A maintainer will review the pull request shortly.

Copy link

Copy link
Contributor

This pull request has conflicts, please resolve those before we can evaluate the pull request.

Copy link
Contributor
@carl-baillargeon carl-baillargeon left a comment

Choose a reason for hiding this comment

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

@gmuloc I am ok with merging this (after fixing my review comments) in a minor release but please confirm and consider the following for refresh:

1- Currently, is_online could be True even if subsequent commands failed due to HTTP errors (like 401 Unauthorized). Now, such errors will cause is_online to be False right away.

2- Errors like 401, 500 are detected before attempting show version, whereas before they were detected after the _collect(show_version) failed.

3- The warning message associated with these failures changes from potentially Cannot get hardware information... (if _collect failed) to the new Could not connect... eAPI endpoint: [Error details].

4- Because of this, self.established will become False earlier and for more reasons (HTTP errors during the initial check) than before.

To me it's arguably an improvement/fix but I will let you chime in.

* HEAD request to /command-api url
* Handle HTTPStatus error in refresh, log error message
* Update unit tests
@dlobato dlobato force-pushed the check-connection-with-head branch from d9b4b61 to 6c1e9aa Compare April 9, 2025 08:09
Copy link
Contributor
github-actions bot commented Apr 9, 2025

Conflicts have been resolved. A maintainer will review the pull request shortly.

@gmuloc gmuloc dismissed mtache’s stale review April 9, 2025 15:47

Comments addressed

Copy link
Contributor
@carl-baillargeon carl-baillargeon left a comment

Choose a reason for hiding this comment

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

LGTM.

@gmuloc gmuloc modified the milestones: v2.0.0, v.1.4.0 Apr 9, 2025
Copy link
sonarqubecloud bot commented Apr 9, 2025

@carl-baillargeon carl-baillargeon merged commit 7835a2b into aristanetworks:main Apr 9, 2025
24 checks passed
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
0