-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
new: [website] Added first version of the API endpoint in order to st…
…ore status from the Fediverse in kvrocks.
- Loading branch information
1 parent
cbff442
commit a0cd2e9
Showing
2 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
from typing import Tuple | ||
from datetime import datetime | ||
|
||
import logging | ||
import orjson | ||
from flask_restx import Namespace | ||
from flask_restx import reqparse | ||
from flask_restx import Resource | ||
from redis import Redis | ||
|
||
from vulnerabilitylookup.default import get_config | ||
from website.web.api.v1.common import auth_func | ||
from website.web.api.v1.types import ResultType | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
status_ns = Namespace("status", description="status related operations") | ||
|
||
storage = Redis( | ||
host=get_config("generic", "storage_db_hostname"), | ||
port=get_config("generic", "storage_db_port"), | ||
) | ||
|
||
|
||
# Argument Parsing | ||
parser = reqparse.RequestParser() | ||
parser.add_argument( | ||
"data", | ||
type=dict, | ||
location="json", | ||
help="The JSON data of the status.", | ||
) | ||
|
||
|
||
@status_ns.route("/status/") | ||
class StatusList(Resource): # type: ignore[misc] | ||
@status_ns.expect(parser) # type: ignore[misc] | ||
@auth_func | ||
def post(self) -> Tuple[ResultType, int]: | ||
"""Create a new status.""" | ||
result: ResultType = { | ||
"data": [], | ||
"metadata": { | ||
"count": 0, | ||
"offset": 0, | ||
"limit": 10, | ||
}, | ||
} | ||
|
||
date = datetime.now() | ||
|
||
ids: dict[str, float] = {} | ||
|
||
status = status_ns.payload["status"] | ||
vuln_id = status_ns.payload["vuln_id"] | ||
|
||
uri = status["url"] | ||
ids[uri] = status["created_at"] | ||
|
||
# Store the status in kvrocks | ||
p = storage.pipeline() | ||
p.set(uri, orjson.dumps(status)) | ||
|
||
# p.hincrby("top:{vuln_id}", date.strftime("%Y%m%d"), 1) | ||
p.hincrby("top_sighting_vulns", vuln_id, 1) | ||
|
||
# p.zadd(f"index:{source}", ids) # type: ignore | ||
# p.zadd("index", ids) # type: ignore | ||
p.zadd(f"status:{vuln_id}", ids) # type: ignore | ||
# p.zadd(f"top_status:{vuln_id}", {uri: float(nb_items)}) # type: ignore | ||
|
||
p.execute() | ||
|
||
return status |