summaryrefslogtreecommitdiff
path: root/src/main.py
blob: 9beace0991246dde53ae5c150cf724daf79fcbef (plain)
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import os
import sys
import uvicorn

from fastapi import FastAPI, Depends, Request
from fastapi.responses import JSONResponse
from fastapi_jwt_auth import AuthJWT
from fastapi_jwt_auth.exceptions import AuthJWTException
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
from index import CouchIindex
import time
from db import DictDB
import requests

app = FastAPI()

app.add_middleware(
    CORSMiddleware,
    allow_origins=["http://localhost:8001"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
    expose_headers=["X-Total-Count"],
)

# TODO: X-Total-Count


@app.middleware("http")
async def mock_x_total_count_header(request: Request, call_next):
    response = await call_next(request)
    response.headers["X-Total-Count"] = "100"
    return response

for i in range(10):
    try:
        db = DictDB()
    except requests.exceptions.ConnectionError:
        print(f'Database not responding, will try again soon.' +
              'Attempt {i + 1} of 10.')
    else:
        break
    time.sleep(10)
else:
    print('Database did not respond after 10 attempts, quitting.')
    sys.exit(-1)


def get_pubkey():
    try:
        if 'JWT_PUBKEY_PATH' in os.environ:
            keypath = os.environ['JWT_PUBKEY_PATH']
        else:
            keypath = '/opt/certs/public.pem'

        with open(keypath, "r") as fd:
            pubkey = fd.read()
    except FileNotFoundError:
        print(f"Could not find JWT certificate in {keypath}")
        sys.exit(-1)

    return pubkey


def get_data(key=None, limit=25, skip=0, ip=None,
             port=None, asn=None, domain=None):
    if key:
        return db.get(key)

    selectors = dict()
    indexes = CouchIindex().dict()
    selectors['domain'] = domain

    if ip and 'ip' in indexes:
        selectors['ip'] = ip
    if port and 'port' in indexes:
        selectors['port'] = port
    if asn and 'asn' in indexes:
        selectors['asn'] = asn

    data = db.search(**selectors, limit=limit, skip=skip)

    return data


class JWTConfig(BaseModel):
    authjwt_algorithm: str = "ES256"
    authjwt_public_key: str = get_pubkey()


@AuthJWT.load_config
def jwt_config():
    return JWTConfig()


@app.exception_handler(AuthJWTException)
def authjwt_exception_handler(request: Request, exc: AuthJWTException):
    return JSONResponse(content={"status": "error", "message":
                                 exc.message}, status_code=400)


@app.exception_handler(RuntimeError)
def app_exception_handler(request: Request, exc: RuntimeError):
    return JSONResponse(content={"status": "error", "message":
                                 str(exc.with_traceback(None))},
                        status_code=400)


@app.get('/sc/v0/get')
async def get(key=None, limit=25, skip=0, ip=None, port=None,
              asn=None, Authorize: AuthJWT = Depends()):

    Authorize.jwt_required()

    data = []
    raw_jwt = Authorize.get_raw_jwt()

    if "read" not in raw_jwt:
        return JSONResponse(
            content={
                "status": "error",
                "message": "Could not find read claim in JWT token",
            },
            status_code=400,
        )
    else:
        domains = raw_jwt["read"]

    for domain in domains:
        data.extend(get_data(key, limit, skip, ip, port, asn, domain))

    return JSONResponse(content={"status": "success", "docs": data})


@app.get('/sc/v0/get/{key}')
async def get_key(key=None, Authorize: AuthJWT = Depends()):

    Authorize.jwt_required()

    raw_jwt = Authorize.get_raw_jwt()

    if "read" not in raw_jwt:
        return JSONResponse(
            content={
                "status": "error",
                "message": "Could not find read claim in JWT token",
            },
            status_code=400,
        )
    else:
        allowed_domains = raw_jwt["read"]

    data = get_data(key)

    if data["domain"] not in allowed_domains:
        return JSONResponse(
            content={
                "status": "error",
                "message": "User not authorized to view this object",
            },
            status_code=400,
        )

    return JSONResponse(content={"status": "success", "docs": data})


@app.post('/sc/v0/add')
async def add(data: Request, Authorize: AuthJWT = Depends()):

    # Maybe we should protect this enpoint too and let the scanner use
    # a JWT token as well.
    # Authorize.jwt_required()

    json_data = await data.json()

    key = db.add(json_data)

    return JSONResponse(content={"status": "success", "docs": key})


@app.delete('/sc/v0/delete/{key}')
async def delete(key, Authorize: AuthJWT = Depends()):

    Authorize.jwt_required()

    raw_jwt = Authorize.get_raw_jwt()

    if "write" not in raw_jwt:
        return JSONResponse(
            content={
                "status": "error",
                "message": "Could not find write claim in JWT token",
            },
            status_code=400,
        )
    else:
        allowed_domains = raw_jwt["write"]

    data = get_data(key)

    if data["domain"] not in allowed_domains:
        return JSONResponse(
            content={
                "status": "error",
                "message": "User not authorized to delete this object",
            },
            status_code=400,
        )

    if db.delete(key) is None:
        return JSONResponse(content={"status": "error",
                                     "message": "Document not found"},
                            status_code=400)

    return JSONResponse(content={"status": "success", "docs": data})


def main(standalone=False):
    if not standalone:
        return app

    uvicorn.run(app, host="0.0.0.0", port=8000, log_level="debug")


if __name__ == '__main__':
    main(standalone=True)
else:
    app = main()