Revize d53c2fdc
Přidáno uživatelem Michal Seják před více než 3 roky(ů)
app.py | ||
---|---|---|
30 | 30 |
return CertController.get_certificate_details_by_id(id) |
31 | 31 |
|
32 | 32 |
|
33 |
@app.route('/api/certificates/<id>/root', methods=["GET"]) |
|
34 |
def get_cert_root(id): |
|
35 |
return CertController.get_certificate_root_by_id(id) |
|
36 |
|
|
37 |
|
|
33 | 38 |
if __name__ == '__main__': |
34 | 39 |
host = "0.0.0.0" |
35 | 40 |
port = 5000 |
src/controllers/certificates_controller.py | ||
---|---|---|
29 | 29 |
ID = "id" |
30 | 30 |
E_NO_ISSUER_FOUND = {"success": False, |
31 | 31 |
"data": "No certificate authority with such unique ID exists."} |
32 |
E_NO_CERTIFICATES_FOUND = {"success": False, "data": "No certificates found."}
|
|
32 |
E_NO_CERTIFICATES_FOUND = {"success": False, "data": "No such certificate found."}
|
|
33 | 33 |
E_NOT_JSON_FORMAT = {"success": False, "data": "The request must be JSON-formatted."} |
34 | 34 |
E_CORRUPTED_DATABASE = {"success": False, "data": "Internal server error (corrupted database)."} |
35 | 35 |
E_GENERAL_ERROR = {"success": False, "data": "Internal server error (unknown origin)."} |
... | ... | |
250 | 250 |
) |
251 | 251 |
return {"success": True, "data": ret} |
252 | 252 |
|
253 |
|
|
254 | 253 |
@staticmethod |
255 | 254 |
def get_certificate_root_by_id(id): # noqa: E501 |
256 | 255 |
"""get certificate's root of trust chain by ID |
... | ... | |
264 | 263 |
""" |
265 | 264 |
CertController.setup() # TODO remove after issue fixed |
266 | 265 |
|
267 |
if connexion.request.is_json: |
|
268 |
id = IdParameter.from_dict(connexion.request.get_json()) # noqa: E501 |
|
269 |
return 'do some magic!' |
|
266 |
try: |
|
267 |
v = int(id) |
|
268 |
except ValueError: |
|
269 |
return E_WRONG_PARAMETERS, 400 |
|
270 |
|
|
271 |
cert = CERTIFICATE_SERVICE.get_certificate(v) |
|
272 |
|
|
273 |
if cert is None: |
|
274 |
return E_NO_CERTIFICATES_FOUND, 205 # TODO related to 204 issue |
|
275 |
|
|
276 |
while cert.parent_id != cert.certificate_id: |
|
277 |
cert = CERTIFICATE_SERVICE.get_certificate(cert.parent_id) |
|
278 |
if cert is None: |
|
279 |
return E_CORRUPTED_DATABASE, 500 |
|
280 |
|
|
281 |
return {"success": True, "data": cert.pem_data} |
|
270 | 282 |
|
271 | 283 |
@staticmethod |
272 | 284 |
def get_certificate_trust_chain_by_id(id): # noqa: E501 |
tests/unit_tests/rest_api/certificates.py | ||
---|---|---|
151 | 151 |
assert not d["success"] |
152 | 152 |
|
153 | 153 |
|
154 |
def test_root_inter_ca(server):
|
|
154 |
def test_create_chain(server):
|
|
155 | 155 |
ret = server.post("/api/certificates", content_type="application/json", json={ |
156 | 156 |
"subject": { |
157 | 157 |
"C": "CZ", |
... | ... | |
209 | 209 |
assert "success" in d |
210 | 210 |
assert d["success"] |
211 | 211 |
|
212 |
ret = server.post("/api/certificates", content_type="application/json", json={ |
|
213 |
"CA": 3, |
|
214 |
"subject": { |
|
215 |
"C": "CZ", |
|
216 |
"CN": "Legit end certificate s.r.o.", |
|
217 |
"L": "Pilsen", |
|
218 |
"O": "I swear s.r.o.", |
|
219 |
"OU": "IT department", |
|
220 |
"ST": "Pilsen Region", |
|
221 |
"emailAddress": "end@ca.com" |
|
222 |
}, |
|
223 |
"usage": { |
|
224 |
"CA": True, |
|
225 |
"SSL": True, |
|
226 |
"authentication": True, |
|
227 |
"digitalSignature": True |
|
228 |
}, |
|
229 |
"validityDays": 30 |
|
230 |
}) |
|
231 |
|
|
232 |
assert ret.status_code == 201 |
|
233 |
|
|
234 |
d = ret.json |
|
235 |
|
|
236 |
assert "data" in d |
|
237 |
assert d["data"] == 4 |
|
238 |
assert "success" in d |
|
239 |
assert d["success"] |
|
240 |
|
|
212 | 241 |
|
213 | 242 |
def test_list_of_certificates(server): |
214 | 243 |
ret = server.get("/api/certificates") |
... | ... | |
270 | 299 |
d = ret.json |
271 | 300 |
|
272 | 301 |
assert "data" in d |
273 |
assert d["data"] == 4
|
|
302 |
assert d["data"] == 5
|
|
274 | 303 |
assert "success" in d |
275 | 304 |
assert d["success"] |
276 | 305 |
|
277 | 306 |
ret = server.post("/api/certificates", content_type="application/json", json={ |
278 |
"CA": 2,
|
|
307 |
"CA": 5,
|
|
279 | 308 |
"subject": { |
280 | 309 |
"C": "CZ", |
281 | 310 |
"CN": "End certificate signed by end certificate s.r.o.", |
... | ... | |
300 | 329 |
d = ret.json |
301 | 330 |
|
302 | 331 |
assert "data" in d |
303 |
assert d["data"] == 5
|
|
332 |
assert d["data"] == 6
|
|
304 | 333 |
assert "success" in d |
305 | 334 |
assert d["success"] |
306 | 335 |
|
... | ... | |
370 | 399 |
assert "data" in ret.json |
371 | 400 |
assert "success" in ret.json |
372 | 401 |
assert not ret.json["success"] |
373 |
assert ret.json["data"] == "No certificates found."
|
|
402 |
assert ret.json["data"] == "No such certificate found."
|
|
374 | 403 |
|
375 | 404 |
|
376 | 405 |
def test_get_detes(server): |
... | ... | |
403 | 432 |
|
404 | 433 |
d = ret.json |
405 | 434 |
|
406 |
assert d["data"] == 6
|
|
435 |
assert d["data"] == 7
|
|
407 | 436 |
|
408 | 437 |
id = d["data"] |
409 | 438 |
|
... | ... | |
421 | 450 |
|
422 | 451 |
assert original["CA"] == new["CA"] |
423 | 452 |
assert original["subject"] == new["subject"] |
424 |
assert original["usage"] == new["usage"] |
|
453 |
assert original["usage"] == new["usage"] |
|
454 |
|
|
455 |
|
|
456 |
def test_get_root(server): |
|
457 |
ret = server.get("/api/certificates/2") |
|
458 |
|
|
459 |
assert ret.status_code == 200 |
|
460 |
|
|
461 |
assert "data" in ret.json |
|
462 |
assert "success" in ret.json |
|
463 |
assert ret.json["success"] |
|
464 |
|
|
465 |
original = ret.json["data"] |
|
466 |
|
|
467 |
ret = server.get("/api/certificates/4/root") |
|
468 |
|
|
469 |
assert ret.status_code == 200 |
|
470 |
|
|
471 |
assert "data" in ret.json |
|
472 |
assert "success" in ret.json |
|
473 |
assert ret.json["success"] |
|
474 |
|
|
475 |
new = ret.json["data"] |
|
476 |
|
|
477 |
assert original == new |
|
478 |
|
|
479 |
|
|
480 |
def test_get_root_invalid(server): |
|
481 |
ret = server.get("/api/certificates/805/root") |
|
482 |
|
|
483 |
assert ret.status_code == 205 |
|
484 |
assert "data" in ret.json |
|
485 |
assert "success" in ret.json |
|
486 |
assert not ret.json["success"] |
|
487 |
assert ret.json["data"] == "No such certificate found." |
|
488 |
|
Také k dispozici: Unified diff
Re #8476 - Implemented and tested `get_cert_root(id)`.