Skip to content

API

Encapsulates the response data for the jobs router.

CreateJobResponse

Bases: BaseModel

Encapsulates the response for a create jobs API call

Source code in src/mmisp/worker/api/response_schemas.py
42
43
44
45
46
47
48
49
50
class CreateJobResponse(BaseModel):
    """
    Encapsulates the response for a create jobs API call
    """

    success: bool
    """The API call was successful or not"""
    job_id: str | None = None
    """The id of the created job"""

job_id = None class-attribute instance-attribute

The id of the created job

success instance-attribute

The API call was successful or not

DeleteJobResponse

Bases: BaseModel

Encapsulates the response for a remove jobs API call

Source code in src/mmisp/worker/api/response_schemas.py
53
54
55
56
57
58
59
class DeleteJobResponse(BaseModel):
    """
    Encapsulates the response for a remove jobs API call
    """

    success: bool
    """The API call was successful or not"""

success instance-attribute

The API call was successful or not

ExceptionResponse

Bases: BaseModel

Encapsulates the response for a jobs where an exception was raised

Source code in src/mmisp/worker/api/response_schemas.py
33
34
35
36
37
38
39
class ExceptionResponse(BaseModel):
    """
    Encapsulates the response for a jobs where an exception was raised
    """

    message: str
    """A costume message which describes the error that occurred"""

message instance-attribute

A costume message which describes the error that occurred

JobStatusEnum

Bases: StrEnum

Encapsulates the status of a Job

Source code in src/mmisp/worker/api/response_schemas.py
10
11
12
13
14
15
16
17
18
19
class JobStatusEnum(StrEnum):
    """
    Encapsulates the status of a Job
    """

    SUCCESS = "success"
    FAILED = "failed"
    IN_PROGRESS = "inProgress"
    QUEUED = "queued"
    REVOKED = "revoked"

JobStatusResponse

Bases: BaseModel

Encapsulates the response for a jobs status API call

Source code in src/mmisp/worker/api/response_schemas.py
22
23
24
25
26
27
28
29
30
class JobStatusResponse(BaseModel):
    """
    Encapsulates the response for a jobs status API call
    """

    status: JobStatusEnum
    """The status of the job"""
    message: str
    """A costume message which describes the success of getting the job status"""

message instance-attribute

A costume message which describes the success of getting the job status

status instance-attribute

The status of the job

StartStopWorkerResponse

Bases: BaseModel

Represents the API response of starting and stopping a worker

Source code in src/mmisp/worker/api/response_schemas.py
77
78
79
80
81
82
83
84
85
86
87
class StartStopWorkerResponse(BaseModel):
    """
    Represents the API response of starting and stopping a worker
    """

    success: bool
    """The API call was successful or not"""
    message: str
    """A costume message which describes the success of starting or stopping the worker"""
    url: str
    """The API url"""

message instance-attribute

A costume message which describes the success of starting or stopping the worker

success instance-attribute

The API call was successful or not

url instance-attribute

The API url

WorkerStatusEnum

Bases: StrEnum

Represents different statuses of a worker

Source code in src/mmisp/worker/api/response_schemas.py
67
68
69
70
71
72
73
74
class WorkerStatusEnum(StrEnum):
    """
    Represents different statuses of a worker
    """

    IDLE = "idle"
    WORKING = "working"
    DEACTIVATED = "deactivated"

WorkerStatusResponse

Bases: BaseModel

Represents the API response of getting the status of a worker

Source code in src/mmisp/worker/api/response_schemas.py
90
91
92
93
94
95
96
97
98
class WorkerStatusResponse(BaseModel):
    """
    Represents the API response of getting the status of a worker
    """

    status: WorkerStatusEnum
    """The status of the worker"""
    jobs_queued: int
    """The number of queued jobs of the worker"""

jobs_queued instance-attribute

The number of queued jobs of the worker

status instance-attribute

The status of the worker

Encapsulates input data classes for the jobs router.

JobEnum

Bases: StrEnum

Represents the implemented jobs

