Skip to content

Commit 2ca2eb7

Browse files
committed
add ssl certificates path existence verification
1 parent 37996c3 commit 2ca2eb7

File tree

1 file changed

+37
-7
lines changed

1 file changed

+37
-7
lines changed

source/zimonGrafanaIntf.py

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@
4646
'IntError':'Server internal error occurred. Reason: {}',
4747
'sysStart':'Initial cherryPy server engine start have been invoked. Python version: {}, cherryPy version: {}.',
4848
'MissingParm':'Missing mandatory parameters, quitting',
49+
'KeyPathError':'KeyPath directory not found, quitting',
50+
'CertError':'Missing certificates in tht specified keyPath directory, quitting',
4951
'CollectorErr':'Failed to initialize connection to pmcollector, quitting',
5052
'MetaError':'Metadata could not be retrieved. Check log file for more details, quitting',
5153
'MetaSuccess': 'Successfully retrieved MetaData',
@@ -615,6 +617,21 @@ def validateCollectorConf(args, logger):
615617
logger.info(MSG['Query2port'].format(args.serverPort))
616618

617619

620+
def findKeyFile(path):
621+
for name in ["privkey.pem","tls.key"]:
622+
for root, dirs, files in os.walk(path):
623+
if name in files:
624+
return name
625+
return None
626+
627+
def findCertFile(path):
628+
for name in ["cert.pem","tls.crt"]:
629+
for root, dirs, files in os.walk(path):
630+
if name in files:
631+
return name
632+
return None
633+
634+
618635

619636
def updateCherrypyConf(args):
620637

@@ -637,13 +654,15 @@ def updateCherrypyConf(args):
637654
'tools.encode.encoding' : 'utf-8'}}
638655
cherrypy.config.update(globalConfig)
639656

640-
if args.port == 8443:
641-
sslConfig = {
642-
'global' : {
643-
'server.ssl_module' : 'builtin',
644-
'server.ssl_certificate' : args.keyPath + "/cert.pem",
645-
'server.ssl_private_key' : args.keyPath + "/privkey.pem" }}
646-
cherrypy.config.update(sslConfig)
657+
def updateCherrypySslConf(args, certFile, keyFile):
658+
certPath = os.path.join(args.keyPath, certFile)
659+
keyPath = os.path.join(args.keyPath, keyFile)
660+
sslConfig = {
661+
'global' : {
662+
'server.ssl_module' : 'builtin',
663+
'server.ssl_certificate' : certPath,
664+
'server.ssl_private_key' : keyPath }}
665+
cherrypy.config.update(sslConfig)
647666

648667

649668

@@ -666,13 +685,24 @@ def main(argv):
666685
if args.port == 8443 and not args.keyPath:
667686
print(MSG['MissingParm'])
668687
return
688+
elif not os.path.exists(args.keyPath):
689+
print(MSG['KeyPathError'])
690+
return
691+
else:
692+
certFile = findCertFile(args.keyPath)
693+
keyFile = findKeyFile(args.keyPath)
694+
if (not certFile) or (not keyFile):
695+
print(MSG['CertError'])
696+
return
669697

670698
# prepare the logger
671699
logger = configureLogging(args.logFile, args.logLevel)
672700
logger.info('zimonGrafanaItf invoked with parameters:%s', str(args))
673701

674702
#prepare cherrypy server configuration
675703
updateCherrypyConf(args)
704+
if args.port == 8443:
705+
updateCherrypySslConf(args, certFile, keyFile)
676706

677707

678708

0 commit comments

Comments
 (0)