commit a6727aa94deee91907246ec2c39ef931f2c2dc2d
Author: Stanislav Král <stanislav.kral3@gmail.com>
Date:   Tue Apr 6 00:41:12 2021 +0200

    Re #8472 - Added missing docstrings to KeyService and CertificateService classes

diff --git a/src/services/certificate_service.py b/src/services/certificate_service.py
index c9c29d0..9559ffe 100644
--- a/src/services/certificate_service.py
+++ b/src/services/certificate_service.py
@@ -22,6 +22,15 @@ class CertificateService:
     # TODO usages present in method parameters but not in class diagram
     def create_root_ca(self, key: PrivateKey, subject: Subject, extensions: str = "", config: str = "",
                        usages=None):
+        """
+        Creates a root CA certificate based on the given parameters.
+        :param key: Private key to be used when generating the certificate
+        :param subject: Subject to be used put into the certificate
+        :param config: String containing the configuration to be used
+        :param extensions: Name of the section in the configuration representing extensions
+        :param usages: A dictionary containing usages of the certificate to be generated (see constants.py)
+        :return: An instance of Certificate class representing the generated root CA cert
+        """
         if usages is None:
             usages = {}
 
@@ -32,7 +41,7 @@ class CertificateService:
         usages[CA_ID] = True
 
         # wrap into Certificate class
