Skip to content

Commit 2f1cfc5

Browse files
committed
Address review comments
Signed-off-by: Tushar Goel <[email protected]>
1 parent 8429fc5 commit 2f1cfc5

File tree

3 files changed

+34
-96
lines changed

3 files changed

+34
-96
lines changed

vulnerabilities/api.py

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,7 @@
2323
from vulnerabilities.models import VulnerabilityReference
2424
from vulnerabilities.models import VulnerabilitySeverity
2525
from vulnerabilities.models import get_purl_query_lookups
26-
from vulnerabilities.throttling import AliasesAPIThrottle
27-
from vulnerabilities.throttling import BulkSearchCPEAPIThrottle
28-
from vulnerabilities.throttling import BulkSearchPackagesAPIThrottle
29-
from vulnerabilities.throttling import CPEAPIThrottle
30-
from vulnerabilities.throttling import PackagesAPIThrottle
31-
from vulnerabilities.throttling import VulnerabilitiesAPIThrottle
32-
from vulnerabilities.throttling import VulnerablePackagesAPIThrottle
26+
from vulnerabilities.throttling import StaffUserRateThrottle
3327

3428

3529
class VulnerabilitySeveritySerializer(serializers.ModelSerializer):
@@ -227,18 +221,11 @@ class PackageViewSet(viewsets.ReadOnlyModelViewSet):
227221
serializer_class = PackageSerializer
228222
filter_backends = (filters.DjangoFilterBackend,)
229223
filterset_class = PackageFilterSet
230-
231-
def get_throttles(self):
232-
if self.action == "bulk_search":
233-
throttle_classes = [BulkSearchPackagesAPIThrottle]
234-
elif self.action == "all":
235-
throttle_classes = [VulnerablePackagesAPIThrottle]
236-
else:
237-
throttle_classes = [PackagesAPIThrottle]
238-
return [throttle() for throttle in throttle_classes]
224+
throttle_classes = [StaffUserRateThrottle]
225+
throttle_scope = "packages"
239226

240227
# TODO: Fix the swagger documentation for this endpoint
241-
@action(detail=False, methods=["post"])
228+
@action(detail=False, methods=["post"], throttle_scope="bulk_search_packages")
242229
def bulk_search(self, request):
243230
"""
244231
See https://github.com/nexB/vulnerablecode/pull/369#issuecomment-796877606 for docs
@@ -270,7 +257,7 @@ def bulk_search(self, request):
270257

271258
return Response(response)
272259

273-
@action(detail=False, methods=["get"])
260+
@action(detail=False, methods=["get"], throttle_scope="vulnerable_packages")
274261
def all(self, request):
275262
"""
276263
Return all the vulnerable Package URLs.
@@ -318,7 +305,8 @@ def get_queryset(self):
318305
serializer_class = VulnerabilitySerializer
319306
filter_backends = (filters.DjangoFilterBackend,)
320307
filterset_class = VulnerabilityFilterSet
321-
throttle_classes = [VulnerabilitiesAPIThrottle]
308+
throttle_classes = [StaffUserRateThrottle]
309+
throttle_scope = "vulnerabilities"
322310

323311

324312
class CPEFilterSet(filters.FilterSet):
@@ -335,16 +323,11 @@ class CPEViewSet(viewsets.ReadOnlyModelViewSet):
335323
).distinct()
336324
serializer_class = VulnerabilitySerializer
337325
filter_backends = (filters.DjangoFilterBackend,)
326+
throttle_classes = [StaffUserRateThrottle]
338327
filterset_class = CPEFilterSet
328+
throttle_scope = "cpes"
339329

340-
def get_throttles(self):
341-
if self.action == "bulk_search":
342-
throttle_classes = [BulkSearchCPEAPIThrottle]
343-
else:
344-
throttle_classes = [CPEAPIThrottle]
345-
return [throttle() for throttle in throttle_classes]
346-
347-
@action(detail=False, methods=["post"])
330+
@action(detail=False, methods=["post"], throttle_scope="bulk_search_cpes")
348331
def bulk_search(self, request):
349332
"""
350333
This endpoint is used to search for vulnerabilities by more than one CPE.
@@ -381,4 +364,5 @@ class AliasViewSet(viewsets.ReadOnlyModelViewSet):
381364
serializer_class = VulnerabilitySerializer
382365
filter_backends = (filters.DjangoFilterBackend,)
383366
filterset_class = AliasFilterSet
384-
throttle_classes = [AliasesAPIThrottle]
367+
throttle_classes = [StaffUserRateThrottle]
368+
throttle_scope = "aliases"

vulnerabilities/throttling.py

Lines changed: 2 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,10 @@
66
# See https://github.com/nexB/vulnerablecode for support or download.
77
# See https://aboutcode.org for more information about nexB OSS projects.
88
#
9+
from rest_framework.throttling import ScopedRateThrottle
910

10-
from django.contrib.auth import get_user_model
11-
from rest_framework.throttling import SimpleRateThrottle
1211

13-
User = get_user_model()
14-
15-
16-
class StaffUserRateThrottle(SimpleRateThrottle):
12+
class StaffUserRateThrottle(ScopedRateThrottle):
1713
def allow_request(self, request, view):
1814
"""
1915
Do not apply throttling for superusers and admins.
@@ -22,42 +18,3 @@ def allow_request(self, request, view):
2218
return True
2319

