Skip to content

Commit

Permalink
Change: Allow to auto-generate password and ssh-key for credentials
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bjoernricks committed Jun 18, 2024
1 parent ae1bd44 commit 029aa88
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 6 deletions.
25 changes: 24 additions & 1 deletion gvm/protocols/gmp/requests/v224/_credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,26 @@ def create_credential(
credential_type=CredentialType.PASSWORD_ONLY,
password='foo',
)
Creating an auto-generated password
.. code-block:: python
request = Credentials.create_credential(
name='UP Credential',
credential_type=CredentialType.USERNAME_PASSWORD,
login='foo',
)
Creating an auto-generated SSH-Key credential
.. code-block:: python
request = Credentials.create_credential(
name='USK Credential',
credential_type=CredentialType.USERNAME_SSH_KEY,
login='foo',
)
"""
if not name:
raise RequiredArgument(
Expand Down Expand Up @@ -256,7 +276,10 @@ def create_credential(
) and password:
cmd.add_element("password", password)

if credential_type == CredentialType.USERNAME_SSH_KEY:
if (
credential_type == CredentialType.USERNAME_SSH_KEY
and private_key is not None
):
if not private_key:
raise RequiredArgument(
function=cls.create_credential.__name__,
Expand Down
36 changes: 31 additions & 5 deletions tests/protocols/gmp/requests/v224/test_credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,21 @@ def test_create_credential_username_password(self):
b"</create_credential>",
)

def test_create_credential_username_password_auto_generate_password(self):
request = Credentials.create_credential(
"name",
CredentialType.USERNAME_PASSWORD,
login="username",
)
self.assertEqual(
bytes(request),
b"<create_credential>"
b"<name>name</name>"
b"<type>up</type>"
b"<login>username</login>"
b"</create_credential>",
)

def test_create_username_password_credential_with_allow_insecure(self):
request = Credentials.create_credential(
"name",
Expand Down Expand Up @@ -207,12 +222,23 @@ def test_create_credential_username_ssh_key_with_passphrase(self):
b"</create_credential>",
)

def test_create_credential_username_ssh_key_missing_private_key(self):
with self.assertRaises(RequiredArgument):
Credentials.create_credential(
"name", CredentialType.USERNAME_SSH_KEY, login="username"
)
def test_create_credential_username_ssh_key_auto_generate_key(self):
request = Credentials.create_credential(
"name",
CredentialType.USERNAME_SSH_KEY,
login="username",
)

self.assertEqual(
bytes(request),
b"<create_credential>"
b"<name>name</name>"
b"<type>usk</type>"
b"<login>username</login>"
b"</create_credential>",
)

def test_create_credential_username_ssh_key_missing_private_key(self):
with self.assertRaises(RequiredArgument):
Credentials.create_credential(
"name",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,21 @@ def test_create_up_credential_with_allow_insecure(self):
b"</create_credential>"
)

def test_create_up_credential_auto_generate_password(self):
self.gmp.create_credential(
name="foo",
credential_type=CredentialType.USERNAME_PASSWORD,
login="Max",
)

self.connection.send.has_been_called_with(
b"<create_credential>"
b"<name>foo</name>"
b"<type>up</type>"
b"<login>Max</login>"
b"</create_credential>"
)

def test_create_cc_credential_missing_certificate(self):
with self.assertRaises(RequiredArgument):
self.gmp.create_credential(
Expand Down Expand Up @@ -133,6 +148,7 @@ def test_create_usk_credential_missing_private_key(self):
name="foo",
credential_type=CredentialType.USERNAME_SSH_KEY,
login="foo",
private_key="",
)

def test_create_usk_credential_missing_login(self):
Expand Down Expand Up @@ -183,6 +199,21 @@ def test_create_usk_credential_with_key_phrase(self):
b"</create_credential>"
)

def test_create_usk_credential_auto_generate_ssh_key(self):
self.gmp.create_credential(
name="foo",
credential_type=CredentialType.USERNAME_SSH_KEY,
login="foo",
)

self.connection.send.has_been_called_with(
b"<create_credential>"
b"<name>foo</name>"
b"<type>usk</type>"
b"<login>foo</login>"
b"</create_credential>"
)

def test_create_snmp_credential_invalid_auth_algorithm(self):
with self.assertRaises(RequiredArgument):
self.gmp.create_credential(
Expand Down

0 comments on commit 029aa88

Please sign in to comment.