-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
422 lines (353 loc) · 12 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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
// Copyright 2020 Adam Chalkley
//
// https://github.com/atc0005/check-cert
//
// Licensed under the MIT License. See LICENSE file in the project root for
// full license information.
//go:generate go-winres make --product-version=git-tag --file-version=git-tag
package main
import (
"crypto/x509"
"errors"
"fmt"
"os"
"github.com/rs/zerolog"
"github.com/grantae/certinfo"
"github.com/atc0005/check-cert/internal/certs"
"github.com/atc0005/check-cert/internal/config"
"github.com/atc0005/check-cert/internal/netutils"
"github.com/atc0005/check-cert/internal/textutils"
"github.com/atc0005/go-nagios"
)
func main() {
// Setup configuration by parsing user-provided flags.
cfg, cfgErr := config.New(config.AppType{Inspector: true})
switch {
case errors.Is(cfgErr, config.ErrVersionRequested):
fmt.Println(config.Version())
return
case cfgErr != nil:
// We make some assumptions when setting up our logger as we do not
// have a working configuration based on sysadmin-specified choices.
consoleWriter := zerolog.ConsoleWriter{Out: os.Stderr}
logger := zerolog.New(consoleWriter).With().Timestamp().Caller().Logger()
logger.Err(cfgErr).Msg("Error initializing application")
return
}
log := cfg.Log.With().Logger()
var certChain []*x509.Certificate
// Anything from the specified file that couldn't be converted to a
// certificate chain. While likely not of high value, it could help
// identify why a certificate isn't being properly trusted by a client
// application, so emitting it may be useful to the user of this
// application.
var parseAttemptLeftovers []byte
var certChainSource string
// Honor request to parse filename first
switch {
case cfg.Filename != "":
log.Debug().Msg("Attempting to retrieve certificates from file")
var err error
certChain, parseAttemptLeftovers, err = certs.GetCertsFromFile(cfg.Filename)
if err != nil {
log.Error().Err(err).Msg(
"Error parsing certificates file")
os.Exit(config.ExitCodeCatchall)
}
certChainSource = cfg.Filename
case cfg.Server != "":
log.Debug().Msg("Expanding given host pattern in order to obtain IP Address")
expandedHost, expandErr := netutils.ExpandHost(cfg.Server)
switch {
case expandErr != nil:
log.Error().Err(expandErr).Msg(
"Error expanding given host pattern")
os.Exit(config.ExitCodeCatchall)
// Fail early for IP Ranges. While we could just grab the first
// expanded IP Address, this may be a potential source of confusion
// best avoided.
case expandedHost.Range:
log.Error().Msgf(
"Given host pattern invalid; " +
"host pattern is a CIDR or partial IP range",
)
os.Exit(config.ExitCodeCatchall)
case len(expandedHost.Expanded) == 0:
log.Error().Msg(
"Failed to expand given host value to IP Address")
os.Exit(config.ExitCodeCatchall)
case len(expandedHost.Expanded) > 1:
ipAddrs := zerolog.Arr()
for _, ip := range expandedHost.Expanded {
ipAddrs.Str(ip)
}
log.Debug().
Int("num_ip_addresses", len(expandedHost.Expanded)).
Array("ip_addresses", ipAddrs).
Msg("Multiple IP Addresses resolved from given host pattern")
log.Debug().Msg("Using first IP Address, ignoring others")
}
// Grab first IP Address from the resolved collection. We'll
// explicitly use it for cert retrieval and note it in the report
// output.
ipAddr := expandedHost.Expanded[0]
// Server Name Indication (SNI) support is used to request a specific
// certificate chain from a remote server.
//
// We use the value specified by the server flag to open a connection
// to the remote server. If available, we use the DNS Name value
// specified by the DNS Name flag as our host value, otherwise we
// fallback to using the value specified by the server flag as our
// host value.
//
// For a service with only one certificate chain the host value is
// less important, but for a host with multiple certificate chains
// having the correct host value is crucial.
var hostVal string
switch {
// We have a resolved IP Address and a sysadmin-specified DNS Name
// value to use for a SNI-enabled certificate retrieval attempt.
case expandedHost.Resolved && cfg.DNSName != "":
hostVal = cfg.DNSName
certChainSource = fmt.Sprintf(
"service running on %s (%s) at port %d using host value %q",
expandedHost.Given,
ipAddr,
cfg.Port,
hostVal,
)
// We have a valid IP Address to use for opening the connection and a
// sysadmin-specified DNS Name value to use for a SNI-enabled
// certificate retrieval attempt.
case cfg.DNSName != "":
hostVal = cfg.DNSName
certChainSource = fmt.Sprintf(
"service running on %s at port %d using host value %q",
ipAddr,
cfg.Port,
hostVal,
)
// We have a resolved IP Address, but not a sysadmin-specified DNS
// Name value. We'll use the resolvable name/FQDN for a SNI-enabled
// certificate retrieval attempt.
case expandedHost.Resolved && cfg.DNSName == "":
hostVal = expandedHost.Given
certChainSource = fmt.Sprintf(
"service running on %s (%s) at port %d using host value %q",
expandedHost.Given,
ipAddr,
cfg.Port,
expandedHost.Given,
)
default:
certChainSource = fmt.Sprintf(
"service running on %s at port %d",
ipAddr,
cfg.Port,
)
}
log.Debug().
Str("server", cfg.Server).
Str("dns_name", cfg.DNSName).
Str("ip_address", ipAddr).
Str("host_value", hostVal).
Int("port", cfg.Port).
Msg("Retrieving certificate chain")
var certFetchErr error
certChain, certFetchErr = netutils.GetCerts(
hostVal,
ipAddr,
cfg.Port,
cfg.Timeout(),
log,
)
if certFetchErr != nil {
log.Error().Err(certFetchErr).Msg(
"Error fetching certificates chain")
os.Exit(config.ExitCodeCatchall)
}
}
textutils.PrintHeader("CERTIFICATES | SUMMARY")
switch {
case len(certChain) == 0:
log.Err(certs.ErrNoCertsFound).Msg("")
os.Exit(config.ExitCodeCatchall)
default:
// If a certificate chain was pulled from a file, we "found" it, if it
// was pulled from a server we "retrieved" it.
var template string
switch {
case cfg.Filename != "":
template = "- %s: %d certs found in %s\n"
default:
template = "- %s: %d certs retrieved for %s\n"
}
fmt.Printf(
template,
nagios.StateOKLabel,
len(certChain),
certChainSource,
)
}
hostnameValidationResult := certs.ValidateHostname(
certChain,
cfg.Server,
cfg.DNSName,
config.IgnoreHostnameVerificationFailureIfEmptySANsListFlag,
certs.CertChainValidationOptions{
IgnoreHostnameVerificationFailureIfEmptySANsList: cfg.IgnoreHostnameVerificationFailureIfEmptySANsList,
IgnoreValidationResultHostname: !cfg.ApplyCertHostnameValidationResults(),
},
)
switch {
case hostnameValidationResult.IsFailed():
log.Debug().
Err(hostnameValidationResult.Err()).
Msgf("%s validation failure", hostnameValidationResult.CheckName())
fmt.Printf(
"- %s: %s %s\n",
hostnameValidationResult.ServiceState().Label,
hostnameValidationResult.Status(),
hostnameValidationResult.Overview(),
)
case hostnameValidationResult.IsIgnored():
log.Debug().
Msgf("%s validation ignored", hostnameValidationResult.CheckName())
fmt.Printf(
"- %s: %s %s\n",
hostnameValidationResult.ServiceState().Label,
hostnameValidationResult.Status(),
hostnameValidationResult.Overview(),
)
default:
log.Debug().Msg("Hostname validation successful")
fmt.Printf(
"- %s: %s %s\n",
hostnameValidationResult.ServiceState().Label,
hostnameValidationResult.Status(),
hostnameValidationResult.Overview(),
)
}
sansValidationResult := certs.ValidateSANsList(
certChain,
cfg.DNSName,
cfg.SANsEntries,
certs.CertChainValidationOptions{
IgnoreValidationResultSANs: !cfg.ApplyCertSANsListValidationResults(),
},
)
switch {
case sansValidationResult.IsFailed():
log.Debug().
Err(sansValidationResult.Err()).
Int("sans_entries_requested", sansValidationResult.NumExpected()).
Int("sans_entries_found", sansValidationResult.NumMatched()).
Int("sans_entries_mismatched", sansValidationResult.NumMismatched()).
Msg("SANs entries mismatch")
fmt.Printf(
"- %s: %s\n",
sansValidationResult.ServiceState().Label,
sansValidationResult.String(),
)
case sansValidationResult.IsIgnored():
log.Debug().
Msgf("%s validation ignored", sansValidationResult.CheckName())
fmt.Printf(
"- %s: %s\n",
sansValidationResult.ServiceState().Label,
sansValidationResult.String(),
)
default:
log.Debug().
Int("sans_entries_requested", sansValidationResult.NumExpected()).
Int("sans_entries_found", sansValidationResult.NumMatched()).
Msgf("%s validation successful", sansValidationResult.CheckName())
fmt.Printf(
"- %s: %s\n",
sansValidationResult.ServiceState().Label,
sansValidationResult.String(),
)
}
expirationValidationResult := certs.ValidateExpiration(
certChain,
cfg.AgeCritical,
cfg.AgeWarning,
cfg.VerboseOutput,
certs.CertChainValidationOptions{
IgnoreExpiredIntermediateCertificates: cfg.IgnoreExpiredIntermediateCertificates,
IgnoreExpiredRootCertificates: cfg.IgnoreExpiredRootCertificates,
IgnoreValidationResultExpiration: !cfg.ApplyCertExpirationValidationResults(),
},
)
switch {
case expirationValidationResult.IsFailed():
log.Debug().
Err(expirationValidationResult.Err()).
Int("total_certificates", expirationValidationResult.TotalCerts()).
Int("expired_certificates", expirationValidationResult.NumExpiredCerts()).
Int("expiring_certificates", expirationValidationResult.NumExpiringCerts()).
Int("valid_certificates", expirationValidationResult.NumValidCerts()).
Str("threshold_expires_warning", expirationValidationResult.WarningDateThreshold()).
Str("threshold_expires_critical", expirationValidationResult.CriticalDateThreshold()).
Msgf("%s validation failure", expirationValidationResult.CheckName())
fmt.Printf(
"- %s: %s %s\n",
expirationValidationResult.ServiceState().Label,
expirationValidationResult.Status(),
expirationValidationResult.Overview(),
)
case expirationValidationResult.IsIgnored():
log.Debug().
Msgf("%s validation ignored", expirationValidationResult.CheckName())
fmt.Printf(
"- %s: %s\n",
expirationValidationResult.ServiceState().Label,
expirationValidationResult.String(),
)
default:
log.Debug().
Int("total_certificates", expirationValidationResult.TotalCerts()).
Int("expired_certificates", expirationValidationResult.NumExpiredCerts()).
Int("expiring_certificates", expirationValidationResult.NumExpiringCerts()).
Int("valid_certificates", expirationValidationResult.NumValidCerts()).
Str("threshold_expires_warning", expirationValidationResult.WarningDateThreshold()).
Str("threshold_expires_critical", expirationValidationResult.CriticalDateThreshold()).
Msgf("%s validation successful", expirationValidationResult.CheckName())
fmt.Printf(
"- %s: %s %s\n",
expirationValidationResult.ServiceState().Label,
expirationValidationResult.Status(),
expirationValidationResult.Overview(),
)
}
textutils.PrintHeader("CERTIFICATES | CHAIN DETAILS")
// We request these details even if the user opted to disable expiration
// validation since this info provides an overview of the certificate
// chain evaluated.
fmt.Println(expirationValidationResult.StatusDetail())
// Generate text version of the certificate if requested.
if cfg.EmitCertText {
textutils.PrintHeader("CERTIFICATES | OpenSSL Text Format")
for idx, certificate := range certChain {
certText, err := certinfo.CertificateText(certificate)
if err != nil {
certText = err.Error()
}
fmt.Printf(
"\nCertificate %d of %d:\n%s\n",
idx+1,
len(certChain),
certText,
)
}
}
if len(parseAttemptLeftovers) > 0 {
textutils.PrintHeader("CERTIFICATES | UNKNOWN data in cert file")
fmt.Printf("The following data (converted to text) was found in the %q file"+
" and is provided here in case it is useful for"+
" troubleshooting purposes.\n\n",
cfg.Filename,
)
fmt.Println(string(parseAttemptLeftovers))
}
}