~coreygoldberg/canonical-identity-provider/fix-helper-import

« back to all changes in this revision

Viewing changes to identityprovider/middleware/util.py

  • Committer: Tarmac
  • Author(s): David Owen
  • Date: 2011-12-16 15:46:14 UTC
  • mfrom: (240.2.10 log-request)
  • Revision ID: tarmac@199959-20111216154614-53iyzgg25yrpdh8a
[r=bloodearnest] Extracted log_request and helpers from process_exception into new util module.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2011 Canonical Ltd.  This software is licensed under the
 
2
# GNU Affero General Public License version 3 (see the file LICENSE).
 
3
 
 
4
import logging
 
5
import re
 
6
import sys
 
7
import traceback
 
8
 
 
9
from django.conf import settings
 
10
from django.db import connection
 
11
from django.views import debug
 
12
 
 
13
debug.HIDDEN_SETTINGS = re.compile(
 
14
    'SECRET|PASSWORD|PROFANITIES_LIST|PRIVATE|secret|password|private')
 
15
 
 
16
 
 
17
def _sanitize_dict(dirty):
 
18
    clean = dirty.copy()
 
19
    for key in dirty.iterkeys():
 
20
        if debug.HIDDEN_SETTINGS.search(key):
 
21
            clean[key] = '********'
 
22
    return clean
 
23
 
 
24
def _sanitize_request(request):
 
25
    """Remove sensitive information from the request before it is
 
26
    displayed."""
 
27
    request.GET = _sanitize_dict(request.GET)
 
28
    request.POST = _sanitize_dict(request.POST)
 
29
    return request
 
30
 
 
31
def _sanitize_vars(items):
 
32
    sanitized = []
 
33
    for item in items:
 
34
        if debug.HIDDEN_SETTINGS.search(item[0]):
 
35
            sanitized.append((item[0], '********'))
 
36
        else:
 
37
            sanitized.append(item)
 
38
    return sanitized
 
39
 
 
40
 
 
41
class _ExceptionReporter(debug.ExceptionReporter):
 
42
 
 
43
    # This function was taken from the django project.
 
44
    # Please see the license file in the third-party/django directory.
 
45
    def get_traceback_frames(self):
 
46
        frames = []
 
47
        tb = sys.exc_info()[2]
 
48
        while tb is not None:
 
49
            # support for __traceback_hide__ which is used by a few libraries
 
50
            # to hide internal frames.
 
51
            if tb.tb_frame.f_locals.get('__traceback_hide__'):
 
52
                tb = tb.tb_next
 
53
                continue
 
54
            filename = tb.tb_frame.f_code.co_filename
 
55
            function = tb.tb_frame.f_code.co_name
 
56
            lineno = tb.tb_lineno - 1
 
57
            loader = tb.tb_frame.f_globals.get('__loader__')
 
58
            module_name = tb.tb_frame.f_globals.get('__name__')
 
59
            pre_context_lineno, pre_context, context_line, post_context = \
 
60
                    self._get_lines_from_file(filename, lineno, 7, loader,
 
61
                                              module_name)
 
62
            if pre_context_lineno is not None:
 
63
                frames.append({
 
64
                    'tb': tb,
 
65
                    'filename': filename,
 
66
                    'function': function,
 
67
                    'lineno': lineno + 1,
 
68
                    'vars': _sanitize_vars(tb.tb_frame.f_locals.items()),
 
69
                    'id': id(tb),
 
70
                    'pre_context': pre_context,
 
71
                    'context_line': context_line,
 
72
                    'post_context': post_context,
 
73
                    'pre_context_lineno': pre_context_lineno + 1,
 
74
                })
 
75
            tb = tb.tb_next
 
76
 
 
77
        if not frames:
 
78
            frames = [{
 
79
                'filename': '<unknown>',
 
80
                'function': '?',
 
81
                'lineno': '?',
 
82
                'context_line': '???',
 
83
            }]
 
84
 
 
85
        return frames
 
86
 
 
87
 
 
88
def log_request(request):
 
89
    """Log the request, so that it can be kept in the oops file"""
 
90
    request = _sanitize_request(request)
 
91
    reporter = _ExceptionReporter(request, *sys.exc_info())
 
92
    template_debug = settings.TEMPLATE_DEBUG
 
93
    settings.TEMPLATE_DEBUG = True
 
94
    try:
 
95
        logging.exception('Unhandled exception in application')
 
96
        logging.warn(reporter.get_traceback_html())
 
97
        logging.warn(traceback.format_exc())
 
98
        for query in connection.queries:
 
99
            logging.warn("time: %(time)s sql: %(sql)s" % query)
 
100
    finally:
 
101
        settings.TEMPLATE_DEBUG = template_debug