Skip to content

Commit

Permalink
feat: Handle get_targets_response item
Browse files Browse the repository at this point in the history
  • Loading branch information
linuxdaemon committed May 17, 2024
1 parent 228b2d4 commit 7b4556e
Show file tree
Hide file tree
Showing 9 changed files with 321 additions and 72 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ repos:
repo: https://github.com/commitizen-tools/commitizen
rev: faff6a61f4f53585a965addd06156eedf0027643 # frozen: v3.25.0
- hooks:
- entry: hatch run python3 -m mypy
- entry: hatch run types:check
id: mypy
language: system
name: mypy
Expand Down
7 changes: 6 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ setup-dev = ["setup-pre-commit"]

pre-commit = "python3 -m pre_commit run {args:--all}"

[tool.hatch.envs.types]
dependencies = ["mypy>=1.8", "types-lxml"]

[tool.hatch.envs.types.scripts]
check = "mypy --non-interactive --install-types {args:src/gvm_sync_targets tests}"

[tool.hatch.envs.testall]
type = "container"
dependencies = ["coverage[toml]>=6.5", "pytest>=6.0"]
Expand Down Expand Up @@ -170,7 +176,6 @@ extra_checks = true
warn_unreachable = true
warn_return_any = true
warn_no_return = true
incremental = false
enable_error_code = [
"redundant-self",
"redundant-expr",
Expand Down
10 changes: 10 additions & 0 deletions src/gvm_sync_targets/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,23 @@
)
from gvm_sync_targets.models.auth_response import AuthenticateResponse
from gvm_sync_targets.models.response import Response
from gvm_sync_targets.models.targets_response import GetTargetsResponse
from gvm_sync_targets.util import Element

__all__ = [
"GetAssetsResponse",
"AuthenticateResponse",
"DeleteAssetResponse",
"CreateAssetResponse",
"GetTargetsResponse",
]

_MODEL_MAP: Mapping[str, type[Response]] = {
"get_assets_response": GetAssetsResponse,
"authenticate_response": AuthenticateResponse,
"delete_asset_response": DeleteAssetResponse,
"create_asset_response": CreateAssetResponse,
"get_targets_response": GetTargetsResponse,
}


Expand Down
80 changes: 11 additions & 69 deletions src/gvm_sync_targets/models/assets_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,20 @@
# SPDX-License-Identifier: MIT

import datetime
from typing import Annotated, Optional
from typing import Optional

from pydantic import PlainSerializer
from pydantic_xml import attr, element

from gvm_sync_targets.models.model import Model
from gvm_sync_targets.models.model import IntBoolean, Model
from gvm_sync_targets.models.resource import (
Count,
Filters,
Pagination,
Resource,
Sort,
)
from gvm_sync_targets.models.response import Response

IntBoolean = Annotated[
bool, PlainSerializer(lambda value: 1 if value else 0, return_type=int)
]


class Severity(Model, tag="severity"):
value: Optional[float] = element(default=None)
Expand All @@ -40,18 +42,6 @@ class Host(Model, tag="host"):
details: list[Detail] = element("detail", default_factory=list)


class Owner(Model, tag="owner"):
name: str = element()


class Permission(Model, tag="permission"):
name: str = element()


class Permissions(Model, tag="permissions"):
permissions: list[Permission] = element()


class Tag(Model, tag="tag"):
uuid: str = attr("id")

Expand Down Expand Up @@ -86,67 +76,19 @@ class Identifiers(Model, tag="identifiers"):
identifiers: list[Identifier] = element()


class Asset(Model, tag="asset"):
uuid: str = attr("id")

owner: Owner
name: str = element()
comment: Optional[str] = element(default=None)
creation_time: datetime.datetime = element()
modification_time: datetime.datetime = element()
writable: IntBoolean = element()
in_use: IntBoolean = element()

permissions: Permissions = element()
class Asset(Resource, tag="asset"):
user_tags: Optional[UserTags] = element(default=None)
identifiers: Identifiers = element()
type: str = element()
host: Host = element("host")


class Keyword(Model, tag="keyword"):
column: str = element()
relation: str = element()
value: str = element()


class Keywords(Model, tag="keywords"):
keywords: list[Keyword] = element()


class Filters(Model, tag="filters"):
uuid: str = attr("id")

term: str = element()
keywords: Keywords = element()


