Skip to content
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 50 additions & 1 deletion src/murfey/server/api/session_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
from logging import getLogger
from pathlib import Path
from typing import Dict, List, Optional
import requests

from fastapi import APIRouter, Depends, Request
from fastapi.responses import FileResponse
from fastapi.responses import FileResponse, JSONResponse
from pydantic import BaseModel
from sqlmodel import select

Expand Down Expand Up @@ -470,3 +471,51 @@
visit_name=visit_name, session_id=session_id, tiff_path=tiff_path, db=db
)
return FileResponse(path=tiff_file) if isinstance(tiff_file, Path) else tiff_file

#Methods for turning alerts on and off

@router.get("/silences/{instrument_name}")
def get_silences(instrument_name: MurfeyInstrumentName):
machine_config = machine_info_by_instrument(instrument_name)
alertmanager_url = machine_config.alertmanager_url
silences = requests.get(f"{alertmanager_url}/api/v2/silences?filter=microscope={sanitise(instrument_name)}")

Check failure

Code scanning / CodeQL

Partial server-side request forgery Critical

Part of the URL of this request depends on a
user-provided value
.
Part of the URL of this request depends on a
user-provided value
.
Comment thread
github-advanced-security[bot] marked this conversation as resolved.
Fixed
active_silences = []
for silence in silences.json():
if silence['status']['state'] == 'active':
active_silences.append(silence)
return (active_silences)

@router.post("/silences/{instrument_name}")
def create_silence(instrument_name: MurfeyInstrumentName, end_time: datetime ):
machine_config = machine_info_by_instrument(instrument_name)
alertmanager_url = machine_config.alertmanager_url
start_time = datetime.now().astimezone().isoformat()
end_time = end_time.astimezone().isoformat()
silence_json = {
"matchers":[
{
"name": "microscope",
"value": instrument_name,
"isRegex": False
}],
"createdBy": "murfey",
"annotations":{"description": "Test"},
"comment": "silence created from murfey",
"status": {"state": "active"},
"startsAt": str(start_time),
"endsAt": str(end_time)
}
response = requests.post(f"{alertmanager_url}/api/v2/silences", json=silence_json)
return JSONResponse(status_code=response.status_code, content=response.json()) #return a response with same data and code as from alertmanager

@router.delete("/silences/{instrument_name}") #delete all silences for given microscope
def delete_silences(instrument_name: MurfeyInstrumentName):
machine_config = machine_info_by_instrument(instrument_name)
alertmanager_url = machine_config.alertmanager_url
silences = get_silences(instrument_name)
if len(silences) == 0:
return None
for silence in silences:
id = silence['id']
response = requests.delete(f"{alertmanager_url}/api/v2/silence/{id}")

Check failure

Code scanning / CodeQL

Partial server-side request forgery Critical

Part of the URL of this request depends on a
user-provided value
.
Part of the URL of this request depends on a
user-provided value
.
Comment thread
github-advanced-security[bot] marked this conversation as resolved.
Fixed
return response #returns final response in loop
3 changes: 3 additions & 0 deletions src/murfey/util/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ class MachineConfig(BaseModel): # type: ignore
# Pydantic BaseModel settings
model_config = ConfigDict(extra="allow")

# Alertmanager URL
alertmanager_url: str = "https://murfey-alertmanager.diamond.ac.uk"
Comment thread
stephen-riggs marked this conversation as resolved.
Outdated

@field_validator("calibrations", mode="before")
@classmethod
def validate_calibration_data(
Expand Down