Verified Commit fec3b7da authored by Davide Depau's avatar Davide Depau
Browse files

Add management cli

parent 8007c28f
#!/usr/bin/env python3 __all__ = ('app', 'redis_store', 'management_cli')
import flask
from flask_redis import FlaskRedis
from flask_httpauth import HTTPBasicAuth
import re
import os import os
import crypt import re
import flask
from flask_httpauth import HTTPBasicAuth
from flask_redis import FlaskRedis
app = flask.Flask(__name__) app = flask.Flask(__name__)
auth = HTTPBasicAuth() auth = HTTPBasicAuth()
...@@ -13,7 +13,18 @@ app.config['REDIS_URL'] = os.environ.get('REDIS_URL', 'redis://127.0.0.1:6379/0' ...@@ -13,7 +13,18 @@ app.config['REDIS_URL'] = os.environ.get('REDIS_URL', 'redis://127.0.0.1:6379/0'
redis_store = FlaskRedis() redis_store = FlaskRedis()
redis_store.init_app(app) redis_store.init_app(app)
pure_string = re.compile(r'^\w+$') pure_string = re.compile(r'^\w+$')
admin_dir = os.path.join(os.path.dirname(__file__), 'admin')
def redis_key_for_user(user: str) -> str:
return "user.{}".format(user)
def redis_key_for_url(url: str) -> str:
return "url.{}".format(url)
def redis_key_for_count(url: str) -> str:
return "count.{}".format(url)
def handle_redirect(path): def handle_redirect(path):
...@@ -27,7 +38,7 @@ def handle_redirect(path): ...@@ -27,7 +38,7 @@ def handle_redirect(path):
@auth.verify_password @auth.verify_password
def handle_login(username, password): def handle_login(username, password):
crypt_pw = redis_store.get("user." + username) crypt_pw = redis_store.get(redis_key_for_user(username))
if crypt_pw is not None: if crypt_pw is not None:
crypt_pw = crypt_pw.decode() crypt_pw = crypt_pw.decode()
return crypt_pw == crypt.crypt(password, crypt_pw) return crypt_pw == crypt.crypt(password, crypt_pw)
...@@ -51,9 +62,9 @@ def add_url(): ...@@ -51,9 +62,9 @@ def add_url():
long_url = payload['url'] long_url = payload['url']
if not pure_string.match(short_url): if not pure_string.match(short_url):
return flask.abort(400) return flask.abort(400)
if not redis_store.exists('url.' + short_url): if not redis_store.exists(redis_key_for_url(short_url)):
redis_store.set('count.' + short_url, 0) redis_store.set(redis_key_for_count(short_url), 0)
redis_store.set('url.' + short_url, long_url) redis_store.set(redis_key_for_url(short_url), long_url)
return '' return ''
except KeyError: except KeyError:
return flask.abort(400) return flask.abort(400)
...@@ -67,8 +78,8 @@ def del_url(): ...@@ -67,8 +78,8 @@ def del_url():
payload = flask.request.get_json() payload = flask.request.get_json()
if payload: if payload:
try: try:
redis_store.delete('url.' + payload['name']) redis_store.delete(redis_key_for_url(payload['name']))
redis_store.delete('count.' + payload['name']) redis_store.delete(redis_key_for_count(payload['name']))
return '' return ''
except KeyError: except KeyError:
return flask.abort(400) return flask.abort(400)
...@@ -84,12 +95,11 @@ def list_urls(): ...@@ -84,12 +95,11 @@ def list_urls():
for key in keys: for key in keys:
real_key = key.replace('url.', '', 1) real_key = key.replace('url.', '', 1)
return_dict[real_key] = {'url': redis_store.get(key).decode(), return_dict[real_key] = {'url': redis_store.get(key).decode(),
'count': int(redis_store.get('count.' + real_key).decode())} 'count': int(redis_store.get(redis_key_for_count(real_key)).decode())}
return flask.jsonify(return_dict) return flask.jsonify(return_dict)
@app.route('/<path:path>') @app.route('/<path:path>', strict_slashes=False)
@app.route('/<path:path>/')
def redirect(path): def redirect(path):
if pure_string.match(path): if pure_string.match(path):
return handle_redirect(path) return handle_redirect(path)
...@@ -97,5 +107,7 @@ def redirect(path): ...@@ -97,5 +107,7 @@ def redirect(path):
return flask.abort(404) return flask.abort(404)
from .management_cli import *
if __name__ == "__main__": if __name__ == "__main__":
app.run() app.run()
import crypt
import click
from . import app, redis_store, redis_key_for_user
@app.cli.command()
@click.argument("username")
@click.option("--passwd", "-p", prompt="Password", help="Password", hide_input=True)
def adduser(username, passwd):
"""Add UUS admin user"""
cryptpwd = crypt.crypt(passwd)
redis_store.set(redis_key_for_user(username), cryptpwd)
@app.cli.command()
@click.argument("username")
@click.option("--passwd", "-p", prompt="Password", help="Password", hide_input=True)
def passwd(username, passwd):
"""Change UUS admin user password"""
adduser(username, passwd)
@app.cli.command()
@click.argument("username")
def deluser(username):
"""Delete UUS admin user"""
redis_store.delete(redis_key_for_user(username))
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment