+
+def timeIncrement(timePassed):
+ time.sleep(INTERVAL)
+ timePassed = timePassed + INTERVAL
+ return timePassed
+
+def get_cadi_password():
+ try:
+ with open(Path + '/' + cadi_file , 'r') as file_obj:
+ cadi_pass = file_obj.read().split('=', 1)[1].strip()
+ return cadi_pass
+ except Exception as e:
+ logging.error("Error occurred while fetching password : %s", e)
+ exit()
+
+def cleanup():
+ for file in os.listdir(Path):
+ if os.path.isfile(Path + '/' + file):
+ logging.debug("Cleaning up the file %s", Path + '/'+ file)
+ os.remove(Path + '/'+ file)
+
+def extract_content(file, password, count):
+ try:
+ certList = []
+ key = None
+ cert = None
+ if (file.endswith('.jks')):
+ p12_file = file.replace('.jks', '.p12')
+ jks_cmd = 'keytool -importkeystore -srckeystore {src_file} -destkeystore {dest_file} -srcstoretype JKS -srcstorepass {src_pass} -deststoretype PKCS12 -deststorepass {dest_pass}'.format(src_file=file, dest_file=p12_file, src_pass=password, dest_pass=password)
+ logging.debug("Converting %s into p12 format", file)
+ os.system(jks_cmd)
+ file = p12_file
+
+ clcrt_cmd = 'openssl pkcs12 -in {src_file} -clcerts -nokeys -passin pass:{src_pass}'.format(src_file=file, src_pass=password)
+ clkey_cmd = 'openssl pkcs12 -in {src_file} -nocerts -nodes -passin pass:{src_pass}'.format(src_file=file, src_pass=password)
+ trust_file = file.split('/')[2] + '.trust'
+ trustCerts_cmd = 'openssl pkcs12 -in {src_file} -out {out_file} -cacerts -nokeys -passin pass:{src_pass} '.format(src_file=file, out_file=Path + '/' + trust_file, src_pass=password)
+
+ result_key = subprocess.check_output(clkey_cmd , shell=True)
+ if result_key:
+ key = result_key.split('-----BEGIN PRIVATE KEY-----', 1)[1].lstrip().split('-----END PRIVATE KEY-----')[0]
+
+ os.system(trustCerts_cmd)
+ if os.path.exists(Path + '/' + trust_file):
+ certList = readTrustedCertificate(Path, trust_file)
+
+ result_crt = subprocess.check_output(clcrt_cmd , shell=True)
+ if result_crt:
+ cert = result_crt.split('-----BEGIN CERTIFICATE-----', 1)[1].lstrip().split('-----END CERTIFICATE-----')[0]
+ """
+ To-do: Posting the key, cert, certList might need modification
+ based on how AAF distributes the files.
+
+ """
+ post_content(key, cert, certList, count)
+ except Exception as e:
+ logging.error("Error occurred while processing the file %s : %s", file,e)
+
+def lookforfiles():
+ count = 0
+ for file in os.listdir(Path):
+ if (file.endswith(('.p12', '.jks'))):
+ if os.path.exists(Path + '/' + cadi_file):
+ cert_password = get_cadi_password()
+ logging.debug("Extracting contents from the file %s", file)
+ extract_content(Path + '/' + file, cert_password, count)
+ count += 1
+ else:
+ logging.error("Cadi password file %s not present under cert directory", cadi_file)
+ exit()
+ if count > 0:
+ cleanup()
+ else:
+ logging.debug("No jks/p12 files found under cert directory %s", Path)
+
+
+def readCertProperties():
+ connected = makeHealthcheckCall(headers, timePassed)