Skip to content

Commit

Permalink
Fix formatting, import order and linter warnings
Browse files Browse the repository at this point in the history
Run black and ruff on all the scripts.
  • Loading branch information
bjoernricks committed Jun 19, 2024
1 parent cfeb98c commit f708774
Show file tree
Hide file tree
Showing 48 changed files with 619 additions and 569 deletions.
1 change: 1 addition & 0 deletions scripts/bulk-modify-schedules.gmp.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import sys
from argparse import Namespace

from gvm.protocols.gmp import Gmp


Expand Down
3 changes: 2 additions & 1 deletion scripts/certbund-report.gmp.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@
from itertools import zip_longest
from typing import Dict, List, Optional, Sequence, Tuple, TypeVar, overload

import ssv_csv
from gvm.errors import GvmResponseError
from gvm.protocols.gmp import Gmp

import ssv_csv

# from gvm.xml import pretty_print

sys.path.append(os.path.dirname(args.argv[0])) # type: ignore
Expand Down
47 changes: 21 additions & 26 deletions scripts/clean-sensor.gmp.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

from argparse import Namespace

from gvm.protocols.gmp import Gmp
from gvm.errors import GvmResponseError
from gvm.protocols.gmp import Gmp


def clean_sensor(gmp: Gmp) -> None:
Expand All @@ -24,7 +24,7 @@ def clean_sensor(gmp: Gmp) -> None:
print(status_text)
except GvmResponseError as gvmerr:
print(f"{gvmerr=}")
pass
pass

targets = gmp.get_targets(filter_string="rows=-1 not _owner=""")
try:
Expand All @@ -36,39 +36,37 @@ def clean_sensor(gmp: Gmp) -> None:
print(status_text)
except GvmResponseError as gvmerr:
print(f"{gvmerr=}")
pass
pass

configs = gmp.get_scan_configs(
filter_string="rows=-1 not _owner="""
)
try:
for config_id in configs.xpath("config/@id"):
print(f"Removing config {config_id} ... ")
status_text = gmp.delete_scan_config(config_id, ultimate=True).xpath(
"@status_text"
)[0]
status_text = gmp.delete_scan_config(
config_id, ultimate=True
).xpath("@status_text")[0]
print(status_text)
except GvmResponseError as gvmerr:
print(f"{gvmerr=}")
pass
pass

port_lists = gmp.get_port_lists(
filter_string="rows=-1 not _owner="""
)
try:
for port_list_id in port_lists.xpath("port_list/@id"):
print(f"Removing port_list {port_list_id} ... ")
status_text = gmp.delete_port_list(port_list_id, ultimate=True).xpath(
"@status_text"
)[0]
status_text = gmp.delete_port_list(
port_list_id, ultimate=True
).xpath("@status_text")[0]
print(status_text)
except GvmResponseError as gvmerr:
print(f"{gvmerr=}")
pass
pass

alerts = gmp.get_alerts(
filter_string="rows=-1 not _owner="""
)
alerts = gmp.get_alerts(filter_string="rows=-1 not _owner=""")
try:
for alert_id in alerts.xpath("alert/@id"):
print(f"Removing alert {alert_id} ... ")
Expand All @@ -78,7 +76,7 @@ def clean_sensor(gmp: Gmp) -> None:
print(status_text)
except GvmResponseError as gvmerr:
print(f"{gvmerr=}")
pass
pass

