|
| 1 | +""" |
| 2 | +Authentication and authorization utilities. |
| 3 | +
|
| 4 | +""" |
| 5 | +import logging |
| 6 | + |
| 7 | +import google.auth.compute_engine |
| 8 | +import google.auth.credentials |
| 9 | +import google.auth.exceptions |
| 10 | + |
| 11 | +from . import exceptions |
| 12 | +from .common import GcpCredentials |
| 13 | + |
| 14 | +# Make sure package 'cryptography' is available for 'google.auth', which prefers that lib instead |
| 15 | +# of falling back (silently) to package 'rsa' (pure Python). |
| 16 | +# https://github.com/googleapis/google-auth-library-python/blob/v1.5.1/google/auth/crypt/rsa.py#L19 |
| 17 | +try: |
| 18 | + import google.auth.crypt._cryptography_rsa |
| 19 | +except ImportError as exc: # pragma: no cover |
| 20 | + msg = "Package 'cryptography' is required for optimum performance of 'google.auth'." |
| 21 | + raise ImportError(msg) from exc |
| 22 | + |
| 23 | + |
| 24 | +logger = logging.getLogger(__name__) |
| 25 | + |
| 26 | + |
| 27 | +def get_env_default_credentials() -> GcpCredentials: |
| 28 | + """ |
| 29 | + Return the default credentials for the current GCP environment. |
| 30 | +
|
| 31 | + .. warning:: if the env var ``GOOGLE_APPLICATION_CREDENTIALS`` is set, then |
| 32 | + the returned value might correspond to something else. |
| 33 | +
|
| 34 | + """ |
| 35 | + try: |
| 36 | + credentials, _ = google.auth.default() |
| 37 | + except google.auth.exceptions.DefaultCredentialsError as exc: |
| 38 | + raise exceptions.AuthError from exc |
| 39 | + return credentials |
| 40 | + |
| 41 | + |
| 42 | +def get_env_project_id() -> str: |
| 43 | + """ |
| 44 | + Return the project ID of the current GCP environment. |
| 45 | +
|
| 46 | + .. warning:: if the env var ``GOOGLE_APPLICATION_CREDENTIALS`` is set, then |
| 47 | + the returned value might correspond to something else. |
| 48 | +
|
| 49 | + """ |
| 50 | + try: |
| 51 | + _, project_id = google.auth.default() |
| 52 | + except google.auth.exceptions.DefaultCredentialsError as exc: |
| 53 | + raise exceptions.AuthError from exc |
| 54 | + if not isinstance(project_id, str): |
| 55 | + raise exceptions.Error("Unexpected Google Auth lib response.", project_id) |
| 56 | + |
| 57 | + return project_id |
| 58 | + |
| 59 | + |
| 60 | +def get_gce_credentials(service_account_email: str =None) -> GcpCredentials: |
| 61 | + """ |
| 62 | + Return credentials provided by Compute Engine service account. |
| 63 | +
|
| 64 | + .. warning:: This function does not attempt to authenticate or verify that |
| 65 | + the ``service_account_email`` does indeed exist. It will return a |
| 66 | + credentials object anyway. |
| 67 | +
|
| 68 | + A Compute Engine instance may have multiple service accounts. |
| 69 | +
|
| 70 | + `Google's Auth Library for Python docs`_ say: |
| 71 | +
|
| 72 | + "Applications running on Compute Engine, Container Engine, or the |
| 73 | + App Engine flexible environment can obtain credentials provided by |
| 74 | + Compute Engine service accounts." |
| 75 | +
|
| 76 | + .. _Google's Auth Library for Python docs: |
| 77 | + https://google-auth.readthedocs.io/en/latest/user-guide.html#compute-engine-container-engine-and-the-app-engine-flexible-environment |
| 78 | +
|
| 79 | + """ |
| 80 | + service_account_email = service_account_email or 'default' |
| 81 | + return google.auth.compute_engine.Credentials(service_account_email) |
| 82 | + |
| 83 | + |
| 84 | +def load_credentials_from_file(filename: str) -> GcpCredentials: |
| 85 | + credentials, _ = google.auth._default._load_credentials_from_file(filename) |
| 86 | + return credentials |
0 commit comments