1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

feat(redis_info): use module_utils redis to support TLS (#7267)

feat(redis_info): use redis module_utils to support TLS
This commit is contained in:
Grégoire Martini 2023-09-28 21:04:58 +02:00 committed by GitHub
parent b88b04593f
commit 43396efa2c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 53 additions and 33 deletions

View file

@ -0,0 +1,2 @@
minor_changes:
- redis_info - refactor the redis_info module to use the redis module_utils enabling to pass TLS parameters to the Redis client (https://github.com/ansible-collections/community.general/pull/7267).

View file

@ -17,30 +17,21 @@ version_added: '0.2.0'
description: description:
- Gathers information and statistics about Redis servers. - Gathers information and statistics about Redis servers.
extends_documentation_fragment: extends_documentation_fragment:
- community.general.redis
- community.general.attributes - community.general.attributes
- community.general.attributes.info_module - community.general.attributes.info_module
options: options:
login_host: login_user:
description: version_added: 7.5.0
- The host running the database. validate_certs:
type: str version_added: 7.5.0
default: localhost tls:
login_port: default: false
description: version_added: 7.5.0
- The port to connect to. ca_certs:
type: int version_added: 7.5.0
default: 6379
login_password:
description:
- The password used to authenticate with, when authentication is enabled for the Redis server.
type: str
notes:
- Requires the redis-py Python package on the remote host. You can
install it with pip (C(pip install redis)) or with a package manager.
U(https://github.com/andymccurdy/redis-py)
seealso: seealso:
- module: community.general.redis - module: community.general.redis
requirements: [ redis ]
author: "Pavlo Bashynskyi (@levonet)" author: "Pavlo Bashynskyi (@levonet)"
''' '''
@ -199,8 +190,10 @@ except ImportError:
REDIS_IMP_ERR = traceback.format_exc() REDIS_IMP_ERR = traceback.format_exc()
HAS_REDIS_PACKAGE = False HAS_REDIS_PACKAGE = False
from ansible.module_utils.basic import AnsibleModule, missing_required_lib from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.common.text.converters import to_native from ansible.module_utils.common.text.converters import to_native
from ansible_collections.community.general.plugins.module_utils.redis import (
fail_imports, redis_auth_argument_spec, redis_auth_params)
def redis_client(**client_params): def redis_client(**client_params):
@ -210,23 +203,16 @@ def redis_client(**client_params):
# Module execution. # Module execution.
def main(): def main():
module = AnsibleModule( module = AnsibleModule(
argument_spec=dict( argument_spec=redis_auth_argument_spec(tls_default=False),
login_host=dict(type='str', default='localhost'),
login_port=dict(type='int', default=6379),
login_password=dict(type='str', no_log=True),
),
supports_check_mode=True, supports_check_mode=True,
) )
if not HAS_REDIS_PACKAGE: fail_imports(module, module.params['tls'])
module.fail_json(msg=missing_required_lib('redis'), exception=REDIS_IMP_ERR)
login_host = module.params['login_host'] redis_params = redis_auth_params(module)
login_port = module.params['login_port']
login_password = module.params['login_password']
# Connect and check # Connect and check
client = redis_client(host=login_host, port=login_port, password=login_password) client = redis_client(**redis_params)
try: try:
client.ping() client.ping()
except Exception as e: except Exception as e:

View file

@ -50,7 +50,12 @@ class TestRedisInfoModule(ModuleTestCase):
set_module_args({}) set_module_args({})
self.module.main() self.module.main()
self.assertEqual(redis_client.call_count, 1) self.assertEqual(redis_client.call_count, 1)
self.assertEqual(redis_client.call_args, ({'host': 'localhost', 'port': 6379, 'password': None},)) self.assertEqual(redis_client.call_args, ({'host': 'localhost',
'port': 6379,
'password': None,
'ssl': False,
'ssl_ca_certs': None,
'ssl_cert_reqs': 'required'},))
self.assertEqual(result.exception.args[0]['info']['redis_version'], '999.999.999') self.assertEqual(result.exception.args[0]['info']['redis_version'], '999.999.999')
def test_with_parameters(self): def test_with_parameters(self):
@ -64,7 +69,34 @@ class TestRedisInfoModule(ModuleTestCase):
}) })
self.module.main() self.module.main()
self.assertEqual(redis_client.call_count, 1) self.assertEqual(redis_client.call_count, 1)
self.assertEqual(redis_client.call_args, ({'host': 'test', 'port': 1234, 'password': 'PASS'},)) self.assertEqual(redis_client.call_args, ({'host': 'test',
'port': 1234,
'password': 'PASS',
'ssl': False,
'ssl_ca_certs': None,
'ssl_cert_reqs': 'required'},))
self.assertEqual(result.exception.args[0]['info']['redis_version'], '999.999.999')
def test_with_tls_parameters(self):
"""Test with tls parameters"""
with self.patch_redis_client(side_effect=FakeRedisClient) as redis_client:
with self.assertRaises(AnsibleExitJson) as result:
set_module_args({
'login_host': 'test',
'login_port': 1234,
'login_password': 'PASS',
'tls': True,
'ca_certs': '/etc/ssl/ca.pem',
'validate_certs': False
})
self.module.main()
self.assertEqual(redis_client.call_count, 1)
self.assertEqual(redis_client.call_args, ({'host': 'test',
'port': 1234,
'password': 'PASS',
'ssl': True,
'ssl_ca_certs': '/etc/ssl/ca.pem',
'ssl_cert_reqs': None},))
self.assertEqual(result.exception.args[0]['info']['redis_version'], '999.999.999') self.assertEqual(result.exception.args[0]['info']['redis_version'], '999.999.999')
def test_with_fail_client(self): def test_with_fail_client(self):