-
Notifications
You must be signed in to change notification settings - Fork 147
/
config.go
470 lines (407 loc) · 14.8 KB
/
config.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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
package config
import (
"crypto/tls"
"errors"
"fmt"
"io/ioutil"
"log"
"math"
"net"
"os"
"sort"
"strconv"
"strings"
"time"
"github.com/buchgr/bazel-remote/cache"
"github.com/buchgr/bazel-remote/cache/s3proxy"
"github.com/urfave/cli/v2"
yaml "gopkg.in/yaml.v2"
)
// GoogleCloudStorageConfig stores the configuration of a GCS proxy backend.
type GoogleCloudStorageConfig struct {
Bucket string `yaml:"bucket"`
UseDefaultCredentials bool `yaml:"use_default_credentials"`
JSONCredentialsFile string `yaml:"json_credentials_file"`
}
// HTTPBackendConfig stores the configuration for a HTTP proxy backend.
type HTTPBackendConfig struct {
BaseURL string `yaml:"url"`
}
// Config holds the top-level configuration for bazel-remote.
type Config struct {
HTTPAddress string `yaml:"http_address"`
GRPCAddress string `yaml:"grpc_address"`
ProfileAddress string `yaml:"profile_address"`
Dir string `yaml:"dir"`
MaxSize int `yaml:"max_size"`
StorageMode string `yaml:"storage_mode"`
HtpasswdFile string `yaml:"htpasswd_file"`
TLSCaFile string `yaml:"tls_ca_file"`
TLSCertFile string `yaml:"tls_cert_file"`
TLSKeyFile string `yaml:"tls_key_file"`
AllowUnauthenticatedReads bool `yaml:"allow_unauthenticated_reads"`
S3CloudStorage *S3CloudStorageConfig `yaml:"s3_proxy,omitempty"`
GoogleCloudStorage *GoogleCloudStorageConfig `yaml:"gcs_proxy,omitempty"`
HTTPBackend *HTTPBackendConfig `yaml:"http_proxy,omitempty"`
NumUploaders int `yaml:"num_uploaders"`
MaxQueuedUploads int `yaml:"max_queued_uploads"`
IdleTimeout time.Duration `yaml:"idle_timeout"`
DisableHTTPACValidation bool `yaml:"disable_http_ac_validation"`
DisableGRPCACDepsCheck bool `yaml:"disable_grpc_ac_deps_check"`
EnableACKeyInstanceMangling bool `yaml:"enable_ac_key_instance_mangling"`
EnableEndpointMetrics bool `yaml:"enable_endpoint_metrics"`
MetricsDurationBuckets []float64 `yaml:"endpoint_metrics_duration_buckets"`
ExperimentalRemoteAssetAPI bool `yaml:"experimental_remote_asset_api"`
HTTPReadTimeout time.Duration `yaml:"http_read_timeout"`
HTTPWriteTimeout time.Duration `yaml:"http_write_timeout"`
AccessLogLevel string `yaml:"access_log_level"`
MaxBlobSize int64 `yaml:"max_blob_size"`
MaxProxyBlobSize int64 `yaml:"max_proxy_blob_size"`
// Fields that are created by combinations of the flags above.
ProxyBackend cache.Proxy
TLSConfig *tls.Config
AccessLogger *log.Logger
ErrorLogger *log.Logger
}
type YamlConfig struct {
Config `yaml:",inline"`
// Deprecated fields, retained for backwards compatibility when
// parsing config files.
// If set, these will be used to populate *Address fields.
Host string `yaml:"host"`
Port int `yaml:"port"`
GRPCPort int `yaml:"grpc_port"`
ProfileHost string `yaml:"profile_host"`
ProfilePort int `yaml:"profile_port"`
}
const disabledGRPCListener = "none"
var defaultDurationBuckets = []float64{.5, 1, 2.5, 5, 10, 20, 40, 80, 160, 320}
// newFromArgs returns a validated Config with the specified values, and
// an error if there were any problems with the validation.
func newFromArgs(dir string, maxSize int, storageMode string,
httpAddress string, grpcAddress string,
profileAddress string,
htpasswdFile string,
maxQueuedUploads int,
numUploaders int,
tlsCaFile string,
tlsCertFile string,
tlsKeyFile string,
allowUnauthenticatedReads bool,
idleTimeout time.Duration,
hc *HTTPBackendConfig,
gcs *GoogleCloudStorageConfig,
s3 *S3CloudStorageConfig,
disableHTTPACValidation bool,
disableGRPCACDepsCheck bool,
enableACKeyInstanceMangling bool,
enableEndpointMetrics bool,
experimentalRemoteAssetAPI bool,
httpReadTimeout time.Duration,
httpWriteTimeout time.Duration,
accessLogLevel string,
maxBlobSize int64,
maxProxyBlobSize int64) (*Config, error) {
c := Config{
HTTPAddress: httpAddress,
GRPCAddress: grpcAddress,
ProfileAddress: profileAddress,
Dir: dir,
MaxSize: maxSize,
StorageMode: storageMode,
HtpasswdFile: htpasswdFile,
MaxQueuedUploads: maxQueuedUploads,
NumUploaders: numUploaders,
TLSCaFile: tlsCaFile,
TLSCertFile: tlsCertFile,
TLSKeyFile: tlsKeyFile,
AllowUnauthenticatedReads: allowUnauthenticatedReads,
S3CloudStorage: s3,
GoogleCloudStorage: gcs,
HTTPBackend: hc,
IdleTimeout: idleTimeout,
DisableHTTPACValidation: disableHTTPACValidation,
DisableGRPCACDepsCheck: disableGRPCACDepsCheck,
EnableACKeyInstanceMangling: enableACKeyInstanceMangling,
EnableEndpointMetrics: enableEndpointMetrics,
MetricsDurationBuckets: defaultDurationBuckets,
ExperimentalRemoteAssetAPI: experimentalRemoteAssetAPI,
HTTPReadTimeout: httpReadTimeout,
HTTPWriteTimeout: httpWriteTimeout,
AccessLogLevel: accessLogLevel,
MaxBlobSize: maxBlobSize,
MaxProxyBlobSize: maxProxyBlobSize,
}
err := validateConfig(&c)
if err != nil {
return nil, err
}
return &c, nil
}
// newFromYamlFile reads configuration settings from a YAML file then returns
// a validated Config with those settings, and an error if there were any
// problems.
func newFromYamlFile(path string) (*Config, error) {
file, err := os.Open(path)
if err != nil {
return nil, fmt.Errorf("Failed to open config file '%s': %v", path, err)
}
defer file.Close()
data, err := ioutil.ReadAll(file)
if err != nil {
return nil, fmt.Errorf("Failed to read config file '%s': %v", path, err)
}
return newFromYaml(data)
}
func newFromYaml(data []byte) (*Config, error) {
yc := YamlConfig{
Config: Config{
StorageMode: "zstd",
NumUploaders: 100,
MaxQueuedUploads: 1000000,
MaxBlobSize: math.MaxInt64,
MaxProxyBlobSize: math.MaxInt64,
MetricsDurationBuckets: defaultDurationBuckets,
AccessLogLevel: "all",
},
}
err := yaml.Unmarshal(data, &yc)
if err != nil {
return nil, fmt.Errorf("Failed to parse YAML config: %v", err)
}
c := yc.Config
if c.HTTPAddress == "" {
c.HTTPAddress = net.JoinHostPort(yc.Host, strconv.Itoa(yc.Port))
}
if c.GRPCAddress == "" && yc.GRPCPort > 0 {
c.GRPCAddress = net.JoinHostPort(yc.Host, strconv.Itoa(yc.GRPCPort))
}
if c.ProfileAddress == "" && yc.ProfilePort > 0 {
c.ProfileAddress = net.JoinHostPort(yc.ProfileHost, strconv.Itoa(yc.ProfilePort))
}
if c.MetricsDurationBuckets != nil {
sort.Float64s(c.MetricsDurationBuckets)
}
err = validateConfig(&c)
if err != nil {
return nil, err
}
return &c, nil
}
func validateConfig(c *Config) error {
if c.Dir == "" {
return errors.New("The 'dir' flag/key is required")
}
if c.MaxSize <= 0 {
return errors.New("The 'max_size' flag/key must be set to a value > 0")
}
if c.StorageMode != "zstd" && c.StorageMode != "uncompressed" {
return errors.New("storage_mode must be set to either \"zstd\" or \"uncompressed\"")
}
proxyCount := 0
if c.S3CloudStorage != nil {
proxyCount++
}
if c.HTTPBackend != nil {
proxyCount++
}
if c.GoogleCloudStorage != nil {
proxyCount++
}
if proxyCount > 1 {
return errors.New("At most one of the S3/GCS/HTTP proxy backends is allowed")
}
var httpPort string
if strings.HasPrefix(c.HTTPAddress, "unix://") {
if c.HTTPAddress[len("unix://"):] == "" {
return errors.New("'http_address' Unix socket specification is missing a socket path")
}
} else {
var err error
_, httpPort, err = net.SplitHostPort(c.HTTPAddress)
if err != nil {
return errors.New("'http_address' must either be formatted as [host]:port or unix://socket.path")
}
}
if c.GRPCAddress != "" && c.GRPCAddress != disabledGRPCListener {
if strings.HasPrefix(c.GRPCAddress, "unix://") {
if c.GRPCAddress[len("unix://"):] == "" {
return errors.New("'grpc_address' Unix socket specification is missing a socket path")
}
} else {
_, grpcPort, err := net.SplitHostPort(c.GRPCAddress)
if err != nil {
return errors.New("'grpc_address' must either be formatted as [host]:port or unix://socket.path")
}
if httpPort != "" && grpcPort != "" && httpPort == grpcPort {
return fmt.Errorf("HTTP and gRPC server TCP ports conflict: %s", httpPort)
}
}
}
if c.ProfileAddress != "" && c.ProfileAddress != disabledGRPCListener {
if strings.HasPrefix(c.ProfileAddress, "unix://") {
if c.ProfileAddress[len("unix://"):] == "" {
return errors.New("'profile_address' Unix socket specification is missing a socket path")
}
}
}
if c.GRPCAddress == disabledGRPCListener && c.ExperimentalRemoteAssetAPI {
return errors.New("Remote Asset API support depends on gRPC being enabled")
}
if (c.TLSCertFile != "" && c.TLSKeyFile == "") || (c.TLSCertFile == "" && c.TLSKeyFile != "") {
return errors.New("When enabling TLS one must specify both " +
"'tls_key_file' and 'tls_cert_file'")
}
if c.TLSCaFile != "" && (c.TLSCertFile == "" || c.TLSKeyFile == "") {
return errors.New("When enabling mTLS (authenticating client " +
"certificates) the server must have it's own 'tls_key_file' " +
"and 'tls_cert_file' specified.")
}
if c.AllowUnauthenticatedReads && c.TLSCaFile == "" && c.HtpasswdFile == "" {
return errors.New("AllowUnauthenticatedReads setting is only available when authentication is enabled")
}
if c.MaxBlobSize <= 0 {
return errors.New("The 'max_blob_size' flag/key must be a positive integer")
}
if c.MaxProxyBlobSize <= 0 {
return errors.New("The 'max_proxy_blob_size' flag/key must be a positive integer")
}
if c.GoogleCloudStorage != nil && c.HTTPBackend != nil && c.S3CloudStorage != nil {
return errors.New("One can specify at most one proxying backend")
}
if c.GoogleCloudStorage != nil {
if c.GoogleCloudStorage.Bucket == "" {
return errors.New("The 'bucket' field is required for 'gcs_proxy'")
}
}
if c.HTTPBackend != nil {
if c.HTTPBackend.BaseURL == "" {
return errors.New("The 'url' field is required for 'http_proxy'")
}
}
if c.S3CloudStorage != nil {
if !s3proxy.IsValidAuthMethod(c.S3CloudStorage.AuthMethod) {
return fmt.Errorf("invalid s3.auth_method: %s", c.S3CloudStorage.AuthMethod)
}
if c.S3CloudStorage.KeyVersion != nil && *c.S3CloudStorage.KeyVersion != 2 {
return fmt.Errorf("s3.key_version (deprecated) must be 2, found %d", c.S3CloudStorage.KeyVersion)
}
}
if c.MetricsDurationBuckets != nil {
duplicates := make(map[float64]bool)
for _, bucket := range c.MetricsDurationBuckets {
_, dupe := duplicates[bucket]
if dupe {
return errors.New("'endpoint_metrics_duration_buckets' must not contain duplicate buckets")
}
duplicates[bucket] = true
}
}
switch c.AccessLogLevel {
case "none", "all":
default:
return errors.New("'access_log_level' must be set to either \"none\" or \"all\"")
}
return nil
}
func Get(ctx *cli.Context) (*Config, error) {
// Get a Config with all the basic fields set.
cfg, err := get(ctx)
if err != nil {
return nil, err
}
// Set the non-basic fields...
err = cfg.setLogger()
if err != nil {
return nil, err
}
err = cfg.setProxy()
if err != nil {
return nil, err
}
err = cfg.setTLSConfig()
if err != nil {
return nil, err
}
return cfg, nil
}
// Return a Config with all the basic fields set.
func get(ctx *cli.Context) (*Config, error) {
configFile := ctx.String("config_file")
if configFile != "" {
return newFromYamlFile(configFile)
}
httpAddress := ctx.String("http_address")
if httpAddress == "" {
httpAddress = net.JoinHostPort(ctx.String("host"), strconv.Itoa(ctx.Int("port")))
}
grpcAddress := ctx.String("grpc_address")
if grpcAddress == "" && ctx.Int("grpc_port") > 0 {
grpcAddress = net.JoinHostPort(ctx.String("host"), strconv.Itoa(ctx.Int("grpc_port")))
}
profileAddress := ctx.String("profile_address")
if profileAddress == "" && ctx.Int("profile_port") > 0 {
profileAddress = net.JoinHostPort(ctx.String("profile_host"), strconv.Itoa(ctx.Int("profile_port")))
} else if profileAddress == "none" {
profileAddress = ""
}
var s3 *S3CloudStorageConfig
if ctx.String("s3.bucket") != "" {
s3 = &S3CloudStorageConfig{
Endpoint: ctx.String("s3.endpoint"),
Bucket: ctx.String("s3.bucket"),
Prefix: ctx.String("s3.prefix"),
AuthMethod: ctx.String("s3.auth_method"),
AccessKeyID: ctx.String("s3.access_key_id"),
SecretAccessKey: ctx.String("s3.secret_access_key"),
DisableSSL: ctx.Bool("s3.disable_ssl"),
IAMRoleEndpoint: ctx.String("s3.iam_role_endpoint"),
Region: ctx.String("s3.region"),
AWSProfile: ctx.String("s3.aws_profile"),
AWSSharedCredentialsFile: ctx.String("s3.aws_shared_credentials_file"),
}
}
var hc *HTTPBackendConfig
if ctx.String("http_proxy.url") != "" {
hc = &HTTPBackendConfig{
BaseURL: ctx.String("http_proxy.url"),
}
}
var gcs *GoogleCloudStorageConfig
if ctx.String("gcs_proxy.bucket") != "" {
gcs = &GoogleCloudStorageConfig{
Bucket: ctx.String("gcs_proxy.bucket"),
UseDefaultCredentials: ctx.Bool("gcs_proxy.use_default_credentials"),
JSONCredentialsFile: ctx.String("gcs_proxy.json_credentials_file"),
}
}
return newFromArgs(
ctx.String("dir"),
ctx.Int("max_size"),
ctx.String("storage_mode"),
httpAddress,
grpcAddress,
profileAddress,
ctx.String("htpasswd_file"),
ctx.Int("max_queued_uploads"),
ctx.Int("num_uploaders"),
ctx.String("tls_ca_file"),
ctx.String("tls_cert_file"),
ctx.String("tls_key_file"),
ctx.Bool("allow_unauthenticated_reads"),
ctx.Duration("idle_timeout"),
hc,
gcs,
s3,
ctx.Bool("disable_http_ac_validation"),
ctx.Bool("disable_grpc_ac_deps_check"),
ctx.Bool("enable_ac_key_instance_mangling"),
ctx.Bool("enable_endpoint_metrics"),
ctx.Bool("experimental_remote_asset_api"),
ctx.Duration("http_read_timeout"),
ctx.Duration("http_write_timeout"),
ctx.String("access_log_level"),
ctx.Int64("max_blob_size"),
ctx.Int64("max_proxy_blob_size"),
)
}