Skip to content

Commit

Permalink
Add: New create-consolidated-report container opts
Browse files Browse the repository at this point in the history
This adds the command line options ++container-id and
++new-container-name to the create-consolidated-report script.

The ++container-id option checks if a container task with the
given id exists and upload the consolidated report there instead
of creating a new task.

The ++new-container-name defines the name of the container task
when creating a new one instead of using an automatically generated
name.
  • Loading branch information
timopollmeier authored and greenbonebot committed Nov 21, 2023
1 parent 5e211b3 commit c06eca9
Show file tree
Hide file tree
Showing 2 changed files with 174 additions and 6 deletions.
77 changes: 71 additions & 6 deletions scripts/create-consolidated-report.gmp.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,28 @@ def parse_args(args: Namespace) -> Namespace: # pylint: disable=unused-argument
),
)

container_args = parser.add_mutually_exclusive_group()

container_args.add_argument(
"++container-id",
type=str,
dest="container_id",
help=(
"Add the consolidated report to the container task"
" with the given id instead of creating a new one."
),
)

container_args.add_argument(
"++new-container-name",
type=str,
dest="new_container_name",
help=(
"Create a new container task with the given name instead"
" of using an automatically generated name."
),
)

parser.add_argument(
"+t",
"++tags",
Expand Down Expand Up @@ -289,8 +311,31 @@ def combine_reports(
return combined_report


def get_container_name(gmp: Gmp, container_id: str):
"""
Gets the name of a task by id and checks if it is a container
gmp: the GMP object
container_id: Id of the task to check
"""
res = gmp.get_task(container_id)

task_name = res.xpath("//task/name")[0].text
task_target_id = res.xpath("//task/target/@id")[0]

if task_target_id != "":
error_and_exit(f"Task [{container_id}] is not a container")

return task_name


def send_report(
gmp: Gmp, combined_report: e.Element, period_start: date, period_end: date
gmp: Gmp,
combined_report: e.Element,
period_start: date,
period_end: date,
container_id: str,
new_container_name: str,
) -> str:
"""Creating a container task and sending the combined report to the GSM
Expand All @@ -300,13 +345,31 @@ def send_report(
period_end: the end date
"""

task_name = f"Consolidated Report [{period_start} - {period_end}]"
task_id = None
task_name = None

res = gmp.create_container_task(
name=task_name, comment="Created with gvm-tools."
)
if container_id:
task_name = get_container_name(gmp, container_id)
task_id = container_id
print(
"Adding consolidated report to existing container task"
f" [{task_name}] with UUID [{task_id}]"
)
else:
if new_container_name:
task_name = new_container_name
else:
task_name = f"Consolidated Report [{period_start} - {period_end}]"

task_id = res.xpath("//@id")[0]
res = gmp.create_container_task(
name=task_name, comment="Created with gvm-tools."
)

task_id = res.xpath("//@id")[0]
print(
"Adding consolidated report to new container task"
f" [{task_name}] with UUID [{task_id}]"
)

combined_report = e.tostring(combined_report)

Expand Down Expand Up @@ -386,6 +449,8 @@ def main(gmp: Gmp, args: Namespace) -> None:
combined_report=combined_report,
period_start=period_start,
period_end=period_end,
container_id=parsed_args.container_id,
new_container_name=parsed_args.new_container_name,
)

print(f"Successfully imported new consolidated report [{report}]")
Expand Down
103 changes: 103 additions & 0 deletions tests/scripts/test_create_consolidated_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,109 @@ def test_send_report(self, mock_gmp: GmpMockFactory):
combined_report=combined_report,
period_start=period_start,
period_end=period_end,
container_id=None,
new_container_name=None,
)

self.assertEqual(report_id, created_report_id)

@patch("gvm.protocols.latest.Gmp", new_callable=GmpMockFactory)
def test_send_report_with_container_id(self, mock_gmp: GmpMockFactory):
combined_report = etree.fromstring(
'<report id="20574712-c404-4a04-9c83-03144ae02dca" '
'format_id="d5da9f67-8551-4e51-807b-b6a873d70e34" '
'extension="xml" content_type="text/xml">'
'<report id="20574712-c404-4a04-9c83-03144ae02dca">'
'<results start="1" max="-1">'
'<result id="00000001-0000-0000-0000-000000000000"/>'
'<result id="00000001-0000-0000-0000-000000000001"/>'
'<result id="00000001-0000-0000-0000-000000000002"/>'
'<result id="00000001-0000-0000-0000-000000000003"/>'
'<result id="00000001-0000-0000-0000-000000000004"/>'
"</results></report></report>"
)

task_id = "c347836e-3c51-4045-872d-3cb12637f4cc"
report_id = "0e4d8fb2-47fa-494e-a242-d5327d3772f9"

mock_gmp.mock_response(
"import_report",
'<create_report_response status="201" status_text="OK, '
f'resource created" id="{report_id}"/>',
)

# Returns a container task without a target
mock_gmp.mock_response(
"get_task",
'<get_tasks_response status="200" status_text="OK">'
'<task id="c347836e-3c51-4045-872d-3cb12637f4cc">'
"<name>test</name>"
'<target id=""/>'
"</task>"
"</get_tasks_response>",
)

period_start = date(2020, 1, 1)
period_end = date(2020, 2, 1)

created_report_id = self.create_consolidated_report.send_report(
gmp=mock_gmp.gmp_protocol,
combined_report=combined_report,
period_start=period_start,
period_end=period_end,
container_id=task_id,
new_container_name="test",
)

self.assertEqual(report_id, created_report_id)

@patch("gvm.protocols.latest.Gmp", new_callable=GmpMockFactory)
def test_send_report_with_container_id_failure(
self, mock_gmp: GmpMockFactory
):
combined_report = etree.fromstring(
'<report id="20574712-c404-4a04-9c83-03144ae02dca" '
'format_id="d5da9f67-8551-4e51-807b-b6a873d70e34" '
'extension="xml" content_type="text/xml">'
'<report id="20574712-c404-4a04-9c83-03144ae02dca">'
'<results start="1" max="-1">'
'<result id="00000001-0000-0000-0000-000000000000"/>'
'<result id="00000001-0000-0000-0000-000000000001"/>'
'<result id="00000001-0000-0000-0000-000000000002"/>'
'<result id="00000001-0000-0000-0000-000000000003"/>'
'<result id="00000001-0000-0000-0000-000000000004"/>'
"</results></report></report>"
)

task_id = "c347836e-3c51-4045-872d-3cb12637f4cc"
report_id = "0e4d8fb2-47fa-494e-a242-d5327d3772f9"

mock_gmp.mock_response(
"import_report",
'<create_report_response status="201" status_text="OK, '
f'resource created" id="{report_id}"/>',
)

# Returns a non-container task with a target defined
mock_gmp.mock_response(
"get_task",
'<get_tasks_response status="200" status_text="OK">'
'<task id="c347836e-3c51-4045-872d-3cb12637f4cc">'
"<name>test</name>"
'<target id="35995ae0-2430-4f0d-97da-fcb93350abb4"/>'
"</task>"
"</get_tasks_response>",
)

period_start = date(2020, 1, 1)
period_end = date(2020, 2, 1)

with self.assertRaises(SystemExit):
self.create_consolidated_report.send_report(
gmp=mock_gmp.gmp_protocol,
combined_report=combined_report,
period_start=period_start,
period_end=period_end,
container_id=task_id,
new_container_name="test",
)

0 comments on commit c06eca9

Please sign in to comment.