summaryrefslogtreecommitdiffstats
path: root/src/go/collectors/go.d.plugin/modules/x509check/collect.go
blob: 3b5eebdb2739e1afdc35cc5c334042f3c01a7765 (plain)
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
// SPDX-License-Identifier: GPL-3.0-or-later

package x509check

import (
	"crypto/x509"
	"fmt"
	"time"

	"github.com/cloudflare/cfssl/revoke"
)

func (x *X509Check) collect() (map[string]int64, error) {
	certs, err := x.prov.certificates()
	if err != nil {
		return nil, err
	}

	if len(certs) == 0 {
		return nil, fmt.Errorf("no certificate was provided by '%s'", x.Config.Source)
	}

	mx := make(map[string]int64)

	x.collectExpiration(mx, certs)
	if x.CheckRevocation {
		x.collectRevocation(mx, certs)
	}

	return mx, nil
}

func (x *X509Check) collectExpiration(mx map[string]int64, certs []*x509.Certificate) {
	expiry := time.Until(certs[0].NotAfter).Seconds()
	mx["expiry"] = int64(expiry)
	mx["days_until_expiration_warning"] = x.DaysUntilWarn
	mx["days_until_expiration_critical"] = x.DaysUntilCritical

}

func (x *X509Check) collectRevocation(mx map[string]int64, certs []*x509.Certificate) {
	rev, ok, err := revoke.VerifyCertificateError(certs[0])
	if err != nil {
		x.Debug(err)
	}
	if !ok {
		return
	}
	if rev {
		mx["revoked"] = 1
	} else {
		mx["revoked"] = 0
	}
}