-
Notifications
You must be signed in to change notification settings - Fork 8
/
collector.go
281 lines (253 loc) · 7.78 KB
/
collector.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
package main
import (
"code.cloudfoundry.org/credhub-cli/credhub"
"code.cloudfoundry.org/credhub-cli/credhub/credentials"
"crypto/x509"
"encoding/json"
"encoding/pem"
"fmt"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
log "github.com/sirupsen/logrus"
"regexp"
"strings"
"time"
)
const (
beginCertificate = "-----BEGIN CERTIFICATE-----"
endCertificate = "-----END CERTIFICATE-----"
)
type certificate struct {
format string
index int
notAfter *time.Time
}
type credential struct {
createdAt time.Time
name string
path string
id string
credtype string
certs []certificate
}
// CredhubCollector -
type CredhubCollector struct {
filters []*regexp.Regexp
cli *credhub.CredHub
nameLike string
path string
credentialMetrics *prometheus.GaugeVec
certificateExpiresMetrics *prometheus.GaugeVec
scrapeErrorMetric prometheus.Gauge
lastScrapeTimestampMetric prometheus.Gauge
flushCache bool
}
// NewCredhubCollector -
func NewCredhubCollector(
deployment string,
environment string,
filters []*regexp.Regexp,
cli *credhub.CredHub) *CredhubCollector {
credentialMetrics = promauto.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: "credhub",
Subsystem: "credential",
Name: "created_at",
Help: "Number of seconds since 1970 since last rotation of credhub credential",
ConstLabels: prometheus.Labels{"environment": environment, "deployment": deployment},
},
[]string{"path", "name", "id", "type"},
)
certificateExpiresMetrics = promauto.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: "credhub",
Subsystem: "certificate",
Name: "expires_at",
Help: "Number of seconds since 1970 until certificate will expire",
ConstLabels: prometheus.Labels{"environment": environment, "deployment": deployment},
},
[]string{"path", "name", "id", "index"},
)
scrapeErrorMetric := promauto.NewGauge(
prometheus.GaugeOpts{
Namespace: "credhub",
Subsystem: "",
Name: "last_scrap_error",
Help: "Whether the last scrape of Applications metrics from Credhub resulted in an error (1 for error, 0 for success)",
ConstLabels: prometheus.Labels{"environment": environment, "deployment": deployment},
},
)
lastScrapeTimestampMetric := promauto.NewGauge(
prometheus.GaugeOpts{
Namespace: "credhub",
Subsystem: "",
Name: "last_scrape_timestamp",
Help: "Number of seconds since 1970 since last scrape of metrics from credhub.",
ConstLabels: prometheus.Labels{"environment": environment, "deployment": deployment},
},
)
return &CredhubCollector{
cli: cli,
filters: filters,
nameLike: "",
path: "",
credentialMetrics: credentialMetrics,
certificateExpiresMetrics: certificateExpiresMetrics,
scrapeErrorMetric: scrapeErrorMetric,
lastScrapeTimestampMetric: lastScrapeTimestampMetric,
}
}
func (c CredhubCollector) filterNameLike(name string) {
c.nameLike = name
}
func (c CredhubCollector) filterPath(path string) {
c.path = path
}
func (c CredhubCollector) processCertificates(info *credential, format string, content string) {
data := []byte(content)
for idx := 1; len(data) != 0; idx++ {
block, rest := pem.Decode(data)
dataStr := strings.TrimSpace(string(rest))
data = []byte(dataStr)
if block == nil || block.Bytes == nil {
log.Errorf("error while reading certificate '%s': invalid pem decode", info.path)
info.certs = append(info.certs, certificate{
index: idx,
})
return
}
cert, err := x509.ParseCertificate(block.Bytes)
if err != nil {
log.Errorf("error while reading certificate '%s' : %s", info.path, err.Error())
info.certs = append(info.certs, certificate{
index: idx,
})
return
}
info.certs = append(info.certs, certificate{
format: format,
index: idx,
notAfter: &cert.NotAfter,
})
}
}
func (c CredhubCollector) searchCertificate(info *credential, cred credentials.Credential) {
log.Debugf("searching for certificates in credential '%s'", cred.Name)
bytes, _ := cred.MarshalJSON()
raw := string(bytes)
raw = strings.Replace(raw, "\\n", "\n", -1)
certs := []string{}
for start := 0; start != -1; {
start := strings.Index(raw, beginCertificate)
stop := strings.Index(raw, endCertificate)
if start == -1 || stop == -1 {
break
}
certificate := raw[start : stop+len(endCertificate)]
certs = append(certs, certificate)
raw = raw[stop+len(endCertificate) : len(raw)-1]
}
c.processCertificates(info, "%s", strings.Join(certs, "\n"))
}
func (c CredhubCollector) filterCertificates(info *credential, cred credentials.Credential) {
for _, r := range c.filters {
if r.MatchString(info.path) {
log.Debugf("regexp match : '%s'", info.name)
c.searchCertificate(info, cred)
}
}
}
func (c CredhubCollector) run(interval time.Duration) {
go func() {
for {
results, err := c.search()
if err != nil {
c.lastScrapeTimestampMetric.Set(float64(time.Now().Unix()))
c.scrapeErrorMetric.Set(1.0)
} else {
creds, errCount := c.analyze(results)
c.writeMetrics(creds, errCount)
}
time.Sleep(interval)
}
}()
}
func (c CredhubCollector) update() {
}
func (c CredhubCollector) search() (credentials.FindResults, error) {
var (
results credentials.FindResults
err error
)
log.Debugf("searching creadhub credentials")
if c.nameLike != "" {
results, err = c.cli.FindByPartialName(c.nameLike)
} else if c.path != "" {
results, err = c.cli.FindByPath(c.path)
} else {
results, err = c.cli.FindByPartialName("")
}
if err != nil {
log.Errorf("Error fetching credentials from credhub: %s", err.Error())
}
return results, err
}
func (c CredhubCollector) analyze(results credentials.FindResults) ([]credential, int) {
errors := 0
creds := []credential{}
log.Debugf("analyzing %d found credentials", len(results.Credentials))
for _, cred := range results.Credentials {
log.Debugf("reading credential '%s'", cred.Name)
cred, err := c.cli.GetLatestVersion(cred.Name)
if err != nil {
log.Errorf("Error fetching credential '%s' from credhub: %s", cred.Name, err.Error())
errors++
continue
}
datetime, _ := time.Parse(time.RFC3339, cred.VersionCreatedAt)
parts := strings.Split(cred.Name, "/")
info := credential{
createdAt: datetime,
name: parts[len(parts)-1],
path: cred.Name,
id: cred.Id,
credtype: cred.Type,
certs: []certificate{},
}
if cred.Type == "certificate" {
var data credentials.Certificate
bytes, _ := cred.MarshalJSON()
_ = json.Unmarshal(bytes, &data)
c.processCertificates(&info, "%s-cert", data.Value.Certificate)
c.processCertificates(&info, "%s-ca", data.Value.Ca)
} else {
c.filterCertificates(&info, cred)
}
creds = append(creds, info)
}
return creds, errors
}
func (c CredhubCollector) writeMetrics(creds []credential, errors int) {
log.Debugf("writing metrics for %d analyzed credentials", len(creds))
c.scrapeErrorMetric.Set(float64(errors))
c.lastScrapeTimestampMetric.Set(float64(time.Now().Unix()))
c.credentialMetrics.Reset()
c.certificateExpiresMetrics.Reset()
for _, cred := range creds {
c.credentialMetrics.WithLabelValues(cred.path, cred.name, cred.id, cred.credtype).Set(float64(cred.createdAt.Unix()))
for _, cert := range cred.certs {
if cert.notAfter == nil {
c.scrapeErrorMetric.Add(1.0)
continue
}
index := fmt.Sprintf("%d", cert.index)
name := fmt.Sprintf(cert.format, cred.name)
certificateExpiresMetrics.
WithLabelValues(cred.path, name, cred.id, index).
Set(float64(cert.notAfter.Unix()))
}
}
}
// Local Variables:
// ispell-local-dictionary: "american"
// End: