parameters: (logger, config_path, mode)
return_statement: return context
docstring: If use https method, it is used for checking whether CA and Agent certificate is valid,
          if certificate is not valid, then exit process, otherwise return right context;
          if use http method, it skip checking and just return None.
          :param mode: agent|server
          :param logger: logger object.
          :param config_path: string, config path.
          :return: if 'https', return certificate context, else return None.
docstring_summary: If use https method, it is used for checking whether CA and Agent certificate is valid,
          if certificate is not valid, then exit process, otherwise return right context;
          if use http method, it skip checking and just return None.
          :param mode: agent|server
          :param logger: logger object.
          :param config_path: string, config path.
          :return: if 'https', return certificate context, else return None.
docstring_tokens: If use https method it is used for checking whether CA and Agent certificate is valid if certificate is not valid then exit process otherwise return right context ; if use http method it skip checking and just return None . : param mode : agent|server : param logger : logger object . : param config_path : string config path . : return : if https return certificate context else return None .
function: def check_certificate_setting(logger, config_path, mode):
          """
          If use https method, it is used for checking whether CA and Agent certificate is valid,
          if certificate is not valid, then exit process, otherwise return right context;
          if use http method, it skip checking and just return None.
          :param mode: agent|server
          :param logger: logger object.
          :param config_path: string, config path.
          :return: if 'https', return certificate context, else return None.
          """
          logger.info("Checking certificate setting...")
          context = None
          config = ConfigParser()
          config.read(config_path)
          if config.has_option('security', 'tls') and config.getboolean('security', 'tls'):
              try:
                  agent_cert = os.path.realpath(config.get('security', '%s_cert' % mode))
                  agent_key = os.path.realpath(config.get('security', '%s_key' % mode))
                  ca = os.path.realpath(config.get('security', 'ca'))
              except Exception as e:
                  logger.error(e)
                  sys.exit(1)
              else:
                  ssl_certificate_status = Common.check_certificate(agent_cert)
                  ca_certificate_status = Common.check_certificate(ca)
                  if ssl_certificate_status['status'] == 'fail' \
                          or ca_certificate_status['status'] == 'fail':
                      logger.warn("error occur when check 'certificate'.")
                  else:
                      if ssl_certificate_status['level'] == 'error' or \
                              ca_certificate_status['level'] == 'error':
                          logger.error("{ssl_certificate_info}; {ca_certificate_info}".format(
                              ssl_certificate_info=ssl_certificate_status['info'],
                              ca_certificate_info=ca_certificate_status['info']))
                          sys.exit(1)
                      else:
                          logger.warn("{ssl_certificate_info}; {ca_certificate_info}".format(
                              ssl_certificate_info=ssl_certificate_status['info'],
                              ca_certificate_info=ca_certificate_status['info']))
  
                          pw_file = os.path.join(os.path.dirname(config_path), 'certificate/pwf')
                          from common.encrypt_decrypt_handler import AesCbcUtil
                          pw = AesCbcUtil.aes_cbc_decrypt_with_multi(pw_file)
  
                          context = ssl.create_default_context(ssl.Purpose.SERVER_AUTH, cafile=ca)
                          context.check_hostname = False
                          context.load_cert_chain(certfile=agent_cert, keyfile=agent_key, password=pw)
          logger.info("Successfully checked certificate setting.")
          return context
function_tokens: def check_certificate_setting ( logger , config_path , mode ) : logger . info ( "Checking certificate setting..." ) context = None config = ConfigParser ( ) config . read ( config_path ) if config . has_option ( 'security' , 'tls' ) and config . getboolean ( 'security' , 'tls' ) : try : agent_cert = os . path . realpath ( config . get ( 'security' , '%s_cert' % mode ) ) agent_key = os . path . realpath ( config . get ( 'security' , '%s_key' % mode ) ) ca = os . path . realpath ( config . get ( 'security' , 'ca' ) ) except Exception as e : logger . error ( e ) sys . exit ( 1 ) else : ssl_certificate_status = Common . check_certificate ( agent_cert ) ca_certificate_status = Common . check_certificate ( ca ) if ssl_certificate_status [ 'status' ] == 'fail' or ca_certificate_status [ 'status' ] == 'fail' : logger . warn ( "error occur when check 'certificate'." ) else : if ssl_certificate_status [ 'level' ] == 'error' or ca_certificate_status [ 'level' ] == 'error' : logger . error ( "{ssl_certificate_info}; {ca_certificate_info}" . format ( ssl_certificate_info = ssl_certificate_status [ 'info' ] , ca_certificate_info = ca_certificate_status [ 'info' ] ) ) sys . exit ( 1 ) else : logger . warn ( "{ssl_certificate_info}; {ca_certificate_info}" . format ( ssl_certificate_info = ssl_certificate_status [ 'info' ] , ca_certificate_info = ca_certificate_status [ 'info' ] ) ) pw_file = os . path . join ( os . path . dirname ( config_path ) , 'certificate/pwf' ) from common . encrypt_decrypt_handler import AesCbcUtil pw = AesCbcUtil . aes_cbc_decrypt_with_multi ( pw_file ) context = ssl . create_default_context ( ssl . Purpose . SERVER_AUTH , cafile = ca ) context . check_hostname = False context . load_cert_chain ( certfile = agent_cert , keyfile = agent_key , password = pw ) logger . info ( "Successfully checked certificate setting." ) return context