allow mysql connections from unix socket (#14)
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
Co-authored-by: Andrew Wang <someone.zip@gmail.com> Co-authored-by: Max Erenberg <merenber@csclub.uwaterloo.ca> Reviewed-on: #14 Co-authored-by: Andrew Wang <a268wang@localhost> Co-committed-by: Andrew Wang <a268wang@localhost>
This commit is contained in:
parent
6f1851fc19
commit
c6c01d8720
|
@ -1,12 +1,9 @@
|
||||||
import json
|
|
||||||
import socket
|
import socket
|
||||||
import sys
|
|
||||||
from typing import List, Tuple, Dict
|
from typing import List, Tuple, Dict
|
||||||
|
|
||||||
import click
|
import click
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
from ..operation_strings import descriptions as op_desc
|
|
||||||
from ..utils import space_colon_kv, generic_handle_stream_response
|
from ..utils import space_colon_kv, generic_handle_stream_response
|
||||||
from .CLIStreamResponseHandler import CLIStreamResponseHandler
|
from .CLIStreamResponseHandler import CLIStreamResponseHandler
|
||||||
|
|
||||||
|
|
|
@ -46,17 +46,21 @@ class MySQLService:
|
||||||
password = gen_password()
|
password = gen_password()
|
||||||
search_for_user = f"SELECT user FROM mysql.user WHERE user='{username}'"
|
search_for_user = f"SELECT user FROM mysql.user WHERE user='{username}'"
|
||||||
search_for_db = f"SHOW DATABASES LIKE '{username}'"
|
search_for_db = f"SHOW DATABASES LIKE '{username}'"
|
||||||
create_user = f"""
|
# CREATE USER can't be used in a query with multiple statements
|
||||||
CREATE USER '{username}'@'%' IDENTIFIED BY %(password)s;
|
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 = f"""
|
||||||
CREATE DATABASE {username};
|
CREATE DATABASE {username};
|
||||||
|
GRANT ALL PRIVILEGES ON {username}.* TO '{username}'@'localhost';
|
||||||
GRANT ALL PRIVILEGES ON {username}.* TO '{username}'@'%';
|
GRANT ALL PRIVILEGES ON {username}.* TO '{username}'@'%';
|
||||||
"""
|
"""
|
||||||
|
|
||||||
with self.mysql_connection() as con, con.cursor() as cursor:
|
with self.mysql_connection() as con, con.cursor() as cursor:
|
||||||
if response_is_empty(search_for_user, con):
|
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):
|
if response_is_empty(search_for_db, con):
|
||||||
cursor.execute(create_database)
|
cursor.execute(create_database)
|
||||||
else:
|
else:
|
||||||
|
@ -67,7 +71,8 @@ class MySQLService:
|
||||||
password = gen_password()
|
password = gen_password()
|
||||||
search_for_user = f"SELECT user FROM mysql.user WHERE user='{username}'"
|
search_for_user = f"SELECT user FROM mysql.user WHERE user='{username}'"
|
||||||
reset_password = f"""
|
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:
|
with self.mysql_connection() as con, con.cursor() as cursor:
|
||||||
|
@ -80,6 +85,7 @@ class MySQLService:
|
||||||
def delete_db(self, username: str):
|
def delete_db(self, username: str):
|
||||||
drop_db = f"DROP DATABASE IF EXISTS {username}"
|
drop_db = f"DROP DATABASE IF EXISTS {username}"
|
||||||
drop_user = f"""
|
drop_user = f"""
|
||||||
|
DROP USER IF EXISTS '{username}'@'localhost';
|
||||||
DROP USER IF EXISTS '{username}'@'%';
|
DROP USER IF EXISTS '{username}'@'%';
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue