diff --git a/nsupdate/api/views.py b/nsupdate/api/views.py index d04fa44..111e8d1 100644 --- a/nsupdate/api/views.py +++ b/nsupdate/api/views.py @@ -139,9 +139,14 @@ def check_api_auth(username, password): host = Host.get_by_fqdn(fqdn) except ValueError: return None - if host is None or not check_password(password, host.update_secret): - return None - return host + if host is not None: + ok = check_password(password, host.update_secret) + success_msg = ('failure', 'success')[ok] + msg = "api authentication %s. [hostname: %s (given in basic auth)]" % (success_msg, fqdn, ) + host.register_api_auth_result(msg, fault=not ok) + if ok: + return host + return None def check_session_auth(user, hostname): diff --git a/nsupdate/main/migrations/0004_auto_20141115_2349.py b/nsupdate/main/migrations/0004_auto_20141115_2349.py new file mode 100644 index 0000000..48ac76d --- /dev/null +++ b/nsupdate/main/migrations/0004_auto_20141115_2349.py @@ -0,0 +1,26 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import models, migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('main', '0003_auto_20141115_2230'), + ] + + operations = [ + migrations.AddField( + model_name='host', + name='api_auth_faults', + field=models.PositiveIntegerField(default=0, verbose_name='api auth faults'), + preserve_default=True, + ), + migrations.AddField( + model_name='host', + name='api_auth_result_msg', + field=models.CharField(default=b'', max_length=255, blank=True, help_text='Latest result message relating to api authentication', null=True, verbose_name='api auth result msg'), + preserve_default=True, + ), + ] diff --git a/nsupdate/main/models.py b/nsupdate/main/models.py index 8da8cd3..398cbc8 100644 --- a/nsupdate/main/models.py +++ b/nsupdate/main/models.py @@ -220,6 +220,14 @@ class Host(models.Model): default='', blank=True, null=True, help_text=_("Latest result message relating to the server")) + # count api auth errors - maybe caused by host owner (misconfigured update client) + api_auth_faults = models.PositiveIntegerField(_("api auth faults"), default=0) + api_auth_result_msg = models.CharField( + _("api auth result msg"), + max_length=RESULT_MSG_LEN, + default='', blank=True, null=True, + help_text=_("Latest result message relating to api authentication")) + # when we received the last update for v4/v6 addr last_update_ipv4 = models.DateTimeField(_("last update IPv4"), blank=True, null=True) last_update_ipv6 = models.DateTimeField(_("last update IPv6"), blank=True, null=True) @@ -296,6 +304,12 @@ class Host(models.Model): self.server_result_msg = result_fmt(msg) self.save() + def register_api_auth_result(self, msg, fault=False): + if fault: + self.api_auth_faults += 1 + self.api_auth_result_msg = result_fmt(msg) + self.save() + def generate_secret(self, secret=None): # note: we use a quick hasher for the update_secret as expensive # more modern hashes might put too much load on the servers. also diff --git a/nsupdate/main/templates/main/host.html b/nsupdate/main/templates/main/host.html index 1518c06..af48aa4 100644 --- a/nsupdate/main/templates/main/host.html +++ b/nsupdate/main/templates/main/host.html @@ -131,6 +131,8 @@