Skip to content

Commit 3282550

Browse files
committed
feat:api token
1 parent 5f7b15d commit 3282550

File tree

7 files changed

+42
-6
lines changed

7 files changed

+42
-6
lines changed

docs/changelog.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## v1.1.0 🌈
4+
5+
### 🚀 Features
6+
7+
- Enable using stats view using api token
8+
39
## v1.0.2 🌈
410

511
### 🧰 Maintenance

docs/configuration.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ SCHEDULER_QUEUES = {
2222
'REDIS_CLIENT_KWARGS': { # Eventual additional Redis connection arguments
2323
'ssl_cert_reqs': None,
2424
},
25+
'TOKEN_VALIDATION_METHOD': None, # Method to validate auth-header
2526
},
2627
'high': {
2728
'URL': os.getenv('REDISTOGO_URL', 'redis://localhost:6379/0'), # If you're on Heroku
@@ -59,6 +60,13 @@ will check which job executions are pending.
5960

6061
Default: `10` (10 seconds).
6162

63+
### SCHEDULER_CONFIG: `TOKEN_VALIDATION_METHOD`
64+
65+
Method to validate request `Authorization` header with.
66+
Enables checking stats using API token.
67+
68+
Default: no tokens allowed.
69+
6270
### `SCHEDULER_QUEUES`
6371

6472
You can configure the queues to work with.

poetry.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ poetry = "^1.6"
5656
coverage = "^7"
5757
fakeredis = { version = "^2.15", extras = ['lua'] }
5858
Flake8-pyproject = "^1.2"
59-
59+
pyyaml = "^6.0"
6060

6161
[tool.poetry.extras]
6262
yaml = ["pyyaml"]

scheduler/settings.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
SCHEDULER_CONFIG = dict()
1010

1111

12+
def _token_validation(token: str) -> bool:
13+
return False
14+
15+
1216
def conf_settings():
1317
global QUEUES
1418
global SCHEDULER_CONFIG
@@ -26,6 +30,7 @@ def conf_settings():
2630
'DEFAULT_TIMEOUT': 300, # 5 minutes
2731
'SCHEDULER_INTERVAL': 10, # 10 seconds
2832
'FAKEREDIS': False, # For testing purposes
33+
'TOKEN_VALIDATION_METHOD': _token_validation, # Access stats from another application using API tokens
2934
}
3035
user_settings = getattr(settings, 'SCHEDULER_CONFIG', {})
3136
SCHEDULER_CONFIG.update(user_settings)

scheduler/tests/test_views.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ def test_statistics_json_view(self):
471471
res = self.client.get(reverse('queues_home_json'))
472472
self.assertEqual(res.status_code, 200)
473473

474-
# Not staff, only token
474+
# Not staff => return 404
475475
self.user.is_staff = False
476476
self.user.save()
477477

@@ -481,3 +481,18 @@ def test_statistics_json_view(self):
481481
# 404 code for stats
482482
res = self.client.get(reverse('queues_home_json'))
483483
self.assertEqual(res.status_code, 404)
484+
485+
@staticmethod
486+
def token_validation(token: str) -> bool:
487+
return token == 'valid'
488+
489+
@patch('scheduler.views.SCHEDULER_CONFIG')
490+
def test_statistics_json_view_token(self, configuration):
491+
configuration.get.return_value = ViewTest.token_validation
492+
self.user.is_staff = False
493+
self.user.save()
494+
res = self.client.get(reverse('queues_home_json'), headers={'Authorization': 'valid'})
495+
self.assertEqual(res.status_code, 200)
496+
497+
res = self.client.get(reverse('queues_home_json'), headers={'Authorization': 'invalid'})
498+
self.assertEqual(res.status_code, 404)

scheduler/views.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,10 @@ def stats(request):
4646

4747

4848
def stats_json(request):
49-
# TODO support API token
50-
if request.user.is_staff:
49+
auth_token = request.headers.get('Authorization')
50+
token_validation_func = SCHEDULER_CONFIG.get('TOKEN_VALIDATION_METHOD')
51+
if (request.user.is_staff or
52+
(token_validation_func and auth_token and token_validation_func(auth_token))):
5153
return JsonResponse(get_statistics())
5254

5355
return HttpResponseNotFound()

0 commit comments

Comments
 (0)