-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathconfig.go
396 lines (345 loc) · 16.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
389
390
391
392
393
394
395
396
/*
Copyright © 2024 Acronis International GmbH.
Released under MIT license.
*/
package authkit
import (
"fmt"
"net/url"
"time"
"github.com/acronis/go-appkit/config"
"github.com/acronis/go-authkit/idptoken"
"github.com/acronis/go-authkit/internal/idputil"
"github.com/acronis/go-authkit/jwks"
"github.com/acronis/go-authkit/jwt"
)
const cfgDefaultKeyPrefix = "auth"
const (
cfgKeyHTTPClientRequestTimeout = "httpClient.requestTimeout"
cfgKeyGRPCClientRequestTimeout = "grpcClient.requestTimeout"
cfgKeyJWTTrustedIssuers = "jwt.trustedIssuers"
cfgKeyJWTTrustedIssuerURLs = "jwt.trustedIssuerUrls"
cfgKeyJWTRequireAudience = "jwt.requireAudience"
cfgKeyJWTExceptedAudience = "jwt.expectedAudience"
cfgKeyJWTClaimsCacheEnabled = "jwt.claimsCache.enabled"
cfgKeyJWTClaimsCacheMaxEntries = "jwt.claimsCache.maxEntries"
cfgKeyJWKSCacheUpdateMinInterval = "jwks.cache.updateMinInterval"
cfgKeyIntrospectionEnabled = "introspection.enabled"
cfgKeyIntrospectionEndpoint = "introspection.endpoint"
cfgKeyIntrospectionGRPCEndpoint = "introspection.grpc.endpoint"
cfgKeyIntrospectionGRPCTLSEnabled = "introspection.grpc.tls.enabled"
cfgKeyIntrospectionGRPCTLSCACert = "introspection.grpc.tls.caCert"
cfgKeyIntrospectionGRPCTLSClientCert = "introspection.grpc.tls.clientCert"
cfgKeyIntrospectionGRPCTLSClientKey = "introspection.grpc.tls.clientKey"
cfgKeyIntrospectionAccessTokenScope = "introspection.accessTokenScope" // nolint:gosec // false positive
cfgKeyIntrospectionClaimsCacheEnabled = "introspection.claimsCache.enabled"
cfgKeyIntrospectionClaimsCacheMaxEntries = "introspection.claimsCache.maxEntries"
cfgKeyIntrospectionClaimsCacheTTL = "introspection.claimsCache.ttl"
cfgKeyIntrospectionNegativeCacheEnabled = "introspection.negativeCache.enabled"
cfgKeyIntrospectionNegativeCacheMaxEntries = "introspection.negativeCache.maxEntries"
cfgKeyIntrospectionNegativeCacheTTL = "introspection.negativeCache.ttl"
cfgKeyIntrospectionEndpointDiscoveryCacheEnabled = "introspection.endpointDiscoveryCache.enabled"
cfgKeyIntrospectionEndpointDiscoveryCacheMaxEntries = "introspection.endpointDiscoveryCache.maxEntries"
cfgKeyIntrospectionEndpointDiscoveryCacheTTL = "introspection.endpointDiscoveryCache.ttl"
)
// Config represents a set of configuration parameters for authentication and authorization.
type Config struct {
HTTPClient HTTPClientConfig `mapstructure:"httpClient" yaml:"httpClient" json:"httpClient"`
GRPCClient GRPCClientConfig `mapstructure:"grpcClient" yaml:"grpcClient"`
JWT JWTConfig `mapstructure:"jwt" yaml:"jwt" json:"jwt"`
JWKS JWKSConfig `mapstructure:"jwks" yaml:"jwks" json:"jwks"`
Introspection IntrospectionConfig `mapstructure:"introspection" yaml:"introspection" json:"introspection"`
keyPrefix string
}
var _ config.Config = (*Config)(nil)
var _ config.KeyPrefixProvider = (*Config)(nil)
// ConfigOption is a type for functional options for the Config.
type ConfigOption func(*configOptions)
type configOptions struct {
keyPrefix string
}
// WithKeyPrefix returns a ConfigOption that sets a key prefix for parsing configuration parameters.
// This prefix will be used by config.Loader.
func WithKeyPrefix(keyPrefix string) ConfigOption {
return func(o *configOptions) {
o.keyPrefix = keyPrefix
}
}
// NewConfig creates a new instance of the Config.
func NewConfig(options ...ConfigOption) *Config {
var opts = configOptions{keyPrefix: cfgDefaultKeyPrefix} // cfgDefaultKeyPrefix is used here for backward compatibility
for _, opt := range options {
opt(&opts)
}
return &Config{keyPrefix: opts.keyPrefix}
}
// NewConfigWithKeyPrefix creates a new instance of the Config with a key prefix.
// This prefix will be used by config.Loader.
//
// Deprecated: use NewConfig with WithKeyPrefix instead.
// Note that the WithKeyPrefix option doesn't add "auth" to the key prefix by default as NewConfigWithKeyPrefix does.
func NewConfigWithKeyPrefix(keyPrefix string) *Config {
if keyPrefix != "" {
keyPrefix += "."
}
keyPrefix += cfgDefaultKeyPrefix // cfgDefaultKeyPrefix is added here for backward compatibility
return &Config{keyPrefix: keyPrefix}
}
// NewDefaultConfig creates a new instance of the Config with default values.
func NewDefaultConfig(options ...ConfigOption) *Config {
opts := configOptions{keyPrefix: cfgDefaultKeyPrefix}
for _, opt := range options {
opt(&opts)
}
return &Config{
keyPrefix: opts.keyPrefix,
HTTPClient: HTTPClientConfig{
RequestTimeout: config.TimeDuration(idputil.DefaultHTTPRequestTimeout),
},
GRPCClient: GRPCClientConfig{
RequestTimeout: config.TimeDuration(idptoken.DefaultGRPCClientRequestTimeout),
},
JWT: JWTConfig{
ClaimsCache: ClaimsCacheConfig{
MaxEntries: jwt.DefaultClaimsCacheMaxEntries,
},
},
JWKS: JWKSConfig{
Cache: JWKSCacheConfig{
UpdateMinInterval: config.TimeDuration(jwks.DefaultCacheUpdateMinInterval),
},
},
Introspection: IntrospectionConfig{
ClaimsCache: IntrospectionCacheConfig{
MaxEntries: idptoken.DefaultIntrospectionClaimsCacheMaxEntries,
TTL: config.TimeDuration(idptoken.DefaultIntrospectionClaimsCacheTTL),
},
NegativeCache: IntrospectionCacheConfig{
MaxEntries: idptoken.DefaultIntrospectionNegativeCacheMaxEntries,
TTL: config.TimeDuration(idptoken.DefaultIntrospectionNegativeCacheTTL),
},
EndpointDiscoveryCache: IntrospectionCacheConfig{
Enabled: true,
MaxEntries: idptoken.DefaultIntrospectionEndpointDiscoveryCacheMaxEntries,
TTL: config.TimeDuration(idptoken.DefaultIntrospectionEndpointDiscoveryCacheTTL),
},
},
}
}
// KeyPrefix returns a key prefix with which all configuration parameters should be presented.
// Implements config.KeyPrefixProvider interface.
func (c *Config) KeyPrefix() string {
if c.keyPrefix == "" {
return cfgDefaultKeyPrefix
}
return c.keyPrefix
}
// SetProviderDefaults sets default configuration values for auth in config.DataProvider.
func (c *Config) SetProviderDefaults(dp config.DataProvider) {
dp.SetDefault(cfgKeyHTTPClientRequestTimeout, idputil.DefaultHTTPRequestTimeout.String())
dp.SetDefault(cfgKeyGRPCClientRequestTimeout, idptoken.DefaultGRPCClientRequestTimeout.String())
dp.SetDefault(cfgKeyJWTClaimsCacheMaxEntries, jwt.DefaultClaimsCacheMaxEntries)
dp.SetDefault(cfgKeyJWKSCacheUpdateMinInterval, jwks.DefaultCacheUpdateMinInterval.String())
dp.SetDefault(cfgKeyIntrospectionClaimsCacheMaxEntries, idptoken.DefaultIntrospectionClaimsCacheMaxEntries)
dp.SetDefault(cfgKeyIntrospectionClaimsCacheTTL, idptoken.DefaultIntrospectionClaimsCacheTTL.String())
dp.SetDefault(cfgKeyIntrospectionNegativeCacheMaxEntries, idptoken.DefaultIntrospectionNegativeCacheMaxEntries)
dp.SetDefault(cfgKeyIntrospectionNegativeCacheTTL, idptoken.DefaultIntrospectionNegativeCacheTTL.String())
dp.SetDefault(cfgKeyIntrospectionEndpointDiscoveryCacheEnabled, true)
dp.SetDefault(cfgKeyIntrospectionEndpointDiscoveryCacheMaxEntries, idptoken.DefaultIntrospectionEndpointDiscoveryCacheMaxEntries)
dp.SetDefault(cfgKeyIntrospectionEndpointDiscoveryCacheTTL, idptoken.DefaultIntrospectionEndpointDiscoveryCacheTTL.String())
}
// JWTConfig is a configuration of how JWT will be verified.
type JWTConfig struct {
TrustedIssuers map[string]string `mapstructure:"trustedIssuers" yaml:"trustedIssuers" json:"trustedIssuers"`
TrustedIssuerURLs []string `mapstructure:"trustedIssuerUrls" yaml:"trustedIssuerUrls" json:"trustedIssuerUrls"`
RequireAudience bool `mapstructure:"requireAudience" yaml:"requireAudience" json:"requireAudience"`
ExpectedAudience []string `mapstructure:"expectedAudience" yaml:"expectedAudience" json:"expectedAudience"`
ClaimsCache ClaimsCacheConfig `mapstructure:"claimsCache" yaml:"claimsCache" json:"claimsCache"`
}
// JWKSConfig is a configuration of how JWKS will be used.
type JWKSConfig struct {
Cache JWKSCacheConfig `mapstructure:"cache" yaml:"cache" json:"cache"`
}
type JWKSCacheConfig struct {
UpdateMinInterval config.TimeDuration `mapstructure:"updateMinInterval" yaml:"updateMinInterval" json:"updateMinInterval"`
}
// IntrospectionConfig is a configuration of how token introspection will be used.
type IntrospectionConfig struct {
Enabled bool `mapstructure:"enabled" yaml:"enabled" json:"enabled"`
Endpoint string `mapstructure:"endpoint" yaml:"endpoint" json:"endpoint"`
AccessTokenScope []string `mapstructure:"accessTokenScope" yaml:"accessTokenScope" json:"accessTokenScope"`
ClaimsCache IntrospectionCacheConfig `mapstructure:"claimsCache" yaml:"claimsCache" json:"claimsCache"`
NegativeCache IntrospectionCacheConfig `mapstructure:"negativeCache" yaml:"negativeCache" json:"negativeCache"`
EndpointDiscoveryCache IntrospectionCacheConfig `mapstructure:"endpointDiscoveryCache" yaml:"endpointDiscoveryCache" json:"endpointDiscoveryCache"` // nolint:lll
GRPC IntrospectionGRPCConfig `mapstructure:"grpc" yaml:"grpc" json:"grpc"`
}
// ClaimsCacheConfig is a configuration of how claims cache will be used.
type ClaimsCacheConfig struct {
Enabled bool `mapstructure:"enabled" yaml:"enabled" json:"enabled"`
MaxEntries int `mapstructure:"maxEntries" yaml:"maxEntries" json:"maxEntries"`
}
// IntrospectionCacheConfig is a configuration of how claims cache will be used for introspection.
type IntrospectionCacheConfig struct {
Enabled bool `mapstructure:"enabled" yaml:"enabled" json:"enabled"`
MaxEntries int `mapstructure:"maxEntries" yaml:"maxEntries" json:"maxEntries"`
TTL config.TimeDuration `mapstructure:"ttl" yaml:"ttl" json:"ttl"`
}
// IntrospectionGRPCConfig is a configuration of how token will be introspected via gRPC.
type IntrospectionGRPCConfig struct {
Endpoint string `mapstructure:"endpoint" yaml:"endpoint" json:"endpoint"`
RequestTimeout config.TimeDuration `mapstructure:"requestTimeout" yaml:"requestTimeout" json:"requestTimeout"`
TLS GRPCTLSConfig `mapstructure:"tls" yaml:"tls" json:"tls"`
}
// GRPCTLSConfig is a configuration of how gRPC connection will be secured.
type GRPCTLSConfig struct {
Enabled bool `mapstructure:"enabled" yaml:"enabled" json:"enabled"`
CACert string `mapstructure:"caCert" yaml:"caCert" json:"caCert"`
ClientCert string `mapstructure:"clientCert" yaml:"clientCert" json:"clientCert"`
ClientKey string `mapstructure:"clientKey" yaml:"clientKey" json:"clientKey"`
}
type HTTPClientConfig struct {
RequestTimeout config.TimeDuration `mapstructure:"requestTimeout" yaml:"requestTimeout" json:"requestTimeout"`
}
type GRPCClientConfig struct {
RequestTimeout config.TimeDuration `mapstructure:"requestTimeout" yaml:"requestTimeout" json:"requestTimeout"`
}
// Set sets auth configuration values from config.DataProvider.
func (c *Config) Set(dp config.DataProvider) error {
var err error
var reqDuration time.Duration
if reqDuration, err = dp.GetDuration(cfgKeyHTTPClientRequestTimeout); err != nil {
return err
}
c.HTTPClient.RequestTimeout = config.TimeDuration(reqDuration)
if reqDuration, err = dp.GetDuration(cfgKeyGRPCClientRequestTimeout); err != nil {
return err
}
c.GRPCClient.RequestTimeout = config.TimeDuration(reqDuration)
if err = c.setJWTConfig(dp); err != nil {
return err
}
if err = c.setJWKSConfig(dp); err != nil {
return err
}
if err = c.setIntrospectionConfig(dp); err != nil {
return err
}
return nil
}
func (c *Config) setJWTConfig(dp config.DataProvider) error {
var err error
if c.JWT.TrustedIssuers, err = dp.GetStringMapString(cfgKeyJWTTrustedIssuers); err != nil {
return err
}
if c.JWT.TrustedIssuerURLs, err = dp.GetStringSlice(cfgKeyJWTTrustedIssuerURLs); err != nil {
return err
}
for _, issURL := range c.JWT.TrustedIssuerURLs {
if _, err = url.Parse(issURL); err != nil {
return dp.WrapKeyErr(cfgKeyJWTTrustedIssuerURLs, err)
}
}
if c.JWT.RequireAudience, err = dp.GetBool(cfgKeyJWTRequireAudience); err != nil {
return err
}
if c.JWT.ExpectedAudience, err = dp.GetStringSlice(cfgKeyJWTExceptedAudience); err != nil {
return err
}
if c.JWT.ClaimsCache.Enabled, err = dp.GetBool(cfgKeyJWTClaimsCacheEnabled); err != nil {
return err
}
if c.JWT.ClaimsCache.MaxEntries, err = dp.GetInt(cfgKeyJWTClaimsCacheMaxEntries); err != nil {
return err
}
if c.JWT.ClaimsCache.MaxEntries < 0 {
return dp.WrapKeyErr(cfgKeyJWTClaimsCacheMaxEntries, fmt.Errorf("max entries should be non-negative"))
}
return nil
}
func (c *Config) setJWKSConfig(dp config.DataProvider) error {
updateMinInterval, err := dp.GetDuration(cfgKeyJWKSCacheUpdateMinInterval)
if err != nil {
return err
}
c.JWKS.Cache.UpdateMinInterval = config.TimeDuration(updateMinInterval)
return nil
}
func (c *Config) setIntrospectionConfig(dp config.DataProvider) error {
var err error
if c.Introspection.Enabled, err = dp.GetBool(cfgKeyIntrospectionEnabled); err != nil {
return err
}
if c.Introspection.Endpoint, err = dp.GetString(cfgKeyIntrospectionEndpoint); err != nil {
return err
}
if _, err = url.Parse(c.Introspection.Endpoint); err != nil {
return dp.WrapKeyErr(cfgKeyIntrospectionEndpoint, err)
}
// GRPC
if c.Introspection.GRPC.Endpoint, err = dp.GetString(cfgKeyIntrospectionGRPCEndpoint); err != nil {
return err
}
if c.Introspection.GRPC.TLS.Enabled, err = dp.GetBool(cfgKeyIntrospectionGRPCTLSEnabled); err != nil {
return err
}
if c.Introspection.GRPC.TLS.CACert, err = dp.GetString(cfgKeyIntrospectionGRPCTLSCACert); err != nil {
return err
}
if c.Introspection.GRPC.TLS.ClientCert, err = dp.GetString(cfgKeyIntrospectionGRPCTLSClientCert); err != nil {
return err
}
if c.Introspection.GRPC.TLS.ClientKey, err = dp.GetString(cfgKeyIntrospectionGRPCTLSClientKey); err != nil {
return err
}
if c.Introspection.AccessTokenScope, err = dp.GetStringSlice(cfgKeyIntrospectionAccessTokenScope); err != nil {
return err
}
// Claims cache
if c.Introspection.ClaimsCache.Enabled, err = dp.GetBool(cfgKeyIntrospectionClaimsCacheEnabled); err != nil {
return err
}
if c.Introspection.ClaimsCache.MaxEntries, err = dp.GetInt(cfgKeyIntrospectionClaimsCacheMaxEntries); err != nil {
return err
}
if c.Introspection.ClaimsCache.MaxEntries < 0 {
return dp.WrapKeyErr(cfgKeyIntrospectionClaimsCacheMaxEntries, fmt.Errorf("max entries should be non-negative"))
}
var cacheTTL time.Duration
if cacheTTL, err = dp.GetDuration(cfgKeyIntrospectionClaimsCacheTTL); err != nil {
return err
}
c.Introspection.ClaimsCache.TTL = config.TimeDuration(cacheTTL)
// Negative cache
if c.Introspection.NegativeCache.Enabled, err = dp.GetBool(cfgKeyIntrospectionNegativeCacheEnabled); err != nil {
return err
}
if c.Introspection.NegativeCache.MaxEntries, err = dp.GetInt(cfgKeyIntrospectionNegativeCacheMaxEntries); err != nil {
return err
}
if c.Introspection.NegativeCache.MaxEntries < 0 {
return dp.WrapKeyErr(cfgKeyIntrospectionNegativeCacheMaxEntries, fmt.Errorf("max entries should be non-negative"))
}
if cacheTTL, err = dp.GetDuration(cfgKeyIntrospectionNegativeCacheTTL); err != nil {
return err
}
c.Introspection.NegativeCache.TTL = config.TimeDuration(cacheTTL)
// OpenID configuration cache
if c.Introspection.EndpointDiscoveryCache.Enabled, err = dp.GetBool(
cfgKeyIntrospectionEndpointDiscoveryCacheEnabled,
); err != nil {
return err
}
if c.Introspection.EndpointDiscoveryCache.MaxEntries, err = dp.GetInt(
cfgKeyIntrospectionEndpointDiscoveryCacheMaxEntries,
); err != nil {
return err
}
if c.Introspection.EndpointDiscoveryCache.MaxEntries < 0 {
return dp.WrapKeyErr(cfgKeyIntrospectionEndpointDiscoveryCacheMaxEntries, fmt.Errorf("max entries should be non-negative"))
}
if cacheTTL, err = dp.GetDuration(cfgKeyIntrospectionEndpointDiscoveryCacheTTL); err != nil {
return err
}
c.Introspection.EndpointDiscoveryCache.TTL = config.TimeDuration(cacheTTL)
return nil
}