Skip to content

Commit c491e47

Browse files
committed
WIP: Updating allocations was not authenticated
Use a simple authentication method: the hash of the signature should match the value in the settings
1 parent d2c231e commit c491e47

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

vm_supervisor/conf.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,9 @@ class Settings(BaseSettings):
105105
MAX_PROGRAM_ARCHIVE_SIZE = 10_000_000 # 10 MB
106106
MAX_DATA_ARCHIVE_SIZE = 10_000_000 # 10 MB
107107

108+
# hashlib.sha256(b"secret-token").hexdigest()
109+
ALLOCATION_TOKEN_HASH = "151ba92f2eb90bce67e912af2f7a5c17d8654b3d29895b042107ea312a7eebda"
110+
108111
FAKE_DATA_PROGRAM: Optional[Path] = None
109112
BENCHMARK_FAKE_DATA_PROGRAM = Path(
110113
abspath(join(__file__, "../../examples/example_fastapi"))

vm_supervisor/views.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import binascii
22
import logging
33
import os.path
4+
from hashlib import sha256
45
from string import Template
56
from typing import Awaitable, Optional
67

@@ -170,8 +171,26 @@ async def status_check_version(request: web.Request):
170171
return web.HTTPForbidden(text=f"Outdated: version {current} < {reference}")
171172

172173

174+
def authenticate_api_request(request: web.Request) -> bool:
175+
"""Authenticate an API request to update the VM allocations.
176+
"""
177+
signature: str = request.headers.get('X-Auth-Signature')
178+
# body: bytes = await request.read()
179+
if not signature:
180+
raise web.HTTPUnauthorized(text="Authentication token is missing")
181+
182+
# Use a simple authentication method: the hash of the signature should match the value in the settings
183+
if sha256(signature).hexdigest() != settings.ALLOCATION_TOKEN_HASH:
184+
raise web.HTTPUnauthorized(text="Authentication token received is invalid")
185+
186+
return True
187+
188+
189+
173190
async def update_allocations(request: web.Request):
174-
# TODO: Add some form of authentication
191+
if not authenticate_api_request(request):
192+
return web.HTTPUnauthorized(text="Invalid authentication")
193+
175194
try:
176195
data = await request.json()
177196
allocation = Allocation(**data)

0 commit comments

Comments
 (0)