@@ -129,6 +129,7 @@ def __init__(
129129 else :
130130 self .extra_context = extra_context
131131 self .TOKENS = LRU (token_cache_size )
132+ self .token_cache_size = token_cache_size
132133 self .KEY_METADATA = {}
133134 self .stats = stats
134135 self ._validate ()
@@ -169,13 +170,17 @@ def _format_auth_key(self, keys):
169170 )
170171
171172 def _get_key_arn (self , key ):
172- logging .debug (f'Getting key ARN for { key } ' )
173173 if key .startswith ('arn:aws:kms:' ):
174174 self .KEY_METADATA [key ] = {
175175 'KeyMetadata' : {'Arn' : key }
176176 }
177177 if key not in self .KEY_METADATA :
178- with self .stats .timer ('kms_describe_key' ):
178+ if self .stats :
179+ with self .stats .timer ('kms_describe_key' ):
180+ self .KEY_METADATA [key ] = self .kms_client .describe_key (
181+ KeyId = '{0}' .format (key )
182+ )
183+ else :
179184 self .KEY_METADATA [key ] = self .kms_client .describe_key (
180185 KeyId = '{0}' .format (key )
181186 )
@@ -261,10 +266,11 @@ def decrypt_token(self, username, token):
261266 except Exception :
262267 raise TokenValidationError ('Authentication error.' )
263268 if token_key not in self .TOKENS :
264- self .stats .incr ('token_cache.miss' )
265- self .stats .gauge ('token_cache.size_at_miss' , len (self .TOKENS ))
266- if len (self .TOKENS ) >= self .token_cache_size :
267- self .stats .incr ('token_cache.eviction' )
269+ if self .stats :
270+ self .stats .incr ('token_cache.miss' )
271+ self .stats .gauge ('token_cache.size_at_miss' , len (self .TOKENS ))
272+ if len (self .TOKENS ) >= self .token_cache_size :
273+ self .stats .incr ('token_cache.eviction' )
268274
269275 try :
270276 token = base64 .b64decode (token )
@@ -323,7 +329,8 @@ def decrypt_token(self, username, token):
323329 'Authentication error. General error.'
324330 )
325331 else :
326- self .stats .incr ('token_cache.hit' )
332+ if self .stats :
333+ self .stats .incr ('token_cache.hit' )
327334 ret = self .TOKENS [token_key ]
328335 now = datetime .datetime .utcnow ()
329336 try :
@@ -353,8 +360,9 @@ def decrypt_token(self, username, token):
353360 raise TokenValidationError (
354361 'Authentication error. Invalid time validity for token.'
355362 )
356- self .stats .incr ('token_cache.set' )
357- self .stats .gauge ('token_cache.size_at_set' , len (self .TOKENS ))
363+ if self .stats :
364+ self .stats .incr ('token_cache.set' )
365+ self .stats .gauge ('token_cache.size_at_set' , len (self .TOKENS ))
358366 self .TOKENS [token_key ] = ret
359367 return self .TOKENS [token_key ]
360368
0 commit comments