def handle_xml(self, xml: Element) -> Iterator[bytes]: """ Handles <get_vts> command. Writes the vt collection on the stream. The <get_vts> element accept two optional arguments. vt_id argument receives a single vt id. filter argument receives a filter selecting a sub set of vts. If both arguments are given, the vts which match with the filter are return. @return: Response string for <get_vts> command on fail. """ xml_helper = XmlStringHelper() vt_id = xml.get('vt_id') vt_filter = xml.get('filter') _details = xml.get('details') vt_details = False if _details == '0' else True if vt_id and vt_id not in self._daemon.vts: text = "Failed to find vulnerability test '{0}'".format(vt_id) raise OspdCommandError(text, 'get_vts', 404) filtered_vts = None if vt_filter: filtered_vts = self._daemon.vts_filter.get_filtered_vts_list( self._daemon.vts, vt_filter) vts_selection = self._daemon.get_vts_selection_list( vt_id, filtered_vts) # List of xml pieces with the generator to be iterated yield xml_helper.create_response('get_vts') begin_vts_tag = xml_helper.create_element('vts') val = len(self._daemon.vts) begin_vts_tag = xml_helper.add_attr(begin_vts_tag, "total", val) if filtered_vts: val = len(filtered_vts) begin_vts_tag = xml_helper.add_attr(begin_vts_tag, "sent", val) if self._daemon.vts.sha256_hash is not None: begin_vts_tag = xml_helper.add_attr(begin_vts_tag, "sha256_hash", self._daemon.vts.sha256_hash) yield begin_vts_tag for vt in self._daemon.get_vt_iterator(vts_selection, vt_details): yield xml_helper.add_element(self._daemon.get_vt_xml(vt)) yield xml_helper.create_element('vts', end=True) yield xml_helper.create_response('get_vts', end=True)
def handle_xml(self, xml: Element) -> Iterator[bytes]: """Handles <get_vts> command. Writes the vt collection on the stream. The <get_vts> element accept two optional arguments. vt_id argument receives a single vt id. filter argument receives a filter selecting a sub set of vts. If both arguments are given, the vts which match with the filter are return. @return: Response string for <get_vts> command on fail. """ self._daemon.vts.is_cache_available = False xml_helper = XmlStringHelper() vt_id = xml.get('vt_id') vt_filter = xml.get('filter') _details = xml.get('details') version_only = xml.get('version_only') vt_details = False if _details == '0' else True if self._daemon.vts and vt_id and vt_id not in self._daemon.vts: self._daemon.vts.is_cache_available = True text = f"Failed to find vulnerability test '{vt_id}'" raise OspdCommandError(text, 'get_vts', 404) filtered_vts = None if vt_filter and not version_only: try: filtered_vts = self._daemon.vts_filter.get_filtered_vts_list( self._daemon.vts, vt_filter) except OspdCommandError as filter_error: self._daemon.vts.is_cache_available = True raise filter_error if not version_only: vts_selection = self._daemon.get_vts_selection_list( vt_id, filtered_vts) # List of xml pieces with the generator to be iterated yield xml_helper.create_response('get_vts') begin_vts_tag = xml_helper.create_element('vts') # Add Feed information as attributes begin_vts_tag = xml_helper.add_attr(begin_vts_tag, "vts_version", self._daemon.get_vts_version()) begin_vts_tag = xml_helper.add_attr(begin_vts_tag, "feed_vendor", self._daemon.get_feed_vendor()) begin_vts_tag = xml_helper.add_attr(begin_vts_tag, "feed_home", self._daemon.get_feed_home()) begin_vts_tag = xml_helper.add_attr(begin_vts_tag, "feed_name", self._daemon.get_feed_name()) val = len(self._daemon.vts) begin_vts_tag = xml_helper.add_attr(begin_vts_tag, "total", val) if filtered_vts and not version_only: val = len(filtered_vts) begin_vts_tag = xml_helper.add_attr(begin_vts_tag, "sent", val) if self._daemon.vts.sha256_hash is not None: begin_vts_tag = xml_helper.add_attr(begin_vts_tag, "sha256_hash", self._daemon.vts.sha256_hash) yield begin_vts_tag if not version_only: for vt in self._daemon.get_vt_iterator(vts_selection, vt_details): yield xml_helper.add_element(self._daemon.get_vt_xml(vt)) yield xml_helper.create_element('vts', end=True) yield xml_helper.create_response('get_vts', end=True) self._daemon.vts.is_cache_available = True