Initial commit but hopefully working application.

This commit is contained in:
Amritanshu
2013-12-03 00:11:03 +05:30
commit 107be1af8c
10 changed files with 311 additions and 0 deletions

39
bifrost/__init__.py Normal file
View File

@ -0,0 +1,39 @@
from crypt import crypt
import os
from pyramid.authentication import BasicAuthAuthenticationPolicy
from pyramid.authorization import ACLAuthorizationPolicy
from pyramid.config import Configurator
from pyramid.security import Authenticated, Allow, Everyone
def main(global_config, **settings):
config = Configurator(settings=settings, authentication_policy=BasicAuthAuthenticationPolicy(htpasswd),
authorization_policy=ACLAuthorizationPolicy(), root_factory='bifrost.RootFactory')
config.add_route('update', '/update')
config.scan()
return config.make_wsgi_app()
def htpasswd(username, password, request):
settings = request.registry.settings
file = settings['biforst.auth']
if not os.path.isfile(file):
return None
users = {}
with open(file) as f:
for line in f:
login, pwd = line.split(':')
users[login] = pwd.rstrip('\n')
if username in users and crypt(password, users[username]) == users[username]:
return [Authenticated]
return None
class RootFactory(object):
@property
def __acl__(self):
acl = [(Allow, Authenticated, Authenticated)]
return acl
def __init__(self, request):
pass

58
bifrost/views.py Normal file
View File

@ -0,0 +1,58 @@
import logging
import os
import xmlrpc.client
from pyramid.httpexceptions import HTTPUnauthorized
from pyramid.security import forget, Authenticated
from pyramid.view import view_config, forbidden_view_config
log = logging.getLogger('bifrost.updater')
@view_config(route_name='update', request_param='domain', renderer='json', permission=Authenticated)
def update_view(request):
file = request.registry.settings['biforst.file']
username = request.registry.settings['webfaction.username']
password = request.registry.settings['webfaction.password']
current_ip = request.GET.get('ip',None)
if current_ip is None:
current_ip = request.remote_addr
domain = request.GET['domain']
db = load(file)
if domain in db and db[domain] != current_ip:
server = xmlrpc.client.ServerProxy('https://api.webfaction.com/')
session_id, account = server.login(username, password)
server.delete_dns_override(session_id, domain)
server.create_dns_override(session_id, domain, current_ip, '', '', '', '')
db[domain] = current_ip
update(file, db)
log.info('{0} updated to {1} at {3}'.format(domain, current_ip))
else:
log.info('{0} not updated'.format(domain))
return {}
def load(file):
if not os.path.isfile(file):
return {}
current = {}
with open(file) as f:
for line in f:
domain, ip = line.split(':')
current[domain] = ip.rstrip('\n')
return current
def update(file, db):
with open(file, 'w') as f:
f.writelines(['{0}:{1}'.format(key, value) for key, value in db.items()])
@forbidden_view_config()
def basic_challenge(request):
response = HTTPUnauthorized()
response.headers.update(forget(request))
return response