mirror of
https://github.com/GNS3/gns3-server.git
synced 2026-10-25 11:22:26 +02:00
feat(compute): option to disable compute authentication
This commit is contained in:
parent
dce4a71004
commit
8c3aaa78f5
@ -26,12 +26,29 @@ from gns3server.config import Config
|
|||||||
from typing import Optional, Union
|
from typing import Optional, Union
|
||||||
|
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
security = HTTPBasic()
|
security = HTTPBasic(auto_error=False)
|
||||||
|
|
||||||
|
|
||||||
def compute_authentication(credentials: Optional[HTTPBasicCredentials] = Depends(security)) -> None:
|
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
|
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)
|
username = secrets.compare_digest(credentials.username, server_settings.compute_username)
|
||||||
password = secrets.compare_digest(credentials.password, server_settings.compute_password.get_secret_value())
|
password = secrets.compare_digest(credentials.password, server_settings.compute_password.get_secret_value())
|
||||||
if not (username and password):
|
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]:
|
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()
|
await websocket.accept()
|
||||||
|
|
||||||
@ -68,7 +91,6 @@ async def ws_compute_authentication(websocket: WebSocket) -> Union[None, WebSock
|
|||||||
if not separator:
|
if not separator:
|
||||||
raise invalid_user_credentials_exc
|
raise invalid_user_credentials_exc
|
||||||
|
|
||||||
server_settings = Config.instance().settings.Server
|
|
||||||
username = secrets.compare_digest(username, server_settings.compute_username)
|
username = secrets.compare_digest(username, server_settings.compute_username)
|
||||||
password = secrets.compare_digest(password, server_settings.compute_password.get_secret_value())
|
password = secrets.compare_digest(password, server_settings.compute_password.get_secret_value())
|
||||||
if not (username and password):
|
if not (username and password):
|
||||||
|
|||||||
@ -114,6 +114,7 @@ class BuiltinSymbolTheme(str, Enum):
|
|||||||
class ServerSettings(BaseModel):
|
class ServerSettings(BaseModel):
|
||||||
|
|
||||||
local: bool = False
|
local: bool = False
|
||||||
|
enable_http_auth: bool = True
|
||||||
name: str = f"{socket.gethostname()} (controller)"
|
name: str = f"{socket.gethostname()} (controller)"
|
||||||
protocol: ServerProtocol = ServerProtocol.http
|
protocol: ServerProtocol = ServerProtocol.http
|
||||||
host: str = "0.0.0.0"
|
host: str = "0.0.0.0"
|
||||||
|
|||||||
@ -240,7 +240,10 @@ class Server:
|
|||||||
|
|
||||||
self._set_config_defaults_from_command_line(args)
|
self._set_config_defaults_from_command_line(args)
|
||||||
config = Config.instance().settings
|
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
|
alphabet = string.ascii_letters + string.digits + string.punctuation
|
||||||
generated_password = ''.join(secrets.choice(alphabet) for _ in range(16))
|
generated_password = ''.join(secrets.choice(alphabet) for _ in range(16))
|
||||||
config.Server.compute_password = SecretStr(generated_password)
|
config.Server.compute_password = SecretStr(generated_password)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user