remove dead code for unsupported agents

This commit is contained in:
wh1te909 2022-01-10 19:32:48 +00:00
parent 80070b333e
commit a14b0278c8
9 changed files with 52 additions and 81 deletions

View File

@ -18,7 +18,6 @@ from django.db import models
from django.utils import timezone as djangotime
from nats.aio.client import Client as NATS
from nats.aio.errors import ErrTimeout
from packaging import version as pyver
from core.models import TZ_CHOICES, CoreSettings
from logs.models import BaseAuditModel, DebugLog
@ -345,7 +344,7 @@ class Agent(BaseAuditModel):
},
}
if history_pk != 0 and pyver.parse(self.version) >= pyver.parse("1.6.0"):
if history_pk != 0:
data["id"] = history_pk
running_agent = self

View File

@ -626,7 +626,7 @@ class TestAgentViews(TacticalTestCase):
@patch("agents.tasks.run_script_email_results_task.delay")
@patch("agents.models.Agent.run_script")
def test_run_script(self, run_script, email_task):
from .models import AgentCustomField, Note
from .models import AgentCustomField, Note, AgentHistory
from clients.models import ClientCustomField, SiteCustomField
run_script.return_value = "ok"
@ -643,8 +643,9 @@ class TestAgentViews(TacticalTestCase):
r = self.client.post(url, data, format="json")
self.assertEqual(r.status_code, 200)
hist = AgentHistory.objects.filter(agent=self.agent, script=script).last()
run_script.assert_called_with(
scriptpk=script.pk, args=[], timeout=18, wait=True, history_pk=0
scriptpk=script.pk, args=[], timeout=18, wait=True, history_pk=hist.pk
)
run_script.reset_mock()
@ -690,8 +691,9 @@ class TestAgentViews(TacticalTestCase):
r = self.client.post(url, data, format="json")
self.assertEqual(r.status_code, 200)
hist = AgentHistory.objects.filter(agent=self.agent, script=script).last()
run_script.assert_called_with(
scriptpk=script.pk, args=["hello", "world"], timeout=25, history_pk=0
scriptpk=script.pk, args=["hello", "world"], timeout=25, history_pk=hist.pk
)
run_script.reset_mock()
@ -710,12 +712,13 @@ class TestAgentViews(TacticalTestCase):
r = self.client.post(url, data, format="json")
self.assertEqual(r.status_code, 200)
hist = AgentHistory.objects.filter(agent=self.agent, script=script).last()
run_script.assert_called_with(
scriptpk=script.pk,
args=["hello", "world"],
timeout=25,
wait=True,
history_pk=0,
history_pk=hist.pk,
)
run_script.reset_mock()
@ -737,12 +740,13 @@ class TestAgentViews(TacticalTestCase):
r = self.client.post(url, data, format="json")
self.assertEqual(r.status_code, 200)
hist = AgentHistory.objects.filter(agent=self.agent, script=script).last()
run_script.assert_called_with(
scriptpk=script.pk,
args=["hello", "world"],
timeout=25,
wait=True,
history_pk=0,
history_pk=hist.pk,
)
run_script.reset_mock()
@ -766,12 +770,13 @@ class TestAgentViews(TacticalTestCase):
r = self.client.post(url, data, format="json")
self.assertEqual(r.status_code, 200)
hist = AgentHistory.objects.filter(agent=self.agent, script=script).last()
run_script.assert_called_with(
scriptpk=script.pk,
args=["hello", "world"],
timeout=25,
wait=True,
history_pk=0,
history_pk=hist.pk,
)
run_script.reset_mock()
@ -792,12 +797,13 @@ class TestAgentViews(TacticalTestCase):
r = self.client.post(url, data, format="json")
self.assertEqual(r.status_code, 200)
hist = AgentHistory.objects.filter(agent=self.agent, script=script).last()
run_script.assert_called_with(
scriptpk=script.pk,
args=["hello", "world"],
timeout=25,
wait=True,
history_pk=0,
history_pk=hist.pk,
)
run_script.reset_mock()

View File

@ -335,14 +335,13 @@ def send_raw_cmd(request, agent_id):
},
}
if pyver.parse(agent.version) >= pyver.parse("1.6.0"):
hist = AgentHistory.objects.create(
agent=agent,
type="cmd_run",
command=request.data["cmd"],
username=request.user.username[:50],
)
data["id"] = hist.pk
hist = AgentHistory.objects.create(
agent=agent,
type="cmd_run",
command=request.data["cmd"],
username=request.user.username[:50],
)
data["id"] = hist.pk
r = asyncio.run(agent.nats_cmd(data, timeout=timeout + 2))
@ -613,15 +612,13 @@ def run_script(request, agent_id):
debug_info={"ip": request._client_ip},
)
history_pk = 0
if pyver.parse(agent.version) >= pyver.parse("1.6.0"):
hist = AgentHistory.objects.create(
agent=agent,
type="script_run",
script=script,
username=request.user.username[:50],
)
history_pk = hist.pk
hist = AgentHistory.objects.create(
agent=agent,
type="script_run",
script=script,
username=request.user.username[:50],
)
history_pk = hist.pk
if output == "wait":
r = agent.run_script(

View File

@ -153,14 +153,6 @@ class WinUpdates(APIView):
).save()
agent.delete_superseded_updates()
# more superseded updates cleanup
if pyver.parse(agent.version) <= pyver.parse("1.4.2"):
for u in agent.winupdates.filter( # type: ignore
date_installed__isnull=True, result="failed"
).exclude(installed=True):
u.delete()
return Response("ok")
@ -230,8 +222,6 @@ class CheckRunner(APIView):
def patch(self, request):
check = get_object_or_404(Check, pk=request.data["id"])
if pyver.parse(check.agent.version) < pyver.parse("1.5.7"):
return notify_error("unsupported")
check.last_run = djangotime.now()
check.save(update_fields=["last_run"])

