Skip to content

Commit

Permalink
Merge pull request #67 from P-T-I/CveXplore-66
Browse files Browse the repository at this point in the history
Create possibility to set or delete sources from cli
  • Loading branch information
P-T-I authored Sep 7, 2021
2 parents 0f442da + 100e636 commit fc8d136
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 16 deletions.
1 change: 1 addition & 0 deletions CveXplore/.sources.ini
Original file line number Diff line number Diff line change
@@ -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"}
2 changes: 1 addition & 1 deletion CveXplore/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.2.6.1
0.2.6.1.dev6
53 changes: 50 additions & 3 deletions CveXplore/cli_cmds/db_cmds/commands.py
Original file line number Diff line number Diff line change
@@ -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(
Expand All @@ -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:
Expand All @@ -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)
12 changes: 8 additions & 4 deletions CveXplore/cli_cmds/search_cmds/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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"])
17 changes: 10 additions & 7 deletions CveXplore/database/maintenance/Config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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", "")

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit fc8d136

Please sign in to comment.