From dc3a8ccc4599b5b4bb39f175f592bc2f556fd0ae Mon Sep 17 00:00:00 2001 From: Paul Tikken Laptop Date: Tue, 7 Sep 2021 12:55:48 +0000 Subject: [PATCH 1/3] Added schema_version handling and created cli command to show the current database sources for update --- CveXplore/.sources.ini | 1 + 1 file changed, 1 insertion(+) create mode 100644 CveXplore/.sources.ini diff --git a/CveXplore/.sources.ini b/CveXplore/.sources.ini new file mode 100644 index 00000000..46ca52c4 --- /dev/null +++ b/CveXplore/.sources.ini @@ -0,0 +1 @@ +{"cve": "https://nvd.nist.gov/feeds/json/cve/1.1/", "cpe": "https://nvd.nist.gov/feeds/json/cpematch/1.0/nvdcpematch-1.0.json.zip", "cwe": "https://cwe.mitre.org/data/xml/cwec_v4.4.xml.zip", "capec": "https://capec.mitre.org/data/xml/capec_v3.5.xml", "via4": "https://www.cve-search.org/feeds/via4.json"} \ No newline at end of file From ce706a723094743ec8ce3406236d01c4c5ee9ae9 Mon Sep 17 00:00:00 2001 From: Paul Tikken Laptop Date: Tue, 7 Sep 2021 13:02:13 +0000 Subject: [PATCH 2/3] Added schema_version handling and created cli command to show the current database sources for update --- CveXplore/VERSION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CveXplore/VERSION b/CveXplore/VERSION index 088a324e..44b02c75 100644 --- a/CveXplore/VERSION +++ b/CveXplore/VERSION @@ -1 +1 @@ -0.2.6.1 \ No newline at end of file +0.2.6.1.dev5 \ No newline at end of file From 100e636eee24737b337626c1e28dde0e374903d5 Mon Sep 17 00:00:00 2001 From: Paul Tikken Laptop Date: Tue, 7 Sep 2021 13:27:09 +0000 Subject: [PATCH 3/3] Created possibility to set or delete sources from cli --- CveXplore/VERSION | 2 +- CveXplore/cli_cmds/db_cmds/commands.py | 53 ++++++++++++++++++++-- CveXplore/cli_cmds/search_cmds/commands.py | 12 +++-- CveXplore/database/maintenance/Config.py | 17 ++++--- setup.py | 2 +- 5 files changed, 70 insertions(+), 16 deletions(-) diff --git a/CveXplore/VERSION b/CveXplore/VERSION index 44b02c75..e6753ce4 100644 --- a/CveXplore/VERSION +++ b/CveXplore/VERSION @@ -1 +1 @@ -0.2.6.1.dev5 \ No newline at end of file +0.2.6.1.dev6 \ No newline at end of file diff --git a/CveXplore/cli_cmds/db_cmds/commands.py b/CveXplore/cli_cmds/db_cmds/commands.py index ac0c1995..be370839 100644 --- a/CveXplore/cli_cmds/db_cmds/commands.py +++ b/CveXplore/cli_cmds/db_cmds/commands.py @@ -1,7 +1,10 @@ +import json +import os + import click from CveXplore.cli_cmds.cli_utils.utils import printer -from CveXplore.database.maintenance.Config import Configuration +from CveXplore.database.maintenance.Config import Configuration, runPath @click.group( @@ -25,10 +28,15 @@ def initialize_cmd(ctx): @db_cmd.group("sources", invoke_without_command=True, help="Database source management") -@click.option("--pretty", is_flag=True, help="Pretty print the output") @click.pass_context -def sources_cmd(ctx, pretty): +def sources_cmd(ctx): + pass + +@sources_cmd.group("show", invoke_without_command=True, help="Show sources") +@click.option("--pretty", is_flag=True, help="Pretty print the output") +@click.pass_context +def show_cmd(ctx, pretty): config = Configuration() if ctx.invoked_subcommand is None: @@ -41,3 +49,42 @@ def sources_cmd(ctx, pretty): input_data=config.SOURCES, pretty=pretty, ) + + +@sources_cmd.group("set", invoke_without_command=True, help="Set sources") +@click.option( + "-k", + "--key", + help="Set the source key", + type=click.Choice(["capec", "cpe", "cwe", "via4", "cves"], case_sensitive=False), +) +@click.option( + "-v", + "--value", + help="Set the source key value", +) +@click.pass_context +def set_cmd(ctx, key, value): + config = Configuration() + + sources = config.SOURCES + + sources[key] = value + + with open(os.path.join(runPath, "../../.sources.ini"), "w") as f: + f.write(json.dumps(sources)) + + printer(input_data={"SOURCES SET TO": sources}, pretty=True) + + +@sources_cmd.group("reset", invoke_without_command=True, help="Set sources") +@click.pass_context +def reset_cmd(ctx): + config = Configuration() + + sources = config.DEFAULT_SOURCES + + with open(os.path.join(runPath, "../../.sources.ini"), "w") as f: + f.write(json.dumps(sources)) + + printer(input_data={"SOURCES RESET TO": sources}, pretty=True) diff --git a/CveXplore/cli_cmds/search_cmds/commands.py b/CveXplore/cli_cmds/search_cmds/commands.py index 515e296f..0d9608ee 100644 --- a/CveXplore/cli_cmds/search_cmds/commands.py +++ b/CveXplore/cli_cmds/search_cmds/commands.py @@ -11,7 +11,13 @@ invoke_without_command=True, help="Perform search queries on a single collection", ) -@click.option("-c", "--collection", required=True, help="Collection to query") +@click.option( + "-c", + "--collection", + required=True, + type=click.Choice(["capec", "cpe", "cwe", "via4", "cves"], case_sensitive=False), + help="Collection to query", +) @click.option("-f", "--field", required=True, help="Field to query") @click.option("-v", "--value", required=True, help="Value to query") @click.option("-l", "--limit", default=10, help="Query limit") @@ -48,9 +54,7 @@ def search_cmd(ctx, collection, field, value, limit, pretty, output): ctx.obj["RESULT"] = result -@search_cmd.command( - "less", help="Lets you scroll through the returned results" -) +@search_cmd.command("less", help="Lets you scroll through the returned results") @click.pass_context def less_cmd(ctx): click.echo_via_pager(ctx.obj["RESULT"]) diff --git a/CveXplore/database/maintenance/Config.py b/CveXplore/database/maintenance/Config.py index 9564d3c6..045ef19c 100644 --- a/CveXplore/database/maintenance/Config.py +++ b/CveXplore/database/maintenance/Config.py @@ -20,13 +20,16 @@ class Configuration(object): if os.getenv("SOURCES") is not None: SOURCES = json.loads(os.getenv("SOURCES")) else: - SOURCES = { - "cve": "https://nvd.nist.gov/feeds/json/cve/1.1/", - "cpe": "https://nvd.nist.gov/feeds/json/cpematch/1.0/nvdcpematch-1.0.json.zip", - "cwe": "https://cwe.mitre.org/data/xml/cwec_v4.4.xml.zip", - "capec": "https://capec.mitre.org/data/xml/capec_v3.5.xml", - "via4": "https://www.cve-search.org/feeds/via4.json", - } + with open(os.path.join(runPath, "../../.sources.ini")) as f: + SOURCES = json.loads(f.read()) + + DEFAULT_SOURCES = { + "cve": "https://nvd.nist.gov/feeds/json/cve/1.1/", + "cpe": "https://nvd.nist.gov/feeds/json/cpematch/1.0/nvdcpematch-1.0.json.zip", + "cwe": "https://cwe.mitre.org/data/xml/cwec_v4.4.xml.zip", + "capec": "https://capec.mitre.org/data/xml/capec_v3.5.xml", + "via4": "https://www.cve-search.org/feeds/via4.json", + } HTTP_PROXY = os.getenv("HTTP_PROXY", "") diff --git a/setup.py b/setup.py index 4ac00fa8..932d1fed 100644 --- a/setup.py +++ b/setup.py @@ -36,7 +36,7 @@ description="Package for interacting with cve-search", long_description=README, long_description_content_type="text/x-rst", - package_data={"CveXplore": ["LICENSE", "VERSION", ".cvexplore-complete.bash", ".schema_version"]}, + package_data={"CveXplore": ["LICENSE", "VERSION", ".cvexplore-complete.bash", ".schema_version", ".sources.ini"]}, entry_points=''' [console_scripts] cvexplore=CveXplore.cli:main