telegraf/plugins/common/cookie/cookie.go

129 lines
2.7 KiB
Go
Raw Normal View History

2021-07-14 05:58:49 +08:00
package cookie
import (
"context"
2021-07-14 05:58:49 +08:00
"fmt"
"io"
"net/http"
"net/http/cookiejar"
"strings"
"sync"
2021-07-14 05:58:49 +08:00
"time"
2021-08-18 05:22:14 +08:00
clockutil "github.com/benbjohnson/clock"
2021-07-14 05:58:49 +08:00
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/config"
)
type CookieAuthConfig struct {
URL string `toml:"cookie_auth_url"`
Method string `toml:"cookie_auth_method"`
Headers map[string]string `toml:"cookie_auth_headers"`
2021-07-14 05:58:49 +08:00
// HTTP Basic Auth Credentials
Username string `toml:"cookie_auth_username"`
Password string `toml:"cookie_auth_password"`
Body string `toml:"cookie_auth_body"`
Renewal config.Duration `toml:"cookie_auth_renewal"`
client *http.Client
wg sync.WaitGroup
2021-07-14 05:58:49 +08:00
}
2021-08-18 05:22:14 +08:00
func (c *CookieAuthConfig) Start(client *http.Client, log telegraf.Logger, clock clockutil.Clock) (err error) {
if err = c.initializeClient(client); err != nil {
return err
}
// continual auth renewal if set
if c.Renewal > 0 {
ticker := clock.Ticker(time.Duration(c.Renewal))
// this context is used in the tests only, it is to cancel the goroutine
go c.authRenewal(context.Background(), ticker, log)
}
return nil
}
func (c *CookieAuthConfig) initializeClient(client *http.Client) (err error) {
2021-07-14 05:58:49 +08:00
c.client = client
if c.Method == "" {
c.Method = http.MethodPost
}
return c.auth()
}
2021-07-14 05:58:49 +08:00
func (c *CookieAuthConfig) authRenewal(ctx context.Context, ticker *clockutil.Ticker, log telegraf.Logger) {
for {
select {
case <-ctx.Done():
c.wg.Done()
return
case <-ticker.C:
if err := c.auth(); err != nil && log != nil {
log.Errorf("renewal failed for %q: %v", c.URL, err)
2021-07-14 05:58:49 +08:00
}
}
2021-07-14 05:58:49 +08:00
}
}
func (c *CookieAuthConfig) auth() error {
var err error
// everytime we auth we clear out the cookie jar to ensure that the cookie
// is not used as a part of re-authing. The only way to empty or reset is
// to create a new cookie jar.
c.client.Jar, err = cookiejar.New(nil)
if err != nil {
return err
}
var body io.Reader
2021-07-14 05:58:49 +08:00
if c.Body != "" {
body = strings.NewReader(c.Body)
2021-07-14 05:58:49 +08:00
}
req, err := http.NewRequest(c.Method, c.URL, body)
if err != nil {
return err
}
if c.Username != "" {
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)
}
}
2021-07-14 05:58:49 +08:00
resp, err := c.client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
respBody, err := io.ReadAll(resp.Body)
if err != nil {
2021-07-14 05:58:49 +08:00
return err
}
// check either 200 or 201 as some devices may return either
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated {
return fmt.Errorf("cookie auth renewal received status code: %v (%v) [%v]",
2021-07-14 05:58:49 +08:00
resp.StatusCode,
http.StatusText(resp.StatusCode),
string(respBody),
2021-07-14 05:58:49 +08:00
)
}
return nil
}