Beispiel #1
0
    def __init__(self, inventory_url):
        self.inventory_url = inventory_url
        configs = Configuration()
        configs.host = self.inventory_url + "/api/assisted-install/v1"
        configs.verify_ssl = False
        self.set_config_auth(configs)

        self.api = ApiClient(configuration=configs)
        self.client = api.InstallerApi(api_client=self.api)
def get_inventory_hosts(inventory_endpoint, cluster_id, token):
    configs = Configuration()
    configs.host = inventory_endpoint
    configs.api_key["X-Secret-Key"] = token
    apiClient = ApiClient(configuration=configs)
    client = api.InstallerApi(api_client=apiClient)
    hosts_list = client.list_hosts(cluster_id=cluster_id)
    return [
        InventoryHost(host) for host in hosts_list
        if host['status'] != 'disabled'
    ]
    def __init__(self, inventory_url, offline_token, pull_secret):
        self.inventory_url = inventory_url
        configs = Configuration()
        configs.host = self.inventory_url + "/api/assisted-install/v1"
        configs.verify_ssl = False
        self.set_config_auth(configs, offline_token)
        self._set_x_secret_key(configs, pull_secret)

        self.api = ApiClient(configuration=configs)
        self.client = api.InstallerApi(api_client=self.api)
        self.events = api.EventsApi(api_client=self.api)
    def __init__(self, inventory_url: str, offline_token: Optional[str],
                 pull_secret: str):
        self.inventory_url = inventory_url
        configs = Configuration()
        configs.host = self.get_host(configs)
        configs.verify_ssl = False
        self.set_config_auth(configs, offline_token)
        self._set_x_secret_key(configs, pull_secret)

        self.api = ApiClient(configuration=configs)
        self.client = api.InstallerApi(api_client=self.api)
        self.events = api.EventsApi(api_client=self.api)
        self.versions = api.VersionsApi(api_client=self.api)
        self.domains = api.ManagedDomainsApi(api_client=self.api)
        self.operators = api.OperatorsApi(api_client=self.api)
        def refresh_api_key(config: Configuration) -> None:
            # Get the properly padded key segment
            auth = config.api_key.get("Authorization", None)
            if auth is not None:
                segment = auth.split(".")[1]
                padding = len(segment) % 4
                segment = segment + padding * "="

                expires_on = json.loads(base64.b64decode(segment))["exp"]

                # if this key doesn't expire or if it has more than 10 minutes left, don't refresh
                remaining = expires_on - time.time()
                if expires_on == 0 or remaining > 600:
                    return

            # fetch new key if expired or not set yet
            params = {
                "client_id": "cloud-services",
                "grant_type": "refresh_token",
                "refresh_token": offline_token,
            }

            log.info("Refreshing API key")
            response = requests.post(os.environ.get("SSO_URL"), data=params)
            response.raise_for_status()

            config.api_key["Authorization"] = response.json()["access_token"]
    def _set_x_secret_key(cls, c: Configuration, pull_secret: str) -> None:
        if not pull_secret:
            log.info("pull secret not set, skipping agent authentication headers")
            return

        log.info("Setting X-Secret-Key")
        c.api_key["X-Secret-Key"] = json.loads(pull_secret)["auths"]["cloud.openshift.com"]["auth"]
    def __init__(self, inventory_url: str, offline_token: Optional[str], pull_secret: str):
        self.inventory_url = inventory_url
        configs = Configuration()
        configs.host = self.get_host(configs)
        configs.verify_ssl = False
        self.set_config_auth(configs, offline_token)
        self._set_x_secret_key(configs, pull_secret)

        self.api = ApiClient(configuration=configs)
        self.client = api.InstallerApi(api_client=self.api)
        self.events = api.EventsApi(api_client=self.api)
        self.versions = api.VersionsApi(api_client=self.api)
        self.domains = api.ManagedDomainsApi(api_client=self.api)
        self.operators = api.OperatorsApi(api_client=self.api)

        fmt = CaseFormatKeys(case_name="cluster_id", severity_key="severity", case_timestamp="event_time")
        self._events_junit_exporter = JsonJunitExporter(fmt)
    def set_config_auth(cls, c: Configuration,
                        offline_token: Optional[str]) -> None:
        if not offline_token:
            log.info("OFFLINE_TOKEN not set, skipping authentication headers")
            return

        @retry(exceptions=requests.HTTPError, tries=5, delay=5)
        def refresh_api_key(config: Configuration) -> None:
            # Get the properly padded key segment
            auth = config.api_key.get("Authorization", None)
            if auth is not None:
                segment = auth.split(".")[1]
                padding = len(segment) % 4
                segment = segment + padding * "="

                expires_on = json.loads(base64.b64decode(segment))["exp"]

                # if this key doesn't expire or if it has more than 10 minutes left, don't refresh
                remaining = expires_on - time.time()
                if expires_on == 0 or remaining > 600:
                    return

            # fetch new key if expired or not set yet
            params = {
                "client_id": "cloud-services",
                "grant_type": "refresh_token",
                "refresh_token": offline_token,
            }

            log.info("Refreshing API key")
            response = requests.post(os.environ.get("SSO_URL"), data=params)
            response.raise_for_status()

            config.api_key["Authorization"] = response.json()["access_token"]

        c.api_key_prefix["Authorization"] = "Bearer"
        c.refresh_api_key_hook = refresh_api_key
 def __init__(self, url, token=None, offlinetoken=None):
     self.url = url
     self.config = Configuration()
     self.config.host = self.url + "/api/assisted-install"
     self.config.verify_ssl = False
     proxies = urllib.request.getproxies()
     if proxies:
         proxy = proxies.get('https') or proxies.get('http')
         if 'http' not in proxy:
             proxy = "http://" + proxy
             warning(f"Detected proxy env var without scheme, updating proxy to {proxy}")
         self.config.proxy = proxy
     aihome = f"{os.environ['HOME']}/.aicli"
     if not os.path.exists(aihome):
         os.mkdir(aihome)
     if url in ['https://api.openshift.com', 'https://api.stage.openshift.com']:
         if offlinetoken is None:
             if os.path.exists(f'{aihome}/offlinetoken.txt'):
                 offlinetoken = open(f'{aihome}/offlinetoken.txt').read().strip()
             else:
                 error(f"offlinetoken needs to be set to gather token for {url}")
                 error("get it at https://cloud.redhat.com/openshift/token")
                 if os.path.exists('/i_am_a_container'):
                     error("use -e AI_OFFLINETOKEN=$AI_OFFLINETOKEN to expose it in container mode")
                 sys.exit(1)
         if not os.path.exists(f'{aihome}/offlinetoken.txt'):
             with open(f'{aihome}/offlinetoken.txt', 'w') as f:
                 f.write(offlinetoken)
         self.offlinetoken = offlinetoken
         self.token = token
         if os.path.exists(f'{aihome}/token.txt'):
             self.token = open(f'{aihome}/token.txt').read().strip()
         try:
             self.token = get_token(token=self.token, offlinetoken=self.offlinetoken)
         except Exception as e:
             error(f"Hit issue when trying to set token. Got {e}")
             if os.path.exists(f'{aihome}/offlinetoken.txt'):
                 error("Removing offlinetoken file")
                 os.remove(f'{aihome}/offlinetoken.txt')
             sys.exit(1)
         self.config.api_key['Authorization'] = self.token
         self.config.api_key_prefix['Authorization'] = 'Bearer'
     self.api = ApiClient(configuration=self.config)
     self.client = api.InstallerApi(api_client=self.api)
def get_inventory_hosts(inventory_endpoint,
                        cluster_id,
                        token,
                        skip_cert_verification=False,
                        ca_cert_path=None):
    configs = Configuration()
    configs.host = inventory_endpoint
    configs.api_key["X-Secret-Key"] = token
    configs.verify_ssl = not skip_cert_verification
    configs.ssl_ca_cert = ca_cert_path
    apiClient = ApiClient(configuration=configs)
    client = api.InstallerApi(api_client=apiClient)
    hosts_list = client.list_hosts(cluster_id=cluster_id)
    return [
        InventoryHost(host) for host in hosts_list
        if host['status'] != 'disabled'
    ]