mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-04-27 21:01:27 -07:00
java_cert: Remove redundant return (#52879)
* Remove redundant return statements for module.exit_json * Doc changes * Typo fixes * Refactor for check_mode Based upon work of pilou- Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
This commit is contained in:
parent
2e47e9fffa
commit
3bdbe24861
1 changed files with 46 additions and 58 deletions
|
@ -23,7 +23,7 @@ options:
|
||||||
cert_url:
|
cert_url:
|
||||||
description:
|
description:
|
||||||
- Basic URL to fetch SSL certificate from.
|
- Basic URL to fetch SSL certificate from.
|
||||||
- One of iC(cert_url) or C(cert_path) is required to load certificate.
|
- One of C(cert_url) or C(cert_path) is required to load certificate.
|
||||||
type: str
|
type: str
|
||||||
cert_port:
|
cert_port:
|
||||||
description:
|
description:
|
||||||
|
@ -34,7 +34,7 @@ options:
|
||||||
cert_path:
|
cert_path:
|
||||||
description:
|
description:
|
||||||
- Local path to load certificate from.
|
- Local path to load certificate from.
|
||||||
- One of cert_url or cert_path is required to load certificate.
|
- One of C(cert_url) or C(cert_path) is required to load certificate.
|
||||||
type: path
|
type: path
|
||||||
cert_alias:
|
cert_alias:
|
||||||
description:
|
description:
|
||||||
|
@ -158,6 +158,7 @@ cmd:
|
||||||
'''
|
'''
|
||||||
|
|
||||||
import os
|
import os
|
||||||
|
import re
|
||||||
|
|
||||||
# import module snippets
|
# import module snippets
|
||||||
from ansible.module_utils.basic import AnsibleModule
|
from ansible.module_utils.basic import AnsibleModule
|
||||||
|
@ -166,7 +167,7 @@ from ansible.module_utils.basic import AnsibleModule
|
||||||
def get_keystore_type(keystore_type):
|
def get_keystore_type(keystore_type):
|
||||||
''' Check that custom keystore is presented in parameters '''
|
''' Check that custom keystore is presented in parameters '''
|
||||||
if keystore_type:
|
if keystore_type:
|
||||||
return (" -storetype '%s'") % (keystore_type)
|
return " -storetype '%s'" % keystore_type
|
||||||
return ''
|
return ''
|
||||||
|
|
||||||
|
|
||||||
|
@ -184,7 +185,6 @@ def check_cert_present(module, executable, keystore_path, keystore_pass, alias,
|
||||||
|
|
||||||
def import_cert_url(module, executable, url, port, keystore_path, keystore_pass, alias, keystore_type):
|
def import_cert_url(module, executable, url, port, keystore_path, keystore_pass, alias, keystore_type):
|
||||||
''' Import certificate from URL into keystore located at keystore_path '''
|
''' Import certificate from URL into keystore located at keystore_path '''
|
||||||
import re
|
|
||||||
|
|
||||||
https_proxy = os.getenv("https_proxy")
|
https_proxy = os.getenv("https_proxy")
|
||||||
no_proxy = os.getenv("no_proxy")
|
no_proxy = os.getenv("no_proxy")
|
||||||
|
@ -192,7 +192,7 @@ def import_cert_url(module, executable, url, port, keystore_path, keystore_pass,
|
||||||
proxy_opts = ''
|
proxy_opts = ''
|
||||||
if https_proxy is not None:
|
if https_proxy is not None:
|
||||||
(proxy_host, proxy_port) = https_proxy.split(':')
|
(proxy_host, proxy_port) = https_proxy.split(':')
|
||||||
proxy_opts = ("-J-Dhttps.proxyHost=%s -J-Dhttps.proxyPort=%s") % (proxy_host, proxy_port)
|
proxy_opts = "-J-Dhttps.proxyHost=%s -J-Dhttps.proxyPort=%s" % (proxy_host, proxy_port)
|
||||||
|
|
||||||
if no_proxy is not None:
|
if no_proxy is not None:
|
||||||
# For Java's nonProxyHosts property, items are separated by '|',
|
# For Java's nonProxyHosts property, items are separated by '|',
|
||||||
|
@ -202,17 +202,14 @@ def import_cert_url(module, executable, url, port, keystore_path, keystore_pass,
|
||||||
|
|
||||||
# The property name is http.nonProxyHosts, there is no
|
# The property name is http.nonProxyHosts, there is no
|
||||||
# separate setting for HTTPS.
|
# separate setting for HTTPS.
|
||||||
proxy_opts += (" -J-Dhttp.nonProxyHosts='%s'") % (non_proxy_hosts)
|
proxy_opts += " -J-Dhttp.nonProxyHosts='%s'" % non_proxy_hosts
|
||||||
|
|
||||||
fetch_cmd = ("%s -printcert -rfc -sslserver %s %s:%d") % (executable, proxy_opts, url, port)
|
fetch_cmd = "%s -printcert -rfc -sslserver %s %s:%d" % (executable, proxy_opts, url, port)
|
||||||
import_cmd = ("%s -importcert -noprompt -keystore '%s' "
|
import_cmd = ("%s -importcert -noprompt -keystore '%s' "
|
||||||
"-storepass '%s' -alias '%s' %s") % (executable, keystore_path,
|
"-storepass '%s' -alias '%s' %s") % (executable, keystore_path,
|
||||||
keystore_pass, alias,
|
keystore_pass, alias,
|
||||||
get_keystore_type(keystore_type))
|
get_keystore_type(keystore_type))
|
||||||
|
|
||||||
if module.check_mode:
|
|
||||||
module.exit_json(changed=True)
|
|
||||||
|
|
||||||
# Fetch SSL certificate from remote host.
|
# Fetch SSL certificate from remote host.
|
||||||
(_, fetch_out, _) = module.run_command(fetch_cmd, check_rc=True)
|
(_, fetch_out, _) = module.run_command(fetch_cmd, check_rc=True)
|
||||||
|
|
||||||
|
@ -222,11 +219,11 @@ def import_cert_url(module, executable, url, port, keystore_path, keystore_pass,
|
||||||
check_rc=False)
|
check_rc=False)
|
||||||
diff = {'before': '\n', 'after': '%s\n' % alias}
|
diff = {'before': '\n', 'after': '%s\n' % alias}
|
||||||
if import_rc == 0:
|
if import_rc == 0:
|
||||||
return module.exit_json(changed=True, msg=import_out,
|
module.exit_json(changed=True, msg=import_out,
|
||||||
rc=import_rc, cmd=import_cmd, stdout=import_out,
|
rc=import_rc, cmd=import_cmd, stdout=import_out,
|
||||||
diff=diff)
|
diff=diff)
|
||||||
else:
|
else:
|
||||||
return module.fail_json(msg=import_out, rc=import_rc, cmd=import_cmd,
|
module.fail_json(msg=import_out, rc=import_rc, cmd=import_cmd,
|
||||||
error=import_err)
|
error=import_err)
|
||||||
|
|
||||||
|
|
||||||
|
@ -238,20 +235,17 @@ def import_cert_path(module, executable, path, keystore_path, keystore_pass, ali
|
||||||
keystore_pass, path, alias,
|
keystore_pass, path, alias,
|
||||||
get_keystore_type(keystore_type))
|
get_keystore_type(keystore_type))
|
||||||
|
|
||||||
if module.check_mode:
|
|
||||||
module.exit_json(changed=True)
|
|
||||||
|
|
||||||
# Use local certificate from local path and import it to a java keystore
|
# Use local certificate from local path and import it to a java keystore
|
||||||
(import_rc, import_out, import_err) = module.run_command(import_cmd,
|
(import_rc, import_out, import_err) = module.run_command(import_cmd,
|
||||||
check_rc=False)
|
check_rc=False)
|
||||||
|
|
||||||
diff = {'before': '\n', 'after': '%s\n' % alias}
|
diff = {'before': '\n', 'after': '%s\n' % alias}
|
||||||
if import_rc == 0:
|
if import_rc == 0:
|
||||||
return module.exit_json(changed=True, msg=import_out,
|
module.exit_json(changed=True, msg=import_out,
|
||||||
rc=import_rc, cmd=import_cmd, stdout=import_out,
|
rc=import_rc, cmd=import_cmd, stdout=import_out,
|
||||||
error=import_err, diff=diff)
|
error=import_err, diff=diff)
|
||||||
else:
|
else:
|
||||||
return module.fail_json(msg=import_out, rc=import_rc, cmd=import_cmd)
|
module.fail_json(msg=import_out, rc=import_rc, cmd=import_cmd)
|
||||||
|
|
||||||
|
|
||||||
def import_pkcs12_path(module, executable, path, keystore_path, keystore_pass, pkcs12_pass, pkcs12_alias, alias, keystore_type):
|
def import_pkcs12_path(module, executable, path, keystore_path, keystore_pass, pkcs12_pass, pkcs12_alias, alias, keystore_type):
|
||||||
|
@ -263,20 +257,17 @@ def import_pkcs12_path(module, executable, path, keystore_path, keystore_pass, p
|
||||||
keystore_pass, path, pkcs12_pass, pkcs12_alias,
|
keystore_pass, path, pkcs12_pass, pkcs12_alias,
|
||||||
alias, get_keystore_type(keystore_type))
|
alias, get_keystore_type(keystore_type))
|
||||||
|
|
||||||
if module.check_mode:
|
|
||||||
module.exit_json(changed=True)
|
|
||||||
|
|
||||||
# Use local certificate from local path and import it to a java keystore
|
# Use local certificate from local path and import it to a java keystore
|
||||||
(import_rc, import_out, import_err) = module.run_command(import_cmd,
|
(import_rc, import_out, import_err) = module.run_command(import_cmd,
|
||||||
check_rc=False)
|
check_rc=False)
|
||||||
|
|
||||||
diff = {'before': '\n', 'after': '%s\n' % alias}
|
diff = {'before': '\n', 'after': '%s\n' % alias}
|
||||||
if import_rc == 0:
|
if import_rc == 0:
|
||||||
return module.exit_json(changed=True, msg=import_out,
|
module.exit_json(changed=True, msg=import_out,
|
||||||
rc=import_rc, cmd=import_cmd, stdout=import_out,
|
rc=import_rc, cmd=import_cmd, stdout=import_out,
|
||||||
error=import_err, diff=diff)
|
error=import_err, diff=diff)
|
||||||
else:
|
else:
|
||||||
return module.fail_json(msg=import_out, rc=import_rc, cmd=import_cmd)
|
module.fail_json(msg=import_out, rc=import_rc, cmd=import_cmd)
|
||||||
|
|
||||||
|
|
||||||
def delete_cert(module, executable, keystore_path, keystore_pass, alias, keystore_type):
|
def delete_cert(module, executable, keystore_path, keystore_pass, alias, keystore_type):
|
||||||
|
@ -284,24 +275,19 @@ def delete_cert(module, executable, keystore_path, keystore_pass, alias, keystor
|
||||||
del_cmd = ("%s -delete -keystore '%s' -storepass '%s' "
|
del_cmd = ("%s -delete -keystore '%s' -storepass '%s' "
|
||||||
"-alias '%s' %s") % (executable, keystore_path, keystore_pass, alias, get_keystore_type(keystore_type))
|
"-alias '%s' %s") % (executable, keystore_path, keystore_pass, alias, get_keystore_type(keystore_type))
|
||||||
|
|
||||||
if module.check_mode:
|
|
||||||
module.exit_json(changed=True)
|
|
||||||
|
|
||||||
# Delete SSL certificate from keystore
|
# Delete SSL certificate from keystore
|
||||||
(del_rc, del_out, del_err) = module.run_command(del_cmd, check_rc=True)
|
(del_rc, del_out, del_err) = module.run_command(del_cmd, check_rc=True)
|
||||||
|
|
||||||
diff = {'before': '%s\n' % alias, 'after': None}
|
diff = {'before': '%s\n' % alias, 'after': None}
|
||||||
|
|
||||||
return module.exit_json(changed=True, msg=del_out,
|
module.exit_json(changed=True, msg=del_out,
|
||||||
rc=del_rc, cmd=del_cmd, stdout=del_out,
|
rc=del_rc, cmd=del_cmd, stdout=del_out,
|
||||||
error=del_err, diff=diff)
|
error=del_err, diff=diff)
|
||||||
|
|
||||||
|
|
||||||
def test_keytool(module, executable):
|
def test_keytool(module, executable):
|
||||||
''' Test if keytool is actuall executable or not '''
|
''' Test if keytool is actually executable or not '''
|
||||||
test_cmd = "%s" % (executable)
|
module.run_command("%s" % executable, check_rc=True)
|
||||||
|
|
||||||
module.run_command(test_cmd, check_rc=True)
|
|
||||||
|
|
||||||
|
|
||||||
def test_keystore(module, keystore_path):
|
def test_keystore(module, keystore_path):
|
||||||
|
@ -311,9 +297,7 @@ def test_keystore(module, keystore_path):
|
||||||
|
|
||||||
if not os.path.exists(keystore_path) and not os.path.isfile(keystore_path):
|
if not os.path.exists(keystore_path) and not os.path.isfile(keystore_path):
|
||||||
# Keystore doesn't exist we want to create it
|
# Keystore doesn't exist we want to create it
|
||||||
return module.fail_json(changed=False,
|
module.fail_json(changed=False, msg="Module require existing keystore at keystore_path '%s'" % keystore_path)
|
||||||
msg="Module require existing keystore at keystore_path '%s'"
|
|
||||||
% (keystore_path))
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
@ -363,7 +347,7 @@ def main():
|
||||||
if path and not cert_alias:
|
if path and not cert_alias:
|
||||||
module.fail_json(changed=False,
|
module.fail_json(changed=False,
|
||||||
msg="Using local path import from %s requires alias argument."
|
msg="Using local path import from %s requires alias argument."
|
||||||
% (keystore_path))
|
% keystore_path)
|
||||||
|
|
||||||
test_keytool(module, executable)
|
test_keytool(module, executable)
|
||||||
|
|
||||||
|
@ -373,12 +357,16 @@ def main():
|
||||||
cert_present = check_cert_present(module, executable, keystore_path,
|
cert_present = check_cert_present(module, executable, keystore_path,
|
||||||
keystore_pass, cert_alias, keystore_type)
|
keystore_pass, cert_alias, keystore_type)
|
||||||
|
|
||||||
if state == 'absent':
|
if state == 'absent' and cert_present:
|
||||||
if cert_present:
|
if module.check_mode:
|
||||||
|
module.exit_json(changed=True)
|
||||||
|
|
||||||
delete_cert(module, executable, keystore_path, keystore_pass, cert_alias, keystore_type)
|
delete_cert(module, executable, keystore_path, keystore_pass, cert_alias, keystore_type)
|
||||||
|
|
||||||
elif state == 'present':
|
elif state == 'present' and not cert_present:
|
||||||
if not cert_present:
|
if module.check_mode:
|
||||||
|
module.exit_json(changed=True)
|
||||||
|
|
||||||
if pkcs12_path:
|
if pkcs12_path:
|
||||||
import_pkcs12_path(module, executable, pkcs12_path, keystore_path,
|
import_pkcs12_path(module, executable, pkcs12_path, keystore_path,
|
||||||
keystore_pass, pkcs12_pass, pkcs12_alias, cert_alias, keystore_type)
|
keystore_pass, pkcs12_pass, pkcs12_alias, cert_alias, keystore_type)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue