-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
156 lines (131 loc) · 3.93 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
package main
import (
"flag"
"github.com/gustavoluvizotto/cert-validator/input"
"github.com/gustavoluvizotto/cert-validator/prepare"
"github.com/gustavoluvizotto/cert-validator/result"
"github.com/gustavoluvizotto/cert-validator/rootstores"
"github.com/gustavoluvizotto/cert-validator/validator"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"os"
"time"
)
func main() {
// parse command line arguments
var inputCsv string
flag.StringVar(&inputCsv,
"input-csv",
"",
"The input file in CSV format")
var inputParquet string
flag.StringVar(&inputParquet,
"input-parquet",
"example/input-sample.parquet",
"The input file in Parquet format")
var logFile string
flag.StringVar(&logFile,
"log-file",
"",
"The log file in JSON format")
var output string
flag.StringVar(&output,
"output",
"",
"The output file in Parquet format (provide extension)")
var prep bool
flag.BoolVar(&prep,
"prep",
false,
"Preparation step; download root stores")
var rM bool
flag.BoolVar(&rM,
"rm",
false,
"Remove temporary files")
var rootCAFile string
flag.StringVar(&rootCAFile,
"root-ca-file",
"",
"Use root store from PEM file")
var scanDateArg string
flag.StringVar(&scanDateArg,
"scan-date",
"",
"Date the certificates were collected. Format: YYYYMMDD")
// default value is no verbosity
var verbosity int
flag.IntVar(&verbosity,
"v",
0,
"Verbosity level (1 or 2)")
flag.Parse()
log.Logger = log.Output(zerolog.NewConsoleWriter())
if verbosity >= 2 {
zerolog.SetGlobalLevel(zerolog.DebugLevel)
} else if verbosity == 1 {
zerolog.SetGlobalLevel(zerolog.InfoLevel)
} else {
zerolog.SetGlobalLevel(zerolog.WarnLevel)
}
if logFile != "" {
fh, err := os.Create(logFile)
if err != nil {
log.Fatal().Err(err).Str("file", logFile).Msg("Error creating log file")
}
log.Logger = log.Output(fh)
}
if scanDateArg == "" {
log.Fatal().Msg("Scan date is required")
}
scanDate, err := time.Parse("20060102", scanDateArg)
if err != nil {
log.Fatal().Str("scanDateArg", scanDateArg).Msg("Incorrect format for scan date argument. Use YYYYMMDD")
}
if prep {
err := prepare.RetrieveAllRootStores(scanDate)
if err != nil {
log.Fatal().Err(err).Msg("Error retrieving root stores")
}
return
}
if output == "" {
log.Fatal().Msg("Output file is required")
}
logInputs(inputCsv, inputParquet, logFile, output, rM, rootCAFile, scanDateArg, verbosity)
var certChains []input.CertChain
if inputCsv != "" {
certChains = input.LoadCsv(inputCsv)
} else {
certChains = input.LoadParquet(inputParquet)
}
if certChains == nil || len(certChains) == 0 {
log.Fatal().Msg("No certificate chain to validate")
}
startTime := time.Now()
validChainChan := validateChain(certChains, rootCAFile, scanDate)
nrChains := len(certChains)
result.ConsumeResultChannel(*validChainChan, nrChains, output)
endTime := time.Now()
if rM {
rootstores.RemoveDownloadedRootCertificates()
}
log.Info().Str("validationTime", endTime.Sub(startTime).String()).Msg("Validation time")
}
func logInputs(inputCsv string, inputParquet string, logFile string, output string, rM bool, rootCAFile string, scanDate string, verbosity int) {
log.Info().Str("input-csv", inputCsv).Str("input-parquet", inputParquet).Str("log-file", logFile).Str("output", output).Bool("rm", rM).Str("root-ca-file", rootCAFile).Str("scan-date", scanDate).Int("verbosity", verbosity).Msg("Inputs")
}
func validateChain(certChains []input.CertChain, rootCAFile string, scanDate time.Time) *chan result.ValidationResult {
err := rootstores.PoolRootCerts(rootCAFile, scanDate)
if err != nil {
log.Fatal().Err(err).Msg("Error loading root certificates")
}
if rootstores.IsEmptyRootCertsPool() {
log.Fatal().Msg("No root certificates loaded")
}
validChainChan := make(chan result.ValidationResult, len(certChains))
for _, certChain := range certChains {
go validator.ValidateChainPem(certChain, validChainChan, scanDate)
}
return &validChainChan
}