Skip to content

Commit 71f1f3f

Browse files
committed
Add: SSL verify mode option for client cert auth"
1 parent 0b5ab1b commit 71f1f3f

3 files changed

Lines changed: 76 additions & 80 deletions

File tree

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM ghcr.io/cosmian/cenclave-base-beta:764d2b9114072facd659383539673018a51f55de
1+
FROM ghcr.io/cosmian/cenclave-base:20241213084448
22

33
RUN . /opt/venv/bin/activate && \
4-
pip3 install "fastapi>=0.115.6,<0.116"
4+
pip3 install "flask==3.1.0"

examples/yaos_millionaires/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ then populate `src/config.json` with participant's public key base64-encoded:
3838
$ cenclave localtest --code src/ \
3939
--dockerfile Dockerfile \
4040
--config config.toml \
41-
--test tests/ \
41+
--test tests/
4242
--simu-enclave-keypair tests/data/keypair_enclave.bin
4343
```
4444

examples/yaos_millionaires/src/app.py

Lines changed: 73 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -9,119 +9,115 @@
99
from typing import Any, Optional
1010

1111
from cenclave_lib_crypto.seal_box import seal, unseal
12-
from fastapi import FastAPI, Response, Request
12+
from flask import Flask, Response, jsonify, request
1313

1414
import globs
1515

16-
app = FastAPI()
16+
app = Flask(__name__)
1717

1818
CONFIG = json.loads((Path(__file__).parent / "config.json").read_text(encoding="utf-8"))
1919

2020
ENCLAVE_SK: bytes = Path(os.environ["ENCLAVE_SK_PATH"]).read_bytes()
2121

2222

2323
@app.get("/health")
24-
async def health_check(request: Request) -> Response:
24+
def health_check() -> Response:
2525
"""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)
3127

3228

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)
3733

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)
4137

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")
4440

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)
4844

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)
5248

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)
6056

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)
6460

65-
# n: bytes = unseal(base64.b64decode(data["n"]), ENCLAVE_SK)
61+
n: bytes = unseal(base64.b64decode(data["n"]), ENCLAVE_SK)
6662

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))
6965

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)
7268

7369

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)
7874

7975

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}
8682

87-
# data: Optional[Any] = request.get_json(silent=True)
83+
data: Optional[Any] = request.get_json(silent=True)
8884

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)
9288

93-
# recipient_pk: Optional[str] = data.get("recipient_pk")
89+
recipient_pk: Optional[str] = data.get("recipient_pk")
9490

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)
10096

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)
108104

109-
# raw_recipient_pk: bytes = base64.b64decode(recipient_pk)
105+
raw_recipient_pk: bytes = base64.b64decode(recipient_pk)
110106

111-
# (pk, _) = max(globs.POOL, key=lambda t: t[1])
107+
(pk, _) = max(globs.POOL, key=lambda t: t[1])
112108

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")
116112

117-
# return jsonify({"max": encrypted_b64_result})
113+
return jsonify({"max": encrypted_b64_result})
118114

119115

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 = []
124120

125-
# app.logger.info("Reset successfully")
121+
app.logger.info("Reset successfully")
126122

127-
# return Response(status=HTTPStatus.OK)
123+
return Response(status=HTTPStatus.OK)

0 commit comments

Comments
 (0)