Browse Source

injector: add hostname/key validation using previously created library

pull/1/head
Hidden Z 14 years ago
parent
commit
f6f2d25426
  1. 29
      injector

29
injector

@ -3,6 +3,7 @@
import os import os
import sys import sys
import argparse import argparse
from django.core.exceptions import ValidationError
# django setup # django setup
DJANGO_SETTINGS_MODULE = 'settings' DJANGO_SETTINGS_MODULE = 'settings'
@ -10,6 +11,8 @@ DJANGO_PROJECT_PATH = os.path.dirname(sys.argv[0]) + '/web'
sys.path.insert(1, DJANGO_PROJECT_PATH) sys.path.insert(1, DJANGO_PROJECT_PATH)
os.environ['DJANGO_SETTINGS_MODULE'] = DJANGO_SETTINGS_MODULE os.environ['DJANGO_SETTINGS_MODULE'] = DJANGO_SETTINGS_MODULE
from web.postkey.models import i2phost from web.postkey.models import i2phost
from web.lib.validation import validate_hostname
from web.lib.validation import validate_b64hash
# parse command line options # parse command line options
parser = argparse.ArgumentParser( parser = argparse.ArgumentParser(
@ -22,15 +25,21 @@ args = parser.parse_args()
f = open(args.hostsfile, 'r') f = open(args.hostsfile, 'r')
for line in f: for line in f:
entry = line.split('=') entry = line.split('=')
# Check for already existed hosts in database to avoid adding duplicates. try:
# Do lookup by hostname only. hostname = validate_hostname(entry[0])
qs = i2phost.objects.filter(name=entry[0]) base64 = validate_b64hash(entry[1], check_uniq=False) # don't require uniqueness
if qs.exists(): except ValidationError, e:
print 'Host %s already exists' % entry[0] print 'validation error: %s: %s' % (e, line)
else: else:
print 'Adding %s' % entry[0] # Check for already existed hosts in database to avoid unneeded INSERTs
host = i2phost(name=entry[0], b64hash=entry[1], # beacuse they will fail anyway.
description='Auto-added from external hosts.txt', qs = i2phost.objects.filter(name=hostname)
activated=True, external=True) if qs.exists():
host.save() print 'Host %s already exists' % hostname
else:
print 'Adding %s' % hostname
host = i2phost(name=hostname, b64hash=base64,
description='Auto-added from external hosts.txt',
activated=True, external=True)
host.save()
f.close() f.close()

Loading…
Cancel
Save