Skip to content

Commit

Permalink
feat: Add hostnames as comment
Browse files Browse the repository at this point in the history
  • Loading branch information
linuxdaemon committed May 21, 2024
1 parent a4ead79 commit cc4f080
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
14 changes: 14 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,24 @@ enable_error_code = [
"possibly-undefined",
"truthy-bool",
"truthy-iterable",
"unused-awaitable",
"explicit-override",
"mutable-override",
"unimported-reveal",
"ignore-without-code",
"narrowed-type-not-subtype",
]
untyped_calls_exclude = ["gvm.transforms"]
follow_imports = "silent"
strict_concatenate = true
allow_redefinition = false
allow_untyped_globals = false
disallow_any_decorated = true
disallow_any_explicit = true
disallow_any_unimported = true
disallow_incomplete_defs = true
disallow_subclassing_any = true
warn_incomplete_stub = true

[tool.pydantic-mypy]
init_forbid_extra = true
Expand Down
20 changes: 18 additions & 2 deletions src/gvm_sync_targets/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,20 @@ def gvm_sync_targets(
) as gmp:
gmp = cast(Gmpv225, gmp)
hosts = read_lines(hosts_file.read())
host_map: dict[str, str | None] = {}
for entry in hosts:
if " " in entry:
k, v = entry.split(None, 1)
else:
k = entry
v = None

host_map[k] = v

gmp.authenticate(username, password)
to_add = hosts.copy()
to_add = list(host_map.keys())
to_remove: list[str] = []
to_update: list[tuple[str, str]] = []

for host in get_all_hosts(gmp, details=True):
ips = {
Expand All @@ -62,11 +73,16 @@ def gvm_sync_targets(
for ip in ips:
if ip in to_add:
to_add.remove(ip)
if host.comment != host_map.get(ip):
to_update.append((host.uuid, ip))
else:
to_remove.append(host.uuid)

for ip in set(to_add):
gmp.create_host(ip)
gmp.create_host(ip, comment=host_map.get(ip))

for ip, uuid in set(to_update):
gmp.modify_host(uuid, comment=host_map.get(ip))

for uuid in set(to_remove):
gmp.delete_host(uuid)
Expand Down
3 changes: 2 additions & 1 deletion src/gvm_sync_targets/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# SPDX-License-Identifier: MIT

from collections.abc import Mapping
from typing import cast
from typing import cast, override

from gvm.transforms import EtreeCheckCommandTransform

Expand Down Expand Up @@ -66,6 +66,7 @@ class ModelTransform(EtreeCheckCommandTransform):
def __init__(self) -> None:
super().__init__() # type: ignore[no-untyped-call]

@override
def __call__(self, data: str) -> Response:
elem = cast(Element, super().__call__(data))
return _MODEL_MAP[elem.tag].from_xml_tree(elem) # type: ignore[arg-type] # etree.Element isn't a type but that's what this function expects

0 comments on commit cc4f080

Please sign in to comment.