From 3574eacb2513758acecf965172f357613bc26f18 Mon Sep 17 00:00:00 2001 From: jokajak Date: Sun, 24 Apr 2016 08:35:37 -0400 Subject: [PATCH 1/6] Add example files for running clipperz with uwsgi and nginx Additionally make changes to enable running clipperz from uwsgi --- backend/flask/src/clipperz/views.py | 15 ++++++++---- backend/flask/src/nginx.conf.example | 11 +++++++++ backend/flask/src/run.py | 10 ++++++++ backend/flask/src/uwsg-emperor.ini.example | 28 ++++++++++++++++++++++ 4 files changed, 60 insertions(+), 4 deletions(-) create mode 100644 backend/flask/src/nginx.conf.example create mode 100644 backend/flask/src/run.py create mode 100644 backend/flask/src/uwsg-emperor.ini.example diff --git a/backend/flask/src/clipperz/views.py b/backend/flask/src/clipperz/views.py index 0bece2a..e57fbed 100644 --- a/backend/flask/src/clipperz/views.py +++ b/backend/flask/src/clipperz/views.py @@ -1,10 +1,11 @@ """Clipperz views.""" -from flask import session, request, g +from flask import session, request, g, send_from_directory from clipperz import app, db, lm from .models import User from .api import * # NOQA from .exceptions import InvalidUsage from flask.ext.login import login_required +from os.path import dirname @lm.user_loader @@ -113,19 +114,25 @@ def dump(frontend_version): @app.route('/beta/') def beta(path): """Fallback for serving beta version.""" - return send_from_directory('beta', path) + here = dirname(__file__) + file_path = "{0}/../beta/".format(here) + return send_from_directory(file_path, path) @app.route('/gamma/') def gamma(path): """Fallback for serving gamma version.""" - return send_from_directory('gamma', path) + here = dirname(__file__) + file_path = "{0}/../gamma/".format(here) + return send_from_directory(file_path, path) @app.route('/delta/') def delta(path): """Fallback for serving delta version.""" - return send_from_directory('delta', path) + here = dirname(__file__) + file_path = "{0}/../delta/".format(here) + return send_from_directory(file_path, path) @app.route('/pm', methods=['GET', 'OPTIONS', 'POST']) diff --git a/backend/flask/src/nginx.conf.example b/backend/flask/src/nginx.conf.example new file mode 100644 index 0000000..8dad8ce --- /dev/null +++ b/backend/flask/src/nginx.conf.example @@ -0,0 +1,11 @@ + location /pm { + auth_basic "Restricted"; + auth_basic_user_file /etc/nginx/.htpasswd; + try_files $uri $uri/ @pm; + } + location @pm { + include uwsgi_params; + uwsgi_param SCRIPT_NAME /pm; + uwsgi_modifier1 30; + uwsgi_pass unix:/run/uwsgi/pm.sock; + } diff --git a/backend/flask/src/run.py b/backend/flask/src/run.py new file mode 100644 index 0000000..5fa654f --- /dev/null +++ b/backend/flask/src/run.py @@ -0,0 +1,10 @@ +#!/usr/bin/env python +from clipperz import app, db + + +def main(): + db.create_all() + app.run(debug=True) + +if __name__ == "__main__": + main() diff --git a/backend/flask/src/uwsg-emperor.ini.example b/backend/flask/src/uwsg-emperor.ini.example new file mode 100644 index 0000000..d301077 --- /dev/null +++ b/backend/flask/src/uwsg-emperor.ini.example @@ -0,0 +1,28 @@ +[uwsgi] +plugin = python +socket = /run/uwsgi/%n.sock +chmod-socket = 660 +master = true +processes = 1 +uid = clipperz +gid = clipperz +umask = 027 +procname-master = uwsgi pm + +vacuum = true + +die-on-term = true + +base = /home/clipperz/password-manager/target/flask/ +module = clipperz +callable = app +venv = /home/clipperz/password-manager/target/flask/ + +#the variable that holds a flask application inside the module imported at line #6 +callable = app + +#location of log files +logto = /var/log/uwsgi/%n.log + +env = PYTHON_EGG_CACHE=/var/tmp/clipperz +env = DATABASE_URL = 'mysql://clipperz:clipperz@localhost/clipperz' From e4e4de7c1de64969d6d7c4a2f3c29b34e5a56fb7 Mon Sep 17 00:00:00 2001 From: jokajak Date: Sun, 24 Apr 2016 12:17:22 -0400 Subject: [PATCH 2/6] Fix flask model initialization This fixes Records and RecordVersions to populate parent objects as part of the initialization --- backend/flask/src/clipperz/models.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/backend/flask/src/clipperz/models.py b/backend/flask/src/clipperz/models.py index 61ca25d..65466b5 100644 --- a/backend/flask/src/clipperz/models.py +++ b/backend/flask/src/clipperz/models.py @@ -87,8 +87,11 @@ class RecordVersion(db.Model): order_by=id, cascade='all,delete')) - def __init__(self): + def __init__(self, record=None): """Initialize a record version.""" + if record: + self.record = record + self.record_id = record.id self.creation_date = datetime.datetime.utcnow() def update(self, someData): @@ -128,8 +131,11 @@ class Record(db.Model): uselist=False, cascade='save-update, merge, delete, delete-orphan') - def __init__(self): + def __init__(self, user=None): """Initialize a record.""" + if user: + self.user_id = user.id + self.version = 1 self.creation_date = datetime.datetime.utcnow() def update(self, data, record_version): @@ -139,10 +145,7 @@ class Record(db.Model): self.api_version = data['version'] self.update_date = datetime.datetime.now() self.current_record_version = record_version - if self.version: - self.version += 1 - else: - self.version = 1 + self.version += 1 # ------------------------------------------------------------------------------ From a794e5ea061a12e4b5543aff29011c1b2c627e44 Mon Sep 17 00:00:00 2001 From: jokajak Date: Sun, 24 Apr 2016 12:18:59 -0400 Subject: [PATCH 3/6] Add logger for debugging invalid methods --- backend/flask/src/clipperz/views.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/backend/flask/src/clipperz/views.py b/backend/flask/src/clipperz/views.py index e57fbed..79fbaea 100644 --- a/backend/flask/src/clipperz/views.py +++ b/backend/flask/src/clipperz/views.py @@ -140,8 +140,9 @@ def pm(): """Main request handler.""" method = request.form['method'] if method not in globals(): + app.logger.error(method) raise InvalidUsage('This method is not yet implemented', status_code=501) - handler = globals()[method]() app.logger.debug(method) + handler = globals()[method]() return handler.handle_request(request) From e9b52791dd737d25c9f846b7751d5b3095c725e0 Mon Sep 17 00:00:00 2001 From: jokajak Date: Sun, 24 Apr 2016 12:20:03 -0400 Subject: [PATCH 4/6] Fix config to pass in variables properly --- backend/flask/src/config.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/backend/flask/src/config.py b/backend/flask/src/config.py index a32c6f7..c3fff6b 100644 --- a/backend/flask/src/config.py +++ b/backend/flask/src/config.py @@ -3,16 +3,13 @@ import os basedir = os.path.abspath(os.path.dirname(__file__)) -CSRF_ENABLED = True - - if os.environ.get('DATABASE_URL') is None: SQLALCHEMY_DATABASE_URI = ('sqlite:///' + os.path.join(basedir, 'app.db') + '?check_same_thread=False') else: SQLALCHEMY_DATABASE_URI = os.environ['DATABASE_URL'] SQLALCHEMY_MIGRATE_REPO = os.path.join(basedir, 'db_repository') -SQLALCHEMY_RECRD_QUERIES = True +SQLALCHEMY_RECORD_QUERIES = True ADMINS = ['you@example.com'] @@ -20,15 +17,20 @@ ADMINS = ['you@example.com'] class Config(object): DEBUG = False TESTING = False - SQLALCHEMY_ECHO = False + CSRF_ENABLED = True WTF_CSRF_ENABLED = True SECRET_KEY = 'you-will-never-guess' sessionTimeout = datetime.timedelta(minutes=-2) + SQLALCHEMY_ECHO = False + SQLALCHEMY_DATABASE_URI = SQLALCHEMY_DATABASE_URI + SQLALCHEMY_MIGRATE_REPO = os.path.join(basedir, 'db_repository') + class DevelopmentConfig(Config): DEBUG = True SQLALCHEMY_ECHO = True + SQLALCHEMY_RECORD_QUERIES = True class TestingConfig(Config): From a25b3aaa22b699409567aa6defc0c51c215c7d13 Mon Sep 17 00:00:00 2001 From: jokajak Date: Fri, 27 May 2016 13:01:15 -0400 Subject: [PATCH 5/6] flask backend: fix access_date population Enables saving updates to access_date and ensures it is populated --- backend/flask/src/clipperz/api.py | 5 +++++ backend/flask/src/clipperz/models.py | 8 ++++++++ 2 files changed, 13 insertions(+) diff --git a/backend/flask/src/clipperz/api.py b/backend/flask/src/clipperz/api.py index 4f6a277..6d59c11 100644 --- a/backend/flask/src/clipperz/api.py +++ b/backend/flask/src/clipperz/api.py @@ -438,6 +438,11 @@ class message(HandlerMixin): result['oldestUsedEncryptedVersion'] = oldest_encryption_version result['versions'] = record_versions result['currentVersion'] = record.current_record_version.reference + record.current_record_version.access() + record.access() + db.session.add(record) + db.session.add(record_version) + db.session.commit() return jsonify({'result': result}) @login_required diff --git a/backend/flask/src/clipperz/models.py b/backend/flask/src/clipperz/models.py index 65466b5..6403881 100644 --- a/backend/flask/src/clipperz/models.py +++ b/backend/flask/src/clipperz/models.py @@ -104,8 +104,12 @@ class RecordVersion(db.Model): self.version = self.record.version self.previous_version_key = recordVersionData['previousVersionKey'] self.update_date = datetime.datetime.utcnow() + self.access_date = datetime.datetime.utcnow() self.record.update(someData['record'], self) + + def access(self): + self.access_date = datetime.datetime.now() # ------------------------------------------------------------------------------ @@ -144,9 +148,13 @@ class Record(db.Model): self.data = data['data'] self.api_version = data['version'] self.update_date = datetime.datetime.now() + self.access_date = datetime.datetime.now() self.current_record_version = record_version self.version += 1 + def access(self): + self.access_date = datetime.datetime.now() + # ------------------------------------------------------------------------------ From 4d0550934a1c6fe2264280b01b7d3ce8e49630dd Mon Sep 17 00:00:00 2001 From: jokajak Date: Fri, 27 May 2016 12:59:55 -0400 Subject: [PATCH 6/6] flask backend: Add a stub for bitcoin certificates Returns an empty array. No plans to implement. --- backend/flask/src/clipperz/api.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/backend/flask/src/clipperz/api.py b/backend/flask/src/clipperz/api.py index 4f6a277..7075de9 100644 --- a/backend/flask/src/clipperz/api.py +++ b/backend/flask/src/clipperz/api.py @@ -659,6 +659,15 @@ class message(HandlerMixin): result['result'] = 'done' return jsonify({'result': result}) + @login_required + def getCertificatesStatus(self, parameters, request): + """ + Provides support for BTC Certificate feature. + + No idea how it works. + """ + return jsonify({'result': {}}) + class logout(HandlerMixin):