-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
240 lines (208 loc) · 6.78 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package main
import (
"crypto/x509"
"encoding/json"
"encoding/pem"
"flag"
"fmt"
"io/ioutil"
stdlog "log"
"net/url"
"os"
"strings"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/aws/external"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/aws/aws-sdk-go-v2/service/s3/s3manager"
"github.com/aws/aws-sdk-go/aws/endpoints"
"github.com/cloudflare/cfssl/revoke"
"github.com/peterbourgon/ff"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
// Config provides a deadline in days before actual expiration to consider a
// certificate as "expiring." Bundles are the actual bundles to check. If the
// bundle begins with "s3://" it will be retrieved from S3. Otherwise, the
// bundle will be treated as a local file path.
type Config struct {
DeadlineDays uint64 `json:"deadline_days"`
Bundles []string `json:"bundles"`
}
func configure(configFile string, debug bool) (Config, error) {
// Default to info level output
zerolog.SetGlobalLevel(zerolog.InfoLevel)
// Use nicer human output
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
if debug {
// Output debug level messages and everything in a human-friendly format
// and allow logging from cfssl revocation check to output
zerolog.SetGlobalLevel(zerolog.DebugLevel)
} else {
// Discard output from cfssl logger
stdlog.SetOutput(ioutil.Discard)
}
// Read in and parse json for the Config struct
raw, err := ioutil.ReadFile(configFile)
if err != nil {
return Config{}, fmt.Errorf("reading file %v: %w", configFile, err)
}
var cfg Config
err = json.Unmarshal(raw, &cfg)
if err != nil {
return Config{}, fmt.Errorf("processing config file %v: %w", configFile, err)
}
return cfg, nil
}
func getCerts(location string) ([]byte, error) {
log.Info().Str("location", location).Msg("trying to load new bundle")
if strings.HasPrefix(location, "s3://") {
u, err := url.Parse(location)
if err != nil {
return []byte{}, fmt.Errorf("could not parse S3 url %v: %w", location, err)
}
bucket := u.Host
// strip leading slash from path
key := u.Path[1:]
log.Debug().Str("bucket", bucket).Str("key", key).Msg("trying s3 location")
cfg, err := external.LoadDefaultAWSConfig()
if err != nil {
return []byte{}, fmt.Errorf("failed to configure AWS client: %w", err)
}
cfg.Region = endpoints.UsEast1RegionID
var raw []byte
buf := aws.NewWriteAtBuffer(raw)
downloader := s3manager.NewDownloader(cfg)
_, err = downloader.Download(buf, &s3.GetObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(key),
})
if err != nil {
return []byte{}, fmt.Errorf("failed to download file, %w", err)
}
return buf.Bytes(), nil
}
// If not S3 then treat as a local file path
raw, err := ioutil.ReadFile(location)
if err != nil {
return []byte{}, fmt.Errorf("reading file %v: %w", location, err)
}
return raw, nil
}
func checkCerts(raw []byte, config Config) []error {
// Decode each PEM block present in the bundle file
var errs []error
rest := raw
var blocks []*pem.Block
var block *pem.Block
for len(rest) > 0 {
block, rest = pem.Decode(rest)
if block == nil {
errs = append(errs, ErrNoCertificates)
break
} else if block.Type != "CERTIFICATE" {
errs = append(errs, ErrParsing)
continue
}
blocks = append(blocks, block)
}
// Now parse each into an x.509 certificate
var certs []x509.Certificate
for _, block := range blocks {
cert, err := x509.ParseCertificate(block.Bytes)
if err != nil || cert == nil {
errs = append(errs, ErrParsing)
continue
}
certs = append(certs, *cert)
}
for _, cert := range certs {
errs = append(errs, validateCert(cert, config)...)
}
if len(certs) == 3 {
log.Debug().Msg("found three certificates, attempting to verify proper chain")
leaf := certs[0]
intermediate := certs[1]
root := certs[2]
if len(leaf.DNSNames) == 0 {
errs = append(errs, ErrParsing)
return errs
}
roots := x509.NewCertPool()
roots.AddCert(&root)
intermediates := x509.NewCertPool()
intermediates.AddCert(&intermediate)
opts := x509.VerifyOptions{
DNSName: leaf.DNSNames[0],
Intermediates: intermediates,
Roots: roots,
}
if _, err := leaf.Verify(opts); err != nil {
log.Error().Str("leaf certificate", leaf.Subject.CommonName).Msgf("could not verify leaf with rest of chain: %v", err)
e := fmt.Errorf("failed to verify certificate using bundled chain: %w", err)
errs = append(errs, e)
}
if leaf.NotAfter.After(intermediate.NotAfter) || leaf.NotAfter.After(root.NotAfter) {
log.Error().Str("leaf certificate", leaf.Subject.CommonName).Msg("leaf expires before a parent certificate")
errs = append(errs, ErrParentExpiresFirst)
}
} else {
log.Warn().Msg("fewer than 3 certificates found, skipping chain validation")
}
return errs
}
func validateCert(cert x509.Certificate, config Config) []error {
var errs []error
now := time.Now()
// How many days before expiration to begin failing
softDeadline := now.AddDate(0, 0, int(config.DeadlineDays))
// Check all certificates expirations
if cert.NotAfter.Before(softDeadline) {
log.Error().Str("subject", cert.Subject.CommonName).Time("expiration", cert.NotAfter).Msgf("violates soft deadline of %d days", config.DeadlineDays)
errs = append(errs, ErrViolatesDeadline)
}
if cert.NotAfter.Before(now) {
log.Error().Str("subject", cert.Subject.CommonName).Time("expiration", cert.NotAfter).Msg("expired")
errs = append(errs, ErrExpired)
}
revoked, ok := revoke.VerifyCertificate(&cert)
if !ok {
log.Warn().Str("subject", cert.Subject.CommonName).Msg("failed to verify revocation status, possibly retry")
// We don't log this as a failure as some certificate may not include information needed to check on
// revocation status, e.g., Digicert root certificate
} else if revoked {
log.Error().Str("subject", cert.Subject.CommonName).Msg("revoked")
errs = append(errs, ErrRevoked)
}
if len(errs) == 0 {
log.Debug().Str("subject", cert.Subject.CommonName).Time("expiration", cert.NotAfter).Msg("certificate not expired or revoked")
}
return errs
}
func main() {
fs := flag.NewFlagSet("my-program", flag.ExitOnError)
var (
configFile = fs.String("config", "chkbundle.json", "location of configuration file")
debug = fs.Bool("debug", false, "log debug information")
)
ff.Parse(fs, os.Args[1:], ff.WithEnvVarPrefix("CHKBUNDLE"))
config, err := configure(*configFile, *debug)
if err != nil {
log.Fatal().Err(err).Msg("could not configure")
}
ok := true
for _, bundle := range config.Bundles {
bytes, err := getCerts(bundle)
if err != nil {
log.Fatal().Err(err).Msg("could not get certificates")
}
errs := checkCerts(bytes, config)
if len(errs) != 0 {
ok = false
}
}
if !ok {
log.Fatal().Msg("one or more bundles failed validity checks")
}
log.Info().Msg("all bundles passed checks!")
}