argos/argos/checks/checks.py
Alexis Métaireau 43f8aabb2c Refactor server codebase for testing.
- Restructured server module to separate the application creation and configuration.
- Moved code dealing with SQLAlchemy database
  setup and teardown to the main application file.
- Moved functions related to configuration file loading to `argos.server.settings`.
- Fixed SQLAchemy expressions in `argos.server.queries`.
- Implemented a more granular system of setting checks' schedule on the server.
- Introduced frequency scheduling on per-website basis in the YAML config.
- Introduced Pytest fixtures for handling test database and authorized HTTP client in `tests/conftest.py`.
- Included a first test for the api
- Implemented changes to models to accommodate changes to task scheduling.
- Fixed errors concerning database concurrency arising from changes to the application setup.
2023-10-11 23:52:33 +02:00

63 lines
2 KiB
Python

import ssl
import time
from datetime import datetime
from OpenSSL import crypto
from argos.checks.base import (BaseCheck, ExpectedIntValue,
ExpectedStringValue, Response, Severity, Status)
from argos.logging import logger
class HTTPStatus(BaseCheck):
config = "status-is"
expected_cls = ExpectedIntValue
async def run(self) -> dict:
# XXX Get the method from the task
task = self.task
response = await self.http_client.request(method="get", url=task.url)
return self.response(
status=response.status_code == self.expected,
expected=self.expected,
retrieved=response.status_code,
)
class HTTPBodyContains(BaseCheck):
config = "body-contains"
expected_cls = ExpectedStringValue
async def run(self) -> dict:
response = await self.http_client.request(method="get", url=self.task.url)
return self.response(status=self.expected in response.text)
class SSLCertificateExpiration(BaseCheck):
config = "ssl-certificate-expiration"
expected_cls = ExpectedStringValue
async def run(self):
"""Returns the number of days in which the certificate will expire."""
response = await self.http_client.get(self.task.url)
if response.is_error:
raise
network_stream = ssl_object = response.extensions["network_stream"]
ssl_obj = network_stream.get_extra_info("ssl_object")
cert = ssl_obj.getpeercert()
not_after = datetime.strptime(cert.get("notAfter"), "%b %d %H:%M:%S %Y %Z")
expires_in = (not_after - datetime.now()).days
return self.response(status=Status.ON_CHECK, expires_in=expires_in)
@classmethod
async def finalize(cls, config, result, expires_in):
thresholds = config.ssl.thresholds
thresholds.sort()
for days, severity in thresholds:
if expires_in < days:
return Status.FAILURE, severity
return Status.SUCCESS, Severity.OK