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

Enterprise API Usage Report #35532

Merged
merged 6 commits into from
Dec 18, 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
2 changes: 2 additions & 0 deletions corehq/apps/enterprise/api/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
ODataFeedResource,
WebUserResource,
SMSResource,
APIUsageResource,
TwoFactorAuthResource,
)

Expand All @@ -19,4 +20,5 @@
v1_api.register(ODataFeedResource())
v1_api.register(CommCareVersionComplianceResource())
v1_api.register(SMSResource())
v1_api.register(APIUsageResource())
v1_api.register(TwoFactorAuthResource())
23 changes: 23 additions & 0 deletions corehq/apps/enterprise/api/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -438,3 +438,26 @@ def dehydrate(self, bundle):

def get_primary_keys(self):
return ('mobile_worker', 'domain',)


class APIUsageResource(ODataEnterpriseReportResource):
web_user = fields.CharField()
api_key_name = fields.CharField()
scope = fields.CharField()
expiration_date = fields.DateTimeField()
created_date = fields.DateTimeField()
last_used_date = fields.DateTimeField()

REPORT_SLUG = EnterpriseReport.API_USAGE

def dehydrate(self, bundle):
bundle.data['web_user'] = bundle.obj[0]
bundle.data['api_key_name'] = bundle.obj[1]
bundle.data['scope'] = bundle.obj[2]
bundle.data['expiration_date'] = self.convert_datetime(bundle.obj[3])
bundle.data['created_date'] = self.convert_datetime(bundle.obj[4])
bundle.data['last_used_date'] = self.convert_datetime(bundle.obj[5])
return bundle

def get_primary_keys(self):
return ('web_user', 'api_key_name',)
56 changes: 54 additions & 2 deletions corehq/apps/enterprise/enterprise.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from abc import ABC, abstractmethod
import re
from django.db.models import Count
from datetime import datetime, timedelta

from django.conf import settings
from django.contrib.auth.models import User
from django.db.models import Count, Subquery, Q
from django.utils.translation import gettext as _
from django.utils.translation import gettext_lazy

Expand Down Expand Up @@ -36,7 +37,7 @@
get_mobile_user_count,
get_web_user_count,
)
from corehq.apps.users.models import CouchUser, Invitation
from corehq.apps.users.models import CouchUser, HQApiKey, Invitation, WebUser


class EnterpriseReport(ABC):
Expand All @@ -47,6 +48,7 @@ class EnterpriseReport(ABC):
ODATA_FEEDS = 'odata_feeds'
COMMCARE_VERSION_COMPLIANCE = 'commcare_version_compliance'
SMS = 'sms'
API_USAGE = 'api_usage'
TWO_FACTOR_AUTH = '2fa'

DATE_ROW_FORMAT = '%Y/%m/%d %H:%M:%S'
Expand Down Expand Up @@ -95,6 +97,8 @@ def create(cls, slug, account_id, couch_user, **kwargs):
report = EnterpriseCommCareVersionReport(account, couch_user, **kwargs)
elif slug == cls.SMS:
report = EnterpriseSMSReport(account, couch_user, **kwargs)
elif slug == cls.API_USAGE:
report = EnterpriseAPIReport(account, couch_user, **kwargs)
elif slug == cls.TWO_FACTOR_AUTH:
report = Enterprise2FAReport(account, couch_user, **kwargs)

Expand Down Expand Up @@ -566,6 +570,54 @@ def rows_for_domain(self, domain_obj):
return [(domain_obj.name, num_sent, num_received, num_errors), ]


class EnterpriseAPIReport(EnterpriseReport):
title = gettext_lazy('API Usage')
total_description = gettext_lazy('# of Active API Keys')

@property
def headers(self):
return [_('Web User'), _('API Key Name'), _('Scope'), _('Expiration Date [UTC]'), _('Created On [UTC]'),
_('Last Used On [UTC]')]

@property
def rows(self):
return [self._get_api_key_row(api_key) for api_key in self.unique_api_keys()]

@property
def total(self):
return self.unique_api_keys().count()

def unique_api_keys(self):
usernames = self.account.get_web_user_usernames()
user_ids = User.objects.filter(username__in=usernames).values_list('id', flat=True)
domains = self.account.get_domains()

return HQApiKey.objects.filter(
user_id__in=Subquery(user_ids),
is_active=True
).filter(
Q(domain__in=domains) | Q(domain='')
)

def _get_api_key_row(self, api_key):
if api_key.domain:
scope = api_key.domain
else:
user_domains = set(WebUser.get_by_username(api_key.user.username).get_domains())
account_domains = set(self.account.get_domains())
intersected_domains = user_domains.intersection(account_domains)
scope = ', '.join((intersected_domains))

return [
api_key.user.username,
api_key.name,
scope,
self.format_date(api_key.expiration_date),
self.format_date(api_key.created),
self.format_date(api_key.last_used),
]


class Enterprise2FAReport(EnterpriseReport):
title = gettext_lazy('Two Factor Authentication')
total_description = gettext_lazy('# of Project Spaces without 2FA')
Expand Down
3 changes: 2 additions & 1 deletion corehq/apps/enterprise/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,12 @@ def security_center(request, domain):

context.update({
'reports': [EnterpriseReport.create(slug, request.account.id, request.couch_user) for slug in (
EnterpriseReport.API_USAGE,
EnterpriseReport.TWO_FACTOR_AUTH,
)],
'metric_type': 'Security Center',
'max_date_range_days': EnterpriseFormReport.MAX_DATE_RANGE_DAYS,
'uses_date_range': [],
'metric_type': 'Security Center',
})

return render(request, "enterprise/project_dashboard.html", context)
Expand Down
Loading