Skip to content
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

Add client tool to get single CVE #48

Merged
merged 1 commit into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ packages = [
]

[tool.poetry.scripts]
glvd = 'glvd.cli.client.__main__:main'
glvd-data = 'glvd.cli.data.__main__:main'

[tool.poetry.dependencies]
Expand Down
8 changes: 8 additions & 0 deletions src/glvd/cli/client/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# SPDX-License-Identifier: MIT

from __future__ import annotations

from ..registry import CliRegistry


cli = CliRegistry()
18 changes: 18 additions & 0 deletions src/glvd/cli/client/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# SPDX-License-Identifier: MIT

from __future__ import annotations

from . import cli

# Import to register all the commands
from . import ( # noqa: F401
cve,
)


def main() -> None:
cli.main()


if __name__ == '__main__':
main()
63 changes: 63 additions & 0 deletions src/glvd/cli/client/cve.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# SPDX-License-Identifier: MIT

from __future__ import annotations

import json
import logging
import sys
import urllib.parse

from ...util import requests
from . import cli


logger = logging.getLogger(__name__)


class ClientCve:
server: str

@staticmethod
@cli.register(
'cve',
arguments=[
cli.add_argument(
'cve',
help='the CVE to look up',
metavar='CVE',
),
cli.add_argument(
'--server',
default='http://localhost:5000',
help='the server to use',
),
cli.add_argument(
'--debug',
action='store_true',
help='enable debug output',
),
]
)
def run(cve: str, server: str, debug: bool) -> None:
logging.basicConfig(level=debug and logging.DEBUG or logging.INFO)
ClientCve(server)(cve)

def __init__(self, server: str) -> None:
self.server = server

def __call__(self, cve: str) -> None:
with requests.RetrySession() as rsession:
resp = rsession.get(
urllib.parse.urljoin(self.server, f'v1/cves/{cve}'),
)
if resp.status_code == 200:
data = resp.json()
json.dump(data, sys.stdout, indent=2)
elif resp.status_code == 404:
print(f'{cve} not found', file=sys.stderr)
else:
resp.raise_for_status()


if __name__ == '__main__':
ClientCve.run()
19 changes: 19 additions & 0 deletions tests/cli/client/test_cve.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# SPDX-License-Identifier: MIT

from glvd.cli.client.cve import ClientCve


class TestIngestNvd:
async def test_one(self, requests_mock):
requests_mock.get(
'http://localhost/v1/cves/TEST-0',
json=[
{
'id': 'TEST-0',
'lastModified': '2019-04-01T00:00:00',
},
],
)

client = ClientCve(server='http://localhost')
assert client(cve='TEST-0') is None
Loading