implement / configure logging to stderr, reorder actions for update api, so that a lot of info can be logged

This commit is contained in:
Thomas Waldmann 2013-09-28 21:41:47 +02:00
parent 44770d9cbd
commit 60353a6429
2 changed files with 34 additions and 9 deletions

View File

@ -1,4 +1,8 @@
# -*- coding: utf-8 -*-
import logging
logger = logging.getLogger(__name__)
from django.http import HttpResponse
from django.conf import settings
from main.forms import *
@ -39,29 +43,35 @@ def check_auth(username, password):
return password == 'pass' # FIXME
def Response(content, logmsg=None):
def Response(content):
return HttpResponse(content, content_type='text/plain')
def NicUpdateView(request):
agent = request.META.get('HTTP_USER_AGENT')
if agent in settings.BAD_AGENTS:
return Response('badagent')
hostname = request.GET.get('hostname')
auth = request.META.get('HTTP_AUTHORIZATION')
if auth is None:
logger.warning('%s - received no auth' % (hostname, ))
return basic_challenge("authenticate to update DNS")
username, password = basic_authenticate(auth)
if not check_auth(username, password):
logger.info('%s - received bad credentials, username: %s' % (hostname, username, ))
return Response('badauth')
# as we use update_username == hostname, we can fall back to that:
hostname = request.GET.get('hostname', username)
# XXX when do we return Response('badhost') ?
if hostname is None:
# as we use update_username == hostname, we can fall back to that:
hostname = username
ipaddr = request.GET.get('myip')
if ipaddr is None:
ipaddr = request.META.get('REMOTE_ADDR')
agent = request.META.get('HTTP_USER_AGENT')
if agent in settings.BAD_AGENTS:
logger.info('%s - received update from bad user agent %s' % (hostname, agent, ))
return Response('badagent')
ipaddr = str(ipaddr) # XXX bug in dnspython: crashes if ipaddr is unicode, wants a str!
try:
update(hostname, ipaddr)
logger.info('%s - received good update -> ip: %s' % (hostname, ipaddr, ))
return Response('good %s' % ipaddr)
except SameIpError:
logger.warning('%s - received no-change update, ip: %s' % (hostname, ipaddr, ))
return Response('nochg %s' % ipaddr)

View File

@ -169,15 +169,30 @@ LOGGING = {
'level': 'ERROR',
'filters': ['require_debug_false'],
'class': 'django.utils.log.AdminEmailHandler'
},
'stderr': {
'level': 'DEBUG',
'class': 'logging.StreamHandler',
'formatter': 'stderr'
}
},
'loggers': {
'api.views': {
'handlers': ['stderr', ],
'level': 'DEBUG',
'propagate': True,
},
'django.request': {
'handlers': ['mail_admins'],
'handlers': ['mail_admins', ],
'level': 'ERROR',
'propagate': True,
},
}
},
'formatters': {
'stderr': {
'format': '[%(asctime)s] %(levelname)s %(message)s'
},
},
}
ACCOUNT_ACTIVATION_DAYS = 7