Esempio n. 1
0
    def hosts_facts(self):
        """Obtain the managed hosts detail raw facts."""
        systems_count = len(self.conn_result.systems.all())
        self.initialize_stats(systems_count)

        orgs = self.get_orgs()
        for org_id in orgs:
            jsonresult = {}
            page = 0
            per_page = 100
            while (page == 0 or int(jsonresult.get(PER_PAGE, 0)) == len(
                    jsonresult.get(RESULTS, []))):
                page += 1
                params = {PAGE: page, PER_PAGE: per_page, THIN: 1}
                response, url = utils.execute_request(self.scan_task,
                                                      url=HOSTS_V1_URL,
                                                      org_id=org_id,
                                                      query_params=params)
                # pylint: disable=no-member
                if response.status_code != requests.codes.ok:
                    raise SatelliteException('Invalid response code %s'
                                             ' for url: %s' %
                                             (response.status_code, url))
                jsonresult = response.json()
                for host in jsonresult.get(RESULTS, []):
                    self.host_details(org_id, host.get(ID), host.get(NAME))
Esempio n. 2
0
    def hosts(self):
        """Obtain the managed hosts."""
        hosts = []
        jsonresult = {}
        page = 0
        per_page = 100
        credential = utils.get_credential(self.connect_scan_task)
        while (page == 0 or int(jsonresult.get(PER_PAGE, 0)) ==
               len(jsonresult.get(RESULTS, []))):
            page += 1
            params = {PAGE: page, PER_PAGE: per_page, THIN: 1}
            response, url = utils.execute_request(self.connect_scan_task,
                                                  url=HOSTS_V2_URL,
                                                  query_params=params)
            # pylint: disable=no-member
            if response.status_code != requests.codes.ok:
                raise SatelliteException('Invalid response code %s'
                                         ' for url: %s' %
                                         (response.status_code, url))
            jsonresult = response.json()
            for result in jsonresult.get(RESULTS, []):
                host_name = result.get(NAME)
                host_id = result.get(ID)

                if host_name is not None and host_id is not None:
                    unique_name = '%s_%s' % (host_name, host_id)
                    hosts.append(unique_name)
                    self.record_conn_result(unique_name, credential)

        return hosts
Esempio n. 3
0
    def get_orgs(self):
        """Get the organization ids.

        :returns: List of organization ids
        """
        if self.orgs:
            return self.orgs

        orgs = []
        jsonresult = {}
        page = 0
        per_page = 100
        while (page == 0 or int(jsonresult.get(PER_PAGE, 0)) ==
               len(jsonresult.get(RESULTS, []))):
            page += 1
            params = {PAGE: page, PER_PAGE: per_page, THIN: 1}
            response, url = utils.execute_request(self.connect_scan_task,
                                                  ORGS_V1_URL, params)
            # pylint: disable=no-member
            if response.status_code != requests.codes.ok:
                raise SatelliteException('Invalid response code %s'
                                         ' for url: %s' %
                                         (response.status_code, url))
            jsonresult = response.json()
            for result in jsonresult.get(RESULTS, []):
                org_id = result.get(ID)
                if org_id is not None:
                    orgs.append(org_id)
        self.orgs = orgs
        return self.orgs
Esempio n. 4
0
    def hosts_facts(self):
        """Obtain the managed hosts detail raw facts."""
        systems_count = len(
            self.connect_scan_task.connection_result.systems.all())
        if self.inspect_scan_task is None:
            raise SatelliteException(
                'hosts_facts cannot be called for a connection scan')
        self.inspect_scan_task.update_stats(
            'INITIAL STATELLITE STATS', sys_count=systems_count)

        jsonresult = {}
        page = 0
        per_page = 100
        while (page == 0 or int(jsonresult.get(PER_PAGE, 0)) ==
               len(jsonresult.get(RESULTS, []))):
            page += 1
            params = {PAGE: page, PER_PAGE: per_page, THIN: 1}
            response, url = utils.execute_request(self.inspect_scan_task,
                                                  url=HOSTS_V2_URL,
                                                  query_params=params)
            # pylint: disable=no-member
            if response.status_code != requests.codes.ok:
                raise SatelliteException('Invalid response code %s'
                                         ' for url: %s' %
                                         (response.status_code, url))
            jsonresult = response.json()
            for host in jsonresult.get(RESULTS, []):
                self.host_details(host.get(ID), host.get(NAME))