View File

@ -484,9 +484,7 @@ class AutomatedTask(BaseAuditModel):
},
}
if self.run_asap_after_missed and pyver.parse(
agent.version
) >= pyver.parse("1.4.7"):
if self.run_asap_after_missed:
nats_data["schedtaskpayload"]["run_asap_after_missed"] = True
if self.remove_if_not_scheduled:

View File

@ -1,6 +1,7 @@
from unittest.mock import patch
from django.utils import timezone as djangotime
from django.conf import settings
from model_bakery import baker
from checks.models import CheckHistory
@ -215,13 +216,7 @@ class TestCheckViews(TacticalTestCase):
@patch("agents.models.Agent.nats_cmd")
def test_run_checks(self, nats_cmd):
agent = baker.make_recipe("agents.agent", version="1.4.1")
agent_b4_141 = baker.make_recipe("agents.agent", version="1.4.0")
url = f"{base_url}/{agent_b4_141.agent_id}/run/"
r = self.client.post(url)
self.assertEqual(r.status_code, 200)
nats_cmd.assert_called_with({"func": "runchecks"}, wait=False)
agent = baker.make_recipe("agents.agent", version=settings.LATEST_AGENT_VER)
nats_cmd.reset_mock()
nats_cmd.return_value = "busy"

View File

@ -4,7 +4,6 @@ from datetime import datetime as dt
from django.db.models import Q
from django.shortcuts import get_object_or_404
from django.utils import timezone as djangotime
from packaging import version as pyver
from rest_framework.decorators import api_view, permission_classes
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
@ -200,14 +199,10 @@ class GetCheckHistory(APIView):
def run_checks(request, agent_id):
agent = get_object_or_404(Agent, agent_id=agent_id)
if pyver.parse(agent.version) >= pyver.parse("1.4.1"):
r = asyncio.run(agent.nats_cmd({"func": "runchecks"}, timeout=15))
if r == "busy":
return notify_error(f"Checks are already running on {agent.hostname}")
elif r == "ok":
return Response(f"Checks will now be re-run on {agent.hostname}")
else:
return notify_error("Unable to contact the agent")
else:
asyncio.run(agent.nats_cmd({"func": "runchecks"}, wait=False))
r = asyncio.run(agent.nats_cmd({"func": "runchecks"}, timeout=15))
if r == "busy":
return notify_error(f"Checks are already running on {agent.hostname}")
elif r == "ok":
return Response(f"Checks will now be re-run on {agent.hostname}")
else:
return notify_error("Unable to contact the agent")

View File

@ -1,6 +1,5 @@
import asyncio
from packaging import version as pyver
from agents.models import Agent, AgentHistory
from scripts.models import Script
@ -20,14 +19,13 @@ def handle_bulk_command_task(
},
}
for agent in Agent.objects.filter(pk__in=agentpks):
if pyver.parse(agent.version) >= pyver.parse("1.6.0"):
hist = AgentHistory.objects.create(
agent=agent,
type="cmd_run",
command=cmd,
username=username,
)
nats_data["id"] = hist.pk
hist = AgentHistory.objects.create(
agent=agent,
type="cmd_run",
command=cmd,
username=username,
)
nats_data["id"] = hist.pk
asyncio.run(agent.nats_cmd(nats_data, wait=False))
@ -36,15 +34,12 @@ def handle_bulk_command_task(
def handle_bulk_script_task(scriptpk, agentpks, args, timeout, username) -> None:
script = Script.objects.get(pk=scriptpk)
for agent in Agent.objects.filter(pk__in=agentpks):
history_pk = 0
if pyver.parse(agent.version) >= pyver.parse("1.6.0"):
hist = AgentHistory.objects.create(
agent=agent,
type="script_run",
script=script,
username=username,
)
history_pk = hist.pk
agent.run_script(
scriptpk=script.pk, args=args, timeout=timeout, history_pk=history_pk
hist = AgentHistory.objects.create(
agent=agent,
type="script_run",
script=script,
username=username,
)
agent.run_script(
scriptpk=script.pk, args=args, timeout=timeout, history_pk=hist.pk
)

View File

@ -2,7 +2,6 @@ import asyncio
from typing import Any
from django.shortcuts import get_object_or_404
from packaging import version as pyver
from rest_framework.decorators import api_view
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
@ -42,9 +41,6 @@ class GetSoftware(APIView):
# software install
def post(self, request, agent_id):
agent = get_object_or_404(Agent, agent_id=agent_id)
if pyver.parse(agent.version) < pyver.parse("1.4.8"):
return notify_error("Requires agent v1.4.8")
name = request.data["name"]
action = PendingAction.objects.create(