/
config.go
388 lines (334 loc) · 13.1 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
/*
* Copyright © 2015-2018 Aeneas Rekkas <aeneas+oss@aeneas.io>
*
* 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.
*
* @author Aeneas Rekkas <aeneas+oss@aeneas.io>
* @copyright 2015-2018 Aeneas Rekkas <aeneas+oss@aeneas.io>
* @license Apache-2.0
*/
package config
import (
"crypto/sha256"
"fmt"
"io/ioutil"
"net"
"net/http"
"net/url"
"os"
"strings"
"time"
_ "github.com/go-sql-driver/mysql"
_ "github.com/lib/pq"
"github.com/ory/fosite"
foauth2 "github.com/ory/fosite/handler/oauth2"
"github.com/ory/fosite/token/hmac"
"github.com/ory/go-convenience/stringslice"
"github.com/ory/go-convenience/urlx"
"github.com/ory/hydra/health"
"github.com/ory/hydra/metrics/prometheus"
"github.com/ory/hydra/pkg"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"gopkg.in/yaml.v2"
)
type Config struct {
// These are used by client commands
EndpointURL string `mapstructure:"HYDRA_URL" yaml:"-"`
// These are used by the host command
FrontendBindPort int `mapstructure:"PUBLIC_PORT" yaml:"-"`
FrontendBindHost string `mapstructure:"PUBLIC_HOST" yaml:"-"`
BackendBindPort int `mapstructure:"ADMIN_PORT" yaml:"-"`
BackendBindHost string `mapstructure:"ADMIN_HOST" yaml:"-"`
Issuer string `mapstructure:"OAUTH2_ISSUER_URL" yaml:"-"`
SystemSecret string `mapstructure:"SYSTEM_SECRET" yaml:"-"`
RotatedSystemSecret string `mapstructure:"ROTATED_SYSTEM_SECRET" yaml:"-"`
DatabaseURL string `mapstructure:"DATABASE_URL" yaml:"-"`
DatabasePlugin string `mapstructure:"DATABASE_PLUGIN" yaml:"-"`
ConsentURL string `mapstructure:"OAUTH2_CONSENT_URL" yaml:"-"`
LoginURL string `mapstructure:"OAUTH2_LOGIN_URL" yaml:"-"`
LogoutRedirectURL string `mapstructure:"OAUTH2_LOGOUT_REDIRECT_URL" yaml:"-"`
DefaultClientScope string `mapstructure:"OIDC_DYNAMIC_CLIENT_REGISTRATION_DEFAULT_SCOPE" yaml:"-"`
ErrorURL string `mapstructure:"OAUTH2_ERROR_URL" yaml:"-"`
AllowTLSTermination string `mapstructure:"HTTPS_ALLOW_TERMINATION_FROM" yaml:"-"`
BCryptWorkFactor int `mapstructure:"BCRYPT_COST" yaml:"-"`
AccessTokenLifespan string `mapstructure:"ACCESS_TOKEN_LIFESPAN" yaml:"-"`
ScopeStrategy string `mapstructure:"SCOPE_STRATEGY" yaml:"-"`
AuthCodeLifespan string `mapstructure:"AUTH_CODE_LIFESPAN" yaml:"-"`
IDTokenLifespan string `mapstructure:"ID_TOKEN_LIFESPAN" yaml:"-"`
ChallengeTokenLifespan string `mapstructure:"CHALLENGE_TOKEN_LIFESPAN" yaml:"-"`
CookieSecret string `mapstructure:"COOKIE_SECRET" yaml:"-"`
LogLevel string `mapstructure:"LOG_LEVEL" yaml:"-"`
LogFormat string `mapstructure:"LOG_FORMAT" yaml:"-"`
AccessControlResourcePrefix string `mapstructure:"RESOURCE_NAME_PREFIX" yaml:"-"`
SubjectTypesSupported string `mapstructure:"OIDC_SUBJECT_TYPES_SUPPORTED" yaml:"-"`
SubjectIdentifierAlgorithmSalt string `mapstructure:"OIDC_SUBJECT_TYPE_PAIRWISE_SALT" yaml:"-"`
OpenIDDiscoveryClaimsSupported string `mapstructure:"OIDC_DISCOVERY_CLAIMS_SUPPORTED" yaml:"-"`
OpenIDDiscoveryScopesSupported string `mapstructure:"OIDC_DISCOVERY_SCOPES_SUPPORTED" yaml:"-"`
OpenIDDiscoveryUserinfoEndpoint string `mapstructure:"OIDC_DISCOVERY_USERINFO_ENDPOINT" yaml:"-"`
SendOAuth2DebugMessagesToClients bool `mapstructure:"OAUTH2_SHARE_ERROR_DEBUG" yaml:"-"`
OAuth2AccessTokenStrategy string `mapstructure:"OAUTH2_ACCESS_TOKEN_STRATEGY" yaml:"-"`
ForceHTTP bool `yaml:"-"`
BuildVersion string `yaml:"-"`
BuildHash string `yaml:"-"`
BuildTime string `yaml:"-"`
logger *logrus.Logger `yaml:"-"`
prometheus *prometheus.MetricsManager `yaml:"-"`
cluster *url.URL `yaml:"-"`
oauth2Client *http.Client `yaml:"-"`
context *Context `yaml:"-"`
systemSecret []byte `yaml:"-"`
}
func (c *Config) MustValidate() {
if stringslice.Has(c.GetSubjectTypesSupported(), "pairwise") && c.OAuth2AccessTokenStrategy == "jwt" {
c.GetLogger().Fatalf(`The pairwise subject identifier algorithm is not supported by the JWT OAuth 2.0 Access Token Strategy. Please remove "pairwise" from OIDC_SUBJECT_TYPES_SUPPORTED or set OAUTH2_ACCESS_TOKEN_STRATEGY to "opaque"`)
}
if stringslice.Has(c.GetSubjectTypesSupported(), "pairwise") && len(c.SubjectIdentifierAlgorithmSalt) < 8 {
c.GetLogger().Fatalf(`The pairwise subject identifier algorithm was set but length of OIDC_SUBJECT_TYPE_PAIRWISE_SALT is too small (%d < 8), please set OIDC_SUBJECT_TYPE_PAIRWISE_SALT to a random string with 8 characters or more`, len(c.SubjectIdentifierAlgorithmSalt))
}
}
func (c *Config) GetSubjectTypesSupported() []string {
types := strings.Split(c.SubjectTypesSupported, ",")
if len(types) == 0 {
return []string{"public"}
}
return types
}
func (c *Config) GetClusterURLWithoutTailingSlashOrFail(cmd *cobra.Command) string {
endpoint := c.GetClusterURLWithoutTailingSlash(cmd)
if endpoint == "" {
fmt.Println("To execute this command, the endpoint URL must point to the URL where ORY Hydra is located. To set the endpoint URL, use flag --endpoint or environment variable HYDRA_URL or HYDRA_ADMIN_URL if an administrative command was used.")
os.Exit(1)
}
return endpoint
}
func (c *Config) GetClusterURLWithoutTailingSlash(cmd *cobra.Command) string {
if endpoint, _ := cmd.Flags().GetString("endpoint"); endpoint != "" {
return strings.TrimRight(endpoint, "/")
}
return strings.TrimRight(c.EndpointURL, "/")
}
func (c *Config) GetScopeStrategy() fosite.ScopeStrategy {
if c.ScopeStrategy == "DEPRECATED_HIERARCHICAL_SCOPE_STRATEGY" {
c.GetLogger().Warn("Using deprecated hierarchical scope strategy, consider upgrading to wildcards.")
return fosite.HierarchicScopeStrategy
}
return fosite.WildcardScopeStrategy
}
func matchesRange(r *http.Request, ranges []string) error {
ip, _, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
return errors.WithStack(err)
}
for _, rn := range ranges {
_, cidr, err := net.ParseCIDR(rn)
if err != nil {
return errors.WithStack(err)
}
addr := net.ParseIP(ip)
if cidr.Contains(addr) {
return nil
}
}
return errors.Errorf("Remote address %s does not match cidr ranges %v", ip, ranges)
}
func newLogger(c *Config) *logrus.Logger {
var (
err error
logger = logrus.New()
)
if c.LogFormat == "json" {
logger.Formatter = new(logrus.JSONFormatter)
}
logger.Level, err = logrus.ParseLevel(c.LogLevel)
if err != nil {
logger.Errorf("Couldn't parse log level: %s", c.LogLevel)
logger.Level = logrus.InfoLevel
}
return logger
}
func (c *Config) GetLogger() *logrus.Logger {
if c.logger == nil {
c.logger = newLogger(c)
}
return c.logger
}
func (c *Config) GetPrometheusMetrics() *prometheus.MetricsManager {
c.GetLogger().Info("Setting up Prometheus middleware")
if c.prometheus == nil {
c.prometheus = prometheus.NewMetricsManager(c.BuildVersion, c.BuildHash, c.BuildTime)
}
return c.prometheus
}
func (c *Config) DoesRequestSatisfyTermination(r *http.Request) error {
if c.AllowTLSTermination == "" {
return errors.New("TLS termination is not enabled")
}
if r.URL.Path == health.AliveCheckPath || r.URL.Path == health.ReadyCheckPath {
return nil
}
ranges := strings.Split(c.AllowTLSTermination, ",")
if err := matchesRange(r, ranges); err != nil {
return err
}
proto := r.Header.Get("X-Forwarded-Proto")
if proto == "" {
return errors.New("X-Forwarded-Proto header is missing")
} else if proto != "https" {
return errors.Errorf("Expected X-Forwarded-Proto header to be https, got %s", proto)
}
return nil
}
func (c *Config) GetChallengeTokenLifespan() time.Duration {
d, err := time.ParseDuration(c.ChallengeTokenLifespan)
if err != nil {
c.GetLogger().Warnf("Could not parse challenge token lifespan value (%s). Defaulting to 10m", c.AccessTokenLifespan)
return time.Minute * 10
}
return d
}
func (c *Config) GetAccessTokenLifespan() time.Duration {
d, err := time.ParseDuration(c.AccessTokenLifespan)
if err != nil {
c.GetLogger().Warnf("Could not parse access token lifespan value (%s). Defaulting to 1h", c.AccessTokenLifespan)
return time.Hour
}
return d
}
func (c *Config) GetAuthCodeLifespan() time.Duration {
d, err := time.ParseDuration(c.AuthCodeLifespan)
if err != nil {
c.GetLogger().Warnf("Could not parse auth code lifespan value (%s). Defaulting to 10m", c.AuthCodeLifespan)
return time.Minute * 10
}
return d
}
func (c *Config) GetIDTokenLifespan() time.Duration {
d, err := time.ParseDuration(c.IDTokenLifespan)
if err != nil {
c.GetLogger().Warnf("Could not parse id token lifespan value (%s). Defaulting to 1h", c.IDTokenLifespan)
return time.Hour
}
return d
}
func (c *Config) Context() *Context {
if c.context != nil {
return c.context
}
if c.DatabaseURL == "" {
c.GetLogger().Fatalf(`DATABASE_URL is not set, use "export DATABASE_URL=memory" for an in memory storage or the documented database adapters.`)
} else if c.DatabasePlugin != "" {
c.GetLogger().Infof("Database plugin set to %s", c.DatabasePlugin)
pc := &PluginConnection{Config: c, Logger: c.GetLogger()}
if err := pc.Load(); err != nil {
c.GetLogger().Fatalf("Could not connect via database plugin: %s", err)
}
}
var connection BackendConnector
scheme := "memory"
if c.DatabaseURL != "memory" {
u, err := url.Parse(c.DatabaseURL)
if err != nil {
c.GetLogger().Fatalf("Could not parse DATABASE_URL: %s", err)
}
scheme = u.Scheme
}
if backend, ok := backends[scheme]; ok {
if err := backend.Init(c.DatabaseURL, c.GetLogger()); err != nil {
c.GetLogger().Fatalf(`Could not connect to database backend: %s`, err)
}
connection = backend
} else {
c.GetLogger().Fatalf(`Unknown DSN scheme "%s" in DATABASE_URL "%s", schemes %v supported`, scheme, c.DatabaseURL, supportedSchemes())
}
c.context = &Context{
Connection: connection,
Hasher: &fosite.BCrypt{
WorkFactor: c.BCryptWorkFactor,
},
FositeStrategy: &foauth2.HMACSHAStrategy{
Enigma: &hmac.HMACStrategy{
GlobalSecret: c.GetSystemSecret(),
},
AccessTokenLifespan: c.GetAccessTokenLifespan(),
AuthorizeCodeLifespan: c.GetAuthCodeLifespan(),
},
}
return c.context
}
func (c *Config) Resolve(join ...string) *url.URL {
if c.cluster == nil {
cluster, err := url.Parse(c.EndpointURL)
c.cluster = cluster
pkg.Must(err, "Could not parse cluster url: %s", err)
}
if len(join) == 0 {
return c.cluster
}
return urlx.AppendPaths(c.cluster, join...)
}
func (c *Config) GetCookieSecret() []byte {
if c.CookieSecret != "" {
return []byte(c.CookieSecret)
}
return c.GetSystemSecret()
}
func (c *Config) GetRotatedSystemSecrets() [][]byte {
return [][]byte{[]byte(c.RotatedSystemSecret)}
}
func (c *Config) GetSystemSecret() []byte {
if len(c.systemSecret) > 0 {
return c.systemSecret
}
var secret = []byte(c.SystemSecret)
if len(secret) >= 16 {
hash := sha256.Sum256(secret)
secret = hash[:]
c.systemSecret = secret
return secret
}
if len(c.SystemSecret) > 0 {
c.GetLogger().Fatalln("System secret must be undefined or have at least 16 characters, but it has %d characters.", len(c.SystemSecret))
return nil
}
c.GetLogger().Warnf("Expected system secret to be at least %d characters long, got %d characters.", 32, len(c.SystemSecret))
c.GetLogger().Infoln("Generating a random system secret...")
var err error
secret, err = pkg.GenerateSecret(32)
pkg.Must(err, "Could not generate global secret: %s", err)
c.GetLogger().Infof("Generated system secret: %s", secret)
hash := sha256.Sum256(secret)
secret = hash[:]
c.systemSecret = secret
c.GetLogger().Warnln("WARNING: DO NOT generate system secrets in production. The secret will be leaked to the logs.")
return secret
}
func (c *Config) GetFrontendAddress() string {
return fmt.Sprintf("%s:%d", c.FrontendBindHost, c.FrontendBindPort)
}
func (c *Config) GetBackendAddress() string {
return fmt.Sprintf("%s:%d", c.BackendBindHost, c.BackendBindPort)
}
func (c *Config) Persist() error {
out, err := yaml.Marshal(c)
if err != nil {
return errors.WithStack(err)
}
c.GetLogger().Infof("Persisting config in file %s", viper.ConfigFileUsed())
if err := ioutil.WriteFile(viper.ConfigFileUsed(), out, 0700); err != nil {
return errors.Errorf(`Could not write to "%s" because: %s`, viper.ConfigFileUsed(), err)
}
return nil
}