schedules = gmp.get_schedules(
filter_string="rows=-1 not _owner="""
Expand All @@ -92,11 +90,9 @@ def clean_sensor(gmp: Gmp) -> None:
print(status_text)
except GvmResponseError as gvmerr:
print(f"{gvmerr=}")
pass
pass

tags = gmp.get_tags(
filter_string="rows=-1 not _owner="""
)
tags = gmp.get_tags(filter_string="rows=-1 not _owner=""")
try:
for tag_id in tags.xpath("tag/@id"):
print(f"Removing tag {tag_id} ... ")
Expand All @@ -106,11 +102,9 @@ def clean_sensor(gmp: Gmp) -> None:
print(status_text)
except GvmResponseError as gvmerr:
print(f"{gvmerr=}")
pass
pass

filters = gmp.get_filters(
filter_string="rows=-1 not _owner="""
)
filters = gmp.get_filters(filter_string="rows=-1 not _owner=""")
try:
for filter_id in filters.xpath("filter/@id"):
print(f"Removing filter {filter_id} ... ")
Expand All @@ -120,7 +114,7 @@ def clean_sensor(gmp: Gmp) -> None:
print(status_text)
except GvmResponseError as gvmerr:
print(f"{gvmerr=}")
pass
pass

credentials = gmp.get_credentials(
filter_string="rows=-1 not _owner="""
Expand All @@ -134,15 +128,16 @@ def clean_sensor(gmp: Gmp) -> None:
print(status_text)
except GvmResponseError as gvmerr:
print(f"{gvmerr=}")
pass
pass

print("Emptying trash... ")
try:
status_text = gmp.empty_trashcan().xpath("@status_text")[0]
print(status_text)
except GvmResponseError as gvmerr:
print(f"{gvmerr=}")
pass
pass


def main(gmp: Gmp, args: Namespace) -> None:
# pylint: disable=unused-argument
Expand Down
3 changes: 1 addition & 2 deletions scripts/combine-reports.gmp.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@
from argparse import Namespace

from gvm.protocols.gmp import Gmp
from lxml import etree as e

from gvmtools.helper import generate_uuid
from lxml import etree as e


def check_args(args: Namespace) -> None:
Expand Down
72 changes: 38 additions & 34 deletions scripts/create-alerts-from-csv.gmp.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,14 @@
# Information on Variables to be used in alerts: https://docs.greenbone.net/GSM-Manual/gos-22.04/en/scanning.html#using-alerts
# Example script: https://forum.greenbone.net/t/working-example-of-creating-an-alert-using-script/7511/2

import csv
import sys
import time
import csv
import json

from argparse import ArgumentParser, Namespace, RawTextHelpFormatter
from pathlib import Path
from typing import List
from gvm.errors import GvmResponseError

from gvm.errors import GvmResponseError
from gvm.protocols.gmp import Gmp

from gvmtools.helper import error_and_exit

HELP_TEXT = (
Expand All @@ -30,6 +26,7 @@
"It should be rather self explanatory."
)


def check_args(args):
len_args = len(args.script) - 1
if len_args != 2:
Expand Down Expand Up @@ -74,6 +71,7 @@ def parse_args(args: Namespace) -> Namespace: # pylint: disable=unused-argument
script_args, _ = parser.parse_known_args(args)
return script_args


def alert_id(
gmp: Gmp,
alert_name: str,
Expand All @@ -83,50 +81,55 @@ def alert_id(
alert_id = ""

for alert in alerts_xml:
name = "".join(alert.xpath("name/text()"))
"".join(alert.xpath("name/text()"))
alert_id = alert.get("id")
return alert_id


def credential_id(
gmp: Gmp,
credential_name: str,
):
response_xml = gmp.get_credentials(filter_string="rows=-1, name=" + credential_name)
response_xml = gmp.get_credentials(
filter_string="rows=-1, name=" + credential_name
)
credentials_xml = response_xml.xpath("credential")
credential_id = ""

for credential in credentials_xml:
name = "".join(credential.xpath("name/text()"))
credential_id = credential.get("id")
return credential_id


def report_format_id(
gmp: Gmp,
report_format_name: str,
):
response_xml = gmp.get_report_formats(details=True, filter_string="rows=-1, name=" + report_format_name)
response_xml = gmp.get_report_formats(
details=True, filter_string="rows=-1, name=" + report_format_name
)
report_formats_xml = response_xml.xpath("report_format")
report_format_id = ""

for report_format in report_formats_xml:
name = "".join(report_format.xpath("name/text()"))
report_format_id = report_format.get("id")
return report_format_id


def event_list(string):
event_list = list(string.split(" "))
return event_list


def create_alerts(

def create_alerts(
gmp: Gmp,
alert_file: Path,
):
try:
numberalerts = 0
with open(alert_file, encoding="utf-8") as csvFile:
content = csv.reader(csvFile, delimiter=',') #read the data
for row in content: #loop through each row
content = csv.reader(csvFile, delimiter=",") # read the data
for row in content: # loop through each row
if len(row) == 0:
continue
alert_name = row[0]
Expand All @@ -140,12 +143,12 @@ def create_alerts(
event_data = row[8]

comment = f"Created: {time.strftime('%Y/%m/%d-%H:%M:%S')}"
alert_type=getattr(gmp.types.AlertMethod, str_alert_type)
alert_type = getattr(gmp.types.AlertMethod, str_alert_type)

if alert_id(gmp, alert_name):
print(f"Alert: {alert_name} exist, not creating...")
continue

if str_alert_type == "EMAIL":
sender_email = strRow2
recipient_email = strRow3
Expand Down Expand Up @@ -174,7 +177,7 @@ def create_alerts(
numberalerts = numberalerts + 1
except GvmResponseError as gvmerr:
print(f"{gvmerr=}, name: {alert_name}")
pass
pass
else:
smb_credential = credential_id(gmp, strRow2)
smb_share_path = strRow3
Expand All @@ -185,33 +188,34 @@ def create_alerts(
try:
print("Creating alert: " + alert_name)
gmp.create_alert(
name=alert_name,
comment=comment,
event=gmp.types.AlertEvent.TASK_RUN_STATUS_CHANGED,
event_data={"status": event_data},
condition=gmp.types.AlertCondition.ALWAYS,
method=alert_type,
method_data={
"smb_credential": smb_credential,
"smb_share_path": smb_share_path,
"smb_report_format": report_format,
"smb_file_path": smb_file_path,
},
name=alert_name,
comment=comment,
event=gmp.types.AlertEvent.TASK_RUN_STATUS_CHANGED,
event_data={"status": event_data},
condition=gmp.types.AlertCondition.ALWAYS,
method=alert_type,
method_data={
"smb_credential": smb_credential,
"smb_share_path": smb_share_path,
"smb_report_format": report_format,
"smb_file_path": smb_file_path,
},
)
numberalerts = numberalerts + 1
except GvmResponseError as gvmerr:
print(f"{gvmerr=}, name: {alert_name}")
pass
csvFile.close() #close the csv file
pass
csvFile.close() # close the csv file

except IOError as e:
error_and_exit(f"Failed to read alert_file: {str(e)} (exit)")

if len(row) == 0:
error_and_exit("alerts file is empty (exit)")

return numberalerts



def main(gmp: Gmp, args: Namespace) -> None:
# pylint: disable=undefined-variable
if args.script:
Expand Down
3 changes: 1 addition & 2 deletions scripts/create-consolidated-report.gmp.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@

from gvm.errors import GvmError
from gvm.protocols.gmp import Gmp
from lxml import etree as e

from gvmtools.helper import error_and_exit, generate_uuid
from lxml import etree as e

HELP_TEXT = (
"This script creates a consolidated report and imports it to the GSM. "
Expand Down
Loading

0 comments on commit f708774

Please sign in to comment.