From c6c01d8720f5f2fb2fd29ef818caf2f8a2d88ea9 Mon Sep 17 00:00:00 2001 From: Andrew Wang Date: Sat, 4 Sep 2021 22:25:37 -0400 Subject: [PATCH] allow mysql connections from unix socket (#14) Co-authored-by: Andrew Wang Co-authored-by: Max Erenberg Reviewed-on: https://git.csclub.uwaterloo.ca/public/pyceo/pulls/14 Co-authored-by: Andrew Wang Co-committed-by: Andrew Wang --- ceo/cli/utils.py | 3 --- ceo/tui/TransactionView.py | 2 +- ceod/db/MySQLService.py | 16 +++++++++++----- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/ceo/cli/utils.py b/ceo/cli/utils.py index 76389c9..d50f38e 100644 --- a/ceo/cli/utils.py +++ b/ceo/cli/utils.py @@ -1,12 +1,9 @@ -import json import socket -import sys from typing import List, Tuple, Dict import click import requests -from ..operation_strings import descriptions as op_desc from ..utils import space_colon_kv, generic_handle_stream_response from .CLIStreamResponseHandler import CLIStreamResponseHandler diff --git a/ceo/tui/TransactionView.py b/ceo/tui/TransactionView.py index 1773cfd..7adc26b 100644 --- a/ceo/tui/TransactionView.py +++ b/ceo/tui/TransactionView.py @@ -45,7 +45,7 @@ class TransactionView(Frame): if self._loaded: return self._loaded = True - + for _ in range(2): self._add_line() for operation in self._model.operations: diff --git a/ceod/db/MySQLService.py b/ceod/db/MySQLService.py index 043a906..e6a194d 100644 --- a/ceod/db/MySQLService.py +++ b/ceod/db/MySQLService.py @@ -46,17 +46,21 @@ class MySQLService: password = gen_password() search_for_user = f"SELECT user FROM mysql.user WHERE user='{username}'" search_for_db = f"SHOW DATABASES LIKE '{username}'" - create_user = f""" - CREATE USER '{username}'@'%' IDENTIFIED BY %(password)s; - """ + # CREATE USER can't be used in a query with multiple statements + create_user_commands = [ + f"CREATE USER '{username}'@'localhost' IDENTIFIED BY %(password)s", + f"CREATE USER '{username}'@'%' IDENTIFIED BY %(password)s", + ] create_database = f""" CREATE DATABASE {username}; + GRANT ALL PRIVILEGES ON {username}.* TO '{username}'@'localhost'; GRANT ALL PRIVILEGES ON {username}.* TO '{username}'@'%'; """ with self.mysql_connection() as con, con.cursor() as cursor: if response_is_empty(search_for_user, con): - cursor.execute(create_user, {'password': password}) + for cmd in create_user_commands: + cursor.execute(cmd, {'password': password}) if response_is_empty(search_for_db, con): cursor.execute(create_database) else: @@ -67,7 +71,8 @@ class MySQLService: password = gen_password() search_for_user = f"SELECT user FROM mysql.user WHERE user='{username}'" reset_password = f""" - ALTER USER '{username}'@'%' IDENTIFIED BY %(password)s + ALTER USER '{username}'@'localhost' IDENTIFIED BY %(password)s; + ALTER USER '{username}'@'%' IDENTIFIED BY %(password)s; """ with self.mysql_connection() as con, con.cursor() as cursor: @@ -80,6 +85,7 @@ class MySQLService: def delete_db(self, username: str): drop_db = f"DROP DATABASE IF EXISTS {username}" drop_user = f""" + DROP USER IF EXISTS '{username}'@'localhost'; DROP USER IF EXISTS '{username}'@'%'; """