turbaszek commented on a change in pull request #9761:
URL: https://github.com/apache/airflow/pull/9761#discussion_r453183487
##########
File path: airflow/providers/google/cloud/utils/credentials_provider.py
##########
@@ -194,64 +190,115 @@ def get_credentials_and_project_id(
:param keyfile_dict: A dict representing GCP Credential as in the
Credential JSON file
:type keyfile_dict: Dict[str, str]
:param scopes: OAuth scopes for the connection
- :type scopes: Sequence[str]
+ :type scopes: Collection[str]
:param delegate_to: The account to impersonate, if any.
For this to work, the service account making the request must have
domain-wide delegation enabled.
:type delegate_to: str
- :return: Google Auth Credentials
- :type: google.auth.credentials.Credentials
+ :param disable_logging: If true, disable all log messages, which allows
you to use this
+ class to configure Logger.
"""
- if key_path and keyfile_dict:
- raise AirflowException(
- "The `keyfile_dict` and `key_path` fields are mutually exclusive. "
- "Please provide only one value."
- )
- if not key_path and not keyfile_dict:
- log.info(
- 'Getting connection using `google.auth.default()` since no key
file is defined for hook.'
- )
- credentials, project_id = google.auth.default(scopes=scopes)
- elif key_path:
- # Get credentials from a JSON file.
- if key_path.endswith('.json'):
- log.debug('Getting connection using JSON key file %s', key_path)
- credentials = (
-
google.oauth2.service_account.Credentials.from_service_account_file(
- key_path, scopes=scopes)
- )
- project_id = credentials.project_id
- elif key_path.endswith('.p12'):
+ def __init__(
+ self,
+ key_path: Optional[str] = None,
+ keyfile_dict: Optional[Dict[str, str]] = None,
+ # See: https://github.com/PyCQA/pylint/issues/2377
+ scopes: Optional[Collection[str]] = None, # pylint:
disable=unsubscriptable-object
+ delegate_to: Optional[str] = None,
+ disable_logging: bool = False
+ ):
+ super().__init__()
+ if key_path and keyfile_dict:
raise AirflowException(
- 'Legacy P12 key file are not supported, use a JSON key file.'
+ "The `keyfile_dict` and `key_path` fields are mutually
exclusive. "
+ "Please provide only one value."
)
+ self.key_path = key_path
+ self.keyfile_dict = keyfile_dict
+ self.scopes = scopes
+ self.delegate_to = delegate_to
+ self.disable_logging = disable_logging
+
+ def get_credentials_and_project(self):
+ """
+ Get current credentials and project ID.
+
+ :return: Google Auth Credentials
+ :type: Tuple[google.auth.credentials.Credentials, str]
+ """
+ if not self.key_path and not self.keyfile_dict:
+ credentials, project_id = self._get_credentials_using_adc()
+ elif self.key_path:
+ credentials, project_id = self._get_credentials_using_key_path()
+ elif self.keyfile_dict:
+ credentials, project_id =
self._get_credentials_using_keyfile_dict()
Review comment:
```suggestion
if self.key_path:
credentials, project_id = self._get_credentials_using_key_path()
elif self.keyfile_dict:
credentials, project_id =
self._get_credentials_using_keyfile_dict()
else:
credentials, project_id = self._get_credentials_using_adc()
```
Will give the same result I think and theres no reason for the ValueError.
If both are False we use `_get_credentials_using_adc` (last branch), if any is
True then we follow one of the first branches
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]