Skip to content

Commit 43a8b6b

Browse files
authored
Encrypt api key and add readme for docker (#107)
1 parent 52ade4f commit 43a8b6b

24 files changed

+210
-183
lines changed

.env.test

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
ENV=test
22
APP_NAME=Whitebox | Test
33
APP_NAME_CRON=Whitebox | Test
4+
SECRET_KEY=3beae33e30bcdaf6b172e17dc8f26341
45

56
DATABASE_URL=postgresql://postgres:postgres@localhost:5432/test
67

README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,39 @@ docker compose up postgres -d
102102
mkdocs serve -f docs/mkdocs/mkdocs.yml -a localhost:8001
103103
```
104104

105+
# Deploy Whitebox
106+
## Using docker
107+
108+
Whitebox uses postgres as its database. They need to run in the same docker network. An example docker-compose file is located in the `examples` folder. Make sure you replace the SECRET_KEY with one of your own. Look below for more info.
109+
110+
```bash
111+
docker-compose -f examples/docker-compose/docker-compose.yml up
112+
```
113+
114+
If you just need to run Whitebox, make sure you set the `DATABASE_URL` in the environment.
115+
116+
```bash
117+
docker run -dp 8000:8000 sqdhub/whitebox:main -e DATABASE_URL=postgresql://user:password@host:port/db_name
118+
```
119+
To save the api key encrypted in the database, provide a SECRET_KEY variable in the environment that is consisted of a 16 bytes string.
120+
```bash
121+
python -c "from secrets import token_hex; print(token_hex(16))"
122+
```
123+
***Save this token somewhere safe.***
124+
125+
The api key can be retrieved directly from the postgres database:
126+
127+
```bash
128+
API_KEY=$(docker exec <postgres_container_id> /bin/sh -c "psql -U postgres -c \"SELECT api_key FROM users WHERE username='admin';\" -tA")
129+
130+
echo $API_KEY
131+
```
132+
If you've set the `SECRET_KEY` in the environment get the decrypted key using:
133+
134+
```bash
135+
docker exec <whitebox_container_id> /usr/local/bin/python scripts/decrypt_api_key.py $API_KEY
136+
```
137+
105138
# Contributing
106139

107140
We happily welcome contributions to Whitebox. You can start by opening a new issue!

docker-compose.yml renamed to examples/docker-compose/docker-compose.yml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
version: "3.10"
2+
name: Whitebox
23
services:
34
postgres:
45
image: postgres:15
@@ -15,18 +16,26 @@ services:
1516
- "5432:5432"
1617
volumes:
1718
- wb_data:/var/lib/postgresql/data
19+
networks:
20+
- whitebox
1821

1922
whitebox:
20-
profiles: ["whitebox"]
2123
image: sqdhub/whitebox:main
2224
restart: unless-stopped
2325
environment:
2426
- APP_NAME=Whitebox | Docker
2527
- DATABASE_URL=postgresql://postgres:postgres@postgres:5432/postgres
28+
- SECRET_KEY=<add_your_own>
2629
ports:
2730
- "8000:8000"
2831
depends_on:
2932
- postgres
33+
networks:
34+
- whitebox
3035

3136
volumes:
3237
wb_data:
38+
39+
networks:
40+
whitebox:
41+
name: whitebox
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
repositoryID: 5276bf98-7f0a-407d-ba4d-5b3083801cd6
2+
owners:
3+
- name: Squaredev
4+
email: hello@squaredev.io

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ python-jose==3.3.0
117117
python-multipart==0.0.5
118118
pytz==2022.5
119119
pyu2f==0.1.5
120-
PyYAML==5.1
120+
PyYAML==5.3.1
121121
requests==2.28.1
122122
requests-mock==1.10.0
123123
requests-oauthlib==1.3.1

scripts/decrypt_api_key.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import os
2+
import sys
3+
4+
sys.path.append(os.getcwd())
5+
6+
from whitebox.utils.passwords import decrypt_api_key
7+
from whitebox.core.settings import get_settings
8+
9+
value = sys.argv[1]
10+
settings = get_settings()
11+
12+
if __name__ == "__main__":
13+
api_key = decrypt_api_key(value, settings.SECRET_KEY.encode())
14+
print(api_key)

whitebox/api/v1/__init__.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from fastapi import APIRouter
22
from .health import health_router
33

4-
from .users import users_router
54
from .models import models_router
65
from .dataset_rows import dataset_rows_router
76
from .inference_rows import inference_rows_router
@@ -17,7 +16,6 @@
1716
v1 = "/v1"
1817

1918
v1_router.include_router(health_router, prefix=v1)
20-
v1_router.include_router(users_router, prefix=v1)
2119
v1_router.include_router(models_router, prefix=v1)
2220
v1_router.include_router(dataset_rows_router, prefix=v1)
2321
v1_router.include_router(inference_rows_router, prefix=v1)

whitebox/api/v1/users.py

Lines changed: 0 additions & 42 deletions
This file was deleted.

whitebox/core/db.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@
66
from whitebox.schemas.user import UserCreateDto
77

88
from whitebox import crud
9-
from whitebox.utils.passwords import hash_password
9+
from whitebox.utils.passwords import encrypt_api_key
1010
from whitebox.utils.logger import cronLogger as logger
11-
import os
1211

1312
from secrets import token_hex
1413

@@ -32,13 +31,20 @@ async def connect():
3231
"""
3332
Base.metadata.create_all(engine)
3433
db = SessionLocal()
35-
if not os.getenv("ENV") == "test":
36-
admin_exists = crud.users.get_first_by_filter(db=db, username="admin")
37-
if not admin_exists:
38-
api_key = token_hex(32)
39-
obj_in = UserCreateDto(username="admin", api_key=hash_password(api_key))
40-
crud.users.create(db=db, obj_in=obj_in)
41-
logger.info(f"Created username: admin, API key: {api_key}")
34+
35+
admin_exists = crud.users.get_first_by_filter(db=db, username="admin")
36+
if not admin_exists:
37+
plain_api_key = token_hex(32)
38+
secret_key = settings.SECRET_KEY
39+
api_key = (
40+
encrypt_api_key(plain_api_key, secret_key.encode())
41+
if secret_key
42+
else plain_api_key
43+
)
44+
45+
obj_in = UserCreateDto(username="admin", api_key=api_key)
46+
crud.users.create(db=db, obj_in=obj_in)
47+
logger.info(f"Created username: admin, API key: {plain_api_key}")
4248
await database.connect()
4349

4450

whitebox/core/settings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ class Settings(BaseSettings):
99
DATABASE_URL: str = ""
1010
VERSION: str = ""
1111
MODEL_PATH: str = ""
12+
SECRET_KEY: str = ""
1213

1314
class Config:
1415
env_file = f".env.{os.getenv('ENV')}" or ".env.dev"

0 commit comments

Comments
 (0)