forked from hashicorp/terraform
/
resource_circonus_check_tcp.go
254 lines (218 loc) · 7.62 KB
/
resource_circonus_check_tcp.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
package circonus
import (
"bytes"
"fmt"
"log"
"strconv"
"strings"
"github.com/circonus-labs/circonus-gometrics/api/config"
"github.com/hashicorp/errwrap"
"github.com/hashicorp/terraform/helper/hashcode"
"github.com/hashicorp/terraform/helper/schema"
)
const (
// circonus_check.tcp.* resource attribute names
checkTCPBannerRegexpAttr = "banner_regexp"
checkTCPCAChainAttr = "ca_chain"
checkTCPCertFileAttr = "certificate_file"
checkTCPCiphersAttr = "ciphers"
checkTCPHostAttr = "host"
checkTCPKeyFileAttr = "key_file"
checkTCPPortAttr = "port"
checkTCPTLSAttr = "tls"
)
var checkTCPDescriptions = attrDescrs{
checkTCPBannerRegexpAttr: `This regular expression is matched against the response banner. If a match is not found, the check will be marked as bad.`,
checkTCPCAChainAttr: "A path to a file containing all the certificate authorities that should be loaded to validate the remote certificate (for TLS checks).",
checkTCPCertFileAttr: "A path to a file containing the client certificate that will be presented to the remote server (for TLS checks).",
checkTCPCiphersAttr: "A list of ciphers to be used when establishing a TLS connection",
checkTCPHostAttr: "Specifies the host name or IP address to connect to for this TCP check",
checkTCPKeyFileAttr: "A path to a file containing key to be used in conjunction with the cilent certificate (for TLS checks)",
checkTCPPortAttr: "Specifies the port on which the management interface can be reached.",
checkTCPTLSAttr: "Upgrade TCP connection to use TLS.",
}
var schemaCheckTCP = &schema.Schema{
Type: schema.TypeSet,
Optional: true,
MaxItems: 1,
MinItems: 1,
Set: hashCheckTCP,
Elem: &schema.Resource{
Schema: convertToHelperSchema(checkTCPDescriptions, map[schemaAttr]*schema.Schema{
checkTCPBannerRegexpAttr: &schema.Schema{
Type: schema.TypeString,
Optional: true,
ValidateFunc: validateRegexp(checkTCPBannerRegexpAttr, `.+`),
},
checkTCPCAChainAttr: &schema.Schema{
Type: schema.TypeString,
Optional: true,
ValidateFunc: validateRegexp(checkTCPCAChainAttr, `.+`),
},
checkTCPCertFileAttr: &schema.Schema{
Type: schema.TypeString,
Optional: true,
ValidateFunc: validateRegexp(checkTCPCertFileAttr, `.+`),
},
checkTCPCiphersAttr: &schema.Schema{
Type: schema.TypeString,
Optional: true,
ValidateFunc: validateRegexp(checkTCPCiphersAttr, `.+`),
},
checkTCPHostAttr: &schema.Schema{
Type: schema.TypeString,
Required: true,
ValidateFunc: validateRegexp(checkTCPHostAttr, `.+`),
},
checkTCPKeyFileAttr: &schema.Schema{
Type: schema.TypeString,
Optional: true,
ValidateFunc: validateRegexp(checkTCPKeyFileAttr, `.+`),
},
checkTCPPortAttr: &schema.Schema{
Type: schema.TypeInt,
Required: true,
ValidateFunc: validateFuncs(
validateIntMin(checkTCPPortAttr, 0),
validateIntMax(checkTCPPortAttr, 65535),
),
},
checkTCPTLSAttr: &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Default: false,
},
}),
},
}
// checkAPIToStateTCP reads the Config data out of circonusCheck.CheckBundle into the
// statefile.
func checkAPIToStateTCP(c *circonusCheck, d *schema.ResourceData) error {
tcpConfig := make(map[string]interface{}, len(c.Config))
// swamp is a sanity check: it must be empty by the time this method returns
swamp := make(map[config.Key]string, len(c.Config))
for k, v := range c.Config {
swamp[k] = v
}
saveBoolConfigToState := func(apiKey config.Key, attrName schemaAttr) {
if s, ok := c.Config[apiKey]; ok {
switch strings.ToLower(s) {
case "1", "true", "t", "yes", "y":
tcpConfig[string(attrName)] = true
case "0", "false", "f", "no", "n":
tcpConfig[string(attrName)] = false
default:
log.Printf("PROVIDER BUG: unsupported boolean: %q for API Config Key %q", s, string(apiKey))
return
}
}
delete(swamp, apiKey)
}
saveIntConfigToState := func(apiKey config.Key, attrName schemaAttr) {
if v, ok := c.Config[apiKey]; ok {
i, err := strconv.ParseInt(v, 10, 64)
if err != nil {
log.Printf("[ERROR]: Unable to convert %s to an integer: %v", apiKey, err)
return
}
tcpConfig[string(attrName)] = int(i)
}
delete(swamp, apiKey)
}
saveStringConfigToState := func(apiKey config.Key, attrName schemaAttr) {
if v, ok := c.Config[apiKey]; ok {
tcpConfig[string(attrName)] = v
}
delete(swamp, apiKey)
}
saveStringConfigToState(config.BannerMatch, checkTCPBannerRegexpAttr)
saveStringConfigToState(config.CAChain, checkTCPCAChainAttr)
saveStringConfigToState(config.CertFile, checkTCPCertFileAttr)
saveStringConfigToState(config.Ciphers, checkTCPCiphersAttr)
tcpConfig[string(checkTCPHostAttr)] = c.Target
saveStringConfigToState(config.KeyFile, checkTCPKeyFileAttr)
saveIntConfigToState(config.Port, checkTCPPortAttr)
saveBoolConfigToState(config.UseSSL, checkTCPTLSAttr)
whitelistedConfigKeys := map[config.Key]struct{}{
config.ReverseSecretKey: struct{}{},
config.SubmissionURL: struct{}{},
}
for k := range swamp {
if _, ok := whitelistedConfigKeys[k]; ok {
delete(c.Config, k)
}
if _, ok := whitelistedConfigKeys[k]; !ok {
log.Printf("[ERROR]: PROVIDER BUG: API Config not empty: %#v", swamp)
}
}
if err := d.Set(checkTCPAttr, schema.NewSet(hashCheckTCP, []interface{}{tcpConfig})); err != nil {
return errwrap.Wrapf(fmt.Sprintf("Unable to store check %q attribute: {{err}}", checkTCPAttr), err)
}
return nil
}
// hashCheckTCP creates a stable hash of the normalized values
func hashCheckTCP(v interface{}) int {
m := v.(map[string]interface{})
b := &bytes.Buffer{}
b.Grow(defaultHashBufSize)
writeBool := func(attrName schemaAttr) {
if v, ok := m[string(attrName)]; ok {
fmt.Fprintf(b, "%t", v.(bool))
}
}
writeInt := func(attrName schemaAttr) {
if v, ok := m[string(attrName)]; ok {
fmt.Fprintf(b, "%x", v.(int))
}
}
writeString := func(attrName schemaAttr) {
if v, ok := m[string(attrName)]; ok && v.(string) != "" {
fmt.Fprint(b, strings.TrimSpace(v.(string)))
}
}
// Order writes to the buffer using lexically sorted list for easy visual
// reconciliation with other lists.
writeString(checkTCPBannerRegexpAttr)
writeString(checkTCPCAChainAttr)
writeString(checkTCPCertFileAttr)
writeString(checkTCPCiphersAttr)
writeString(checkTCPHostAttr)
writeString(checkTCPKeyFileAttr)
writeInt(checkTCPPortAttr)
writeBool(checkTCPTLSAttr)
s := b.String()
return hashcode.String(s)
}
func checkConfigToAPITCP(c *circonusCheck, l interfaceList) error {
c.Type = string(apiCheckTypeTCP)
// Iterate over all `tcp` attributes, even though we have a max of 1 in the
// schema.
for _, mapRaw := range l {
tcpConfig := newInterfaceMap(mapRaw)
if v, found := tcpConfig[checkTCPBannerRegexpAttr]; found {
c.Config[config.BannerMatch] = v.(string)
}
if v, found := tcpConfig[checkTCPCAChainAttr]; found {
c.Config[config.CAChain] = v.(string)
}
if v, found := tcpConfig[checkTCPCertFileAttr]; found {
c.Config[config.CertFile] = v.(string)
}
if v, found := tcpConfig[checkTCPCiphersAttr]; found {
c.Config[config.Ciphers] = v.(string)
}
if v, found := tcpConfig[checkTCPHostAttr]; found {
c.Target = v.(string)
}
if v, found := tcpConfig[checkTCPKeyFileAttr]; found {
c.Config[config.KeyFile] = v.(string)
}
if v, found := tcpConfig[checkTCPPortAttr]; found {
c.Config[config.Port] = fmt.Sprintf("%d", v.(int))
}
if v, found := tcpConfig[checkTCPTLSAttr]; found {
c.Config[config.UseSSL] = fmt.Sprintf("%t", v.(bool))
}
}
return nil
}