-
-
Notifications
You must be signed in to change notification settings - Fork 931
/
provider_viper.go
544 lines (430 loc) · 18 KB
/
provider_viper.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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
package configuration
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"net/url"
"os"
"runtime"
"time"
"github.com/google/uuid"
"github.com/pkg/errors"
"github.com/ory/x/logrusx"
"github.com/ory/x/stringsx"
"github.com/ory/x/tracing"
"github.com/ory/viper"
"github.com/ory/x/jsonx"
"github.com/ory/x/urlx"
"github.com/ory/x/viperx"
)
type ViperProvider struct {
l *logrusx.Logger
dev bool
}
var _ Provider = new(ViperProvider)
const DefaultBrowserReturnURL = "default_browser_return_url"
const DefaultSQLiteMemoryDSN = "sqlite://:memory:?_fk=true"
const UnknownVersion = "unknown version"
const (
ViperKeyDSN = "dsn"
ViperKeyCourierSMTPURL = "courier.smtp.connection_uri"
ViperKeyCourierTemplatesPath = "courier.template_override_path"
ViperKeyCourierSMTPFrom = "courier.smtp.from_address"
ViperKeySecretsDefault = "secrets.default"
ViperKeySecretsCookie = "secrets.cookie"
ViperKeyPublicBaseURL = "serve.public.base_url"
ViperKeyPublicPort = "serve.public.port"
ViperKeyPublicHost = "serve.public.host"
ViperKeyAdminBaseURL = "serve.admin.base_url"
ViperKeyAdminPort = "serve.admin.port"
ViperKeyAdminHost = "serve.admin.host"
ViperKeySessionLifespan = "session.lifespan"
ViperKeySessionSameSite = "session.cookie.same_site"
ViperKeySessionDomain = "session.cookie.domain"
ViperKeySessionPath = "session.cookie.path"
ViperKeySessionPersistentCookie = "session.cookie.persistent"
ViperKeySelfServiceStrategyConfig = "selfservice.strategies"
ViperKeySelfServiceBrowserDefaultReturnTo = "selfservice." + DefaultBrowserReturnURL
ViperKeyURLsWhitelistedReturnToDomains = "selfservice.whitelisted_return_urls"
ViperKeySelfServiceRegistrationUI = "selfservice.flows.registration.ui_url"
ViperKeySelfServiceRegistrationRequestLifespan = "selfservice.flows.registration.request_lifespan"
ViperKeySelfServiceRegistrationAfter = "selfservice.flows.registration.after"
ViperKeySelfServiceRegistrationBeforeHooks = "selfservice.flows.registration.before.hooks"
ViperKeySelfServiceLoginUI = "selfservice.flows.login.ui_url"
ViperKeySelfServiceLoginRequestLifespan = "selfservice.flows.login.request_lifespan"
ViperKeySelfServiceLoginAfter = "selfservice.flows.login.after"
ViperKeySelfServiceLoginBeforeHooks = "selfservice.flows.login.before.hooks"
ViperKeySelfServiceErrorUI = "selfservice.flows.error.ui_url"
ViperKeySelfServiceLogoutBrowserDefaultReturnTo = "selfservice.flows.logout.after." + DefaultBrowserReturnURL
ViperKeySelfServiceSettingsURL = "selfservice.flows.settings.ui_url"
ViperKeySelfServiceSettingsAfter = "selfservice.flows.settings.after"
ViperKeySelfServiceSettingsRequestLifespan = "selfservice.flows.settings.request_lifespan"
ViperKeySelfServiceSettingsPrivilegedAuthenticationAfter = "selfservice.flows.settings.privileged_session_max_age"
ViperKeySelfServiceRecoveryEnabled = "selfservice.flows.recovery.enabled"
ViperKeySelfServiceRecoveryUI = "selfservice.flows.recovery.ui_url"
ViperKeySelfServiceRecoveryRequestLifespan = "selfservice.flows.recovery.request_lifespan"
ViperKeySelfServiceRecoveryBrowserDefaultReturnTo = "selfservice.flows.recovery.after." + DefaultBrowserReturnURL
ViperKeySelfServiceVerificationEnabled = "selfservice.flows.verification.enabled"
ViperKeySelfServiceVerificationUI = "selfservice.flows.verification.ui_url"
ViperKeySelfServiceVerificationRequestLifespan = "selfservice.flows.verification.request_lifespan"
ViperKeySelfServiceVerificationBrowserDefaultReturnTo = "selfservice.flows.verification.after." + DefaultBrowserReturnURL
ViperKeyDefaultIdentitySchemaURL = "identity.default_schema_url"
ViperKeyIdentitySchemas = "identity.schemas"
ViperKeyHasherArgon2ConfigMemory = "hashers.argon2.memory"
ViperKeyHasherArgon2ConfigIterations = "hashers.argon2.iterations"
ViperKeyHasherArgon2ConfigParallelism = "hashers.argon2.parallelism"
ViperKeyHasherArgon2ConfigSaltLength = "hashers.argon2.salt_length"
ViperKeyHasherArgon2ConfigKeyLength = "hashers.argon2.key_length"
ViperKeyVersion = "version"
)
func HookStrategyKey(key, strategy string) string {
return fmt.Sprintf("%s.%s.hooks", key, strategy)
}
func NewViperProvider(l *logrusx.Logger, dev bool) *ViperProvider {
return &ViperProvider{l: l, dev: dev}
}
func (p *ViperProvider) SessionDomain() string {
return viperx.GetString(p.l, ViperKeySessionDomain, "")
}
func (p *ViperProvider) SessionPath() string {
return viperx.GetString(p.l, ViperKeySessionPath, "")
}
func (p *ViperProvider) HasherArgon2() *HasherArgon2Config {
return &HasherArgon2Config{
Memory: uint32(viperx.GetInt(p.l, ViperKeyHasherArgon2ConfigMemory, 4*1024*1024)),
Iterations: uint32(viperx.GetInt(p.l, ViperKeyHasherArgon2ConfigIterations, 4)),
Parallelism: uint8(viperx.GetInt(p.l, ViperKeyHasherArgon2ConfigParallelism, runtime.NumCPU()*2)),
SaltLength: uint32(viperx.GetInt(p.l, ViperKeyHasherArgon2ConfigSaltLength, 16)),
KeyLength: uint32(viperx.GetInt(p.l, ViperKeyHasherArgon2ConfigKeyLength, 32)),
}
}
func (p *ViperProvider) listenOn(key string) string {
fb := 4433
if key == "admin" {
fb = 4434
}
port := viperx.GetInt(p.l, "serve."+key+".port", fb)
if port < 1 {
p.l.Fatalf("serve.%s.port can not be zero or negative", key)
}
return fmt.Sprintf("%s:%d", viper.GetString("serve."+key+".host"), port)
}
func (p *ViperProvider) DefaultIdentityTraitsSchemaURL() *url.URL {
return mustParseURLFromViper(p.l, ViperKeyDefaultIdentitySchemaURL)
}
func (p *ViperProvider) IdentityTraitsSchemas() SchemaConfigs {
ds := SchemaConfig{
ID: DefaultIdentityTraitsSchemaID,
URL: p.DefaultIdentityTraitsSchemaURL().String(),
}
var b bytes.Buffer
var ss SchemaConfigs
raw := viper.Get(ViperKeyIdentitySchemas)
if raw == nil {
return SchemaConfigs{ds}
}
if err := json.NewEncoder(&b).Encode(raw); err != nil {
p.l.WithError(err).Fatalf("Unable to encode values from %s.", ViperKeyIdentitySchemas)
}
if err := jsonx.NewStrictDecoder(&b).Decode(&ss); err != nil {
p.l.WithError(err).Fatalf("Unable to decode values from %s.", ViperKeyIdentitySchemas)
}
return append(ss, ds)
}
func (p *ViperProvider) AdminListenOn() string {
return p.listenOn("admin")
}
func (p *ViperProvider) PublicListenOn() string {
return p.listenOn("public")
}
func (p *ViperProvider) DSN() string {
dsn := viperx.GetString(p.l, ViperKeyDSN, "")
if dsn == "memory" {
return DefaultSQLiteMemoryDSN
}
if len(dsn) > 0 {
return dsn
}
p.l.Fatal("dsn must be set")
return ""
}
func (p *ViperProvider) SelfServiceFlowVerificationEnabled() bool {
return viperx.GetBool(p.l, ViperKeySelfServiceVerificationEnabled, false)
}
func (p *ViperProvider) SelfServiceFlowRecoveryEnabled() bool {
return viperx.GetBool(p.l, ViperKeySelfServiceRecoveryEnabled, false)
}
func (p *ViperProvider) SelfServiceFlowLoginBeforeHooks() []SelfServiceHook {
return p.selfServiceHooks(ViperKeySelfServiceLoginBeforeHooks)
}
func (p *ViperProvider) SelfServiceFlowRegistrationBeforeHooks() []SelfServiceHook {
return p.selfServiceHooks(ViperKeySelfServiceRegistrationBeforeHooks)
}
func (p *ViperProvider) selfServiceHooks(key string) []SelfServiceHook {
var b bytes.Buffer
var hooks []SelfServiceHook
raw := viper.Get(key)
if raw == nil {
return []SelfServiceHook{}
}
if err := json.NewEncoder(&b).Encode(raw); err != nil {
p.l.WithError(err).Fatalf("Unable to decode values from configuration key: %s", key)
}
if err := jsonx.NewStrictDecoder(&b).Decode(&hooks); err != nil {
p.l.WithError(err).Fatalf("Unable to encode values from configuration key: %s", key)
}
for k := range hooks {
if len(hooks[k].Config) == 0 {
hooks[k].Config = json.RawMessage("{}")
}
}
return hooks
}
func (p *ViperProvider) SelfServiceFlowLoginAfterHooks(strategy string) []SelfServiceHook {
return p.selfServiceHooks(HookStrategyKey(ViperKeySelfServiceLoginAfter, strategy))
}
func (p *ViperProvider) SelfServiceFlowSettingsAfterHooks(strategy string) []SelfServiceHook {
return p.selfServiceHooks(HookStrategyKey(ViperKeySelfServiceSettingsAfter, strategy))
}
func (p *ViperProvider) SelfServiceFlowRegistrationAfterHooks(strategy string) []SelfServiceHook {
return p.selfServiceHooks(HookStrategyKey(ViperKeySelfServiceRegistrationAfter, strategy))
}
func (p *ViperProvider) SelfServiceStrategy(strategy string) *SelfServiceStrategy {
var s SelfServiceStrategy
if err := viperx.UnmarshalKey(ViperKeySelfServiceStrategyConfig+"."+strategy, &s); err != nil {
p.l.WithError(errors.WithStack(err)).Fatalf("Unable to encode values from configuration for strategy %s", strategy)
}
// FIXME The default value can for some reason not be set from the JSON Schema. This is a workaround.
// FIXME see https://github.com/ory/x/issues/169
if viper.Get(fmt.Sprintf("%s.%s.enabled", ViperKeySelfServiceStrategyConfig, strategy)) == nil {
switch strategy {
case "password":
fallthrough
case "profile":
fallthrough
case "link":
s.Enabled = true
}
}
if len(s.Config) == 0 {
s.Config = json.RawMessage("{}")
}
return &s
}
func (p *ViperProvider) SecretsDefault() [][]byte {
secrets := viperx.GetStringSlice(p.l, ViperKeySecretsDefault, nil)
if len(secrets) == 0 {
secrets = []string{uuid.New().String()}
viper.Set(ViperKeySecretsDefault, secrets)
}
result := make([][]byte, len(secrets))
for k, v := range secrets {
result[k] = []byte(v)
}
return result
}
func (p *ViperProvider) SecretsSession() [][]byte {
secrets := viperx.GetStringSlice(p.l, ViperKeySecretsCookie, nil)
if len(secrets) == 0 {
return p.SecretsDefault()
}
result := make([][]byte, len(secrets))
for k, v := range secrets {
result[k] = []byte(v)
}
return result
}
func (p *ViperProvider) SelfServiceBrowserDefaultReturnTo() *url.URL {
return mustParseURLFromViper(p.l, ViperKeySelfServiceBrowserDefaultReturnTo)
}
func (p *ViperProvider) guessBaseURL(keyHost, keyPort string, defaultPort int) *url.URL {
port := viperx.GetInt(p.l, keyPort, defaultPort)
host := viper.GetString(keyHost)
if host == "0.0.0.0" || len(host) == 0 {
var err error
host, err = os.Hostname()
if err != nil {
p.l.WithError(err).Warn("Unable to get hostname from system, falling back to 127.0.0.1.")
host = "127.0.0.1"
}
}
guess := url.URL{Host: fmt.Sprintf("%s:%d", host, port), Scheme: "https", Path: "/"}
if p.IsInsecureDevMode() {
guess.Scheme = "http"
}
return &guess
}
func (p *ViperProvider) baseURL(keyURL, keyHost, keyPort string, defaultPort int) *url.URL {
baseURL := viper.GetString(keyURL)
if baseURL == "" {
p.l.Warnf("Configuration key %s was left empty. Optimistically guessing the server's base URL. Please set a value to avoid problems with redirects and cookies.", ViperKeyPublicBaseURL)
return p.guessBaseURL(keyHost, keyPort, defaultPort)
}
parsed, err := url.ParseRequestURI(baseURL)
if err != nil {
p.l.WithError(err).Errorf("Configuration key %s is not a valid URL. Falling back to optimistically guessing the server's base URL. Please set a value to avoid problems with redirects and cookies.", ViperKeyPublicBaseURL)
return p.guessBaseURL(keyHost, keyPort, defaultPort)
}
return parsed
}
func (p *ViperProvider) SelfPublicURL() *url.URL {
return p.baseURL(ViperKeyPublicBaseURL, ViperKeyPublicHost, ViperKeyPublicPort, 4433)
}
func (p *ViperProvider) SelfAdminURL() *url.URL {
return p.baseURL(ViperKeyAdminBaseURL, ViperKeyAdminHost, ViperKeyAdminPort, 4434)
}
func (p *ViperProvider) CourierSMTPURL() *url.URL {
u, err := url.ParseRequestURI(viper.GetString(ViperKeyCourierSMTPURL))
if err != nil {
return new(url.URL)
}
return u
}
func (p *ViperProvider) SelfServiceFlowLoginUI() *url.URL {
return mustParseURLFromViper(p.l, ViperKeySelfServiceLoginUI)
}
func (p *ViperProvider) SelfServiceFlowSettingsUI() *url.URL {
return mustParseURLFromViper(p.l, ViperKeySelfServiceSettingsURL)
}
func (p *ViperProvider) SelfServiceFlowErrorURL() *url.URL {
return mustParseURLFromViper(p.l, ViperKeySelfServiceErrorUI)
}
func (p *ViperProvider) SelfServiceFlowRegistrationUI() *url.URL {
return mustParseURLFromViper(p.l, ViperKeySelfServiceRegistrationUI)
}
func (p *ViperProvider) SelfServiceFlowRecoveryUI() *url.URL {
return mustParseURLFromViper(p.l, ViperKeySelfServiceRecoveryUI)
}
// SessionLifespan returns nil when the value is not set.
func (p *ViperProvider) SessionLifespan() time.Duration {
return viperx.GetDuration(p.l, ViperKeySessionLifespan, time.Hour*24)
}
func (p *ViperProvider) SessionPersistentCookie() bool {
return viper.GetBool(ViperKeySessionPersistentCookie)
}
func (p *ViperProvider) SelfServiceBrowserWhitelistedReturnToDomains() (us []url.URL) {
src := viperx.GetStringSlice(p.l, ViperKeyURLsWhitelistedReturnToDomains, []string{})
for _, u := range src {
if len(u) > 0 {
us = append(us, *urlx.ParseOrFatal(p.l, u))
}
}
return us
}
func (p *ViperProvider) SelfServiceFlowLoginRequestLifespan() time.Duration {
return viperx.GetDuration(p.l, ViperKeySelfServiceLoginRequestLifespan, time.Hour)
}
func (p *ViperProvider) SelfServiceFlowSettingsFlowLifespan() time.Duration {
return viperx.GetDuration(p.l, ViperKeySelfServiceSettingsRequestLifespan, time.Hour)
}
func (p *ViperProvider) SelfServiceFlowRegistrationRequestLifespan() time.Duration {
return viperx.GetDuration(p.l, ViperKeySelfServiceRegistrationRequestLifespan, time.Hour)
}
func (p *ViperProvider) SelfServiceFlowLogoutRedirectURL() *url.URL {
redir, err := url.ParseRequestURI(
viperx.GetString(p.l, ViperKeySelfServiceLogoutBrowserDefaultReturnTo, ""))
if err != nil {
return p.SelfServiceBrowserDefaultReturnTo()
}
return redir
}
func (p *ViperProvider) CourierSMTPFrom() string {
return viperx.GetString(p.l, ViperKeyCourierSMTPFrom, "noreply@kratos.ory.sh")
}
func (p *ViperProvider) CourierTemplatesRoot() string {
return viperx.GetString(p.l, ViperKeyCourierTemplatesPath, "")
}
func mustParseURLFromViper(l *logrusx.Logger, key string) *url.URL {
u, err := url.ParseRequestURI(viper.GetString(key))
if err != nil {
l.WithError(err).Fatalf("Configuration value from key %s is not a valid URL: %s", key, viper.GetString(key))
}
return u
}
func (p *ViperProvider) TracingServiceName() string {
return viperx.GetString(p.l, "tracing.service_name", "ORY Kratos")
}
func (p *ViperProvider) TracingProvider() string {
return viperx.GetString(p.l, "tracing.provider", "", "TRACING_PROVIDER")
}
func (p *ViperProvider) TracingJaegerConfig() *tracing.JaegerConfig {
return &tracing.JaegerConfig{
LocalAgentHostPort: viperx.GetString(p.l, "tracing.providers.jaeger.local_agent_address", "", "TRACING_PROVIDER_JAEGER_LOCAL_AGENT_ADDRESS"),
SamplerType: viperx.GetString(p.l, "tracing.providers.jaeger.sampling.type", "const", "TRACING_PROVIDER_JAEGER_SAMPLING_TYPE"),
SamplerValue: viperx.GetFloat64(p.l, "tracing.providers.jaeger.sampling.value", float64(1), "TRACING_PROVIDER_JAEGER_SAMPLING_VALUE"),
SamplerServerURL: viperx.GetString(p.l, "tracing.providers.jaeger.sampling.server_url", "", "TRACING_PROVIDER_JAEGER_SAMPLING_SERVER_URL"),
Propagation: stringsx.Coalesce(
viper.GetString("JAEGER_PROPAGATION"), // Standard Jaeger client config
viperx.GetString(p.l, "tracing.providers.jaeger.propagation", "", "TRACING_PROVIDER_JAEGER_PROPAGATION"),
),
}
}
func (p *ViperProvider) IsInsecureDevMode() bool {
return p.dev
}
func (p *ViperProvider) SelfServiceFlowVerificationUI() *url.URL {
return mustParseURLFromViper(p.l, ViperKeySelfServiceVerificationUI)
}
func (p *ViperProvider) SelfServiceFlowVerificationRequestLifespan() time.Duration {
return viperx.GetDuration(p.l, ViperKeySelfServiceVerificationRequestLifespan, time.Hour)
}
func (p *ViperProvider) SelfServiceFlowVerificationReturnTo() *url.URL {
redir, err := url.ParseRequestURI(
viperx.GetString(p.l, ViperKeySelfServiceVerificationBrowserDefaultReturnTo, ""))
if err != nil {
return p.SelfServiceBrowserDefaultReturnTo()
}
return redir
}
func (p *ViperProvider) SelfServiceFlowRecoveryReturnTo() *url.URL {
redir, err := url.ParseRequestURI(
viperx.GetString(p.l, ViperKeySelfServiceRecoveryBrowserDefaultReturnTo, ""))
if err != nil {
return p.SelfServiceBrowserDefaultReturnTo()
}
return redir
}
func (p *ViperProvider) SelfServiceFlowRecoveryRequestLifespan() time.Duration {
return viperx.GetDuration(p.l, ViperKeySelfServiceRecoveryRequestLifespan, time.Hour)
}
func (p *ViperProvider) SelfServiceFlowSettingsPrivilegedSessionMaxAge() time.Duration {
return viperx.GetDuration(p.l, ViperKeySelfServiceSettingsPrivilegedAuthenticationAfter, time.Hour)
}
func (p *ViperProvider) SessionSameSiteMode() http.SameSite {
switch viperx.GetString(p.l, ViperKeySessionSameSite, "Lax") {
case "Lax":
return http.SameSiteLaxMode
case "Strict":
return http.SameSiteStrictMode
case "None":
return http.SameSiteNoneMode
}
return http.SameSiteDefaultMode
}
func (p *ViperProvider) SelfServiceFlowLoginReturnTo(strategy string) *url.URL {
return p.selfServiceReturnTo(ViperKeySelfServiceLoginAfter, strategy)
}
func (p *ViperProvider) SelfServiceFlowRegistrationReturnTo(strategy string) *url.URL {
return p.selfServiceReturnTo(ViperKeySelfServiceRegistrationAfter, strategy)
}
func (p *ViperProvider) SelfServiceFlowSettingsReturnTo(strategy string, defaultReturnTo *url.URL) *url.URL {
redir, err := url.ParseRequestURI(
viperx.GetString(p.l, ViperKeySelfServiceSettingsAfter+"."+strategy+"."+DefaultBrowserReturnURL,
viperx.GetString(p.l, ViperKeySelfServiceSettingsAfter+"."+DefaultBrowserReturnURL, "")))
if err != nil {
return defaultReturnTo
}
return redir
}
func (p *ViperProvider) selfServiceReturnTo(key string, strategy string) *url.URL {
redir, err := url.ParseRequestURI(
viperx.GetString(p.l, key+"."+strategy+"."+DefaultBrowserReturnURL, viperx.GetString(p.l, key+"."+DefaultBrowserReturnURL, "")))
if err != nil {
return p.SelfServiceBrowserDefaultReturnTo()
}
return redir
}
func (p *ViperProvider) ConfigVersion() string {
return viperx.GetString(p.l, ViperKeyVersion, UnknownVersion)
}