Skip to content
Open
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
16 changes: 8 additions & 8 deletions tools/content_manager/content_manager/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from typing import Literal

import click
from content_manager.common.constants import Constants
from content_manager.common import datetime_converter
from content_manager.common.custom_exceptions import RuleVerificationError
from content_manager.data_tables import DataTables
Expand All @@ -40,14 +41,13 @@

LOGGER = logging.getLogger()

ROOT_DIR = pathlib.Path(__file__).parent.parent
RULES_DIR = ROOT_DIR / "rules"
RULE_CONFIG_FILE = ROOT_DIR / "rule_config.yaml"
REF_LISTS_DIR = ROOT_DIR / "reference_lists"
REF_LIST_CONFIG_FILE = ROOT_DIR / "reference_list_config.yaml"
DATA_TABLES_DIR = ROOT_DIR / "data_tables"
DATA_TABLE_CONFIG_FILE = ROOT_DIR / "data_table_config.yaml"
RULE_EXCLUSIONS_CONFIG_FILE = ROOT_DIR / "rule_exclusions_config.yaml"
RULES_DIR = Constants.ROOT_DIR / "rules"
RULE_CONFIG_FILE = Constants.ROOT_DIR / "rule_config.yaml"
REF_LISTS_DIR = Constants.ROOT_DIR / "reference_lists"
REF_LIST_CONFIG_FILE = Constants.ROOT_DIR / "reference_list_config.yaml"
DATA_TABLES_DIR = Constants.ROOT_DIR / "data_tables"
DATA_TABLE_CONFIG_FILE = Constants.ROOT_DIR / "data_table_config.yaml"
RULE_EXCLUSIONS_CONFIG_FILE = Constants.ROOT_DIR / "rule_exclusions_config.yaml"

dotenv.load_dotenv()

Expand Down
14 changes: 14 additions & 0 deletions tools/content_manager/content_manager/common/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import os
import pathlib


class Constants:
"""Handles constants used throughout the content manager."""
ROOT_DIR = os.getenv("ROOT_DIR", pathlib.Path(__file__).parent.parent)
# Variables for a future support
# RULES_DIR = ROOT_DIR / pathlib.Path(os.getenv("RULES_DIR", "rules"))
# RULE_CONFIG_FILE = ROOT_DIR / pathlib.Path(os.getenv("RULE_CONFIG_FILE", "rules"))
# REF_LISTS_DIR = ROOT_DIR / pathlib.Path(os.getenv("REF_LISTS_DIR", "rules"))
# DATA_TABLES_DIR = ROOT_DIR / pathlib.Path(os.getenv("DATA_TABLES_DIR", "rules"))
# DATA_TABLE_CONFIG_FILE = ROOT_DIR / pathlib.Path(os.getenv("DATA_TABLE_CONFIG_FILE", "rules"))
# RULE_EXCLUSION_CONFIG_FILE = ROOT_DIR / pathlib.Path(os.getenv("RULE_EXCLUSION_CONFIG_FILE", "rules"))
8 changes: 4 additions & 4 deletions tools/content_manager/content_manager/data_tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import pathlib
from typing import Any, Literal, Mapping, Sequence

from content_manager.common.constants import Constants
from content_manager.common.custom_exceptions import DataTableConfigError
from google.auth.transport import requests
from google_secops_api.data_table_rows.bulk_create_data_table_rows import bulk_create_data_table_rows
Expand All @@ -34,9 +35,8 @@

LOGGER = logging.getLogger()

ROOT_DIR = pathlib.Path(__file__).parent.parent
DATA_TABLES_DIR = ROOT_DIR / "data_tables"
DATA_TABLE_CONFIG_FILE = ROOT_DIR / "data_table_config.yaml"
DATA_TABLES_DIR = Constants.ROOT_DIR / "data_tables"
DATA_TABLE_CONFIG_FILE = Constants.ROOT_DIR / "data_table_config.yaml"
DATA_TABLE_COLUMN_TYPES = Literal["CIDR", "STRING", "REGEX"] # pylint: disable="invalid-name"

# Use ruamel.yaml to raise an exception if a YAML file contains duplicate keys
Expand Down Expand Up @@ -384,7 +384,7 @@ def dump_data_table_config(self):
exclude={"name"}
)

data_table_config_file_path = ROOT_DIR / "data_table_config.yaml"
data_table_config_file_path = Constants.ROOT_DIR / "data_table_config.yaml"

