Minor: Fixes Bigquery External Account AttributeError (#16801)

This commit is contained in:
Ayush Shah 2024-06-26 20:19:48 +05:30 committed by GitHub
parent 3f5bc1948d
commit 2e694b5180
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 21 additions and 1 deletions

View File

@ -137,12 +137,21 @@ def set_google_credentials(gcp_credentials: GCPCredentials) -> None:
os.environ[GOOGLE_CREDENTIALS] = str(gcp_credentials.gcpConfig.root) os.environ[GOOGLE_CREDENTIALS] = str(gcp_credentials.gcpConfig.root)
return return
if gcp_credentials.gcpConfig.projectId is None: if (
isinstance(gcp_credentials.gcpConfig, GcpCredentialsValues)
and gcp_credentials.gcpConfig.projectId is None
):
logger.info( logger.info(
"No credentials available, using the current environment permissions authenticated via gcloud SDK." "No credentials available, using the current environment permissions authenticated via gcloud SDK."
) )
return return
if isinstance(gcp_credentials.gcpConfig, GcpExternalAccount):
logger.info(
"Using External account credentials to authenticate with GCP services."
)
return
if isinstance(gcp_credentials.gcpConfig, GcpCredentialsValues): if isinstance(gcp_credentials.gcpConfig, GcpCredentialsValues):
if ( if (
gcp_credentials.gcpConfig.projectId gcp_credentials.gcpConfig.projectId

View File

@ -15,6 +15,7 @@ from unittest import TestCase
from pydantic import AnyUrl, SecretStr from pydantic import AnyUrl, SecretStr
from metadata.generated.schema.security.credentials.gcpCredentials import GCPCredentials
from metadata.generated.schema.security.credentials.gcpExternalAccount import ( from metadata.generated.schema.security.credentials.gcpExternalAccount import (
GcpExternalAccount, GcpExternalAccount,
) )
@ -24,7 +25,9 @@ from metadata.generated.schema.security.credentials.gcpValues import (
from metadata.utils.credentials import ( from metadata.utils.credentials import (
InvalidPrivateKeyException, InvalidPrivateKeyException,
build_google_credentials_dict, build_google_credentials_dict,
set_google_credentials,
) )
from metadata.utils.logger import Loggers
class TestCredentials(TestCase): class TestCredentials(TestCase):
@ -105,3 +108,11 @@ VEhPQF0i0tUU7Fl071hcYaiQoZx4nIjN+NG6p5QKbl6k
} }
self.assertEqual(expected_dict, build_google_credentials_dict(gcp_values)) self.assertEqual(expected_dict, build_google_credentials_dict(gcp_values))
with self.assertLogs(Loggers.UTILS.value, level="INFO") as log:
set_google_credentials(
GCPCredentials(gcpConfig=gcp_values, gcpImpersonateServiceAccount=None)
)
self.assertIn(
"Using External account credentials to authenticate with GCP services.",
log.output[0],
)