|
9 | 9 | from typing import Any, Optional |
10 | 10 |
|
11 | 11 | from cenclave_lib_crypto.seal_box import seal, unseal |
12 | | -from fastapi import FastAPI, Response, Request |
| 12 | +from flask import Flask, Response, jsonify, request |
13 | 13 |
|
14 | 14 | import globs |
15 | 15 |
|
16 | | -app = FastAPI() |
| 16 | +app = Flask(__name__) |
17 | 17 |
|
18 | 18 | CONFIG = json.loads((Path(__file__).parent / "config.json").read_text(encoding="utf-8")) |
19 | 19 |
|
20 | 20 | ENCLAVE_SK: bytes = Path(os.environ["ENCLAVE_SK_PATH"]).read_bytes() |
21 | 21 |
|
22 | 22 |
|
23 | 23 | @app.get("/health") |
24 | | -async def health_check(request: Request) -> Response: |
| 24 | +def health_check() -> Response: |
25 | 25 | """Health check of the application.""" |
26 | | - print(request.scope) |
27 | | - if "tls" in request.scope["extensions"]: |
28 | | - client_cert = request.scope["extensions"]["tls"]["client_cert_chain"] |
29 | | - print("client_cert: %s", client_cert) |
30 | | - return Response(content="OK", status_code=HTTPStatus.OK) |
| 26 | + return Response(response="OK", status=HTTPStatus.OK) |
31 | 27 |
|
32 | 28 |
|
33 | | -# @app.post("/") |
34 | | -# def push(): |
35 | | -# """Add a number to the pool.""" |
36 | | -# content: Optional[Any] = request.get_json(silent=True) |
| 29 | +@app.post("/") |
| 30 | +def push() -> Response: |
| 31 | + """Add a number to the pool.""" |
| 32 | + content: Optional[Any] = request.get_json(silent=True) |
37 | 33 |
|
38 | | -# if content is None or not isinstance(content, dict): |
39 | | -# app.logger.error("TypeError with data: '%s'", content) |
40 | | -# return Response(status=HTTPStatus.UNPROCESSABLE_ENTITY) |
| 34 | + if content is None or not isinstance(content, dict): |
| 35 | + app.logger.error("TypeError with data: '%s'", content) |
| 36 | + return Response(status=HTTPStatus.UNPROCESSABLE_ENTITY) |
41 | 37 |
|
42 | | -# data: Optional[Any] = content.get("data") |
43 | | -# pk: Optional[str] = content.get("pk") |
| 38 | + data: Optional[Any] = content.get("data") |
| 39 | + pk: Optional[str] = content.get("pk") |
44 | 40 |
|
45 | | -# if data is None or not isinstance(data, dict): |
46 | | -# app.logger.error("TypeError with data content: '%s' (%s)", data, type(data)) |
47 | | -# return Response(status=HTTPStatus.UNPROCESSABLE_ENTITY) |
| 41 | + if data is None or not isinstance(data, dict): |
| 42 | + app.logger.error("TypeError with data content: '%s' (%s)", data, type(data)) |
| 43 | + return Response(status=HTTPStatus.UNPROCESSABLE_ENTITY) |
48 | 44 |
|
49 | | -# if pk is None or not isinstance(pk, str): |
50 | | -# app.logger.error("TypeError with data content: '%s' (%s)", pk, type(pk)) |
51 | | -# return Response(status=HTTPStatus.UNPROCESSABLE_ENTITY) |
| 45 | + if pk is None or not isinstance(pk, str): |
| 46 | + app.logger.error("TypeError with data content: '%s' (%s)", pk, type(pk)) |
| 47 | + return Response(status=HTTPStatus.UNPROCESSABLE_ENTITY) |
52 | 48 |
|
53 | | -# if pk not in CONFIG["participants"]: |
54 | | -# app.logger.error( |
55 | | -# "The public key provided is not in the participants: '%s' (%s)", |
56 | | -# pk, |
57 | | -# CONFIG["participants"], |
58 | | -# ) |
59 | | -# return Response(status=HTTPStatus.UNPROCESSABLE_ENTITY) |
| 49 | + if pk not in CONFIG["participants"]: |
| 50 | + app.logger.error( |
| 51 | + "The public key provided is not in the participants: '%s' (%s)", |
| 52 | + pk, |
| 53 | + CONFIG["participants"], |
| 54 | + ) |
| 55 | + return Response(status=HTTPStatus.UNPROCESSABLE_ENTITY) |
60 | 56 |
|
61 | | -# if pk in dict(globs.POOL): |
62 | | -# app.logger.error("Public key already pushed data") |
63 | | -# return Response(status=HTTPStatus.CONFLICT) |
| 57 | + if pk in dict(globs.POOL): |
| 58 | + app.logger.error("Public key already pushed data") |
| 59 | + return Response(status=HTTPStatus.CONFLICT) |
64 | 60 |
|
65 | | -# n: bytes = unseal(base64.b64decode(data["n"]), ENCLAVE_SK) |
| 61 | + n: bytes = unseal(base64.b64decode(data["n"]), ENCLAVE_SK) |
66 | 62 |
|
67 | | -# deser_n, *_ = struct.unpack("<d", n) |
68 | | -# globs.POOL.append((pk, deser_n)) |
| 63 | + deser_n, *_ = struct.unpack("<d", n) |
| 64 | + globs.POOL.append((pk, deser_n)) |
69 | 65 |
|
70 | | -# app.logger.info("Successfully added (%s, %s)", deser_n, pk) |
71 | | -# return Response(status=HTTPStatus.OK) |
| 66 | + app.logger.info("Successfully added (%s, %s)", deser_n, pk) |
| 67 | + return Response(status=HTTPStatus.OK) |
72 | 68 |
|
73 | 69 |
|
74 | | -# @app.get("/participants") |
75 | | -# def participants() -> Response: |
76 | | -# """Get all the public keys of participants""" |
77 | | -# return jsonify(CONFIG) |
| 70 | +@app.get("/participants") |
| 71 | +def participants() -> Response: |
| 72 | + """Get all the public keys of participants""" |
| 73 | + return jsonify(CONFIG) |
78 | 74 |
|
79 | 75 |
|
80 | | -# @app.post("/richest") |
81 | | -# def richest(): |
82 | | -# """Get the current max in pool.""" |
83 | | -# if len(globs.POOL) < 1: |
84 | | -# app.logger.error("need more than 1 value to compute the max") |
85 | | -# return {"max": None} |
| 76 | +@app.post("/richest") |
| 77 | +def richest(): |
| 78 | + """Get the current max in pool.""" |
| 79 | + if len(globs.POOL) < 1: |
| 80 | + app.logger.error("need more than 1 value to compute the max") |
| 81 | + return {"max": None} |
86 | 82 |
|
87 | | -# data: Optional[Any] = request.get_json(silent=True) |
| 83 | + data: Optional[Any] = request.get_json(silent=True) |
88 | 84 |
|
89 | | -# if data is None or not isinstance(data, dict): |
90 | | -# app.logger.error("TypeError with data: '%s'", data) |
91 | | -# return Response(status=HTTPStatus.UNPROCESSABLE_ENTITY) |
| 85 | + if data is None or not isinstance(data, dict): |
| 86 | + app.logger.error("TypeError with data: '%s'", data) |
| 87 | + return Response(status=HTTPStatus.UNPROCESSABLE_ENTITY) |
92 | 88 |
|
93 | | -# recipient_pk: Optional[str] = data.get("recipient_pk") |
| 89 | + recipient_pk: Optional[str] = data.get("recipient_pk") |
94 | 90 |
|
95 | | -# if recipient_pk is None or not isinstance(recipient_pk, str): |
96 | | -# app.logger.error( |
97 | | -# "TypeError with data content: '%s' (%s)", recipient_pk, type(recipient_pk) |
98 | | -# ) |
99 | | -# return Response(status=HTTPStatus.UNPROCESSABLE_ENTITY) |
| 91 | + if recipient_pk is None or not isinstance(recipient_pk, str): |
| 92 | + app.logger.error( |
| 93 | + "TypeError with data content: '%s' (%s)", recipient_pk, type(recipient_pk) |
| 94 | + ) |
| 95 | + return Response(status=HTTPStatus.UNPROCESSABLE_ENTITY) |
100 | 96 |
|
101 | | -# if recipient_pk not in CONFIG["participants"]: |
102 | | -# app.logger.error( |
103 | | -# "The public key provided is not in the participants: '%s' (%s)", |
104 | | -# recipient_pk, |
105 | | -# CONFIG["participants"], |
106 | | -# ) |
107 | | -# return Response(status=HTTPStatus.UNPROCESSABLE_ENTITY) |
| 97 | + if recipient_pk not in CONFIG["participants"]: |
| 98 | + app.logger.error( |
| 99 | + "The public key provided is not in the participants: '%s' (%s)", |
| 100 | + recipient_pk, |
| 101 | + CONFIG["participants"], |
| 102 | + ) |
| 103 | + return Response(status=HTTPStatus.UNPROCESSABLE_ENTITY) |
108 | 104 |
|
109 | | -# raw_recipient_pk: bytes = base64.b64decode(recipient_pk) |
| 105 | + raw_recipient_pk: bytes = base64.b64decode(recipient_pk) |
110 | 106 |
|
111 | | -# (pk, _) = max(globs.POOL, key=lambda t: t[1]) |
| 107 | + (pk, _) = max(globs.POOL, key=lambda t: t[1]) |
112 | 108 |
|
113 | | -# encrypted_b64_result: str = base64.b64encode( |
114 | | -# seal(base64.b64decode(pk), raw_recipient_pk) |
115 | | -# ).decode("utf-8") |
| 109 | + encrypted_b64_result: str = base64.b64encode( |
| 110 | + seal(base64.b64decode(pk), raw_recipient_pk) |
| 111 | + ).decode("utf-8") |
116 | 112 |
|
117 | | -# return jsonify({"max": encrypted_b64_result}) |
| 113 | + return jsonify({"max": encrypted_b64_result}) |
118 | 114 |
|
119 | 115 |
|
120 | | -# @app.delete("/") |
121 | | -# def reset(): |
122 | | -# """Reset the current pool.""" |
123 | | -# globs.POOL = [] |
| 116 | +@app.delete("/") |
| 117 | +def reset(): |
| 118 | + """Reset the current pool.""" |
| 119 | + globs.POOL = [] |
124 | 120 |
|
125 | | -# app.logger.info("Reset successfully") |
| 121 | + app.logger.info("Reset successfully") |
126 | 122 |
|
127 | | -# return Response(status=HTTPStatus.OK) |
| 123 | + return Response(status=HTTPStatus.OK) |
0 commit comments