Skip to content

Commit 4487fe8

Browse files
authored
Merge branch 'main' into patch-1
2 parents 8a0fc25 + a9b8f12 commit 4487fe8

File tree

6 files changed

+82
-12
lines changed

6 files changed

+82
-12
lines changed

google/auth/__init__.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,17 @@
1717
import logging
1818

1919
from google.auth import version as google_auth_version
20-
from google.auth._default import default, load_credentials_from_file
20+
from google.auth._default import (
21+
default,
22+
load_credentials_from_dict,
23+
load_credentials_from_file,
24+
)
2125

2226

2327
__version__ = google_auth_version.__version__
2428

2529

26-
__all__ = ["default", "load_credentials_from_file"]
30+
__all__ = ["default", "load_credentials_from_file", "load_credentials_from_dict"]
2731

2832
# Set default logging handler to avoid "No handler found" warnings.
2933
logging.getLogger(__name__).addHandler(logging.NullHandler())

google/auth/_default.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,50 @@ def load_credentials_from_file(
130130
)
131131

132132

133+
def load_credentials_from_dict(
134+
info, scopes=None, default_scopes=None, quota_project_id=None, request=None
135+
):
136+
"""Loads Google credentials from a dict.
137+
138+
The credentials file must be a service account key, stored authorized
139+
user credentials, external account credentials, or impersonated service
140+
account credentials.
141+
142+
Args:
143+
info (Dict[str, Any]): A dict object containing the credentials
144+
scopes (Optional[Sequence[str]]): The list of scopes for the credentials. If
145+
specified, the credentials will automatically be scoped if
146+
necessary
147+
default_scopes (Optional[Sequence[str]]): Default scopes passed by a
148+
Google client library. Use 'scopes' for user-defined scopes.
149+
quota_project_id (Optional[str]): The project ID used for
150+
quota and billing.
151+
request (Optional[google.auth.transport.Request]): An object used to make
152+
HTTP requests. This is used to determine the associated project ID
153+
for a workload identity pool resource (external account credentials).
154+
If not specified, then it will use a
155+
google.auth.transport.requests.Request client to make requests.
156+
157+
Returns:
158+
Tuple[google.auth.credentials.Credentials, Optional[str]]: Loaded
159+
credentials and the project ID. Authorized user credentials do not
160+
have the project ID information. External account credentials project
161+
IDs may not always be determined.
162+
163+
Raises:
164+
google.auth.exceptions.DefaultCredentialsError: if the file is in the
165+
wrong format or is missing.
166+
"""
167+
if not isinstance(info, dict):
168+
raise exceptions.DefaultCredentialsError(
169+
"info object was of type {} but dict type was expected.".format(type(info))
170+
)
171+
172+
return _load_credentials_from_info(
173+
"dict object", info, scopes, default_scopes, quota_project_id, request
174+
)
175+
176+
133177
def _load_credentials_from_info(
134178
filename, info, scopes, default_scopes, quota_project_id, request
135179
):

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@
3131
)
3232

