'''Policies========Methods described in this section relate to the files API.These methods can be accessed at ``Nessus.policies``... rst-class:: hide-signature.. autoclass:: PoliciesAPI :members:'''fromioimportBytesIOfromtypingimportOptional,Dict,Listfromtenable.base.endpointimportAPIEndpoint
[docs]defcopy(self,policy_id:int)->Dict:''' Duplicates an existing scan policy. Args: policy_id (int): The id of the policy to clone. Returns: Dict: The cloned policy object. Example: >>> nessus.policies.copy(1) '''returnself._post(f'{policy_id}/copy')
[docs]defcreate(self,uuid:str,**kwargs)->Dict:''' Creates a new scan policy using the provided settings. Args: uuid (str): The UUID for the editor template to use. **kwargs (dict): Additional settings to use to create the policy. Returns: Dict: Response object with identifying information on the new policy Example: >>> tmpl = '731a8e52-3ea6-a291-ec0a-d2ff0619c19d7bd788d6be818b65' >>> nessus.policies.create(tmpl_uuid, settings={ ... 'name': 'Sample Policy' ... }) '''kwargs['uuid']=uuidreturnself._post(json=kwargs)
[docs]defdelete(self,policy_id:int)->None:''' Deletes the specified scan policy. Args: policy_id (int): The id of the policy to delete. Example: >>> nessus.policies.delete(1) '''returnself._delete(f'{policy_id}')
[docs]defdelete_many(self,policy_ids:List[int])->List[int]:''' Deletes the specified scan policies. Args: policy_ids (list[int]): The list of policy ids to delete. Example: >>> nessus.policies.delete_many([1, 2, 3]) '''returnself._delete(json={'ids':policy_ids})['deleted']
[docs]defdetails(self,policy_id:int)->Dict:''' Returns the details of the selected policy. Args: policy_id (int): The id of the policy to retrieve. Returns: Dict: The policy object. Example: >>> nessus.policies.details(1) '''returnself._get(f'{policy_id}')
[docs]defedit(self,policy_id:int,**kwargs)->None:''' Updates an existing scan policy. Args: policy_id (int): The id of the policy to edit. **kwargs (dict): Attributes to be passed into the JSON body. Example: >>> policy = nessus.policies.details(1) >>> policy['settings']['name'] = 'Updated Policy' >>> nessus.policies.edit(1, **policy) '''returnself._put(f'{policy_id}',json=kwargs)
[docs]defimport_policy(self,fobj:BytesIO)->Dict:''' Imports the policy into the nessus scanner. Args: fobj (BytesIO): The file object containing the policy. Returns: Dict: The imported policy object. Example: >>> with open('policy.xml', 'rb') as policy: ... nessus.policies.import_policy(policy) '''filename=self._api.files.upload(fobj)returnself._post('import',json={'file':filename})
[docs]defexport_policy(self,policy_id:int,fobj:Optional[BytesIO]=None,**kwargs)->BytesIO:''' Export the specified policy and download it. Args: policy_id (int): The id of the policy to export. fobj (BytexIO, optional): The file object to write the exported file to. If none is specified then a BytesIO object is written to in memory. chunk_size (int, optional): The chunk sizing for the download itself. stream_hook (callable, optional): Overload the default downloading behavior with a custom stream hook. hook_kwargs (dict, optional): keyword arguments to pass to the stream_hook callable in addition to the default passed params. '''kwargs['fobj']=fobjtoken=self._get(f'{policy_id}/export/prepare')['token']returnself._api.tokens._fetch(token,**kwargs)# noqa PLW0212
[docs]deflist(self,)->List[Dict]:''' Lists the available policies. Returns: List[Dict]: List of policy objects. Example: >>> for policy in nessus.policies.list(): ... print(policy) '''returnself._get()['policies']