Skip to content

Commit 029aa88

Browse files
committed
Change: Allow to auto-generate password and ssh-key for credentials
Support auto-generating the password for a username+password credential and the ssh-key for a username+ssh-key credential. This is already supported by GMP since early versions but wasn't implemented in python-gvm.
1 parent ae1bd44 commit 029aa88

File tree

3 files changed

+86
-6
lines changed

3 files changed

+86
-6
lines changed

gvm/protocols/gmp/requests/v224/_credentials.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,26 @@ def create_credential(
194194
credential_type=CredentialType.PASSWORD_ONLY,
195195
password='foo',
196196
)
197+
198+
Creating an auto-generated password
199+
200+
.. code-block:: python
201+
202+
request = Credentials.create_credential(
203+
name='UP Credential',
204+
credential_type=CredentialType.USERNAME_PASSWORD,
205+
login='foo',
206+
)
207+
208+
Creating an auto-generated SSH-Key credential
209+
210+
.. code-block:: python
211+
212+
request = Credentials.create_credential(
213+
name='USK Credential',
214+
credential_type=CredentialType.USERNAME_SSH_KEY,
215+
login='foo',
216+
)
197217
"""
198218
if not name:
199219
raise RequiredArgument(
@@ -256,7 +276,10 @@ def create_credential(
256276
) and password:
257277
cmd.add_element("password", password)
258278

259-
if credential_type == CredentialType.USERNAME_SSH_KEY:
279+
if (
280+
credential_type == CredentialType.USERNAME_SSH_KEY
281+
and private_key is not None
282+
):
260283
if not private_key:
261284
raise RequiredArgument(
262285
function=cls.create_credential.__name__,

tests/protocols/gmp/requests/v224/test_credentials.py

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,21 @@ def test_create_credential_username_password(self):
6868
b"</create_credential>",
6969
)
7070

71+
def test_create_credential_username_password_auto_generate_password(self):
72+
request = Credentials.create_credential(
73+
"name",
74+
CredentialType.USERNAME_PASSWORD,
75+
login="username",
76+
)
77+
self.assertEqual(
78+
bytes(request),
79+
b"<create_credential>"
80+
b"<name>name</name>"
81+
b"<type>up</type>"
82+
b"<login>username</login>"
83+
b"</create_credential>",
84+
)
85+
7186
def test_create_username_password_credential_with_allow_insecure(self):
7287
request = Credentials.create_credential(
7388
"name",
@@ -207,12 +222,23 @@ def test_create_credential_username_ssh_key_with_passphrase(self):
207222
b"</create_credential>",
208223
)
209224

210-
def test_create_credential_username_ssh_key_missing_private_key(self):
211-
with self.assertRaises(RequiredArgument):
212-
Credentials.create_credential(
213-
"name", CredentialType.USERNAME_SSH_KEY, login="username"
214-
)
225+
def test_create_credential_username_ssh_key_auto_generate_key(self):
226+
request = Credentials.create_credential(
227+
"name",
228+
CredentialType.USERNAME_SSH_KEY,
229+
login="username",
230+
)
231+
232+
self.assertEqual(
233+
bytes(request),
234+
b"<create_credential>"
235+
b"<name>name</name>"
236+
b"<type>usk</type>"
237+
b"<login>username</login>"
238+
b"</create_credential>",
239+
)
215240

241+
def test_create_credential_username_ssh_key_missing_private_key(self):
216242
with self.assertRaises(RequiredArgument):
217243
Credentials.create_credential(
218244
"name",

tests/protocols/gmpv224/entities/credentials/test_create_credential.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,21 @@ def test_create_up_credential_with_allow_insecure(self):
8787
b"</create_credential>"
8888
)
8989

90+
def test_create_up_credential_auto_generate_password(self):
91+
self.gmp.create_credential(
92+
name="foo",
93+
credential_type=CredentialType.USERNAME_PASSWORD,
94+
login="Max",
95+
)
96+
97+
self.connection.send.has_been_called_with(
98+
b"<create_credential>"
99+
b"<name>foo</name>"
100+
b"<type>up</type>"
101+
b"<login>Max</login>"
102+
b"</create_credential>"
103+
)
104+
90105
def test_create_cc_credential_missing_certificate(self):
91106
with self.assertRaises(RequiredArgument):
92107
self.gmp.create_credential(
@@ -133,6 +148,7 @@ def test_create_usk_credential_missing_private_key(self):
133148
name="foo",
134149
credential_type=CredentialType.USERNAME_SSH_KEY,
135150
login="foo",
151+
private_key="",
136152
)
137153

138154
def test_create_usk_credential_missing_login(self):
@@ -183,6 +199,21 @@ def test_create_usk_credential_with_key_phrase(self):
183199
b"</create_credential>"
184200
)
185201

202+
def test_create_usk_credential_auto_generate_ssh_key(self):
203+
self.gmp.create_credential(
204+
name="foo",
205+
credential_type=CredentialType.USERNAME_SSH_KEY,
206+
login="foo",
207+
)
208+
209+
self.connection.send.has_been_called_with(
210+
b"<create_credential>"
211+
b"<name>foo</name>"
212+
b"<type>usk</type>"
213+
b"<login>foo</login>"
214+
b"</create_credential>"
215+
)
216+
186217
def test_create_snmp_credential_invalid_auth_algorithm(self):
187218
with self.assertRaises(RequiredArgument):
188219
self.gmp.create_credential(

0 commit comments

Comments
 (0)