Skip to content
This repository has been archived by the owner on Jul 9, 2020. It is now read-only.

Commit

Permalink
Refactored utils.forbid_unallowed in order to log request details
Browse files Browse the repository at this point in the history
  • Loading branch information
nemesifier committed Feb 29, 2016
1 parent 80806d7 commit dba328c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
8 changes: 4 additions & 4 deletions django_netjsonconfig/controller/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def checksum(request, pk):
returns configuration checksum
"""
config = get_config_or_404(pk)
bad_request = forbid_unallowed(request.GET, 'key', config.key)
bad_request = forbid_unallowed(request, 'GET', 'key', config.key)
if bad_request:
return bad_request
update_last_ip(config, request)
Expand All @@ -26,7 +26,7 @@ def download_config(request, pk):
returns configuration archive as attachment
"""
config = get_config_or_404(pk)
return (forbid_unallowed(request.GET, 'key', config.key) or
return (forbid_unallowed(request, 'GET', 'key', config.key) or
send_config(config, request))


Expand All @@ -42,7 +42,7 @@ def report_status(request, pk):
required_params = [('key', config.key),
('status', allowed_status)]
for key, value in required_params:
bad_response = forbid_unallowed(request.POST, key, value)
bad_response = forbid_unallowed(request, 'POST', key, value)
if bad_response:
return bad_response
config.status = request.POST.get('status')
Expand All @@ -65,7 +65,7 @@ def register(request):
('name', None),
('backend', allowed_backends)]
for key, value in required_params:
bad_response = forbid_unallowed(request.POST, key, value)
bad_response = forbid_unallowed(request, 'POST', key, value)
if bad_response:
return bad_response
# create new Config
Expand Down
17 changes: 9 additions & 8 deletions django_netjsonconfig/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,16 @@ def update_last_ip(config, request):
config.save()


def forbid_unallowed(params, param, allowed_values=None):
value = params.get(param)
def forbid_unallowed(request, param_group, param, allowed_values=None):
error = None
value = getattr(request, param_group).get(param)
if not value:
msg = 'error: missing required parameter "{}"\n'.format(param)
logger.warning(msg)
return ControllerResponse(msg, content_type='text/plain', status=400)
error = 'error: missing required parameter "{}"\n'.format(param)
logger.warning(error, extra={'request': request, 'stack': True})
return ControllerResponse(error, content_type='text/plain', status=400)
if allowed_values and not isinstance(allowed_values, list):
allowed_values = [allowed_values]
if allowed_values is not None and value not in allowed_values:
msg = 'error: wrong {}\n'.format(param)
logger.warning(msg)
return ControllerResponse(msg, content_type='text/plain', status=403)
error = 'error: wrong {}\n'.format(param)
logger.warning(error, extra={'request': request, 'stack': True})
return ControllerResponse(error, content_type='text/plain', status=403)

0 comments on commit dba328c

Please sign in to comment.