forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
splunk.go
56 lines (47 loc) · 1.26 KB
/
splunk.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package utils
import (
"bytes"
"crypto/tls"
"fmt"
"net/http"
"net/url"
"path"
"github.com/pkg/errors"
"github.com/rancher/types/apis/management.cattle.io/v3"
"github.com/rancher/types/config/dialer"
)
var (
httpTestData = []byte(`{"event": "` + testMessage + `", "sourcetype": "rancher"}`)
)
type splunkTestWrap struct {
*v3.SplunkConfig
}
func (w *splunkTestWrap) TestReachable(dial dialer.Dialer, includeSendTestLog bool) error {
url, err := url.Parse(w.Endpoint)
if err != nil {
return errors.Wrapf(err, "couldn't parse url %s", w.Endpoint)
}
isTLS := url.Scheme == "https"
var tlsConfig *tls.Config
if isTLS {
tlsConfig, err = buildTLSConfig(w.Certificate, w.ClientCert, w.ClientKey, w.ClientKeyPass, "", url.Hostname(), w.SSLVerify)
if err != nil {
return err
}
}
if !includeSendTestLog {
conn, err := newTCPConn(dial, url.Host, tlsConfig, true)
if err != nil {
return err
}
conn.Close()
return nil
}
url.Path = path.Join(url.Path, "/services/collector")
req, err := http.NewRequest(http.MethodPost, url.String(), bytes.NewReader(httpTestData))
if err != nil {
return errors.Wrap(err, "create request failed")
}
req.Header.Set("Authorization", fmt.Sprintf("Splunk %s", w.Token))
return testReachableHTTP(dial, req, tlsConfig)
}