@@ -85,8 +85,7 @@ def _check_load(isamAppliance, kdb_id, label, server, port):
8585 cert_id = cert_data ['id' ]
8686 cert_pem = get (isamAppliance , kdb_id , cert_id )['data' ]['contents' ]
8787 if cert_id == label : # label exists on appliance already
88- logger .debug (f"Comparing certificates: appliance[{ cert_pem } ] remote[{ remote_cert_pem } ]." )
89- if cert_pem == remote_cert_pem : # certificate data is the same
88+ if cert_pem == remote_cert_pem :
9089 logger .debug ("The certificate already exits on the appliance with the same label name and same content." )
9190 return True # both the labels and certificates match
9291 else :
@@ -136,7 +135,7 @@ def export_cert(isamAppliance, kdb_id, cert_id, filename, check_mode=False, forc
136135 """
137136 import os .path
138137
139- if force is True or _check (isamAppliance , kdb_id , cert_id ) is True :
138+ if force or _check (isamAppliance , kdb_id , cert_id ):
140139 if check_mode is False : # No point downloading a file if in check_mode
141140 return isamAppliance .invoke_get_file (
142141 "Export a certificate database" ,
@@ -149,36 +148,55 @@ def export_cert(isamAppliance, kdb_id, cert_id, filename, check_mode=False, forc
149148def import_cert (isamAppliance , kdb_id , cert , label , preserve_label = 'false' , check_mode = False , force = False ):
150149 """
151150 Importing a signer certificate into a certificate database
151+ cert can be a file or a string
152152 """
153- if force is True or _check_import (isamAppliance , kdb_id , label , cert , check_mode = check_mode ):
154- if check_mode is True :
155- return isamAppliance .create_return_object (changed = True )
156- else :
157- if version_compare (isamAppliance .facts ['version' ], "10.0.5.0" ) < 0 :
158- return isamAppliance .invoke_post_files (
159- "Importing a signer certificate into a certificate database" ,
160- f"/isam/ssl_certificates/{ kdb_id } /signer_cert" ,
161- [
162- {
163- 'file_formfield' : 'cert' ,
164- 'filename' : cert ,
165- 'mimetype' : 'application/octet-stream'
166- }
167- ],
168- {'label' : label })
169- else :
170- return isamAppliance .invoke_post_files (
171- "Importing a signer certificate into a certificate database" ,
172- f"/isam/ssl_certificates/{ kdb_id } /signer_cert" ,
173- [
174- {
175- 'file_formfield' : 'cert' ,
176- 'filename' : cert ,
177- 'mimetype' : 'application/octet-stream'
178- }
179- ],
180- {'label' : label ,
181- 'preserve_label' : preserve_label })
153+ # Let's do some simple check
154+ # check if the string begins with -----BEGIN CERTIFICATE-----
155+ # so simply check if it's a long string
156+ if cert .startswith ('-----BEGIN CERTIFICATE-----' ):
157+ if force or _check_import_string (isamAppliance , kdb_id , label , cert , check_mode = check_mode ):
158+ if check_mode :
159+ return isamAppliance .create_return_object (changed = True )
160+ else :
161+ json_data = {}
162+ json_data ['label' ] = label
163+ json_data ['cert' ] = cert
164+ json_data ['operation' ] = "import" # this is missing from the documentation.
165+ if version_compare (isamAppliance .facts ['version' ], "10.0.5.0" ) >= 0 :
166+ json_data ['preserve_label' ] = preserve_label
167+ return isamAppliance .invoke_post ("Create signer cert from certificate string" ,
168+ f"/isam/ssl_certificates/{ kdb_id } /signer_cert" ,
169+ json_data )
170+ else :
171+ if force or _check_import (isamAppliance , kdb_id , label , cert , check_mode = check_mode ):
172+ if check_mode :
173+ return isamAppliance .create_return_object (changed = True )
174+ else :
175+ if version_compare (isamAppliance .facts ['version' ], "10.0.5.0" ) < 0 :
176+ return isamAppliance .invoke_post_files (
177+ "Importing a signer certificate into a certificate database" ,
178+ f"/isam/ssl_certificates/{ kdb_id } /signer_cert" ,
179+ [
180+ {
181+ 'file_formfield' : 'cert' ,
182+ 'filename' : cert ,
183+ 'mimetype' : 'application/octet-stream'
184+ }
185+ ],
186+ {'label' : label })
187+ else :
188+ return isamAppliance .invoke_post_files (
189+ "Importing a signer certificate into a certificate database" ,
190+ f"/isam/ssl_certificates/{ kdb_id } /signer_cert" ,
191+ [
192+ {
193+ 'file_formfield' : 'cert' ,
194+ 'filename' : cert ,
195+ 'mimetype' : 'application/octet-stream'
196+ }
197+ ],
198+ {'label' : label ,
199+ 'preserve_label' : preserve_label })
182200
183201 return isamAppliance .create_return_object ()
184202
@@ -196,6 +214,16 @@ def _check(isamAppliance, kdb_id, cert_id):
196214 return False
197215
198216
217+ def _check_import_string (isamAppliance , kdb_id , label , certstring , check_mode = False ):
218+ # TODO: DO SOMETHING
219+ cert_pem = get (isamAppliance , kdb_id , label )['data' ]['contents' ]
220+ if cert_pem .replace ("\n " , "" ) == certstring .replace ("\n " , "" ):
221+ logger .debug (f"Certificate already exists with same label { label } " )
222+ return False
223+ else :
224+ return True
225+
226+
199227def _check_import (isamAppliance , kdb_id , cert_id , filename , check_mode = False ):
200228 """
201229 Checks if certificate on the Appliance exists and if so, whether it is different from
0 commit comments