-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
200 additions
and
19 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,4 @@ build/ | |
dist/ | ||
*.spec | ||
.venv/ | ||
.coverage |
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,31 @@ | ||
[project] | ||
name = "signalilo-scrubbed" | ||
description = "Signalilo with alert scrubber included" | ||
version = "0.0.0" | ||
authors = [ | ||
{ name = "Adfinis", email = "[email protected]" } | ||
] | ||
|
||
dependencies = [ | ||
"Flask==3.0.3", | ||
"requests==2.32.3", | ||
"pyinstaller==6.8.0", | ||
"waitress==3.0.0", | ||
] | ||
|
||
[project.optional-dependencies] | ||
test = [ | ||
"ruff==0.5.0", | ||
"types-waitress==3.0.0.20240423", | ||
"types-requests==2.32.0.20240622", | ||
"pytest==8.2.2", | ||
"pytest-cov==5.0.0", | ||
"requests-mock==1.12.1", | ||
] | ||
|
||
[tool.flit.module] | ||
name = "scrubbed" | ||
|
||
[build-system] | ||
build-backend = "flit_core.buildapi" | ||
requires = ["flit_core >=3.2,<4"] |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Empty file.
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,128 @@ | ||
import json | ||
|
||
import pytest | ||
from werkzeug.test import Client | ||
|
||
from scrubbed.main import app | ||
|
||
|
||
@pytest.fixture() | ||
def client() -> Client: | ||
"""Test client for calling server endpoints.""" | ||
return Client(app) | ||
|
||
|
||
def test_invalid_get(client): | ||
response = client.get("/webhook") | ||
|
||
assert response.status == "405 METHOD NOT ALLOWED" | ||
assert response.content_type == "text/html; charset=utf-8" | ||
|
||
|
||
def test_invalid_content_type(client): | ||
response = client.post( | ||
"/webhook", content_type="text/html", data="<h1>Hello World!</h1>" | ||
) | ||
|
||
assert response.status == "400 BAD REQUEST" | ||
assert response.content_type == "application/json" | ||
assert response.json.get("message") == "request must be in JSON format" | ||
assert response.json.get("status") == "error" | ||
|
||
|
||
def test_invalid_data(client): | ||
response = client.post( | ||
"/webhook", content_type="application/json", data="<h1>Hello World!</h1>" | ||
) | ||
|
||
assert response.status == "500 INTERNAL SERVER ERROR" | ||
assert response.content_type == "application/json" | ||
assert ( | ||
response.json.get("message") | ||
== "400 Bad Request: The browser (or proxy) sent a request that this server could not understand." # noqa: E501 | ||
) | ||
assert response.json.get("status") == "error" | ||
|
||
|
||
def test_scrubbing_alert(requests_mock, client): | ||
upstream_request = requests_mock.post("http://localhost:6725") | ||
|
||
alerts = { | ||
"version": "4", | ||
"groupKey": "groupkey", | ||
"truncatedAlerts": 0, | ||
"status": "firing", | ||
"receiver": "test", | ||
"groupLabels": { | ||
"KEY": "SECRET", | ||
}, | ||
"commonLabels": { | ||
"KEY": "SECRET", | ||
}, | ||
"commonAnnotations": { | ||
"KEY": "SECRET", | ||
}, | ||
"externalURL": "https://SECRET.alertmanager.example.com", | ||
"alerts": [ | ||
{ | ||
"status": "firing", | ||
"labels": {"KEY": "SECRET"}, | ||
"annotations": {"KEY": "SECRET"}, | ||
"startsAt": "<rfc3339>", | ||
"endsAt": "<rfc3339>", | ||
"generatorURL": "https://SECRET.generator.example.com", | ||
"fingerprint": "fingerprint", | ||
} | ||
], | ||
} | ||
response = client.post( | ||
"/webhook", | ||
content_type="application/json", | ||
headers={ | ||
"KEY": "SECRET", | ||
}, | ||
data=json.dumps(alerts), | ||
) | ||
|
||
assert response.status == "200 OK" | ||
assert response.content_type == "application/json" | ||
assert upstream_request.call_count == 1 | ||
assert "SECRET" not in upstream_request.last_request.text | ||
assert upstream_request.last_request.json() == { | ||
"version": "4", | ||
"groupKey": "REDACTED", | ||
"truncatedAlerts": 0, | ||
"status": "firing", | ||
"receiver": "test", | ||
"groupLabels": { | ||
"KEY": "REDACTED", | ||
}, | ||
"commonLabels": { | ||
"KEY": "REDACTED", | ||
}, | ||
"commonAnnotations": { | ||
"KEY": "REDACTED", | ||
}, | ||
"externalURL": "REDACTED", | ||
"alerts": [ | ||
{ | ||
"annotations": { | ||
"KEY": "REDACTED", | ||
}, | ||
"endsAt": "<rfc3339>", | ||
"fingerprint": "fingerprint", | ||
"generatorURL": "REDACTED", | ||
"labels": { | ||
"KEY": "REDACTED", | ||
}, | ||
"startsAt": "<rfc3339>", | ||
"status": "firing", | ||
}, | ||
], | ||
} | ||
assert upstream_request.last_request.headers == { | ||
"Host": "localhost", | ||
"Content-Type": "application/json", | ||
"Content-Length": "451", | ||
"Key": "SECRET", # TODO: redact this? | ||
Check failure on line 127 in tests/test_webhook.py GitHub Actions / ruffRuff (TD002)
|
||
} |