-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck-certs.go
135 lines (109 loc) · 2.66 KB
/
check-certs.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
package main
import (
"bufio"
"crypto/tls"
"flag"
"fmt"
"os"
"strings"
"time"
"github.com/pkg/errors"
)
// from man sysexits
// ExUsage - The command was used incorrectly, e.g., with the
// wrong number of arguments, a bad flag, a bad syntax
// in a parameter, or whatever.
const ExUsage = 64
var hostsFile = flag.String("hosts", "", "path of file containing hostnames to check")
var days = flag.Int("days", 30, "number of days to look into the future")
var concurrency = flag.Int("concurrency", 8, "concurrent checks")
type result struct {
Hostname string
Err error
}
func readHostsFile(hostsFile string) ([]string, error) {
var hosts []string
if _, err := os.Stat(hostsFile); os.IsNotExist(err) {
return hosts, errors.Errorf("provided hosts file %s does not exist", hostsFile)
}
f, err := os.Open(hostsFile)
if err != nil {
return hosts, errors.Wrap(err, "error opening hosts file")
}
scanner := bufio.NewScanner(f)
for scanner.Scan() {
hosts = append(hosts, strings.TrimSpace(scanner.Text()))
}
if err := scanner.Err(); err != nil {
return hosts, errors.Wrap(err, "error reading hosts file")
}
return hosts, nil
}
func worker(queue chan string, results chan result) {
for host := range queue {
r, err := checkCertificate(host)
if err != nil {
r.Err = err
}
results <- r
}
}
func checkCertificate(host string) (result, error) {
r := result{
Hostname: host,
}
connectHost := host
if !strings.Contains(host, ":") {
connectHost = host + ":443"
}
conn, err := tls.Dial("tcp", connectHost, &tls.Config{})
if err != nil {
return r, errors.Wrap(err, "tls dial")
}
conn.Close()
certExpiry := time.Now().AddDate(0, 0, *days)
for i, cert := range conn.ConnectionState().PeerCertificates {
if certExpiry.After(cert.NotAfter) {
return r, errors.Errorf("cert[%d] %s expires at %v", i, cert.Subject.CommonName, cert.NotAfter)
}
}
return r, nil
}
func main() {
flag.Parse()
var hosts []string
hosts = append(hosts, flag.Args()...)
if len(*hostsFile) > 0 {
fileHosts, err := readHostsFile(*hostsFile)
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(ExUsage)
}
hosts = append(hosts, fileHosts...)
}
queue := make(chan string)
results := make(chan result)
// start workers
for i := 0; i < *concurrency; i++ {
go worker(queue, results)
}
// enqueue work
go func() {
for _, host := range hosts {
queue <- host
}
close(queue)
}()
// consume results
anyErrors := false
for i := 0; i < len(hosts); i++ {
r := <-results
if r.Err != nil {
fmt.Fprintf(os.Stderr, "error: %s: %v\n", r.Hostname, r.Err)
anyErrors = true
}
}
if anyErrors {
os.Exit(1)
}
}