Skip to content

Commit 31a0cff

Browse files
committed
enhance: add functions for daemon tools to do mTLS
Signed-off-by: Grant Linville <[email protected]>
1 parent 0cebee3 commit 31a0cff

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

gptscript/daemon.py

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import base64
2+
import ssl
3+
import os
4+
import tempfile
5+
6+
7+
def start_fastapi(app):
8+
cert, key, client_cert = save_certificates_from_env()
9+
import uvicorn
10+
uvicorn.run(
11+
app,
12+
host="127.0.0.1",
13+
port=int(os.getenv("PORT")),
14+
ssl_certfile=cert,
15+
ssl_keyfile=key,
16+
ssl_ca_certs=client_cert,
17+
ssl_cert_reqs=ssl.CERT_REQUIRED,
18+
)
19+
20+
def save_certificates_from_env():
21+
cert = base64.b64decode(os.getenv("CERT", ""))
22+
key = base64.b64decode(os.getenv("PRIVATE_KEY", ""))
23+
client_cert = base64.b64decode(os.getenv("GPTSCRIPT_CERT", ""))
24+
25+
if cert == "":
26+
print("error: CERT env var is empty")
27+
exit(1)
28+
elif key == "":
29+
print("error: PRIVATE_KEY env var is empty")
30+
exit(1)
31+
elif client_cert == "":
32+
print("error: GPTSCRIPT_CERT env var is empty")
33+
exit(1)
34+
35+
cert_file = tempfile.NamedTemporaryFile(delete=False, suffix=".pem")
36+
key_file = tempfile.NamedTemporaryFile(delete=False, suffix=".pem")
37+
client_cert_file = tempfile.NamedTemporaryFile(delete=False, suffix=".pem")
38+
39+
cert_file.write(cert)
40+
key_file.write(key)
41+
client_cert_file.write(client_cert)
42+
43+
cert_file.close()
44+
key_file.close()
45+
client_cert_file.close()
46+
47+
return cert_file.name, key_file.name, client_cert_file.name

pyproject.toml

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ dependencies = [
1919
"urllib3==2.2.1",
2020
"pydantic==2.9.2",
2121
"pywin32==306 ; sys_platform == 'win32'",
22+
"uvicorn==0.32.1",
2223
]
2324

2425
[project.urls]

requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ build==1.1.1
1212
httpx==0.27.0
1313
pydantic==2.9.2
1414
pywin32==306; sys_platform == 'win32'
15+
uvicorn==0.32.1

0 commit comments

Comments
 (0)