3333
extras = {
34-
"aiohttp": ["aiohttp >= 3.6.2, < 4.0.0dev", "requests >= 2.20.0, < 3.0.0dev"],
34+
"aiohttp": ["aiohttp >= 3.6.2, < 4.0.0.dev0", "requests >= 2.20.0, < 3.0.0.dev0"],
3535
"pyopenssl": ["pyopenssl>=20.0.0", "cryptography>=38.0.3"],
36-
"requests": "requests >= 2.20.0, < 3.0.0dev",
36+
"requests": "requests >= 2.20.0, < 3.0.0.dev0",
3737
"reauth": "pyu2f>=0.1.5",
3838
# Enterprise cert only works for OpenSSL 1.1.1. Newer versions of these
3939
# dependencies are built with OpenSSL 3.0 so we need to fix the version.

system_tests/secrets.tar.enc

0 Bytes
Binary file not shown.

tests/test__default.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,28 @@ def test_load_credentials_from_missing_file():
188188
assert excinfo.match(r"not found")
189189

190190

191+
def test_load_credentials_from_dict_non_dict_object():
192+
with pytest.raises(exceptions.DefaultCredentialsError) as excinfo:
193+
_default.load_credentials_from_dict("")
194+
assert excinfo.match(r"dict type was expected")
195+
196+
with pytest.raises(exceptions.DefaultCredentialsError) as excinfo:
197+
_default.load_credentials_from_dict(None)
198+
assert excinfo.match(r"dict type was expected")
199+
200+
with pytest.raises(exceptions.DefaultCredentialsError) as excinfo:
201+
_default.load_credentials_from_dict(1)
202+
assert excinfo.match(r"dict type was expected")
203+
204+
205+
def test_load_credentials_from_dict_authorized_user():
206+
credentials, project_id = _default.load_credentials_from_dict(
207+
AUTHORIZED_USER_FILE_DATA
208+
)
209+
assert isinstance(credentials, google.oauth2.credentials.Credentials)
210+
assert project_id is None
211+
212+
191213
def test_load_credentials_from_file_invalid_json(tmpdir):
192214
jsonfile = tmpdir.join("invalid.json")
193215
jsonfile.write("{")

tests/test_aws.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,10 @@
6363
CRED_VERIFICATION_URL = (
6464
"https://sts.{region}.amazonaws.com?Action=GetCallerIdentity&Version=2011-06-15"
6565
)
66-
# Sample AWS security credentials to be used with tests that require a session token.
67-
ACCESS_KEY_ID = "ASIARD4OQDT6A77FR3CL"
68-
SECRET_ACCESS_KEY = "Y8AfSaucF37G4PpvfguKZ3/l7Id4uocLXxX0+VTx"
69-
TOKEN = "IQoJb3JpZ2luX2VjEIz//////////wEaCXVzLWVhc3QtMiJGMEQCIH7MHX/Oy/OB8OlLQa9GrqU1B914+iMikqWQW7vPCKlgAiA/Lsv8Jcafn14owfxXn95FURZNKaaphj0ykpmS+Ki+CSq0AwhlEAAaDDA3NzA3MTM5MTk5NiIMx9sAeP1ovlMTMKLjKpEDwuJQg41/QUKx0laTZYjPlQvjwSqS3OB9P1KAXPWSLkliVMMqaHqelvMF/WO/glv3KwuTfQsavRNs3v5pcSEm4SPO3l7mCs7KrQUHwGP0neZhIKxEXy+Ls//1C/Bqt53NL+LSbaGv6RPHaX82laz2qElphg95aVLdYgIFY6JWV5fzyjgnhz0DQmy62/Vi8pNcM2/VnxeCQ8CC8dRDSt52ry2v+nc77vstuI9xV5k8mPtnaPoJDRANh0bjwY5Sdwkbp+mGRUJBAQRlNgHUJusefXQgVKBCiyJY4w3Csd8Bgj9IyDV+Azuy1jQqfFZWgP68LSz5bURyIjlWDQunO82stZ0BgplKKAa/KJHBPCp8Qi6i99uy7qh76FQAqgVTsnDuU6fGpHDcsDSGoCls2HgZjZFPeOj8mmRhFk1Xqvkbjuz8V1cJk54d3gIJvQt8gD2D6yJQZecnuGWd5K2e2HohvCc8Fc9kBl1300nUJPV+k4tr/A5R/0QfEKOZL1/k5lf1g9CREnrM8LVkGxCgdYMxLQow1uTL+QU67AHRRSp5PhhGX4Rek+01vdYSnJCMaPhSEgcLqDlQkhk6MPsyT91QMXcWmyO+cAZwUPwnRamFepuP4K8k2KVXs/LIJHLELwAZ0ekyaS7CptgOqS7uaSTFG3U+vzFZLEnGvWQ7y9IPNQZ+Dffgh4p3vF4J68y9049sI6Sr5d5wbKkcbm8hdCDHZcv4lnqohquPirLiFQ3q7B17V9krMPu3mz1cg4Ekgcrn/E09NTsxAqD8NcZ7C7ECom9r+X3zkDOxaajW6hu3Az8hGlyylDaMiFfRbBJpTIlxp7jfa7CxikNgNtEKLH9iCzvuSg2vhA=="
66+
# Sample fictitious AWS security credentials to be used with tests that require a session token.
67+
ACCESS_KEY_ID = "AKIAIOSFODNN7EXAMPLE"
68+
SECRET_ACCESS_KEY = "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY"
69+
TOKEN = "AQoEXAMPLEH4aoAH0gNCAPyJxz4BlCFFxWNE1OPTgk5TthT+FvwqnKwRcOIfrRh3c/LTo6UDdyJwOOvEVPvLXCrrrUtdnniCEXAMPLE/IvU1dYUg2RVAJBanLiHb4IgRmpRV3zrkuWJOgQs8IZZaIv2BXIa2R4OlgkBN9bkUDNCJiBeb/AXlzBBko7b15fjrBs2+cTQtpZ3CYWFXG8C5zqx37wnOE49mRl/+OtkIKGO7fAE"
7070
# To avoid json.dumps() differing behavior from one version to other,
7171
# the JSON payload is hardcoded.
7272
REQUEST_PARAMS = '{"KeySchema":[{"KeyType":"HASH","AttributeName":"Id"}],"TableName":"TestTable","AttributeDefinitions":[{"AttributeName":"Id","AttributeType":"S"}],"ProvisionedThroughput":{"WriteCapacityUnits":5,"ReadCapacityUnits":5}}'
@@ -514,7 +514,7 @@
514514
"headers": {
515515
"Authorization": "AWS4-HMAC-SHA256 Credential="
516516
+ ACCESS_KEY_ID
517-
+ "/20200811/us-east-2/ec2/aws4_request, SignedHeaders=host;x-amz-date;x-amz-security-token, Signature=631ea80cddfaa545fdadb120dc92c9f18166e38a5c47b50fab9fce476e022855",
517+
+ "/20200811/us-east-2/ec2/aws4_request, SignedHeaders=host;x-amz-date;x-amz-security-token, Signature=41e226f997bf917ec6c9b2b14218df0874225f13bb153236c247881e614fafc9",
518518
"host": "ec2.us-east-2.amazonaws.com",
519519
"x-amz-date": "20200811T065522Z",
520520
"x-amz-security-token": TOKEN,
@@ -540,7 +540,7 @@
540540
"headers": {
541541
"Authorization": "AWS4-HMAC-SHA256 Credential="
542542
+ ACCESS_KEY_ID
543-
+ "/20200811/us-east-2/sts/aws4_request, SignedHeaders=host;x-amz-date;x-amz-security-token, Signature=73452984e4a880ffdc5c392355733ec3f5ba310d5e0609a89244440cadfe7a7a",
543+
+ "/20200811/us-east-2/sts/aws4_request, SignedHeaders=host;x-amz-date;x-amz-security-token, Signature=596aa990b792d763465d73703e684ca273c45536c6d322c31be01a41d02e5b60",
544544
"host": "sts.us-east-2.amazonaws.com",
545545
"x-amz-date": "20200811T065522Z",
546546
"x-amz-security-token": TOKEN,
@@ -562,7 +562,7 @@
562562
"headers": {
563563
"Authorization": "AWS4-HMAC-SHA256 Credential="
564564
+ ACCESS_KEY_ID
565-
+ "/20200811/us-east-2/sts/aws4_request, SignedHeaders=host;x-amz-date, Signature=d095ba304919cd0d5570ba8a3787884ee78b860f268ed040ba23831d55536d56",
565+
+ "/20200811/us-east-2/sts/aws4_request, SignedHeaders=host;x-amz-date, Signature=9e722e5b7bfa163447e2a14df118b45ebd283c5aea72019bdf921d6e7dc01a9a",
566566
"host": "sts.us-east-2.amazonaws.com",
567567
"x-amz-date": "20200811T065522Z",
568568
},
@@ -592,7 +592,7 @@
592592
"headers": {
593593
"Authorization": "AWS4-HMAC-SHA256 Credential="
594594
+ ACCESS_KEY_ID
595-
+ "/20200811/us-east-2/dynamodb/aws4_request, SignedHeaders=content-type;host;x-amz-date;x-amz-security-token;x-amz-target, Signature=fdaa5b9cc9c86b80fe61eaf504141c0b3523780349120f2bd8145448456e0385",
595+
+ "/20200811/us-east-2/dynamodb/aws4_request, SignedHeaders=content-type;host;x-amz-date;x-amz-security-token;x-amz-target, Signature=eb8bce0e63654bba672d4a8acb07e72d69210c1797d56ce024dbbc31beb2a2c7",
596596
"host": "dynamodb.us-east-2.amazonaws.com",
597597
"x-amz-date": "20200811T065522Z",
598598
"Content-Type": "application/x-amz-json-1.0",

0 commit comments

Comments
 (0)