Skip to content

Commit 58b66ec

Browse files
fix: Deprecating generic load methods and adding warnings on few cred types (#1812)
* fix: Deprecating generic load methods and adding warnings on few cred types * update secret * extra line * remove being * method to class * secret update * secret update * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md --------- Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent 1e8a867 commit 58b66ec

File tree

7 files changed

+163
-4
lines changed

7 files changed

+163
-4
lines changed

google/auth/_default.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,38 @@
5959
https://cloud.google.com/docs/authentication/adc-troubleshooting/user-creds. \
6060
"""
6161

62+
_GENERIC_LOAD_METHOD_WARNING = """\
63+
The {} method is deprecated because of a potential security risk.
64+
65+
This method does not validate the credential configuration. The security
66+
risk occurs when a credential configuration is accepted from a source that
67+
is not under your control and used without validation on your side.
68+
69+
If you know that you will be loading credential configurations of a
70+
specific type, it is recommended to use a credential-type-specific
71+
load method.
72+
This will ensure that an unexpected credential type with potential for
73+
malicious intent is not loaded unintentionally. You might still have to do
74+
validation for certain credential types. Please follow the recommendations
75+
for that method. For example, if you want to load only service accounts,
76+
you can create the service account credentials explicitly:
77+
78+
```
79+
from google.oauth2 import service_account
80+
creds = service_account.Credentials.from_service_account_file(filename)
81+
```
82+
83+
If you are loading your credential configuration from an untrusted source and have
84+
not mitigated the risks (e.g. by validating the configuration yourself), make
85+
these changes as soon as possible to prevent security risks to your environment.
86+
87+
Regardless of the method used, it is always your responsibility to validate
88+
configurations received from external sources.
89+
90+
Refer to https://cloud.google.com/docs/authentication/external/externally-sourced-credentials
91+
for more details.
92+
"""
93+
6294
# The subject token type used for AWS external_account credentials.
6395
_AWS_SUBJECT_TOKEN_TYPE = "urn:ietf:params:aws:token-type:aws4_request"
6496

@@ -76,6 +108,20 @@ def _warn_about_problematic_credentials(credentials):
76108
warnings.warn(_CLOUD_SDK_CREDENTIALS_WARNING)
77109

78110

111+
def _warn_about_generic_load_method(method_name): # pragma: NO COVER
112+
"""Warns that a generic load method is being used.
113+
114+
This is to discourage use of the generic load methods in favor of
115+
more specific methods. The generic methods are more likely to lead to
116+
security issues if the input is not validated.
117+
118+
Args:
119+
method_name (str): The name of the method being used.
120+
"""
121+
122+
warnings.warn(_GENERIC_LOAD_METHOD_WARNING.format(method_name), DeprecationWarning)
123+
124+
79125
def load_credentials_from_file(
80126
filename, scopes=None, default_scopes=None, quota_project_id=None, request=None
81127
):
@@ -121,6 +167,8 @@ def load_credentials_from_file(
121167
google.auth.exceptions.DefaultCredentialsError: if the file is in the
122168
wrong format or is missing.
123169
"""
170+
_warn_about_generic_load_method("load_credentials_from_file")
171+
124172
if not os.path.exists(filename):
125173
raise exceptions.DefaultCredentialsError(
126174
"File {} was not found.".format(filename)
@@ -184,6 +232,7 @@ def load_credentials_from_dict(
184232
google.auth.exceptions.DefaultCredentialsError: if the file is in the
185233
wrong format or is missing.
186234
"""
235+
_warn_about_generic_load_method("load_credentials_from_dict")
187236
if not isinstance(info, dict):
188237
raise exceptions.DefaultCredentialsError(
189238
"info object was of type {} but dict type was expected.".format(type(info))

google/auth/external_account.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,14 @@ class Credentials(
8989
credentials for Google access token and authorizing requests to Google APIs.
9090
The base class implements the common logic for exchanging external account
9191
credentials for Google access tokens.
92-
"""
92+
93+
**IMPORTANT**:
94+
This class does not validate the credential configuration. A security
95+
risk occurs when a credential configuration configured with malicious urls
96+
is used.
97+
When the credential configuration is accepted from an
98+
untrusted source, you should validate it before using.
99+
Refer https://cloud.google.com/docs/authentication/external/externally-sourced-credentials for more details."""
93100

94101
def __init__(
95102
self,
@@ -576,6 +583,14 @@ def _get_mtls_cert_and_key_paths(self):
576583
def from_info(cls, info, **kwargs):
577584
"""Creates a Credentials instance from parsed external account info.
578585
586+
**IMPORTANT**:
587+
This method does not validate the credential configuration. A security
588+
risk occurs when a credential configuration configured with malicious urls
589+
is used.
590+
When the credential configuration is accepted from an
591+
untrusted source, you should validate it before using with this method.
592+
Refer https://cloud.google.com/docs/authentication/external/externally-sourced-credentials for more details.
593+
579594
Args:
580595
info (Mapping[str, str]): The external account info in Google
581596
format.
@@ -615,6 +630,14 @@ def from_info(cls, info, **kwargs):
615630
def from_file(cls, filename, **kwargs):
616631
"""Creates a Credentials instance from an external account json file.
617632
633+
**IMPORTANT**:
634+
This method does not validate the credential configuration. A security
635+
risk occurs when a credential configuration configured with malicious urls
636+
is used.
637+
When the credential configuration is accepted from an
638+
untrusted source, you should validate it before using with this method.
639+
Refer https://cloud.google.com/docs/authentication/external/externally-sourced-credentials for more details.
640+
618641
Args:
619642
filename (str): The path to the external account json file.
620643
kwargs: Additional arguments to pass to the constructor.

google/auth/external_account_authorized_user.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,14 @@ class Credentials(
6060
The credentials are considered immutable. If you want to modify the
6161
quota project, use `with_quota_project` and if you want to modify the token
6262
uri, use `with_token_uri`.
63-
"""
63+
64+
**IMPORTANT**:
65+
This class does not validate the credential configuration. A security
66+
risk occurs when a credential configuration configured with malicious urls
67+
is used.
68+
When the credential configuration is accepted from an
69+
untrusted source, you should validate it before using.
70+
Refer https://cloud.google.com/docs/authentication/external/externally-sourced-credentials for more details."""
6471

6572
def __init__(
6673
self,
@@ -328,6 +335,14 @@ def with_universe_domain(self, universe_domain):
328335
def from_info(cls, info, **kwargs):
329336
"""Creates a Credentials instance from parsed external account info.
330337
338+
**IMPORTANT**:
339+
This method does not validate the credential configuration. A security
340+
risk occurs when a credential configuration configured with malicious urls
341+
is used.
342+
When the credential configuration is accepted from an
343+
untrusted source, you should validate it before using with this method.
344+
Refer https://cloud.google.com/docs/authentication/external/externally-sourced-credentials for more details.
345+
331346
Args:
332347
info (Mapping[str, str]): The external account info in Google
333348
format.
@@ -367,6 +382,14 @@ def from_info(cls, info, **kwargs):
367382
def from_file(cls, filename, **kwargs):
368383
"""Creates a Credentials instance from an external account json file.
369384
385+
**IMPORTANT**:
386+
This method does not validate the credential configuration. A security
387+
risk occurs when a credential configuration configured with malicious urls
388+
is used.
389+
When the credential configuration is accepted from an
390+
untrusted source, you should validate it before using with this method.
391+
Refer https://cloud.google.com/docs/authentication/external/externally-sourced-credentials for more details.
392+
370393
Args:
371394
filename (str): The path to the external account json file.
372395
kwargs: Additional arguments to pass to the constructor.

google/auth/identity_pool.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,15 @@ def _parse_token_data(token_content, format_type="text", subject_token_field_nam
253253

254254

255255
class Credentials(external_account.Credentials):
256-
"""External account credentials sourced from files and URLs."""
256+
"""External account credentials sourced from files and URLs.
257+
258+
**IMPORTANT**:
259+
This class does not validate the credential configuration. A security
260+
risk occurs when a credential configuration configured with malicious urls
261+
is used.
262+
When the credential configuration is accepted from an
263+
untrusted source, you should validate it before using.
264+
Refer https://cloud.google.com/docs/authentication/external/externally-sourced-credentials for more details."""
257265

258266
def __init__(
259267
self,
@@ -497,6 +505,14 @@ def _validate_single_source(self):
497505
def from_info(cls, info, **kwargs):
498506
"""Creates an Identity Pool Credentials instance from parsed external account info.
499507
508+
**IMPORTANT**:
509+
This method does not validate the credential configuration. A security
510+
risk occurs when a credential configuration configured with malicious urls
511+
is used.
512+
When the credential configuration is accepted from an
513+
untrusted source, you should validate it before using with this method.
514+
Refer https://cloud.google.com/docs/authentication/external/externally-sourced-credentials for more details.
515+
500516
Args:
501517
info (Mapping[str, str]): The Identity Pool external account info in Google
502518
format.
@@ -517,6 +533,14 @@ def from_info(cls, info, **kwargs):
517533
def from_file(cls, filename, **kwargs):
518534
"""Creates an IdentityPool Credentials instance from an external account json file.
519535
536+
**IMPORTANT**:
537+
This method does not validate the credential configuration. A security
538+
risk occurs when a credential configuration configured with malicious urls
539+
is used.
540+
When the credential configuration is accepted from an
541+
untrusted source, you should validate it before using with this method.
542+
Refer https://cloud.google.com/docs/authentication/external/externally-sourced-credentials for more details.
543+
520544
Args:
521545
filename (str): The path to the IdentityPool external account json file.
522546
kwargs: Additional arguments to pass to the constructor.

google/auth/impersonated_credentials.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,14 @@ class Credentials(
184184
buckets = client.list_buckets(project='your_project')
185185
for bucket in buckets:
186186
print(bucket.name)
187+
188+
**IMPORTANT**:
189+
This class does not validate the credential configuration. A security
190+
risk occurs when a credential configuration configured with malicious urls
191+
is used.
192+
When the credential configuration is accepted from an
193+
untrusted source, you should validate it before using.
194+
Refer https://cloud.google.com/docs/authentication/external/externally-sourced-credentials for more details.
187195
"""
188196

189197
def __init__(
@@ -454,6 +462,14 @@ def with_scopes(self, scopes, default_scopes=None):
454462
def from_impersonated_service_account_info(cls, info, scopes=None):
455463
"""Creates a Credentials instance from parsed impersonated service account credentials info.
456464
465+
**IMPORTANT**:
466+
This method does not validate the credential configuration. A security
467+
risk occurs when a credential configuration configured with malicious urls
468+
is used.
469+
When the credential configuration is accepted from an
470+
untrusted source, you should validate it before using with this method.
471+
Refer https://cloud.google.com/docs/authentication/external/externally-sourced-credentials for more details.
472+
457473
Args:
458474
info (Mapping[str, str]): The impersonated service account credentials info in Google
459475
format.

google/auth/pluggable.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,15 @@
5757

5858

5959
class Credentials(external_account.Credentials):
60-
"""External account credentials sourced from executables."""
60+
"""External account credentials sourced from executables.
61+
62+
**IMPORTANT**:
63+
This class does not validate the credential configuration. A security
64+
risk occurs when a credential configuration configured with malicious urls
65+
is used.
66+
When the credential configuration is accepted from an
67+
untrusted source, you should validate it before using.
68+
Refer https://cloud.google.com/docs/authentication/external/externally-sourced-credentials for more details."""
6169

6270
def __init__(
6371
self,
@@ -300,6 +308,14 @@ def external_account_id(self):
300308
def from_info(cls, info, **kwargs):
301309
"""Creates a Pluggable Credentials instance from parsed external account info.
302310
311+
**IMPORTANT**:
312+
This method does not validate the credential configuration. A security
313+
risk occurs when a credential configuration configured with malicious urls
314+
is used.
315+
When the credential configuration is accepted from an
316+
untrusted source, you should validate it before using with this method.
317+
Refer https://cloud.google.com/docs/authentication/external/externally-sourced-credentials for more details.
318+
303319
Args:
304320
info (Mapping[str, str]): The Pluggable external account info in Google
305321
format.
@@ -319,6 +335,14 @@ def from_info(cls, info, **kwargs):
319335
def from_file(cls, filename, **kwargs):
320336
"""Creates an Pluggable Credentials instance from an external account json file.
321337
338+
**IMPORTANT**:
339+
This method does not validate the credential configuration. A security
340+
risk occurs when a credential configuration configured with malicious urls
341+
is used.
342+
When the credential configuration is accepted from an
343+
untrusted source, you should validate it before using with this method.
344+
Refer https://cloud.google.com/docs/authentication/external/externally-sourced-credentials for more details.
345+
322346
Args:
323347
filename (str): The path to the Pluggable external account json file.
324348
kwargs: Additional arguments to pass to the constructor.

system_tests/secrets.tar.enc

0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)