Files
karma/internal/uri/http.go
Łukasz Mierzwa fd0eb46adf Sanitize Alertmanager URI before putting it in /alerts.json reponse
Alertmanager URI might contain basic auth username & password, we should replace password with 'xxx' in logs and and error messages.
Go will still print it in HTTP request errors, but that will be fixed in the next Go release - https://go-review.googlesource.com/c/go/+/102855
Fixes #259
2018-04-27 09:36:12 -07:00

47 lines
976 B
Go

package uri
import (
"compress/gzip"
"fmt"
"io"
"net/http"
log "github.com/sirupsen/logrus"
)
// HTTPURIReader can read data from http:// and https:// URIs
type HTTPURIReader struct {
client http.Client
}
func (r *HTTPURIReader) Read(uri string) (io.ReadCloser, error) {
log.Infof("GET %s timeout=%s", SanitizeURI(uri), r.client.Timeout)
request, err := http.NewRequest("GET", uri, nil)
if err != nil {
return nil, err
}
request.Header.Add("Accept-Encoding", "gzip")
resp, err := r.client.Do(request)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("Request to %s failed with %s", SanitizeURI(uri), resp.Status)
}
var reader io.ReadCloser
switch resp.Header.Get("Content-Encoding") {
case "gzip":
reader, err = gzip.NewReader(resp.Body)
if err != nil {
return nil, fmt.Errorf("Failed to decode gzipped content: %s", err.Error())
}
default:
reader = resp.Body
}
return reader, nil
}