Esempio n. 5
0
    def hosts_facts(self, manager_interrupt):
        """Obtain the managed hosts detail raw facts."""
        systems_count = len(
            self.connect_scan_task.connection_result.systems.all())
        if self.inspect_scan_task is None:
            raise SatelliteException(
                'hosts_facts cannot be called for a connection scan')
        self.inspect_scan_task.update_stats('INITIAL STATELLITE STATS',
                                            sys_count=systems_count)
        deduplicated_hosts = []

        # pylint: disable=too-many-nested-blocks
        with Pool(processes=self.max_concurrency) as pool:
            orgs = self.get_orgs()
            for org_id in orgs:
                jsonresult = {}
                page = 0
                per_page = 100
                while (page == 0 or int(jsonresult.get(PER_PAGE, 0)) == len(
                        jsonresult.get(RESULTS, []))):
                    page += 1
                    params = {PAGE: page, PER_PAGE: per_page, THIN: 1}
                    response, url = \
                        utils.execute_request(self.inspect_scan_task,
                                              url=HOSTS_V1_URL,
                                              org_id=org_id,
                                              query_params=params)
                    # pylint: disable=no-member
                    if response.status_code != requests.codes.ok:
                        raise SatelliteException('Invalid response code %s'
                                                 ' for url: %s' %
                                                 (response.status_code, url))
                    jsonresult = response.json()

                    hosts_before_dedup = jsonresult.get(RESULTS, [])
                    hosts_after_dedup = []
                    for host in hosts_before_dedup:
                        if host not in deduplicated_hosts:
                            hosts_after_dedup.append(host)
                            deduplicated_hosts.append(host)
                    hosts = hosts_after_dedup
                    chunks = [
                        hosts[i:i + self.max_concurrency]
                        for i in range(0, len(hosts), self.max_concurrency)
                    ]
                    for chunk in chunks:
                        if manager_interrupt.value == \
                                ScanJob.JOB_TERMINATE_CANCEL:
                            raise SatelliteCancelException()

                        if manager_interrupt.value == \
                                ScanJob.JOB_TERMINATE_PAUSE:
                            raise SatellitePauseException()

                        host_params = self.prepare_host(chunk)
                        results = pool.starmap(request_host_details,
                                               host_params)
                        process_results(self, results, 1)
        utils.validate_task_stats(self.inspect_scan_task)
Esempio n. 6
0
def host_subscriptions(scan_task, url, org_id, host_id):
    """Obtain the subscriptions for a given host id.

    :param scan_task: The scan task being executed
    :param url: The endpoint URL to get data from
    :param org_id: The organization identifier
    :param host_id: The identifier of the host being queried.
    :returns: dictionary of facts from subscriptions endpoint
    """
    response, url = utils.execute_request(scan_task,
                                          url=url,
                                          org_id=org_id,
                                          host_id=host_id)
    # pylint: disable=no-member
    if response.status_code == 400 or response.status_code == 404:
        content_type = response.headers.get(CONTENT_TYPE)
        if content_type and APP_JSON in content_type:
            message = 'Invalid status code %s for url: %s. Response: %s' %\
                (response.status_code, url, response.json())
            scan_task.log_message(message, log_level=logging.WARN)
        else:
            message = 'Invalid status code %s for url: %s. '\
                'Response not JSON' %\
                (response.status_code, url)
            scan_task.log_message(message, log_level=logging.WARN)
        subs_dict = {ENTITLEMENTS: []}
        return subs_dict
    elif response.status_code != requests.codes.ok:
        raise SatelliteException('Invalid response code %s'
                                 ' for url: %s' %
                                 (response.status_code, url))
    entitlements = response.json().get(RESULTS, [])
    subscriptons = []
    for entitlement in entitlements:
        sub = {
            DERIVED_ENTITLEMENT: False,
            NAME: entitlement.get(PRODUCT_NAME),
            ACCOUNT_NUMBER: entitlement.get(ACCOUNT_NUMBER),
            CONTRACT_NUMBER: entitlement.get(CONTRACT_NUMBER),
            START_DATE: entitlement.get(START_DATE),
            END_DATE: entitlement.get(END_DATE),
        }
        amount = entitlement.get(QUANTITY_CONSUMED)
        if amount is None:
            amount = entitlement.get(AMOUNT)
        sub[AMOUNT] = amount

        entitlement_type = entitlement.get(ENTITLEMENT_TYPE)
        if (entitlement_type and
                entitlement_type in ENTITLEMENT_DERIVED_LIST):
            sub[DERIVED_ENTITLEMENT] = True
        subscriptons.append(sub)

    subs_dict = {ENTITLEMENTS: subscriptons}
    return subs_dict
