예제 #1
0
 def execute_with_ctx(cls, cmd: List[str]):
     """
     Executes command with context created by provide_aws_context.
     """
     executor = get_executor()
     with provide_aws_context():
         executor.execute_cmd(cmd=cmd)
예제 #2
0
 def execute_with_ctx(cmd: List[str], key: str = GCP_GCS_KEY):
     """
     Executes command with context created by provide_gcp_context and activated
     service key.
     """
     executor = get_executor()
     with provide_gcp_context(key), GoogleSystemTest.authentication():
         env = os.environ.copy()
         executor.execute_cmd(cmd=cmd, env=env)
 def execute_with_ctx(cls, cmd: List[str], key: str = GCP_GCS_KEY, project_id=None, scopes=None):
     """
     Executes command with context created by provide_gcp_context and activated
     service key.
     """
     executor = get_executor()
     current_project_id = project_id or cls._project_id()
     with provide_gcp_context(key, project_id=current_project_id, scopes=scopes):
         executor.execute_cmd(cmd=cmd)
예제 #4
0
 def _revoke_authentication():
     """
     Change default authentication to none - which is not existing one.
     """
     executor = get_executor()
     executor.execute_cmd([
         "gcloud",
         "config",
         "set",
         "account",
         "none",
         f"--project={GoogleSystemTest._project_id()}",
     ])
예제 #5
0
 def _authenticate():
     """
     Authenticate with service account specified via key name.
     Required only when we use gcloud / gsutil.
     """
     executor = get_executor()
     executor.execute_cmd([
         "gcloud",
         "auth",
         "activate-service-account",
         f"--key-file={GoogleSystemTest._service_key()}",
         f"--project={GoogleSystemTest._project_id()}",
     ])
예제 #6
0
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.
    :type project_id: str
    """
    key_file_path = resolve_full_gcp_key_path(key_file_path)  # type: ignore
    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
예제 #7
0
 def check_output(*args, **kwargs):
     executor = get_executor()
     return executor.check_output(*args, **kwargs)
예제 #8
0
 def execute_cmd(*args, **kwargs):
     executor = get_executor()
     return executor.execute_cmd(*args, **kwargs)