Skip to content

Commit 0eaa611

Browse files
authored
Move sensitive information to the secret manager for the system test google_analytics_admin (#40951)
1 parent 29f456a commit 0eaa611

File tree

2 files changed

+34
-10
lines changed

2 files changed

+34
-10
lines changed

airflow/providers/google/marketing_platform/operators/analytics_admin.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,7 @@ class GoogleAnalyticsAdminGetGoogleAdsLinkOperator(GoogleCloudBaseOperator):
530530
"gcp_conn_id",
531531
"impersonation_chain",
532532
"google_ads_link_id",
533+
"property_id",
533534
)
534535

535536
def __init__(

tests/system/providers/google/marketing_platform/example_analytics_admin.py

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,14 @@
2424
2525
In order to run this test, make sure you followed steps:
2626
1. Login to https://analytics.google.com
27-
2. In the settings section create an account and save its ID in the variable GA_ACCOUNT_ID.
27+
2. In the settings section create an account and save its ID in the Google Cloud Secret Manager with id
28+
saved in the constant GOOGLE_ANALYTICS_ACCOUNT_SECRET_ID.
2829
3. In the settings section go to the Property access management page and add your service account email with
2930
Editor permissions. This service account should be created on behalf of the account from the step 1.
3031
4. Make sure Google Analytics Admin API is enabled in your GCP project.
3132
5. Create Google Ads account and link it to your Google Analytics account in the GA admin panel.
32-
6. Associate the Google Ads account with a property, and save this property's id in the variable
33-
GA_GOOGLE_ADS_PROPERTY_ID.
33+
6. Associate the Google Ads account with a property, and save this property's id in the Google Cloud Secret
34+
Manager with id saved in the constant GOOGLE_ADS_PROPERTY_ID.
3435
"""
3536

3637
from __future__ import annotations
@@ -41,11 +42,13 @@
4142
from datetime import datetime
4243

4344
from google.analytics import admin_v1beta as google_analytics
45+
from google.cloud.exceptions import NotFound
4446

4547
from airflow.decorators import task
4648
from airflow.models import Connection
4749
from airflow.models.dag import DAG
4850
from airflow.operators.bash import BashOperator
51+
from airflow.providers.google.cloud.hooks.secret_manager import GoogleCloudSecretManagerHook
4952
from airflow.providers.google.marketing_platform.operators.analytics_admin import (
5053
GoogleAnalyticsAdminCreateDataStreamOperator,
5154
GoogleAnalyticsAdminCreatePropertyOperator,
@@ -59,17 +62,25 @@
5962
from airflow.utils.trigger_rule import TriggerRule
6063

6164
ENV_ID = os.environ.get("SYSTEM_TESTS_ENV_ID")
62-
DAG_ID = "example_google_analytics_admin"
65+
DAG_ID = "google_analytics_admin"
6366

6467
CONNECTION_ID = f"connection_{DAG_ID}_{ENV_ID}"
65-
ACCOUNT_ID = os.environ.get("GA_ACCOUNT_ID", "123456789")
68+
GOOGLE_ANALYTICS_ACCOUNT_SECRET_ID = "google_analytics_account_id"
69+
GOOGLE_ADS_PROPERTY_SECRET_ID = "google_ads_property_id"
6670
PROPERTY_ID = "{{ task_instance.xcom_pull('create_property')['name'].split('/')[-1] }}"
6771
DATA_STREAM_ID = "{{ task_instance.xcom_pull('create_data_stream')['name'].split('/')[-1] }}"
68-
GA_GOOGLE_ADS_PROPERTY_ID = os.environ.get("GA_GOOGLE_ADS_PROPERTY_ID", "123456789")
6972
GA_ADS_LINK_ID = "{{ task_instance.xcom_pull('list_google_ads_links')[0]['name'].split('/')[-1] }}"
7073

7174
log = logging.getLogger(__name__)
7275

76+
77+
def get_secret(secret_id: str) -> str:
78+
hook = GoogleCloudSecretManagerHook()
79+
if hook.secret_exists(secret_id=secret_id):
80+
return hook.access_secret(secret_id=secret_id).payload.data.decode()
81+
raise NotFound("The secret '%s' not found", secret_id)
82+
83+
7384
with DAG(
7485
DAG_ID,
7586
schedule="@once", # Override to match your needs,
@@ -102,6 +113,18 @@ def setup_connection(**kwargs) -> None:
102113

103114
setup_connection_task = setup_connection()
104115

116+
@task
117+
def get_google_analytics_account_id():
118+
return get_secret(secret_id=GOOGLE_ANALYTICS_ACCOUNT_SECRET_ID)
119+
120+
get_google_analytics_account_id_task = get_google_analytics_account_id()
121+
122+
@task
123+
def get_google_ads_property_id():
124+
return get_secret(secret_id=GOOGLE_ADS_PROPERTY_SECRET_ID)
125+
126+
get_google_ads_property_id_task = get_google_ads_property_id()
127+
105128
# [START howto_marketing_platform_list_accounts_operator]
106129
list_accounts = GoogleAnalyticsAdminListAccountsOperator(
107130
task_id="list_account",
@@ -114,7 +137,7 @@ def setup_connection(**kwargs) -> None:
114137
create_property = GoogleAnalyticsAdminCreatePropertyOperator(
115138
task_id="create_property",
116139
analytics_property={
117-
"parent": f"accounts/{ACCOUNT_ID}",
140+
"parent": f"accounts/{get_google_analytics_account_id_task}",
118141
"display_name": "Test display name",
119142
"time_zone": "America/Los_Angeles",
120143
},
@@ -158,15 +181,15 @@ def setup_connection(**kwargs) -> None:
158181
# [START howto_marketing_platform_list_google_ads_links]
159182
list_google_ads_links = GoogleAnalyticsAdminListGoogleAdsLinksOperator(
160183
task_id="list_google_ads_links",
161-
property_id=GA_GOOGLE_ADS_PROPERTY_ID,
184+
property_id=get_google_ads_property_id_task,
162185
gcp_conn_id=CONNECTION_ID,
163186
)
164187
# [END howto_marketing_platform_list_google_ads_links]
165188

166189
# [START howto_marketing_platform_get_google_ad_link]
167190
get_ad_link = GoogleAnalyticsAdminGetGoogleAdsLinkOperator(
168191
task_id="get_ad_link",
169-
property_id=GA_GOOGLE_ADS_PROPERTY_ID,
192+
property_id=get_google_ads_property_id_task,
170193
google_ads_link_id=GA_ADS_LINK_ID,
171194
gcp_conn_id=CONNECTION_ID,
172195
)
@@ -180,7 +203,7 @@ def setup_connection(**kwargs) -> None:
180203

181204
(
182205
# TEST SETUP
183-
setup_connection_task
206+
[setup_connection_task, get_google_analytics_account_id_task, get_google_ads_property_id_task]
184207
# TEST BODY
185208
>> list_accounts
186209
>> create_property

0 commit comments

Comments
 (0)