def provide_gcp_context( key_file_path: Optional[str] = None, scopes: Optional[Sequence] = None, project_id: Optional[str] = None, ): """ Context manager that provides both: - GCP credentials for application supporting `Application Default Credentials (ADC) strategy <https://cloud.google.com/docs/authentication/production>`__. - temporary value of ``AIRFLOW_CONN_GOOGLE_CLOUD_DEFAULT`` connection Moreover it resolves full path to service keys so user can pass ``myservice.json`` as ``key_file_path``. :param key_file_path: Path to file with GCP credentials .json file. :type key_file_path: str :param scopes: OAuth scopes for the connection :type scopes: Sequence :param project_id: The id of GCP project for the connection. :type project_id: str """ key_file_path = resolve_full_gcp_key_path(key_file_path) # type: ignore return provide_gcp_conn_and_credentials(key_file_path=key_file_path, scopes=scopes, project_id=project_id)
def test_provide_gcp_conn_and_credentials(self, mock_builder): mock_builder.return_value = TEMP_VARIABLE path = "path/to/file.json" scopes = ["scopes"] project_id = "project_id" with provide_gcp_conn_and_credentials(path, scopes, project_id): mock_builder.assert_called_once_with(key_file_path=path, scopes=scopes, project_id=project_id) self.assertEqual(os.environ[AIRFLOW_CONN_GOOGLE_CLOUD_DEFAULT], TEMP_VARIABLE) self.assertEqual(os.environ[CREDENTIALS], path) self.assertEqual(os.environ[AIRFLOW_CONN_GOOGLE_CLOUD_DEFAULT], ENV_VALUE) self.assertEqual(os.environ[CREDENTIALS], ENV_VALUE)
def provide_gcp_context( key_file_path: Optional[str] = None, scopes: Optional[Sequence] = None, project_id: Optional[str] = None, ): """ Context manager that provides: - GCP credentials for application supporting `Application Default Credentials (ADC) strategy <https://cloud.google.com/docs/authentication/production>`__. - temporary value of :envvar:`AIRFLOW_CONN_GOOGLE_CLOUD_DEFAULT` variable - the ``gcloud`` config directory isolated from user configuration Moreover it resolves full path to service keys so user can pass ``myservice.json`` as ``key_file_path``. :param key_file_path: Path to file with GCP credentials .json file. :type key_file_path: str :param scopes: OAuth scopes for the connection :type scopes: Sequence :param project_id: The id of GCP project for the connection. Default: ``os.environ["GCP_PROJECT_ID"]`` or None :type project_id: str """ key_file_path = resolve_full_gcp_key_path(key_file_path) # type: ignore if project_id is None: project_id = os.environ.get("GCP_PROJECT_ID") with provide_gcp_conn_and_credentials( key_file_path, scopes, project_id ), tempfile.TemporaryDirectory() as gcloud_config_tmp, mock.patch.dict( 'os.environ', {CLOUD_SDK_CONFIG_DIR: gcloud_config_tmp}): executor = get_executor() if project_id: executor.execute_cmd( ["gcloud", "config", "set", "core/project", project_id]) if key_file_path: executor.execute_cmd([ "gcloud", "auth", "activate-service-account", f"--key-file={key_file_path}", ]) yield