Skip to content

Commit bf143e1

Browse files
author
Ryan Lane
authored
Accept a stats client as an argument to the validator (#15)
1 parent b53a53c commit bf143e1

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.5.0
2+
3+
* KMSTokenValidator now accepts a ``stats`` argument, which allows you to pass in an instance of a statsd client, so that the validator can track stats.
4+
15
## 0.4.0
26

37
* KMSTokenValidator now accepts a ``token_cache_size`` argument, to set the size of the in-memory LRU token cache.

kmsauth/__init__.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ def __init__(
5151
extra_context=None,
5252
endpoint_url=None,
5353
token_cache_size=4096,
54+
stats=None,
5455
):
5556
"""Create a KMSTokenValidator object.
5657
@@ -79,6 +80,8 @@ def __init__(
7980
credentials. Default: None
8081
endpoint_url: A URL to override the default endpoint used to access
8182
the KMS service. Default: None
83+
stats: A statsd client instance, to be used to track stats.
84+
Default: None
8285
"""
8386
self.auth_key = auth_key
8487
self.user_auth_key = user_auth_key
@@ -113,6 +116,7 @@ def __init__(
113116
self.extra_context = extra_context
114117
self.TOKENS = lru.LRUCache(token_cache_size)
115118
self.KEY_METADATA = {}
119+
self.stats = stats
116120
self._validate()
117121

118122
def _validate(self):
@@ -226,6 +230,8 @@ def decrypt_token(self, username, token):
226230
if (version > self.maximum_token_version or
227231
version < self.minimum_token_version):
228232
raise TokenValidationError('Unacceptable token version.')
233+
if self.stats:
234+
self.stats.incr('token_version_{0}'.format(version))
229235
try:
230236
token_key = '{0}{1}{2}{3}'.format(
231237
hashlib.sha256(ensure_bytes(token)).hexdigest(),
@@ -245,10 +251,17 @@ def decrypt_token(self, username, token):
245251
context['from'] = _from
246252
if version > 1:
247253
context['user_type'] = user_type
248-
data = self.kms_client.decrypt(
249-
CiphertextBlob=token,
250-
EncryptionContext=context
251-
)
254+
if self.stats:
255+
with self.stats.timer('kms_decrypt_token'):
256+
data = self.kms_client.decrypt(
257+
CiphertextBlob=token,
258+
EncryptionContext=context
259+
)
260+
else:
261+
data = self.kms_client.decrypt(
262+
CiphertextBlob=token,
263+
EncryptionContext=context
264+
)
252265
# Decrypt doesn't take KeyId as an argument. We need to verify
253266
# the correct key was used to do the decryption.
254267
# Annoyingly, the KeyId from the data is actually an arn.

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
from setuptools import setup, find_packages
1515

16-
VERSION = "0.4.0"
16+
VERSION = "0.5.0"
1717

1818
requirements = [
1919
# Boto3 is the Amazon Web Services (AWS) Software Development Kit (SDK)

0 commit comments

Comments
 (0)