-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmain.go
174 lines (146 loc) · 4.23 KB
/
main.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
package main
import (
"crypto/tls"
"encoding/csv"
"errors"
"flag"
"fmt"
"log"
"net"
"net/smtp"
"os"
"strings"
"sync"
"time"
ini "gopkg.in/ini.v1"
)
var (
errExpiringSoon = errors.New("expiring soon")
errExpired = errors.New("expired")
)
func main() {
urlFile := flag.String("urls", "/etc/certwatcher/urls.csv", "path to CSV containing list of URLs to monitor")
iniFile := flag.String("config", "/etc/certwatcher/config.ini", "path to config.ini")
days := flag.Int("days", 30, "number of days before triggering alert")
verbose := flag.Bool("v", false, "verbose output")
flag.Parse()
// read config
cfg, err := ini.Load(*iniFile)
if err != nil {
log.Fatalf("could not open config file: %s", err)
}
// load list of hosts to watch
f, err := os.Open(*urlFile)
if err != nil {
log.Fatalf("could not open URL file: %s", err)
}
rdr := csv.NewReader(f)
rdr.FieldsPerRecord = 2
rdr.Comment = '#'
records, err := rdr.ReadAll()
if err != nil {
log.Fatalf("could not read %s: %s", *urlFile, err)
}
var wg sync.WaitGroup
for _, r := range records {
host, desc := r[0], r[1]
if desc == "" {
desc = host
}
wg.Add(1)
go func() {
defer wg.Done()
if err := check(host, "443", *days, *verbose); err != nil {
switch err {
case errExpiringSoon, errExpired:
notify(host, desc, cfg, *days, err, *verbose)
log.Printf("main: sent notification for host %s - %s", host, err)
default:
log.Printf("main: ERROR: unexpected error checking host %s - %s", host, err)
}
}
}()
}
wg.Wait()
}
func check(host, port string, days int, verbose bool) error {
dialer := &net.Dialer{Timeout: 5 * time.Second}
conn, err := tls.DialWithDialer(dialer, "tcp", host+":"+port, &tls.Config{
InsecureSkipVerify: true,
})
if err != nil {
return err
}
defer conn.Close()
if err := conn.Handshake(); err != nil {
return err
}
for i, cert := range conn.ConnectionState().PeerCertificates {
if cert.IsCA {
continue
}
if verbose {
log.Printf("check: %s certificate %d: expires after %s (%s)", host, i, cert.NotAfter, time.Until(cert.NotAfter))
log.Printf("check: %s certificate %d: issuer: %s", host, i, cert.Issuer.Names)
log.Printf("check: %s certificate %d: names: %s", host, i, cert.Subject.Names)
log.Printf("check: %s certificate %d: DNSNames: %s", host, i, cert.DNSNames)
}
if time.Now().After(cert.NotAfter) {
return errExpired
}
if time.Until(cert.NotAfter) < time.Duration(days)*time.Hour*24 {
return errExpiringSoon
}
}
log.Printf("check: %s - certificate is ok", host)
return nil
}
func notify(host, desc string, cfg *ini.File, days int, err error, verbose bool) {
section := cfg.Section("certwatcher")
if !section.Key("sendmail").MustBool() {
log.Println("notify: refusing to send email due to config.")
return
}
port := "587"
if section.Key("port").String() != "" {
port = section.Key("port").String()
}
mailhost := section.Key("host").String()
auth := smtp.PlainAuth("",
section.Key("username").String(),
section.Key("password").String(),
mailhost,
)
to := []string{section.Key("rcpt").String()}
var subject, body string
if err == errExpiringSoon {
subject = fmt.Sprintf("Subject: %s certificate expiring soon: %s", section.Key("subjectprefix").String(), desc)
body = fmt.Sprintf("The SSL certificate for the host %s (%s) is expiring in less than %d days.", host, desc, days)
} else if err == errExpired {
subject = fmt.Sprintf("Subject: %s certificate has expired! %s", section.Key("subjectprefix").String(), desc)
body = fmt.Sprintf("The SSL certificate for the host %s (%s) has expired!", host, desc)
}
msg := []byte(strings.Join([]string{subject,
fmt.Sprintf("To: %s", strings.Join(to, ", ")),
fmt.Sprintf("From: %s", section.Key("from").String()),
"",
body,
"",
"Please take appropriate action!",
},
"\r\n",
))
if verbose {
log.Printf("notify: sending host %s expiration notification to %s", host, section.Key("rcpt").String())
}
errc := make(chan error, 1)
go func() {
errc <- smtp.SendMail(mailhost+":"+port, auth, section.Key("from").String(), to, msg)
}()
select {
case err := <-errc:
log.Fatalf("could not send email: %s", err)
case <-time.After(30 * time.Second):
log.Fatalf("Timeout reaching mail server")
}
}