diff --git a/cmd/config-current.go b/cmd/config-current.go index d469b7ff8..aeedd7f98 100644 --- a/cmd/config-current.go +++ b/cmd/config-current.go @@ -422,14 +422,28 @@ func lookupConfigs(s config.Config) { for _, l := range loggerCfg.HTTP { if l.Enabled { // Enable http logging - logger.AddTarget(http.New(l.Endpoint, loggerUserAgent, string(logger.All), NewGatewayHTTPTransport())) + logger.AddTarget( + http.New(http.WithEndpoint(l.Endpoint), + http.WithAuthToken(l.AuthToken), + http.WithUserAgent(loggerUserAgent), + http.WithLogKind(string(logger.All)), + http.WithTransport(NewGatewayHTTPTransport()), + ), + ) } } for _, l := range loggerCfg.Audit { if l.Enabled { // Enable http audit logging - logger.AddAuditTarget(http.New(l.Endpoint, loggerUserAgent, string(logger.All), NewGatewayHTTPTransport())) + logger.AddAuditTarget( + http.New(http.WithEndpoint(l.Endpoint), + http.WithAuthToken(l.AuthToken), + http.WithUserAgent(loggerUserAgent), + http.WithLogKind(string(logger.All)), + http.WithTransport(NewGatewayHTTPTransport()), + ), + ) } } diff --git a/cmd/logger/target/http/http.go b/cmd/logger/target/http/http.go index 7ba8b52eb..b777a01f8 100644 --- a/cmd/logger/target/http/http.go +++ b/cmd/logger/target/http/http.go @@ -37,7 +37,9 @@ type Target struct { // HTTP(s) endpoint endpoint string - // User-Agent to be set on each log request sent to the `endpoint` + // Authorization token for `endpoint` + authToken string + // User-Agent to be set on each log to `endpoint` userAgent string logKind string client http.Client @@ -53,7 +55,7 @@ func (h *Target) startHTTPLogger() { continue } - req, err := http.NewRequest(http.MethodPost, h.endpoint, bytes.NewBuffer(logJSON)) + req, err := http.NewRequest(http.MethodPost, h.endpoint, bytes.NewReader(logJSON)) if err != nil { continue } @@ -63,6 +65,10 @@ func (h *Target) startHTTPLogger() { // version to the configured log endpoint req.Header.Set("User-Agent", h.userAgent) + if h.authToken != "" { + req.Header.Set("Authorization", h.authToken) + } + resp, err := h.client.Do(req) if err != nil { h.client.CloseIdleConnections() @@ -75,21 +81,62 @@ func (h *Target) startHTTPLogger() { }() } +// Option is a function type that accepts a pointer Target +type Option func(*Target) + +// WithEndpoint adds a new endpoint +func WithEndpoint(endpoint string) Option { + return func(t *Target) { + t.endpoint = endpoint + } +} + +// WithLogKind adds a log type for this target +func WithLogKind(logKind string) Option { + return func(t *Target) { + t.logKind = strings.ToUpper(logKind) + } +} + +// WithUserAgent adds a custom user-agent sent to the target. +func WithUserAgent(userAgent string) Option { + return func(t *Target) { + t.userAgent = userAgent + } +} + +// WithAuthToken adds a new authorization header to be sent to target. +func WithAuthToken(authToken string) Option { + return func(t *Target) { + t.authToken = authToken + } +} + +// WithTransport adds a custom transport with custom timeouts and tuning. +func WithTransport(transport *http.Transport) Option { + return func(t *Target) { + t.client = http.Client{ + Transport: transport, + } + } +} + // New initializes a new logger target which // sends log over http to the specified endpoint -func New(endpoint, userAgent, logKind string, transport *http.Transport) *Target { - h := Target{ - endpoint: endpoint, - userAgent: userAgent, - logKind: strings.ToUpper(logKind), - client: http.Client{ - Transport: transport, - }, +func New(opts ...Option) *Target { + h := &Target{ logCh: make(chan interface{}, 10000), } + // Loop through each option + for _, opt := range opts { + // Call the option giving the instantiated + // *Target as the argument + opt(h) + } + h.startHTTPLogger() - return &h + return h } // Send log message 'e' to http target. @@ -97,6 +144,7 @@ func (h *Target) Send(entry interface{}, errKind string) error { if h.logKind != errKind && h.logKind != "ALL" { return nil } + select { case h.logCh <- entry: default: