Moving to Sanic from Pyramid.
Views working as of now. Need to work on Authentication Signed-off-by: tanshu <tanshu@gmail.com>
This commit is contained in:
@ -1,17 +1,5 @@
|
||||
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):
|
||||
@ -27,13 +15,3 @@ def htpasswd(username, password, request):
|
||||
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
|
||||
|
||||
3
bifrost/__main__.py
Normal file
3
bifrost/__main__.py
Normal file
@ -0,0 +1,3 @@
|
||||
from bifrost.main import init
|
||||
|
||||
init()
|
||||
25
bifrost/main.py
Normal file
25
bifrost/main.py
Normal file
@ -0,0 +1,25 @@
|
||||
from sanic import Sanic
|
||||
|
||||
from environs import Env
|
||||
|
||||
from bifrost.settings import Settings
|
||||
|
||||
app = Sanic(__name__, load_env='BIFROST_')
|
||||
|
||||
|
||||
def init():
|
||||
env = Env()
|
||||
env.read_env()
|
||||
|
||||
app.config.from_object(Settings)
|
||||
|
||||
from bifrost.views import update_view
|
||||
app.add_route(update_view, '/update', methods=['GET'])
|
||||
|
||||
app.run(host='0.0.0.0', port=8000, debug=True, access_log=True)
|
||||
app.run(
|
||||
host=app.config.HOST,
|
||||
port=app.config.PORT,
|
||||
debug=app.config.DEBUG,
|
||||
access_log=app.config.ACCESS_LOG
|
||||
)
|
||||
9
bifrost/settings.py
Normal file
9
bifrost/settings.py
Normal file
@ -0,0 +1,9 @@
|
||||
from sanic_envconfig import EnvConfig
|
||||
|
||||
|
||||
class Settings(EnvConfig):
|
||||
DEBUG: bool = True
|
||||
HOST: str = '0.0.0.0'
|
||||
PORT: int = 8000
|
||||
API_URL_BASE: str = 'https://api.digitalocean.com/v2'
|
||||
API_TOKEN: str = None
|
||||
@ -1,31 +1,28 @@
|
||||
import logging
|
||||
from sanic.log import logger
|
||||
from sanic.response import json
|
||||
|
||||
from pyramid.httpexceptions import HTTPUnauthorized
|
||||
from pyramid.security import forget, Authenticated
|
||||
from pyramid.view import view_config, forbidden_view_config
|
||||
from bifrost.digital_ocean import update_domain
|
||||
|
||||
log = logging.getLogger('bifrost.updater')
|
||||
api_url_base = 'https://api.digitalocean.com/v2'
|
||||
api_token = None
|
||||
|
||||
|
||||
@view_config(route_name='update', request_param='domain', renderer='json', permission=Authenticated)
|
||||
def update_view(request):
|
||||
current_ip = request.GET.get('ip', None)
|
||||
if current_ip is None:
|
||||
async def update_view(request):
|
||||
logger.info('Here is your log')
|
||||
if 'domain' not in request.args or len(request.args['domain']) != 1:
|
||||
return json({"message": "Domain error"})
|
||||
domain = request.args['domain'][0]
|
||||
if 'ip' in request.args and len(request.args['ip']) == 1:
|
||||
current_ip = request.args['ip'][0]
|
||||
else:
|
||||
if 'X-Forwarded-For' in request.headers:
|
||||
current_ip = request.headers['X-Forwarded-For']
|
||||
else:
|
||||
current_ip = request.remote_addr
|
||||
|
||||
name, domain = request.GET['domain'].split('.', maxsplit=1)
|
||||
update_domain(domain, name, current_ip, api_url_base, api_token, log)
|
||||
return {}
|
||||
current_ip = request.ip
|
||||
name, domain = domain.split('.', maxsplit=1)
|
||||
update_domain(domain, name, current_ip, request.app.config.API_URL_BASE, request.app.config.API_TOKEN, logger)
|
||||
return json({"domain": domain, "name": name, "current_ip": current_ip, "api_base": request.app.config.API_URL_BASE, "api_token": request.app.config.API_TOKEN})
|
||||
|
||||
|
||||
@forbidden_view_config()
|
||||
def basic_challenge(request):
|
||||
response = HTTPUnauthorized()
|
||||
response.headers.update(forget(request))
|
||||
return response
|
||||
# @forbidden_view_config()
|
||||
# def basic_challenge(request):
|
||||
# response = HTTPUnauthorized()
|
||||
# response.headers.update(forget(request))
|
||||
# return response
|
||||
|
||||
Reference in New Issue
Block a user