commit ca3ac7c043e34648121cb1e002a5e9327f5464a3
Author: Stanislav Král <stanislav.kral3@gmail.com>
Date:   Mon Apr 5 20:47:37 2021 +0200

    Re #8472 - Added the ability to pass usage dictionary to create_ca and create_root_ca methods via optional parameter

diff --git a/src/services/certificate_service.py b/src/services/certificate_service.py
index 0854702..111dffc 100644
--- a/src/services/certificate_service.py
+++ b/src/services/certificate_service.py
@@ -1,4 +1,4 @@
-from src.constants import ROOT_CA_ID, INTERMEDIATE_CA_ID
+from src.constants import ROOT_CA_ID, INTERMEDIATE_CA_ID, CA_ID
 from src.dao.certificate_repository import CertificateRepository
 from src.model.certificate import Certificate
 from src.model.private_key import PrivateKey
@@ -18,7 +18,11 @@ class CertificateService:
         self.certificate_repository = certificate_repository
 
     # TODO usages present in method parameters but not in class diagram
-    def create_root_ca(self, key: PrivateKey, subject: Subject, extensions: str = "", config: str = ""):
+    def create_root_ca(self, key: PrivateKey, subject: Subject, extensions: str = "", config: str = "",
+                       usages=None):
+        if usages is None:
+            usages = {}
+
         # create a new self signed  certificate
         cert_pem = self.cryptography_service.create_sscrt(subject, key.private_key, key_pass=key.password,
                                                           extensions=extensions, config=config)
@@ -30,9 +34,12 @@ class CertificateService:
         not_before_formatted = time.strftime(DATE_FORMAT, not_before)
         not_after_formatted = time.strftime(DATE_FORMAT, not_after)
 
+        # specify CA usage
+        usages[CA_ID] = True
+
         # create a certificate wrapper
         certificate = Certificate(-1, subject.common_name, not_before_formatted, not_after_formatted, cert_pem,
-                                  key.private_key_id, ROOT_CA_ID, 0, {})
+                                  key.private_key_id, ROOT_CA_ID, 0, usages)
 
         # store the wrapper into the repository
         created_id = self.certificate_repository.create(certificate)
@@ -44,7 +51,10 @@ 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):
+                  extensions: str = "", days: int = 30, usages=None):
+        if usages is None:
+            usages = {}
+
         extensions = extensions + "\n" + CA_EXTENSIONS
         # TODO implement AIA URI via extensions
         cert_pem = self.cryptography_service.create_crt(subject, subject_key.private_key, issuer_cert.pem_data,
@@ -60,9 +70,12 @@ class CertificateService:
         not_before_formatted = time.strftime(DATE_FORMAT, not_before)
         not_after_formatted = time.strftime(DATE_FORMAT, not_after)
 
+        # specify CA usage
+        usages[CA_ID] = True
+
         # create a certificate wrapper
         certificate = Certificate(-1, subject.common_name, not_before_formatted, not_after_formatted, cert_pem,
-                                  subject_key.private_key_id, INTERMEDIATE_CA_ID, 0, {})
+                                  subject_key.private_key_id, INTERMEDIATE_CA_ID, issuer_cert.certificate_id, usages)
 
         # store the wrapper into the repository
         created_id = self.certificate_repository.create(certificate)
diff --git a/tests/integration_tests/services/certificate_key_service_test.py b/tests/integration_tests/services/certificate_key_service_test.py
index f06a325..8daec91 100644
--- a/tests/integration_tests/services/certificate_key_service_test.py
+++ b/tests/integration_tests/services/certificate_key_service_test.py
@@ -1,5 +1,6 @@
 import subprocess
 
+from src.constants import SSL_ID, CA_ID
 from src.model.subject import Subject
 
 
@@ -21,7 +22,11 @@ def test_create_and_get_root_ca(private_key_service, certificate_service):
     private_key = private_key_service.create_new_key(passphrase="foobar")
 
     cert = certificate_service.create_root_ca(private_key,
-                                              Subject(common_name="FooName", organization_unit="Department of Foo"))
+                                              Subject(common_name="FooName", organization_unit="Department of Foo"),
+                                              usages={SSL_ID: True})
+
+    assert CA_ID in cert.usages
+    assert SSL_ID in cert.usages
 
     cert_loaded = certificate_service.get_certificate(cert.certificate_id)
 
@@ -49,7 +54,10 @@ def test_create_and_get_cert(private_key_service, certificate_service):
                                                  Subject(common_name="RootFoo", organization_unit="Department of Foo"))
 
     inter_cert = certificate_service.create_ca(inter_ca_private_key, Subject(common_name="Intermediate CA"), root_ca,
-                                             root_ca_private_key)
+                                               root_ca_private_key, usages={SSL_ID: True})
+
+    assert CA_ID in inter_cert.usages
+    assert SSL_ID in inter_cert.usages
 
     inter_cert_loaded = certificate_service.get_certificate(inter_cert.certificate_id)
 
