add optional auth and tenant

This commit is contained in:
davidnewhall2 2020-06-24 13:52:44 -07:00
parent c00e3111e3
commit 6a0cc0d2a6
3 changed files with 35 additions and 14 deletions

View File

@ -20,13 +20,13 @@ var (
errStatusCode = fmt.Errorf("unexpected HTTP status code")
)
type client struct {
type Client struct {
*Config
*http.Client
}
func (l *Loki) httpClient() *client {
return &client{
func (l *Loki) httpClient() *Client {
return &Client{
Config: l.Config,
Client: &http.Client{
Timeout: l.Timeout.Duration,
@ -40,7 +40,7 @@ func (l *Loki) httpClient() *client {
}
// Send marshals and posts a batch of log messages.
func (c *client) Send(logs Logs) error {
func (c *Client) Send(logs Logs) error {
msg, err := json.Marshal(logs)
if err != nil {
return err
@ -48,9 +48,13 @@ func (c *client) Send(logs Logs) error {
u := strings.TrimSuffix(c.URL, lokiPushPath) + lokiPushPath
code, body, err := c.PostReq(u, "application/json", msg)
req, err := c.NewRequest(u, "POST", "application/json", msg)
if err != nil {
return err
}
if code, body, err := c.Do(req); err != nil {
return err
} else if code != http.StatusNoContent {
m := fmt.Sprintf("%s (%d/%s) %s, msg: %s", u, code, http.StatusText(code),
strings.TrimSpace(strings.ReplaceAll(string(body), "\n", " ")), msg)
@ -61,17 +65,31 @@ func (c *client) Send(logs Logs) error {
return nil
}
// PostReq posts data to a url with a custom content type.
// Returns the status code, body and/or an error.
func (c *client) PostReq(url, cType string, msg []byte) (int, []byte, error) {
req, err := http.NewRequest("POST", url, bytes.NewBuffer(msg))
// NewRequest creates the http request based on input data.
func (c *Client) NewRequest(url, method, cType string, msg []byte) (*http.Request, error) {
req, err := http.NewRequest(method, url, bytes.NewBuffer(msg))
if err != nil {
return 0, nil, err
return nil, err
}
req.Header.Set("Content-Type", cType)
if cType != "" {
req.Header.Set("Content-Type", cType)
}
resp, err := c.Do(req)
if c.Config.Username != "" || c.Config.Password != "" {
req.SetBasicAuth(c.Config.Username, c.Config.Password)
}
if c.Config.TenantID != "" {
req.Header.Set("X-Scope-OrgID", c.Config.TenantID)
}
return req, nil
}
// Do makes an http request and returns the status code, body and/or an error.
func (c *Client) Do(req *http.Request) (int, []byte, error) {
resp, err := c.Client.Do(req)
if err != nil {
return 0, nil, err
}

View File

@ -28,6 +28,9 @@ type Config struct {
Disable bool `json:"disable" toml:"disable" xml:"disable" yaml:"disable"`
VerifySSL bool `json:"verify_ssl" toml:"verify_ssl" xml:"verify_ssl" yaml:"verify_ssl"`
URL string `json:"url" toml:"url" xml:"url" yaml:"url"`
Username string `json:"user" toml:"user" xml:"user" yaml:"user"`
Password string `json:"pass" toml:"pass" xml:"pass" yaml:"pass"`
TenantID string `json:"tenant_id" toml:"tenant_id" xml:"tenant_id" yaml:"tenant_id"`
Interval cnfg.Duration `json:"interval" toml:"interval" xml:"interval" yaml:"interval"`
Timeout cnfg.Duration `json:"timeout" toml:"timeout" xml:"timeout" yaml:"timeout"`
}
@ -36,7 +39,7 @@ type Config struct {
type Loki struct {
poller.Collect
*Config `json:"loki" toml:"loki" xml:"loki" yaml:"loki"`
client *client
client *Client
last time.Time
}

View File

@ -26,7 +26,7 @@ type Report struct {
Eve int // Total count of Events.
IDS int // Total count of IDS/IPS Events.
Last time.Time
Loki *client
Loki *Client
Events *poller.Events
Logs
poller.Logger