-
Notifications
You must be signed in to change notification settings - Fork 26
/
config.go
260 lines (217 loc) · 7.62 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
package config
import (
"fmt"
"io"
"os"
"path/filepath"
"github.com/aserto-dev/certs"
"github.com/aserto-dev/go-aserto/client"
"github.com/aserto-dev/go-edge-ds/pkg/directory"
"github.com/aserto-dev/logger"
"github.com/aserto-dev/runtime"
builder "github.com/aserto-dev/service-host"
"github.com/pkg/errors"
"github.com/rs/zerolog"
)
// CommandMode -- enum type.
type CommandMode int
var (
DefaultTLSGenDir = os.ExpandEnv("$HOME/.config/topaz/certs")
CertificateSets = []string{"grpc", "gateway"}
)
// CommandMode -- enum constants.
const (
CommandModeUnknown CommandMode = 0 + iota
CommandModeRun
CommandModeBuild
)
type ServicesConfig struct {
Health struct {
ListenAddress string `json:"listen_address"`
Certificates *certs.TLSCredsConfig `json:"certs"`
} `json:"health"`
Metrics struct {
ListenAddress string `json:"listen_address"`
Certificates *certs.TLSCredsConfig `json:"certs"`
ZPages bool `json:"zpages"`
} `json:"metrics"`
Services map[string]*builder.API `json:"services"`
}
// Config holds the configuration for the app.
type Common struct {
Version int `json:"version"`
Logging logger.Config `json:"logging"`
Command struct {
Mode CommandMode
} `json:"-"`
APIConfig ServicesConfig `json:"api"`
JWT struct {
// Specifies the duration in which exp (Expiry) and nbf (Not Before)
// claims may differ by. This value should be positive.
AcceptableTimeSkewSeconds int `json:"acceptable_time_skew_seconds"`
} `json:"jwt"`
// Directory configuration
Edge directory.Config `json:"directory"`
// Authorizer directory resolver configuration
DirectoryResolver client.Config `json:"remote_directory"`
// Default OPA configuration
OPA runtime.Config `json:"opa"`
}
// LoggerConfig is a basic Config copy that gets loaded before everything else,
// so we can log during resolving configuration.
type LoggerConfig Config
// Path represents the path to a configuration file.
type Path string
// Overrider is a func that mutates configuration.
type Overrider func(*Config)
// NewConfig creates the configuration by reading env & files.
func NewConfig(configPath Path, log *zerolog.Logger, overrides Overrider, certsGenerator *certs.Generator) (*Config, error) { // nolint:funlen // default list of values can be long
newLogger := log.With().Str("component", "config").Logger()
log = &newLogger
file := "config.yaml"
if configPath != "" {
exists, err := FileExists(string(configPath))
if err != nil {
return nil, errors.Wrapf(err, "failed to determine if config file '%s' exists", configPath)
}
if !exists {
return nil, errors.Errorf("config file '%s' doesn't exist", configPath)
}
file = string(configPath)
}
configExists, err := FileExists(file)
if err != nil {
return nil, errors.Wrapf(err, "filesystem error")
}
configLoader := new(Loader)
if configExists {
configLoader, err = LoadConfiguration(file)
if err != nil {
return nil, err
}
err = validateVersion(configLoader.Configuration.Version)
if err != nil {
return nil, err
}
}
if overrides != nil {
overrides(configLoader.Configuration)
}
// This is where validation of config happens.
err = func() error {
var err error
if configLoader.Configuration.Logging.LogLevel == "" {
configLoader.Configuration.Logging.LogLevelParsed = zerolog.InfoLevel
} else {
configLoader.Configuration.Logging.LogLevelParsed, err = zerolog.ParseLevel(configLoader.Configuration.Logging.LogLevel)
if err != nil {
return errors.Wrapf(err, "logging.log_level failed to parse")
}
}
if configLoader.Configuration.JWT.AcceptableTimeSkewSeconds < 0 {
return errors.New("jwt.acceptable_time_skew_seconds must be positive or 0")
}
return configLoader.Configuration.validation()
}()
if err != nil {
return nil, errors.Wrap(err, "failed to validate config file")
}
err = setDefaultCerts(configLoader.Configuration)
if err != nil {
return nil, err
}
if certsGenerator != nil {
err = configLoader.Configuration.setupCerts(log, certsGenerator)
if err != nil {
return nil, errors.Wrap(err, "failed to setup certs")
}
}
return configLoader.Configuration, nil
}
// NewLoggerConfig creates a new LoggerConfig.
func NewLoggerConfig(configPath Path, overrides Overrider) (*logger.Config, error) {
discardLogger := zerolog.New(io.Discard)
cfg, err := NewConfig(configPath, &discardLogger, overrides, nil)
if err != nil {
return nil, errors.Wrap(err, "failed to create new config")
}
lCfg := logger.Config{
Prod: cfg.Logging.Prod,
LogLevel: cfg.Logging.LogLevel,
LogLevelParsed: cfg.Logging.LogLevelParsed,
}
return &lCfg, nil
}
func (c *Config) setupCerts(log *zerolog.Logger, certsGenerator *certs.Generator) error {
existingFiles := []string{}
for serviceName, config := range c.APIConfig.Services {
for _, file := range []string{
config.GRPC.Certs.TLSCACertPath,
config.GRPC.Certs.TLSCertPath,
config.GRPC.Certs.TLSKeyPath,
config.Gateway.Certs.TLSCACertPath,
config.Gateway.Certs.TLSCertPath,
config.Gateway.Certs.TLSKeyPath,
} {
exists, err := FileExists(file)
if err != nil {
return errors.Wrapf(err, "failed to determine if file '%s' exists (%s)", file, serviceName)
}
if !exists {
continue
}
existingFiles = append(existingFiles, file)
}
if len(existingFiles) == 0 {
err := certsGenerator.MakeDevCert(&certs.CertGenConfig{
CommonName: fmt.Sprintf("%s-grpc", serviceName),
CertKeyPath: config.GRPC.Certs.TLSKeyPath,
CertPath: config.GRPC.Certs.TLSCertPath,
CACertPath: config.GRPC.Certs.TLSCACertPath,
DefaultTLSGenDir: DefaultTLSGenDir,
})
if err != nil {
return errors.Wrapf(err, "failed to generate grpc certs (%s)", serviceName)
}
err = certsGenerator.MakeDevCert(&certs.CertGenConfig{
CommonName: fmt.Sprintf("%s-gateway", serviceName),
CertKeyPath: config.Gateway.Certs.TLSKeyPath,
CertPath: config.Gateway.Certs.TLSCertPath,
CACertPath: config.Gateway.Certs.TLSCACertPath,
DefaultTLSGenDir: DefaultTLSGenDir,
})
if err != nil {
return errors.Wrapf(err, "failed to generate gateway certs (%s)", serviceName)
}
}
log.Info().Str("service", serviceName).Msg("certs configured")
}
return nil
}
func FileExists(path string) (bool, error) {
if _, err := os.Stat(path); err == nil {
return true, nil
} else if os.IsNotExist(err) {
return false, nil
} else {
return false, errors.Wrapf(err, "failed to stat file '%s'", path)
}
}
func setDefaultCerts(cfg *Config) error {
for srvName, config := range cfg.APIConfig.Services {
if config.GRPC.ListenAddress == "" {
return errors.New(fmt.Sprintf("%s must have a grpc listen address specified", srvName))
}
if config.GRPC.Certs.TLSCACertPath == "" || config.GRPC.Certs.TLSCertPath == "" || config.GRPC.Certs.TLSKeyPath == "" {
config.GRPC.Certs.TLSKeyPath = filepath.Join(DefaultTLSGenDir, fmt.Sprintf("%s_grpc.key", srvName))
config.GRPC.Certs.TLSCertPath = filepath.Join(DefaultTLSGenDir, fmt.Sprintf("%s_grpc.crt", srvName))
config.GRPC.Certs.TLSCACertPath = filepath.Join(DefaultTLSGenDir, fmt.Sprintf("%s_grpc-ca.crt", srvName))
}
if config.Gateway.Certs.TLSCACertPath == "" || config.Gateway.Certs.TLSCertPath == "" || config.Gateway.Certs.TLSKeyPath == "" {
config.Gateway.Certs.TLSKeyPath = filepath.Join(DefaultTLSGenDir, fmt.Sprintf("%s_gateway.key", srvName))
config.Gateway.Certs.TLSCertPath = filepath.Join(DefaultTLSGenDir, fmt.Sprintf("%s_gateway.crt", srvName))
config.Gateway.Certs.TLSCACertPath = filepath.Join(DefaultTLSGenDir, fmt.Sprintf("%s_gateway-ca.crt", srvName))
}
}
return nil
}