fix: use specified authToken for audit/logger HTTP targets (#9249)

We were not using the auth token specified
even when config supports it.
master
Harshavardhana 5 years ago committed by GitHub
parent 8dd63a462f
commit d49f2ec19c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 18
      cmd/config-current.go
  2. 70
      cmd/logger/target/http/http.go

@ -422,14 +422,28 @@ func lookupConfigs(s config.Config) {
for _, l := range loggerCfg.HTTP { for _, l := range loggerCfg.HTTP {
if l.Enabled { if l.Enabled {
// Enable http logging // 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 { for _, l := range loggerCfg.Audit {
if l.Enabled { if l.Enabled {
// Enable http audit logging // 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()),
),
)
} }
} }

@ -37,7 +37,9 @@ type Target struct {
// HTTP(s) endpoint // HTTP(s) endpoint
endpoint string 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 userAgent string
logKind string logKind string
client http.Client client http.Client
@ -53,7 +55,7 @@ func (h *Target) startHTTPLogger() {
continue 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 { if err != nil {
continue continue
} }
@ -63,6 +65,10 @@ func (h *Target) startHTTPLogger() {
// version to the configured log endpoint // version to the configured log endpoint
req.Header.Set("User-Agent", h.userAgent) req.Header.Set("User-Agent", h.userAgent)
if h.authToken != "" {
req.Header.Set("Authorization", h.authToken)
}
resp, err := h.client.Do(req) resp, err := h.client.Do(req)
if err != nil { if err != nil {
h.client.CloseIdleConnections() 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 // New initializes a new logger target which
// sends log over http to the specified endpoint // sends log over http to the specified endpoint
func New(endpoint, userAgent, logKind string, transport *http.Transport) *Target { func New(opts ...Option) *Target {
h := Target{ h := &Target{
endpoint: endpoint,
userAgent: userAgent,
logKind: strings.ToUpper(logKind),
client: http.Client{
Transport: transport,
},
logCh: make(chan interface{}, 10000), 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() h.startHTTPLogger()
return &h return h
} }
// Send log message 'e' to http target. // 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" { if h.logKind != errKind && h.logKind != "ALL" {
return nil return nil
} }
select { select {
case h.logCh <- entry: case h.logCh <- entry:
default: default:

Loading…
Cancel
Save