Source code in src/mmisp/worker/api/requests_schemas.py
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
class JobEnum(StrEnum):
    """
    Represents the implemented jobs
    """

    PULL = "pull"
    PUSH = "push"
    CORRELATE = "correlation"
    ENRICHMENT = "enrichment"
    SEND_EMAIL = "email"
    PROCESS_FREE_TEXT = "processfreetext"
    IMPORT_TAXONOMIES = "taxonomy"
    IMPORT_OBJECT_TEMPLATES = "object_template"
    IMPORT_GALAXIES = "galaxy"
    IMPORT_FEED = "importFeed"
    DEBUG = "debug"

UserData

Bases: BaseModel

Data class for user_id

Source code in src/mmisp/worker/api/requests_schemas.py
10
11
12
13
14
15
16
class UserData(BaseModel):
    """
    Data class for user_id
    """

    user_id: int
    """The id of the user"""

user_id instance-attribute

The id of the user

worker_router = APIRouter(prefix='/worker', tags=['worker']) module-attribute

Every method in this file is a route for the worker_router every endpoint is prefixed with /worker and requires the user to be verified

add_queue(id, body, response) async

Adds an existing queue to a worker.

Parameters:

Name Type Description Default
id str

The id of the worker.

required
body RemoveAddQueueToWorker

The request body containing the queue name to add.

required

Returns:

Type Description
None

None

Raises:

Type Description
HTTPException

If the worker or queue cannot be found or if an error occurs during queue addition.

Source code in src/mmisp/worker/api/worker_router.py
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
@worker_router.post("/addQueue/{id}", dependencies=[Depends(verified)])
async def add_queue(id: str, body: RemoveAddQueueToWorker, response: Response) -> None:
    """
    Adds an existing queue to a worker.

    Args:
        id (str): The id of the worker.
        body (RemoveAddQueueToWorker): The request body containing the queue name to add.

    Returns:
        None

    Raises:
        HTTPException: If the worker or queue cannot be found or if an error occurs during queue addition.
    """
    if not worker_controller.check_worker_name(id):
        raise HTTPException(status_code=404, detail=f'A worker with the name "{id}" does not exist.')
    response.headers["x-queue-name-header"] = body.queue_name
    try:
        JobEnum(body.queue_name)
    except ValueError:
        raise HTTPException(status_code=404, detail="Queue not found")

    await worker_controller.add_queue_to_worker(id, body.queue_name)

get_job_queue(id) async

Get a list of all job queues for the worker specified by the id.

Parameters:

Name Type Description Default
id str

The id of the worker.

required

Returns:

Type Description
list[GetWorkerJobqueue]

list[GetWorkerJobqueue]: A list of job queue objects for the worker.

Raises:

Type Description
HTTPException

If an error occurs while retrieving the job queues or the worker id is invalid.

Source code in src/mmisp/worker/api/worker_router.py
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
@worker_router.get("/jobqueue/{id}", dependencies=[Depends(verified)])
async def get_job_queue(id: str) -> list[GetWorkerJobqueue]:
    """
    Get a list of all job queues for the worker specified by the id.

    Args:
        id (str): The id of the worker.

    Returns:
        list[GetWorkerJobqueue]: A list of job queue objects for the worker.

    Raises:
        HTTPException: If an error occurs while retrieving the job queues or the worker id is invalid.
    """
    if not worker_controller.check_worker_name(id):
        raise HTTPException(status_code=404, detail=f'A worker with the name "{id}" does not exist.')

    return await worker_controller.get_worker_jobqueues(id)

list_all_workers() async

Get a list of all workers.

Returns:

Type Description
list[GetWorkerWorkers]

list[GetWorkerWorkers]: A list of GetWorkerWorkers objects with status, queues,

list[GetWorkerWorkers]

and job counts of a single worker.

Raises:

Type Description
HTTPException

If an error occurs while retrieving the worker list.

