commit 04805a416971c7262eabc37d67bd8f37ba5cd23b
Author: Stanislav Král <stanislav.kral3@gmail.com>
Date:   Mon May 3 10:00:04 2021 +0200

    Re #8708 - Changed the generate_pkcs_identity method of the CertificateService in such way that the Certificate class instance is now passed instead of a certificate ID resulting in a decrease of SQL queries.

diff --git a/src/controllers/certificates_controller.py b/src/controllers/certificates_controller.py
index 8763bd3..4e5bb76 100644
--- a/src/controllers/certificates_controller.py
+++ b/src/controllers/certificates_controller.py
@@ -649,7 +649,7 @@ class CertController:
                     return E_NO_CERTIFICATES_FOUND, C_INTERNAL_SERVER_ERROR
                 else:
                     # generate PKCS12 identity
-                    identity_byte_array = self.certificate_service.generate_pkcs_identity(cert.certificate_id, key,
+                    identity_byte_array = self.certificate_service.generate_pkcs_identity(cert, key,
                                                                                           identity_name,
                                                                                           identity_password)
                     return Response(identity_byte_array, mimetype='application/x-pkcs12')
diff --git a/src/services/certificate_service.py b/src/services/certificate_service.py
index e5c61cb..bac2cbb 100644
--- a/src/services/certificate_service.py
+++ b/src/services/certificate_service.py
@@ -477,12 +477,12 @@ class CertificateService:
 
         return self.configuration.base_server_url + "/api/ocsp/" + str(ca_identifier)
 
-    def generate_pkcs_identity(self, cert_id: int, cert_key: PrivateKey, identity_name: str, identity_passphrase: str):
+    def generate_pkcs_identity(self, certificate: Certificate, cert_key: PrivateKey, identity_name: str, identity_passphrase: str):
         """
         Generates a PKCS identity of the certificate given by the specified ID while using the private key passed.
         A name of the identity to be used and certificate's passphrase have to be specified as well as the passphrase
         of certificate's private key (if encrypted).
-        :param cert_id: ID of the certificate to be put into the PKCS identity store
+        :param certificate: certificate to be put into the PKCS identity store
         :param cert_key: key used to sign the given certificate
         :param identity_name: name to be given to the identity to be created
         :param identity_passphrase: passphrase to be used to encrypt the identity
@@ -490,15 +490,9 @@ class CertificateService:
         """
         Logger.debug("Function launched.")
 
-        # Read the selected certificate from the repository
-        certificate = self.certificate_repository.read(cert_id)
-        if certificate is None:
-            Logger.error("Certificate whose identity should be generated does not exist.")
-            raise CertificateNotFoundException(cert_id)
-
         # get the chain of trust of the certificate whose identity should be generated and exclude the certificate
         # whose chain of trust we are querying
-        cot_pem_list = [cert.pem_data for cert in self.get_chain_of_trust(cert_id, exclude_root=False)[1:]]
+        cot_pem_list = [cert.pem_data for cert in self.get_chain_of_trust(certificate.certificate_id, exclude_root=False)[1:]]
 
         return self.cryptography_service.generate_pkcs_identity(certificate.pem_data, cert_key.private_key,
                                                                 identity_name,
diff --git a/tests/integration_tests/rest_api/certificates_test.py b/tests/integration_tests/rest_api/certificates_test.py
index 09e7423..789c850 100644
--- a/tests/integration_tests/rest_api/certificates_test.py
+++ b/tests/integration_tests/rest_api/certificates_test.py
@@ -985,8 +985,8 @@ def test_generate_identity_non_well_formed(server):
     assert identity_ret.status_code == 400
     assert identity_ret.json == E_IDENTITY_NAME_NOT_SPECIFIED
 
-    # certificate not existing
-    identity_ret = server.post(f"/api/certificates/0/identity", content_type="application/json",
+    # certificate not existing (incorrect ID)
+    identity_ret = server.post(f"/api/certificates/999/identity", content_type="application/json",
                                json={"password": "foopass", "name": "Foo"})
     assert identity_ret.status_code == 404
     assert identity_ret.json == E_NO_CERTIFICATES_FOUND
diff --git a/tests/integration_tests/services/certificate_service_test.py b/tests/integration_tests/services/certificate_service_test.py
index fae56c3..c597a0a 100644
--- a/tests/integration_tests/services/certificate_service_test.py
+++ b/tests/integration_tests/services/certificate_service_test.py
@@ -418,7 +418,7 @@ def test_create_pkcs_identity(certificate_service_unique, private_key_service_un
                                                             intermediate_ca_key, usages={SSL_ID: True})
 
     # generate the identity
-    pkcs = certificate_service_unique.generate_pkcs_identity(child_cert.certificate_id, child_key, "Foo Child's Identity", "pass")
+    pkcs = certificate_service_unique.generate_pkcs_identity(child_cert, child_key, "Foo Child's Identity", "pass")
 
     # print out the pkcs store in order to be able to check it
     pkcs_info = subprocess.check_output(
@@ -440,7 +440,7 @@ def test_create_pkcs_identity_empty_chain_of_trust(certificate_service_unique, p
                                                                      organization_unit="Department of Foo"))
 
     # generate the identity
-    pkcs = certificate_service_unique.generate_pkcs_identity(root_ca_cert.certificate_id, root_ca_private_key, "Foo Child's Identity", "pass")
+    pkcs = certificate_service_unique.generate_pkcs_identity(root_ca_cert, root_ca_private_key, "Foo Child's Identity", "pass")
 
     # print out the pkcs store in order to be able to check it
     pkcs_info = subprocess.check_output(
@@ -452,8 +452,3 @@ def test_create_pkcs_identity_empty_chain_of_trust(certificate_service_unique, p
     assert root_ca_cert.pem_data in pkcs_info
     assert len(re.findall("BEGIN CERTIFICATE", pkcs_info)) == 1
     assert len(re.findall("END CERTIFICATE", pkcs_info)) == 1
-
-
-def test_create_pkcs_identity_incorrect_id(certificate_service_unique):
-    with pytest.raises(CertificateNotFoundException):
-        certificate_service_unique.generate_pkcs_identity(999, None, None, None)
