Skip to content

Commit 22b1c5a

Browse files
committed
Debug confidant low cache hit
1 parent aa96497 commit 22b1c5a

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

kmsauth/__init__.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,14 +169,16 @@ def _format_auth_key(self, keys):
169169
)
170170

171171
def _get_key_arn(self, key):
172+
logging.debug(f'Getting key ARN for {key}')
172173
if key.startswith('arn:aws:kms:'):
173174
self.KEY_METADATA[key] = {
174175
'KeyMetadata': {'Arn': key}
175176
}
176177
if key not in self.KEY_METADATA:
177-
self.KEY_METADATA[key] = self.kms_client.describe_key(
178-
KeyId='{0}'.format(key)
179-
)
178+
with self.stats.timer('kms_describe_key'):
179+
self.KEY_METADATA[key] = self.kms_client.describe_key(
180+
KeyId='{0}'.format(key)
181+
)
180182
return self.KEY_METADATA[key]['KeyMetadata']['Arn']
181183

182184
def _get_key_alias_from_cache(self, key_arn):
@@ -246,6 +248,9 @@ def decrypt_token(self, username, token):
246248
raise TokenValidationError('Unacceptable token version.')
247249
if self.stats:
248250
self.stats.incr('token_version_{0}'.format(version))
251+
self.stats.incr(f'cache_key.from.{_from}')
252+
self.stats.incr(f'cache_key.to.{self.to_auth_context}')
253+
self.stats.incr(f'cache_key.user_type.{user_type}')
249254
try:
250255
token_key = '{0}{1}{2}{3}'.format(
251256
hashlib.sha256(ensure_bytes(token)).hexdigest(),
@@ -256,6 +261,11 @@ def decrypt_token(self, username, token):
256261
except Exception:
257262
raise TokenValidationError('Authentication error.')
258263
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')
268+
259269
try:
260270
token = base64.b64decode(token)
261271
# Ensure normal context fields override whatever is in
@@ -313,6 +323,7 @@ def decrypt_token(self, username, token):
313323
'Authentication error. General error.'
314324
)
315325
else:
326+
self.stats.incr('token_cache.hit')
316327
ret = self.TOKENS[token_key]
317328
now = datetime.datetime.utcnow()
318329
try:
@@ -342,6 +353,8 @@ def decrypt_token(self, username, token):
342353
raise TokenValidationError(
343354
'Authentication error. Invalid time validity for token.'
344355
)
356+
self.stats.incr('token_cache.set')
357+
self.stats.gauge('token_cache.size_at_set', len(self.TOKENS))
345358
self.TOKENS[token_key] = ret
346359
return self.TOKENS[token_key]
347360

0 commit comments

Comments
 (0)