1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
|
# SPDX-FileCopyrightText: 2024 Greenbone AG
#
# SPDX-License-Identifier: GPL-3.0-or-later
from typing import Optional
from gvm.errors import InvalidArgument, RequiredArgument
from gvm.protocols.core import Request
from gvm.utils import check_port, to_bool, to_comma_list
from gvm.xml import XmlCommand
from .._entity_id import EntityID
from ._severity import Severity
class Notes:
@classmethod
def create_note(
cls,
text: str,
nvt_oid: str,
*,
days_active: Optional[int] = None,
hosts: Optional[list[str]] = None,
port: Optional[str] = None,
result_id: Optional[EntityID] = None,
severity: Optional[Severity] = None,
task_id: Optional[EntityID] = None,
) -> Request:
"""Create a new note
Args:
text: Text of the new note
nvt_id: OID of the nvt to which note applies
days_active: Days note will be active. -1 on
always, 0 off
hosts: A list of host addresses
port: Port to which the override applies, needs to be a string
in the form {number}/{protocol}
result_id: UUID of a result to which note applies
severity: Severity to which note applies
task_id: UUID of task to which note applies
"""
if not text:
raise RequiredArgument(
function=cls.create_note.__name__, argument="text"
)
if not nvt_oid:
raise RequiredArgument(
function=cls.create_note.__name__, argument="nvt_oid"
)
cmd = XmlCommand("create_note")
cmd.add_element("text", text)
cmd.add_element("nvt", attrs={"oid": nvt_oid})
if days_active is not None:
cmd.add_element("active", str(days_active))
if hosts:
cmd.add_element("hosts", to_comma_list(hosts))
if port:
if check_port(port):
cmd.add_element("port", port)
else:
raise InvalidArgument(
function=cls.create_note.__name__, argument="port"
)
if result_id:
cmd.add_element("result", attrs={"id": str(result_id)})
if severity is not None:
cmd.add_element("severity", str(severity))
if task_id:
cmd.add_element("task", attrs={"id": str(task_id)})
return cmd
@classmethod
def modify_note(
cls,
note_id: EntityID,
text: str,
*,
days_active: Optional[int] = None,
hosts: Optional[list[str]] = None,
port: Optional[str] = None,
result_id: Optional[EntityID] = None,
severity: Optional[Severity] = None,
task_id: Optional[EntityID] = None,
) -> Request:
"""Modify a note
Args:
note_id: The UUID of the note to modify
text: Text of the note
days_active: Days note will be active. -1 on always, 0 off
hosts: A list of host addresses
port: Port to which the override applies, needs to be a string
in the form {number}/{protocol}
result_id: UUID of a result to which note applies
severity: Severity to which note applies
task_id: UUID of task to which note applies
"""
if not note_id:
raise RequiredArgument(
function=cls.modify_note.__name__, argument="note_id"
)
if not text:
raise RequiredArgument(
function=cls.modify_note.__name__, argument="text"
)
cmd = XmlCommand("modify_note")
cmd.set_attribute("note_id", str(note_id))
cmd.add_element("text", text)
if days_active is not None:
cmd.add_element("active", str(days_active))
if hosts:
cmd.add_element("hosts", to_comma_list(hosts))
if port:
if check_port(port):
cmd.add_element("port", port)
else:
raise InvalidArgument(
function=cls.modify_note.__name__, argument="port"
)
if result_id:
cmd.add_element("result", attrs={"id": str(result_id)})
if severity is not None:
cmd.add_element("severity", str(severity))
if task_id:
cmd.add_element("task", attrs={"id": str(task_id)})
return cmd
@classmethod
def clone_note(cls, note_id: EntityID) -> Request:
"""Clone an existing note
Args:
note_id: UUID of an existing note to clone from
"""
if not note_id:
raise RequiredArgument(
function=cls.clone_note.__name__, argument="note_id"
)
cmd = XmlCommand("create_note")
cmd.add_element("copy", str(note_id))
return cmd
@classmethod
def delete_note(
cls, note_id: EntityID, *, ultimate: Optional[bool] = False
) -> Request:
"""Delete an existing note
Args:
note_id: UUID of the note to be deleted.
ultimate: Whether to remove entirely or to the trashcan.
"""
if not note_id:
raise RequiredArgument(
function=cls.delete_note.__name__, argument="note_id"
)
cmd = XmlCommand("delete_note")
cmd.set_attribute("note_id", str(note_id))
cmd.set_attribute("ultimate", to_bool(ultimate))
return cmd
@classmethod
def get_notes(
cls,
*,
filter_string: Optional[str] = None,
filter_id: Optional[EntityID] = None,
details: Optional[bool] = None,
result: Optional[bool] = None,
) -> Request:
"""Request a list of notes
Args:
filter_string: Filter notes by a string
filter_string: Filter term to use for the query
filter_id: UUID of an existing filter to use for the query
details: Add info about connected results and tasks
result: Return the details of possible connected results.
"""
cmd = XmlCommand("get_notes")
cmd.add_filter(filter_string, filter_id)
if details is not None:
cmd.set_attribute("details", to_bool(details))
if result is not None:
cmd.set_attribute("result", to_bool(result))
return cmd
@classmethod
def get_note(cls, note_id: EntityID) -> Request:
"""Request a single note
Arguments:
note_id: UUID of an existing note
"""
if not note_id:
raise RequiredArgument(
function=cls.get_note.__name__, argument="note_id"
)
cmd = XmlCommand("get_notes")
cmd.set_attribute("note_id", str(note_id))
# for single entity always request all details
cmd.set_attribute("details", "1")
return cmd
|