Skip to content

Commit 8f909e7

Browse files
authored
Merge pull request #7 from lyft/SEC-620-allow-list-of-validation-keys
Allow list of keys for user verification
2 parents ec8c30e + f2d570e commit 8f909e7

3 files changed

Lines changed: 52 additions & 19 deletions

File tree

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,10 @@ token = generator.get_token()
7070
```python
7171
import kmsauth
7272
validator = kmsauth.KMSTokenValidator(
73-
# KMS key to use for service authentication
74-
'alias/authnz-production',
75-
# KMS key to use for user authentication
76-
'alias/authnz-users-production',
73+
# KMS keys to use for service authentication
74+
['alias/authnz-production'],
75+
# KMS keys to use for user authentication
76+
['alias/authnz-users-production', '6655d2a8-0606-4727-a1f6-f5b6a6754377'],
7777
# The context of this validation (the "to" context to validate against)
7878
'confidant-production',
7979
# Find the KMS keys in this region
@@ -88,10 +88,10 @@ context into the validator:
8888
```python
8989
import kmsauth
9090
validator = kmsauth.KMSTokenValidator(
91-
# KMS key to use for service authentication
92-
'alias/authnz-production',
93-
# KMS key to use for user authentication
94-
'alias/authnz-users-production',
91+
# KMS keys to use for service authentication
92+
['alias/authnz-production'],
93+
# KMS keys to use for user authentication
94+
['alias/authnz-users-production', '6655d2a8-0606-4727-a1f6-f5b6a6754377'],
9595
# The context of this validation (the "to" context to validate against)
9696
'confidant-production',
9797
# Find the KMS keys in this region

kmsauth/__init__.py

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import sys
88
import copy
99

10+
from botocore.vendored import six
11+
1012
import kmsauth.services
1113
from kmsauth.utils import lru
1214

@@ -35,9 +37,9 @@ def __init__(
3537
"""Create a KMSTokenValidator object.
3638
3739
Args:
38-
auth_key: The KMS key ARN or alias to use for service
40+
auth_key: A list of KMS key ARNs or aliases to use for service
3941
authentication. Required.
40-
user_auth_key: The KMS key ARN or alias to use for user
42+
user_auth_key: A list of KMS key ARNs or aliases to use for user
4143
authentication. Required.
4244
to_auth_context: The KMS encryption context to use for the to
4345
context for authentication. Required.
@@ -103,6 +105,21 @@ def _validate(self):
103105
'minimum_token_version can not be greater than'
104106
' self.minimum_token_version'
105107
)
108+
self.auth_key = self._format_auth_key(self.auth_key)
109+
self.user_auth_key = self._format_auth_key(self.user_auth_key)
110+
111+
def _format_auth_key(self, keys):
112+
if isinstance(keys, six.string_types):
113+
logging.debug(
114+
'Passing auth key as string is deprecated, and will be removed'
115+
' in 1.0.0'
116+
)
117+
return [keys]
118+
elif (keys is None or isinstance(keys, list)):
119+
return keys
120+
raise ConfigurationError(
121+
'auth_key and user_auth_key must be a string, list, or None'
122+
)
106123

107124
def _get_key_arn(self, key):
108125
if key not in self.KEY_METADATA:
@@ -126,8 +143,9 @@ def _get_key_alias_from_cache(self, key_arn):
126143
def _valid_service_auth_key(self, key_arn):
127144
if self.auth_key is None:
128145
return False
129-
if key_arn == self._get_key_arn(self.auth_key):
130-
return True
146+
for key in self.auth_key:
147+
if key_arn == self._get_key_arn(key):
148+
return True
131149
for key in self.scoped_auth_keys:
132150
if key_arn == self._get_key_arn(key):
133151
return True
@@ -136,8 +154,9 @@ def _valid_service_auth_key(self, key_arn):
136154
def _valid_user_auth_key(self, key_arn):
137155
if self.user_auth_key is None:
138156
return False
139-
if key_arn == self._get_key_arn(self.user_auth_key):
140-
return True
157+
for key in self.user_auth_key:
158+
if key_arn == self._get_key_arn(key):
159+
return True
141160
return False
142161

143162
def _parse_username(self, username):

tests/unit/kmsauth/kmsauth_test.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class KMSTokenValidatorTest(unittest.TestCase):
1818
def test_validate_config(self):
1919
with self.assertRaises(kmsauth.ConfigurationError):
2020
kmsauth.KMSTokenValidator(
21-
'alias/authnz-unittest',
21+
['alias/authnz-unittest'],
2222
None,
2323
'kmsauth-unittest',
2424
'us-east-1',
@@ -27,7 +27,7 @@ def test_validate_config(self):
2727
)
2828
with self.assertRaises(kmsauth.ConfigurationError):
2929
kmsauth.KMSTokenValidator(
30-
'alias/authnz-unittest',
30+
['alias/authnz-unittest'],
3131
None,
3232
'kmsauth-unittest',
3333
'us-east-1',
@@ -36,7 +36,7 @@ def test_validate_config(self):
3636
)
3737
with self.assertRaises(kmsauth.ConfigurationError):
3838
kmsauth.KMSTokenValidator(
39-
'alias/authnz-unittest',
39+
['alias/authnz-unittest'],
4040
None,
4141
'kmsauth-unittest',
4242
'us-east-1',
@@ -45,7 +45,7 @@ def test_validate_config(self):
4545
)
4646
with self.assertRaises(kmsauth.ConfigurationError):
4747
kmsauth.KMSTokenValidator(
48-
'alias/authnz-unittest',
48+
['alias/authnz-unittest'],
4949
None,
5050
'kmsauth-unittest',
5151
'us-east-1',
@@ -54,20 +54,34 @@ def test_validate_config(self):
5454
)
5555
with self.assertRaises(kmsauth.ConfigurationError):
5656
kmsauth.KMSTokenValidator(
57-
'alias/authnz-unittest',
57+
['alias/authnz-unittest'],
5858
None,
5959
'kmsauth-unittest',
6060
'us-east-1',
6161
# minimum can't be greater than maximum
6262
minimum_token_version=2,
6363
maximum_token_version=1
6464
)
65+
with self.assertRaises(kmsauth.ConfigurationError):
66+
kmsauth.KMSTokenValidator(
67+
# kms key must be string, list, or None
68+
1234,
69+
None,
70+
'kmsauth-unittest',
71+
'us-east-1',
72+
)
6573
assert(kmsauth.KMSTokenValidator(
6674
'alias/authnz-unittest',
6775
None,
6876
'kmsauth-unittest',
6977
'us-east-1'
7078
))
79+
assert(kmsauth.KMSTokenValidator(
80+
['alias/authnz-unittest'],
81+
None,
82+
'kmsauth-unittest',
83+
'us-east-1'
84+
))
7185

7286
def test__get_key_arn(self):
7387
validator = kmsauth.KMSTokenValidator(

0 commit comments

Comments
 (0)