LOGGER.info("Writing data table config to %s", data_table_config_file_path)
with open(
Expand Down
8 changes: 4 additions & 4 deletions tools/content_manager/content_manager/reference_lists.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import pathlib
from typing import Any, List, Literal, Mapping, Sequence, Tuple

from content_manager.common.constants import Constants
from content_manager.common.custom_exceptions import ReferenceListConfigError
from google.auth.transport import requests
from google_secops_api.reference_lists.create_reference_list import create_reference_list
Expand All @@ -34,9 +35,8 @@

LOGGER = logging.getLogger()

ROOT_DIR = pathlib.Path(__file__).parent.parent
REF_LISTS_DIR = ROOT_DIR / "reference_lists"
REF_LIST_CONFIG_FILE = ROOT_DIR / "reference_list_config.yaml"
REF_LISTS_DIR = Constants.ROOT_DIR / "reference_lists"
REF_LIST_CONFIG_FILE = Constants.ROOT_DIR / "reference_list_config.yaml"
REF_LIST_SYNTAX_TYPES = Literal[ # pylint: disable="invalid-name"
"REFERENCE_LIST_SYNTAX_TYPE_UNSPECIFIED",
"REFERENCE_LIST_SYNTAX_TYPE_PLAIN_TEXT_STRING",
Expand Down Expand Up @@ -322,7 +322,7 @@ def dump_ref_list_config(self):
exclude={"name"}
)

ref_list_config_file_path = ROOT_DIR / "reference_list_config.yaml"
ref_list_config_file_path = Constants.ROOT_DIR / "reference_list_config.yaml"

LOGGER.info(
"Writing reference list config to %s", ref_list_config_file_path
Expand Down
4 changes: 2 additions & 2 deletions tools/content_manager/content_manager/rule_exclusions.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import re
from typing import Any, Literal, Mapping, Sequence

from content_manager.common.constants import Constants
from content_manager.common.custom_exceptions import RuleExclusionConfigError
from google.auth.transport import requests
from google_secops_api.findings_refinements.create_findings_refinement import create_findings_refinement
Expand All @@ -38,8 +39,7 @@

LOGGER = logging.getLogger()

ROOT_DIR = pathlib.Path(__file__).parent.parent
RULE_EXCLUSIONS_CONFIG_FILE = ROOT_DIR / "rule_exclusions_config.yaml"
RULE_EXCLUSIONS_CONFIG_FILE = Constants.ROOT_DIR / "rule_exclusions_config.yaml"
RULE_EXCLUSION_TYPES = Literal["DETECTION_EXCLUSION"] # pylint: disable="invalid-name"
EXCLUSION_APPLICATIONS = Literal["curated_rule_sets", "curated_rules"] # pylint: disable="invalid-name"

Expand Down
8 changes: 4 additions & 4 deletions tools/content_manager/content_manager/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import re
from typing import Any, List, Mapping, Sequence, Tuple

from content_manager.common.constants import Constants
from content_manager.common.custom_exceptions import DuplicateRuleIdError
from content_manager.common.custom_exceptions import DuplicateRuleNameError
from content_manager.common.custom_exceptions import RuleConfigError
Expand All @@ -43,9 +44,8 @@

LOGGER = logging.getLogger()

ROOT_DIR = pathlib.Path(__file__).parent.parent
RULES_DIR = ROOT_DIR / "rules"
RULE_CONFIG_FILE = ROOT_DIR / "rule_config.yaml"
RULES_DIR = Constants.ROOT_DIR / "rules"
RULE_CONFIG_FILE = Constants.ROOT_DIR / "rule_config.yaml"

# Use ruamel.yaml to raise an exception if a YAML file contains duplicate keys
# (i.e. duplicate rule names)
Expand Down Expand Up @@ -329,7 +329,7 @@ def dump_rule_config(self):
exclude={"name"}
)

rule_config_file_path = ROOT_DIR / "rule_config.yaml"
rule_config_file_path = Constants.ROOT_DIR / "rule_config.yaml"

LOGGER.info("Writing rule config to %s", rule_config_file_path)
with open(rule_config_file_path, "w", encoding="utf-8") as rule_config_file:
Expand Down
6 changes: 3 additions & 3 deletions tools/content_manager/content_manager/test_data_tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import pathlib
from typing import Any, Mapping, Sequence

from content_manager.common.constants import Constants
from content_manager.common.custom_exceptions import DataTableConfigError
from content_manager.data_tables import DataTable
from content_manager.data_tables import DataTableColumn
Expand All @@ -29,9 +30,8 @@
import ruamel.yaml.constructor


ROOT_DIR = pathlib.Path(__file__).parent.parent
DATA_TABLES_DIR = ROOT_DIR / "data_tables"
DATA_TABLE_CONFIG_FILE = ROOT_DIR / "data_table_config.yaml"
DATA_TABLES_DIR = Constants.ROOT_DIR / "data_tables"
DATA_TABLE_CONFIG_FILE = Constants.ROOT_DIR / "data_table_config.yaml"
TEST_DATA_DIR = pathlib.Path(__file__).parent / "test_data"
TEST_DATA_TABLES_DIR = TEST_DATA_DIR / "data_tables"
TEST_DATA_TABLE_CONFIG_FILE = TEST_DATA_DIR / "test_data_table_config.yaml"
Expand Down
6 changes: 3 additions & 3 deletions tools/content_manager/content_manager/test_reference_lists.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import pathlib
from typing import Any, Mapping, Sequence

from content_manager.common.constants import Constants
from content_manager.common.custom_exceptions import ReferenceListConfigError
from content_manager.reference_lists import ReferenceList
from content_manager.reference_lists import ReferenceListConfigEntry
Expand All @@ -28,9 +29,8 @@
import ruamel.yaml.constructor


ROOT_DIR = pathlib.Path(__file__).parent.parent
REF_LISTS_DIR = ROOT_DIR / "reference_lists"
REF_LIST_CONFIG_FILE = ROOT_DIR / "reference_list_config.yaml"
REF_LISTS_DIR = Constants.ROOT_DIR / "reference_lists"
REF_LIST_CONFIG_FILE = Constants.ROOT_DIR / "reference_list_config.yaml"
TEST_DATA_DIR = pathlib.Path(__file__).parent / "test_data"
TEST_REF_LISTS_DIR = TEST_DATA_DIR / "reference_lists"
TEST_REF_LISTS_CONFIG_FILE = TEST_DATA_DIR / "test_reference_list_config.yaml"
Expand Down
4 changes: 2 additions & 2 deletions tools/content_manager/content_manager/test_rule_exclusions.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import pathlib
from typing import Any, Mapping, Sequence

from content_manager.common.constants import Constants
from content_manager.common.custom_exceptions import RuleExclusionConfigError
from content_manager.rule_exclusions import RuleExclusion
from content_manager.rule_exclusions import RuleExclusionConfigEntry
Expand All @@ -27,8 +28,7 @@
import ruamel.yaml
import ruamel.yaml.constructor

ROOT_DIR = pathlib.Path(__file__).parent.parent
RULE_EXCLUSIONS_CONFIG_FILE = ROOT_DIR / "rule_exclusions_config.yaml"
RULE_EXCLUSIONS_CONFIG_FILE = Constants.ROOT_DIR / "rule_exclusions_config.yaml"
TEST_DATA_DIR = pathlib.Path(__file__).parent / "test_data"
TEST_RULE_EXCLUSIONS_CONFIG_FILE = (
TEST_DATA_DIR / "test_rule_exclusions_config.yaml"
Expand Down
6 changes: 3 additions & 3 deletions tools/content_manager/content_manager/test_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import pathlib
from typing import Any, Mapping, Sequence

from content_manager.common.constants import Constants
from content_manager.common.custom_exceptions import DuplicateRuleIdError
from content_manager.common.custom_exceptions import DuplicateRuleNameError
from content_manager.common.custom_exceptions import RuleConfigError
Expand All @@ -32,9 +33,8 @@
import pytest
import ruamel.yaml.constructor

ROOT_DIR = pathlib.Path(__file__).parent.parent
RULES_DIR = ROOT_DIR / "rules"
RULE_CONFIG_FILE = ROOT_DIR / "rule_config.yaml"
RULES_DIR = Constants.ROOT_DIR / "rules"
RULE_CONFIG_FILE = Constants.ROOT_DIR / "rule_config.yaml"
TEST_DATA_DIR = pathlib.Path(__file__).parent / "test_data"
TEST_RULES_DIR = TEST_DATA_DIR / "rules"
TEST_RULE_CONFIG_FILE = TEST_DATA_DIR / "test_rule_config.yaml"
Expand Down