2016-12-22 13:40:14 +01:00
|
|
|
package beater
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
2017-12-20 16:35:22 +01:00
|
|
|
"crypto/x509"
|
2016-12-30 13:32:56 +01:00
|
|
|
"errors"
|
2017-12-20 16:35:22 +01:00
|
|
|
"io/ioutil"
|
2016-12-22 13:40:14 +01:00
|
|
|
"net/http"
|
2016-12-30 16:00:50 +01:00
|
|
|
"net/url"
|
2017-12-20 12:48:06 +01:00
|
|
|
"time"
|
2016-12-22 13:40:14 +01:00
|
|
|
|
|
|
|
"github.com/elastic/beats/libbeat/logp"
|
|
|
|
)
|
|
|
|
|
2016-12-30 16:00:50 +01:00
|
|
|
func requestURL(bt *Icingabeat, method string, URL *url.URL) (*http.Response, error) {
|
2017-12-20 16:35:22 +01:00
|
|
|
|
|
|
|
var skipSslVerify bool
|
|
|
|
certPool := x509.NewCertPool()
|
|
|
|
|
|
|
|
if bt.config.SSL.Verify {
|
|
|
|
skipSslVerify = false
|
|
|
|
|
|
|
|
for _, ca := range bt.config.SSL.CertificateAuthorities {
|
|
|
|
cert, err := ioutil.ReadFile(ca)
|
|
|
|
if err != nil {
|
|
|
|
logp.Warn("Could not load certificate: %v", err)
|
|
|
|
}
|
|
|
|
certPool.AppendCertsFromPEM(cert)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
skipSslVerify = true
|
|
|
|
}
|
|
|
|
|
|
|
|
tlsConfig := &tls.Config{
|
|
|
|
InsecureSkipVerify: skipSslVerify,
|
|
|
|
RootCAs: certPool,
|
|
|
|
}
|
|
|
|
|
2016-12-22 13:40:14 +01:00
|
|
|
transport := &http.Transport{
|
2017-12-20 16:35:22 +01:00
|
|
|
TLSClientConfig: tlsConfig,
|
2017-12-20 12:48:06 +01:00
|
|
|
MaxIdleConns: 10,
|
|
|
|
IdleConnTimeout: 30 * time.Second,
|
2016-12-22 13:40:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
client := &http.Client{
|
|
|
|
Transport: transport,
|
|
|
|
}
|
|
|
|
|
2017-03-15 15:15:59 +01:00
|
|
|
logp.Debug("icingabeat", "Requested URL: %v", URL.String())
|
2016-12-30 16:00:50 +01:00
|
|
|
|
|
|
|
request, err := http.NewRequest(method, URL.String(), nil)
|
2016-12-22 13:40:14 +01:00
|
|
|
|
|
|
|
if err != nil {
|
2016-12-30 13:32:56 +01:00
|
|
|
logp.Info("Request: %v", err)
|
2016-12-22 13:40:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
request.Header.Add("Accept", "application/json")
|
|
|
|
request.SetBasicAuth(bt.config.User, bt.config.Password)
|
|
|
|
response, err := client.Do(request)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-12-30 13:32:56 +01:00
|
|
|
switch response.StatusCode {
|
|
|
|
case 401:
|
|
|
|
err = errors.New("Authentication failed for user " + bt.config.User)
|
2017-12-20 12:48:06 +01:00
|
|
|
defer response.Body.Close()
|
2018-04-10 15:46:13 +02:00
|
|
|
case 404:
|
|
|
|
err = errors.New("404 Not Found. Missing permissions may be a reason for this.")
|
|
|
|
defer response.Body.Close()
|
2016-12-30 13:32:56 +01:00
|
|
|
}
|
|
|
|
|
2016-12-22 13:40:14 +01:00
|
|
|
return response, err
|
|
|
|
}
|