Skip to content

Commit c0653ee

Browse files
authored
Merge pull request #2 from elfranne/cert
add certificate authentication
2 parents 02760c3 + 45630b5 commit c0653ee

File tree

3 files changed

+70
-6
lines changed

3 files changed

+70
-6
lines changed

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
module github.com/elfranne/sensu-etcd-check
22

3-
go 1.19
3+
go 1.21
44

55
require (
66
github.com/sensu/core/v2 v2.19.0
77
github.com/sensu/sensu-plugin-sdk v0.18.0
8+
go.etcd.io/etcd/client/pkg/v3 v3.5.10
89
go.etcd.io/etcd/client/v3 v3.5.10
910
)
1011

@@ -37,7 +38,6 @@ require (
3738
github.com/spf13/viper v1.7.0 // indirect
3839
github.com/subosito/gotenv v1.2.0 // indirect
3940
go.etcd.io/etcd/api/v3 v3.5.10 // indirect
40-
go.etcd.io/etcd/client/pkg/v3 v3.5.10 // indirect
4141
go.uber.org/atomic v1.7.0 // indirect
4242
go.uber.org/multierr v1.6.0 // indirect
4343
go.uber.org/zap v1.17.0 // indirect

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
4545
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
4646
github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no=
4747
github.com/echlebek/crock v1.0.1 h1:KbzamClMIfVIkkjq/GTXf+N16KylYBpiaTitO3f1ujg=
48+
github.com/echlebek/crock v1.0.1/go.mod h1:/kvwHRX3ZXHj/kHWJkjXDmzzRow54EJuHtQ/PapL/HI=
4849
github.com/echlebek/timeproxy v1.0.0 h1:V41/v8tmmMDNMA2GrBPI45nlXb3F7+OY+nJz1BqKsCk=
4950
github.com/echlebek/timeproxy v1.0.0/go.mod h1:0dg2Lnb8no/jFwoMQKMTU6iAivgoMptGqSTprhnrRtk=
5051
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
@@ -184,6 +185,7 @@ github.com/sensu/core/v2 v2.19.0/go.mod h1:2etWGsa+nx5G2Q3CKiSJY9kSg8VhCgGzgp1Vy
184185
github.com/sensu/sensu-api-tools v0.1.0 h1:ctEyFIY1aKis1KqL7wOo+Apg/5t9X6vBVLzrqUUuBkQ=
185186
github.com/sensu/sensu-api-tools v0.1.0/go.mod h1:SNISS4OhwNSZI9/YKTQr1bghOEwed9ZT4v+ztKk1Mq0=
186187
github.com/sensu/sensu-go/types v0.12.0 h1:t8gupS1QhkuA/b9LzTaF0h6DBGHX2UzKHyuBPhj/PoA=
188+
github.com/sensu/sensu-go/types v0.12.0/go.mod h1:PHk3pUJHCsFzoXnKmm9ERfnHnerzaG2rjISWGcZq3os=
187189
github.com/sensu/sensu-licensing/v2 v2.2.1 h1:9JI4iVm4ujWN4etI/Kdper6Q2lOn3HIEaGe234N8j40=
188190
github.com/sensu/sensu-licensing/v2 v2.2.1/go.mod h1:53lwddwN4XwZUld5KtnWQduSH6F8rBOsWuEk2EUeooI=
189191
github.com/sensu/sensu-plugin-sdk v0.18.0 h1:aR5N9SsqRm1NqiJo7k8GeLEPwGQIfeY4bbePPByUdnI=
@@ -216,6 +218,7 @@ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXf
216218
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
217219
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
218220
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
221+
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
219222
github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s=
220223
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
221224
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
@@ -397,6 +400,7 @@ gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
397400
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
398401
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
399402
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
403+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
400404
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
401405
honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
402406
honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=

main.go

Lines changed: 64 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,27 @@ package main
22

33
import (
44
"context"
5+
"crypto/tls"
6+
"errors"
57
"fmt"
8+
"os"
69
"time"
710

811
corev2 "github.com/sensu/core/v2"
912
"github.com/sensu/sensu-plugin-sdk/sensu"
13+
"go.etcd.io/etcd/client/pkg/v3/transport"
1014
clientv3 "go.etcd.io/etcd/client/v3"
1115
)
1216

1317
// Config represents the check plugin config.
1418
type Config struct {
1519
sensu.PluginConfig
16-
Url []string
17-
Size int64
20+
Url []string
21+
Size int64
22+
CertFile string
23+
KeyFile string
24+
TrustedCAFile string
25+
Timeout int64
1826
}
1927

2028
var (
@@ -37,10 +45,35 @@ var (
3745
&sensu.PluginConfigOption[int64]{
3846
Path: "size",
3947
Argument: "size",
40-
Default: 3_000_000_000, // Alarm at 3G, default DB is set to 4G
48+
Default: 1_500_000_000, // Alarm at 1.5G, default DB is set to 2G
4149
Usage: "Maximum aatabase Size",
4250
Value: &plugin.Size,
4351
},
52+
&sensu.PluginConfigOption[string]{
53+
Path: "cert-file",
54+
Argument: "cert-file",
55+
Usage: "Path to the cert",
56+
Value: &plugin.CertFile,
57+
},
58+
&sensu.PluginConfigOption[string]{
59+
Path: "key-file",
60+
Argument: "key-file",
61+
Usage: "Path to the key",
62+
Value: &plugin.KeyFile,
63+
},
64+
&sensu.PluginConfigOption[string]{
65+
Path: "trusted-ca-file",
66+
Argument: "trusted-ca-file",
67+
Usage: "Path to the CA file",
68+
Value: &plugin.TrustedCAFile,
69+
},
70+
&sensu.PluginConfigOption[int64]{
71+
Path: "timeout",
72+
Argument: "timeout",
73+
Usage: "Request timeout",
74+
Default: 5,
75+
Value: &plugin.Timeout,
76+
},
4477
}
4578
)
4679

@@ -50,15 +83,42 @@ func main() {
5083
}
5184

5285
func checkArgs(event *corev2.Event) (int, error) {
86+
87+
if _, err := os.Stat(plugin.CertFile); errors.Is(err, os.ErrNotExist) {
88+
fmt.Printf("could not load certificate(%s): %v", plugin.CertFile, err)
89+
return sensu.CheckStateCritical, nil
90+
}
91+
92+
if _, err := os.Stat(plugin.KeyFile); errors.Is(err, os.ErrNotExist) {
93+
fmt.Printf("could not load certificate key(%s): %v", plugin.KeyFile, err)
94+
return sensu.CheckStateCritical, nil
95+
}
96+
97+
if _, err := os.Stat(plugin.TrustedCAFile); errors.Is(err, os.ErrNotExist) {
98+
fmt.Printf("could not load CA(%s): %v", plugin.TrustedCAFile, err)
99+
return sensu.CheckStateCritical, nil
100+
}
101+
53102
return sensu.CheckStateOK, nil
54103
}
55104

56105
func executeCheck(event *corev2.Event) (int, error) {
106+
tlsConfig := &tls.Config{}
107+
if len(plugin.CertFile) > 0 && len(plugin.KeyFile) > 0 && len(plugin.TrustedCAFile) > 0 {
108+
tlsInfo := transport.TLSInfo{
109+
CertFile: plugin.CertFile,
110+
KeyFile: plugin.KeyFile,
111+
TrustedCAFile: plugin.TrustedCAFile,
112+
}
113+
tlsConfig, _ = tlsInfo.ClientConfig()
114+
}
57115

58116
cli, err := clientv3.New(clientv3.Config{
59117
Endpoints: plugin.Url,
60-
DialTimeout: 5 * time.Second,
118+
DialTimeout: time.Duration(plugin.Timeout) * time.Second,
119+
TLS: tlsConfig,
61120
})
121+
62122
if err != nil {
63123
fmt.Printf("could not connect: %s", err)
64124
return sensu.CheckStateCritical, nil

0 commit comments

Comments
 (0)