mirror of
https://github.com/microsoft/onefuzz.git
synced 2025-06-19 04:58:09 +00:00
basic list proxy functionality (#905)
This commit is contained in:
@ -9,7 +9,7 @@ import azure.functions as func
|
|||||||
from onefuzztypes.enums import ErrorCode, VmState
|
from onefuzztypes.enums import ErrorCode, VmState
|
||||||
from onefuzztypes.models import Error
|
from onefuzztypes.models import Error
|
||||||
from onefuzztypes.requests import ProxyCreate, ProxyDelete, ProxyGet, ProxyReset
|
from onefuzztypes.requests import ProxyCreate, ProxyDelete, ProxyGet, ProxyReset
|
||||||
from onefuzztypes.responses import BoolResult, ProxyGetResult
|
from onefuzztypes.responses import BoolResult, ProxyGetResult, ProxyInfo, ProxyList
|
||||||
|
|
||||||
from ..onefuzzlib.endpoint_authorization import call_if_user
|
from ..onefuzzlib.endpoint_authorization import call_if_user
|
||||||
from ..onefuzzlib.events import get_events
|
from ..onefuzzlib.events import get_events
|
||||||
@ -36,26 +36,37 @@ def get(req: func.HttpRequest) -> func.HttpResponse:
|
|||||||
if isinstance(request, Error):
|
if isinstance(request, Error):
|
||||||
return not_ok(request, context="ProxyGet")
|
return not_ok(request, context="ProxyGet")
|
||||||
|
|
||||||
scaleset = Scaleset.get_by_id(request.scaleset_id)
|
if (
|
||||||
if isinstance(scaleset, Error):
|
request.scaleset_id is not None
|
||||||
return not_ok(scaleset, context="ProxyGet")
|
and request.machine_id is not None
|
||||||
|
and request.dst_port is not None
|
||||||
|
):
|
||||||
|
scaleset = Scaleset.get_by_id(request.scaleset_id)
|
||||||
|
if isinstance(scaleset, Error):
|
||||||
|
return not_ok(scaleset, context="ProxyGet")
|
||||||
|
|
||||||
proxy = Proxy.get_or_create(scaleset.region)
|
proxy = Proxy.get_or_create(scaleset.region)
|
||||||
forwards = ProxyForward.search_forward(
|
forwards = ProxyForward.search_forward(
|
||||||
scaleset_id=request.scaleset_id,
|
scaleset_id=request.scaleset_id,
|
||||||
machine_id=request.machine_id,
|
machine_id=request.machine_id,
|
||||||
dst_port=request.dst_port,
|
dst_port=request.dst_port,
|
||||||
)
|
|
||||||
if not forwards:
|
|
||||||
return not_ok(
|
|
||||||
Error(
|
|
||||||
code=ErrorCode.INVALID_REQUEST,
|
|
||||||
errors=["no forwards for scaleset and node"],
|
|
||||||
),
|
|
||||||
context="debug_proxy get",
|
|
||||||
)
|
)
|
||||||
|
if not forwards:
|
||||||
|
return not_ok(
|
||||||
|
Error(
|
||||||
|
code=ErrorCode.INVALID_REQUEST,
|
||||||
|
errors=["no forwards for scaleset and node"],
|
||||||
|
),
|
||||||
|
context="debug_proxy get",
|
||||||
|
)
|
||||||
|
|
||||||
return ok(get_result(forwards[0], proxy))
|
return ok(get_result(forwards[0], proxy))
|
||||||
|
else:
|
||||||
|
proxies = [
|
||||||
|
ProxyInfo(region=x.region, proxy_id=x.proxy_id, state=x.state)
|
||||||
|
for x in Proxy.search()
|
||||||
|
]
|
||||||
|
return ok(ProxyList(proxies=proxies))
|
||||||
|
|
||||||
|
|
||||||
def post(req: func.HttpRequest) -> func.HttpResponse:
|
def post(req: func.HttpRequest) -> func.HttpResponse:
|
||||||
|
@ -1468,6 +1468,9 @@ class ScalesetProxy(Endpoint):
|
|||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def list(self) -> responses.ProxyList:
|
||||||
|
return self._req_model("GET", responses.ProxyList, data=requests.ProxyGet())
|
||||||
|
|
||||||
|
|
||||||
class Command:
|
class Command:
|
||||||
def __init__(self, onefuzz: "Onefuzz", logger: logging.Logger):
|
def __init__(self, onefuzz: "Onefuzz", logger: logging.Logger):
|
||||||
|
@ -3,10 +3,10 @@
|
|||||||
# Copyright (c) Microsoft Corporation.
|
# Copyright (c) Microsoft Corporation.
|
||||||
# Licensed under the MIT License.
|
# Licensed under the MIT License.
|
||||||
|
|
||||||
from typing import Dict, List, Optional
|
from typing import Any, Dict, List, Optional
|
||||||
from uuid import UUID
|
from uuid import UUID
|
||||||
|
|
||||||
from pydantic import AnyHttpUrl, BaseModel, Field, validator
|
from pydantic import AnyHttpUrl, BaseModel, Field, root_validator, validator
|
||||||
|
|
||||||
from .consts import ONE_HOUR, SEVEN_DAYS
|
from .consts import ONE_HOUR, SEVEN_DAYS
|
||||||
from .enums import (
|
from .enums import (
|
||||||
@ -107,9 +107,20 @@ class PoolStop(BaseRequest):
|
|||||||
|
|
||||||
|
|
||||||
class ProxyGet(BaseRequest):
|
class ProxyGet(BaseRequest):
|
||||||
scaleset_id: UUID
|
scaleset_id: Optional[UUID]
|
||||||
machine_id: UUID
|
machine_id: Optional[UUID]
|
||||||
dst_port: int
|
dst_port: Optional[int]
|
||||||
|
|
||||||
|
@root_validator()
|
||||||
|
def check_proxy_get(cls, value: Any) -> Any:
|
||||||
|
check_keys = ["scaleset_id", "machine_id", "dst_port"]
|
||||||
|
included = [x in value for x in check_keys]
|
||||||
|
if any(included) and not all(included):
|
||||||
|
raise ValueError(
|
||||||
|
"ProxyGet must provide all or none of the following: %s"
|
||||||
|
% ", ".join(check_keys)
|
||||||
|
)
|
||||||
|
return value
|
||||||
|
|
||||||
|
|
||||||
class ProxyCreate(BaseRequest):
|
class ProxyCreate(BaseRequest):
|
||||||
|
@ -3,11 +3,12 @@
|
|||||||
# Copyright (c) Microsoft Corporation.
|
# Copyright (c) Microsoft Corporation.
|
||||||
# Licensed under the MIT License.
|
# Licensed under the MIT License.
|
||||||
|
|
||||||
from typing import Dict, Optional
|
from typing import Dict, List, Optional
|
||||||
from uuid import UUID
|
from uuid import UUID
|
||||||
|
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
|
|
||||||
|
from .enums import VmState
|
||||||
from .models import Forward, NodeCommandEnvelope
|
from .models import Forward, NodeCommandEnvelope
|
||||||
from .primitives import Region
|
from .primitives import Region
|
||||||
|
|
||||||
@ -25,6 +26,16 @@ class ProxyGetResult(BaseResponse):
|
|||||||
forward: Forward
|
forward: Forward
|
||||||
|
|
||||||
|
|
||||||
|
class ProxyInfo(BaseModel):
|
||||||
|
region: Region
|
||||||
|
proxy_id: UUID
|
||||||
|
state: VmState
|
||||||
|
|
||||||
|
|
||||||
|
class ProxyList(BaseResponse):
|
||||||
|
proxies: List[ProxyInfo]
|
||||||
|
|
||||||
|
|
||||||
class Version(BaseResponse):
|
class Version(BaseResponse):
|
||||||
git: str
|
git: str
|
||||||
build: str
|
build: str
|
||||||
|
Reference in New Issue
Block a user