Skip to content

Commit

Permalink
Remove mongodb
Browse files Browse the repository at this point in the history
  • Loading branch information
antibagr committed Nov 24, 2023
1 parent a7aa7d6 commit 40e7eb1
Show file tree
Hide file tree
Showing 21 changed files with 720 additions and 638 deletions.
674 changes: 674 additions & 0 deletions LICENCE

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions app/dto/annotations.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import typing as t

TestSSLRecord = t.TypedDict(
"Record",
"TestSSLRecord",
{
"id": str,
"ip": str,
Expand All @@ -11,5 +11,5 @@
},
)

TestSSLRecords = list[TestSSLRecord]
TestSSLRecords: t.TypeAlias = list[TestSSLRecord]
Domain = t.NewType("Domain", str)
Empty file removed app/dto/constants.py
Empty file.
19 changes: 0 additions & 19 deletions app/dto/entities/base.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,13 @@
from __future__ import annotations

import datetime as dt
import typing as t

import bson
import pydantic
from bson.errors import InvalidId


class ObjectId(bson.ObjectId):
@classmethod
def __get_validators__(cls):
yield cls.validate

@classmethod
def validate(cls, v: t.Any) -> ObjectId:
try:
return cls(v)
except InvalidId as exc:
raise ValueError(f"{v} is not a valid ObjectId") from exc


class BaseModel(pydantic.BaseModel):
model_config = pydantic.ConfigDict(
validate_assignment=True,
json_encoders={
dt.datetime: dt.datetime.isoformat,
ObjectId: str,
set: list,
},
)
Expand Down
16 changes: 0 additions & 16 deletions app/dto/entities/collections.py

This file was deleted.

2 changes: 1 addition & 1 deletion app/dto/entities/fqdn.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import datetime as dt
import typing as t

from pydantic import ConfigDict, Field, field_serializer
Expand All @@ -7,6 +6,7 @@
from app.dto.entities.base import BaseModel


@t.final
class FQDN(BaseModel):
model_config = ConfigDict(from_attributes=True)
fqdn: str = Field(..., alias="fqdn")
Expand Down
14 changes: 0 additions & 14 deletions app/dto/enums.py

This file was deleted.

Empty file removed app/dto/exceptions.py
Empty file.
8 changes: 6 additions & 2 deletions app/lib/docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import json
import pathlib
import tarfile
import typing as t

import docker
import requests
Expand Down Expand Up @@ -46,8 +47,11 @@ def get_json(self) -> list[TestSSLRecord]:
mode="r",
) as tar:
try:
json_bytes: bytes = tar.extractfile(self._output_path.name).read()
if _file := tar.extractfile(self._output_path.name):
json_bytes = _file.read()
else:
raise KeyError
except KeyError:
logger.error("No file in tar archive")
return []
return json.loads(json_bytes)
return t.cast(list[TestSSLRecord], json.loads(json_bytes))
16 changes: 4 additions & 12 deletions app/lib/test_ssl_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,15 @@

from loguru import logger

from app.dto.annotations import TestSSLRecords
from app.dto.annotations import Domain, TestSSLRecords
from app.dto.entities.fqdn import FQDN

Protocols = ("SSLv2", "SSLv3", "TLS1", "TLS1_1", "TLS1_2", "TLS1_3")

logger.bind(context="TestSSL")


class JsonParserWarning(Warning):
"""
Warning indicating that the JSON parser has encountered an error.
"""
Protocols: t.Final = ("SSLv2", "SSLv3", "TLS1", "TLS1_1", "TLS1_2", "TLS1_3")


class TestSSLJsonParser:
def __init__(self) -> None:
self._data = []
self._data: TestSSLRecords = []

def set_data(self, *, data: TestSSLRecords) -> None:
self._data = data
Expand All @@ -45,6 +37,6 @@ def parse(self) -> t.Iterable[FQDN]:
if record["finding"] != "not offered":
fqdns[fqdn].supported_protocols.append(record["id"])
elif record["id"].startswith("cert_subjectAltName"):
fqdns[fqdn].alt_names |= set(record["finding"].split())
fqdns[fqdn].alt_names |= set(t.cast(list[Domain], record["finding"].split()))

return iter(fqdns.values())
3 changes: 0 additions & 3 deletions app/repository/db/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
import typing as t

