-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.py
More file actions
57 lines (42 loc) · 1.36 KB
/
Copy pathdb.py
File metadata and controls
57 lines (42 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import environ
import structlog
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine
from models import HTTPRequest, SSHRequest
DATABASE_FILE = "/data/app.db"
log = structlog.get_logger(__name__)
env = environ.Env()
ECHO_SQL_QUERIES = env("ECHO_SQL_QUERIES", default=False, cast=bool)
SQL_QUERY_TIMEOUT = env("SQL_QUERY_TIMEOUT", default=30, cast=int)
async def _persist_request(request):
engine = create_async_engine(
f"sqlite+aiosqlite:///{DATABASE_FILE}",
echo=ECHO_SQL_QUERIES,
connect_args={"timeout": SQL_QUERY_TIMEOUT},
)
async with AsyncSession(engine) as session:
async with session.begin():
session.add(request)
await session.commit()
await engine.dispose()
async def create_http_request_database_entry(
path, method, remote_ip, started, finished
):
request = HTTPRequest(
path=path,
method=method,
remote_ip=remote_ip,
request_started=started,
request_finished=finished,
)
await _persist_request(request)
async def create_ssh_request_database_entry(
username, password, remote_ip, started, finished
):
request = SSHRequest(
username=username,
password=password,
remote_ip=remote_ip,
request_started=started,
request_finished=finished,
)
await _persist_request(request)