annotate MoinMoin/util/abuse.py @ 6076:f4ec84967c27

add colon to make log_attempt even more parseable
author 'Karl O. Pinc' <kop@meme.com>
date Mon, 15 Sep 2014 22:10:45 -0500
parents 3f7f4cef7c2a
children
rev   line source
6045
f029e42ecdec add logging for login to detect potential abuse
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
1 # -*- coding: iso-8859-1 -*-
f029e42ecdec add logging for login to detect potential abuse
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
2 """
f029e42ecdec add logging for login to detect potential abuse
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
3 MoinMoin - (ab)use logging
f029e42ecdec add logging for login to detect potential abuse
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
4 Log some data that can be used for usage analysis and abuse detection.
f029e42ecdec add logging for login to detect potential abuse
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
5
f029e42ecdec add logging for login to detect potential abuse
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
6 This logging functionality is kept in this separate module so we can
f029e42ecdec add logging for login to detect potential abuse
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
7 easily redirect the output to a separate log using logging configuration.
f029e42ecdec add logging for login to detect potential abuse
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
8
f029e42ecdec add logging for login to detect potential abuse
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
9 @copyright: 2013 MoinMoin:ThomasWaldmann
f029e42ecdec add logging for login to detect potential abuse
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
10 @license: GNU GPL, see COPYING for details.
f029e42ecdec add logging for login to detect potential abuse
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
11 """
f029e42ecdec add logging for login to detect potential abuse
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
12
f029e42ecdec add logging for login to detect potential abuse
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
13 from MoinMoin import log
f029e42ecdec add logging for login to detect potential abuse
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
14 logging = log.getLogger(__name__)
f029e42ecdec add logging for login to detect potential abuse
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
15
f029e42ecdec add logging for login to detect potential abuse
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
16
6070
2e0530a78920 use pagename as argument instead of name
'Karl O. Pinc' <kop@meme.com>
parents: 6066
diff changeset
17 def log_attempt(system, success, request=None, username=None, pagename=None):
6045
f029e42ecdec add logging for login to detect potential abuse
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
18 """
f029e42ecdec add logging for login to detect potential abuse
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
19 log attempts to use <system>, log success / failure / username / ip
f029e42ecdec add logging for login to detect potential abuse
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
20
f029e42ecdec add logging for login to detect potential abuse
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
21 @param system: some string telling about the system that was used, e.g.
6075
3f7f4cef7c2a make log_attempt output easily parsed
'Karl O. Pinc' <kop@meme.com>
parents: 6070
diff changeset
22 "auth/login" or "textcha"
6045
f029e42ecdec add logging for login to detect potential abuse
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
23 @param success: whether the attempt was successful
f029e42ecdec add logging for login to detect potential abuse
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
24 @param request: request object (optional, to determine remote's ip address)
f029e42ecdec add logging for login to detect potential abuse
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
25 @param username: user's name (optional, if None: determined from request)
6070
2e0530a78920 use pagename as argument instead of name
'Karl O. Pinc' <kop@meme.com>
parents: 6066
diff changeset
26 @param pagename: name of the page (optional)
6045
f029e42ecdec add logging for login to detect potential abuse
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
27 """
f029e42ecdec add logging for login to detect potential abuse
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
28 if username is None:
6065
bbbfb024a967 log abuse when abused action is not presented to user
'Karl O. Pinc' <kop@meme.com>
parents: 6045
diff changeset
29 if request and hasattr(request, 'user') and request.user.valid:
6045
f029e42ecdec add logging for login to detect potential abuse
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
30 username = request.user.name
f029e42ecdec add logging for login to detect potential abuse
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
31 else:
f029e42ecdec add logging for login to detect potential abuse
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
32 username = u'anonymous'
f029e42ecdec add logging for login to detect potential abuse
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
33 level = (logging.WARNING, logging.INFO)[success]
6076
f4ec84967c27 add colon to make log_attempt even more parseable
'Karl O. Pinc' <kop@meme.com>
parents: 6075
diff changeset
34 msg = """: %s: status %s: username "%s": ip %s: page %s"""
6045
f029e42ecdec add logging for login to detect potential abuse
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
35 status = ("failure", "success")[success]
f029e42ecdec add logging for login to detect potential abuse
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
36 ip = request and request.remote_addr or 'unknown'
6070
2e0530a78920 use pagename as argument instead of name
'Karl O. Pinc' <kop@meme.com>
parents: 6066
diff changeset
37 logging.log(level, msg, system, status, username, ip, pagename)