from loguru import logger
from pymongo.mongo_client import MongoClient

from app.settings import settings


class BaseDB:
Expand Down
2 changes: 1 addition & 1 deletion app/repository/db/clickhouse.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def connect(self) -> None:
)

def disconnect(self) -> None:
return self._client.close()
self._client.close()

def is_alive(self) -> bool:
return self._client.ping()
15 changes: 1 addition & 14 deletions app/repository/db/fqdn.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,8 @@
from clickhouse_connect.driver.client import Client as ClickHouseClient
from clickhouse_connect.driver.insert import InsertContext
from loguru import logger
from pymongo.mongo_client import MongoClient

from app.dto.entities.collections import FQDNRepository, MongoFQDN
from app.dto.entities.fqdn import FQDN
from app.repository.db.clickhouse import ClickHouseDB
from app.repository.db.mongo import MongoDB


class MongoFQDNDB(MongoDB):
def __init__(self, *, client: MongoClient, database: str) -> None:
super().__init__(client=client, database=database)
self._repository = FQDNRepository(database=self._database)

def save_fqdn(self, *, fqdn: MongoFQDN) -> None:
self._repository.save(fqdn)


class ClickHouseFQDNDB(ClickHouseDB):
Expand All @@ -26,7 +13,7 @@ def __init__(
table_name: str,
) -> None:
super().__init__(client=client, table_name=table_name)
self._context = None
self._context: InsertContext = None # type: ignore

@property
def context(self) -> InsertContext:
Expand Down
24 changes: 0 additions & 24 deletions app/repository/db/mongo.py

This file was deleted.

4 changes: 2 additions & 2 deletions app/repository/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,5 @@ def get(self) -> t.Generator[FQDN, None, None]:
fqdn_list.append(fqdn.fqdn)
yield fqdn
finally:
for fqdn in self.get_missing_fqdn(parsed=fqdn_list):
logger.error(f"Failed to parse {fqdn}")
for missing_fqdn in self.get_missing_fqdn(parsed=fqdn_list):
logger.error(f"Failed to parse {missing_fqdn}")
44 changes: 0 additions & 44 deletions app/services/liveness_probe.py

This file was deleted.

15 changes: 2 additions & 13 deletions app/services/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import clickhouse_connect
import docker
from loguru import logger
from pymongo.mongo_client import MongoClient

from app.lib.docker import TestSSLContainer
from app.lib.test_ssl_parser import TestSSLJsonParser
Expand Down Expand Up @@ -48,22 +47,12 @@


def startup() -> None:
logger.info("starting up")
logger.info(f"database status: {db.is_alive()}")
logger.info("Starting up")
db.connect()
from app.dto.entities.fqdn import FQDN

db.save_fqdn(
fqdn=FQDN(
fqdn="test.com",
alt_names=["test.com", "test2.com"],
supported_protocols=["TLSv1.2", "TLSv1.3"],
)
)


def shutdown() -> None:
logger.info("shutting down")
logger.info("Shutting down")
test_ssl_container.stop()
db.disconnect()

Expand Down
5 changes: 1 addition & 4 deletions app/services/ssl_checker.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import typing as t

import attrs
import docker
from loguru import logger

from app.dto.entities.fqdn import FQDN
from app.repository.db import DB

logger.bind(context="ssl_checker")


class DataProviderRepositoryInterface(t.Protocol):
def get(self) -> t.Generator[FQDN, None, None]:
Expand All @@ -22,7 +19,7 @@ class SSLCheckerService:
_db: DB

def run(self) -> None:
logger.info("running ssl checker")
logger.info("Starting SSL Checker")
for fqdn in self._data_provider_repo.get():
logger.info(f"Adding {fqdn}")
self._db.save_fqdn(fqdn=fqdn)
3 changes: 0 additions & 3 deletions app/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ class Settings(BaseSettings):
ENVIRONMENT: str
DEBUG: bool

MONGO_DB_URI: str # MongoDsn adds a port number to the URI, which is not supported by the MongoDB client
MONGO_DB_NAME: str

TEST_SSL_CONTAINER_NAME: str
TEST_SSL_OUTPUT_FILE: pathlib.Path
TEST_SSL_INPUT_FILE: pathlib.Path
Expand Down
Loading

0 comments on commit 40e7eb1

Please sign in to comment.