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

feat: add report validated sink config #1688

Merged
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
28 changes: 23 additions & 5 deletions dongtai_protocol/views/agent_configv2.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,34 @@
import json
from typing import Any

from django.utils.translation import gettext_lazy as _
from rest_framework import serializers
from rest_framework.serializers import ValidationError

from dongtai_common.endpoint import OpenApiEndPoint, R
from dongtai_common.models.agent import IastAgent
from dongtai_web.common import get_data_gather_data
from dongtai_common.models.profile import IastProfile
from dongtai_conf.patch import patch_point
from dongtai_web.utils import extend_schema_with_envcheck


class _AgentConfigArgsSerializer(serializers.Serializer):
agent_id = serializers.IntegerField(required=True, help_text=_("Agent id"))


def get_agent_data_gather_config(agent_id):
return get_data_gather_data()
REPORT_VALIDATED_SINK_KEY = "report_validated_sink"
DEFAULT_REPORT_VALIDATED_SINK = {"report_validated_sink": False}


def get_report_validated_sink_profile() -> dict[str, bool]:
profile = IastProfile.objects.filter(key=REPORT_VALIDATED_SINK_KEY).values_list("value", flat=True).first()
if profile is None:
IastProfile(
key=REPORT_VALIDATED_SINK_KEY,
value=json.dumps(DEFAULT_REPORT_VALIDATED_SINK),
).save()
return DEFAULT_REPORT_VALIDATED_SINK
return json.loads(profile)


class AgentConfigAllinOneView(OpenApiEndPoint):
Expand All @@ -29,12 +44,15 @@ def get(self, request):
ser.is_valid(True)
except ValidationError as e:
return R.failure(data=e.detail)
agent = IastAgent.objects.filter(pk=ser.data["agent_id"]).first()
agent_id: int = ser.data["agent_id"]
agent = IastAgent.objects.filter(pk=agent_id).first()
if not agent:
return R.failure(msg="No agent found.")
data = get_agent_data_gather_config(ser.data["agent_id"])
data: dict[str, Any] = {}
if agent.bind_project is not None and agent.bind_project.enable_log is not None:
data["enable_log"] = agent.bind_project.enable_log
if agent.bind_project is not None and agent.bind_project.log_level is not None:
data["log_level"] = agent.bind_project.log_level
data[REPORT_VALIDATED_SINK_KEY] = get_report_validated_sink_profile()[REPORT_VALIDATED_SINK_KEY]
data, agent_id = patch_point(data, agent_id)
return R.success(data=data)
4 changes: 0 additions & 4 deletions dongtai_web/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,3 @@ def get_json_from_iast_profile(key: str, _serializer: type[serializers.Serialize
ser = _serializer(data=profile_data)
ser.is_valid()
return dict(ser.data)


def get_data_gather_data() -> dict:
return {}
8 changes: 5 additions & 3 deletions test/apiserver/test_agent_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,12 @@ def test_rep_agent_config(self):
self.assertEqual(res.status_code, 200)
self.assertEqual(
data["data"],
{},
{"report_validated_sink": False},
)

def test_rep_agent_config2(self):
agent = IastAgent.objects.filter(pk=self.agent_id).first()
assert agent is not None
agent.bind_project.log_level = "INFO"
agent.bind_project.enable_log = True
agent.bind_project.save()
Expand All @@ -48,11 +49,12 @@ def test_rep_agent_config2(self):
self.assertEqual(res.status_code, 200)
self.assertEqual(
data["data"],
{"enable_log": True, "log_level": "INFO"},
{"enable_log": True, "log_level": "INFO", "report_validated_sink": False},
)

def test_rep_agent_config3(self):
agent = IastAgent.objects.filter(pk=self.agent_id).first()
assert agent is not None
agent.bind_project.log_level = "INFO"
agent.bind_project.save()
res = self.client.get(
Expand All @@ -63,5 +65,5 @@ def test_rep_agent_config3(self):
self.assertEqual(res.status_code, 200)
self.assertEqual(
data["data"],
{"log_level": "INFO"},
{"log_level": "INFO", "report_validated_sink": False},
)