Skip to content
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

feat: support headers for http plugin with cookie auth #10404

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions plugins/common/cookie/cookie.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ type CookieAuthConfig struct {
URL string `toml:"cookie_auth_url"`
Method string `toml:"cookie_auth_method"`

Headers map[string]string `toml:"cookie_auth_headers"`

// HTTP Basic Auth Credentials
Username string `toml:"cookie_auth_username"`
Password string `toml:"cookie_auth_password"`
Expand Down Expand Up @@ -90,6 +92,14 @@ func (c *CookieAuthConfig) auth() error {
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)
if err != nil {
return err
Expand Down
31 changes: 28 additions & 3 deletions plugins/common/cookie/cookie_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,17 @@ import (
)

const (
reqUser = "testUser"
reqPasswd = "testPassword"
reqBody = "a body"
reqUser = "testUser"
reqPasswd = "testPassword"
reqBody = "a body"
reqHeaderKey = "hello"
reqHeaderVal = "world"

authEndpointNoCreds = "/auth"
authEndpointWithBasicAuth = "/authWithCreds"
authEndpointWithBasicAuthOnlyUsername = "/authWithCredsUser"
authEndpointWithBody = "/authWithBody"
authEndpointWithHeader = "/authWithHeader"
)

var fakeCookie = &http.Cookie{
Expand All @@ -49,6 +52,12 @@ func newFakeServer(t *testing.T) fakeServer {
switch r.URL.Path {
case authEndpointNoCreds:
authed()
case authEndpointWithHeader:
if !cmp.Equal(r.Header.Get(reqHeaderKey), reqHeaderVal) {
w.WriteHeader(http.StatusUnauthorized)
return
}
authed()
case authEndpointWithBody:
body, err := io.ReadAll(r.Body)
require.NoError(t, err)
Expand Down Expand Up @@ -112,6 +121,7 @@ func TestAuthConfig_Start(t *testing.T) {
Username string
Password string
Body string
Headers map[string]string
}
type args struct {
renewal time.Duration
Expand All @@ -138,6 +148,20 @@ func TestAuthConfig_Start(t *testing.T) {
firstHTTPResponse: 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",
fields: fields{
Expand Down Expand Up @@ -213,6 +237,7 @@ func TestAuthConfig_Start(t *testing.T) {
Username: tt.fields.Username,
Password: tt.fields.Password,
Body: tt.fields.Body,
Headers: tt.fields.Headers,
Renewal: config.Duration(tt.args.renewal),
}
if err := c.initializeClient(srv.Client()); tt.wantErr != nil {
Expand Down
1 change: 1 addition & 0 deletions plugins/inputs/http/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ The HTTP input plugin collects metrics from one or more HTTP(S) endpoints. The
# cookie_auth_method = "POST"
# cookie_auth_username = "username"
# 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_renewal not set or set to "0" will auth once and never renew the cookie
# cookie_auth_renewal = "5m"
Expand Down
1 change: 1 addition & 0 deletions plugins/inputs/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ var sampleConfig = `
# cookie_auth_method = "POST"
# cookie_auth_username = "username"
# 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_renewal not set or set to "0" will auth once and never renew the cookie
# cookie_auth_renewal = "5m"
Expand Down
1 change: 1 addition & 0 deletions plugins/outputs/http/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ batch format by default.
# cookie_auth_method = "POST"
# cookie_auth_username = "username"
# 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_renewal not set or set to "0" will auth once and never renew the cookie
# cookie_auth_renewal = "5m"
Expand Down
1 change: 1 addition & 0 deletions plugins/outputs/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ var sampleConfig = `
# cookie_auth_method = "POST"
# cookie_auth_username = "username"
# 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_renewal not set or set to "0" will auth once and never renew the cookie
# cookie_auth_renewal = "5m"
Expand Down