feat(compute): option to disable compute authentication

This commit is contained in:
Mark Paronyan 2026-03-03 00:28:57 +03:00
parent dce4a71004
commit 8c3aaa78f5
No known key found for this signature in database
GPG Key ID: 8DC7338A87FE22F8
3 changed files with 29 additions and 3 deletions

View File

@ -26,12 +26,29 @@ from gns3server.config import Config
from typing import Optional, Union
log = logging.getLogger(__name__)
security = HTTPBasic()
security = HTTPBasic(auto_error=False)
def compute_authentication(credentials: Optional[HTTPBasicCredentials] = Depends(security)) -> None:
"""
Authenticate compute requests.
Returns None if authentication is disabled or if authentication succeeds
Raises HTTPException if authentication is required but credentials are invalid
"""
server_settings = Config.instance().settings.Server
if not server_settings.enable_http_auth:
return None
if credentials is None:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Invalid compute username or password",
headers={"WWW-Authenticate": "Basic"},
)
username = secrets.compare_digest(credentials.username, server_settings.compute_username)
password = secrets.compare_digest(credentials.password, server_settings.compute_password.get_secret_value())
if not (username and password):
@ -44,6 +61,12 @@ def compute_authentication(credentials: Optional[HTTPBasicCredentials] = Depends
async def ws_compute_authentication(websocket: WebSocket) -> Union[None, WebSocket]:
"""
"""
server_settings = Config.instance().settings.Server
if not server_settings.enable_http_auth:
await websocket.accept()
return websocket
await websocket.accept()
@ -68,7 +91,6 @@ async def ws_compute_authentication(websocket: WebSocket) -> Union[None, WebSock
if not separator:
raise invalid_user_credentials_exc
server_settings = Config.instance().settings.Server
username = secrets.compare_digest(username, server_settings.compute_username)
password = secrets.compare_digest(password, server_settings.compute_password.get_secret_value())
if not (username and password):

View File

@ -114,6 +114,7 @@ class BuiltinSymbolTheme(str, Enum):
class ServerSettings(BaseModel):
local: bool = False
enable_http_auth: bool = True
name: str = f"{socket.gethostname()} (controller)"
protocol: ServerProtocol = ServerProtocol.http
host: str = "0.0.0.0"

View File

@ -240,7 +240,10 @@ class Server:
self._set_config_defaults_from_command_line(args)
config = Config.instance().settings
if not config.Server.compute_password.get_secret_value():
if not config.Server.enable_http_auth:
log.info("Compute authentication is disabled")
elif not config.Server.compute_password.get_secret_value():
alphabet = string.ascii_letters + string.digits + string.punctuation
generated_password = ''.join(secrets.choice(alphabet) for _ in range(16))
config.Server.compute_password = SecretStr(generated_password)