def create(self, name, text_targets, template, credentials=None, policy_id=None, policy_name=None): """Get scan by ID. :param name: The name of the Scan to be created. :param text_targets: A string of comma separated targets or a list of targets. :param template: The name or title of the template, or an instance of Template. :return: ScanRef referenced by id if exists. """ if isinstance(text_targets, list): text_targets = ','.join(text_targets) t = template if not isinstance(t, Template): t = self.template(name=template) if not t: t = self.template(title=template) if not t: raise TenableIOException( u'Template with name or title as "%s" not found.' % template) if not policy_id and policy_name: p = self.policy(policy_name) if not p: raise TenableIOException( u'Policy with name as "%s" not found.' % policy_name) policy_id = p.id if credentials and not isinstance(credentials, PolicyCredentials): raise TenableIOException( u'Credentials is not an instance of PolicyCredentials.') if policy_id and credentials: # create a new policy with the credentials copy_custom_policy_id = self._client.policies_api.copy(policy_id) payload = {"credentials": credentials.as_payload()} self._client.put('policies/%(policy_id)s', payload, path_params={'policy_id': copy_custom_policy_id}) policy_id = copy_custom_policy_id scan_id = self._client.scans_api.create( ScanCreateRequest( t.uuid, ScanSettings( name, text_targets, policy_id=policy_id, ), credentials)) return ScanRef(self._client, scan_id)
def create(self, name, text_targets, template): """Get scan by ID. :param name: The name of the Scan to be created. :param text_targets: A string of comma separated targets or a list of targets. :param template: The name or title of the template, or an instance of Template. :return: ScanRef referenced by id if exists. """ if isinstance(text_targets, list): text_targets = ','.join(text_targets) t = template if not isinstance(t, Template): t = self.template(name=template) if not t: t = self.template(title=template) if not t: raise TenableIOException( u'Template with name or title as "%s" not found.' % template) scan_id = self._client.scans_api.create( ScanCreateRequest(t.uuid, ScanSettings( name, text_targets, ))) return ScanRef(self._client, scan_id)
def upload(self, path): """Uploads a file. Usually used along with another API service that requires a file. :param path: Path of the file. :return: A string identifier for the uploaded file. """ if not os.path.isfile(path): raise TenableIOException(u'File does not exist at path.') with open(path, 'rb') as upload_file: uploaded_file_name = self._client.file_api.upload(upload_file) return uploaded_file_name
def wrapper(self, list_): if isinstance(list_, list): model_list = [] for item in list_: if isinstance(item, class_): model_list.append(item) elif isinstance(item, dict): model_list.append(class_.from_dict(item)) else: raise TenableIOException(u'Invalid element type.') f(self, model_list) else: f(self, [])
def import_scan(self, path): """Uploads and then imports scan report. :param path: Path of the scan report. :return: ScanRef referenced by id if exists. """ if not os.path.isfile(path): raise TenableIOException(u'File does not exist at path.') with open(path, 'rb') as upload_file: upload_file_name = self._client.file_api.upload(upload_file) imported_scan_id = self._client.scans_api.import_scan(ScanImportRequest(upload_file_name)) return self.id(imported_scan_id)
def parse(path, tag=REPORT_HOST): """Parse Nessus XML export from Workbench API into dicts. :param path: The file path. :param tag: The XML tag to iterate on. It should be WorkbenchParser.REPORT_HOST or WorkbenchParser.REPORT_ITEM. """ assert tag in [ WorkbenchParser.REPORT_HOST, WorkbenchParser.REPORT_ITEM ], u'Valid tag for parsing.' report_host = None host_properties = None report_items = [] if tag == WorkbenchParser.REPORT_HOST else None try: for event, elem in ET.iterparse(path, events=('start', 'end')): if event == 'start': if elem.tag == 'ReportHost': report_host = WorkbenchParser._from_report_host(elem) if event == 'end': if elem.tag == WorkbenchParser.REPORT_HOST: elem.clear() if tag == elem.tag: yield { 'report_host': report_host, 'host_properties': host_properties, 'report_items': report_items, } report_items = [] if elem.tag == WorkbenchParser.HOST_PROPERTIES: host_properties = WorkbenchParser._from_host_properties( elem) elem.clear() if elem.tag == WorkbenchParser.REPORT_ITEM: report_item = WorkbenchParser._from_report_item(elem) elem.clear() if tag == elem.tag: yield report_item elif tag == WorkbenchParser.REPORT_HOST: report_items.append(report_item) except ET.ParseError as e: raise TenableIOException(u'Failed to parse Nessus XML: ' + e.message)
def import_policy(self, path): """Upload and import the policy file. :param path: Path of the policy file. :return: PolicyRef referenced by id if exists. """ if not os.path.isfile(path): raise TenableIOException(u'File does not exist') with open(path, 'rb') as upload_file: uploaded_file_name = self._client.file_api.upload(upload_file) policy_id = self._client.policies_api.import_policy( PolicyImportRequest(uploaded_file_name)) return PolicyRef(self._client, policy_id)
def create(self, name, template): """Create a policy. :param name: The name of the policy. :param template: The name or title of the template, or an instance of Template. :return: PolicyRef referenced by id if exists. """ t = template if not isinstance(t, Template): t = self.template(name=template) if not t: t = self.template(title=template) if not t: raise TenableIOException( u'Template with name or title as "%s" not found.' % template) policy_id = self._client.policies_api.create( PolicyCreateRequest(t.uuid, PolicySettings(name=name))) return PolicyRef(self._client, policy_id)
def test_default_error_code_not_none(self): exception = TenableIOException() assert exception.code is not None, u'There should always be a code.' exception2 = TenableIOException(code=None) assert exception2.code is not None, u'Error code is never None even if None is passed as code in constructor.'
def test_default_error_code_is_generic(self): exception = TenableIOException() assert exception.code is TenableIOErrorCode.GENERIC, u'Default error code should be generic.'