This repository was archived by the owner on May 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
Improvements for AssetDetails, Tags & Unique Identifiers + Adhoc Reports #43
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,8 @@ | |
| import urllib.request | ||
| import urllib.parse | ||
| import urllib.error | ||
| import csv | ||
| import io | ||
| import base64 | ||
| import json | ||
| from .json_utils import load_urls | ||
|
|
@@ -620,15 +622,20 @@ def RequestReportDelete(self, report_id, reportconfiguration_id=0): | |
| else: | ||
| return self.ExecuteBasicOnReport("ReportDeleteRequest", report_id) | ||
|
|
||
| def RequestReportAdhocGenerate(self, id): | ||
| def RequestReportAdhocGenerate(self, id, format='raw-xml-v2', template_id='audit-report'): | ||
| request = """ | ||
| <AdhocReportConfig format="raw-xml-v2" template-id="audit-report"> | ||
| <AdhocReportConfig format="{format}" template-id="{template_id}"> | ||
| <Filters> | ||
| <filter type="scan" id="{0}" /> | ||
| <filter type="scan" id="{scan_id}" /> | ||
| </Filters> | ||
| </AdhocReportConfig> | ||
| """ | ||
| return self.ExecuteBasicWithElement("ReportAdhocGenerateRequest", {}, as_xml(request.format(id))) | ||
| request_data = request.format( | ||
| format=format, | ||
| template_id=template_id, | ||
| scan_id=id, | ||
| ) | ||
| return self.ExecuteBasicWithElement("ReportAdhocGenerateRequest", {}, as_xml(request_data)) | ||
| raise NotImplementedError() # TODO | ||
|
|
||
| # | ||
|
|
@@ -1403,7 +1410,7 @@ def GetSiteAssetSummaries(self, site_or_id): | |
| object_creator = lambda xml_data: AssetSummary.CreateFromXML(xml_data, site_id=xml_data.getparent().attrib['site-id']) | ||
| return request_and_create_objects_from_xml(requestor, 'SiteDevices/device', object_creator) | ||
|
|
||
| def GetAssetDetails(self, asset_or_id): | ||
| def GetAssetDetails(self, asset_or_id, ignore_details_error=False): | ||
| """ | ||
| Get detailed information of an asset. | ||
| Requires the 2.1 API! | ||
|
|
@@ -1412,7 +1419,11 @@ def GetAssetDetails(self, asset_or_id): | |
| asset_or_id = asset_or_id.id | ||
| sub_url = APIURL_ASSETS.format(asset_or_id) | ||
| json_dict = self.ExecutePagedGet_v21(sub_url) | ||
| load_urls(json_dict, self.ExecutePagedGet_v21) | ||
| if 'tags' not in json_dict: | ||
| json_dict['tags'] = { | ||
| 'url': json_dict['url'] + '/tags' | ||
| } | ||
| load_urls(json_dict, self.ExecutePagedGet_v21, ignore_error=ignore_details_error) | ||
| return AssetDetails.CreateFromJSON(json_dict) | ||
|
|
||
| def DeleteAsset(self, asset_or_id): | ||
|
|
@@ -2032,22 +2043,45 @@ def DownloadReport(self, report_or_id, callback_function=None, block_size=DEFAUL | |
| reader = self.GetReportStreamReader(report_or_id) | ||
| return DownloadFromStreamReader(reader, callback_function, block_size) | ||
|
|
||
| def GenerateScanReport(self, scan_or_id): | ||
| def GenerateScanReport(self, scan_or_id, format='raw-xml-v2', template_id='audit-report'): | ||
| """ | ||
| Generate a report of a scan. | ||
| """ | ||
| if isinstance(scan_or_id, ScanSummary): | ||
| scan_or_id = scan_or_id.id | ||
| data = self.RequestReportAdhocGenerate(scan_or_id) | ||
| data = self.RequestReportAdhocGenerate(scan_or_id, format, template_id) | ||
| data = self.VerifySuccess(data) | ||
| data = data.tail.replace('\r', '').strip().split('\n') | ||
| assert data[1] == 'Content-Type: text/xml; name=report.xml' | ||
| assert data[2] == 'Content-Transfer-Encoding: base64' | ||
| assert data[3] == '' | ||
| assert data[0] == data[-1][:-2] | ||
| boundary_top = data[0] | ||
| content_type = data[1] | ||
| encoding = data[2] | ||
| body = ''.join(data[4:-1]) | ||
| boundary_bottom = data[-1] | ||
| if boundary_top != boundary_bottom[:-2]: | ||
| raise ValueError("Invalid boundary") | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I replaced the assertions as per Python recommendation: asserts could be compiled out so throwing exceptions is the preferred method here. |
||
| if encoding != 'Content-Transfer-Encoding: base64': | ||
| raise ValueError("Unexpected encoding") | ||
| if format == 'raw-xml-v2': | ||
| return self._ParseScanReportXML(body, content_type) | ||
| elif format == 'csv': | ||
| return self._ParseScanReportCSV(body, content_type) | ||
| else: | ||
| return data | ||
|
|
||
| @staticmethod | ||
| def _ParseScanReportXML(body, content_type): | ||
| if content_type != 'Content-Type: text/xml; name=report.xml': | ||
| raise ValueError("Invalid content type") | ||
| return as_xml(base64.urlsafe_b64decode(body)) | ||
|
|
||
| @staticmethod | ||
| def _ParseScanReportCSV(body, content_type): | ||
| if content_type != 'Content-Type: text/csv; name=report.csv': | ||
| raise ValueError("Invalid content type") | ||
| csv_ = base64.urlsafe_b64decode(body).decode('utf8') | ||
| report_data = csv.DictReader(io.StringIO(csv_)) | ||
| return report_data | ||
|
|
||
| # | ||
| # The following functions implement the Role Management API: | ||
| # ========================================================= | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
where does
scan_idcome from?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It comes from
idas part of the method signature. It's always been there, I just renamed it for clarity.