@ -1,17 +1,15 @@
#!/usr/bin/python
#!/usr/bin/python
#
#
# (c) 2013, RSD Services S.A
# Copyright: (c) 2013, RSD Services S.A
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
from __future__ import absolute_import , division , print_function
from __future__ import absolute_import , division , print_function
__metaclass__ = type
__metaclass__ = type
ANSIBLE_METADATA = { ' metadata_version ' : ' 1.1 ' ,
ANSIBLE_METADATA = { ' metadata_version ' : ' 1.1 ' ,
' status ' : [ ' preview ' ] ,
' status ' : [ ' preview ' ] ,
' supported_by ' : ' community ' }
' supported_by ' : ' community ' }
DOCUMENTATION = '''
DOCUMENTATION = '''
- - -
- - -
module : java_cert
module : java_cert
@ -65,10 +63,10 @@ options:
state :
state :
description :
description :
- Defines action which can be either certificate import or removal .
- Defines action which can be either certificate import or removal .
choices : [ ' present ' , ' absent ' ]
choices : [ absent , present ]
default : present
default : present
author :
author : Adam Hamsik @haad
- Adam Hamsik ( @haad )
'''
'''
EXAMPLES = '''
EXAMPLES = '''
@ -131,17 +129,19 @@ import os
# import module snippets
# import module snippets
from ansible . module_utils . basic import AnsibleModule
from ansible . module_utils . basic import AnsibleModule
def check_cert_present ( module , executable , keystore_path , keystore_pass , alias ) :
def check_cert_present ( module , executable , keystore_path , keystore_pass , alias ) :
''' Check if certificate with alias is present in keystore
''' Check if certificate with alias is present in keystore
located at keystore_path '''
located at keystore_path '''
test_cmd = ( " %s -noprompt -list -keystore ' %s ' -storepass ' %s ' "
test_cmd = ( " %s -noprompt -list -keystore ' %s ' -storepass ' %s ' "
" -alias ' %s ' " ) % ( executable , keystore_path , keystore_pass , alias )
" -alias ' %s ' " ) % ( executable , keystore_path , keystore_pass , alias )
( check_rc , _ , _ ) = module . run_command ( test_cmd )
( check_rc , _ , _ ) = module . run_command ( test_cmd )
if check_rc == 0 :
if check_rc == 0 :
return True
return True
return False
return False
def import_cert_url ( module , executable , url , port , keystore_path , keystore_pass , alias ) :
def import_cert_url ( module , executable , url , port , keystore_path , keystore_pass , alias ) :
''' Import certificate from URL into keystore located at keystore_path '''
''' Import certificate from URL into keystore located at keystore_path '''
import re
import re
@ -152,7 +152,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 '|',
@ -162,12 +162,12 @@ 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 ' " ) % ( executable , keystore_path ,
" -storepass ' %s ' -alias ' %s ' " ) % ( executable , keystore_path ,
keystore_pass , alias )
keystore_pass , alias )
if module . check_mode :
if module . check_mode :
@ -180,7 +180,7 @@ def import_cert_url(module, executable, url, port, keystore_path, keystore_pass,
( import_rc , import_out , import_err ) = module . run_command ( import_cmd ,
( import_rc , import_out , import_err ) = module . run_command ( import_cmd ,
data = fetch_out ,
data = fetch_out ,
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 ,
return 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 ,
@ -189,11 +189,12 @@ def import_cert_url(module, executable, url, port, keystore_path, keystore_pass,
return module . fail_json ( msg = import_out , rc = import_rc , cmd = import_cmd ,
return module . fail_json ( msg = import_out , rc = import_rc , cmd = import_cmd ,
error = import_err )
error = import_err )
def import_cert_path ( module , executable , path , keystore_path , keystore_pass , alias ) :
def import_cert_path ( module , executable , path , keystore_path , keystore_pass , alias ) :
''' Import certificate from path into keystore located on
''' Import certificate from path into keystore located on
keystore_path as alias '''
keystore_path as alias '''
import_cmd = ( " %s -importcert -noprompt -keystore ' %s ' "
import_cmd = ( " %s -importcert -noprompt -keystore ' %s ' "
" -storepass ' %s ' -file ' %s ' -alias ' %s ' " ) % ( executable ,
" -storepass ' %s ' -file ' %s ' -alias ' %s ' " ) % ( executable ,
keystore_path ,
keystore_path ,
keystore_pass ,
keystore_pass ,
path , alias )
path , alias )
@ -205,7 +206,7 @@ def import_cert_path(module, executable, path, keystore_path, keystore_pass, ali
( 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 ,
return 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 ,
@ -213,6 +214,7 @@ def import_cert_path(module, executable, path, keystore_path, keystore_pass, ali
else :
else :
return module . fail_json ( msg = import_out , rc = import_rc , cmd = import_cmd )
return 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 ) :
def import_pkcs12_path ( module , executable , path , keystore_path , keystore_pass , pkcs12_pass , pkcs12_alias , alias ) :
''' Import pkcs12 from path into keystore located on
''' Import pkcs12 from path into keystore located on
keystore_path as alias '''
keystore_path as alias '''
@ -228,7 +230,7 @@ def import_pkcs12_path(module, executable, path, keystore_path, keystore_pass, p
( 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 ,
return 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 ,
@ -240,7 +242,7 @@ def import_pkcs12_path(module, executable, path, keystore_path, keystore_pass, p
def delete_cert ( module , executable , keystore_path , keystore_pass , alias ) :
def delete_cert ( module , executable , keystore_path , keystore_pass , alias ) :
''' Delete certificate identified with alias from keystore on keystore_path '''
''' Delete certificate identified with alias from keystore on keystore_path '''
del_cmd = ( " %s -delete -keystore ' %s ' -storepass ' %s ' "
del_cmd = ( " %s -delete -keystore ' %s ' -storepass ' %s ' "
" -alias ' %s ' " ) % ( executable , keystore_path , keystore_pass , alias )
" -alias ' %s ' " ) % ( executable , keystore_path , keystore_pass , alias )
if module . check_mode :
if module . check_mode :
module . exit_json ( changed = True )
module . exit_json ( changed = True )
@ -248,29 +250,32 @@ def delete_cert(module, executable, keystore_path, keystore_pass, alias):
# 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 ,
return 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 actuall executable or not '''
test_cmd = " %s " % ( executable )
test_cmd = " %s " % ( executable )
module . run_command ( test_cmd , check_rc = True )
module . run_command ( test_cmd , check_rc = True )
def test_keystore ( module , keystore_path ) :
def test_keystore ( module , keystore_path ) :
''' Check if we can access keystore as file or not '''
''' Check if we can access keystore as file or not '''
if keystore_path is None :
if keystore_path is None :
keystore_path = ' '
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 ,
return module . fail_json ( changed = False ,
msg = " Module require existing keystore at keystore_path ' %s ' "
msg = " Module require existing keystore at keystore_path ' %s ' "
% ( keystore_path ) )
% ( keystore_path ) )
def main ( ) :
def main ( ) :
argument_spec = dict (
argument_spec = dict (
cert_url = dict ( type = ' str ' ) ,
cert_url = dict ( type = ' str ' ) ,
@ -279,13 +284,12 @@ def main():
pkcs12_password = dict ( type = ' str ' , no_log = True ) ,
pkcs12_password = dict ( type = ' str ' , no_log = True ) ,
pkcs12_alias = dict ( type = ' str ' ) ,
pkcs12_alias = dict ( type = ' str ' ) ,
cert_alias = dict ( type = ' str ' ) ,
cert_alias = dict ( type = ' str ' ) ,
cert_port = dict ( default = ' 443 ' , type = ' int ' ) ,
cert_port = dict ( type = ' int ' , default = ' 443 ' ) ,
keystore_path = dict ( type = ' path ' ) ,
keystore_path = dict ( type = ' path ' ) ,
keystore_pass = dict ( required = True , type = ' str ' , no_log = True ) ,
keystore_pass = dict ( type = ' str ' , required = True , no_log = True ) ,
keystore_create = dict ( default = False , type = ' bool ' ) ,
keystore_create = dict ( type = ' bool ' , default = False ) ,
executable = dict ( default = ' keytool ' , type = ' str ' ) ,
executable = dict ( type = ' str ' , default = ' keytool ' ) ,
state = dict ( default = ' present ' ,
state = dict ( type = ' str ' , default = ' present ' , choices = [ ' absent ' , ' present ' ] ) ,
choices = [ ' present ' , ' absent ' ] )
)
)
module = AnsibleModule (
module = AnsibleModule (
@ -317,7 +321,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 )
@ -347,5 +351,6 @@ def main():
module . exit_json ( changed = False )
module . exit_json ( changed = False )
if __name__ == " __main__ " :
if __name__ == " __main__ " :
main ( )
main ( )