Skip to content

Commit 4acc1d4

Browse files
committed
from_env_var_v2 optional args
1 parent 595ef73 commit 4acc1d4

3 files changed

Lines changed: 37 additions & 22 deletions

File tree

src/momento/auth/credential_provider.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,11 @@ def from_environment_variable(
4646
Returns:
4747
CredentialProvider
4848
"""
49-
warn("from_environment_variable is deprecated, use from_env_var_v2 instead", DeprecationWarning, stacklevel=2)
49+
warn(
50+
"from_environment_variable is deprecated, use from_environment_variables_v2 instead",
51+
DeprecationWarning,
52+
stacklevel=2,
53+
)
5054
api_key = os.getenv(env_var_name)
5155
if not api_key:
5256
raise RuntimeError(f"Missing required environment variable {env_var_name}")
@@ -115,10 +119,10 @@ def get_auth_token(self) -> str:
115119

116120
@staticmethod
117121
def from_api_key_v2(api_key: str, endpoint: str) -> CredentialProvider:
118-
"""Creates a CredentialProvider from a global API key and endpoint.
122+
"""Creates a CredentialProvider from a v2 API key and endpoint.
119123
120124
Args:
121-
api_key (str): The global API key.
125+
api_key (str): The v2 API key.
122126
endpoint (str): The Momento service endpoint.
123127
124128
Returns:
@@ -143,12 +147,14 @@ def from_api_key_v2(api_key: str, endpoint: str) -> CredentialProvider:
143147
)
144148

145149
@staticmethod
146-
def from_env_var_v2(api_key_env_var: str, endpoint_env_var: str) -> CredentialProvider:
147-
"""Creates a CredentialProvider from an endpoint and a global API key stored in an environment variable.
150+
def from_environment_variables_v2(
151+
api_key_env_var: str = "MOMENTO_API_KEY", endpoint_env_var: str = "MOMENTO_ENDPOINT"
152+
) -> CredentialProvider:
153+
"""Creates a CredentialProvider from an endpoint and v2 API key stored in the environment variables MOMENTO_API_KEY and MOMENTO_ENDPOINT.
148154
149155
Args:
150-
api_key_env_var (str): Name of the environment variable from which the global API key will be read.
151-
endpoint_env_var (str): Name of the environment variable from which the Momento service endpoint will be read.
156+
api_key_env_var (str): Optionally provide an alternate environment variable name from which the v2 API key will be read.
157+
endpoint_env_var (str): Optionally provide an alternate environment variable name from which the Momento service endpoint will be read.
152158
153159
Returns:
154160
CredentialProvider

src/momento/auth/momento_endpoint_resolver.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def resolve(auth_token: str) -> _TokenAndEndpoints:
4848
else:
4949
if _is_v2_api_key(auth_token):
5050
raise InvalidArgumentException(
51-
"Received a v2 API key. Are you using the correct key? Or did you mean to use `from_api_key_v2()` or `from_env_var_v2()` instead?",
51+
"Received a v2 API key. Are you using the correct key? Or did you mean to use `from_api_key_v2()` or `from_environment_variables_v2()` instead?",
5252
Service.AUTH,
5353
)
5454
return _get_endpoint_from_token(auth_token)

tests/momento/auth/test_credential_provider.py

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@
2626
os.environ[test_v1_env_var_name] = test_encoded_v1_token.decode("utf-8")
2727

2828
# For v2 API key tests
29-
test_v2_key_message = {"t": "g", "id": "some-id"}
29+
test_v2_key_message = {"t": "g", "jti": "some-id"}
3030
test_v2_api_key = jwt.encode(test_v2_key_message, "secret", algorithm="HS512")
31-
test_v2_key_env_var_name = uuid_str()
31+
test_v2_key_env_var_name = "MOMENTO_API_KEY"
3232
test_v2_endpoint = "testEndpoint"
33-
test_v2_endpoint_env_var_name = uuid_str()
33+
test_v2_endpoint_env_var_name = "MOMENTO_ENDPOINT"
3434
os.environ[test_v2_key_env_var_name] = test_v2_api_key
3535
os.environ[test_v2_endpoint_env_var_name] = test_v2_endpoint
3636

@@ -113,7 +113,6 @@ def test_env_token_raises_if_not_exists() -> None:
113113
@pytest.mark.parametrize(
114114
"provider, expected_api_key, expected_control_endpoint, expected_cache_endpoint, expected_token_endpoint",
115115
[
116-
# v2_key_from_string - basic usage
117116
(
118117
CredentialProvider.from_api_key_v2(
119118
api_key=test_v2_api_key,
@@ -124,9 +123,8 @@ def test_env_token_raises_if_not_exists() -> None:
124123
f"cache.{test_v2_endpoint}",
125124
f"token.{test_v2_endpoint}",
126125
),
127-
# v2_key_from_environment_variable - basic usage
128126
(
129-
CredentialProvider.from_env_var_v2(
127+
CredentialProvider.from_environment_variables_v2(
130128
api_key_env_var=test_v2_key_env_var_name,
131129
endpoint_env_var=test_v2_endpoint_env_var_name,
132130
),
@@ -135,6 +133,13 @@ def test_env_token_raises_if_not_exists() -> None:
135133
f"cache.{test_v2_endpoint}",
136134
f"token.{test_v2_endpoint}",
137135
),
136+
(
137+
CredentialProvider.from_environment_variables_v2(),
138+
test_v2_api_key,
139+
f"control.{test_v2_endpoint}",
140+
f"cache.{test_v2_endpoint}",
141+
f"token.{test_v2_endpoint}",
142+
),
138143
],
139144
)
140145
def test_v2_api_key_endpoints(
@@ -162,24 +167,28 @@ def test_v2_key_from_string_raises_if_endpoint_empty() -> None:
162167

163168
def test_v2_key_from_env_raises_if_env_var_name_empty() -> None:
164169
with pytest.raises(InvalidArgumentException, match="API key environment variable name cannot be empty"):
165-
CredentialProvider.from_env_var_v2(api_key_env_var="", endpoint_env_var=test_v2_endpoint_env_var_name)
170+
CredentialProvider.from_environment_variables_v2(
171+
api_key_env_var="", endpoint_env_var=test_v2_endpoint_env_var_name
172+
)
166173

167174

168175
def test_v2_key_from_env_raises_if_env_var_missing() -> None:
169176
with pytest.raises(RuntimeError, match="Missing required environment variable"):
170-
CredentialProvider.from_env_var_v2(api_key_env_var=uuid_str(), endpoint_env_var=test_v2_endpoint_env_var_name)
177+
CredentialProvider.from_environment_variables_v2(
178+
api_key_env_var=uuid_str(), endpoint_env_var=test_v2_endpoint_env_var_name
179+
)
171180

172181

173182
def test_v2_key_from_env_raises_if_endpoint_empty() -> None:
174183
with pytest.raises(InvalidArgumentException, match="Endpoint environment variable name cannot be empty"):
175-
CredentialProvider.from_env_var_v2(api_key_env_var=test_v2_key_env_var_name, endpoint_env_var="")
184+
CredentialProvider.from_environment_variables_v2(api_key_env_var=test_v2_key_env_var_name, endpoint_env_var="")
176185

177186

178187
def test_v2_key_from_env_raises_if_api_key_empty_string() -> None:
179188
empty_api_key_env_var = uuid_str()
180189
os.environ[empty_api_key_env_var] = ""
181190
with pytest.raises(RuntimeError, match="Missing required environment variable"):
182-
CredentialProvider.from_env_var_v2(
191+
CredentialProvider.from_environment_variables_v2(
183192
api_key_env_var=empty_api_key_env_var, endpoint_env_var=test_v2_endpoint_env_var_name
184193
)
185194

@@ -203,7 +212,7 @@ def test_v2_key_from_env_raises_if_base64_api_key() -> None:
203212
"Received an invalid v2 API key. Are you using the correct key? Or did you mean to use `from_environment_variable()` with a legacy key instead?"
204213
),
205214
):
206-
CredentialProvider.from_env_var_v2(
215+
CredentialProvider.from_environment_variables_v2(
207216
api_key_env_var=test_v1_env_var_name, endpoint_env_var=test_v2_endpoint_env_var_name
208217
)
209218

@@ -225,7 +234,7 @@ def test_v2_key_from_env_raises_if_pre_v1_token() -> None:
225234
"Received an invalid v2 API key. Are you using the correct key? Or did you mean to use `from_environment_variable()` with a legacy key instead?"
226235
),
227236
):
228-
CredentialProvider.from_env_var_v2(
237+
CredentialProvider.from_environment_variables_v2(
229238
api_key_env_var=test_env_var_name, endpoint_env_var=test_v2_endpoint_env_var_name
230239
)
231240

@@ -234,7 +243,7 @@ def test_v2_key_provided_to_from_string() -> None:
234243
with pytest.raises(
235244
InvalidArgumentException,
236245
match=re.escape(
237-
"Received a v2 API key. Are you using the correct key? Or did you mean to use `from_api_key_v2()` or `from_env_var_v2()` instead?"
246+
"Received a v2 API key. Are you using the correct key? Or did you mean to use `from_api_key_v2()` or `from_environment_variables_v2()` instead?"
238247
),
239248
):
240249
CredentialProvider.from_string(auth_token=test_v2_api_key)
@@ -244,7 +253,7 @@ def test_v2_key_provided_to_from_disposable_token() -> None:
244253
with pytest.raises(
245254
InvalidArgumentException,
246255
match=re.escape(
247-
"Received a v2 API key. Are you using the correct key? Or did you mean to use `from_api_key_v2()` or `from_env_var_v2()` instead?"
256+
"Received a v2 API key. Are you using the correct key? Or did you mean to use `from_api_key_v2()` or `from_environment_variables_v2()` instead?"
248257
),
249258
):
250259
CredentialProvider.from_disposable_token(auth_token=test_v2_api_key)

0 commit comments

Comments
 (0)