-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #69 from Stella-IT/staging
v1.3.10
- Loading branch information
Showing
15 changed files
with
309 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
from http.client import RemoteDisconnected | ||
from xmlrpc.client import Fault | ||
|
||
import ujson | ||
from fastapi import APIRouter, HTTPException, Request | ||
from XenAPI.XenAPI import Failure | ||
from XenGarden.session import create_session | ||
from XenGarden.VM import VM | ||
|
||
from API.v1.Common import xenapi_failure_jsonify | ||
from app.settings import Settings | ||
|
||
router = APIRouter() | ||
|
||
|
||
@router.get("/{cluster_id}/vm/{vm_uuid}/xenstore") | ||
@router.get("/{cluster_id}/template/{vm_uuid}/xenstore") | ||
async def instance_get_xenstore(cluster_id: str, vm_uuid: str): | ||
"""Get Instance (VM/Template) XenStore""" | ||
try: | ||
session = create_session( | ||
_id=cluster_id, get_xen_clusters=Settings.get_xen_clusters() | ||
) | ||
|
||
vm: VM = VM.get_by_uuid(session=session, uuid=vm_uuid) | ||
xenstore = vm.get_xenstore() | ||
|
||
session.xenapi.session.logout() | ||
return dict(success=True, data=xenstore) | ||
except Failure as xenapi_error: | ||
raise HTTPException( | ||
status_code=500, detail=xenapi_failure_jsonify(xenapi_error) | ||
) | ||
except Fault as xml_rpc_error: | ||
raise HTTPException( | ||
status_code=int(xml_rpc_error.faultCode), | ||
detail=xml_rpc_error.faultString, | ||
) | ||
except RemoteDisconnected as rd_error: | ||
raise HTTPException(status_code=500, detail=rd_error.strerror) | ||
|
||
|
||
@router.patch("/{cluster_id}/vm/{vm_uuid}/xenstore") | ||
@router.patch("/{cluster_id}/template/{vm_uuid}/xenstore") | ||
async def instance_add_xen_store(request: Request, cluster_id: str, vm_uuid: str): | ||
"""Add Instance (VM/Template) XenStore by Name""" | ||
try: | ||
body = ujson.decode(await request.body()) | ||
|
||
session = create_session( | ||
_id=cluster_id, get_xen_clusters=Settings.get_xen_clusters() | ||
) | ||
|
||
vm: VM = VM.get_by_uuid(session=session, uuid=vm_uuid) | ||
ret = dict(success=vm.add_xenstore(body)) | ||
|
||
session.xenapi.session.logout() | ||
return ret | ||
except Failure as xenapi_error: | ||
raise HTTPException( | ||
status_code=500, detail=xenapi_failure_jsonify(xenapi_error) | ||
) | ||
except Fault as xml_rpc_error: | ||
raise HTTPException( | ||
status_code=int(xml_rpc_error.faultCode), | ||
detail=xml_rpc_error.faultString, | ||
) | ||
except RemoteDisconnected as rd_error: | ||
raise HTTPException(status_code=500, detail=rd_error.strerror) | ||
|
||
|
||
@router.put("/{cluster_id}/vm/{vm_uuid}/xenstore") | ||
@router.put("/{cluster_id}/template/{vm_uuid}/xenstore") | ||
async def instance_set_bios_property(request: Request, cluster_id: str, vm_uuid: str): | ||
"""Set Instance (VM/Template) XenStore by Name""" | ||
try: | ||
body = ujson.decode(await request.body()) | ||
|
||
session = create_session( | ||
_id=cluster_id, get_xen_clusters=Settings.get_xen_clusters() | ||
) | ||
|
||
vm: VM = VM.get_by_uuid(session=session, uuid=vm_uuid) | ||
ret = dict(success=vm.set_xenstore(body)) | ||
|
||
session.xenapi.session.logout() | ||
return ret | ||
except Failure as xenapi_error: | ||
raise HTTPException( | ||
status_code=500, detail=xenapi_failure_jsonify(xenapi_error) | ||
) | ||
except Fault as xml_rpc_error: | ||
raise HTTPException( | ||
status_code=int(xml_rpc_error.faultCode), | ||
detail=xml_rpc_error.faultString, | ||
) | ||
except RemoteDisconnected as rd_error: | ||
raise HTTPException(status_code=500, detail=rd_error.strerror) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
__all__ = ["services", "controller", "settings", "extension"] | ||
__all__ = ["services", "controller", "settings", "security", "extension"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import secrets | ||
|
||
from fastapi import Depends, HTTPException, status | ||
from fastapi.security import HTTPBasic, HTTPBasicCredentials | ||
|
||
from .settings import Settings | ||
|
||
|
||
class Security: | ||
fastapi_basic = HTTPBasic() | ||
|
||
@classmethod | ||
def load_authentication_config(cls): | ||
return Settings.get_authentication_config() | ||
|
||
@classmethod | ||
def load_authentication_type(cls): | ||
config = cls.load_authentication_config() | ||
|
||
if config is None: | ||
return None | ||
|
||
return str(config["type"]).lower() | ||
|
||
@classmethod | ||
def load_accounts(cls): | ||
config = cls.load_authentication_config() | ||
|
||
accounts = [] | ||
if config is not None: | ||
accounts = config["accounts"] | ||
|
||
return accounts | ||
|
||
@classmethod | ||
def get_authentication_dependencies(cls): | ||
dependencies = [] | ||
|
||
if cls.load_authentication_type() == "basic": | ||
dependencies.append(Depends(cls.authenticate_basic)) | ||
|
||
return dependencies | ||
|
||
@classmethod | ||
def authenticate_basic( | ||
cls, credentials: HTTPBasicCredentials = Depends(fastapi_basic) | ||
): | ||
if cls.load_authentication_type() == "basic": | ||
match_found = False | ||
|
||
username_bytes = credentials.username.encode("utf8") | ||
password_bytes = credentials.password.encode("utf8") | ||
|
||
for account in cls.load_accounts(): | ||
account_username_bytes = account["username"].encode("utf8") | ||
account_password_bytes = account["password"].encode("utf8") | ||
|
||
username_match = secrets.compare_digest( | ||
username_bytes, account_username_bytes | ||
) | ||
password_match = secrets.compare_digest( | ||
password_bytes, account_password_bytes | ||
) | ||
if username_match and password_match: | ||
match_found = True | ||
|
||
if not match_found: | ||
raise HTTPException( | ||
status_code=status.HTTP_401_UNAUTHORIZED, | ||
detail={"type": "unauthorized"}, | ||
headers={"WWW-Authenticate": "Basic"}, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.