Browse Source

injector: add hostname/key validation using previously created library

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

21
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,14 +25,20 @@ 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
except ValidationError, e:
print 'validation error: %s: %s' % (e, line)
else:
# Check for already existed hosts in database to avoid unneeded INSERTs
# beacuse they will fail anyway.
qs = i2phost.objects.filter(name=hostname)
if qs.exists(): if qs.exists():
print 'Host %s already exists' % entry[0] print 'Host %s already exists' % hostname
else: else:
print 'Adding %s' % entry[0] print 'Adding %s' % hostname
host = i2phost(name=entry[0], b64hash=entry[1], host = i2phost(name=hostname, b64hash=base64,
description='Auto-added from external hosts.txt', description='Auto-added from external hosts.txt',
activated=True, external=True) activated=True, external=True)
host.save() host.save()

Loading…
Cancel
Save