Skip to content

Commit

Permalink
Changes positions.conf file path to /var/lib/intelmq/server/positions…
Browse files Browse the repository at this point in the history
….json.
  • Loading branch information
gethvi committed Feb 1, 2024
1 parent 4f3323d commit af8f6ba
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 19 deletions.
1 change: 1 addition & 0 deletions intelmq/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
HARMONIZATION_CONF_FILE = os.path.join(CONFIG_DIR, "harmonization.conf")
RUNTIME_CONF_FILE = os.path.join(CONFIG_DIR, "runtime.yaml")
INTELMQ_CONF_FILE = os.path.join(CONFIG_DIR, "intelmq.yaml")
POSITIONS_FILE = os.path.join(VAR_SERVER_PATH, "positions.json")
old_runtime_conf_file = pathlib.Path(RUNTIME_CONF_FILE).with_suffix('.conf')
if not pathlib.Path(RUNTIME_CONF_FILE).exists() and old_runtime_conf_file.exists():
old_runtime_conf_file.rename(RUNTIME_CONF_FILE)
Expand Down
9 changes: 3 additions & 6 deletions intelmq/app/api/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import intelmq.app.api.runctl as runctl
import intelmq.app.api.session as session

from intelmq import RUNTIME_CONF_FILE, POSITIONS_FILE, HARMONIZATION_CONF_FILE
from intelmq.app.dependencies import (app_config, cached_response, session_store,
token_authorization)
from .models import TokenResponse
Expand Down Expand Up @@ -193,10 +194,8 @@ def post_runtime(body: dict):

@router.get("/positions", dependencies=[authorized], response_model=dict)
def get_positions(runner: runctl.RunIntelMQCtl = Depends(runner)):
positions = pathlib.Path('/opt/intelmq/etc/manager/positions.conf')
paths = runner.get_paths()
if 'CONFIG_DIR' in paths:
positions = pathlib.Path(paths['CONFIG_DIR']) / 'manager/positions.conf'
positions = pathlib.Path(paths.get("POSITIONS_FILE", POSITIONS_FILE))
try:
return json.loads(positions.read_text())
except OSError as e:
Expand All @@ -207,10 +206,8 @@ def get_positions(runner: runctl.RunIntelMQCtl = Depends(runner)):
@router.post("/positions", dependencies=[authorized], response_model=str,
response_class=PlainTextResponse)
def post_positions(body: dict, runner: runctl.RunIntelMQCtl = Depends(runner)):
positions = pathlib.Path('/opt/intelmq/etc/manager/positions.conf')
paths = runner.get_paths()
if 'CONFIG_DIR' in paths:
positions = pathlib.Path(paths['CONFIG_DIR']) / 'manager/positions.conf'
positions = pathlib.Path(paths.get("POSITIONS_FILE", POSITIONS_FILE))
try:
positions.parent.mkdir(exist_ok=True)
positions.write_text(json.dumps(body, indent=4))
Expand Down
15 changes: 11 additions & 4 deletions intelmq/bin/intelmqctl.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
HARMONIZATION_CONF_FILE,
RUNTIME_CONF_FILE, VAR_RUN_PATH, STATE_FILE_PATH,
DEFAULT_LOGGING_PATH, __version_info__,
CONFIG_DIR, ROOT_DIR)
CONFIG_DIR, ROOT_DIR, POSITIONS_FILE, VAR_SERVER_PATH, INTELMQ_CONF_FILE)
from intelmq.lib import utils
from intelmq.lib.datatypes import ReturnType, MESSAGES, LogLevel
from intelmq.lib.processmanager import *
Expand Down Expand Up @@ -1251,9 +1251,16 @@ def debug(self, sections=None):
if self._returntype is ReturnType.TEXT:
print('Paths:')
for path in ('HARMONIZATION_CONF_FILE',
'RUNTIME_CONF_FILE', 'VAR_RUN_PATH', 'STATE_FILE_PATH',
'DEFAULT_LOGGING_PATH', '__file__',
'CONFIG_DIR', 'ROOT_DIR'):
'RUNTIME_CONF_FILE',
'INTELMQ_CONF_FILE',
'POSITIONS_FILE',
'VAR_RUN_PATH',
'STATE_FILE_PATH',
'DEFAULT_LOGGING_PATH',
'__file__',
'CONFIG_DIR',
'ROOT_DIR',
'VAR_SERVER_PATH'):
output['paths'][path] = variables[path]
if self._returntype is ReturnType.TEXT:
print(f'{path}: {variables[path]!r}')
Expand Down
20 changes: 11 additions & 9 deletions intelmq/tests/app/api/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
from unittest.mock import patch, MagicMock
from fastapi.testclient import TestClient


with patch("intelmq.lib.utils.get_intelmq_settings", MagicMock(return_value={})):

from intelmq.app import dependencies
Expand Down Expand Up @@ -97,7 +96,11 @@ def setUp(self) -> None:
dependencies.startup(DummyConfig())
self.conf_dir = TemporaryDirectory()
app.dependency_overrides[runner] = get_dummy_reader(
paths={"CONFIG_DIR": self.conf_dir.name})
paths={
"CONFIG_DIR": self.conf_dir.name,
"POSITIONS_FILE": os.path.join(self.conf_dir.name, "positions.json")
}
)

self.save_runtime()
self.save_positions()
Expand All @@ -110,9 +113,12 @@ def save_runtime(self):
with open(f"{self.conf_dir.name}/runtime.yaml", "w+") as f:
json.dump({}, f)

def load_positions(self):
with open(f"{self.conf_dir.name}/positions.json") as f:
return json.load(f)

def save_positions(self):
os.makedirs(f"{self.conf_dir.name}/manager", exist_ok=True)
with open(f"{self.conf_dir.name}/manager/positions.conf", "w+") as f:
with open(f"{self.conf_dir.name}/positions.json", "w+") as f:
json.dump({}, f)

def tearDown(self) -> None:
Expand Down Expand Up @@ -141,7 +147,6 @@ def test_post_runtime(self):

self.assertEqual(response.status_code, 200)
self.assertEqual(response.text, "success")

self.assertEqual(utils.get_runtime(), data)

def test_post_positions(self):
Expand All @@ -155,10 +160,7 @@ def test_post_positions(self):

self.assertEqual(response.status_code, 200)
self.assertEqual(response.text, "success")

with open(f"{self.conf_dir.name}/manager/positions.conf", "r") as f:
saved = json.load(f)
self.assertEqual(saved, data)
self.assertEqual(self.load_positions(), data)


class TestAPILogin(TestCase):
Expand Down

0 comments on commit af8f6ba

Please sign in to comment.