Projekt

Obecné

Profil

« Předchozí | Další » 

Revize ce8b9aaf

Přidáno uživatelem Stanislav Král před asi 4 roky(ů)

Re #8573 - Implemented get_private_key_of_a_certificate method in CertController and added /api/certificates/<id>/private_key endpoint

Zobrazit rozdíly:

app.py
46 46
def get_cert_chain(id, certificate_controller: CertController):
47 47
    return certificate_controller.get_certificate_trust_chain_by_id(id)
48 48

  
49
@app.route('/api/certificates/<id>/private_key', methods=["GET"])
50
def get_private_key_of_a_certificate(id, certificate_controller: CertController):
51
    return certificate_controller.get_private_key_of_a_certificate(id)
49 52

  
50 53
@app.route('/api/certificates/<id>/public_key', methods=["GET"])
51 54
def get_public_key_of_a_certificate(id, certificate_controller: CertController):
src/controllers/certificates_controller.py
345 345
            CA: c_issuer.certificate_id
346 346
        }
347 347

  
348
    def get_public_key_of_a_certificate(self, id):
348
    def get_private_key_of_a_certificate(self, id):
349 349
        """
350 350
        Get a private key used to sign a certificate in PEM format specified by certificate's ID
351 351

  
352
        :param id: ID of a certificate whose private key is to be queried
353
        :type id: dict | bytes
354

  
355
        :rtype: PemResponse
356
        """
357

  
358
        # try to parse the supplied ID
359
        try:
360
            v = int(id)
361
        except ValueError:
362
            return E_WRONG_PARAMETERS, C_BAD_REQUEST
363

  
364
        # find a certificate with using the given ID
365
        cert = self.certificate_service.get_certificate(v)
366

  
367
        if cert is None:
368
            return E_NO_CERTIFICATES_FOUND, C_NOT_FOUND
369
        else:
370
            # certificate exists, fetch it's private key
371
            private_key = self.key_service.get_key(cert.private_key_id)
372
            if cert is None:
373
                return E_NO_CERT_PRIVATE_KEY_FOUND, C_INTERNAL_SERVER_ERROR
374
            else:
375
                return {"success": True, "data": private_key.private_key}, C_SUCCESS
376

  
377
    def get_public_key_of_a_certificate(self, id):
378
        """
379
        Get a public key of a certificate in PEM format specified by certificate's ID
380

  
352 381
        :param id: ID of a certificate whose public key is to be queried
353 382
        :type id: dict | bytes
354 383

  
tests/integration_tests/rest_api/certificates_test.py
545 545
    assert "".join(expected) == actual
546 546

  
547 547

  
548
def test_get_cert_private_key(server):
549
    for i in range(6, 2):
550
        ret = server.get(f"/api/certificates/{i}/private_key")
551

  
552
        assert ret.status_code == 200
553

  
554
        assert "data" in ret.json
555
        assert "success" in ret.json
556
        assert ret.json["success"]
557
        assert "-----BEGIN RSA PRIVATE KEY-----" in ret.json["data"]
558

  
559

  
560
def test_get_cert_private_key_incorrect_id(server):
561
    for i in range(111, 222, 333):
562
        ret = server.get(f"/api/certificates/{i}/private_key")
563

  
564
        assert ret.status_code == 404
565

  
566
        assert "data" in ret.json
567
        assert "success" in ret.json
568
        assert not ret.json["success"]
569
        assert "No such certificate found." == ret.json["data"]
570

  
571

  
548 572
def test_get_cert_public_key(server):
549 573
    for i in range(6, 2):
550 574
        ret = server.get(f"/api/certificates/{i}/public_key")

Také k dispozici: Unified diff