Esempio n. 1
0
    def _send(self, url, data):
        response_data = None

        for host in self.hosts:
            try:
                full_url = '{}{}{}'.format(host, self.api_path, url)
                if self.ssl:
                    s = requests.Session()
                    s.mount(host, HTTPSAdapter())
                    r = s.post(full_url,
                               data=json.dumps(data),
                               timeout=self.timeout,
                               verify=self.ssl.server_verify,
                               cert=self.ssl.get_client_certs())
                else:
                    r = requests.post(full_url,
                                      data=json.dumps(data),
                                      timeout=self.timeout)

                r.raise_for_status()
                response_data = r.json()
                break
            except requests.exceptions.Timeout:
                log.warning('EtcdDatabase: Timeout on host {}'.format(host))

        return response_data
Esempio n. 2
0
    def do_query(self, query: str, use_time: bool = True):
        """ Implements: https://prometheus.io/docs/prometheus/2.16/querying/api/#instant-queries"""
        url = URL_TPL.format(prometheus_ip='{}:{}'.format(
            self.host, str(self.port)),
                             path=QUERY_PATH,
                             name=query)

        if self.time and use_time:
            url += '&time={}'.format(self.time)

        try:
            if self.ssl:
                s = requests.Session()
                s.mount(self.ip, HTTPSAdapter())
                response = s.get(url,
                                 timeout=self.timeout,
                                 verify=self.ssl.server_verify,
                                 cert=self.ssl.get_client_certs())
            else:
                response = requests.get(url, timeout=self.timeout)
                response.raise_for_status()
        except requests.exceptions.RequestException as e:
            raise PrometheusDataProviderException(e)

        return response.json()['data']['result']
Esempio n. 3
0
def test_unsupported_rsa_1024():
    ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2)
    ssl_context.load_cert_chain('tests/ssl/rsa1024.crt',
                                'tests/ssl/rsa1024.key')
    server = Process(target=run_simple_https_server, args=(ssl_context, ))
    server.start()
    time.sleep(0.5)
    with pytest.raises(requests.exceptions.SSLError):
        s = requests.Session()
        try:
            s.mount('https://localhost:8080/', HTTPSAdapter())
            s.get('https://localhost:8080/', verify='tests/ssl/rootCA.crt')
            server.terminate()
        except Exception:
            server.terminate()
            raise
Esempio n. 4
0
def test_supported_tls_1_2():
    ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2)
    ssl_context.load_cert_chain('tests/ssl/goodkey.crt',
                                'tests/ssl/goodkey.key')
    server = Process(target=run_simple_https_server, args=(ssl_context, ))
    server.start()
    time.sleep(0.5)
    try:
        s = requests.Session()
        s.mount('https://localhost:8080/', HTTPSAdapter())
        r = s.get('https://localhost:8080/', verify='tests/ssl/rootCA.crt')
        assert r.text == 'Passed'
        server.terminate()
    except Exception:
        server.terminate()
        raise
Esempio n. 5
0
def test_good_certificate():
    # Disable due to https://github.com/urllib3/urllib3/issues/497
    requests.packages.urllib3.disable_warnings(
        requests.packages.urllib3.exceptions.SubjectAltNameWarning)

    ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2)
    ssl_context.load_cert_chain('tests/ssl/goodkey.crt',
                                'tests/ssl/goodkey.key')
    server = Process(target=run_simple_https_server, args=(ssl_context, ))
    server.start()
    time.sleep(0.5)
    try:
        s = requests.Session()
        s.mount('https://localhost:8080/', HTTPSAdapter())
        r = s.get('https://localhost:8080/', verify='tests/ssl/rootCA.crt')
        assert r.text == 'Passed'
        server.terminate()
    except Exception:
        server.terminate()
        raise
    def _request_kubelet(self):
        PODS_PATH = '/pods'
        full_url = urljoin(self.kubelet_endpoint, PODS_PATH)

        if self.ssl:
            s = requests.Session()
            s.mount(self.kubelet_endpoint, HTTPSAdapter())
            r = s.get(full_url,
                      json=dict(type='GET_STATE'),
                      timeout=self.timeout,
                      verify=self.ssl.server_verify,
                      cert=self.ssl.get_client_certs())
        else:
            r = requests.get(full_url,
                             json=dict(type='GET_STATE'),
                             timeout=self.timeout)

        r.raise_for_status()

        return r.json()
Esempio n. 7
0
    def get_tasks(self):
        """ only return running tasks """
        full_url = urllib.parse.urljoin(self.mesos_agent_endpoint,
                                        self.api_path)

        try:
            if self.ssl:
                s = requests.Session()
                s.mount(self.mesos_agent_endpoint, HTTPSAdapter())
                r = s.post(full_url,
                           json=dict(type=self.METHOD),
                           timeout=self.timeout,
                           verify=self.ssl.server_verify,
                           cert=self.ssl.get_client_certs())
            else:
                r = requests.post(full_url,
                                  json=dict(type=self.METHOD),
                                  timeout=self.timeout)
        except requests.exceptions.ConnectionError as e:
            raise TaskSynchronizationException('%s' % e) from e

        r.raise_for_status()
        state = r.json()

        tasks = []

        # Fast return path if there is no any launched tasks.
        if 'launched_tasks' not in state['get_state']['get_tasks']:
            return []

        for launched_task in state['get_state']['get_tasks']['launched_tasks']:
            if 'statuses' not in launched_task or not len(
                    launched_task['statuses']):
                continue

            statuses = launched_task['statuses']
            last_status = statuses[
                -1]  # Assume the last on is the latest state # TODO: confirm
            if last_status['state'] != MESOS_TASK_STATE_RUNNING:
                continue

            if 'executor_pid' not in last_status['container_status']:
                log.warning(
                    "'executor_pid' not found in container status for task %s on agent %s",
                    last_status['task_id']['value'],
                    last_status['agent_id']['value'])
                continue

            executor_pid = last_status['container_status']['executor_pid']
            task_name = launched_task['name']

            try:
                cgroup_path = find_cgroup(executor_pid)
            except MesosCgroupNotFoundException as e:
                log.warning(
                    'Cannot determine proper cgroup for task=%r! '
                    'Ignoring this task. Reason: %s', task_name, e)
                continue

            labels = {
                sanitize_label(label['key']): label['value']
                for label in launched_task['labels']['labels']
            }

            # Extract scalar resources.
            resources = dict()
            for resource in launched_task['resources']:
                if resource['type'] == 'SCALAR':
                    resources[resource['name']] = float(
                        resource['scalar']['value'])

            tasks.append(
                MesosTask(name=task_name,
                          executor_pid=executor_pid,
                          cgroup_path=cgroup_path,
                          subcgroups_paths=[],
                          container_id=last_status['container_status']
                          ['container_id']['value'],
                          task_id=last_status['task_id']['value'],
                          agent_id=last_status['agent_id']['value'],
                          executor_id=last_status['executor_id']['value'],
                          labels=labels,
                          resources=resources))

        return tasks