Esempio n. 7
0
 def test_execute_request(self):
     """Test the method to execute a request against a satellite server."""
     status_url = 'https://{sat_host}:{port}/api/status'
     with requests_mock.Mocker() as mocker:
         url = construct_url(status_url, '1.2.3.4')
         jsonresult = {'api_version': 2}
         mocker.get(url, status_code=200, json=jsonresult)
         response, formatted_url = execute_request(self.scan_task,
                                                   status_url)
         self.assertEqual(url, formatted_url)
         self.assertEqual(response.status_code, 200)
         self.assertEqual(response.json(), jsonresult)
Esempio n. 8
0
 def host_count(self):
     """Obtain the count of managed hosts."""
     params = {PAGE: 1, PER_PAGE: 10, THIN: 1}
     response, url = utils.execute_request(self.scan_task,
                                           url=HOSTS_V2_URL,
                                           query_params=params)
     # pylint: disable=no-member
     if response.status_code != requests.codes.ok:
         raise SatelliteException('Invalid response code %s for url: %s' %
                                  (response.status_code, url))
     systems_count = response.json().get('total', 0)
     self.initialize_stats(systems_count)
     return systems_count
Esempio n. 9
0
    def hosts_facts(self, manager_interrupt):
        """Obtain the managed hosts detail raw facts."""
        systems_count = len(
            self.connect_scan_task.connection_result.systems.all())
        if self.inspect_scan_task is None:
            raise SatelliteException(
                'hosts_facts cannot be called for a connection scan')
        self.inspect_scan_task.update_stats('INITIAL STATELLITE STATS',
                                            sys_count=systems_count)

        with Pool(processes=self.max_concurrency) as pool:
            jsonresult = {}
            page = 0
            per_page = 100
            while (page == 0 or int(jsonresult.get(PER_PAGE, 0)) == len(
                    jsonresult.get(RESULTS, []))):
                page += 1
                params = {PAGE: page, PER_PAGE: per_page, THIN: 1}
                response, url = utils.execute_request(self.inspect_scan_task,
                                                      url=HOSTS_V2_URL,
                                                      query_params=params)
                # pylint: disable=no-member
                if response.status_code != requests.codes.ok:
                    raise SatelliteException('Invalid response code %s'
                                             ' for url: %s' %
                                             (response.status_code, url))
                jsonresult = response.json()

                hosts = jsonresult.get(RESULTS, [])
                chunks = [
                    hosts[i:i + self.max_concurrency]
                    for i in range(0, len(hosts), self.max_concurrency)
                ]
                for chunk in chunks:
                    if manager_interrupt.value == ScanJob.JOB_TERMINATE_CANCEL:
                        raise SatelliteCancelException()

                    if manager_interrupt.value == ScanJob.JOB_TERMINATE_PAUSE:
                        raise SatellitePauseException()

                    host_params = [(host.get(ID), host.get(NAME))
                                   for host in chunk]
                    results = pool.starmap(self.host_details, host_params)
                    for result in results:
                        if result is not None:
                            self.record_inspect_result(result.get('name'),
                                                       result.get('details'),
                                                       result.get('status'))

        utils.validate_task_stats(self.inspect_scan_task)