class AssetCount(Model, tag="asset_count"):
total: int
filtered: int = element()
page: int = element()


class Pagination(Model):
start: int = attr()
max: int = attr()


class SortField(Model, tag="field"):
name: str
order: str = element()


class Sort(Model, tag="sort"):
field: SortField


class GetAssetsResponse(Response, tag="get_assets_response"):
assets: list[Asset] = []
filters: Filters
sort: Sort
pagination: Pagination = element("assets")
asset_count: AssetCount
asset_count: Count = element("asset_count")


class DeleteAssetResponse(Response, tag="delete_asset_response"):
Expand Down
9 changes: 8 additions & 1 deletion src/gvm_sync_targets/models/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,15 @@
#
# SPDX-License-Identifier: MIT

from typing import Annotated

from pydantic import PlainSerializer
from pydantic_xml import BaseXmlModel

IntBoolean = Annotated[
bool, PlainSerializer(lambda value: 1 if value else 0, return_type=int)
]


class Model(BaseXmlModel, extra="forbid"):
class Model(BaseXmlModel, search_mode="unordered", extra="forbid"):
pass
69 changes: 69 additions & 0 deletions src/gvm_sync_targets/models/resource.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import datetime
from typing import Optional

from pydantic_xml import attr, element

from gvm_sync_targets.models.model import IntBoolean, Model


class Owner(Model, tag="owner"):
name: str = element()


class Permission(Model, tag="permission"):
name: str = element()


class Permissions(Model, tag="permissions"):
permissions: list[Permission] = element()


class Keyword(Model, tag="keyword"):
column: str = element()
relation: str = element()
value: str = element()


class Keywords(Model, tag="keywords"):
keywords: list[Keyword] = element()


class Filters(Model, tag="filters"):
uuid: str = attr("id")

term: str = element()
keywords: Keywords = element()


class Count(Model):
total: int
filtered: int = element()
page: int = element()


class Pagination(Model):
start: int = attr()
max: int = attr()


class SortField(Model, tag="field"):
name: str
order: str = element()


class Sort(Model, tag="sort"):
field: SortField


class Resource(Model):
uuid: str = attr("id")

owner: Owner
name: str = element()
comment: Optional[str] = element(default=None)
creation_time: datetime.datetime = element()
modification_time: datetime.datetime = element()
writable: IntBoolean = element()
in_use: IntBoolean = element()

permissions: Permissions = element()
72 changes: 72 additions & 0 deletions src/gvm_sync_targets/models/targets_response.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
from typing import Optional

from pydantic_xml import attr, element

from gvm_sync_targets.models.model import IntBoolean, Model
from gvm_sync_targets.models.resource import (
Count,
Filters,
Pagination,
Resource,
Sort,
)
from gvm_sync_targets.models.response import Response


class Credential(Model):
uuid: str = attr("id")
name: Optional[str] = element(default=None)

trash: IntBoolean = element(tag="trash")


class SSHCredential(Credential, tag="ssh_credential"):
port: int = element(tag="port")


class SMBCredential(Credential, tag="smb_credential"):
pass


class ESXICredential(Credential, tag="esxi_credential"):
pass


class SNMPCredential(Credential, tag="snmp_credential"):
pass


class SSHElevateCredential(Credential, tag="ssh_elevate_credential"):
pass


class PortList(Model, tag="port_list"):
uuid: str = attr("id")

name: str = element()
trash: IntBoolean = element()


class Target(Resource, tag="target"):
hosts: str = element()
exclude_hosts: Optional[str] = element(default=None)
max_hosts: int = element()
port_list: PortList = element()
ssh_credential: SSHCredential = element()
smb_credential: SMBCredential = element()
esxi_credential: ESXICredential = element()
snmp_credential: SNMPCredential = element()
ssh_elevate_credential: SSHElevateCredential = element()

reverse_lookup_only: IntBoolean = element()
reverse_lookup_unify: IntBoolean = element()
alive_tests: str = element()
allow_simultaneous_ips: IntBoolean = element()


class GetTargetsResponse(Response, tag="get_targets_response"):
targets: list[Target] = element()
filters: Filters
sort: Sort
pagination: Pagination = element("targets")
asset_count: Count = element("target_count")
Empty file added src/gvm_sync_targets/py.typed
Empty file.
Loading

0 comments on commit 7b4556e

Please sign in to comment.