Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 80f30a68

Přidáno uživatelem David Friesecký před téměř 4 roky(ů)

Re #8589 - Implemented get_root(unique_id) in CertificateService

Zobrazit rozdíly:

src/services/certificate_service.py
569 569
                                                                identity_name,
570 570
                                                                identity_passphrase, cot_pem_list, cert_key.password)
571 571

  
572
    def get_root(self, unique_id: int):
573
        """
574
        Function that calls CertificateService.get_chain_of_trust() and extract root CA from the returned chain.
575
        :param unique_id: ID of the certificate to which to find the root CA
576
        :return: Instance of the Certificate class containing a root certificate which was found in the chain
577
        """
578
        Logger.debug("Function launched.")
579

  
580
        chain_of_trust = self.get_chain_of_trust(from_id=unique_id, exclude_root=False)
581

  
582
        if len(chain_of_trust) == 0:
583
            Logger.error(f"No such certificate found 'ID = {unique_id}'.")
584
            raise CertificateNotFoundException(unique_id)
585

  
586
        root_ca = chain_of_trust[len(chain_of_trust) - 1]
587

  
588
        if root_ca.type_id != ROOT_CA_ID or root_ca.certificate_id != root_ca.parent_id:
589
            Logger.error(f"Certificate id '{root_ca.certificate_id}' has not same parent_id '{root_ca.parent_id} "
590
                         f"or type_id '{root_ca.type_id}' is not a ROOT_CA_ID '{ROOT_CA_ID}'")
591
            raise InvalidRootCA(root_ca.type_id, root_ca.certificate_id, root_ca.parent_id)
592

  
593
        return root_ca
594

  
572 595

  
573 596
class RevocationReasonInvalidException(Exception):
574 597
    """
......
638 661

  
639 662
    def __str__(self):
640 663
        return f"""Subject attribute "{self.attribute_name}" is invalid (reason: {self.reason})."""
664

  
665

  
666
class InvalidRootCA(Exception):
667
    """
668
    Exception that denotes that certificate has invalid root CA parameters.
669
    """
670

  
671
    def __init__(self, type_id, id, parent_id):
672
        self.type_id = type_id
673
        self.id = id
674
        self.parent_id = parent_id
675

  
676
    def __str__(self):
677
        return f"Certificate id '{self.id}' has not same parent_id '{self.parent_id} " \
678
               f"or type_id '{self.type_id}' is not a ROOT_CA_ID '{ROOT_CA_ID}'"
tests/integration_tests/services/certificate_service_test.py
513 513
    assert len(re.findall("END CERTIFICATE", pkcs_info)) == 1
514 514

  
515 515

  
516
def test_get_root(certificate_service_unique, private_key_service):
517
    root_ca_private_key_1 = private_key_service.create_new_key()
518
    root_ca_private_key_2 = private_key_service.create_new_key()
519
    inter_ca_private_key = private_key_service.create_new_key()
520
    end_cert_private_key = private_key_service.create_new_key()
521

  
522
    root_cert_1 = certificate_service_unique.create_root_ca(root_ca_private_key_1,
523
                                                            Subject(common_name="RootFoo1",
524
                                                                    organization_unit="Department of Foo"))
525

  
526
    inter_cert = certificate_service_unique.create_ca(inter_ca_private_key, Subject(common_name="Intermediate CA"),
527
                                                      root_cert_1, root_ca_private_key_1, usages={SSL_ID: True})
528

  
529
    end_cert = certificate_service_unique.create_end_cert(end_cert_private_key,
530
                                                          Subject("Foo Child", email_address="foo@bar.cz"), inter_cert,
531
                                                          inter_ca_private_key, usages={AUTHENTICATION_ID: True})
532

  
533
    root_cert_2 = certificate_service_unique.create_root_ca(root_ca_private_key_2,
534
                                                            Subject(common_name="RootFoo2",
535
                                                                    organization_unit="Department of Foo"))
536

  
537
    test_root = certificate_service_unique.get_root(unique_id=root_cert_1.certificate_id)
538
    assert test_root.certificate_id == root_cert_1.certificate_id
539

  
540
    test_root = certificate_service_unique.get_root(unique_id=inter_cert.certificate_id)
541
    assert test_root.certificate_id == root_cert_1.certificate_id
542

  
543
    test_root = certificate_service_unique.get_root(unique_id=end_cert.certificate_id)
544
    assert test_root.certificate_id == root_cert_1.certificate_id
545

  
546
    test_root = certificate_service_unique.get_root(unique_id=root_cert_2.certificate_id)
547
    assert test_root.certificate_id == root_cert_2.certificate_id
548

  
549

  
516 550
@pytest.mark.parametrize(
517 551
    "cc",
518 552
    [

Také k dispozici: Unified diff