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

ADCM-5539 #2154

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
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
4 changes: 3 additions & 1 deletion python/audit/cef_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class CEFLogConstants:
device_product: str = "Arenadata Cluster Manager"
adcm_version: str = settings.ADCM_VERSION
operation_name_session: str = "User logged"
extension_keys: tuple[str, ...] = ("actor", "act", "operation", "resource", "result", "timestamp", "address")
extension_keys: tuple[str, ...] = ("actor", "act", "operation", "resource", "result", "timestamp", "address", "agent")


def cef_logger(
Expand All @@ -49,6 +49,7 @@ def cef_logger(
extension["result"] = audit_instance.login_result
extension["timestamp"] = str(audit_instance.login_time)
extension["address"] = audit_instance.address
extension["agent"] = audit_instance.agent

elif isinstance(audit_instance, AuditLog):
operation_name = audit_instance.operation_name
Expand All @@ -63,6 +64,7 @@ def cef_logger(
severity = 3
extension["timestamp"] = str(audit_instance.operation_time)
extension["address"] = audit_instance.address
extension["agent"] = audit_instance.agent

else:
raise NotImplementedError
Expand Down
5 changes: 4 additions & 1 deletion python/audit/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from audit.cef_logger import cef_logger
from audit.models import AuditSession, AuditSessionLoginResult, AuditUser
from audit.utils import get_client_ip
from audit.utils import get_client_agent


class LoginMiddleware:
Expand All @@ -35,6 +36,7 @@ def __init__(self, get_response):
def _audit(
request_path: str,
request_host: str | None,
request_agent: str | None,
user: User | AnonymousUser | None = None,
username: str = None,
) -> tuple[User | None, AuditSessionLoginResult]:
Expand All @@ -59,7 +61,7 @@ def _audit(
audit_user = AuditUser.objects.filter(username=user.username).order_by("-pk").first()

audit_session = AuditSession.objects.create(
user=audit_user, login_result=result, login_details=details, address=request_host
user=audit_user, login_result=result, login_details=details, address=request_host, agent=request_agent
)
cef_logger(audit_instance=audit_session, signature_id=resolve(request_path).route)

Expand Down Expand Up @@ -165,6 +167,7 @@ def __call__(self, request):
user, result = self._audit(
request_path=request.path,
request_host=get_client_ip(request=request),
request_agent=get_client_agent(request=request),
user=request.user,
username=username,
)
Expand Down
35 changes: 35 additions & 0 deletions python/audit/migrations/0007_add_agent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Generated by Django 3.2.19 on 2023-11-14 08:35

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("audit", "0006_add_address"),
]

operations = [
migrations.AddField(
model_name="auditlog",
name="agent",
field=models.CharField(max_length=255, null=True),
),
migrations.AddField(
model_name="auditsession",
name="agent",
field=models.CharField(max_length=255, null=True),
),
]

2 changes: 2 additions & 0 deletions python/audit/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ class AuditLog(Model):
user = ForeignKey(AuditUser, on_delete=CASCADE, null=True)
object_changes = JSONField(default=dict)
address = CharField(max_length=255, null=True)
agent = CharField(max_length=255, null=True)


class AuditSession(Model):
Expand All @@ -103,6 +104,7 @@ class AuditSession(Model):
login_time = DateTimeField(auto_now_add=True)
login_details = JSONField(default=dict, null=True)
address = CharField(max_length=255, null=True)
agent = CharField(max_length=255, null=True)


@dataclass
Expand Down
10 changes: 9 additions & 1 deletion python/audit/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,7 @@ def wrapped(*args, **kwargs):
user=audit_user,
object_changes=object_changes,
address=get_client_ip(request=request),
agent=get_client_agent(request=request),
)
cef_logger(audit_instance=auditlog, signature_id=resolve(request.path).route)

Expand Down Expand Up @@ -620,7 +621,14 @@ def get_client_ip(request: WSGIRequest) -> str | None:
break

return host



def get_client_agent(request: WSGIRequest) -> str | None:
user_agent = request.META.get("HTTP_USER_AGENT")
if user_agent:
return user_agent[:255]
return None


def audit_job_finish(owner: NamedCoreObject, display_name: str, is_upgrade: bool, job_result: ExecutionStatus) -> None:
operation_name = f"{display_name} {'upgrade' if is_upgrade else 'action'} completed"
Expand Down