-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.go
208 lines (195 loc) · 5.98 KB
/
main.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package main
import (
"bytes"
"crypto/tls"
"crypto/x509"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"github.com/sensu/sensu-go/types"
)
type Authentication struct {
AccessToken string `json:"access_token"`
RefreshToken string `json:"refresh_token"`
Expiration int64 `json:"expires_at"`
}
type RemediationConfig struct {
Request string `json:"request"`
Occurrences []int `json:"occurrences"`
Severities []int `json:"severities"`
Subscriptions []string `json:"subscriptions"`
}
type RequestPayload struct {
Check string `json:"check"`
Subscriptions []string `json:"subscriptions"`
}
var (
sensuApiUrl string = getenv("SENSU_API_URL", "http://127.0.0.1:8080")
sensuApiCertFile string = getenv("SENSU_API_CERT_FILE", "")
sensuApiUser string = getenv("SENSU_API_USER", "admin")
sensuApiPass string = getenv("SENSU_API_PASS", "P@ssw0rd!")
sensuApiToken string
)
func getenv(key string, fallback string) string {
value := os.Getenv(key)
if len(value) == 0 {
return fallback
}
return value
}
func contains(s []int, i int) bool {
for _, a := range s {
if a == i {
return true
}
}
return false
}
func authenticate(httpClient *http.Client) string {
var authentication Authentication
req, err := http.NewRequest(
"GET",
fmt.Sprintf("%s/auth", sensuApiUrl),
nil,
)
if err != nil {
log.Fatal("ERROR: ", err)
}
req.SetBasicAuth(sensuApiUser, sensuApiPass)
resp, err := httpClient.Do(req)
if err != nil {
log.Fatalf("ERROR: %s", err)
} else if resp.StatusCode == 401 {
log.Fatalf("ERROR: %v %s (please check your access credentials)", resp.StatusCode, http.StatusText(resp.StatusCode))
} else if resp.StatusCode >= 300 {
log.Fatalf("ERROR: %v %s", resp.StatusCode, http.StatusText(resp.StatusCode))
}
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal("ERROR: ", err)
}
err = json.NewDecoder(bytes.NewReader(b)).Decode(&authentication)
if err != nil {
log.Fatal("ERROR: ", err)
}
return authentication.AccessToken
}
func LoadCACerts(path string) (*x509.CertPool, error) {
rootCAs, err := x509.SystemCertPool()
if err != nil {
return nil, fmt.Errorf("Error loading system cert pool: %s", err)
}
if rootCAs == nil {
rootCAs = x509.NewCertPool()
}
if path != "" {
certs, err := ioutil.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("Error reading CA file (%s): %s", path, err)
} else {
rootCAs.AppendCertsFromPEM(certs)
}
}
return rootCAs, nil
}
func initHttpClient() *http.Client {
certs, err := LoadCACerts(sensuApiCertFile)
if err != nil {
log.Fatalf("ERROR: %s\n", err)
}
tlsConfig := &tls.Config{
RootCAs: certs,
}
tr := &http.Transport{
TLSClientConfig: tlsConfig,
}
client := &http.Client{
Transport: tr,
}
return client
}
func main() {
var stdin *os.File
var event types.Event
var actions []RemediationConfig
var annotationConfigKey string = "io.sensu.remediation.config.actions"
var httpClient *http.Client = initHttpClient()
stdin = os.Stdin
err := json.NewDecoder(stdin).Decode(&event)
if err != nil {
log.Fatal("ERROR: ", err)
}
if event.Check.Annotations[annotationConfigKey] != "" {
err = json.Unmarshal([]byte(event.Check.Annotations[annotationConfigKey]), &actions)
if err != nil {
log.Fatal("ERROR: ", err)
}
for _, action := range actions {
// Only perform the action if the event severity matches
if !contains(action.Severities, int(event.Check.Status)) {
// Mismatched severity
log.Printf("Remediation action \"%s\" configured to trigger on severities: %v (nothing to do for serverity %v).", action.Request, action.Severities, event.Check.Status)
} else {
// Only perform the action if the event occurrence matches
if !contains(action.Occurrences, int(event.Check.Occurrences)) {
// Mismatched occurrences
log.Printf("Remediation action \"%s\" configured to trigger on occurrence(s): %v (nothing to do on occurrence #%v).", action.Request, action.Occurrences, event.Check.Occurrences)
} else {
// Perform the remediation action!
if len(action.Subscriptions) == 0 {
action.Subscriptions = append(action.Subscriptions, fmt.Sprintf("entity:%s", event.Entity.Name))
}
log.Printf("Requesting the \"%s\" remediation action on the \"%s\" subscription(s).", action.Request, action.Subscriptions)
data := RequestPayload{
Check: action.Request,
Subscriptions: action.Subscriptions,
}
postBody, err := json.Marshal(data)
if err != nil {
log.Fatal("ERROR: ", err)
}
body := bytes.NewReader(postBody)
req, err := http.NewRequest(
"POST",
fmt.Sprintf("%s/api/core/v2/namespaces/%s/checks/%s/execute",
sensuApiUrl,
event.Entity.Namespace,
action.Request,
),
body,
)
if err != nil {
log.Fatal("ERROR: ", err)
}
sensuApiToken = authenticate(httpClient)
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", sensuApiToken))
req.Header.Set("Content-Type", "application/json")
resp, err := httpClient.Do(req)
if err != nil {
log.Fatalf("ERROR: %s\n", err)
} else if resp.StatusCode == 404 {
log.Fatalf("ERROR: %v %s (%s); no check named \"%s\" found in namespace \"%s\".\n", resp.StatusCode, http.StatusText(resp.StatusCode), req.URL, action.Request, event.Entity.Namespace)
} else if resp.StatusCode >= 300 {
log.Fatalf("ERROR: %v %s", resp.StatusCode, http.StatusText(resp.StatusCode))
}
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Printf("Error reading response body")
log.Fatalf("ERROR: %s\n", err)
}
fmt.Println(resp.StatusCode)
fmt.Println(string(b))
}
}
}
} else {
// No configured actions
log.Printf("No remediation actions configured for check \"%s\"; nothing to do.", event.Check.Name)
log.Printf("To enable remediation actions, provide remediation configuration using the \"%s\" check annotation.", annotationConfigKey)
}
}