74 lines
2.5 KiB
Python
Executable File
74 lines
2.5 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
"""Проверка входа в Matrix: Synapse, SSO-редирект и authorize в Authentik.
|
|
|
|
Ловушка, которую ловит: у OAuth2-провайдера Authentik может не быть
|
|
привязанного приложения — тогда /application/o/authorize/ отдаёт 5xx,
|
|
и вход в Element молча ломается (симптом «Matrix пропал»).
|
|
"""
|
|
import json
|
|
import sys
|
|
import urllib.request as u
|
|
|
|
HUB = "http://127.0.0.1:9120"
|
|
CID = "kEkzD2jN1Pgmg2wrCjzCXDuDcl4pD7p9yDbX81d3"
|
|
RED = "https://matrix.buryans.ru/_synapse/client/oidc/callback"
|
|
|
|
CHECKS = [
|
|
("synapse versions", "https://matrix.buryans.ru/_matrix/client/versions", (200,)),
|
|
("sso redirect", "https://matrix.buryans.ru/_matrix/client/v3/login/sso/redirect/oidc-authentik?redirectUrl=https%3A%2F%2Felement.buryans.ru%2F", (302,)),
|
|
("authentik authorize",
|
|
"https://authentik.buryans.ru/application/o/authorize/?client_id=%s&response_type=code"
|
|
"&scope=openid&redirect_uri=%s&state=t" % (CID, RED), (200, 302)),
|
|
("element", "https://element.buryans.ru/", (200, 302)),
|
|
]
|
|
|
|
|
|
class NoRedirect(u.HTTPRedirectHandler):
|
|
"""Редирект — это ожидаемый результат, а не ошибка: не идём по нему."""
|
|
|
|
def redirect_request(self, req, fp, code, msg, headers, newurl):
|
|
return None
|
|
|
|
|
|
OPENER = u.build_opener(NoRedirect)
|
|
|
|
|
|
def code(url, timeout=15):
|
|
req = u.Request(url, headers={"User-Agent": "hub-check/1"})
|
|
try:
|
|
with OPENER.open(req, timeout=timeout) as r:
|
|
return r.status
|
|
except u.HTTPError as e:
|
|
return e.code
|
|
except Exception as e:
|
|
return "ERR:" + str(e)[:40]
|
|
|
|
|
|
def notify(message, priority="high"):
|
|
body = json.dumps({"title": "Matrix/SSO", "message": message,
|
|
"priority": priority}).encode()
|
|
req = u.Request(HUB + "/notify", data=body, headers={"Content-Type": "application/json"})
|
|
try:
|
|
with u.urlopen(req, timeout=10) as r:
|
|
return r.status
|
|
except Exception:
|
|
return 0
|
|
|
|
|
|
def main():
|
|
bad = []
|
|
for name, url, ok in CHECKS:
|
|
c = code(url)
|
|
mark = "ok" if c in ok else "СБОЙ"
|
|
print("%-22s %s (%s)" % (name, c, mark))
|
|
if c not in ok:
|
|
bad.append("%s → %s" % (name, c))
|
|
if bad:
|
|
notify("🔴 Вход в Matrix сломан: " + "; ".join(bad), "high")
|
|
return 1
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|