File: _credential_stores.py

package info (click to toggle)
python-gvm 26.9.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 5,316 kB
  • sloc: python: 46,784; makefile: 18
file content (171 lines) | stat: -rw-r--r-- 5,764 bytes parent folder | download
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
# SPDX-FileCopyrightText: 2025 Greenbone AG
#
# SPDX-License-Identifier: GPL-3.0-or-later

from base64 import b64encode
from typing import Optional

from gvm.errors import RequiredArgument
from gvm.protocols.core import Request
from gvm.utils import to_bool
from gvm.xml import XmlCommand

from .._entity_id import EntityID


class CredentialStores:
    @classmethod
    def get_credential_store(
        cls,
        credential_store_id: EntityID,
        *,
        details: Optional[bool] = None,
    ) -> Request:
        """Request a credential store

        Args:
            credential_store_id: ID of credential store to fetch
            details: True to request all details
        """

        if not credential_store_id:
            raise RequiredArgument(
                function=cls.get_credential_store.__name__,
                argument="credential_store_id",
            )

        cmd = XmlCommand("get_credential_stores")
        cmd.add_element("credential_store_id", str(credential_store_id))

        if details is not None:
            cmd.set_attribute("details", to_bool(details))

        return cmd

    @classmethod
    def get_credential_stores(
        cls,
        *,
        filter_string: Optional[str] = None,
        filter_id: Optional[EntityID] = None,
        details: Optional[bool] = None,
    ) -> Request:
        """Request a list of credential stores

        Args:
            filter_string: Filter term to use for the query
            filter_id: UUID of an existing filter to use for the query
            details: True to request all details
        """

        cmd = XmlCommand("get_credential_stores")
        cmd.add_filter(filter_string, filter_id)

        if details is not None:
            cmd.set_attribute("details", to_bool(details))

        return cmd

    @classmethod
    def modify_credential_store(
        cls,
        credential_store_id: EntityID,
        *,
        active: Optional[bool] = None,
        host: Optional[str] = None,
        port: Optional[int] = None,
        path: Optional[str] = None,
        app_id: Optional[str] = None,
        client_cert: Optional[str] = None,
        client_key: Optional[str] = None,
        client_pkcs12_file: Optional[str] = None,
        passphrase: Optional[str] = None,
        server_ca_cert: Optional[str] = None,
        comment: Optional[str] = None,
    ) -> Request:
        """Modify a credential store

        Args:
            credential_store_id: ID of credential store to fetch
            active: Whether the credential store is active
            host: The host to use for reaching the credential store
            port: The port to use for reaching the credential store
            path: The URI path the credential store is using
            app_id: Depends on the credential store used. Usually called the same in the credential store
            client_cert: The client certificate to use for authorization, as a plain string
            client_key: The client key to use for authorization, as a plain string
            client_pkcs12_file: The pkcs12 file contents to use for authorization, as a plain string
                (alternative to using client_cert and client_key)
            passphrase: The passphrase to use to decrypt client_pkcs12_file or client_key file
            server_ca_cert: The server certificate, so the credential store can be trusted
            comment: An optional comment to store alongside the credential store
        """

        if not credential_store_id:
            raise RequiredArgument(
                function=cls.verify_credential_store.__name__,
                argument="credential_store_id",
            )

        cmd = XmlCommand("modify_credential_store")
        cmd.set_attribute("credential_store_id", str(credential_store_id))

        if active is not None:
            cmd.add_element("active", to_bool(active))
        if host:
            cmd.add_element("host", host)
        if port:
            cmd.add_element("port", str(port))
        if path:
            cmd.add_element("path", path)
        if comment:
            cmd.add_element("comment", comment)

        preferences = cmd.add_element("preferences")

        if app_id:
            preferences.add_element("app_id", app_id)
        if client_cert:
            preferences.add_element(
                "client_cert",
                b64encode(client_cert.encode("ascii")).decode("ascii"),
            )
        if client_key:
            preferences.add_element(
                "client_key",
                b64encode(client_key.encode("ascii")).decode("ascii"),
            )
        if client_pkcs12_file:
            preferences.add_element(
                "client_pkcs12_file",
                b64encode(client_pkcs12_file.encode("ascii")).decode("ascii"),
            )
        if passphrase:
            preferences.add_element("passphrase", passphrase)
        if server_ca_cert:
            preferences.add_element(
                "server_ca_cert",
                b64encode(server_ca_cert.encode("ascii")).decode("ascii"),
            )

        return cmd

    @classmethod
    def verify_credential_store(
        cls,
        credential_store_id: EntityID,
    ) -> Request:
        """Verify that the connection to a credential store works

        Args:
            credential_store_id: The uuid of the credential store to verify
        """
        if not credential_store_id:
            raise RequiredArgument(
                function=cls.verify_credential_store.__name__,
                argument="credential_store_id",
            )

        cmd = XmlCommand("verify_credential_store")
        cmd.set_attribute("credential_store_id", str(credential_store_id))
        return cmd