Esempio n. 10
0
 def host_count(self):
     """Obtain the count of managed hosts."""
     systems_count = 0
     orgs = self.get_orgs()
     for org_id in orgs:
         params = {PAGE: 1, PER_PAGE: 100, THIN: 1}
         response, url = utils.execute_request(self.connect_scan_task,
                                               url=HOSTS_V1_URL,
                                               org_id=org_id,
                                               query_params=params)
         # pylint: disable=no-member
         if response.status_code != requests.codes.ok:
             raise SatelliteException('Invalid response code %s'
                                      ' for url: %s' %
                                      (response.status_code, url))
         systems_count += response.json().get('total', 0)
         self.connect_scan_task.update_stats(
             'INITIAL STATELLITE STATS', sys_count=systems_count)
         return systems_count
Esempio n. 11
0
def request_host_details(scan_task, logging_options,
                         host_id, host_name,
                         fields_url, subs_url, request_options):
    """Execute both http responses to gather satallite data.

    :param scan_task: The current scan task
    :param logging_options: The metadata for logging
    :param host_id: The id of the host we're inspecting
    :param host_name: The name of the host we're inspecting
    :param fields_url: The sat61 or sat62 fields url
    :param subs_url: The sat61 or sat62 subs url
    :param request_options: A dictionary containing host, port,
        ssl_cert_verify, user, and password
    :returns: A dictionary containing the unique name for the host,
        the response & url for host_fields request, and the
        response & url for the host_subs request.
    """
    unique_name = '%s_%s' % (host_name, host_id)
    host_fields_json = {}
    host_subscriptions_json = {}
    results = {}
    try:
        message = 'REQUESTING HOST DETAILS: %s' % unique_name
        scan_task.log_message(message, logging.INFO, logging_options)
        host_fields_response, host_fields_url = \
            utils.execute_request(scan_task,
                                  url=fields_url,
                                  org_id=None,
                                  host_id=host_id,
                                  query_params=QUERY_PARAMS_FIELDS,
                                  options=request_options)
        # pylint: disable=no-member
        if host_fields_response.status_code != requests.codes.ok:
            raise SatelliteException('Invalid response code %s'
                                     ' for url: %s' %
                                     (host_fields_response.status_code,
                                      host_fields_url))
        host_subscriptions_response, host_subscriptions_url = \
            utils.execute_request(scan_task,
                                  url=subs_url,
                                  org_id=None,
                                  host_id=host_id,
                                  options=request_options)
        # pylint: disable=no-member
        if host_subscriptions_response.status_code == 400 or \
                host_subscriptions_response.status_code == 404:
            content_type = \
                host_subscriptions_response.headers.get(CONTENT_TYPE)
            if content_type and APP_JSON in content_type:
                message = \
                    'Invalid status code %s for url: %s. Response: %s' % \
                    (host_subscriptions_response.status_code,
                     host_subscriptions_url,
                     host_subscriptions_response.json())
                scan_task.log_message(message, logging.WARN, logging_options)
            else:
                message = 'Invalid status code %s for url: %s. ' \
                          'Response not JSON' % \
                          (host_subscriptions_response.status_code,
                           host_subscriptions_url)
                scan_task.log_message(message, logging.WARN, logging_options)
        elif host_subscriptions_response.status_code != requests.codes.ok:
            raise SatelliteException(
                'Invalid response code %s for url: %s' % (
                    host_subscriptions_response.status_code,
                    host_subscriptions_url))
        system_inspection_result = SystemInspectionResult.SUCCESS
        host_fields_json = host_fields_response.json()
        host_subscriptions_json = host_subscriptions_response.json()
    except SatelliteException as sat_error:
        error_message = 'Satellite 6 unknown error encountered: %s\n' \
            % sat_error
        logger.error(error_message)
        system_inspection_result = SystemInspectionResult.FAILED
    except Timeout as timeout_error:
        error_message = 'Satellite 6 timeout error encountered: %s\n' \
            % timeout_error
        logger.error(error_message)
        system_inspection_result = SystemInspectionResult.FAILED
    results['unique_name'] = unique_name
    results['system_inspection_result'] = system_inspection_result
    results['host_fields_response'] = host_fields_json
    results['host_subscriptions_response'] = host_subscriptions_json
    return results