2420
return super().allow_request(request, view)
25-
26-
def get_cache_key(self, request, view):
27-
"""
28-
Return the cache key to use for this request.
29-
"""
30-
if request.user.is_authenticated:
31-
ident = request.user.pk
32-
else:
33-
ident = self.get_ident(request)
34-
35-
return self.cache_format % {"scope": self.scope, "ident": ident}
36-
37-
38-
class VulnerablePackagesAPIThrottle(StaffUserRateThrottle):
39-
scope = "vulnerable_packages"
40-
41-
42-
class BulkSearchPackagesAPIThrottle(StaffUserRateThrottle):
43-
scope = "bulk_search_packages"
44-
45-
46-
class PackagesAPIThrottle(StaffUserRateThrottle):
47-
scope = "packages"
48-
49-
50-
class VulnerabilitiesAPIThrottle(StaffUserRateThrottle):
51-
scope = "vulnerabilities"
52-
53-
54-
class AliasesAPIThrottle(StaffUserRateThrottle):
55-
scope = "aliases"
56-
57-
58-
class CPEAPIThrottle(StaffUserRateThrottle):
59-
scope = "cpes"
60-
61-
62-
class BulkSearchCPEAPIThrottle(StaffUserRateThrottle):
63-
scope = "bulk_search_cpes"

vulnerablecode/settings.py

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -150,23 +150,28 @@
150150

151151
LOGIN_REDIRECT_URL = "/"
152152
LOGOUT_REDIRECT_URL = "/"
153-
TEST_PACKAGE_THROTTLING_RATE = None
154-
TEST_BULK_SEARCH_PACKAGE_THROTTLING_RATE = None
155-
TEST_ALL_VULNERABLE_PACKAGE_THROTTLING_RATE = None
156-
TEST_VULNERABILITIES_THROTTLING_RATE = None
157-
TEST_CPES_THROTTLING_RATE = None
158-
TEST_BULK_SEARCH_CPES_THROTTLING_RATE = None
159-
TEST_ALIASES_THROTTLING_RATE = None
153+
154+
REST_FRAMEWORK_DEFAULT_THROTTLE_RATES = {
155+
"vulnerable_packages": "1/hour",
156+
"bulk_search_packages": "5/hour",
157+
"packages": "10/minute",
158+
"vulnerabilities": "10/minute",
159+
"aliases": "5/minute",
160+
"cpes": "5/minute",
161+
"bulk_search_cpes": "5/hour",
162+
}
160163

161164
if IS_TESTS:
162165
VULNERABLECODEIO_REQUIRE_AUTHENTICATION = True
163-
TEST_PACKAGE_THROTTLING_RATE = "10/day"
164-
TEST_BULK_SEARCH_PACKAGE_THROTTLING_RATE = "6/day"
165-
TEST_ALL_VULNERABLE_PACKAGE_THROTTLING_RATE = "1/day"
166-
TEST_VULNERABILITIES_THROTTLING_RATE = "8/day"
167-
TEST_CPES_THROTTLING_RATE = "4/day"
168-
TEST_BULK_SEARCH_CPES_THROTTLING_RATE = "5/day"
169-
TEST_ALIASES_THROTTLING_RATE = "2/day"
166+
REST_FRAMEWORK_DEFAULT_THROTTLE_RATES = {
167+
"vulnerable_packages": "1/day",
168+
"bulk_search_packages": "6/day",
169+
"packages": "10/day",
170+
"vulnerabilities": "8/day",
171+
"aliases": "2/day",
172+
"cpes": "4/day",
173+
"bulk_search_cpes": "5/day",
174+
}
170175

171176

172177
USE_L10N = True
@@ -202,15 +207,7 @@
202207
"DEFAULT_THROTTLE_CLASSES": [
203208
"vulnerabilities.throttling.StaffUserRateThrottle",
204209
],
205-
"DEFAULT_THROTTLE_RATES": {
206-
"vulnerable_packages": TEST_ALL_VULNERABLE_PACKAGE_THROTTLING_RATE or "1/hour",
207-
"bulk_search_packages": TEST_BULK_SEARCH_PACKAGE_THROTTLING_RATE or "5/hour",
208-
"packages": TEST_PACKAGE_THROTTLING_RATE or "10/minute",
209-
"vulnerabilities": TEST_VULNERABILITIES_THROTTLING_RATE or "10/minute",
210-
"aliases": TEST_ALIASES_THROTTLING_RATE or "5/minute",
211-
"cpes": TEST_CPES_THROTTLING_RATE or "5/minute",
212-
"bulk_search_cpes": TEST_BULK_SEARCH_CPES_THROTTLING_RATE or "5/hour",
213-
},
210+
"DEFAULT_THROTTLE_RATES": REST_FRAMEWORK_DEFAULT_THROTTLE_RATES,
214211
"DEFAULT_PAGINATION_CLASS": "vulnerabilities.pagination.SmallResultSetPagination",
215212
# Limit the load on the Database returning a small number of records by default. https://github.com/nexB/vulnerablecode/issues/819
216213
"PAGE_SIZE": 10,

0 commit comments

Comments
 (0)