Updating the mysql module to not use the PASSWORD when MySQL is version 8.0.11 or higher, where the PASSWORD function has been removed.

This commit is contained in:
Gareth J. Greenaway 2018-06-20 11:41:14 -07:00
parent 6072d1ef9c
commit 1b2ffcef1d
No known key found for this signature in database
GPG key ID: 10B62F8A7CAD7A41
2 changed files with 21 additions and 7 deletions

View file

@ -1203,6 +1203,7 @@ def user_exists(user,
salt '*' mysql.user_exists 'username' passwordless=True
salt '*' mysql.user_exists 'username' password_column='authentication_string'
'''
server_version = version(**connection_args)
dbc = _connect(**connection_args)
# Did we fail to connect with the user we are checking
# Its password might have previously change with the same command/state
@ -1234,7 +1235,10 @@ def user_exists(user,
else:
qry += ' AND ' + password_column + ' = \'\''
elif password:
qry += ' AND ' + password_column + ' = PASSWORD(%(password)s)'
if salt.utils.versions.version_cmp(server_version, '8.0.11') <= 0:
qry += ' AND ' + password_column + ' = %(password)s'
else:
qry += ' AND ' + password_column + ' = PASSWORD(%(password)s)'
args['password'] = six.text_type(password)
elif password_hash:
qry += ' AND ' + password_column + ' = %(password)s'
@ -1333,6 +1337,7 @@ def user_create(user,
salt '*' mysql.user_create 'username' 'hostname' password_hash='hash'
salt '*' mysql.user_create 'username' 'hostname' allow_passwordless=True
'''
server_version = version(**connection_args)
if user_exists(user, host, **connection_args):
log.info('User \'%s\'@\'%s\' already exists', user, host)
return False
@ -1353,7 +1358,10 @@ def user_create(user,
qry += ' IDENTIFIED BY %(password)s'
args['password'] = six.text_type(password)
elif password_hash is not None:
qry += ' IDENTIFIED BY PASSWORD %(password)s'
if salt.utils.versions.version_cmp(server_version, '8.0.11') <= 0:
qry += ' IDENTIFIED BY %(password)s'
else:
qry += ' IDENTIFIED BY PASSWORD %(password)s'
args['password'] = password_hash
elif salt.utils.data.is_true(allow_passwordless):
if salt.utils.data.is_true(unix_socket):
@ -1433,9 +1441,13 @@ def user_chpass(user,
salt '*' mysql.user_chpass frank localhost password_hash='hash'
salt '*' mysql.user_chpass frank localhost allow_passwordless=True
'''
server_version = version(**connection_args)
args = {}
if password is not None:
password_sql = 'PASSWORD(%(password)s)'
if salt.utils.versions.version_cmp(server_version, '8.0.11') <= 0:
password_sql = '%(password)s'
else:
password_sql = 'PASSWORD(%(password)s)'
args['password'] = password
elif password_hash is not None:
password_sql = '%(password)s'

View file

@ -54,10 +54,12 @@ class MySQLTestCase(TestCase, LoaderModuleMockMixin):
# test_user_create_when_user_exists(self):
# ensure we don't try to create a user when one already exists
with patch.object(mysql, 'user_exists', MagicMock(return_value=True)):
with patch.dict(mysql.__salt__, {'config.option': MagicMock()}):
ret = mysql.user_create('testuser')
self.assertEqual(False, ret)
# mock the version of MySQL
with patch.object(mysql, 'version', MagicMock(return_value='8.0.10')):
with patch.object(mysql, 'user_exists', MagicMock(return_value=True)):
with patch.dict(mysql.__salt__, {'config.option': MagicMock()}):
ret = mysql.user_create('testuser')
self.assertEqual(False, ret)
def test_user_create(self):
'''