feat: support headers for http plugin with cookie auth (#10404)
This commit is contained in:
parent
457c98f4c2
commit
48b981bd4e
|
|
@ -19,6 +19,8 @@ type CookieAuthConfig struct {
|
||||||
URL string `toml:"cookie_auth_url"`
|
URL string `toml:"cookie_auth_url"`
|
||||||
Method string `toml:"cookie_auth_method"`
|
Method string `toml:"cookie_auth_method"`
|
||||||
|
|
||||||
|
Headers map[string]string `toml:"cookie_auth_headers"`
|
||||||
|
|
||||||
// HTTP Basic Auth Credentials
|
// HTTP Basic Auth Credentials
|
||||||
Username string `toml:"cookie_auth_username"`
|
Username string `toml:"cookie_auth_username"`
|
||||||
Password string `toml:"cookie_auth_password"`
|
Password string `toml:"cookie_auth_password"`
|
||||||
|
|
@ -90,6 +92,14 @@ func (c *CookieAuthConfig) auth() error {
|
||||||
req.SetBasicAuth(c.Username, c.Password)
|
req.SetBasicAuth(c.Username, c.Password)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for k, v := range c.Headers {
|
||||||
|
if strings.ToLower(k) == "host" {
|
||||||
|
req.Host = v
|
||||||
|
} else {
|
||||||
|
req.Header.Add(k, v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
resp, err := c.client.Do(req)
|
resp, err := c.client.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
|
||||||
|
|
@ -18,14 +18,17 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
reqUser = "testUser"
|
reqUser = "testUser"
|
||||||
reqPasswd = "testPassword"
|
reqPasswd = "testPassword"
|
||||||
reqBody = "a body"
|
reqBody = "a body"
|
||||||
|
reqHeaderKey = "hello"
|
||||||
|
reqHeaderVal = "world"
|
||||||
|
|
||||||
authEndpointNoCreds = "/auth"
|
authEndpointNoCreds = "/auth"
|
||||||
authEndpointWithBasicAuth = "/authWithCreds"
|
authEndpointWithBasicAuth = "/authWithCreds"
|
||||||
authEndpointWithBasicAuthOnlyUsername = "/authWithCredsUser"
|
authEndpointWithBasicAuthOnlyUsername = "/authWithCredsUser"
|
||||||
authEndpointWithBody = "/authWithBody"
|
authEndpointWithBody = "/authWithBody"
|
||||||
|
authEndpointWithHeader = "/authWithHeader"
|
||||||
)
|
)
|
||||||
|
|
||||||
var fakeCookie = &http.Cookie{
|
var fakeCookie = &http.Cookie{
|
||||||
|
|
@ -49,6 +52,12 @@ func newFakeServer(t *testing.T) fakeServer {
|
||||||
switch r.URL.Path {
|
switch r.URL.Path {
|
||||||
case authEndpointNoCreds:
|
case authEndpointNoCreds:
|
||||||
authed()
|
authed()
|
||||||
|
case authEndpointWithHeader:
|
||||||
|
if !cmp.Equal(r.Header.Get(reqHeaderKey), reqHeaderVal) {
|
||||||
|
w.WriteHeader(http.StatusUnauthorized)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
authed()
|
||||||
case authEndpointWithBody:
|
case authEndpointWithBody:
|
||||||
body, err := io.ReadAll(r.Body)
|
body, err := io.ReadAll(r.Body)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
@ -112,6 +121,7 @@ func TestAuthConfig_Start(t *testing.T) {
|
||||||
Username string
|
Username string
|
||||||
Password string
|
Password string
|
||||||
Body string
|
Body string
|
||||||
|
Headers map[string]string
|
||||||
}
|
}
|
||||||
type args struct {
|
type args struct {
|
||||||
renewal time.Duration
|
renewal time.Duration
|
||||||
|
|
@ -138,6 +148,20 @@ func TestAuthConfig_Start(t *testing.T) {
|
||||||
firstHTTPResponse: http.StatusOK,
|
firstHTTPResponse: http.StatusOK,
|
||||||
lastHTTPResponse: http.StatusOK,
|
lastHTTPResponse: http.StatusOK,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "success no creds, no body, default method, header set",
|
||||||
|
args: args{
|
||||||
|
renewal: renewal,
|
||||||
|
endpoint: authEndpointWithHeader,
|
||||||
|
},
|
||||||
|
fields: fields{
|
||||||
|
Headers: map[string]string{reqHeaderKey: reqHeaderVal},
|
||||||
|
},
|
||||||
|
firstAuthCount: 1,
|
||||||
|
lastAuthCount: 3,
|
||||||
|
firstHTTPResponse: http.StatusOK,
|
||||||
|
lastHTTPResponse: http.StatusOK,
|
||||||
|
},
|
||||||
{
|
{
|
||||||
name: "success with creds, no body",
|
name: "success with creds, no body",
|
||||||
fields: fields{
|
fields: fields{
|
||||||
|
|
@ -213,6 +237,7 @@ func TestAuthConfig_Start(t *testing.T) {
|
||||||
Username: tt.fields.Username,
|
Username: tt.fields.Username,
|
||||||
Password: tt.fields.Password,
|
Password: tt.fields.Password,
|
||||||
Body: tt.fields.Body,
|
Body: tt.fields.Body,
|
||||||
|
Headers: tt.fields.Headers,
|
||||||
Renewal: config.Duration(tt.args.renewal),
|
Renewal: config.Duration(tt.args.renewal),
|
||||||
}
|
}
|
||||||
if err := c.initializeClient(srv.Client()); tt.wantErr != nil {
|
if err := c.initializeClient(srv.Client()); tt.wantErr != nil {
|
||||||
|
|
|
||||||
|
|
@ -54,6 +54,7 @@ The HTTP input plugin collects metrics from one or more HTTP(S) endpoints. The
|
||||||
# cookie_auth_method = "POST"
|
# cookie_auth_method = "POST"
|
||||||
# cookie_auth_username = "username"
|
# cookie_auth_username = "username"
|
||||||
# cookie_auth_password = "pa$$word"
|
# cookie_auth_password = "pa$$word"
|
||||||
|
# cookie_auth_headers = '{"Content-Type": "application/json", "X-MY-HEADER":"hello"}'
|
||||||
# cookie_auth_body = '{"username": "user", "password": "pa$$word", "authenticate": "me"}'
|
# 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 not set or set to "0" will auth once and never renew the cookie
|
||||||
# cookie_auth_renewal = "5m"
|
# cookie_auth_renewal = "5m"
|
||||||
|
|
|
||||||
|
|
@ -90,6 +90,7 @@ var sampleConfig = `
|
||||||
# cookie_auth_method = "POST"
|
# cookie_auth_method = "POST"
|
||||||
# cookie_auth_username = "username"
|
# cookie_auth_username = "username"
|
||||||
# cookie_auth_password = "pa$$word"
|
# cookie_auth_password = "pa$$word"
|
||||||
|
# cookie_auth_headers = '{"Content-Type": "application/json", "X-MY-HEADER":"hello"}'
|
||||||
# cookie_auth_body = '{"username": "user", "password": "pa$$word", "authenticate": "me"}'
|
# 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 not set or set to "0" will auth once and never renew the cookie
|
||||||
# cookie_auth_renewal = "5m"
|
# cookie_auth_renewal = "5m"
|
||||||
|
|
|
||||||
|
|
@ -40,6 +40,7 @@ batch format by default.
|
||||||
# cookie_auth_method = "POST"
|
# cookie_auth_method = "POST"
|
||||||
# cookie_auth_username = "username"
|
# cookie_auth_username = "username"
|
||||||
# cookie_auth_password = "pa$$word"
|
# cookie_auth_password = "pa$$word"
|
||||||
|
# cookie_auth_headers = '{"Content-Type": "application/json", "X-MY-HEADER":"hello"}'
|
||||||
# cookie_auth_body = '{"username": "user", "password": "pa$$word", "authenticate": "me"}'
|
# 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 not set or set to "0" will auth once and never renew the cookie
|
||||||
# cookie_auth_renewal = "5m"
|
# cookie_auth_renewal = "5m"
|
||||||
|
|
|
||||||
|
|
@ -58,6 +58,7 @@ var sampleConfig = `
|
||||||
# cookie_auth_method = "POST"
|
# cookie_auth_method = "POST"
|
||||||
# cookie_auth_username = "username"
|
# cookie_auth_username = "username"
|
||||||
# cookie_auth_password = "pa$$word"
|
# cookie_auth_password = "pa$$word"
|
||||||
|
# cookie_auth_headers = '{"Content-Type": "application/json", "X-MY-HEADER":"hello"}'
|
||||||
# cookie_auth_body = '{"username": "user", "password": "pa$$word", "authenticate": "me"}'
|
# 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 not set or set to "0" will auth once and never renew the cookie
|
||||||
# cookie_auth_renewal = "5m"
|
# cookie_auth_renewal = "5m"
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue