@@ -38,28 +38,28 @@ type MutatorIDToken struct {
38
38
templates * template.Template
39
39
templatesLock sync.Mutex
40
40
41
- tokenCache * ristretto.Cache [string , * idTokenCacheContainer ]
42
- tokenCacheEnabled bool
41
+ tokenCache * ristretto.Cache [string , * idTokenCacheContainer ]
43
42
}
44
43
45
44
type CredentialsIDTokenConfig struct {
46
- Claims string `json:"claims"`
47
- IssuerURL string `json:"issuer_url"`
48
- JWKSURL string `json:"jwks_url"`
49
- TTL string `json:"ttl"`
45
+ Claims string `json:"claims"`
46
+ IssuerURL string `json:"issuer_url"`
47
+ JWKSURL string `json:"jwks_url"`
48
+ TTL string `json:"ttl"`
49
+ Cache IdTokenCacheConfig `json:"cache"`
50
+ }
51
+
52
+ type IdTokenCacheConfig struct {
53
+ Enabled bool `json:"enabled"`
54
+ MaxCost int `json:"max_cost"`
50
55
}
51
56
52
57
func (c * CredentialsIDTokenConfig ) ClaimsTemplateID () string {
53
58
return fmt .Sprintf ("%x" , md5 .Sum ([]byte (c .Claims )))
54
59
}
55
60
56
61
func NewMutatorIDToken (c configuration.Provider , r MutatorIDTokenRegistry ) * MutatorIDToken {
57
- cache , _ := ristretto .NewCache (& ristretto.Config [string , * idTokenCacheContainer ]{
58
- NumCounters : 10000 ,
59
- MaxCost : 1 << 25 ,
60
- BufferItems : 64 ,
61
- })
62
- return & MutatorIDToken {r : r , c : c , templates : x .NewTemplate ("id_token" ), tokenCache : cache , tokenCacheEnabled : true }
62
+ return & MutatorIDToken {r : r , c : c , templates : x .NewTemplate ("id_token" )}
63
63
}
64
64
65
65
func (a * MutatorIDToken ) GetID () string {
@@ -70,10 +70,6 @@ func (a *MutatorIDToken) WithCache(t *template.Template) {
70
70
a .templates = t
71
71
}
72
72
73
- func (a * MutatorIDToken ) SetCaching (token bool ) {
74
- a .tokenCacheEnabled = token
75
- }
76
-
77
73
type idTokenCacheContainer struct {
78
74
ExpiresAt time.Time
79
75
Token string
@@ -86,7 +82,7 @@ func (a *MutatorIDToken) cacheKey(config *CredentialsIDTokenConfig, ttl time.Dur
86
82
}
87
83
88
84
func (a * MutatorIDToken ) tokenFromCache (config * CredentialsIDTokenConfig , session * authn.AuthenticationSession , claims []byte , ttl time.Duration ) (string , bool ) {
89
- if ! a . tokenCacheEnabled {
85
+ if ! config . Cache . Enabled {
90
86
return "" , false
91
87
}
92
88
@@ -106,7 +102,7 @@ func (a *MutatorIDToken) tokenFromCache(config *CredentialsIDTokenConfig, sessio
106
102
}
107
103
108
104
func (a * MutatorIDToken ) tokenToCache (config * CredentialsIDTokenConfig , session * authn.AuthenticationSession , claims []byte , ttl time.Duration , expiresAt time.Time , token string ) {
109
- if ! a . tokenCacheEnabled {
105
+ if ! config . Cache . Enabled {
110
106
return
111
107
}
112
108
@@ -197,7 +193,11 @@ func (a *MutatorIDToken) Validate(config json.RawMessage) error {
197
193
}
198
194
199
195
func (a * MutatorIDToken ) Config (config json.RawMessage ) (* CredentialsIDTokenConfig , error ) {
200
- var c CredentialsIDTokenConfig
196
+ c := CredentialsIDTokenConfig {
197
+ Cache : IdTokenCacheConfig {
198
+ Enabled : true , // default to true
199
+ },
200
+ }
201
201
if err := a .c .MutatorConfig (a .GetID (), config , & c ); err != nil {
202
202
return nil , NewErrMutatorMisconfigured (a , err )
203
203
}
@@ -206,5 +206,29 @@ func (a *MutatorIDToken) Config(config json.RawMessage) (*CredentialsIDTokenConf
206
206
c .TTL = "15m"
207
207
}
208
208
209
+ cost := int64 (c .Cache .MaxCost )
210
+ if cost == 0 {
211
+ cost = 1 << 25
212
+ }
213
+
214
+ if a .tokenCache == nil || a .tokenCache .MaxCost () != cost {
215
+ cache , err := ristretto .NewCache (& ristretto.Config [string , * idTokenCacheContainer ]{
216
+ // Guessed approximation of max number of items.
217
+ NumCounters : cost * 4 ,
218
+ // Allocate a max
219
+ MaxCost : cost ,
220
+ // This is a best-practice value.
221
+ BufferItems : 64 ,
222
+ Cost : func (container * idTokenCacheContainer ) int64 {
223
+ return int64 (len (container .Token ))
224
+ },
225
+ })
226
+
227
+ if err != nil {
228
+ return nil , err
229
+ }
230
+ a .tokenCache = cache
231
+ }
232
+
209
233
return & c , nil
210
234
}
0 commit comments