Skip to content

Commit 23ad64b

Browse files
committed
fix(auth): restore accidentally removed helper and fix flakiness in system tests
1 parent 087c4ef commit 23ad64b

2 files changed

Lines changed: 45 additions & 2 deletions

File tree

packages/google-auth/google/auth/_helpers.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import hashlib
2222
import json
2323
import logging
24+
import os
2425
import sys
2526
from typing import Any, Dict, Mapping, Optional, Union
2627
import urllib
@@ -307,6 +308,46 @@ def unpadded_urlsafe_b64encode(value):
307308
return base64.urlsafe_b64encode(value).rstrip(b"=")
308309

309310

311+
def get_bool_from_env(variable_name, default=False):
312+
"""Gets a boolean value from an environment variable.
313+
314+
The environment variable is interpreted as a boolean with the following
315+
(case-insensitive) rules:
316+
- "true", "1" are considered true.
317+
- "false", "0" are considered false.
318+
Any other values will raise an exception.
319+
320+
Args:
321+
variable_name (str): The name of the environment variable.
322+
default (bool): The default value if the environment variable is not
323+
set.
324+
325+
Returns:
326+
bool: The boolean value of the environment variable.
327+
328+
Raises:
329+
google.auth.exceptions.InvalidValue: If the environment variable is
330+
set to a value that can not be interpreted as a boolean.
331+
"""
332+
value = os.environ.get(variable_name)
333+
334+
if value is None:
335+
return default
336+
337+
value = value.lower()
338+
339+
if value in ("true", "1"):
340+
return True
341+
elif value in ("false", "0"):
342+
return False
343+
else:
344+
raise exceptions.InvalidValue(
345+
'Environment variable "{}" must be one of "true", "false", "1", or "0".'.format(
346+
variable_name
347+
)
348+
)
349+
350+
310351
def is_python_3():
311352
"""Check if the Python interpreter is Python 2 or 3.
312353

packages/google-auth/system_tests/system_tests_sync/test_external_accounts.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,10 @@ def check_impersonation_expiration():
216216

217217
credentials.refresh(http_request)
218218

219-
utcmax = _helpers.utcnow() + datetime.timedelta(seconds=TOKEN_LIFETIME_SECONDS)
220-
utcmin = utcmax - datetime.timedelta(seconds=BUFFER_SECONDS)
219+
now = _helpers.utcnow()
220+
# Allow for some clock skew between the test runner and the IAM server.
221+
utcmax = now + datetime.timedelta(seconds=TOKEN_LIFETIME_SECONDS + 10)
222+
utcmin = now + datetime.timedelta(seconds=TOKEN_LIFETIME_SECONDS - BUFFER_SECONDS)
221223
assert utcmin < credentials._impersonated_credentials.expiry <= utcmax
222224

223225
return True

0 commit comments

Comments
 (0)