mirror of
https://github.com/ansible-collections/community.mysql.git
synced 2025-04-20 01:11:27 -07:00
Refactor user implementation to host get_tls_requires
This commit is contained in:
parent
5460dec642
commit
d77be1ba03
6 changed files with 107 additions and 61 deletions
|
@ -29,3 +29,47 @@ def server_supports_password_expire(cursor):
|
|||
version = get_server_version(cursor)
|
||||
|
||||
return LooseVersion(version) >= LooseVersion("10.4.3")
|
||||
|
||||
def get_tls_requires(cursor, user, host):
|
||||
"""Get user TLS requirements.
|
||||
Reads directly from mysql.user table allowing for a more
|
||||
readable code.
|
||||
|
||||
Args:
|
||||
cursor (cursor): DB driver cursor object.
|
||||
user (str): User name.
|
||||
host (str): User host name.
|
||||
|
||||
Returns: Dictionary containing current TLS required
|
||||
"""
|
||||
tls_requires = dict()
|
||||
|
||||
query = ('SELECT ssl_type, ssl_cipher, x509_issuer, x509_subject '
|
||||
'FROM mysql.user WHERE User = %s AND Host = %s')
|
||||
cursor.execute(query, (user, host))
|
||||
res = cursor.fetchone()
|
||||
|
||||
# Mysql_info use a DictCursor so we must convert back to a list
|
||||
# otherwise we get KeyError 0
|
||||
if isinstance(res, dict):
|
||||
res = list(res.values())
|
||||
|
||||
# When user don't require SSL, res value is: ('', '', '', '')
|
||||
if not any(res):
|
||||
return None
|
||||
|
||||
if res[0] == 'ANY':
|
||||
tls_requires['SSL'] = None
|
||||
|
||||
if res[0] == 'X509':
|
||||
tls_requires['X509'] = None
|
||||
|
||||
if res[1]:
|
||||
tls_requires['CIPHER'] = res[1]
|
||||
|
||||
if res[2]:
|
||||
tls_requires['ISSUER'] = res[2]
|
||||
|
||||
if res[3]:
|
||||
tls_requires['SUBJECT'] = res[3]
|
||||
return tls_requires
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue