Skip to content

Commit

Permalink
Fix pre-commit
Browse files Browse the repository at this point in the history
  • Loading branch information
lkubb committed Aug 8, 2024
1 parent 62b869a commit 3e95838
Show file tree
Hide file tree
Showing 19 changed files with 56 additions and 42 deletions.
1 change: 1 addition & 0 deletions .pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/saltext/mysql/auth/mysql.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
:depends: - MySQL-python Python module
"""

import logging

log = logging.getLogger(__name__)
Expand Down
4 changes: 2 additions & 2 deletions src/saltext/mysql/cache/mysql_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
.. _`python-mysql documentation`: http://python-mysql.readthedocs.io/en/latest/
"""

import copy
import logging
import time
Expand All @@ -70,7 +71,6 @@
class InterfaceError(Exception):
pass


except ImportError:
try:
# MySQLdb import failed, try to import PyMySQL
Expand Down Expand Up @@ -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():
Expand Down
52 changes: 29 additions & 23 deletions src/saltext/mysql/modules/mysql.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
CLI by using the arguments defined :mod:`here <salt.states.mysql_user>`.
Additionally, it is now possible to setup a user with no password.
"""

import copy
import hashlib
import logging
Expand All @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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


Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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.
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 = "''"
Expand Down Expand Up @@ -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 = "''"
Expand Down Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down
5 changes: 3 additions & 2 deletions src/saltext/mysql/pillar/mysql.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
as_list: True
with_lists: [1,3]
"""

import logging
from contextlib import contextmanager

Expand Down Expand Up @@ -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
Expand Down
17 changes: 5 additions & 12 deletions src/saltext/mysql/returners/mysql.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@
salt '*' test.ping --return mysql --return_kwargs '{"db": "another-salt"}'
"""

import logging
import sys
from contextlib import contextmanager
Expand All @@ -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
Expand Down Expand Up @@ -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))
Expand All @@ -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))
Expand Down Expand Up @@ -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))

Expand All @@ -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))

Expand Down
1 change: 1 addition & 0 deletions src/saltext/mysql/states/mysql_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
frank:
mysql_database.present
"""

import logging
import sys

Expand Down
1 change: 1 addition & 0 deletions src/saltext/mysql/states/mysql_grants.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
- database: somedb.sometable
- user: joe
"""

import sys


Expand Down
1 change: 1 addition & 0 deletions src/saltext/mysql/states/mysql_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
- query: "SELECT * FROM table;"
- output: "/tmp/query_id.txt"
"""

import os.path
import sys

Expand Down
1 change: 1 addition & 0 deletions src/saltext/mysql/states/mysql_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
:py:mod:`salt.states.mysql_grants` for further instructions.
"""

import sys

import salt.utils.data
Expand Down
2 changes: 1 addition & 1 deletion tests/functional/cache/test_mysql.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
1 change: 1 addition & 0 deletions tests/functional/modules/test_mysql.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Test Salt MySQL module across various MySQL variants
"""

import logging
import time

Expand Down
1 change: 1 addition & 0 deletions tests/functional/states/test_mysql.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Test Salt MySQL state module across various MySQL variants
"""

import logging
import time

Expand Down
3 changes: 2 additions & 1 deletion tests/unit/cache/test_mysql_cache.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
3 changes: 2 additions & 1 deletion tests/unit/modules/test_mysql.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions tests/unit/states/test_database.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
This test checks mysql_database salt state
"""

from unittest.mock import MagicMock
from unittest.mock import patch

Expand Down
Loading

0 comments on commit 3e95838

Please sign in to comment.