Revize cfda1725
Přidáno uživatelem Stanislav Král před asi 4 roky(ů)
app.py | ||
---|---|---|
47 | 47 |
return certificate_controller.get_certificate_trust_chain_by_id(id) |
48 | 48 |
|
49 | 49 |
|
50 |
@app.route('/api/certificates/<id>/public_key', methods=["GET"]) |
|
51 |
def get_public_key_of_a_certificate(id, certificate_controller: CertController): |
|
52 |
return certificate_controller.get_public_key_of_a_certificate(id) |
|
53 |
|
|
50 | 54 |
def initialize_app(application) -> bool: |
51 | 55 |
""" |
52 | 56 |
Initializes the application |
src/controllers/certificates_controller.py | ||
---|---|---|
30 | 30 |
|
31 | 31 |
E_NO_ISSUER_FOUND = {"success": False, "data": "No certificate authority with such unique ID exists."} |
32 | 32 |
E_NO_CERTIFICATES_FOUND = {"success": False, "data": "No such certificate found."} |
33 |
E_NO_CERT_PRIVATE_KEY_FOUND = {"success": False, |
|
34 |
"data": "Internal server error (certificate's private key cannot be found)."} |
|
33 | 35 |
E_NOT_JSON_FORMAT = {"success": False, "data": "The request must be JSON-formatted."} |
34 | 36 |
E_CORRUPTED_DATABASE = {"success": False, "data": "Internal server error (corrupted database)."} |
35 | 37 |
E_GENERAL_ERROR = {"success": False, "data": "Internal server error (unknown origin)."} |
... | ... | |
38 | 40 |
|
39 | 41 |
C_CREATED_SUCCESSFULLY = 201 |
40 | 42 |
C_BAD_REQUEST = 400 |
41 |
C_NO_DATA = 205 # TODO related to 204 issue |
|
43 |
C_NOT_FOUND = 404 |
|
44 |
C_NO_DATA = 205 # TODO related to 204 issue |
|
42 | 45 |
C_INTERNAL_SERVER_ERROR = 500 |
43 | 46 |
C_SUCCESS = 200 |
44 | 47 |
|
... | ... | |
341 | 344 |
USAGE: {CertController.INVERSE_KEY_MAP[k]: v for k, v in c.usages.items()}, |
342 | 345 |
CA: c_issuer.certificate_id |
343 | 346 |
} |
347 |
|
|
348 |
def get_public_key_of_a_certificate(self, id): |
|
349 |
""" |
|
350 |
Get a private key used to sign a certificate in PEM format specified by certificate's ID |
|
351 |
|
|
352 |
:param id: ID of a certificate whose public 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 |
# TODO public key can be extracted from a certificate |
|
376 |
# private key fetched, extract a public key from it |
|
377 |
public_key = self.key_service.get_public_key(private_key) |
|
378 |
return {"success": True, "data": public_key}, C_SUCCESS |
tests/integration_tests/rest_api/certificates_test.py | ||
---|---|---|
542 | 542 |
|
543 | 543 |
actual = ret.json["data"] |
544 | 544 |
|
545 |
assert "".join(expected) == actual |
|
545 |
assert "".join(expected) == actual |
|
546 |
|
|
547 |
|
|
548 |
def test_get_cert_public_key(server): |
|
549 |
for i in range(6, 2): |
|
550 |
ret = server.get(f"/api/certificates/{i}/public_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 PUBLIC KEY-----" in ret.json["data"] |
|
558 |
|
|
559 |
|
|
560 |
def test_get_cert_public_key_incorrect_id(server): |
|
561 |
for i in range(111, 222, 333): |
|
562 |
ret = server.get(f"/api/certificates/{i}/public_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"] |
Také k dispozici: Unified diff
Re #8573 - Implemented get_public_key_of_a_certificate method in CertController and added /api/certificates/<id>/public_key endpoint