5959 https://cloud.google.com/docs/authentication/adc-troubleshooting/user-creds. \
6060 """
6161
62+ _GENERIC_LOAD_METHOD_WARNING = """\
63+ The {} method is being 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
6597
98+ class GenericLoadMethodWarning (DeprecationWarning ): # pragma: NO COVER
99+ """
100+ Deprecation warning raised when a generic load method is used.
101+ """
102+
103+ pass
104+
105+
66106def _warn_about_problematic_credentials (credentials ):
67107 """Determines if the credentials are problematic.
68108
@@ -75,6 +115,23 @@ def _warn_about_problematic_credentials(credentials):
75115 if credentials .client_id == _cloud_sdk .CLOUD_SDK_CLIENT_ID :
76116 warnings .warn (_CLOUD_SDK_CREDENTIALS_WARNING )
77117
118+ def _warn_about_generic_load_method (method_name ): # pragma: NO COVER
119+ """Warns that a generic load method is being used.
120+
121+ This is to discourage use of the generic load methods in favor of
122+ more specific methods. The generic methods are more likely to lead to
123+ security issues if the input is not validated.
124+
125+ Args:
126+ method_name (str): The name of the method being used.
127+ """
128+
129+
130+ warnings .warn (
131+ _GENERIC_LOAD_METHOD_WARNING .format (method_name ),
132+ GenericLoadMethodWarning ,
133+ )
134+
78135
79136def load_credentials_from_file (
80137 filename , scopes = None , default_scopes = None , quota_project_id = None , request = None
@@ -121,6 +178,8 @@ def load_credentials_from_file(
121178 google.auth.exceptions.DefaultCredentialsError: if the file is in the
122179 wrong format or is missing.
123180 """
181+ _warn_about_generic_load_method ("load_credentials_from_file" )
182+
124183 if not os .path .exists (filename ):
125184 raise exceptions .DefaultCredentialsError (
126185 "File {} was not found." .format (filename )
@@ -184,6 +243,7 @@ def load_credentials_from_dict(
184243 google.auth.exceptions.DefaultCredentialsError: if the file is in the
185244 wrong format or is missing.
186245 """
246+ _warn_about_generic_load_method ("load_credentials_from_dict" )
187247 if not isinstance (info , dict ):
188248 raise exceptions .DefaultCredentialsError (
189249 "info object was of type {} but dict type was expected." .format (type (info ))
0 commit comments