feat(inputs.http_response): Add cookie authentication (#15110)

This commit is contained in:
Dane Strandboge 2024-04-15 16:47:30 -05:00 committed by GitHub
parent e2aa248242
commit 43687b4e23
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 39 additions and 0 deletions

View File

@ -106,6 +106,15 @@ to use them.
## Interface to use when dialing an address
# interface = "eth0"
## Optional Cookie authentication
# cookie_auth_url = "https://localhost/authMe"
# cookie_auth_method = "POST"
# cookie_auth_username = "username"
# cookie_auth_password = "pa$$word"
# cookie_auth_body = '{"username": "user", "password": "pa$$word", "authenticate": "me"}'
## cookie_auth_renewal not set or set to "0" will auth once and never renew the cookie
# cookie_auth_renewal = "5m"
```
## Metrics
@ -150,3 +159,14 @@ a successful connection.
```text
http_response,method=GET,result=success,server=http://github.com,status_code=200 content_length=87878i,http_response_code=200i,response_time=0.937655534,result_code=0i,result_type="success" 1565839598000000000
```
## Optional Cookie Authentication Settings
The optional Cookie Authentication Settings will retrieve a cookie from the
given authorization endpoint, and use it in subsequent API requests. This is
useful for services that do not provide OAuth or Basic Auth authentication,
e.g. the [Tesla Powerwall API][tesla], which uses a Cookie Auth Body to retrieve
an authorization cookie. The Cookie Auth Renewal interval will renew the
authorization by retrieving a new cookie at the given interval.
[tesla]: https://www.tesla.com/support/energy/powerwall/own/monitoring-from-home-network

View File

@ -16,9 +16,11 @@ import (
"time"
"unicode/utf8"
"github.com/benbjohnson/clock"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/config"
"github.com/influxdata/telegraf/internal"
"github.com/influxdata/telegraf/plugins/common/cookie"
"github.com/influxdata/telegraf/plugins/common/tls"
"github.com/influxdata/telegraf/plugins/inputs"
)
@ -55,6 +57,7 @@ type HTTPResponse struct {
Username config.Secret `toml:"username"`
Password config.Secret `toml:"password"`
tls.ClientConfig
cookie.CookieAuthConfig
Log telegraf.Logger
@ -114,6 +117,13 @@ func (h *HTTPResponse) createHTTPClient() (*http.Client, error) {
return http.ErrUseLastResponse
}
}
if h.CookieAuthConfig.URL != "" {
if err := h.CookieAuthConfig.Start(client, h.Log, clock.New()); err != nil {
return nil, err
}
}
return client, nil
}

View File

@ -81,3 +81,12 @@
## Interface to use when dialing an address
# interface = "eth0"
## Optional Cookie authentication
# cookie_auth_url = "https://localhost/authMe"
# cookie_auth_method = "POST"
# cookie_auth_username = "username"
# cookie_auth_password = "pa$$word"
# cookie_auth_body = '{"username": "user", "password": "pa$$word", "authenticate": "me"}'
## cookie_auth_renewal not set or set to "0" will auth once and never renew the cookie
# cookie_auth_renewal = "5m"