dybolo commented on code in PR #39873:
URL: https://github.com/apache/airflow/pull/39873#discussion_r1633653414


##########
airflow/providers/google/cloud/utils/credentials_provider.py:
##########
@@ -426,3 +459,38 @@ def 
_get_project_id_from_service_account_email(service_account_email: str) -> st
         raise AirflowException(
             f"Could not extract project_id from service account's email: 
{service_account_email}."
         )
+
+
+def _get_info_from_credential_configuration_file(
+    credential_configuration_file: str | dict[str, str],
+) -> dict[str, str]:
+    """
+    Extract the Credential Configuration File information, either from a json 
file, json string or dictionary.
+
+    :param credential_configuration_file: File path or content (as json string 
or dictionary) of a GCP credential configuration file.
+
+    :return: Returns a dictionary containing the Credential Configuration File 
information.
+    """
+    # if it's already a dict, just return it
+    if isinstance(credential_configuration_file, dict):
+        return credential_configuration_file
+
+    if not isinstance(credential_configuration_file, str):
+        raise AirflowException(
+            f"Invalid argument type, expected str or dict, got 
{type(credential_configuration_file)}."
+        )
+
+    if os.path.exists(credential_configuration_file):  # attempts to load from 
json file
+        with open(credential_configuration_file) as file_obj:
+            try:
+                return json.load(file_obj)
+            except ValueError:
+                raise AirflowException(
+                    f"Credential Configuration File 
'{credential_configuration_file}' is not a valid json file."
+                )
+
+    # if not a file, attempt to load it from a json string
+    try:
+        return json.loads(credential_configuration_file)
+    except ValueError:
+        raise AirflowException("Credential Configuration File is not a valid 
json string.")

Review Comment:
   We used the 
[_get_credentials_using_credential_config_file](https://github.com/apache/airflow/blob/main/airflow/providers/google/cloud/utils/credentials_provider.py#L336)
 function for reference since it was the only function making use of the 
Credential Configuration File field coming from the UI Form. It appears that 
when this field is provided through the UI as a text input, it can accept 
either a file path string or a JSON string literal.
   Please let me know what you think.



-- 
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.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to