|
| 1 | +# |
| 2 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 3 | +# or more contributor license agreements. See the NOTICE file |
| 4 | +# distributed with this work for additional information |
| 5 | +# regarding copyright ownership. The ASF licenses this file |
| 6 | +# to you under the Apache License, Version 2.0 (the |
| 7 | +# "License"); you may not use this file except in compliance |
| 8 | +# with the License. You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, |
| 13 | +# software distributed under the License is distributed on an |
| 14 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | +# KIND, either express or implied. See the License for the |
| 16 | +# specific language governing permissions and limitations |
| 17 | +# under the License. |
| 18 | +# |
| 19 | +import json |
| 20 | +import os |
| 21 | +from argparse import Namespace |
| 22 | +from functools import cached_property |
| 23 | +from typing import Optional, Dict |
| 24 | + |
| 25 | +from cli.constants import ( |
| 26 | + CONFIG_FILE, |
| 27 | + CLIENT_PROFILE_ENV, |
| 28 | + CLIENT_ID_ENV, |
| 29 | + CLIENT_SECRET_ENV, |
| 30 | + REALM_ENV, |
| 31 | + HEADER_ENV, |
| 32 | + Arguments, |
| 33 | + DEFAULT_HOSTNAME, |
| 34 | + DEFAULT_PORT |
| 35 | +) |
| 36 | +from cli.options.option_tree import Argument |
| 37 | +from polaris.management import ApiClient, Configuration |
| 38 | + |
| 39 | + |
| 40 | +def _load_profiles() -> Dict[str, str]: |
| 41 | + if not os.path.exists(CONFIG_FILE): |
| 42 | + return {} |
| 43 | + with open(CONFIG_FILE, "r") as f: |
| 44 | + return json.load(f) |
| 45 | + |
| 46 | + |
| 47 | +class BuilderConfig: |
| 48 | + def __init__(self, options: Namespace): |
| 49 | + self.options = options |
| 50 | + self.proxy = options.proxy |
| 51 | + self.access_token = options.access_token |
| 52 | + |
| 53 | + @cached_property |
| 54 | + def profile(self) -> Dict[str, str]: |
| 55 | + profile = {} |
| 56 | + client_profile = self.options.profile or os.getenv(CLIENT_PROFILE_ENV) |
| 57 | + if client_profile: |
| 58 | + profiles = _load_profiles() |
| 59 | + profile = profiles.get(client_profile) |
| 60 | + if not profile: |
| 61 | + raise Exception(f"Polaris profile {client_profile} not found") |
| 62 | + return profile |
| 63 | + |
| 64 | + @cached_property |
| 65 | + def base_url(self) -> str: |
| 66 | + if self.options.base_url: |
| 67 | + if self.options.host is not None or self.options.port is not None: |
| 68 | + raise Exception( |
| 69 | + f"Please provide either {Argument.to_flag_name(Arguments.BASE_URL)} or" |
| 70 | + f" {Argument.to_flag_name(Arguments.HOST)} &" |
| 71 | + f" {Argument.to_flag_name(Arguments.PORT)}, but not both" |
| 72 | + ) |
| 73 | + return self.options.base_url |
| 74 | + |
| 75 | + host = self.options.host or self.profile.get(Arguments.HOST) or DEFAULT_HOSTNAME |
| 76 | + port = self.options.port or self.profile.get(Arguments.PORT) or DEFAULT_PORT |
| 77 | + return f"http://{host}:{port}" |
| 78 | + |
| 79 | + @cached_property |
| 80 | + def management_url(self) -> str: |
| 81 | + return f"{self.base_url}/api/management/v1" |
| 82 | + |
| 83 | + @cached_property |
| 84 | + def catalog_url(self) -> str: |
| 85 | + return f"{self.base_url}/api/catalog/v1" |
| 86 | + |
| 87 | + @cached_property |
| 88 | + def client_id(self) -> Optional[str]: |
| 89 | + return ( |
| 90 | + self.options.client_id |
| 91 | + or os.getenv(CLIENT_ID_ENV) |
| 92 | + or self.profile.get(Arguments.CLIENT_ID) |
| 93 | + ) |
| 94 | + |
| 95 | + @cached_property |
| 96 | + def client_secret(self) -> Optional[str]: |
| 97 | + return ( |
| 98 | + self.options.client_secret |
| 99 | + or os.getenv(CLIENT_SECRET_ENV) |
| 100 | + or self.profile.get(Arguments.CLIENT_SECRET) |
| 101 | + ) |
| 102 | + |
| 103 | + @cached_property |
| 104 | + def realm(self) -> Optional[str]: |
| 105 | + realms = ( |
| 106 | + self.options.realm |
| 107 | + or os.getenv(REALM_ENV) |
| 108 | + or self.profile.get(Arguments.REALM) |
| 109 | + ) |
| 110 | + return realms |
| 111 | + |
| 112 | + @cached_property |
| 113 | + def header(self) -> Optional[str]: |
| 114 | + return ( |
| 115 | + self.options.header |
| 116 | + or os.getenv(HEADER_ENV) |
| 117 | + or self.profile.get(Arguments.HEADER) |
| 118 | + ) |
| 119 | + |
| 120 | + |
| 121 | +class ApiClientBuilder: |
| 122 | + def __init__(self, options: Namespace, *, direct_authentication: bool = False): |
| 123 | + self.conf = BuilderConfig(options) |
| 124 | + self.direct_auth_enabled = direct_authentication |
| 125 | + |
| 126 | + def _get_token(self): |
| 127 | + header_params = {"Content-Type": "application/x-www-form-urlencoded"} |
| 128 | + if self.conf.header and self.conf.realm: |
| 129 | + header_params[self.conf.header] = self.conf.realm |
| 130 | + |
| 131 | + conf = Configuration(host=self.conf.management_url) |
| 132 | + conf.proxy = self.conf.proxy |
| 133 | + api_client = ApiClient(conf) |
| 134 | + response = api_client.call_api( |
| 135 | + "POST", |
| 136 | + f"{self.conf.catalog_url}/oauth/tokens", |
| 137 | + header_params=header_params, |
| 138 | + post_params={ |
| 139 | + "grant_type": "client_credentials", |
| 140 | + "client_id": self.conf.client_id, |
| 141 | + "client_secret": self.conf.client_secret, |
| 142 | + "scope": "PRINCIPAL_ROLE:ALL", |
| 143 | + }, |
| 144 | + ).response.data |
| 145 | + if "access_token" not in json.loads(response): |
| 146 | + raise Exception("Failed to get access token") |
| 147 | + return json.loads(response)["access_token"] |
| 148 | + |
| 149 | + def _build(self) -> ApiClient: |
| 150 | + has_access_token = self.conf.access_token is not None |
| 151 | + has_client_secret = self.conf.client_id is not None and self.conf.client_secret is not None |
| 152 | + if has_access_token and (self.conf.client_id or self.conf.client_secret): |
| 153 | + raise Exception( |
| 154 | + f"Please provide credentials via either {Argument.to_flag_name(Arguments.CLIENT_ID)} &" |
| 155 | + f" {Argument.to_flag_name(Arguments.CLIENT_SECRET)} or" |
| 156 | + f" {Argument.to_flag_name(Arguments.ACCESS_TOKEN)}, but not both" |
| 157 | + ) |
| 158 | + if not has_access_token and not has_client_secret: |
| 159 | + raise Exception( |
| 160 | + f"Please provide credentials via either {Argument.to_flag_name(Arguments.CLIENT_ID)} &" |
| 161 | + f" {Argument.to_flag_name(Arguments.CLIENT_SECRET)} or" |
| 162 | + f" {Argument.to_flag_name(Arguments.ACCESS_TOKEN)}." |
| 163 | + f" Alternatively, you may set the environment variables {CLIENT_ID_ENV} &" |
| 164 | + f" {CLIENT_SECRET_ENV}." |
| 165 | + ) |
| 166 | + |
| 167 | + config = Configuration(host=self.conf.management_url) |
| 168 | + config.proxy = self.conf.proxy |
| 169 | + if has_access_token: |
| 170 | + config.access_token = self.conf.access_token |
| 171 | + elif has_client_secret: |
| 172 | + config.username = self.conf.client_id |
| 173 | + config.password = self.conf.client_secret |
| 174 | + |
| 175 | + if not has_access_token and not self.direct_auth_enabled: |
| 176 | + config.username = None |
| 177 | + config.password = None |
| 178 | + config.access_token = self._get_token() |
| 179 | + |
| 180 | + client_params = {} |
| 181 | + if self.conf.realm and self.conf.header: |
| 182 | + client_params["header_name"] = self.conf.header |
| 183 | + client_params["header_value"] = self.conf.realm |
| 184 | + |
| 185 | + return ApiClient(config, **client_params) |
| 186 | + |
| 187 | + def get_api_client(self) -> ApiClient: |
| 188 | + return self._build() |
0 commit comments