summaryrefslogtreecommitdiffstats
path: root/src/go/plugin/go.d/modules/x509check/collect.go
blob: fc98e3a26f5e4b3685d79d8f6d0770cd64c5397b (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
55
56
57
58
// 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
	}

	mx["revoked"] = 0
	mx["not_revoked"] = 0

	if rev {
		mx["revoked"] = 1
	} else {
		mx["not_revoked"] = 1
	}
}