~fgallina/python-timeline-django/no-query-fix

« back to all changes in this revision

Viewing changes to timeline_django/filters.py

  • Committer: Tarmac
  • Author(s): James Westby
  • Date: 2012-02-27 10:58:29 UTC
  • mfrom: (8.3.2 obscure-session-info)
  • Revision ID: tarmac@server-5390-20120227105829-xutkpmwzw0svpn6t
Tags: 0.0.1
[r=jml] Add filters to redact session and user queries.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (c) 2012, Canonical Ltd
 
2
#
 
3
# This program is free software: you can redistribute it and/or modify
 
4
# it under the terms of the GNU Lesser General Public License as published by
 
5
# the Free Software Foundation, version 3 only.
 
6
#
 
7
# This program is distributed in the hope that it will be useful,
 
8
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
9
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
10
# GNU Lesser General Public License for more details.
 
11
#
 
12
# You should have received a copy of the GNU Lesser General Public License
 
13
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
14
# GNU Lesser General Public License version 3 (see the file LICENSE).
 
15
 
 
16
 
 
17
SESSION_TABLE_NAME = 'django_session'
 
18
USER_TABLE_NAME = 'auth_user'
 
19
 
 
20
 
 
21
def _filter_table(query, table, reason):
 
22
    if table in query:
 
23
        return query.split(" ", 1)[0] + " <%s query redacted>" % reason
 
24
    return query
 
25
 
 
26
 
 
27
def filter_session_info(query):
 
28
    return _filter_table(query, SESSION_TABLE_NAME, 'session')
 
29
 
 
30
 
 
31
def filter_user_info(query):
 
32
    return _filter_table(query, USER_TABLE_NAME, 'user')
 
33
 
 
34
 
 
35
def _filter_query(report, filter_fn):
 
36
    timeline = report.get('timeline')
 
37
    if timeline is None:
 
38
        return
 
39
    new_timeline = []
 
40
    for event in timeline:
 
41
        start, end, category, detail = event[:4]
 
42
        detail = filter_fn(detail)
 
43
        new_timeline.append((start, end, category, detail) + event[4:])
 
44
    report['timeline'] = new_timeline
 
45
 
 
46
 
 
47
def filter_session_query(report, context):
 
48
    """Filter out queries about the session from the timeline."""
 
49
    _filter_query(report, filter_session_info)
 
50
 
 
51
 
 
52
def filter_user_query(report, context):
 
53
    """Filter out queries about the user from the timeline."""
 
54
    _filter_query(report, filter_user_info)
 
55
 
 
56
 
 
57
def install_hooks(config):
 
58
    """Install on_create hooks in the config to sanitise queries.
 
59
 
 
60
    This should be called after the `oops_timeline` hooks are instaled.
 
61
 
 
62
    :param config: the `oops.Config` to install the hooks in to.
 
63
    """
 
64
    config.on_create.append(filter_session_query)
 
65
    config.on_create.append(filter_user_query)