Skip to content

Commit c384f9b

Browse files
authored
GCP Secret Manager error handling for missing credentials (#17264)
1 parent 36bdfe8 commit c384f9b

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

airflow/providers/google/cloud/secrets/secret_manager.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,24 @@
1616
# under the License.
1717

1818
"""Objects relating to sourcing connections from Google Cloud Secrets Manager"""
19+
import logging
1920
from typing import Optional
2021

2122
try:
2223
from functools import cached_property
2324
except ImportError:
2425
from cached_property import cached_property
2526

27+
from google.auth.exceptions import DefaultCredentialsError
28+
2629
from airflow.exceptions import AirflowException
2730
from airflow.providers.google.cloud._internal_client.secret_manager_client import _SecretManagerClient
2831
from airflow.providers.google.cloud.utils.credentials_provider import get_credentials_and_project_id
2932
from airflow.secrets import BaseSecretsBackend
3033
from airflow.utils.log.logging_mixin import LoggingMixin
3134

35+
log = logging.getLogger(__name__)
36+
3237
SECRET_ID_PATTERN = r"^[a-zA-Z0-9-_]*$"
3338

3439

@@ -101,9 +106,17 @@ def __init__(
101106
"`connections_prefix`, `variables_prefix` and `sep` should "
102107
f"follows that pattern {SECRET_ID_PATTERN}"
103108
)
104-
self.credentials, self.project_id = get_credentials_and_project_id(
105-
keyfile_dict=gcp_keyfile_dict, key_path=gcp_key_path, scopes=gcp_scopes
106-
)
109+
try:
110+
self.credentials, self.project_id = get_credentials_and_project_id(
111+
keyfile_dict=gcp_keyfile_dict, key_path=gcp_key_path, scopes=gcp_scopes
112+
)
113+
except (DefaultCredentialsError, FileNotFoundError):
114+
log.exception(
115+
'Unable to load credentials for GCP Secret Manager. '
116+
'Make sure that the keyfile path, dictionary, or GOOGLE_APPLICATION_CREDENTIALS '
117+
'environment variable is correct and properly configured.'
118+
)
119+
107120
# In case project id provided
108121
if project_id:
109122
self.project_id = project_id

0 commit comments

Comments
 (0)