2025-01-03 09:47:18 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
2025-01-08 06:22:54 +00:00
|
|
|
from django.db.models import Count, F, Q
|
2025-01-03 09:47:18 +00:00
|
|
|
from django.http.response import JsonResponse
|
|
|
|
from rest_framework.views import APIView
|
|
|
|
|
2025-01-09 08:11:37 +00:00
|
|
|
from accounts.models import (
|
|
|
|
Account, RiskChoice, GatherAccountsAutomation,
|
2025-01-09 11:16:26 +00:00
|
|
|
PushAccountAutomation, BackupAccountAutomation,
|
|
|
|
AccountRisk, IntegrationApplication, ChangeSecretAutomation
|
2025-01-09 08:11:37 +00:00
|
|
|
)
|
2025-01-08 06:22:54 +00:00
|
|
|
from assets.const import AllTypes
|
2025-01-06 08:52:12 +00:00
|
|
|
from common.utils.timezone import local_monday
|
2025-01-03 09:47:18 +00:00
|
|
|
|
|
|
|
__all__ = ['PamDashboardApi']
|
|
|
|
|
|
|
|
|
|
|
|
class PamDashboardApi(APIView):
|
|
|
|
http_method_names = ['get']
|
|
|
|
rbac_perms = {
|
|
|
|
'GET': 'accounts.view_account',
|
|
|
|
}
|
|
|
|
|
2025-01-08 06:22:54 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_type_to_accounts():
|
|
|
|
result = Account.objects.annotate(type=F('asset__platform__type')). \
|
|
|
|
values('type').order_by('type').annotate(total=Count(1))
|
|
|
|
all_types_dict = dict(AllTypes.choices())
|
|
|
|
|
|
|
|
result = [
|
|
|
|
{
|
|
|
|
**i,
|
|
|
|
'label': all_types_dict.get(i['type'], i['type'])
|
|
|
|
}
|
|
|
|
for i in result
|
|
|
|
]
|
|
|
|
return result
|
|
|
|
|
2025-01-03 09:47:18 +00:00
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
data = {}
|
2025-01-08 06:22:54 +00:00
|
|
|
monday_time = local_monday()
|
|
|
|
query_params = self.request.query_params
|
|
|
|
|
|
|
|
account_stats = Account.objects.aggregate(
|
|
|
|
total_count=Count('id'),
|
|
|
|
privileged_count=Count('id', filter=Q(privileged=True)),
|
|
|
|
connectivity_ok_count=Count('id', filter=Q(connectivity='ok')),
|
|
|
|
secret_reset_count=Count('id', filter=Q(secret_reset=True)),
|
|
|
|
unavailable_count=Count('id', filter=Q(is_active=False)),
|
|
|
|
week_add_count=Count('id', filter=Q(date_created__gte=monday_time)),
|
|
|
|
)
|
|
|
|
|
|
|
|
_all = query_params.get('all')
|
|
|
|
|
|
|
|
if _all or query_params.get('total_accounts'):
|
|
|
|
data['total_accounts'] = account_stats['total_count']
|
|
|
|
|
|
|
|
if _all or query_params.get('total_week_add_accounts'):
|
|
|
|
data['total_week_add_accounts'] = account_stats['week_add_count']
|
|
|
|
|
|
|
|
if _all or query_params.get('total_privileged_accounts'):
|
|
|
|
data['total_privileged_accounts'] = account_stats['privileged_count']
|
2025-01-03 09:47:18 +00:00
|
|
|
|
2025-01-08 06:22:54 +00:00
|
|
|
if _all or query_params.get('total_connectivity_ok_accounts'):
|
|
|
|
data['total_connectivity_ok_accounts'] = account_stats['connectivity_ok_count']
|
2025-01-03 09:47:18 +00:00
|
|
|
|
2025-01-08 06:22:54 +00:00
|
|
|
if _all or query_params.get('total_secret_reset_accounts'):
|
|
|
|
data['total_secret_reset_accounts'] = account_stats['secret_reset_count']
|
2025-01-06 08:52:12 +00:00
|
|
|
|
2025-01-08 06:22:54 +00:00
|
|
|
if _all or query_params.get('total_ordinary_accounts'):
|
|
|
|
data['total_ordinary_accounts'] = account_stats['total_count'] - account_stats['privileged_count']
|
2025-01-03 09:47:18 +00:00
|
|
|
|
2025-01-08 06:22:54 +00:00
|
|
|
if _all or query_params.get('total_unavailable_accounts'):
|
|
|
|
data['total_unavailable_accounts'] = account_stats['unavailable_count']
|
2025-01-03 09:47:18 +00:00
|
|
|
|
2025-01-08 06:22:54 +00:00
|
|
|
if _all or query_params.get('total_unmanaged_accounts'):
|
|
|
|
data['total_unmanaged_accounts'] = Account.get_risks(
|
|
|
|
risk_type=RiskChoice.new_found).count()
|
2025-01-03 09:47:18 +00:00
|
|
|
|
2025-01-08 06:22:54 +00:00
|
|
|
if _all or query_params.get('total_long_time_no_login_accounts'):
|
|
|
|
data['total_long_time_no_login_accounts'] = Account.get_risks(
|
|
|
|
risk_type=RiskChoice.long_time_no_login).count()
|
2025-01-03 09:47:18 +00:00
|
|
|
|
2025-01-08 06:22:54 +00:00
|
|
|
if _all or query_params.get('total_weak_password_accounts'):
|
|
|
|
data['total_weak_password_accounts'] = Account.get_risks(
|
|
|
|
risk_type=RiskChoice.weak_password).count()
|
2025-01-06 08:52:12 +00:00
|
|
|
|
2025-01-08 06:22:54 +00:00
|
|
|
if _all or query_params.get('total_long_time_change_password_accounts'):
|
|
|
|
data['total_long_time_change_password_accounts'] = Account.get_risks(
|
|
|
|
risk_type=RiskChoice.long_time_password).count()
|
2025-01-03 09:47:18 +00:00
|
|
|
|
2025-01-10 08:19:07 +00:00
|
|
|
if _all or query_params.get('total_leaked_password_accounts'):
|
|
|
|
data['total_leaked_password_accounts'] = Account.get_risks(
|
|
|
|
risk_type=RiskChoice.leaked_password).count()
|
|
|
|
|
|
|
|
if _all or query_params.get('total_repeated_password_accounts'):
|
|
|
|
data['total_repeated_password_accounts'] = Account.get_risks(
|
|
|
|
risk_type=RiskChoice.repeated_password).count()
|
|
|
|
|
2025-01-09 08:11:37 +00:00
|
|
|
if _all or query_params.get('total_count_type_to_accounts'):
|
2025-01-08 06:22:54 +00:00
|
|
|
data.update({
|
2025-01-09 08:11:37 +00:00
|
|
|
'total_count_type_to_accounts': self.get_type_to_accounts(),
|
|
|
|
})
|
|
|
|
|
2025-01-09 11:16:26 +00:00
|
|
|
if _all or query_params.get('total_count_change_secret_automation'):
|
|
|
|
data.update({
|
|
|
|
'total_count_change_secret_automation': ChangeSecretAutomation.objects.count()
|
|
|
|
})
|
|
|
|
|
2025-01-09 08:11:37 +00:00
|
|
|
if _all or query_params.get('total_count_gathered_account_automation'):
|
|
|
|
data.update({
|
|
|
|
'total_count_gathered_account_automation': GatherAccountsAutomation.objects.count()
|
|
|
|
})
|
|
|
|
|
|
|
|
if _all or query_params.get('total_count_push_account_automation'):
|
|
|
|
data.update({
|
|
|
|
'total_count_push_account_automation': PushAccountAutomation.objects.count()
|
|
|
|
})
|
|
|
|
|
|
|
|
if _all or query_params.get('total_count_backup_account_automation'):
|
|
|
|
data.update({
|
|
|
|
'total_count_backup_account_automation': BackupAccountAutomation.objects.count()
|
|
|
|
})
|
|
|
|
|
|
|
|
if _all or query_params.get('total_count_risk_account'):
|
|
|
|
data.update({
|
|
|
|
'total_count_risk_account': AccountRisk.objects.count()
|
|
|
|
})
|
2025-01-09 11:16:26 +00:00
|
|
|
|
2025-01-09 08:11:37 +00:00
|
|
|
if _all or query_params.get('total_count_integration_application'):
|
|
|
|
data.update({
|
|
|
|
'total_count_integration_application': IntegrationApplication.objects.count()
|
2025-01-08 06:22:54 +00:00
|
|
|
})
|
2025-01-03 09:47:18 +00:00
|
|
|
|
|
|
|
return JsonResponse(data, status=200)
|