From 0cc83537112763c1fd39174926bd043614b6eaff Mon Sep 17 00:00:00 2001 From: David Morris Date: Thu, 23 Jan 2025 14:51:00 +0000 Subject: [PATCH 1/7] move db connection to start and pass --- hooky-secret-validation.py | 46 ++++++++++++++++++++++++-------------- 1 file changed, 29 insertions(+), 17 deletions(-) diff --git a/hooky-secret-validation.py b/hooky-secret-validation.py index 4f3b979..9d78e11 100644 --- a/hooky-secret-validation.py +++ b/hooky-secret-validation.py @@ -17,7 +17,7 @@ import json import string import time -from flask import Flask, request, abort +from flask import Flask, request, abort, g import hashlib import hmac from werkzeug.exceptions import HTTPException # Add this import @@ -50,13 +50,29 @@ def verify_signature(payload_body, secret_token, signature_header): app.logger.debug("-" * 21) +# Create Flask app first +app = Flask(__name__) + +# Then define database functions +def get_db(): + if 'db' not in g: + g.db = sqlite3.connect(args.db_name) + g.db.row_factory = sqlite3.Row + return g.db + +@app.teardown_appcontext +def close_db(error): + db = g.pop('db', None) + if db is not None: + db.close() + + def init_db(): """Initialize the SQLite database and create tables if they don't exist.""" app.logger.debug("Initializing database...") - db_path = Path(args.db_name) - conn = sqlite3.connect(str(db_path)) - cursor = conn.cursor() + db = get_db() + cursor = db.cursor() # Create table for webhook events cursor.execute(''' @@ -69,12 +85,8 @@ def init_db(): ) ''') - conn.commit() - conn.close() - app.logger.debug(f"Database initialized at {db_path.absolute()}") - - -app = Flask(__name__) + db.commit() + app.logger.debug(f"Database initialized at {args.db_name}") @app.route('/webhook', methods=['POST']) @@ -100,7 +112,7 @@ def slurphook(): # Store webhook data in database with more detailed logging try: app.logger.debug(f"Attempting to connect to database: {args.db_name}") - conn = sqlite3.connect(args.db_name) + conn = get_db() cursor = conn.cursor() # Log the data we're about to insert @@ -149,8 +161,8 @@ def view_hooks(): sort_by = request.args.get('sort', 'timestamp') sort_dir = request.args.get('dir', 'desc') - conn = sqlite3.connect(args.db_name) - cursor = conn.cursor() + db = get_db() + cursor = db.cursor() cursor.execute('SELECT COUNT(*) FROM webhook_events') total_records = cursor.fetchone()[0] @@ -439,7 +451,6 @@ def view_hooks(): if __name__ == '__main__': - parser = argparse.ArgumentParser() parser.add_argument( @@ -468,9 +479,10 @@ def view_hooks(): args = parser.parse_args() - - # Initialize the database before starting the app - init_db() + + # Create app context before initializing database + with app.app_context(): + init_db() app.config['DEBUG'] = True app.run(host='localhost', port=8000) From f904ea108e3c759d886dbd7c0ea32b9d18d33024 Mon Sep 17 00:00:00 2001 From: David Morris Date: Thu, 23 Jan 2025 14:55:01 +0000 Subject: [PATCH 2/7] Adding search box --- hooky-secret-validation.py | 61 +++++++++++++++++++++++++++++++++----- 1 file changed, 54 insertions(+), 7 deletions(-) diff --git a/hooky-secret-validation.py b/hooky-secret-validation.py index 9d78e11..0d424dc 100644 --- a/hooky-secret-validation.py +++ b/hooky-secret-validation.py @@ -160,11 +160,16 @@ def view_hooks(): page = request.args.get('page', 1, type=int) sort_by = request.args.get('sort', 'timestamp') sort_dir = request.args.get('dir', 'desc') + search = request.args.get('search', '') # Get search parameter db = get_db() cursor = db.cursor() - cursor.execute('SELECT COUNT(*) FROM webhook_events') + # Modify queries to include search + search_condition = "WHERE event_type LIKE ?" if search else "" + search_param = (f"%{search}%",) if search else () + + cursor.execute(f'SELECT COUNT(*) FROM webhook_events {search_condition}', search_param) total_records = cursor.fetchone()[0] # Get current record for main display using ID instead of offset @@ -184,16 +189,18 @@ def view_hooks(): ''') hook = cursor.fetchone() - # Get 8 records for the table with their rowids + # Get 8 records for the table with their rowids, including search filter cursor.execute(f''' SELECT rowid, timestamp, event_type, signature FROM webhook_events + {search_condition} ORDER BY {sort_by} {sort_dir} LIMIT 8 - ''') + ''', search_param) table_records = cursor.fetchall() + # Add search box to HTML html = f''' @@ -334,6 +341,28 @@ def view_hooks(): margin-left: 5px; color: #666; }} + .search-container {{ + margin: 20px 0; + text-align: right; + }} + + .search-box {{ + padding: 8px; + width: 200px; + border: 1px solid #e1e1e1; + border-radius: 3px; + font-family: Helvetica, Arial, sans-serif; + }} + + .search-box:focus {{ + outline: none; + border-color: #000000; + }} + + .search-label {{ + margin-right: 10px; + color: #666666; + }} @@ -360,12 +389,12 @@ def view_hooks(): ''' if page > 1: - html += f'Previous' + html += f'Previous' else: html += 'Previous' if page < total_records: - html += f'Next' + html += f'Next' else: html += 'Next' @@ -373,12 +402,21 @@ def view_hooks():

Recent Webhooks

+
+ + +
''' + # Modify sort headers to include search parameter sort_headers = [ ('timestamp', 'Timestamp'), ('event_type', 'Event Type'), @@ -389,7 +427,7 @@ def view_hooks(): arrow = '↓' if sort_by == col and sort_dir == 'desc' else '↑' if sort_by == col else '' new_dir = 'asc' if sort_by == col and sort_dir == 'desc' else 'desc' html += f''' - ''' @@ -403,7 +441,7 @@ def view_hooks(): for rowid, timestamp, event_type, signature in table_records: selected = 'selected' if page == rowid else '' html += f''' - + @@ -416,6 +454,15 @@ def view_hooks():
+ {label}{arrow}
{timestamp} {event_type} {signature}