Source code in src/mmisp/worker/api/worker_router.py
151
152
153
154
155
156
157
158
159
160
161
162
163
@worker_router.get("/list_workers", dependencies=[Depends(verified)])
async def list_all_workers() -> list[GetWorkerWorkers]:
    """
    Get a list of all workers.

    Returns:
        list[GetWorkerWorkers]: A list of GetWorkerWorkers objects with status, queues,
        and job counts of a single worker.

    Raises:
        HTTPException: If an error occurs while retrieving the worker list.
    """
    return await worker_controller.get_worker_list()

pause_worker(name) async

Pauses a single worker.

Parameters:

Name Type Description Default
name str

The name of the worker.

required

Raises:

Type Description
HTTPException

If the worker name does not exist.

Source code in src/mmisp/worker/api/worker_router.py
119
120
121
122
123
124
125
126
127
128
129
130
131
132
@worker_router.post("/pause/{name}", dependencies=[Depends(verified)])
async def pause_worker(name: str) -> None:
    """
    Pauses a single worker.

    Args:
        name (str): The name of the worker.

    Raises:
        HTTPException: If the worker name does not exist.
    """
    if not worker_controller.check_worker_name(name):
        raise HTTPException(status_code=404, detail=f'A worker with the name "{id}" does not exist.')
    await worker_controller.pause_worker(names=[name])

remove_queue(id, body, response) async

Removes an existing queue from a worker.

Parameters:

Name Type Description Default
id str

The id of the worker.

required
body RemoveAddQueueToWorker

The request body containing the queue name to remove.

required

Returns:

Type Description
None

None

Raises:

Type Description
HTTPException

If the worker or queue cannot be found or if an error occurs during queue removal.

Source code in src/mmisp/worker/api/worker_router.py
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
@worker_router.post("/removeQueue/{id}", dependencies=[Depends(verified)])
async def remove_queue(id: str, body: RemoveAddQueueToWorker, response: Response) -> None:
    """
    Removes an existing queue from a worker.

    Args:
        id (str): The id of the worker.
        body (RemoveAddQueueToWorker): The request body containing the queue name to remove.

    Returns:
        None

    Raises:
        HTTPException: If the worker or queue cannot be found or if an error occurs during queue removal.
    """
    if not worker_controller.check_worker_name(id):
        raise HTTPException(status_code=404, detail=f'A worker with the name "{id}" does not exist.')

    response.headers["x-queue-name-header"] = body.queue_name

    queues = await worker_controller.get_worker_queues(id)

    if body.queue_name not in queues:
        raise HTTPException(status_code=404, detail="Queue not found")

    await worker_controller.remove_queue_from_worker(id, body.queue_name)

unpause_worker(name) async

Unpauses a single worker.

Parameters:

Name Type Description Default
name str

The name of the worker.

required

Raises:

Type Description
HTTPException

If the worker name does not exist.

Source code in src/mmisp/worker/api/worker_router.py
135
136
137
138
139
140
141
142
143
144
145
146
147
148
@worker_router.post("/unpause/{name}", dependencies=[Depends(verified)])
async def unpause_worker(name: str) -> None:
    """
    Unpauses a single worker.

    Args:
        name (str): The name of the worker.

    Raises:
        HTTPException: If the worker name does not exist.
    """
    if not worker_controller.check_worker_name(name):
        raise HTTPException(status_code=404, detail=f'A worker with the name "{id}" does not exist.')
    await worker_controller.reset_worker_queues(names=[name])

Encapsulates API calls for jobs

verified(credentials=Depends(HTTPBearer(auto_error=False)))

A function to verify the api key that is sent by the client if the api key is not correct, it will raise an HTTPException

:param credentials: credentials sent by the client :type credentials: HTTPAuthorizationCredentials

Source code in src/mmisp/worker/api/api_verification.py
 7
 8
 9
10
11
12
13
14
15
16
17
18
def verified(credentials: HTTPAuthorizationCredentials = Depends(HTTPBearer(auto_error=False))) -> None:
    """
    A function to verify the api key that is sent by the client
    if the api key is not correct, it will raise an HTTPException

    :param credentials: credentials sent by the client
    :type credentials: HTTPAuthorizationCredentials
    """
    if credentials is not None:
        if credentials.credentials == system_config_data.api_key:
            return
    raise HTTPException(status_code=403, detail="authentication failed")