Skip to content

Commit

Permalink
added dynamic creation of CveXplore config settings to '.. confval::'…
Browse files Browse the repository at this point in the history
… objects; this makes it easier to add config values to the documentation.
  • Loading branch information
P-T-I committed Apr 21, 2024
1 parent e733c08 commit bc41776
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 17 deletions.
34 changes: 22 additions & 12 deletions CveXplore/common/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ class Configuration(object):
Class holding the configuration
"""

USER_HOME_DIR = os.path.expanduser("~/.cvexplore")
if json.loads(os.getenv("DOC_BUILD"))["DOC_BUILD"] == "YES":
USER_HOME_DIR = "${HOME}/.cvexplore"
else:
USER_HOME_DIR = os.path.expanduser("~/.cvexplore")

CVE_START_YEAR = int(os.getenv("CVE_START_YEAR", 2000))

Expand Down Expand Up @@ -101,31 +104,38 @@ class Configuration(object):
MONGODB_HOST = os.getenv("MONGODB_HOST", "127.0.0.1")
MONGODB_PORT = int(os.getenv("MONGODB_PORT", 27017))

DEFAULT_SOURCES = {
"cwe": "https://cwe.mitre.org/data/xml/cwec_latest.xml.zip",
"capec": "https://capec.mitre.org/data/xml/capec_latest.xml",
"via4": "https://www.cve-search.org/feeds/via4.json",
"epss": "https://epss.cyentia.com/epss_scores-current.csv.gz", # See EPSS at https://www.first.org/epss
}

if os.getenv("SOURCES") is not None:
SOURCES = getenv_dict("SOURCES", None)
else:
with open(os.path.join(USER_HOME_DIR, ".sources.ini")) as f:
SOURCES = json.loads(f.read())
try:
with open(os.path.join(USER_HOME_DIR, ".sources.ini")) as _f:
SOURCES = json.loads(_f.read())
except FileNotFoundError:
SOURCES = DEFAULT_SOURCES

NVD_NIST_API_KEY = os.getenv("NVD_NIST_API_KEY", None)
NVD_NIST_NO_REJECTED = getenv_bool("NVD_NIST_NO_REJECTED", "True")
HTTP_PROXY_DICT = getenv_dict("HTTP_PROXY_DICT", {})
HTTP_PROXY_STRING = os.getenv("HTTP_PROXY_STRING", "")

DEFAULT_SOURCES = {
"cwe": "https://cwe.mitre.org/data/xml/cwec_latest.xml.zip",
"capec": "https://capec.mitre.org/data/xml/capec_latest.xml",
"via4": "https://www.cve-search.org/feeds/via4.json",
"epss": "https://epss.cyentia.com/epss_scores-current.csv.gz", # See EPSS at https://www.first.org/epss
}

LOGGING_TO_FILE = getenv_bool("LOGGING_TO_FILE", "True")
LOGGING_FILE_PATH = os.getenv(
"LOGGING_FILE_PATH", os.path.join(USER_HOME_DIR, "log")
)

if not os.path.exists(LOGGING_FILE_PATH):
os.mkdir(LOGGING_FILE_PATH)
try:
if not os.path.exists(LOGGING_FILE_PATH):
os.mkdir(LOGGING_FILE_PATH)
except FileNotFoundError:
# Probably creating documentation; just pass
pass

LOGGING_MAX_FILE_SIZE = (
int(os.getenv("LOGGING_MAX_FILE_SIZE", 100)) * 1024 * 1024
Expand Down
10 changes: 5 additions & 5 deletions docs/backend/settings.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@ The following config variables are the configuration settings for the backend:

.. confval:: CELERY_REDIS_URL

Url to be used by the backend for contacting redis; defaults to ``redis://redis:6379/``
Url to be used by the backend for contacting redis.

.. confval:: CELERY_REDIS_BROKER_DB

The redis database to use by the broker; defaults to ``5``
The redis database to use by the broker.

.. confval:: CELERY_REDIS_BACKEND_DB

The redis database to use by the brackend to store the results of the tasks in; defaults to ``6``
The redis database to use by the backend to store the results of the tasks in.

.. confval:: CELERY_TASK_FAILED_ERROR_CODE

Code to set in the task result if a task fails; defaults to ``1337``
Code to set in the task result if a task fails.

.. confval:: CELERY_KEEP_TASK_RESULT

The amount of days to keep the task results in the database; default to ``7``
The amount of days to keep the task results in the database.
31 changes: 31 additions & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@

default_role = "any"
autosectionlabel_prefix_document = True
show_warning_types = True
suppress_warnings = [
"ref.ref",
"ref.doc",
"ref.term",
"misc.highlighting_failure",
"toc.excluded",
]

autoclass_content = "both"

intersphinx_mapping = {
"python": ("https://docs.python.org/3", None),
Expand Down Expand Up @@ -381,9 +391,30 @@ class TestColorScheme(TestColor):
)


def _parse_config_values(app, config_class):

logger.info(f"Parsing config values for {config_class}")

all_configs = [c for c in dir(config_class) if not c.startswith("_")]

for each in all_configs:
app.add_config_value(
name=each,
default=getattr(config_class, each),
rebuild="html",
types=[type(getattr(config_class, each))],
)


def setup(app):
python_apigen_modules.update(_find_modules("../CveXplore"))

from CveXplore.common.config import Configuration

config = Configuration()

_parse_config_values(app, config)

app.add_role("test-color-primary", TestColorPrimary())
app.add_role("test-color-accent", TestColorAccent())
app.add_role("test-color-scheme", TestColorScheme())
Expand Down
9 changes: 9 additions & 0 deletions docs/general/settings.rst
Original file line number Diff line number Diff line change
@@ -1,2 +1,11 @@
Settings
--------

CveXplore by default looks for an .env file in ``${HOME}/.cvexplore`` folder, if certain values need to be
overwritten you can do it there.

The following config variables are the configuration settings for CveXplore:

.. confval:: USER_HOME_DIR

Directory to use as main directory for storing data, config files and sources initialization files.

0 comments on commit bc41776

Please sign in to comment.