-        certificate = self.__create_wrapper(cert_pem, key.private_key_id, subject.common_name, usages, 0,
+        certificate = self.__create_wrapper(cert_pem, key.private_key_id, usages, 0,
                                             ROOT_CA_ID)
 
         # store the wrapper into the repository
@@ -43,15 +52,26 @@ class CertificateService:
 
         return certificate
 
-    def __create_wrapper(self, cert_pem, private_key_id, common_name, usages, parent_id, cert_type):
+    def __create_wrapper(self, cert_pem, private_key_id, usages, parent_id, cert_type):
+        """
+        Wraps the given parameters using hte Certificate class. Uses CryptographyService to find out the notBefore and
+        notAfter fields.
+        :param cert_pem: PEM of the cert. to be wrapped
+        :param private_key_id: ID of the private key used to create the given certificate
+        :param usages: A dictionary containing usages of the generated certificate generated (see constants.py)
+        :param parent_id: ID of the CA that issued this certificate
+        :param cert_type: Type of this certificate (see constants.py)
+        :return: An instance of the Certificate class wrapping the values passed  via method parameters
+        """
         # parse the generated pem for subject and notBefore/notAfter fields
+        # TODO this could be improved in the future in such way that calling openssl is not required to parse the dates
         subj, not_before, not_after = self.cryptography_service.parse_cert_pem(cert_pem)
         # format the parsed date
         not_before_formatted = time.strftime(DATE_FORMAT, not_before)
         not_after_formatted = time.strftime(DATE_FORMAT, not_after)
 
         # create a certificate wrapper
-        certificate = Certificate(-1, common_name, not_before_formatted, not_after_formatted, cert_pem,
+        certificate = Certificate(-1, subj.common_name, not_before_formatted, not_after_formatted, cert_pem,
                                   private_key_id, cert_type, parent_id, usages)
 
         return certificate
@@ -59,6 +79,17 @@ class CertificateService:
     # TODO config parameter present in class diagram but not here (unused)
     def create_ca(self, subject_key: PrivateKey, subject: Subject, issuer_cert: Certificate, issuer_key: PrivateKey,
                   extensions: str = "", days: int = 30, usages=None):
+        """
+        Creates an intermediate CA certificate issued by the given parent CA.
+        :param subject_key: Private key to be used when generating the certificate
+        :param subject: Subject to be used put into the certificate
+        :param issuer_cert: Issuer certificate that will sign the CSR required to create an intermediate CA
+        :param issuer_key: PK used to generate the issuer certificate
+        :param extensions: Extensions to be used when generating the certificate
+        :param usages: A dictionary containing usages of the certificate to be generated (see constants.py)
+        :param days: Number of days for which the generated cert. will be considered valid
+        :return: An instance of Certificate class representing the generated intermediate CA cert
+        """
         if usages is None:
             usages = {}
 
@@ -74,7 +105,7 @@ class CertificateService:
         usages[CA_ID] = True
 
         # wrap into Certificate class
-        self.__create_wrapper(cert_pem, subject_key.private_key_id, subject.common_name, usages,
+        self.__create_wrapper(cert_pem, subject_key.private_key_id, usages,
                               issuer_cert.certificate_id, INTERMEDIATE_CA_ID)
 
         # parse the generated pem for subject and notBefore/notAfter fields
@@ -102,6 +133,17 @@ class CertificateService:
     def create_end_cert(self, subject_key: PrivateKey, subject: Subject, issuer_cert: Certificate,
                         issuer_key: PrivateKey,
                         extensions: str = "", days: int = 30, usages=None):
+        """
+        Creates an end certificate issued by the given parent CA.
+        :param subject_key: Private key to be used when generating the certificate
+        :param subject: Subject to be used put into the certificate
+        :param issuer_cert: Issuer certificate that will sign the CSR required to create an intermediate CA
+        :param issuer_key: PK used to generate the issuer certificate
+        :param extensions: Extensions to be used when generating the certificate
+        :param usages: A dictionary containing usages of the certificate to be generated (see constants.py)
+        :param days: Number of days for which the generated cert. will be considered valid
+        :return: An instance of Certificate class representing the generated cert
+        """
         if usages is None:
             usages = {}
 
@@ -113,7 +155,7 @@ class CertificateService:
                                                         days=days)
 
         # wrap the generated certificate using Certificate class
-        certificate = self.__create_wrapper(cert_pem, subject_key.private_key_id, subject.common_name, usages,
+        certificate = self.__create_wrapper(cert_pem, subject_key.private_key_id, usages,
                                             issuer_cert.certificate_id, CERTIFICATE_ID)
 
         created_id = self.certificate_repository.create(certificate)
@@ -123,9 +165,22 @@ class CertificateService:
         return certificate
 
     def get_certificate(self, unique_id: int) -> Certificate:
+        """
+        Tries to fetch a certificate from the certificate repository using a given id.
+        :param unique_id: ID of the certificate to be fetched
+        :return: Instance of the Certificate class containing a certificate with the given id or `None` if such
+        certificate is not found
+        """
         return self.certificate_repository.read(unique_id)
 
     def get_certificates(self, cert_type=None) -> List[Certificate]:
+        """
+        Tries to fetch a list of all certificates from the certificate repository. Using the `cert_type` parameter only
+        certificates of the given type can be returned.
+        :param cert_type: Type of certificates to be returned
+        :return: List of instances of the Certificate class representing all certificates present in the certificate
+        repository. An empty list is returned when no certificates are found.
+        """
         return self.certificate_repository.read_all(cert_type)
 
     def get_chain_of_trust(self, from_id: int, to_id: int = -1, exclude_root=True) -> List[Certificate]:
@@ -178,13 +233,13 @@ class CertificateService:
 
         return chain_of_trust
 
-    def delete_certificate(self, unique_id):
+    def delete_certificate(self, unique_id) -> bool:
         """
         Deletes a certificate
 
         :param unique_id: ID of specific certificate
 
-        :return: the result of whether the deletion was successful
+        :return: `True` when the deletion was successful. `False` in other case
         """
         # TODO delete children?
         return self.certificate_repository.delete(unique_id)
diff --git a/src/services/cryptography.py b/src/services/cryptography.py
index 3b43988..635c277 100644
--- a/src/services/cryptography.py
+++ b/src/services/cryptography.py
@@ -190,7 +190,6 @@ class CryptographyService:
         :param subject_key: string containing the private key to be used when creating the certificate in PEM format
         :param issuer_key: string containing the private key of the issuer's certificate in PEM format
         :param issuer_pem: string containing the certificate of the issuer that will sign this CSR in PEM format
-        :param issuer_key: string containing the private key of the issuer's certificate in PEM format
         :param subject_key_pass: string containing the passphrase of the private key used when creating the certificate
         in PEM format
         :param issuer_key_pass: string containing the passphrase of the private key of the issuer's certificate in PEM
diff --git a/src/services/key_service.py b/src/services/key_service.py
index cfcdf52..1b81239 100644
--- a/src/services/key_service.py
+++ b/src/services/key_service.py
@@ -9,7 +9,12 @@ class KeyService:
         self.cryptography_service = cryptography_service
         self.private_key_repository = private_key_repository
 
-    def create_new_key(self, passphrase=""):
+    def create_new_key(self, passphrase="") -> PrivateKey:
+        """
+        Creates a new private key using the given passphrase.
+        :param passphrase: Passphrase to be used when encrypting the PK
+        :return: An instance of the <PrivateKey> class representing the generated PK
+        """
         # generate a new private key
         private_key_pem = self.cryptography_service.create_private_key(passphrase)
 
@@ -25,9 +30,20 @@ class KeyService:
         return private_key
 
     def get_key(self, unique_id):
+        """
+        Tries to fetch a PK using the given ID.
+        :param unique_id: ID of the PK to be found
+        :return:An instance of the required PK or `None`
+        """
         return self.private_key_repository.read(unique_id)
 
     def get_keys(self, unique_ids=None):
+        """
+        Tries to fetch all PKs in the repository. Exact PKs to be fetched can be specified using the `unique_ids`
+        parameter. If `unique_ids` parameter is not passed then all PKs in the repository are returned.
+        :param unique_ids: An array containing IDs of PKs to be fetched from the repository.
+        :return: A list of instances of the PrivateKey class representing the PKs found
+        """
         if unique_ids is None:
             return self.private_key_repository.read_all()
         else:
@@ -35,4 +51,10 @@ class KeyService:
             return [self.private_key_repository.read(identifier) for identifier in unique_ids]
 
     def delete_key(self, unique_id):
+        """
+        Deletes a private key
+
+        :param unique_id: ID of specific certificate to be deleted
+        :return: `True` when the deletion was successful. `False` in other case
+        """
         return self.private_key_repository.delete(unique_id)
