-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathconfig.go
More file actions
224 lines (203 loc) · 6.51 KB
/
config.go
File metadata and controls
224 lines (203 loc) · 6.51 KB
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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package config
import (
"flag"
"fmt"
"os"
"os/user"
"strconv"
"github.com/golang/glog"
"gopkg.in/ini.v1"
)
type CTConfig struct {
RemoteSettingsURL *string
CTLogMetadata *string
CertPath *string
GoogleProjectId *string
RedisHost *string
RedisTimeout *string
BatchSize *uint64
NumThreads *int
RunForever *bool
LogExpiredEntries *bool
SavePeriod *string
PollingDelay *uint64
StatsRefreshPeriod *string
Config *string
StatsDHost *string
StatsDPort *int
HealthAddr *string
RemoteSettingsUpdateInterval *uint64
}
func confInt(p *int, section *ini.Section, key string, def int) {
val, ok := os.LookupEnv(key)
if ok {
i, err := strconv.ParseInt(val, 10, 32)
if err == nil {
*p = int(i)
return
}
}
*p = def
if section != nil {
k := section.Key(key)
if k != nil {
v, err := k.Int()
if err == nil {
*p = v
}
}
}
}
func confUint64(p *uint64, section *ini.Section, key string, def uint64) {
// Final override is the environment variable
val, ok := os.LookupEnv(key)
if ok {
u, err := strconv.ParseUint(val, 10, 64)
if err == nil {
*p = u
return
}
}
// Assume default
*p = def
if section != nil {
k := section.Key(key)
if k != nil {
v, err := k.Uint64()
if err == nil {
*p = v
}
}
}
}
func confBool(p *bool, section *ini.Section, key string, def bool) {
// Final override is the environment variable
val, ok := os.LookupEnv(key)
if ok {
b, err := strconv.ParseBool(val)
if err == nil {
*p = b
return
}
}
*p = def
if section != nil {
k := section.Key(key)
if k != nil {
v, err := k.Bool()
if err == nil {
*p = v
}
}
}
}
func confString(p *string, section *ini.Section, key string, def string) {
*p = def
if section != nil {
k := section.Key(key)
if k != nil && len(k.String()) > 0 {
*p = k.String()
}
}
val, ok := os.LookupEnv(key)
if ok {
*p = val
}
}
func NewCTConfig() *CTConfig {
return &CTConfig{
BatchSize: new(uint64),
RemoteSettingsURL: new(string),
CTLogMetadata: new(string),
NumThreads: new(int),
LogExpiredEntries: new(bool),
RunForever: new(bool),
CertPath: new(string),
GoogleProjectId: new(string),
StatsDHost: new(string),
StatsDPort: new(int),
HealthAddr: new(string),
RedisHost: new(string),
RedisTimeout: new(string),
SavePeriod: new(string),
StatsRefreshPeriod: new(string),
PollingDelay: new(uint64),
RemoteSettingsUpdateInterval: new(uint64),
}
}
func (c *CTConfig) Init() {
var confFile string
var flagBatchSize uint64
flag.StringVar(&confFile, "config", "", "configuration .ini file")
flag.Uint64Var(&flagBatchSize, "batchSize", 0, "limit on number of CT log entries to download per job")
flag.Parse()
if len(confFile) == 0 {
userObj, err := user.Current()
if err == nil {
defPath := fmt.Sprintf("%s/.ct-fetch.ini", userObj.HomeDir)
if _, err := os.Stat(defPath); err == nil {
confFile = defPath
}
}
}
// First, check the config file, which might have come from a CLI paramater
var section *ini.Section
if len(confFile) > 0 {
cfg, err := ini.Load(confFile)
if err == nil {
glog.Infof("Loaded config file from %s\n", confFile)
section = cfg.Section("")
} else {
glog.Errorf("Could not load config file: %s\n", err)
}
}
// Fill in values, where conf file < env vars
confUint64(c.BatchSize, section, "batchSize", 4096)
confString(c.RemoteSettingsURL, section, "remoteSettingsURL", "")
confString(c.CTLogMetadata, section, "ctLogMetadata", "")
confInt(c.NumThreads, section, "numThreads", 1)
confBool(c.LogExpiredEntries, section, "logExpiredEntries", false)
confBool(c.RunForever, section, "runForever", false)
confUint64(c.PollingDelay, section, "pollingDelay", 600)
confUint64(c.RemoteSettingsUpdateInterval, section, "remoteSettingsUpdateInterval", 3600)
confString(c.SavePeriod, section, "savePeriod", "15m")
confString(c.CertPath, section, "certPath", "")
confString(c.GoogleProjectId, section, "googleProjectId", "")
confString(c.RedisHost, section, "redisHost", "")
confString(c.RedisTimeout, section, "redisTimeout", "5s")
confString(c.StatsRefreshPeriod, section, "statsRefreshPeriod", "10m")
confString(c.StatsDHost, section, "statsdHost", "")
confInt(c.StatsDPort, section, "statsdPort", 8125)
confString(c.HealthAddr, section, "healthAddr", ":8080")
// Finally, CLI flags override
if flagBatchSize > 0 {
*c.BatchSize = flagBatchSize
}
}
func (c *CTConfig) Usage() {
flag.Usage()
fmt.Println("")
fmt.Println("Environment variable or config file directives:")
fmt.Println("")
fmt.Println("The certPath and redisHost variables are mandatory:")
fmt.Println("certPath = path under which to store persistent certificate data")
fmt.Println("redisHost = address:port of the Redis instance")
fmt.Println("")
fmt.Println("Options:")
fmt.Println("remoteSettingsURL = The base url for remote settings requests")
fmt.Println("ctLogMetadata = A string containing a JSON array of CTLogMetadata objects, for debugging")
fmt.Println("googleProjectId = Google Cloud Platform Project ID, used for stackdriver logging")
fmt.Println("runForever = Run forever, pausing `pollingDelay` seconds between runs")
fmt.Println("pollingDelay= Wait time in seconds between polls. Jitter will be added.")
fmt.Println("logExpiredEntries = Add expired entries to the database")
fmt.Println("numThreads = Use this many threads for normal operations")
fmt.Println("savePeriod = Duration between state saves, e.g. 15m")
fmt.Println("statsRefreshPeriod = Period between stats being dumped to stderr, only if statsdDhost and statsdPort are not set")
fmt.Println("statsdHost = host for StatsD information")
fmt.Println("statsdPort = port for StatsD information")
fmt.Println("redisTimeout = Timeout for operations from Redis, e.g. 10s")
fmt.Println("healthAddr = Address to host the /health information http endpoint, e.g. localhost:8080")
}