Skip to content

Commit

Permalink
new: [website] Added first version of the API endpoint in order to st…
Browse files Browse the repository at this point in the history
…ore status from the Fediverse in kvrocks.
  • Loading branch information
cedricbonhomme committed Nov 4, 2024
1 parent cbff442 commit a0cd2e9
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
2 changes: 2 additions & 0 deletions website/web/api/v1/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ def custom_ui() -> str:
from website.web.api.v1 import bundle
from website.web.api.v1 import epss
from website.web.api.v1 import sighting
from website.web.api.v1 import fediverse

api.add_namespace(base.default_ns, path="/")
api.add_namespace(base.api_ns, path="/api")
Expand All @@ -68,6 +69,7 @@ def custom_ui() -> str:
api.add_namespace(bundle.bundle_ns, path="/api")
api.add_namespace(sighting.sighting_ns, path="/api")
api.add_namespace(epss.epss_ns, path="/api")
api.add_namespace(fediverse.status_ns, path="/api")

return api

Expand Down
74 changes: 74 additions & 0 deletions website/web/api/v1/fediverse.py
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

0 comments on commit a0cd2e9

Please sign in to comment.