From 3e958387924a79f435289c04b85c8c06b0c97150 Mon Sep 17 00:00:00 2001 From: jeanluc Date: Thu, 8 Aug 2024 17:27:09 +0200 Subject: [PATCH] Fix pre-commit --- .pylintrc | 1 + src/saltext/mysql/auth/mysql.py | 1 + src/saltext/mysql/cache/mysql_cache.py | 4 +- src/saltext/mysql/modules/mysql.py | 52 ++++++++++++---------- src/saltext/mysql/pillar/mysql.py | 5 ++- src/saltext/mysql/returners/mysql.py | 17 +++---- src/saltext/mysql/states/mysql_database.py | 1 + src/saltext/mysql/states/mysql_grants.py | 1 + src/saltext/mysql/states/mysql_query.py | 1 + src/saltext/mysql/states/mysql_user.py | 1 + tests/functional/cache/test_mysql.py | 2 +- tests/functional/modules/test_mysql.py | 1 + tests/functional/states/test_mysql.py | 1 + tests/unit/cache/test_mysql_cache.py | 3 +- tests/unit/modules/test_mysql.py | 3 +- tests/unit/states/test_database.py | 1 + tests/unit/states/test_grants.py | 1 + tests/unit/states/test_query.py | 1 + tests/unit/states/test_user.py | 1 + 19 files changed, 56 insertions(+), 42 deletions(-) diff --git a/.pylintrc b/.pylintrc index 5692f3b0..f5d47e21 100755 --- a/.pylintrc +++ b/.pylintrc @@ -458,6 +458,7 @@ disable=R, import-outside-toplevel, deprecated-method, keyword-arg-before-vararg, + consider-using-f-string, # Enable the message, report, category or checker with the given id(s). You can # either give multiple identifier separated by comma (,) or put this option diff --git a/src/saltext/mysql/auth/mysql.py b/src/saltext/mysql/auth/mysql.py index ef7c0295..215e7a42 100644 --- a/src/saltext/mysql/auth/mysql.py +++ b/src/saltext/mysql/auth/mysql.py @@ -46,6 +46,7 @@ :depends: - MySQL-python Python module """ + import logging log = logging.getLogger(__name__) diff --git a/src/saltext/mysql/cache/mysql_cache.py b/src/saltext/mysql/cache/mysql_cache.py index ed378e11..457f6ea4 100644 --- a/src/saltext/mysql/cache/mysql_cache.py +++ b/src/saltext/mysql/cache/mysql_cache.py @@ -50,6 +50,7 @@ .. _`python-mysql documentation`: http://python-mysql.readthedocs.io/en/latest/ """ + import copy import logging import time @@ -70,7 +71,6 @@ class InterfaceError(Exception): pass - except ImportError: try: # MySQLdb import failed, try to import PyMySQL @@ -158,7 +158,7 @@ def run_query(conn, query, args=None, retries=3): query = query[:150] + "<...>" raise SaltCacheError( "Error running {}{}: {}".format(query, f"- args: {args}" if args else "", e) - ) + ) from e def _create_table(): diff --git a/src/saltext/mysql/modules/mysql.py b/src/saltext/mysql/modules/mysql.py index 023d8f60..9767ba93 100644 --- a/src/saltext/mysql/modules/mysql.py +++ b/src/saltext/mysql/modules/mysql.py @@ -27,6 +27,7 @@ CLI by using the arguments defined :mod:`here `. Additionally, it is now possible to setup a user with no password. """ + import copy import hashlib import logging @@ -46,7 +47,9 @@ import MySQLdb.converters import MySQLdb.cursors from MySQLdb import OperationalError - from MySQLdb.constants import CLIENT, FIELD_TYPE, FLAG + from MySQLdb.constants import CLIENT + from MySQLdb.constants import FIELD_TYPE + from MySQLdb.constants import FLAG except ImportError: try: # MySQLdb import failed, try to import PyMySQL @@ -57,7 +60,9 @@ import MySQLdb.converters import MySQLdb.cursors from MySQLdb import OperationalError - from MySQLdb.constants import CLIENT, FIELD_TYPE, FLAG + from MySQLdb.constants import CLIENT + from MySQLdb.constants import FIELD_TYPE + from MySQLdb.constants import FLAG except ImportError: MySQLdb = None @@ -261,8 +266,10 @@ def __virtual__(): def __mysql_hash_password(password): - _password = hashlib.sha1(password.encode()).digest() - _password = f"*{hashlib.sha1(_password).hexdigest().upper()}" + # It's actually used for security purposes, but that's the way + # the MySQL native password plugin works and is why it's deprecated + _password = hashlib.sha1(password.encode()).digest() # nosec + _password = f"*{hashlib.sha1(_password).hexdigest().upper()}" # nosec return _password @@ -751,7 +758,7 @@ def query(database, query, **connection_args): .. code-block:: python - {"query time": {"human": "39.0ms", "raw": "0.03899"}, "rows affected": 1L} + {"query time": {"human": "39.0ms", "raw": "0.03899"}, "rows affected": 1l} CLI Example: @@ -767,11 +774,11 @@ def query(database, query, **connection_args): "columns": ("id", "name", "cash"), "query time": {"human": "1.0ms", "raw": "0.001"}, "results": ( - (1L, "User 1", Decimal("110.000000")), - (2L, "User 2", Decimal("215.636756")), - (3L, "User 3", Decimal("0.040000")), + (1l, "User 1", Decimal("110.000000")), + (2l, "User 2", Decimal("215.636756")), + (3l, "User 3", Decimal("0.040000")), ), - "rows returned": 3L, + "rows returned": 3l, } CLI Example: @@ -784,7 +791,7 @@ def query(database, query, **connection_args): .. code-block:: python - {"query time": {"human": "25.6ms", "raw": "0.02563"}, "rows affected": 1L} + {"query time": {"human": "25.6ms", "raw": "0.02563"}, "rows affected": 1l} CLI Example: @@ -796,7 +803,7 @@ def query(database, query, **connection_args): .. code-block:: python - {"query time": {"human": "39.0ms", "raw": "0.03899"}, "rows affected": 1L} + {"query time": {"human": "39.0ms", "raw": "0.03899"}, "rows affected": 1l} Jinja Example: Run a query on ``mydb`` and use row 0, column 0's data. @@ -898,7 +905,7 @@ def file_query(database, file_name, **connection_args): .. code-block:: python - {"query time": {"human": "39.0ms", "raw": "0.03899"}, "rows affected": 1L} + {"query time": {"human": "39.0ms", "raw": "0.03899"}, "rows affected": 1l} """ if not HAS_SQLPARSE: @@ -1673,7 +1680,7 @@ def _mysql_user_create( args["password"] = password_hash else: log.error( - "password or password_hash must be specified, unless " "allow_passwordless=True" + "password or password_hash must be specified, unless allow_passwordless=True" ) qry = False return qry, args @@ -1718,7 +1725,7 @@ def _mariadb_user_create( args["password"] = password_hash else: log.error( - "password or password_hash must be specified, unless " "allow_passwordless=True" + "password or password_hash must be specified, unless allow_passwordless=True" ) qry = False return qry, args @@ -1888,7 +1895,7 @@ def _mysql_user_chpass( password_sql = "%(password)s" args["password"] = password_hash elif not salt.utils.data.is_true(allow_passwordless): - log.error("password or password_hash must be specified, unless " "allow_passwordless=True") + log.error("password or password_hash must be specified, unless allow_passwordless=True") return False else: password_sql = "''" @@ -1964,7 +1971,7 @@ def _mariadb_user_chpass( password_sql = "%(password)s" args["password"] = password_hash elif not salt.utils.data.is_true(allow_passwordless): - log.error("password or password_hash must be specified, unless " "allow_passwordless=True") + log.error("password or password_hash must be specified, unless allow_passwordless=True") return False else: password_sql = "''" @@ -2290,7 +2297,7 @@ def __grant_normalize(grant): exploded_grants = __grant_split(grant) for chkgrant, _ in exploded_grants: if chkgrant.strip().upper() not in __grants__: - raise Exception(f"Invalid grant : '{chkgrant}'") + raise ValueError(f"Invalid grant : '{chkgrant}'") return grant @@ -2310,7 +2317,7 @@ def __ssl_option_sanitize(ssl_option): normal_key = key.strip().upper() if normal_key not in __ssl_options__: - raise Exception(f"Invalid SSL option : '{key}'") + raise ValueError(f"Invalid SSL option : '{key}'") if normal_key in __ssl_options_parameterized__: # SSL option parameters (cipher, issuer, subject) are pasted directly to SQL so @@ -2352,7 +2359,7 @@ def __grant_generate( if dbc != "*": # _ and % are authorized on GRANT queries and should get escaped # on the db name, but only if not requesting a table level grant - dbc = quote_identifier(dbc, for_grants=(table == "*")) + dbc = quote_identifier(dbc, for_grants=table == "*") if table != "*": table = quote_identifier(table) # identifiers cannot be used as values, and same thing for grants @@ -2590,8 +2597,8 @@ def grant_revoke( if dbc != "*": # _ and % are authorized on GRANT queries and should get escaped # on the db name, but only if not requesting a table level grant - s_database = quote_identifier(dbc, for_grants=(table == "*")) - if dbc == "*": + s_database = quote_identifier(dbc, for_grants=table == "*") + else: # add revoke for *.* # before the modification query send to mysql will looks like # REVOKE SELECT ON `*`.* FROM %(user)s@%(host)s @@ -2695,7 +2702,6 @@ def __do_query_into_hash(conn, sql_str): cursor = conn.cursor() except MySQLdb.MySQLError: log.error("%s: Can't get cursor for SQL->%s", mod, sql_str) - cursor.close() log.debug("%s-->", mod) return rtn_results @@ -3037,7 +3043,7 @@ def plugin_status(name, **connection_args): if dbc is None: return "" cur = dbc.cursor() - qry = "SELECT PLUGIN_STATUS FROM INFORMATION_SCHEMA.PLUGINS WHERE PLUGIN_NAME =" " %(name)s" + qry = "SELECT PLUGIN_STATUS FROM INFORMATION_SCHEMA.PLUGINS WHERE PLUGIN_NAME = %(name)s" args = {} args["name"] = name diff --git a/src/saltext/mysql/pillar/mysql.py b/src/saltext/mysql/pillar/mysql.py index 245d7fc7..ae3f9c0b 100644 --- a/src/saltext/mysql/pillar/mysql.py +++ b/src/saltext/mysql/pillar/mysql.py @@ -44,6 +44,7 @@ as_list: True with_lists: [1,3] """ + import logging from contextlib import contextmanager @@ -100,10 +101,10 @@ def _get_options(self): } _options = {} _opts = __opts__.get("mysql", {}) - for attr in defaults: + for attr, default in defaults.items(): if attr not in _opts: log.debug("Using default for MySQL %s", attr) - _options[attr] = defaults[attr] + _options[attr] = default continue _options[attr] = _opts[attr] return _options diff --git a/src/saltext/mysql/returners/mysql.py b/src/saltext/mysql/returners/mysql.py index cb53bfde..1673533b 100644 --- a/src/saltext/mysql/returners/mysql.py +++ b/src/saltext/mysql/returners/mysql.py @@ -137,6 +137,7 @@ salt '*' test.ping --return mysql --return_kwargs '{"db": "another-salt"}' """ + import logging import sys from contextlib import contextmanager @@ -147,10 +148,6 @@ import salt.utils.job import salt.utils.json -# Let's not allow PyLint complain about string substitution -# pylint: disable=W1321,E1321 - - try: # Trying to import MySQLdb import MySQLdb @@ -512,7 +509,7 @@ def _purge_jobs(timestamp): cur.execute("COMMIT") except MySQLdb.Error as e: log.error( - "mysql returner archiver was unable to delete contents of table" " 'salt_returns'" + "mysql returner archiver was unable to delete contents of table 'salt_returns'" ) log.error(str(e)) raise salt.exceptions.SaltRunnerError(str(e)) @@ -523,7 +520,7 @@ def _purge_jobs(timestamp): cur.execute("COMMIT") except MySQLdb.Error as e: log.error( - "mysql returner archiver was unable to delete contents of table" " 'salt_events'" + "mysql returner archiver was unable to delete contents of table 'salt_events'" ) log.error(str(e)) raise salt.exceptions.SaltRunnerError(str(e)) @@ -575,9 +572,7 @@ def _archive_jobs(timestamp): cur.execute(sql, (timestamp,)) cur.execute("COMMIT") except MySQLdb.Error as e: - log.error( - "mysql returner archiver was unable to copy contents of table" " 'salt_returns'" - ) + log.error("mysql returner archiver was unable to copy contents of table 'salt_returns'") log.error(str(e)) raise salt.exceptions.SaltRunnerError(str(e)) @@ -588,9 +583,7 @@ def _archive_jobs(timestamp): cur.execute(sql, (timestamp,)) cur.execute("COMMIT") except MySQLdb.Error as e: - log.error( - "mysql returner archiver was unable to copy contents of table" " 'salt_events'" - ) + log.error("mysql returner archiver was unable to copy contents of table 'salt_events'") log.error(str(e)) raise salt.exceptions.SaltRunnerError(str(e)) diff --git a/src/saltext/mysql/states/mysql_database.py b/src/saltext/mysql/states/mysql_database.py index 823caa8e..ae053fbb 100644 --- a/src/saltext/mysql/states/mysql_database.py +++ b/src/saltext/mysql/states/mysql_database.py @@ -13,6 +13,7 @@ frank: mysql_database.present """ + import logging import sys diff --git a/src/saltext/mysql/states/mysql_grants.py b/src/saltext/mysql/states/mysql_grants.py index cbf88d9c..f4b7894b 100644 --- a/src/saltext/mysql/states/mysql_grants.py +++ b/src/saltext/mysql/states/mysql_grants.py @@ -44,6 +44,7 @@ - database: somedb.sometable - user: joe """ + import sys diff --git a/src/saltext/mysql/states/mysql_query.py b/src/saltext/mysql/states/mysql_query.py index 7e4ec391..8f88af0b 100644 --- a/src/saltext/mysql/states/mysql_query.py +++ b/src/saltext/mysql/states/mysql_query.py @@ -18,6 +18,7 @@ - query: "SELECT * FROM table;" - output: "/tmp/query_id.txt" """ + import os.path import sys diff --git a/src/saltext/mysql/states/mysql_user.py b/src/saltext/mysql/states/mysql_user.py index 9a0b26a9..9612b0af 100644 --- a/src/saltext/mysql/states/mysql_user.py +++ b/src/saltext/mysql/states/mysql_user.py @@ -39,6 +39,7 @@ :py:mod:`salt.states.mysql_grants` for further instructions. """ + import sys import salt.utils.data diff --git a/tests/functional/cache/test_mysql.py b/tests/functional/cache/test_mysql.py index 9a43028a..5fb9f0ce 100644 --- a/tests/functional/cache/test_mysql.py +++ b/tests/functional/cache/test_mysql.py @@ -4,7 +4,7 @@ import salt.cache import salt.loader -from tests.pytests.functional.cache.helpers import run_common_cache_tests +from tests.functional.cache.helpers import run_common_cache_tests from tests.support.mysql import * # pylint: disable=wildcard-import,unused-wildcard-import docker = pytest.importorskip("docker") diff --git a/tests/functional/modules/test_mysql.py b/tests/functional/modules/test_mysql.py index f41ac15a..fb3ca7f1 100644 --- a/tests/functional/modules/test_mysql.py +++ b/tests/functional/modules/test_mysql.py @@ -1,6 +1,7 @@ """ Test Salt MySQL module across various MySQL variants """ + import logging import time diff --git a/tests/functional/states/test_mysql.py b/tests/functional/states/test_mysql.py index 0ed3282a..d8d7f9ac 100644 --- a/tests/functional/states/test_mysql.py +++ b/tests/functional/states/test_mysql.py @@ -1,6 +1,7 @@ """ Test Salt MySQL state module across various MySQL variants """ + import logging import time diff --git a/tests/unit/cache/test_mysql_cache.py b/tests/unit/cache/test_mysql_cache.py index 765c4d2a..a2400c7c 100644 --- a/tests/unit/cache/test_mysql_cache.py +++ b/tests/unit/cache/test_mysql_cache.py @@ -1,9 +1,10 @@ """ unit tests for the mysql_cache cache """ + import logging -from unittest.mock import call from unittest.mock import MagicMock +from unittest.mock import call from unittest.mock import patch import pytest diff --git a/tests/unit/modules/test_mysql.py b/tests/unit/modules/test_mysql.py index 25275ecd..68580753 100644 --- a/tests/unit/modules/test_mysql.py +++ b/tests/unit/modules/test_mysql.py @@ -5,9 +5,10 @@ tests.unit.modules.mysql ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ """ + import logging -from unittest.mock import call from unittest.mock import MagicMock +from unittest.mock import call from unittest.mock import mock_open from unittest.mock import patch diff --git a/tests/unit/states/test_database.py b/tests/unit/states/test_database.py index 7cb420c1..80bd9b08 100644 --- a/tests/unit/states/test_database.py +++ b/tests/unit/states/test_database.py @@ -1,6 +1,7 @@ """ This test checks mysql_database salt state """ + from unittest.mock import MagicMock from unittest.mock import patch diff --git a/tests/unit/states/test_grants.py b/tests/unit/states/test_grants.py index 14d59af4..2ea9a88f 100644 --- a/tests/unit/states/test_grants.py +++ b/tests/unit/states/test_grants.py @@ -1,6 +1,7 @@ """ :codeauthor: Jayesh Kariya """ + from unittest.mock import MagicMock from unittest.mock import patch diff --git a/tests/unit/states/test_query.py b/tests/unit/states/test_query.py index f4862ec8..1d0f3087 100644 --- a/tests/unit/states/test_query.py +++ b/tests/unit/states/test_query.py @@ -1,6 +1,7 @@ """ :codeauthor: Jayesh Kariya """ + import logging import os from unittest.mock import MagicMock diff --git a/tests/unit/states/test_user.py b/tests/unit/states/test_user.py index 71e009c6..f0f68946 100644 --- a/tests/unit/states/test_user.py +++ b/tests/unit/states/test_user.py @@ -1,6 +1,7 @@ """ :codeauthor: Jayesh Kariya """ + from unittest.mock import MagicMock from unittest.mock import patch