55import sys
66import os
77import uuid
8- from base64 import b64encode
8+ import requests
99
1010
1111class MissingFieldError (Exception ):
@@ -35,21 +35,30 @@ def validate_discovery_item_config(item: Dict[str, Any]) -> Dict[str, Any]:
3535def 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):
7685def 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
0 commit comments