Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean out old OSSEC diff and state files #7327

Merged
merged 1 commit into from
Nov 8, 2024
Merged
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
25 changes: 25 additions & 0 deletions molecule/testinfra/common/test_basic_configuration.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import time

import testutils
from testinfra.host import Host

Expand All @@ -21,3 +23,26 @@ def test_system_time(host: Host) -> None:
c = host.run("timedatectl show")
assert "NTP=yes" in c.stdout
assert "NTPSynchronized=yes" in c.stdout


def test_ossec_cleanup(host: Host) -> None:
with host.sudo():
c = host.run("mkdir -p /var/ossec/queue/diff/local/boot/appinfra-test")
assert c.rc == 0
c = host.run("echo 'test' > /var/ossec/queue/diff/local/boot/appinfra-test/state.123456789")
assert c.rc == 0
# change the mtime on the file to be 2 years ago
c = host.run(
"touch -d '2 years ago' /var/ossec/queue/diff/local/boot/appinfra-test/state.123456789"
)
assert c.rc == 0
c = host.run("systemctl start securedrop-cleanup-ossec")
assert c.rc == 0
while host.service("securedrop-cleanup-ossec").is_running:
time.sleep(1)
assert not host.file(
"/var/ossec/queue/diff/local/boot/appinfra-test/state.123456789"
).exists
# cleanup
c = host.run("rm -r /var/ossec/queue/diff/local/boot/appinfra-test")
assert c.rc == 0
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[Unit]
Description=Cleanup OSSEC diff queue

[Service]
Type=oneshot
ExecStart=/usr/bin/securedrop-cleanup-ossec.py
User=root
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[Unit]
Description=Cleanup OSSEC diff queue

[Timer]
OnCalendar=daily
Persistent=true
RandomizedDelaySec=5m

[Install]
WantedBy=timers.target
33 changes: 33 additions & 0 deletions securedrop/debian/config/usr/bin/securedrop-cleanup-ossec.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/usr/bin/python3
"""
Delete OSSEC diff/state files older than a year

Runs as root on both app and mon servers
"""

import os
import re
from datetime import datetime, timedelta

OSSEC_DIFFS = "/var/ossec/queue/diff/local/"
KEEP_DAYS = 365
# Match e.g. state.1667271785
RE_REMOVE = re.compile(r"^(state|diff)\.\d+$")


def main() -> None:
cutoff_date = datetime.now() - timedelta(days=KEEP_DAYS)

for root, dirs, files in os.walk(OSSEC_DIFFS):
for file in files:
if RE_REMOVE.match(file):
file_path = os.path.join(root, file)
modified_time = os.path.getmtime(file_path)
file_modified_date = datetime.fromtimestamp(modified_time)
if file_modified_date < cutoff_date:
os.remove(file_path)
print(f"Deleted file: {file_path} (Last modified: {file_modified_date})")


if __name__ == "__main__":
main()
2 changes: 2 additions & 0 deletions securedrop/debian/rules
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ override_dh_systemd_enable:
dh_systemd_enable --no-enable securedrop-clean-tmp.service
dh_systemd_enable --no-enable securedrop-remove-pending-sources.service
dh_systemd_enable --no-enable securedrop-remove-packages.service
dh_systemd_enable --no-enable securedrop-cleanup-ossec.service
dh_systemd_enable

# This is basically the same as the enable stanza above, just whether the
Expand All @@ -93,4 +94,5 @@ override_dh_systemd_start:
dh_systemd_start --no-start securedrop-clean-tmp.service
dh_systemd_start --no-start securedrop-remove-pending-sources.service
dh_systemd_start --no-start securedrop-remove-packages.service
dh_systemd_start --no-start securedrop-cleanup-ossec.service
dh_systemd_start
1 change: 1 addition & 0 deletions securedrop/debian/securedrop-config.install
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
debian/config/etc /
debian/config/lib /
debian/config/opt /
debian/config/usr /