From 029aa88d0c28c848a7bfad1b6bc503bf0262f109 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Tue, 18 Jun 2024 15:30:25 +0200 Subject: [PATCH] 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. --- .../gmp/requests/v224/_credentials.py | 25 ++++++++++++- .../gmp/requests/v224/test_credentials.py | 36 ++++++++++++++++--- .../credentials/test_create_credential.py | 31 ++++++++++++++++ 3 files changed, 86 insertions(+), 6 deletions(-) diff --git a/gvm/protocols/gmp/requests/v224/_credentials.py b/gvm/protocols/gmp/requests/v224/_credentials.py index ccf011057..f0518fea4 100644 --- a/gvm/protocols/gmp/requests/v224/_credentials.py +++ b/gvm/protocols/gmp/requests/v224/_credentials.py @@ -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( @@ -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__, diff --git a/tests/protocols/gmp/requests/v224/test_credentials.py b/tests/protocols/gmp/requests/v224/test_credentials.py index b5903a87c..cbd57a7f4 100644 --- a/tests/protocols/gmp/requests/v224/test_credentials.py +++ b/tests/protocols/gmp/requests/v224/test_credentials.py @@ -68,6 +68,21 @@ def test_create_credential_username_password(self): b"", ) + 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"" + b"name" + b"up" + b"username" + b"", + ) + def test_create_username_password_credential_with_allow_insecure(self): request = Credentials.create_credential( "name", @@ -207,12 +222,23 @@ def test_create_credential_username_ssh_key_with_passphrase(self): b"", ) - 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"" + b"name" + b"usk" + b"username" + b"", + ) + def test_create_credential_username_ssh_key_missing_private_key(self): with self.assertRaises(RequiredArgument): Credentials.create_credential( "name", diff --git a/tests/protocols/gmpv224/entities/credentials/test_create_credential.py b/tests/protocols/gmpv224/entities/credentials/test_create_credential.py index 04a82dee5..b05f541f4 100644 --- a/tests/protocols/gmpv224/entities/credentials/test_create_credential.py +++ b/tests/protocols/gmpv224/entities/credentials/test_create_credential.py @@ -87,6 +87,21 @@ def test_create_up_credential_with_allow_insecure(self): b"" ) + 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"" + b"foo" + b"up" + b"Max" + b"" + ) + def test_create_cc_credential_missing_certificate(self): with self.assertRaises(RequiredArgument): self.gmp.create_credential( @@ -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): @@ -183,6 +199,21 @@ def test_create_usk_credential_with_key_phrase(self): b"" ) + 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"" + b"foo" + b"usk" + b"foo" + b"" + ) + def test_create_snmp_credential_invalid_auth_algorithm(self): with self.assertRaises(RequiredArgument): self.gmp.create_credential(