Esempio n. 12
0
def host_fields(scan_task, api_version, url, org_id, host_id):
    """Obtain the fields for a given host id.

    :param scan_task: The scan task being executed
    :param api_version: The version of the Satellite api
    :param url: The endpoint URL to get data from
    :param org_id: The organization identifier
    :param host_id: The identifier of the host being queried.
    :returns: dictionary of facts from fields endpoint
    """
    response, url = utils.execute_request(scan_task,
                                          url=url,
                                          org_id=org_id,
                                          host_id=host_id,
                                          query_params=QUERY_PARAMS_FIELDS)
    # pylint: disable=no-member
    if response.status_code != requests.codes.ok:
        raise SatelliteException('Invalid response code %s'
                                 ' for url: %s' % (response.status_code, url))
    fields = response.json()
    host_info = {}
    sub_virt_host = None
    sub_virt_guest = None
    cf_errata_counts = None
    sub_facet_attributes = fields.get(SUBSCRIPTION_FACET)
    content_facet_attributes = fields.get(CONTENT_FACET)
    facts = fields.get(FACTS, {})
    virtual_host = fields.get(VIRTUAL_HOST, {})
    virtual_guests = fields.get(VIRTUAL_GUESTS)
    errata_counts = fields.get(ERRATA_COUNTS, {})

    if sub_facet_attributes:
        sub_virt_host = sub_facet_attributes.get(VIRTUAL_HOST)
        sub_virt_guest = sub_facet_attributes.get(VIRTUAL_GUESTS)
    if content_facet_attributes:
        cf_errata_counts = content_facet_attributes.get(ERRATA_COUNTS)

    host_info.update(utils.data_map(FIELDS_MAPPING, fields))
    host_info.update(utils.data_map(SUBS_FACET_MAPPING, sub_facet_attributes))
    host_info.update(utils.data_map(VIRTUAL_HOST_MAPPING, sub_virt_host))
    host_info.update(
        utils.data_map(CONTENT_FACET_MAPPING, content_facet_attributes))
    host_info.update(utils.data_map(ERRATA_MAPPING, cf_errata_counts))
    if api_version == 1:
        host_info.update(utils.data_map(FACTS_MAPPING, facts))
    else:
        host_info.update(utils.data_map(FACTS_V2_MAPPING, facts))
    host_info.update(utils.data_map(VIRTUAL_HOST_MAPPING, virtual_host))
    host_info.update(utils.data_map(ERRATA_MAPPING, errata_counts))

    if sub_virt_guest:
        host_info[VIRTUAL_GUESTS] = [x[NAME] for x in sub_virt_guest]
        host_info[NUM_VIRTUAL_GUESTS] = len(sub_virt_guest)

    if virtual_guests:
        host_info[VIRTUAL_GUESTS] = [x[NAME] for x in virtual_guests]
        host_info[NUM_VIRTUAL_GUESTS] = len(virtual_guests)

    if host_info.get(VIRTUAL_GUESTS):
        host_info[VIRTUAL] = HYPERVISOR

    host_info[LOCATION] = fields.get(LOCATION_NAME)

    ipv4s = []
    macs = []
    for key in facts:
        net_interface = (key.startswith(NET_INTER_PERIOD)
                         or key.startswith(NET_INTER_COLON))
        net_interface_lo = (key.startswith(NET_INTER_LO_PERIOD)
                            or key.startswith(NET_INTER_LO_COLON))
        if net_interface and not net_interface_lo:
            ipv4_addr = (key.endswith(IPV4_PERIOD) or key.endswith(IPV4_COLON))
            mac_addr = (key.endswith(MAC_PERIOD) or key.endswith(MAC_COLON))
            if ipv4_addr and facts[key]:
                ipv4s.append(facts[key])
            if mac_addr and facts[key]:
                macs.append(facts[key])
    host_info[IP_ADDRESSES] = ipv4s
    host_info[MAC_ADDRESSES] = macs

    os_release = host_info.get(OS_RELEASE)
    if os_release:
        os_name = os_release.split(' ')[0]
        if os_release.lower().startswith('red hat enterprise linux'):
            os_name = 'Red Hat Enterprise Linux'
        elif os_release.lower().startswith('red hat'):
            os_name = 'Red Hat'
        host_info[OS_NAME] = os_name
        host_info[OS_VERSION] = os_release.rsplit(' ').pop()

    host_info.pop(VIRTUAL_GUESTS, None)
    return host_info