-
Notifications
You must be signed in to change notification settings - Fork 1
/
grouped_cert.go
101 lines (88 loc) · 1.92 KB
/
grouped_cert.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
package main
import (
"crypto/md5"
"encoding/hex"
"math"
"sort"
"strconv"
"strings"
"time"
)
type GroupedCert struct {
Fingerprint string
ExpiresInDays int
Certs []Cert
}
func (g GroupedCert) ToSlackMessage() SlackSection {
var sb strings.Builder
// Subject
sb.WriteString(">")
sb.WriteString(g.Certs[0].X509.Subject.CommonName)
sb.WriteString("\n")
// Expiry
days := g.ExpiresInDays
if days < 0 {
sb.WriteString("*EXPIRED ")
days = int(math.Abs(float64(days)))
sb.WriteString(strconv.Itoa(days))
if days == 1 {
sb.WriteString(" DAY")
} else {
sb.WriteString(" DAYS")
}
sb.WriteString(" AGO*")
} else {
sb.WriteString("Expires in ")
sb.WriteString(strconv.Itoa(days))
if days == 1 {
sb.WriteString(" day")
} else {
sb.WriteString(" days")
}
}
sb.WriteString("\n")
// Secret keys
if len(g.Certs) == 1 {
sb.WriteString("Secret:\n")
} else {
sb.WriteString("Secrets:\n")
}
for _, cert := range g.Certs {
sb.WriteString("`")
sb.WriteString(cert.ToKey())
sb.WriteString("`\n")
}
block := SlackSection{
Type: "section",
Text: SlackBlock{
Type: "mrkdwn",
Text: sb.String(),
},
}
return block
}
func groupCerts(certs []Cert, t time.Time) []GroupedCert {
groupedCert := make(map[string][]Cert)
for _, cert := range certs {
cert := cert
fingerprint := getMD5Hash(cert.X509.Raw)
groupedCert[fingerprint] = append(groupedCert[fingerprint], cert)
}
gCerts := make([]GroupedCert, 0, len(groupedCert))
for fingerprint, certs := range groupedCert {
gCerts = append(gCerts, GroupedCert{
Fingerprint: fingerprint,
ExpiresInDays: certs[0].ExpiresInDays(t),
Certs: certs,
})
}
// Sort our grouped into ascending expiry
sort.Slice(gCerts, func(i, j int) bool {
return gCerts[i].ExpiresInDays < gCerts[j].ExpiresInDays
})
return gCerts
}
func getMD5Hash(data []byte) string {
hash := md5.Sum(data)
return hex.EncodeToString(hash[:])
}