From 91fea2066fc9ddfb103abd217a597b0eb0e877c1 Mon Sep 17 00:00:00 2001 From: lza_menace Date: Thu, 24 Dec 2020 21:23:09 -0800 Subject: [PATCH] working on global logging/events --- wowstash/blueprints/auth/routes.py | 14 +++++++------- wowstash/blueprints/wallet/routes.py | 14 +++++++------- wowstash/config.example.py | 7 +++++++ wowstash/helpers.py | 7 +++++++ wowstash/library/docker.py | 6 +++--- wowstash/library/mattermost.py | 22 ++++++++++++++++++++++ 6 files changed, 53 insertions(+), 17 deletions(-) create mode 100644 wowstash/helpers.py create mode 100644 wowstash/library/mattermost.py diff --git a/wowstash/blueprints/auth/routes.py b/wowstash/blueprints/auth/routes.py index c8cf5f0..eb8bfd3 100644 --- a/wowstash/blueprints/auth/routes.py +++ b/wowstash/blueprints/auth/routes.py @@ -7,7 +7,7 @@ from wowstash.forms import Register, Login, Delete from wowstash.models import User from wowstash.factory import db, bcrypt from wowstash.library.docker import docker -from wowstash.library.elasticsearch import send_es +from wowstash.helpers import capture_event @auth_bp.route("/register", methods=["GET", "POST"]) @@ -33,7 +33,7 @@ def register(): db.session.commit() # Capture event, login user and redirect to wallet page - send_es({'type': 'register', 'user': user.email}) + capture_event('register', user) login_user(user) return redirect(url_for('wallet.dashboard')) @@ -63,7 +63,7 @@ def login(): return redirect(url_for('auth.login')) # Capture event, login user, and redirect to wallet page - send_es({'type': 'login', 'user': user.email}) + capture_event('login', user) login_user(user) return redirect(url_for('wallet.dashboard')) @@ -73,9 +73,9 @@ def login(): def logout(): if current_user.is_authenticated: docker.stop_container(current_user.wallet_container) - send_es({'type': 'stop_container', 'user': current_user.email}) + capture_event('stop_container', current_user) current_user.clear_wallet_data() - send_es({'type': 'logout', 'user': current_user.email}) + capture_event('logout', current_user) logout_user() return redirect(url_for('meta.index')) @@ -85,10 +85,10 @@ def delete(): form = Delete() if form.validate_on_submit(): docker.stop_container(current_user.wallet_container) - send_es({'type': 'stop_container', 'user': current_user.email}) + capture_event('stop_container', current_user) sleep(1) docker.delete_wallet_data(current_user.id) - send_es({'type': 'delete_wallet', 'user': current_user.email}) + capture_event('delete_wallet', current_user) current_user.clear_wallet_data(reset_password=True, reset_wallet=True) flash('Successfully deleted wallet data') return redirect(url_for('meta.index')) diff --git a/wowstash/blueprints/wallet/routes.py b/wowstash/blueprints/wallet/routes.py index b26937a..baab818 100644 --- a/wowstash/blueprints/wallet/routes.py +++ b/wowstash/blueprints/wallet/routes.py @@ -10,7 +10,7 @@ from socket import socket from datetime import datetime from wowstash.blueprints.wallet import wallet_bp from wowstash.library.docker import docker -from wowstash.library.elasticsearch import send_es +from wowstash.helpers import capture_event from wowstash.library.jsonrpc import Wallet, to_atomic from wowstash.library.cache import cache from wowstash.forms import Send, Delete @@ -60,7 +60,7 @@ def dashboard(): seed = wallet.seed() spend_key = wallet.spend_key() view_key = wallet.view_key() - send_es({'type': 'load_dashboard', 'user': current_user.email}) + # capture_event('load_dashboard', current_user) return render_template( 'wallet/dashboard.html', transfers=all_transfers, @@ -130,13 +130,13 @@ def send(): # Check if Wownero wallet is available if wallet.connected is False: flash('Wallet RPC interface is unavailable at this time. Try again later.') - send_es({'type': 'tx_fail_rpc_unavailable', 'user': user.email}) + capture_event('tx_fail_rpc_unavailable', user) return redirect(redirect_url) # Quick n dirty check to see if address is WOW if len(address) not in [97, 108]: flash('Invalid Wownero address provided.') - send_es({'type': 'tx_fail_address_invalid', 'user': user.email}) + capture_event('tx_fail_address_invalid', user) return redirect(redirect_url) # Check if we're sweeping or not @@ -148,7 +148,7 @@ def send(): amount = to_atomic(Decimal(send_form.amount.data)) except: flash('Invalid Wownero amount specified.') - send_es({'type': 'tx_fail_amount_invalid', 'user': user.email}) + capture_event('tx_fail_amount_invalid', user) return redirect(redirect_url) # Send transfer @@ -159,10 +159,10 @@ def send(): msg = tx['message'].capitalize() msg_lower = tx['message'].replace(' ', '_').lower() flash(f'There was a problem sending the transaction: {msg}') - send_es({'type': f'tx_fail_{msg_lower}', 'user': user.email}) + capture_event(f'tx_fail_{msg_lower}', user) else: flash('Successfully sent transfer.') - send_es({'type': 'tx_success', 'user': user.email}) + capture_event('tx_success', user) return redirect(redirect_url) else: diff --git a/wowstash/config.example.py b/wowstash/config.example.py index 07131c7..d1e24c2 100644 --- a/wowstash/config.example.py +++ b/wowstash/config.example.py @@ -46,3 +46,10 @@ SOCIAL = { 'comment': 'https://webchat.freenode.net/?room=#wownero', 'reddit': 'https://reddit.com/r/wownero' } + +# Mattermost +MM_ENABLED = False +MM_CHANNEL = 'wowstash' +MM_USERNAME = 'WOW Stash' +MM_ICON = 'https://wowstash.app/static/img/wow-treasure-chest.png' +MM_ENDPOINT = 'xxxxxxx' diff --git a/wowstash/helpers.py b/wowstash/helpers.py new file mode 100644 index 0000000..7e4f043 --- /dev/null +++ b/wowstash/helpers.py @@ -0,0 +1,7 @@ +from wowstash.library.elasticsearch import send_es +from wowstash.library.mattermost import post_webhook + + +def capture_event(event_type, user_obj): + send_es({'type': event_type, 'user': user_obj.email}) + post_webhook(f'`{event_type}` from user {user_obj.id}') diff --git a/wowstash/library/docker.py b/wowstash/library/docker.py index fc381ba..263484e 100644 --- a/wowstash/library/docker.py +++ b/wowstash/library/docker.py @@ -9,7 +9,7 @@ from wowstash import config from wowstash.models import User from wowstash.factory import db from wowstash.library.jsonrpc import daemon -from wowstash.library.elasticsearch import send_es +from wowstash.helpers import capture_event class Docker(object): @@ -53,7 +53,7 @@ class Docker(object): } } ) - send_es({'type': 'create_wallet', 'user': u.email}) + capture_event('create_wallet', u) return container.short_id def start_wallet(self, user_id): @@ -90,7 +90,7 @@ class Docker(object): } } ) - send_es({'type': 'start_wallet', 'user': u.email}) + capture_event('start_wallet', u) return container.short_id except APIError as e: if str(e).startswith('409'): diff --git a/wowstash/library/mattermost.py b/wowstash/library/mattermost.py new file mode 100644 index 0000000..74f66c0 --- /dev/null +++ b/wowstash/library/mattermost.py @@ -0,0 +1,22 @@ +from requests import post as r_post +from json import dumps +from flask import current_app +from wowstash import config + + +def post_webhook(msg): + if getattr(config, 'MM_ENABLED', False): + try: + if current_app.config["DEBUG"]: + msg = "[DEBUG] " + msg + data = { + "text": msg, + "channel": config.MM_CHANNEL, + "username": config.MM_USERNAME, + "icon_url": config.MM_ICON + } + res = r_post(config.MM_ENDPOINT, data=dumps(data)) + res.raise_for_status() + return True + except: + return False