Skip to content

Commit

Permalink
Add login with invalid auth error
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexxIT committed Apr 12, 2024
1 parent 930060a commit 9007dbc
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
4 changes: 4 additions & 0 deletions custom_components/hass_diagnostics/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ def extra_state_attributes(self):
r"\b(connect|connection|disconnected|socket|timed out)\b", flags=re.IGNORECASE
)
RE_IP = re.compile(r"\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b")
RE_LOGIN = re.compile(r"^Login attempt or request [^(]+\(([^)]+)")
RE_LAST_LINE = re.compile(r"\n\S+Error: ([^\n]+)\n$")


Expand Down Expand Up @@ -135,6 +136,9 @@ def parse_log_record(record: logging.LogRecord) -> dict:
elif m := RE_TEMPLATE.search(message):
entry["category"] = "template"
short = m[1]
elif m := RE_LOGIN.search(message):
entry["category"] = "login"
entry["host"] = m[1]
elif m := RE_LAST_LINE.search(text):
short = m[1]
if RE_CONNECT.search(short) and (m := RE_IP.search(short)):
Expand Down
26 changes: 26 additions & 0 deletions tests/test_auth.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from . import parse_log_entry


def test_ban():
entry = {
"name": "homeassistant.components.http.ban",
"message": [
"Login attempt or request with invalid authentication from MACM1 (192.168.1.123). Requested URL: '/api/websocket'. (Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36)"
],
"level": "WARNING",
"source": [
"C:\\venv\\hass-py312\\Lib\\site-packages\\homeassistant\\components\\http\\ban.py",
138,
],
"timestamp": 1712904723.8199923,
"exception": "",
"count": 2,
"first_occurred": 1712904722.8587666,
}
assert parse_log_entry(entry) == {
"category": "login",
"domain": "http",
"host": "192.168.1.123",
"name": "homeassistant.components.http.ban",
"short": "Login attempt or request with invalid authentication from M...",
}

0 comments on commit 9007dbc

Please sign in to comment.