forked from optimizely/agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
286 lines (252 loc) · 10.7 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
/****************************************************************************
* Copyright 2019-2020,2022, Optimizely, Inc. and contributors *
* *
* Licensed under the Apache License, Version 2.0 (the "License"); *
* you may not use this file except in compliance with the License. *
* You may obtain a copy of the License at *
* *
* http://www.apache.org/licenses/LICENSE-2.0 *
* *
* Unless required by applicable law or agreed to in writing, software *
* distributed under the License is distributed on an "AS IS" BASIS, *
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
* See the License for the specific language governing permissions and *
* limitations under the License. *
***************************************************************************/
// Package config contains all the configuration attributes for running Optimizely Agent
package config
import (
"time"
"github.com/rs/zerolog/log"
)
// NewDefaultConfig returns the default configuration for Optimizely Agent
func NewDefaultConfig() *AgentConfig {
config := AgentConfig{
Version: "",
Author: "Optimizely Inc.",
Name: "optimizely",
Admin: AdminConfig{
Auth: ServiceAuthConfig{
Clients: make([]OAuthClientCredentials, 0),
HMACSecrets: make([]string, 0),
TTL: 0,
JwksURL: "",
JwksUpdateInterval: 0,
},
Port: "8088",
},
API: APIConfig{
Auth: ServiceAuthConfig{
Clients: make([]OAuthClientCredentials, 0),
HMACSecrets: make([]string, 0),
TTL: 0,
JwksURL: "",
JwksUpdateInterval: 0,
},
CORS: CORSConfig{
// If AllowedOrigins is nil or empty, value is set to ["*"].
AllowedOrigins: nil,
// If AllowedMethods is nil or empty, value is set to (HEAD, GET and POST).
AllowedMethods: nil,
// Default value is [] but "Origin" is always appended to the list.
AllowedHeaders: []string{},
ExposedHeaders: []string{},
AllowedCredentials: false,
MaxAge: 300,
},
MaxConns: 0,
Port: "8080",
EnableNotifications: false,
EnableOverrides: false,
},
Log: LogConfig{
Pretty: false,
IncludeSDKKey: true,
Level: "info",
},
Client: ClientConfig{
PollingInterval: 1 * time.Minute,
BatchSize: 10,
QueueSize: 1000,
FlushInterval: 30 * time.Second,
DatafileURLTemplate: "https://cdn.optimizely.com/datafiles/%s.json",
EventURL: "https://logx.optimizely.com/v1/events",
// https://github.com/google/re2/wiki/Syntax
SdkKeyRegex: "^\\w+(:\\w+)?$",
UserProfileService: UserProfileServiceConfigs{
"default": "",
"services": map[string]interface{}{},
},
},
Runtime: RuntimeConfig{
BlockProfileRate: 0, // 0 is disabled
MutexProfileFraction: 0, // 0 is disabled
},
Server: ServerConfig{
AllowedHosts: []string{"localhost"},
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
HealthCheckPath: "/health",
CertFile: "",
KeyFile: "",
DisabledCiphers: make([]string, 0),
Host: "127.0.0.1",
Interceptors: make(map[string]interface{}),
BatchRequests: BatchRequestsConfig{
MaxConcurrency: 10,
OperationsLimit: 500,
},
},
Webhook: WebhookConfig{
Port: "8085",
},
}
return &config
}
// AgentConfig is the top level configuration struct
type AgentConfig struct {
Version string `json:"version"`
Author string `json:"author"`
Name string `json:"name"`
SDKKeys []string `yaml:"sdkKeys" json:"sdkKeys"`
Admin AdminConfig `json:"admin"`
API APIConfig `json:"api"`
Log LogConfig `json:"log"`
Client ClientConfig `json:"client"`
Runtime RuntimeConfig `json:"runtime"`
Server ServerConfig `json:"server"`
Webhook WebhookConfig `json:"webhook"`
}
// HTTPSDisabledWarning is logged when keyfile and certfile are not provided in server configuration
var HTTPSDisabledWarning = "keyfile and certfile not available, so server will use HTTP. For production deployments, it is recommended to either set keyfile and certfile for HTTPS, or run Agent behind a load balancer/reverse proxy that uses HTTPS."
// AuthDisabledWarningTemplate is used to log a warning when auth is disabled for API or Admin endpoints
var AuthDisabledWarningTemplate = "Authorization not enabled for %v endpoint. For production deployments, authorization is recommended."
// LogConfigWarnings checks this configuration and logs any relevant warnings.
func (ac *AgentConfig) LogConfigWarnings() {
if !ac.Server.isHTTPSEnabled() {
log.Warn().Msg(HTTPSDisabledWarning)
}
if !ac.API.Auth.isAuthorizationEnabled() {
log.Warn().Msgf(AuthDisabledWarningTemplate, "API")
}
if !ac.Admin.Auth.isAuthorizationEnabled() {
log.Warn().Msgf(AuthDisabledWarningTemplate, "Admin")
}
}
// UserProfileServiceConfigs defines the generic mapping of userprofileservice plugins
type UserProfileServiceConfigs map[string]interface{}
// ClientConfig holds the configuration options for the Optimizely Client.
type ClientConfig struct {
PollingInterval time.Duration `json:"pollingInterval"`
BatchSize int `json:"batchSize" default:"10"`
QueueSize int `json:"queueSize" default:"1000"`
FlushInterval time.Duration `json:"flushInterval" default:"30s"`
DatafileURLTemplate string `json:"datafileURLTemplate"`
EventURL string `json:"eventURL"`
SdkKeyRegex string `json:"sdkKeyRegex"`
UserProfileService UserProfileServiceConfigs `json:"userProfileService"`
}
// LogConfig holds the log configuration
type LogConfig struct {
Pretty bool `json:"pretty"`
IncludeSDKKey bool `json:"includeSdkKey" default:"true"`
Level string `json:"level"`
}
// PluginConfigs defines the generic mapping of middleware plugins
type PluginConfigs map[string]interface{}
// ServerConfig holds the global http server configs
type ServerConfig struct {
AllowedHosts []string `json:"allowedHosts"`
ReadTimeout time.Duration `json:"readTimeout"`
WriteTimeout time.Duration `json:"writeTimeout"`
CertFile string `json:"certFile"`
KeyFile string `json:"keyFile"`
DisabledCiphers []string `json:"disabledCiphers"`
HealthCheckPath string `json:"healthCheckPath"`
Host string `json:"host"`
BatchRequests BatchRequestsConfig `json:"batchRequests"`
Interceptors PluginConfigs `json:"interceptors"`
}
func (sc *ServerConfig) isHTTPSEnabled() bool {
return sc.KeyFile != "" && sc.CertFile != ""
}
// GetAllowedHosts returns the allowed hosts for this server. Requests whose host is not found in this slice should be
// rejected by the server.
func (sc *ServerConfig) GetAllowedHosts() []string {
return append([]string{sc.Host}, sc.AllowedHosts...)
}
// APIConfig holds the REST API configuration
type APIConfig struct {
Auth ServiceAuthConfig `json:"-"`
CORS CORSConfig `json:"cors"`
MaxConns int `json:"maxConns"`
Port string `json:"port"`
EnableNotifications bool `json:"enableNotifications"`
EnableOverrides bool `json:"enableOverrides"`
}
// BatchRequestsConfig holds the configuration for batching
type BatchRequestsConfig struct {
MaxConcurrency int `json:"maxConcurrency"`
OperationsLimit int `json:"operationsLimit"`
}
// CORSConfig holds the CORS middleware configuration
type CORSConfig struct {
AllowedOrigins []string `json:"allowedOrigins"`
AllowedMethods []string `json:"allowedMethods"`
AllowedHeaders []string `json:"allowedHeaders"`
ExposedHeaders []string `json:"exposedHeaders"`
AllowedCredentials bool `json:"allowedCredentials"`
MaxAge int `json:"maxAge"`
}
// AdminConfig holds the configuration for the admin web interface
type AdminConfig struct {
Auth ServiceAuthConfig `json:"-"`
Port string `json:"port"`
}
// WebhookConfig holds configuration for Optimizely Webhooks
type WebhookConfig struct {
Port string `json:"port"`
Projects map[int64]WebhookProject `json:"projects"`
}
// WebhookProject holds the configuration for a single Project webhook
type WebhookProject struct {
SDKKeys []string `json:"sdkKeys"`
Secret string `json:"-"`
SkipSignatureCheck bool `json:"skipSignatureCheck" default:"false"`
}
// OAuthClientCredentials are used for issuing access tokens
type OAuthClientCredentials struct {
ID string `yaml:"id"`
SecretHash string `yaml:"secretHash"`
SDKKeys []string `yaml:"sdkKeys"`
}
// ServiceAuthConfig holds the authentication configuration for a particular service
type ServiceAuthConfig struct {
Clients []OAuthClientCredentials `yaml:"clients" json:"-"`
HMACSecrets []string `yaml:"hmacSecrets" json:"-"`
TTL time.Duration `yaml:"ttl" json:"-"`
JwksURL string `yaml:"jwksURL"`
JwksUpdateInterval time.Duration `yaml:"jwksUpdateInterval"`
}
func (sc *ServiceAuthConfig) isAuthorizationEnabled() bool {
return len(sc.HMACSecrets) > 0 || sc.JwksURL != ""
}
// RuntimeConfig holds any configuration related to the native runtime package
// These should only be configured when debugging in a non-production environment.
type RuntimeConfig struct {
// SetBlockProfileRate controls the fraction of goroutine blocking events
// that are reported in the blocking profile. The profiler aims to sample
// an average of one blocking event per rate nanoseconds spent blocked.
//
// To include every blocking event in the profile, pass rate = 1.
// To turn off profiling entirely, pass rate <= 0.
BlockProfileRate int `json:"blockProfileRate"`
// mutexProfileFraction controls the fraction of mutex contention events
// that are reported in the mutex profile. On average 1/rate events are
// reported. The previous rate is returned.
//
// To turn off profiling entirely, pass rate 0.
// To just read the current rate, pass rate < 0.
// (For n>1 the details of sampling may change.)
MutexProfileFraction int `json:"mutexProfileFraction"`
}