generated from sensu/handler-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
320 lines (289 loc) · 8.72 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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
package main
import (
"context"
"crypto/tls"
"crypto/x509"
"encoding/json"
"encoding/pem"
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"path"
"strings"
corev2 "github.com/sensu/core/v2"
"github.com/sensu/sensu-plugin-sdk/httpclient"
"github.com/sensu/sensu-plugin-sdk/sensu"
)
// Handler represents the sensu-puppet-handler plugin
type Handler struct {
sensu.PluginConfig
endpoint string
puppetCert string
puppetKey string
puppetCACert string
puppetInsecureSkipVerify bool
puppetNodeName string
sensuAPIURL string
sensuAPIKey string
sensuCACert string
}
const (
defaultAPIPath = "pdb/query/v4/nodes"
)
var (
handler = Handler{
PluginConfig: sensu.PluginConfig{
Name: "sensu-puppet-handler",
Short: "Deregister Sensu entities without an associated Puppet node",
Keyspace: "sensu.io/plugins/sensu-puppet-handler/config",
},
}
options = []sensu.ConfigOption{
&sensu.PluginConfigOption[string]{
Path: "endpoint",
Env: "PUPPET_ENDPOINT",
Argument: "endpoint",
Shorthand: "e",
Usage: "the PuppetDB API endpoint (URL). If an API path is not specified, /pdb/query/v4/nodes/ will be used",
Value: &handler.endpoint,
},
&sensu.PluginConfigOption[string]{
Path: "cert",
Env: "PUPPET_CERT",
Argument: "cert",
Usage: "path to the SSL certificate PEM file signed by your site's Puppet CA",
Value: &handler.puppetCert,
},
&sensu.PluginConfigOption[string]{
Path: "key",
Env: "PUPPET_KEY",
Argument: "key",
Usage: "path to the private key PEM file for that certificate",
Value: &handler.puppetKey,
},
&sensu.PluginConfigOption[string]{
Path: "ca-cert",
Env: "PUPPET_CA_CERT",
Argument: "ca-cert",
Usage: "path to the site's Puppet CA certificate PEM file",
Value: &handler.puppetCACert,
},
&sensu.PluginConfigOption[bool]{
Path: "insecure-skip-tls-verify",
Env: "PUPPET_INSECURE_SKIP_TLS_VERIFY",
Argument: "insecure-skip-tls-verify",
Usage: "skip TLS verification for Puppet and sensu-backend",
Value: &handler.puppetInsecureSkipVerify,
},
&sensu.PluginConfigOption[string]{
Path: "node-name",
Env: "PUPPET_NODE_NAME",
Argument: "node-name",
Usage: "node name to use for the entity when querying PuppetDB",
Value: &handler.puppetNodeName,
},
&sensu.PluginConfigOption[string]{
Path: "sensu-api-url",
Env: "SENSU_API_URL",
Argument: "sensu-api-url",
Shorthand: "u",
Default: "http://localhost:8080",
Usage: "The Sensu API URL",
Value: &handler.sensuAPIURL,
},
&sensu.PluginConfigOption[string]{
Path: "sensu-api-key",
Env: "SENSU_API_KEY",
Argument: "sensu-api-key",
Shorthand: "a",
Usage: "The Sensu API key",
Value: &handler.sensuAPIKey,
},
&sensu.PluginConfigOption[string]{
Path: "sensu-ca-cert",
Env: "SENSU_CA_CERT",
Argument: "sensu-ca-cert",
Shorthand: "c",
Usage: "The Sensu Go CA Certificate",
Value: &handler.sensuCACert,
},
}
)
func main() {
handler := sensu.NewGoHandler(&handler.PluginConfig, options, validate, executeHandler)
handler.Execute()
}
func validate(event *corev2.Event) error {
// Make sure we have a valid event
if event.Check == nil || event.Entity == nil {
return errors.New("invalid event")
}
// Make sure all required options are provided
if len(handler.endpoint) == 0 {
return errors.New("the PuppetDB API endpoint is required")
}
if len(handler.puppetCert) == 0 {
return errors.New("the path to the SSL certificate is required")
}
if len(handler.puppetKey) == 0 {
return errors.New("the path to the private key is required")
}
if len(handler.sensuAPIURL) == 0 {
return errors.New("the Sensu API URL is required")
}
if len(handler.sensuAPIKey) == 0 {
return errors.New("the Sensu API key is required")
}
// Make sure the PuppetDB endpoint URL is valid
u, err := url.Parse(handler.endpoint)
if err != nil {
return fmt.Errorf("invalid PuppetDB API endpoint URL: %s", err)
}
if u.Scheme == "" {
u.Host = "https://"
}
if u.Host == "" {
return errors.New("invalid PuppetDB API endpoint URL")
}
if u.Path == "" || u.Path == "/" {
u.Path = path.Join(u.Path, defaultAPIPath)
}
handler.endpoint = u.String()
// Make sure the Sensu API URL is valid
u, err = url.Parse(handler.sensuAPIURL)
if err != nil {
return fmt.Errorf("invalid Sensu API URL: %s", err)
}
if u.Scheme == "" {
return errors.New("invalid Sensu API URL, missing scheme")
}
if u.Host == "" {
return errors.New("invalid Sensu API URL, missing host")
}
return nil
}
func executeHandler(event *corev2.Event) error {
if event.Check.Name != "keepalive" {
log.Print("received non-keepalive event, not checking for puppet node")
return nil
}
puppetClient, err := puppetHTTPClient()
if err != nil {
return err
}
exists, err := puppetNodeExists(puppetClient, event)
if err != nil {
return err
}
if exists {
return nil
}
return deregisterEntity(event)
}
// puppetHTTPClient configures an HTTP client for PuppetDB
func puppetHTTPClient() (*http.Client, error) {
// Load the public/private key pair
cert, err := tls.LoadX509KeyPair(handler.puppetCert, handler.puppetKey)
if err != nil {
return nil, fmt.Errorf("could not read the certificate/key: %s", err)
}
// Load the CA certificate
caCert, err := ioutil.ReadFile(handler.puppetCACert)
if err != nil {
return nil, fmt.Errorf("could not read the CA certificate: %s", err)
}
caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM(caCert)
// Setup the HTTPS client
tlsConfig := &tls.Config{
Certificates: []tls.Certificate{cert},
RootCAs: caCertPool,
InsecureSkipVerify: handler.puppetInsecureSkipVerify,
}
client := &http.Client{Transport: &http.Transport{TLSClientConfig: tlsConfig}}
return client, nil
}
// puppetNodeExists returns whether a given node exists in Puppet and any error
// encountered. The Puppet node name defaults to the entity name but can be
// overriden through the entity label "puppet_node_name"
func puppetNodeExists(client *http.Client, event *corev2.Event) (bool, error) {
// Determine the Puppet node name via the annotations and fallback to the
// entity name
name := handler.puppetNodeName
if handler.puppetNodeName == "" {
name = event.Entity.Name
}
// Get the puppet node
endpoint := strings.TrimRight(handler.endpoint, "/")
endpoint = fmt.Sprintf("%s/%s", endpoint, name)
resp, err := client.Get(endpoint)
if err != nil {
log.Printf("error getting puppet node: %s", err)
return false, err
}
defer resp.Body.Close()
// Determine if the node exists
if resp.StatusCode == http.StatusOK {
var info map[string]interface{}
if err := json.NewDecoder(resp.Body).Decode(&info); err != nil {
log.Printf("puppet node returned invalid response: %s", err)
return false, err
}
nodeInfo := make(map[string]interface{})
timeDeactivated := nodeInfo["deactivated"]
log.Printf("puppet node %q exists, checking if deactivated", name)
if timeDeactivated != nil {
return false, nil
}
return true, nil
} else if resp.StatusCode == http.StatusNotFound {
log.Printf("puppet node %q does not exist", name)
return false, nil
}
return false, fmt.Errorf("unexpected HTTP status %s while querying PuppetDB", http.StatusText(resp.StatusCode))
}
func deregisterEntity(event *corev2.Event) error {
// First authenticate against the Sensu API
config := httpclient.CoreClientConfig{
URL: handler.sensuAPIURL,
APIKey: handler.sensuAPIKey,
}
if handler.sensuCACert != "" {
pemCert, err := ioutil.ReadFile(handler.sensuCACert)
if err != nil {
return fmt.Errorf("unable to load sensu-ca-cert: %s", err)
}
block, _ := pem.Decode([]byte(pemCert))
if block == nil {
return errors.New("failed to decode sensu-ca-cert PEM")
}
cert, err := x509.ParseCertificate(block.Bytes)
if err != nil {
return fmt.Errorf("invalid sensu-ca-cert: %s", err)
}
config.CACert = cert
}
if handler.puppetInsecureSkipVerify {
config.InsecureSkipVerify = true
}
client := httpclient.NewCoreClient(config)
request, err := httpclient.NewResourceRequest("core/v2", "Entity", event.Entity.Namespace, event.Entity.Name)
if err != nil {
return err
}
// Delete the Sensu entity
log.Printf("deleting entity (%s/%s)\n", event.Entity.Namespace, event.Entity.Name)
if _, err := client.DeleteResource(context.Background(), request); err != nil {
if httperr, ok := err.(httpclient.HTTPError); ok {
if httperr.StatusCode < 500 {
log.Printf("entity already deleted (%s/%s)", event.Entity.Namespace, event.Entity.Name)
return nil
}
}
return err
}
return nil
}