-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
new: [website] Added first version of the API endpoint in order to st… #81
Draft
cedricbonhomme
wants to merge
19
commits into
main
Choose a base branch
from
fediverse
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 5 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
a0cd2e9
new: [website] Added first version of the API endpoint in order to st…
cedricbonhomme 38abeda
chg: [typing] Make Mypy Happy Again.
cedricbonhomme ce9ba5a
chg: [website] Added GET List route to the new API endpoint dedicated…
cedricbonhomme ecb2dfc
Merge branch 'main' into fediverse
cedricbonhomme 7ea82e9
chg: [website] Open the tab specified with an anchor in the URL.
cedricbonhomme f78fce1
Merge branch 'main' into fediverse
cedricbonhomme 2a3e5de
Merge branch 'main' into fediverse
cedricbonhomme 395b407
chg: [website] new parser for GET LIST on /api/status
cedricbonhomme d811fae
Merge branch 'main' into fediverse
cedricbonhomme c4f95f5
chg: [website] returns list of status with vuln_id as parameter
cedricbonhomme 98e93c2
Merge branch 'main' into fediverse
cedricbonhomme dcdf9a7
o
cedricbonhomme 69b7b46
o
cedricbonhomme 7244db7
chg: [typing] Make Mypy happy Again.
cedricbonhomme 8d5dc2d
Merge branch 'main' into fediverse
cedricbonhomme c59d621
chg: [website] Let admins save a bio up to 5000 characters.
cedricbonhomme caceb6f
chg: [website] Let admins save a bio up to 5000 characters.
cedricbonhomme c609a29
chg: completed merge.
cedricbonhomme a09291d
Merge branch 'main' into fediverse
cedricbonhomme File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,103 @@ | ||
from typing import Tuple | ||
from datetime import datetime | ||
|
||
import logging | ||
import orjson | ||
from flask_restx import Namespace # type: ignore[import-untyped] | ||
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.doc("list_status") # type: ignore[misc] | ||
# @status_ns.marshal_list_with(status_list_fields) # type: ignore[misc] | ||
def get(self) -> Tuple[ResultType, int]: | ||
"""List all statuses.""" | ||
args = parser.parse_args() | ||
offset = args.pop("page", 1) - 1 | ||
limit = args.pop("per_page", 1000) | ||
vuln_id = args.pop("vuln_id", None) | ||
|
||
result: ResultType = { | ||
"data": [], | ||
"metadata": { | ||
"count": 0, | ||
"offset": offset, | ||
"limit": limit, | ||
}, | ||
} | ||
|
||
# all_entries = storage.zrange(f"status:{vuln_id}", 0, -1, withscores=True) | ||
# latest_entries = r.zrevrange(f"status:{vuln_id}", 0, 4, withscores=True) | ||
for vuln_id in storage.zrevrange(f"status:{vuln_id}", offset, limit, withscores=True): | ||
if vuln := storage.get(vuln_id): | ||
result["data"].append(vuln) | ||
|
||
return result, 200 | ||
|
||
|
||
@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_sighting_vulns", vuln_id, 1) | ||
p.zadd(f"status:{vuln_id}", ids) # type: ignore | ||
|
||
|
||
# p.hincrby("top:{vuln_id}", date.strftime("%Y%m%d"), 1) | ||
|
||
# p.zadd(f"index:{source}", ids) # type: ignore | ||
# p.zadd("index", ids) # type: ignore | ||
|
||
# p.zadd(f"top_status:{vuln_id}", {uri: float(nb_items)}) # type: ignore | ||
|
||
p.execute() | ||
|
||
return status |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Valid day?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
haha, it comes from previous tests of the switch statement. I just removed this console.log. Now it opens the tag of related vulnerabilities, in case the anchor in not correct.