Skip to content

Commit e9ea755

Browse files
authored
feat: use keycloak service account for DAG run (#431)
1 parent e604ee1 commit e9ea755

File tree

3 files changed

+55
-28
lines changed

3 files changed

+55
-28
lines changed

scripts/promote_collection.py

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import sys
66
import os
77
import uuid
8-
from base64 import b64encode
8+
import requests
99

1010

1111
def trigger_collection_dag(payload: Dict[str, Any], stage: str):
@@ -16,34 +16,41 @@ def trigger_collection_dag(payload: Dict[str, Any], stage: str):
1616

1717
if stage == "staging":
1818
api_url_env = "STAGING_SM2A_API_URL"
19-
username_env = "STAGING_SM2A_ADMIN_USERNAME"
20-
password_env = "STAGING_SM2A_ADMIN_PASSWORD"
19+
token_url = f"https://{os.getenv('KEYCLOAK_STAGING_URL')}/realms/veda/protocol/openid-connect/token"
20+
client_id = "airflow-webserver-fab"
21+
client_secret = os.getenv("KEYCLOAK_STAGING_SM2A_FAB_CLIENT_SECRET")
2122
elif stage == "production":
2223
api_url_env = "SM2A_API_URL"
23-
username_env = "SM2A_ADMIN_USERNAME"
24-
password_env = "SM2A_ADMIN_PASSWORD"
24+
token_url = f"https://{os.getenv('KEYCLOAK_PROD_URL')}/realms/veda/protocol/openid-connect/token"
25+
client_id = "airflow-webserver-fab"
26+
client_secret = os.getenv("KEYCLOAK_PROD_SM2A_FAB_CLIENT_SECRET")
2527
else:
2628
raise ValueError(
2729
f"Invalid stage provided: {stage}. Must be 'staging' or 'production'."
2830
)
2931

3032
base_api_url = os.getenv(api_url_env)
31-
username = os.getenv(username_env)
32-
password = os.getenv(password_env)
3333

34-
if not all([base_api_url, username, password]):
34+
response = requests.post(
35+
token_url,
36+
data={
37+
"client_id": client_id,
38+
"client_secret": client_secret,
39+
"grant_type": "client_credentials",
40+
},
41+
)
42+
access_token = response.json()["access_token"]
43+
44+
if not all([base_api_url, access_token]):
3545
raise ValueError(
3646
f"Missing one or more environment variables: "
3747
f"stage is None={stage is None}, "
38-
f"username is None={username_env is None}, "
39-
f"password is None={password_env is None}"
48+
f"access_token is None={access_token is None}"
4049
)
4150

42-
api_token = b64encode(f"{username}:{password}".encode()).decode()
43-
4451
headers = {
4552
"Content-Type": "application/json",
46-
"Authorization": "Basic " + api_token,
53+
"Authorization": "Bearer " + access_token,
4754
}
4855

4956
body = {

scripts/promote_dataset.py

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import sys
66
import os
77
import uuid
8-
from base64 import b64encode
8+
import requests
99

1010

1111
class MissingFieldError(Exception):
@@ -35,21 +35,30 @@ def validate_discovery_item_config(item: Dict[str, Any]) -> Dict[str, Any]:
3535
def publish_to_staging(payload):
3636
base_api_url = os.getenv("STAGING_SM2A_API_URL")
3737
dataset_pipeline_dag = os.getenv("DATASET_DAG_NAME", "veda_dataset_pipeline")
38-
username = os.getenv("STAGING_SM2A_ADMIN_USERNAME")
39-
password = os.getenv("STAGING_SM2A_ADMIN_PASSWORD")
4038

41-
api_token = b64encode(f"{username}:{password}".encode()).decode()
39+
token_url = f"https://{os.getenv('KEYCLOAK_STAGING_URL')}/realms/veda/protocol/openid-connect/token"
40+
client_id = "airflow-webserver-fab"
41+
client_secret = os.getenv("KEYCLOAK_STAGING_SM2A_FAB_CLIENT_SECRET")
42+
43+
response = requests.post(
44+
token_url,
45+
data={
46+
"client_id": client_id,
47+
"client_secret": client_secret,
48+
"grant_type": "client_credentials",
49+
},
50+
)
51+
access_token = response.json()["access_token"]
4252

43-
if not base_api_url or not api_token:
53+
if not base_api_url or not access_token:
4454
raise ValueError(
45-
"STAGING_SM2A_API_URL or STAGING_SM2A_ADMIN_USERNAME"
46-
+ " or STAGING_SM2A_ADMIN_PASSWORD is not"
55+
"STAGING_SM2A_API_URL or KEYCLOAK_STAGING_SM2A_FAB_CLIENT_SECRET is not"
4756
+ " set in the environment variables."
4857
)
4958

5059
headers = {
5160
"Content-Type": "application/json",
52-
"Authorization": "Basic " + api_token,
61+
"Authorization": "Bearer " + access_token,
5362
}
5463

5564
body = {
@@ -76,20 +85,30 @@ def publish_to_staging(payload):
7685
def promote_to_production(payload):
7786
base_api_url = os.getenv("SM2A_API_URL")
7887
promotion_dag = os.getenv("PROMOTION_DAG_NAME", "veda_promotion_pipeline")
79-
username = os.getenv("SM2A_ADMIN_USERNAME")
80-
password = os.getenv("SM2A_ADMIN_PASSWORD")
8188

82-
api_token = b64encode(f"{username}:{password}".encode()).decode()
89+
token_url = f"https://{os.getenv('KEYCLOAK_PROD_URL')}/realms/veda/protocol/openid-connect/token"
90+
client_id = "airflow-webserver-fab"
91+
client_secret = os.getenv("KEYCLOAK_PROD_SM2A_FAB_CLIENT_SECRET")
92+
93+
response = requests.post(
94+
token_url,
95+
data={
96+
"client_id": client_id,
97+
"client_secret": client_secret,
98+
"grant_type": "client_credentials",
99+
},
100+
)
101+
access_token = response.json()["access_token"]
83102

84-
if not base_api_url or not api_token:
103+
if not base_api_url or not access_token:
85104
raise ValueError(
86-
"SM2A_API_URL or SM2A_ADMIN_USERNAME or SM2A_ADMIN_PASSWORD is not"
105+
"SM2A_API_URL or KEYCLOAK_PRODUCTION_SM2A_FAB_CLIENT_SECRET is not"
87106
+ " set in the environment variables."
88107
)
89108

90109
headers = {
91110
"Content-Type": "application/json",
92-
"Authorization": "Basic " + api_token,
111+
"Authorization": "Bearer " + access_token,
93112
}
94113

95114
payload["conf"]["transfer"] = True

scripts/requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
pyyaml
1+
pyyaml
2+
requests

0 commit comments

Comments
 (0)