Skip to content

Impersonation logic missing in BigQuery Async operators #34727

Description

@hkc-8010

Apache Airflow version

main (development)

What happened

BigQueryAsyncOperators uses writing credentials to a file using GoogleBaseHook.provide_gcp_credential_file_as_context() which has logic for key_path and keyfile_dict but logic for impersonation_chain method is missing.

When using Impersonation chain method, the Operator goes into the deferred state and the tasks fail with 403 Access Denied error.

What you think should happen instead

When the operator goes into deferred state, the triggerer should try generating credentials using an impersonated service account instead of the default service account.the

How to reproduce

We have set up an impersonation chain for authentication to BigQuery. Here's how it works:

We assign a Service Account to the Kubernetes namespace.
This namespace-level Service Account impersonates our team's Service Account, which has the necessary roles to access BigQuery.
When the operator runs, the worker inserts a job into BigQuery using the team's Service Account. After that, it defers itself and starts executing get_job to check the job's status.

However, during this process, we encountered a "403 Access Denied" error. After some debugging, we discovered that the Triggerer is checking the job status using the namespace-level Service Account, rather than the team's Service Account. To confirm this, we granted the necessary role to the namespace-level Service Account for checking job status, and after that, the task succeeded.

To setup impersonation_chain, we can refer to this documentation.

Operating System

Debian GNU/Linux 11 (bullseye)

Versions of Apache Airflow Providers

apache-airflow-providers-google==10.9.0

Deployment

Astronomer

Deployment details

Deploy Airflow on Kubernetes so that you can annotate gcloud caller service account to the Airflow worker service account and impersonate a privileged service account that has bigquery permissions to generate short-term credentials.

Anything else

This problem occurs everytime when you use bigquery operators in async mode.

Are you willing to submit PR?

  • Yes I am willing to submit a PR!

Code of Conduct

Activity

  1. hkc-8010 commented on Oct 3, 2023

    @hkc-8010
    ContributorAuthor
  2. added
    provider:googleGoogle (including GCP) related issues
    and removed
    needs-triagelabel for new issues that we didn't triage yet
    on Oct 3, 2023
  3. eladkal commented on Oct 4, 2023

    @eladkal
    Contributor

    cc @shahar1 would you have time to look into this?

  4. nathadfield commented on Dec 18, 2023

    @nathadfield
    Contributor

    @shahar1 I am bumping up against this issue currently and was wondering if you had been able to make any progress?

  5. melugoyal commented on Dec 20, 2023

    @melugoyal

    it looks like #35629 made an attempt to get BigQuery triggers working with impersonation. however i think one place was missed, specifically this invocation of BigQueryInsertJobTrigger. should be a simple fix to pass in self.impersonation_chain there as well

  6. nathadfield commented on Dec 21, 2023

    @nathadfield
    Contributor

    @melugoyal I think it is more complicated than that. As mentioned above, if you follow the trigger through it leads to GoogleBaseHook.provide_gcp_credential_file_as_context() where there isn't any provision for dealing with the impersonation chain.

    https://github.com/apache/airflow/blob/main/airflow/providers/google/common/hooks/base_google.py#L499-L532

  7. shahar1 commented on Dec 23, 2023

    @shahar1
    Contributor

    @melugoyal I think it is more complicated than that. As mentioned above, if you follow the trigger through it leads to GoogleBaseHook.provide_gcp_credential_file_as_context() where there isn't any provision for dealing with the impersonation chain.

    https://github.com/apache/airflow/blob/main/airflow/providers/google/common/hooks/base_google.py#L499-L532

    Apologies for the delayed response - I haven't managed to look up into this until now.
    As you stated, the problem is indeed more complicated, as GoogleBaseHook.provide_gcp_credential_file_as_context() is not provisioned with the impersonation chain.
    It seems that the reason that it hasn't been supported until now, is that the gcloud-aio library (which in turn creates the async Job instance) does not support impersonated credentials. See: #29535 and talkiq/gcloud-aio#421.

    The good news are that there's a fresh PR for implementing it: talkiq/gcloud-aio#665

    @eladkal FYI

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions