-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathconfig.go
314 lines (269 loc) · 11.2 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
/*
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 (
cfgKeyHTTPClientRequestTimeout = "auth.httpClient.requestTimeout"
cfgKeyGRPCClientRequestTimeout = "auth.grpcClient.requestTimeout"
cfgKeyJWTTrustedIssuers = "auth.jwt.trustedIssuers"
cfgKeyJWTTrustedIssuerURLs = "auth.jwt.trustedIssuerUrls"
cfgKeyJWTRequireAudience = "auth.jwt.requireAudience"
cfgKeyJWTExceptedAudience = "auth.jwt.expectedAudience"
cfgKeyJWTClaimsCacheEnabled = "auth.jwt.claimsCache.enabled"
cfgKeyJWTClaimsCacheMaxEntries = "auth.jwt.claimsCache.maxEntries"
cfgKeyJWKSCacheUpdateMinInterval = "auth.jwks.cache.updateMinInterval"
cfgKeyIntrospectionEnabled = "auth.introspection.enabled"
cfgKeyIntrospectionEndpoint = "auth.introspection.endpoint"
cfgKeyIntrospectionGRPCEndpoint = "auth.introspection.grpc.endpoint"
cfgKeyIntrospectionGRPCTLSEnabled = "auth.introspection.grpc.tls.enabled"
cfgKeyIntrospectionGRPCTLSCACert = "auth.introspection.grpc.tls.caCert"
cfgKeyIntrospectionGRPCTLSClientCert = "auth.introspection.grpc.tls.clientCert"
cfgKeyIntrospectionGRPCTLSClientKey = "auth.introspection.grpc.tls.clientKey"
cfgKeyIntrospectionAccessTokenScope = "auth.introspection.accessTokenScope" // nolint:gosec // false positive
cfgKeyIntrospectionClaimsCacheEnabled = "auth.introspection.claimsCache.enabled"
cfgKeyIntrospectionClaimsCacheMaxEntries = "auth.introspection.claimsCache.maxEntries"
cfgKeyIntrospectionClaimsCacheTTL = "auth.introspection.claimsCache.ttl"
cfgKeyIntrospectionNegativeCacheEnabled = "auth.introspection.negativeCache.enabled"
cfgKeyIntrospectionNegativeCacheMaxEntries = "auth.introspection.negativeCache.maxEntries"
cfgKeyIntrospectionNegativeCacheTTL = "auth.introspection.negativeCache.ttl"
cfgKeyIntrospectionEndpointDiscoveryCacheEnabled = "auth.introspection.endpointDiscoveryCache.enabled"
cfgKeyIntrospectionEndpointDiscoveryCacheMaxEntries = "auth.introspection.endpointDiscoveryCache.maxEntries"
cfgKeyIntrospectionEndpointDiscoveryCacheTTL = "auth.introspection.endpointDiscoveryCache.ttl"
)
// JWTConfig is configuration of how JWT will be verified.
type JWTConfig struct {
TrustedIssuers map[string]string
TrustedIssuerURLs []string
RequireAudience bool
ExpectedAudience []string
ClaimsCache ClaimsCacheConfig
}
// JWKSConfig is configuration of how JWKS will be used.
type JWKSConfig struct {
Cache struct {
UpdateMinInterval time.Duration
}
}
// IntrospectionConfig is a configuration of how token introspection will be used.
type IntrospectionConfig struct {
Enabled bool
Endpoint string
AccessTokenScope []string
ClaimsCache IntrospectionCacheConfig
NegativeCache IntrospectionCacheConfig
EndpointDiscoveryCache IntrospectionCacheConfig
GRPC IntrospectionGRPCConfig
}
// ClaimsCacheConfig is a configuration of how claims cache will be used.
type ClaimsCacheConfig struct {
Enabled bool
MaxEntries int
}
// IntrospectionCacheConfig is a configuration of how claims cache will be used for introspection.
type IntrospectionCacheConfig struct {
Enabled bool
MaxEntries int
TTL time.Duration
}
// IntrospectionGRPCConfig is a configuration of how token will be introspected via gRPC.
type IntrospectionGRPCConfig struct {
Endpoint string
RequestTimeout time.Duration
TLS GRPCTLSConfig
}
// GRPCTLSConfig is a configuration of how gRPC connection will be secured.
type GRPCTLSConfig struct {
Enabled bool
CACert string
ClientCert string
ClientKey string
}
type HTTPClientConfig struct {
RequestTimeout time.Duration
}
type GRPCClientConfig struct {
RequestTimeout time.Duration
}
// Config represents a set of configuration parameters for authentication and authorization.
type Config struct {
HTTPClient HTTPClientConfig
GRPCClient GRPCClientConfig
JWT JWTConfig
JWKS JWKSConfig
Introspection IntrospectionConfig
keyPrefix string
}
var _ config.Config = (*Config)(nil)
var _ config.KeyPrefixProvider = (*Config)(nil)
// NewConfig creates a new instance of the Config.
func NewConfig() *Config {
return NewConfigWithKeyPrefix("")
}
// NewConfigWithKeyPrefix creates a new instance of the Config.
// Allows specifying key prefix which will be used for parsing configuration parameters.
func NewConfigWithKeyPrefix(keyPrefix string) *Config {
return &Config{keyPrefix: keyPrefix}
}
// KeyPrefix returns a key prefix with which all configuration parameters should be presented.
func (c *Config) KeyPrefix() string {
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())
}
// Set sets auth configuration values from config.DataProvider.
func (c *Config) Set(dp config.DataProvider) error {
var err error
if c.HTTPClient.RequestTimeout, err = dp.GetDuration(cfgKeyHTTPClientRequestTimeout); err != nil {
return err
}
if c.GRPCClient.RequestTimeout, err = dp.GetDuration(cfgKeyGRPCClientRequestTimeout); err != nil {
return err
}
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 {
var err error
if c.JWKS.Cache.UpdateMinInterval, err = dp.GetDuration(cfgKeyJWKSCacheUpdateMinInterval); err != nil {
return err
}
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"))
}
if c.Introspection.ClaimsCache.TTL, err = dp.GetDuration(cfgKeyIntrospectionClaimsCacheTTL); err != nil {
return err
}
// 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 c.Introspection.NegativeCache.TTL, err = dp.GetDuration(cfgKeyIntrospectionNegativeCacheTTL); err != nil {
return err
}
// 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 c.Introspection.EndpointDiscoveryCache.TTL, err = dp.GetDuration(
cfgKeyIntrospectionEndpointDiscoveryCacheTTL,
); err != nil {
return err
}
return nil
}