Skip to content

Commit 36ee040

Browse files
authored
Build: use a longer and more accurate expiricy time for API key (#12425)
This formula considers builds that are retried multiple times.
1 parent 2134030 commit 36ee040

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

readthedocs/api/v2/models.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,17 @@ def create_key(self, project):
1616
"""
1717
Create a new API key for a project.
1818
19-
Build API keys are valid for 3 hours,
19+
Build API keys are valid for
20+
21+
- project or default build time limit
22+
- plus 25% to cleanup task once build is finished
23+
- plus extra time to allow multiple retries (concurrency limit reached)
24+
2025
and can be revoked at any time by hitting the /api/v2/revoke/ endpoint.
2126
"""
22-
# Use the project or default build time limit + 25% for the API token
23-
delta = (project.container_time_limit or settings.BUILD_TIME_LIMIT) * 1.25
27+
delta = (
28+
project.container_time_limit or settings.BUILD_TIME_LIMIT
29+
) * 1.25 + settings.RTD_BUILDS_RETRY_DELAY * settings.RTD_BUILDS_MAX_RETRIES
2430
expiry_date = timezone.now() + timedelta(seconds=delta)
2531
name_max_length = self.model._meta.get_field("name").max_length
2632
return super().create_key(

readthedocs/rtd_tests/tests/test_api.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -766,6 +766,19 @@ def test_revoke_build_api_key(self):
766766
self.assertEqual(resp.status_code, 204)
767767
self.assertFalse(BuildAPIKey.objects.is_valid(build_api_key))
768768

769+
def test_expiricy_key(self):
770+
project = get(Project)
771+
build_api_key_obj, build_api_key = BuildAPIKey.objects.create_key(project)
772+
expected = (build_api_key_obj.expiry_date - timezone.now()).seconds
773+
self.assertAlmostEqual(expected, 8250, delta=5)
774+
775+
# Project with a custom containe time limit
776+
project.container_time_limit = 1200
777+
project.save()
778+
build_api_key_obj, build_api_key = BuildAPIKey.objects.create_key(project)
779+
expected = (build_api_key_obj.expiry_date - timezone.now()).seconds
780+
self.assertAlmostEqual(expected, 9000, delta=5)
781+
769782
def test_user_doesnt_get_full_api_return(self):
770783
user_normal = get(User, is_staff=False)
771784
user_admin = get(User, is_staff=True)

0 commit comments

Comments
 (0)