from prometheus_client import Histogram import requests from tenacity import retry, stop_after_attempt from config import config from consts.consts import WHITELIST_ORG_ID, WHITELIST_PUBLIC_KEYS from utils.logging import logging EDA_AIPOWER_HISTOGRAM = Histogram("eda_aipower_histogram", "eda aipower request duration", ["resource_uri", "api_key", "status"]) def get_db_credentials(cluster_id: int, public_key: str): url = f"{config.ACCOUNT_ADDR}/inner/account-central/api/v1/apikey-jwt" params = { "cluster_id": cluster_id, "access_key": public_key, } resp = requests.get(url, params=params) resp_json = resp.json() username = resp_json["db_user"] passwd = resp_json["jwt"] logging.info("exchange auth0_token to username & password, resp %s", resp.text) return username, passwd def user_has_permission(user_token: str, resource_uri: str) -> bool: url = f"{config.AIPOWER_ADDR}/inner/aipower/api/v1/user_permission_check" payload = { "user_token": user_token, "resource_uri": resource_uri, } resp = requests.post(url, json=payload) logging.info("check permission of %s, resp %s", payload, resp.text) resp.raise_for_status() resp_json = resp.json() return resp_json["pass"] @retry(stop=stop_after_attempt(3)) def apikey_has_permission(api_key: str, resource_uri: str) -> bool: url = f"{config.AIPOWER_ADDR}/inner/aipower/api/v1/api_key_permission_check" payload = { "api_key": api_key, "resource_uri": resource_uri, } resp = requests.post(url, json=payload) EDA_AIPOWER_HISTOGRAM.labels(resource_uri=resource_uri, api_key=api_key, status=resp.status_code).observe(resp.elapsed.total_seconds()) logging.info("check permission of %s, resp %s", payload, resp.text) resp.raise_for_status() resp_json = resp.json() return resp_json["pass"] def get_cluster_info(cluster_id: int): url = f"{config.AIPOWER_ADDR}/inner/aipower/api/v1/cluster_info" params = { "cluster_id": int(cluster_id), } resp = requests.get(url, params=params) logging.info("get cluster info of %s, resp %s", cluster_id, resp.text) resp.raise_for_status() return resp.json() def get_db_host_and_port(cluster_id: int): resp_json = get_cluster_info(cluster_id) return resp_json["host"], resp_json["port"] def get_db_url(cluster_id, public_key): username, password = get_db_credentials(cluster_id, public_key) host, port = get_db_host_and_port(cluster_id) database_uri = "mysql+pymysql://{}:{}@{}:{}/{}?ssl_ca%2Fetc%2Fssl%2Fcerts%2Fca-certificates.crt&ssl_verify_identity=true".format( # noqa username, password, host, port, "test", ) return database_uri def get_org_ids_by_pubkey(access_key: str): if access_key in WHITELIST_PUBLIC_KEYS: return [WHITELIST_ORG_ID] url = f"{config.AIPOWER_ADDR}/inner/aipower/api/v1/api_key_info" params = { "access_key": access_key, } resp = requests.get(url, params=params) logging.info("get api key info of %s, resp %s", access_key, resp.text) resp.raise_for_status() resp_json = resp.json() org_ids = resp_json["org_ids"] return org_ids or [] def is_apikey_belongs_to_org_id(access_key: str, org_id: int): if access_key in WHITELIST_PUBLIC_KEYS: return True if config.DISABLE_PERMISSION: return True org_ids = get_org_ids_by_pubkey(access_key) return int(org_id) in org_ids def get_org_id_of_api_key(access_key: str): if config.DISABLE_PERMISSION: return 1001010086 org_ids = get_org_ids_by_pubkey(access_key) return org_ids[0] if len(org_ids) else None