예제 #1
0
	def __init__(self):
		self.utility = Utility()
		self.globals = Global()
		self.infra = RestInfra()
		self.logger = self.infra.Logger
			
		self.ui_util = InterfaceUtil()
		self.security = Security()
예제 #2
0
    def __init__(self):
        self.utility = Utility()
        self.globals = Global()
        self.infra = RestInfra()
        self.logger = self.infra.logger

        self.mySqlUtil = MysqlUtil(self.logger)
        self.repConn = self.mySqlUtil.getRepDBConnection()
예제 #3
0
    def __init__(self, logger=None):

        self.util = Utility()
        self.globals = Global()
        #self.env = Environment()
        self.dbResponse = self.util.getACopy(
            self.globals.Template['DBResponse'])
        self.infra = RestInfra()
        self.encryptKey = self.infra._Infra__bootStrapData['Main']['Key']

        if logger:
            self.logger = logger
        else:
            print('no logger is available, can not store logging information')
            self.logger = None
예제 #4
0
    def __init__(self):

        try:
            #print('initializing env 1')
            self.env = Environment()
            self.globals = Global()
            self.util = Utility()
            #self.logging = Logging()

            self.configLoc = self.env.appCfgLoc
            self.bootStrapFile = os.path.join(self.configLoc,
                                              self.globals.bootStrapFile)

            if not self.bootStrapFile:
                print('error: port8-infra10001, bootstrap error !!!')
                sys.exit(-1)

            self.dbConfigData = {}
            self.daemonConfifData = {}
            self.schedulerConfigData = {}
            self.restApiConfigData = {}
            self.agentConfigData = {}

            print("Loading bootstrap config....................".ljust(50),
                  end='')
            self.__loadBootStrapConfig()
            #self.jsonSchema = JsonRef.replace_refs(self.__bootStrapData['Main'])
            #self.__loadSchema()
            #self.jsonSchemaConfigData

            # ensuring all needed lib is available
            print("Checking all required libraries................".ljust(50),
                  end='')
            self.__validateAllLib()

            print("[infra started with OS pid {pid}]".format(
                pid=self.util.getMyOsPid()))

        except Exception as err:

            exc_type, exc_value, exc_traceback = sys.exc_info()
            myErrorMessage = repr(
                traceback.format_exception(exc_type, exc_value, exc_traceback))

            print('Error [{error}], terminating !!!'.format(
                error=myErrorMessage))
            sys.exit(-1)
예제 #5
0
    def __init__(self):

        try:
            '''
            if not infraType or infraType not in ['REST','SCHEDULE','DAEMON']:
                print('expectig valid infra type REST/SCHEDULE/DAMEON got {got}, exiting !!!'.format(got = infraType))
                sys.exit(-1)
            '''
            self.env = Environment()
            self.globals = Global()
            self.util = Utility()
            self.logging = Logging()

            self.configLoc = self.env.appCfgLoc
            #print(self.configLoc)
            self.bootStrapFile = os.path.join(self.configLog, self.globals.bootStrapFile)
            if not self.bootStrapFile:
                print('error: port8-infra10001, bootstrap error !!!')
                sys.exit(-1)

            self.dbConfigData = {}
            self.daemonConfifData = {}
            self.schedulerConfigData = {}
            self.restApiConfigData = {}
            self.agentConfigData = {}

            #print(self.Env._Env__initData)
            #print(self.Env._Env__EnvDetails)

            print("Loading bootstrap config....................".ljust(50),end='')
            self.__loadBootStrapConfig()
            self.__loadSchema()

            # ensuring all needed lib is available
            print("Checking all required libraries................".ljust(50),end='')
            self.__validateAllLib()

            '''
            self.Logger = \
                self.logging.buildLoggingInfra(\
                    self.loggingConfigData,\
                    self.globals.LoggerName[infraType]['LogFile'],
                    self.globals.LoggerName[infraType]['Name']
                    )
            '''
            #self.Logger.debug('this is test from Infrastructure')

            # ensure db is up and repository is abvailable
            #print("Validating database............................".ljust(50),end='')
            #self.__validateDb(infraType)
            #self.db = self.__getNewConnection()

            # loading DB schema
            # populating json schema for database
            '''
            commenting follwoing line, will revisit later to implement jsonref for db
            self.__loadDbJsonSchema()        
            '''
            #print("Loading logging config.........................".ljust(50),end='')
            #self.logger = Logging(self.loggingConfigData)
            #logger 
            print("[infra started with OS pid {pid}]".format(pid=self.getMyOsPid()))

            #self.db_dyna_sql = self.jsonSchema.get('Main').get('db_dyna_sql_call')
            #self.myModuleLogger.info ('Infrastructure started with os pid [{pid}]'.format(pid=os.getpid()))

        except Exception as err:

            exc_type, exc_value, exc_traceback = sys.exc_info()
            myErrorMessage = repr(traceback.format_exception(exc_type, exc_value, exc_traceback))

            print ('Error [{error}], terminating !!!'.format(error=myErrorMessage))
            sys.exit(-1)
예제 #6
0
class Infra(object, metaclass=Singleton):

    def __init__(self):

        try:
            '''
            if not infraType or infraType not in ['REST','SCHEDULE','DAEMON']:
                print('expectig valid infra type REST/SCHEDULE/DAMEON got {got}, exiting !!!'.format(got = infraType))
                sys.exit(-1)
            '''
            self.env = Environment()
            self.globals = Global()
            self.util = Utility()
            self.logging = Logging()

            self.configLoc = self.env.appCfgLoc
            #print(self.configLoc)
            self.bootStrapFile = os.path.join(self.configLog, self.globals.bootStrapFile)
            if not self.bootStrapFile:
                print('error: port8-infra10001, bootstrap error !!!')
                sys.exit(-1)

            self.dbConfigData = {}
            self.daemonConfifData = {}
            self.schedulerConfigData = {}
            self.restApiConfigData = {}
            self.agentConfigData = {}

            #print(self.Env._Env__initData)
            #print(self.Env._Env__EnvDetails)

            print("Loading bootstrap config....................".ljust(50),end='')
            self.__loadBootStrapConfig()
            self.__loadSchema()

            # ensuring all needed lib is available
            print("Checking all required libraries................".ljust(50),end='')
            self.__validateAllLib()

            '''
            self.Logger = \
                self.logging.buildLoggingInfra(\
                    self.loggingConfigData,\
                    self.globals.LoggerName[infraType]['LogFile'],
                    self.globals.LoggerName[infraType]['Name']
                    )
            '''
            #self.Logger.debug('this is test from Infrastructure')

            # ensure db is up and repository is abvailable
            #print("Validating database............................".ljust(50),end='')
            #self.__validateDb(infraType)
            #self.db = self.__getNewConnection()

            # loading DB schema
            # populating json schema for database
            '''
            commenting follwoing line, will revisit later to implement jsonref for db
            self.__loadDbJsonSchema()        
            '''
            #print("Loading logging config.........................".ljust(50),end='')
            #self.logger = Logging(self.loggingConfigData)
            #logger 
            print("[infra started with OS pid {pid}]".format(pid=self.getMyOsPid()))

            #self.db_dyna_sql = self.jsonSchema.get('Main').get('db_dyna_sql_call')
            #self.myModuleLogger.info ('Infrastructure started with os pid [{pid}]'.format(pid=os.getpid()))

        except Exception as err:

            exc_type, exc_value, exc_traceback = sys.exc_info()
            myErrorMessage = repr(traceback.format_exception(exc_type, exc_value, exc_traceback))

            print ('Error [{error}], terminating !!!'.format(error=myErrorMessage))
            sys.exit(-1)

    def __loadBootStrapConfig(self):

        # read bootstrap cnfig file
        try:
            self.__bootStrapData = json.load(open(self.bootStrapFile))

            if not self.__bootStrapData:
                print("Empty bootstrap data, terminating !!!")
                sys.exit(-1) 

            # validating if all modules config data is available
            myMissingModule = self.util.findMissingKeyInDict(self.__bootStrapData['Main']['Modules'], list(self.__bootStrapData['Main']['Modules'].keys()))
            if myMissingModule:
                print('Missing config information for module {0}'.format(myMissingModule))
                sys.exit(-1)

            #extracting module config information
            if self.env.environment not in self.__bootStrapData['Main']['Modules']['DB']['REPOSITORY']:
                print('[Error]\n     unable to find repository information for environment >> {env}'.format(env = self.env.environment))
                sys.exit(-1)

            if not self.env.environment in self.__bootStrapData['Main']['Modules']['DB']['REPOSITORY']:
                print('invalid environment >>> {env}'.format(env=self.env.Environment))
                sys.exit(-1)

            self.repConfigData = self.__bootStrapData['Main']['Modules']['DB']['REPOSITORY'][self.env.environment]
            self.dbConfigData = {
                'user' : self.repConfigData['User'], 
                'password' : self.repConfigData['enc'], 
                'host' : self.repConfigData['Host'], 
                'port' : self.repConfigData['Port'],
                'database' : self.repConfigData['Name']
            }

            self.schedulerConfigData = self.__bootStrapData['Main']['Modules']['Scheduler']
            self.loggingConfigData = self.__bootStrapData['Main']['Modules']['Logging']
            self.restApiConfigData = self.__bootStrapData['Main']['Modules']['RestAPI']
            self.daemonConfigData  = self.__bootStrapData['Main']['Modules']['Daemon']
            self.jsonSchemaConfigData  = self.__bootStrapData['Main']['JsonSchema']

            if (not self.repConfigData):
                print('Repository DB Configuration is empty')
                sys.exit(-1)

            if (not self.schedulerConfigData):
                print('Scheduler Configuration is empty')
                sys.exit(-1)

            if (not self.loggingConfigData):
                print('Loggine Configuration is empty')
                sys.exit(-1)

            if (not self.restApiConfigData):
                print('RestApi Configuration is empty')
                sys.exit(-1)

            if (not self.restApiConfigData):
                print('Daemon Configuration is empty')
                sys.exit(-1)

            if (not self.jsonSchemaConfigData):
                print('Json Schema Configuration is empty')
                sys.exit(-1)

            print("[OK]")

        except Exception as err:
            myErrorMessage = sys.exc_info()[1:],traceback.format_exc(limit=2)            
            #myErrorMessage = self.Utility.extractError()
            print(myErrorMessage)
            #print("Error [{err}] loading bootstrap config data".format(err=err))
            sys.exit(-1)
    
    def __loadSchema(self):
        
        # load json schema data and resolve the reference if used, this will be needed to validate user data
        #print(self.configLoc)
        configFile = self.util.buildFileWPath(self.configLoc, self.jsonSchemaConfigData['ConfigFile'])
        if not self.util.isFileExist(configFile):
            print('Json Schema Configuration file {file} is missing !!!'.format(file=os.path.join(self.configLoc,self.jsonSchemaConfigData['ConfigFile'])))
            sys.exit(-1)
        
        #loading json schema file and resolving references if used in schema
        try:
            self.jsonSchema = JsonRef.replace_refs(json.load(open(configFile)))
            #print(self.jsonSchema)
        except Exception as err:
            myErrorMessage = sys.exc_info()[1:],traceback.format_exc(limit=2)            
            print (myErrorMessage)
            sys.exit(-1)

    def __validateAllLib(self):

        # Ensuring all required library as specified in bootStrap.json is installed
        try:
            for lib in self.__bootStrapData['Main']['libraries']:
                if not self.util.isPackageInstalled(lib):
                    #self.myModuleLogger.error('Lib {frmtLib} is not installed, Pls install it using <pip install {frmtLib}>'.format(frmtLib=lib))
                    print("[missing library {frmtLib}]".format(frmtLib=lib))
                    sys.exit(-1)

            print("[OK]")

        except Exception as err:
            exc_type, exc_value, exc_traceback = sys.exc_info()
            myErrorMessage = repr(traceback.format_exception(exc_type, exc_value, exc_traceback))

            print('[Failed {err}]'.format(err=myErrorMessage))      
            sys.exit(-1)

    def isValidBootStrapData(self):
        if self.__bootStrapData:
            return True
        else:
            return False

    def isValidLogger(self):
        if self.myModuleLogger:
            return True
        else:
            return False

    def getBootStrapFile(self):
        
        #print(self.env._Environment__bootStrapFileKey)
        #print (self.env._Environment__initData)
        return self.env._Environment__initData[self.env._Environment__bootStrapFileKey]
        #mybootStrapFile = os.getEnv[self.Env._Environment__bootStrapKey]

    def getAppName(self):
        return os.Environ['APP_NAME']

    def getMyOsPid(self):
        # returns current OS pid
        return os.getpid()

    def getInfraLogger(self, infraArg):
        if infraArg == 'Scheduler':
            return self.SchedLogger
        elif infraArg == 'Daemon':
            return self.DaemonLogger
        elif infraArg == 'Rest':
            return self.RestLogger

    def isModuleInstalled(self,  argLib):

        # returns existence of a library passed as an argument

        try:
            __import__('imp').find_module(argLib)
            return True
        except ImportError:
            #print('Missing library {lib}'.format(lib=argLib))
            return False

    #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    #                                  method for DB
    #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++    

    def __validateMySqlDB(self):

        self.dbLib = importlib.import_module('mysql.connector')

        # validate the db config file
        try:
            # connecting to database to ensure db is up
            db = self.dbLib.connect(host = self.dbConfigData['host'], port = self.dbConfigData['port'], user = self.dbConfigData['user'], password = self.util.decrypt(self.repConfigData['key'], self.dbConfigData['password']), database = self.dbConfigData['database'])

            # connection is successful, closing the connection
            db.close()
            # connection is successful
        except self.dbLib.Error as err:
            if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
                print("something is wrong with your user name or password, exiting !!!")
                sys.exit(-1)
            elif err.errno == errorcode.ER_BAD_DB_ERROR:
                print("Database does not exist, exiting !!!")
                sys.exit(-1)
            else:
                print(err)
        '''
        except Exception as err:
            exc_type, exc_value, exc_traceback = sys.exc_info()
            myErrorMessage = repr(traceback.format_exception(exc_type, exc_value, exc_traceback))

            print('[Failed {err}]'.format(err=myErrorMessage))      
            sys.exit(-1)
        '''

    def __validateDb(self,infraType):
        #print(self.repConfigData)
        if infraType == 'REST':
            if self.repConfigData['Vendor'] == "mysql" :
                self.__validateMySqlDB()
            else:
                print('unsuported database for repository')
                sys.exit(-1)
        else:
            pass
        print("[OK]")

    def __loadDbJsonSchema(self):
        #self.dbSchema = self.jsonSchema.get('Main').get('db_schema').get(self.__bootStrapData['Main']['Modules']['DB']['REPOSITORY'])
        
        self.dbSchema = self.jsonSchema.get('Main').get('db_schema').get(self.__bootStrapData['Main']['Modules']['DB']['REPOSITORY'][self.env.environment])

    def __getNewConnection(self):
        try:
            #print('db config',self.dbConfigData)
            db = self.dbLib.connect(host = self.dbConfigData['host'], port = self.dbConfigData['port'], user = self.dbConfigData['user'], password = self.util.decrypt(self.repConfigData['key'], self.dbConfigData['password']), database = self.dbConfigData['database'])
            #dbCursor = db.cursor(dictionary=True, buffered=True)
            #dbCursor = db.cursor(buffered=True)
            return db
        except Exception as err:
            self.logger.critical('Error obtaining db connection using {}'.format(self.dbConfigData))
            raise err

    def __closeConnection(self, conn):
        if conn:
            conn.close()

    def isConnectionOpen(self, conn):
        try:
            dbCur = conn.cursor()
            dbCur.execute('select now()')
            return True
        except Exception as err:
            return False
예제 #7
0
 def __init__(self):
     self.env = Environment()
     self.Global = Global()
     self.Utility = Utility()
예제 #8
0
class Logging(object, metaclass=Singleton):
    def __init__(self):
        self.env = Environment()
        self.Global = Global()
        self.Utility = Utility()
        #myLogger = self.__buildLoggingInfra(logging_cfg = loggingCfgData, logger = logger)
        #return myLogger

        #return myLogger

    #def __buildLoggingInfra(self, loggingCfg, logFile = None , logger = 'Default'):
    def buildLoggingInfra(self, loggingCfgData, logFile, logger):
        ''' Logging utility, constructing dictionary object to be used by native logging module
        Arguments:
            loggingCfgData: Logging configuration
            logFile: LogFile need to be written
            logger: name of logger
        Level: 
        ++++++++++++++++++++
        CRITICAL    50
        ERROR   40
        WARNING 30
        INFO    20
        DEBUG   10
        NOTSET  0
        ++++++++++++++++++++
        '''
        try:

            #validating parameter

            if (not loggingCfgData):
                print("missing logging configuraton data !!!")
                sys.exit(-1)
            #fi

            #myLogPath = self.env.
            #print('LogPath',myLogPath)
            #myConfigFile = os.path.join(self.env.getConfigLocation(), os.path.basename(argCfgFile))

            # check if logging config file is available
            #if not self.env.isFileExists(myConfigFile):
            #    print("could not locate Logging Config file {cfgFile}, terminating !!!".format(cfgFile=myConfigFile))
            #    sys.exit(-1)
            #fi

            # reading current config file
            myLoggingConfig = self.Utility.getACopy(loggingCfgData)

            # checking if we got valid logger name, if not will use default logger name "Console"
            if logger not in myLoggingConfig['loggers']:
                myLogger = self.Global.LoggerName[
                    self.Global.DefaultInfra]['Name']
            else:
                myLogger = logger
            #fi

            # will override logfile if passed as an argument also, set the directory as defined in LOG_LOC env variable
            if logFile:
                # will use the path as specified in LOG_PATH environment variable, any path specified in argument will be ignored
                myLogFile = os.path.basename(logFile)
                myLogFileWPath = os.path.join(self.env.appLogLoc, myLogFile)
            else:
                myLogFileWPath = os.path.join(
                    self.env.appLogLoc,
                    os.path.basename(
                        myLoggingConfig['handlers']['file']['filename']))
            #fi

            if not (myLogger == self.Global.DefaultInfra):
                myLoggingConfig['handlers']['file'][
                    'filename'] = myLogFileWPath

            # Loading current configuration to logger's config file
            logging.config.dictConfig(myLoggingConfig)
            #print('Logger',myLogger)
            return logging.getLogger(myLogger)

        except Exception as err:
            print("\n")
            exc_type, exc_value, exc_traceback = sys.exc_info()
            myErrorMessage = repr(
                traceback.format_exception(exc_type, exc_value, exc_traceback))

            print(
                "Error [{err}] builidng logging infra using config [{cfg}], terminating !!!"
                .format(err=myErrorMessage, cfg=myLoggingConfig))
            sys.exit(-1)
예제 #9
0
    def __init__(self):

        self.Global = Global()
        self.util = Utility()

        if not self.util.isEnvSet('ENVIRONMENT'):
            raise ValueError('ENVIORNMENT key is not set')

        if not self.util.isEnvSet('ENVIRONMENT'):
            print('environment is not set, exiting !!!')
            sys.exit(-1)

        self.environment = self.util.getEnv('ENVIRONMENT')

        print('setting environment >> {env}'.format(env=self.environment))
        # need to set environments specific variables dev, test and production
        self.lPad = 50
        self.rPad = 1
        self.__initFile = self.Global.InitFile
        self.__initKeys = self.Global.InitKeys
        self.__bootStrapKey = self.Global.BootStrapKey
        self.__bootStrapFileKey = self.Global.BootStrapFileKey
        self.__logLocKey = self.Global.LogLocKey
        self.__envDetails={}
        self.invalidAppDir = list()
        self.invalidAppFile = list()


        if not self.util.isEnvSet('PYTHONPATH'):
            print('missing PYTHONPATH environment variable, terminating !!!')
            sys.exit(-1)
        else:
            self.__envDetails.update({'PYTHONPATH'  : self.util.getEnv('PYTHONPATH')})
        #fi

        print('Current OS : [{os}]'.format(os=self.util.getUname()))
        print ("Initializing ...")

        # searching init file
        # 1. search in current directory, 2. search in pythonpath, 3. search in APP_CONFIG dir

        print('searching init file')
        # searching file in current dir
        if not self.util.isFileExist(self.__initFile):
            # searching file in PYTHONPATH
            if not(self.util.isFileExist(self.util.buildFileWPath(self.__envDetails['PYTHONPATH'],self.__initFile))):
                if self.util.isEnvSet('APP_CONFIG'):
                    # searching file in APP_CONFIG dir
                    if not(self.util.isFileExist(self.util.buildFileWPath(self.util.getEnv('APP_CONFIG'),self.__initFile))):
                        print("Bootstrap error; missing init.json, terminating !!!")
                        sys.exit(-1)
                    else:
                        self.__initFile = self.util.buildFileWPath(self.util.getEnv('APP_CONFIG'),self.__initFile)
                        print('init file [{file}] found'.format(file=self.__initFile))
                else:
                    print("Bootstrap error; Exhasuted searching couldn\'t find init file, terminating !!!")
                    sys.exit(-1)
            else:
                self.__initFile = self.util.buildFileWPath(self.__envDetails['PYTHONPATH'],self.__initFile)
                print('init file [{file}] found'.format(file=self.__initFile))

        # Loading init file
        self.__initData = json.load(open(self.__initFile))
        if not self.__initData:
            print('Bootstra error, could not load init file >>> {file}'.format(file = self.__initFile))
            sys.exit(-1)

        # ensure all required key found in int file
        if not all (key in self.__initData.keys() for key in self.__initKeys):
            print("Missing required keys in init file, terminating !!!")
            sys.exit(-1)

        # Set all environment variable if not set
        if not all (self.util.isEnvSet(env) for env in self.__envDetails.keys()):
            print("app environment key is not set, building ...")
            #print("Setting environment...........................".ljust(self.lPad),end='')
            # app home
            self.setInitEnv()

        # validating required app directories/files >>> allAppDir = [dir[key] for index, key in enumerate(self.__initData)]
        #print(self.__initData)
        self.validateAppDir(self.__initData['VALIDATE_DIR_LIST'])
        self.validateAppFile(self.__initData['VALIDATE_FILE_LIST'])
        self.validateLogDir(self.__initData['APP_LOG']) # validating app log, create if we are missing

        self.appCfgLoc = self.__initData['APP_CONFIG']
        self.appLogLoc = self.__initData['APP_LOG']
예제 #10
0
class Environment(object, metaclass=Singleton):

    def __init__(self):

        self.Global = Global()
        self.util = Utility()

        if not self.util.isEnvSet('ENVIRONMENT'):
            raise ValueError('ENVIORNMENT key is not set')

        if not self.util.isEnvSet('ENVIRONMENT'):
            print('environment is not set, exiting !!!')
            sys.exit(-1)

        self.environment = self.util.getEnv('ENVIRONMENT')

        print('setting environment >> {env}'.format(env=self.environment))
        # need to set environments specific variables dev, test and production
        self.lPad = 50
        self.rPad = 1
        self.__initFile = self.Global.InitFile
        self.__initKeys = self.Global.InitKeys
        self.__bootStrapKey = self.Global.BootStrapKey
        self.__bootStrapFileKey = self.Global.BootStrapFileKey
        self.__logLocKey = self.Global.LogLocKey
        self.__envDetails={}
        self.invalidAppDir = list()
        self.invalidAppFile = list()


        if not self.util.isEnvSet('PYTHONPATH'):
            print('missing PYTHONPATH environment variable, terminating !!!')
            sys.exit(-1)
        else:
            self.__envDetails.update({'PYTHONPATH'  : self.util.getEnv('PYTHONPATH')})
        #fi

        print('Current OS : [{os}]'.format(os=self.util.getUname()))
        print ("Initializing ...")

        # searching init file
        # 1. search in current directory, 2. search in pythonpath, 3. search in APP_CONFIG dir

        print('searching init file')
        # searching file in current dir
        if not self.util.isFileExist(self.__initFile):
            # searching file in PYTHONPATH
            if not(self.util.isFileExist(self.util.buildFileWPath(self.__envDetails['PYTHONPATH'],self.__initFile))):
                if self.util.isEnvSet('APP_CONFIG'):
                    # searching file in APP_CONFIG dir
                    if not(self.util.isFileExist(self.util.buildFileWPath(self.util.getEnv('APP_CONFIG'),self.__initFile))):
                        print("Bootstrap error; missing init.json, terminating !!!")
                        sys.exit(-1)
                    else:
                        self.__initFile = self.util.buildFileWPath(self.util.getEnv('APP_CONFIG'),self.__initFile)
                        print('init file [{file}] found'.format(file=self.__initFile))
                else:
                    print("Bootstrap error; Exhasuted searching couldn\'t find init file, terminating !!!")
                    sys.exit(-1)
            else:
                self.__initFile = self.util.buildFileWPath(self.__envDetails['PYTHONPATH'],self.__initFile)
                print('init file [{file}] found'.format(file=self.__initFile))

        # Loading init file
        self.__initData = json.load(open(self.__initFile))
        if not self.__initData:
            print('Bootstra error, could not load init file >>> {file}'.format(file = self.__initFile))
            sys.exit(-1)

        # ensure all required key found in int file
        if not all (key in self.__initData.keys() for key in self.__initKeys):
            print("Missing required keys in init file, terminating !!!")
            sys.exit(-1)

        # Set all environment variable if not set
        if not all (self.util.isEnvSet(env) for env in self.__envDetails.keys()):
            print("app environment key is not set, building ...")
            #print("Setting environment...........................".ljust(self.lPad),end='')
            # app home
            self.setInitEnv()

        # validating required app directories/files >>> allAppDir = [dir[key] for index, key in enumerate(self.__initData)]
        #print(self.__initData)
        self.validateAppDir(self.__initData['VALIDATE_DIR_LIST'])
        self.validateAppFile(self.__initData['VALIDATE_FILE_LIST'])
        self.validateLogDir(self.__initData['APP_LOG']) # validating app log, create if we are missing

        self.appCfgLoc = self.__initData['APP_CONFIG']
        self.appLogLoc = self.__initData['APP_LOG']

    def setInitEnv(self):
        # set all missing env key
        for envKey in self.__envDetails:
            if not self.util.isEnvSet(envKey):
                self.util.setEnv(envKey,self.__envDetails[envKey])
            #fi
        #end for

    def validateAppDir(self, dirList):
        '''
        Description: Validate all system dir stored in init.json, if dir name not found will exit
        '''

        isMissing = False
        missingAppDir = list()
        for dirKeyVal in (dirList):
            # we are expecting dict stored in list, will check the 1st key from dict to get the dir key name 
            if not self.util.isDirExist(dirKeyVal[next(iter(dirKeyVal))]):
                missingAppDir.append(dirKeyVal)
                if not isMissing: isMissing = True 

        if isMissing:
            print('system dir is missing >>> {missing}'.format(missing = str(missingAppDir)))
            sys.exit(-1)

    def validateAppFile(self, fileList):
        '''
        Description: Validate all system file stored in init.json, if file name not found will exit
        '''

        isMissing = False
        missingAppFile = list()        
        for fileKeyVal in fileList:
            # we are expecting dict stored in list, will check the 1st key from dict to get the dir key name
            #print(fileKeyVal)
            if not self.util.isFileExist(fileKeyVal[next(iter(fileKeyVal))]):
                missingAppFile.append(fileKeyVal)
                if not isMissing: isMissing = True 

        if isMissing:
            print('system file(s) is missing >>> {missing}'.format(missing = str(missingAppFile)))
            sys.exit(-1)

    def validateLogDir(self, logDir):
        #if not os.path.isdir(os.environ[self.__logLocKey]):
        if not self.util.isDirExist(logDir):
            print('creating app log dir >>> {log}'.format(log = logDir))
            self.util.makeDir(self.util.getEnv(self.logDir))

    '''
    def getEnv(self,argEnvName):
        return os.getenv(argEnvName)

    def setEnv(self,argEnvName, argEnvVal):
        try:
            os.environ[argEnvName] = argEnvVal
        except Exception as err:
            sys.exit(-1)
    def isEnvSet(self,argEnvName):
        if self.util.getEnv(argEnvName):
            return True
        else:
            return False
        #fi
    '''

    #def getConfigLocation(self):
    #    #print(os.getenv(''.join([self.util.getEnv('APP_NAME'),'_CONFIG'])))
    #    return self.util.getEnv('APP_CONFIG')

    #def getLogLocation(self):
    #    return self.util.getEnv(self.Global.LogLocKey)

    #end getLogLocation
    '''
예제 #11
0
 def __init__(self):
     self.util = Utility()
예제 #12
0
class Interface(object, metaclass=Singleton):
    def __init__(self):
        self.utility = Utility()
        self.globals = Global()
        self.infra = RestInfra()
        self.logger = self.infra.Logger

        self.repDB = MysqlUtil(self.logger)
        self.repConn = self.repDB.getRepDBConnection()

    def getScanOverview(self, request):
        try:
            #we are expecting security key as request

            # Average score
            dbResult = self.repDB.execSelectSql(\
             Conn = self.repConn, SqlText = self.globals.avgScoreSql, SqlArgs=None, SqlOutput = self.globals.SqlOutput['Dict'])

            if dbResult['Status'] != self.globals.Success:
                return dbResult

            myAvgScore = dbResult['Data']

            # All location average score
            dbResult = self.repDB.execSelectSql(\
             Conn = self.repConn, SqlText = self.globals.allLocAvgScoreSql, SqlArgs = None, SqlOutput = self.globals.SqlOutput['Dict'])

            if dbResult['Status'] != self.globals.Success:
                return dbResult

            myAllLocAvgScore = dbResult['Data']

            # All host average score
            dbResult = self.repDB.execSelectSql(\
             Conn = self.repConn, SqlText = self.globals.allHostScoreSql, SqlArgs = None, SqlOutput = self.globals.SqlOutput['Dict'])

            if dbResult['Status'] != self.globals.Success:
                return dbResult

            myAllHostAvgScore = dbResult['Data']

            # All host tenant score
            dbResult = self.repDB.execSelectSql(\
             Conn = self.repConn, SqlText = self.globals.allHostTenantScoreSql, SqlArgs = None, SqlOutput = self.globals.SqlOutput['Dict'])

            if dbResult['Status'] != self.globals.Success:
                return dbResult

            myAllHostTenantScore = dbResult[
                'Data']  # we need to add this in formating

            # All tenant average score
            dbResult = self.repDB.execSelectSql(\
             Conn = self.repConn, SqlText = self.globals.allTenantAvgScoreSql, SqlArgs = None, SqlOutput = self.globals.SqlOutput['Dict'])

            if dbResult['Status'] != self.globals.Success:
                return dbResult

            myAllTenantAvgScore = dbResult['Data']

            # All location tenant average score
            dbResult = self.repDB.execSelectSql(\
             Conn = self.repConn, SqlText = self.globals.allLocTenantAvgScoreSql, SqlArgs = None, SqlOutput = self.globals.SqlOutput['Dict'])

            if dbResult['Status'] != self.globals.Success:
                return dbResult

            myAllLocTenantAvgScore = dbResult['Data']

            # Building response
            myResponse = self.utility.buildSummScanResponse(
                myAvgScore[0], myAllLocAvgScore, myAllHostAvgScore,
                myAllTenantAvgScore, myAllLocTenantAvgScore)

            return myResponse
        except Exception as e:
            raise e

    def getAllHost(self, request):
        '''
		dc_info = 'data center'
		os = 'RHEL/ms/oel'
		os_ver = 'version'
		location = 'location'
		'''
        try:
            myReqdArgs = []
            self.utility.validateArgs(request)

        except Exception as error:
            raise error

    def getAllTenants(self, request):
        try:
            pass
        except Exception as error:
            raise e

    def getTenantScan(self, **kwargs):
        pass

    def getScanScore(self, **kwargs):
        pass

    def getTenantConfig(self, **kwargs):
        pass

    def getHostConfig(self, **kwargs):
        pass

    def addCtrlException(self, **kwargs):
        pass

    def addCtrlExclusion(self, **kwargs):
        pass
예제 #13
0
class MysqlUtil(object, metaclass=Singleton):
    def __init__(self, logger=None):

        self.util = Utility()
        self.globals = Global()
        #self.env = Environment()
        self.dbResponse = self.util.getACopy(
            self.globals.Template['DBResponse'])
        self.infra = RestInfra()
        self.encryptKey = self.infra._Infra__bootStrapData['Main']['Key']

        if logger:
            self.logger = logger
        else:
            print('no logger is available, can not store logging information')
            self.logger = None
        # select @@port;
        # show variables where variable_name = 'port'
        # select user();
        # insert json in mysql
        #	insert into temp values(json_array(json_object('id',1001,'when','2015-01-01 10:00:00','score',35), json_object('id',1002,'when','2015-02-01 10:00:00','score',45)) );

    def makeConnection(self,
                       host,
                       port,
                       username,
                       encryptPass,
                       dbName,
                       tzone=None):
        try:
            #print('pass',self.util.decrypt(self.env.encryptPass))
            conn = connect(host=host,
                           port=port,
                           user=username,
                           password=self.util.decrypt(self.encryptKey,
                                                      encryptPass),
                           database=dbName)
            #print(conn)
            # we need to set the timezone as requested for this connection
            if tzone:
                conn.time_zone = '+00:00'
            return conn
        except errors.Error as error:
            #return self.util.buildDBResponse(self.globals.Error, None, None, error)
            raise
        except Exception as error:
            raise
            #return self.util.buildDBResponse(self.globals.Error, None, None, error)

    def getRepDBConnection(self):
        try:
            #print('pass',self.util.decrypt(self.env.encryptPass))
            conn = self.makeConnection(\
             self.infra.dbConfigData['host'], \
             self.infra.dbConfigData['port'], \
             self.infra.dbConfigData['user'], \
             self.infra.dbConfigData['password'], \
             self.infra.dbConfigData['database'])
            return conn

        except Exception as error:
            raise
            #return self.util.buildDBResponse(self.globals.Error, None, None, error)

    def execSelectSql(self, **args):
        '''
		Description: Execute Select statement
		Arguments: Key word arguments
					Conn : Connection handler
					SqlText : Sql to be executed
					SqlArgs : Arguments need to be passed to sql (optional), default = None (SqlArgs should be in dict)
					SqlOutput : Sqloutput Dict/Touple (optional), default = Touple
		Usage : execSelectSql(<connection>, <sql>, <sql args (optional)>, <sql output Dict/Touple (optional)> )
		Return : Array 

		internal --> #mySqlArgs = ','.join(list(map(lambda x: '%s', args)))

		'''
        try:
            myRequiredArgs = ['Conn', 'SqlText']

            #Validating arguments
            myArgValResult = self.util.valArguments(myRequiredArgs, args)
            if myArgValResult['Status'] == self.globals.UnSuccess:
                raise InvalidArguments(myArgValResult['Message'])

            #extracting arguments
            myDBConn = args['Conn']
            mySqlText = args['SqlText']

            if not mySqlText.upper().startswith(
                ('SELECT', 'INSERT', 'UPDATE', 'DELETE')):
                raise InvalidArguments(
                    'SQL statement can be SELECT/INSERT/UPDATE/DELETE only')

            mySqlArgs = args['SqlArgs'] if 'SqlArgs' in args else None
            mySqlOutput = args[
                'SqlOutput'] if 'SqlOutput' in args else self.globals.SqlOutput[
                    'Default']

            if mySqlOutput not in self.globals.SqlOutput['All']:
                mySqlOutput = self.globals.SqlOutput['Default']

            # executing sql
            print('Executing sql >>>', mySqlText, mySqlArgs)
            myDBcur = myDBConn.cursor(buffered=True)
            totalRows = myDBcur.execute(mySqlText, mySqlArgs)

            if mySqlText.upper().startswith('SELECT'):
                if myDBcur.with_rows:  # if we got any data
                    myTotalRows = myDBcur.rowcount
                    #myCurDesc = myDBcur.description
                    myAllColumns = myDBcur.column_names
                    myRawData = myDBcur.fetchall()

                    # build the sqloutput
                    if mySqlOutput == self.globals.SqlOutput['Dict']:
                        #myAllColumns = [column[0] for column in myCurDesc]
                        myData = [
                            dict(zip(myAllColumns, row)) for row in myRawData
                        ]
                    else:
                        myData = myRawData
                else:
                    myData = None
            elif mySqlText.upper().startswith(('INSERT', 'UPDATE', 'DELETE')):
                pass

            myDBcur.close()
            return self.util.buildDBResponse(self.globals.Success, myData,
                                             myTotalRows)

        except errors.Error as error:
            #return self.util.buildDBResponse(self.globals.Error, None, None, error)
            raise
        except Exception as error:
            self.util.logError()
            raise
            #return self.util.buildDBResponse(self.globals.Error, None, None, error)

    def commitTrans(self, dbConn):
        try:
            dbConn.commit()
        except errors.Error as error:
            return self.util.buildDBResponse(self.globals.Error, None, None,
                                             error)
        except Exception as error:
            return self.util.buildDBResponse(self.globals.Error, None, None,
                                             error)

    def rollbackTrans(self, dbConn):
        try:
            dbConn.rollback()
        except errors.Error as error:
            return self.util.buildDBResponse(self.globals.Error, None, None,
                                             error)
        except Exception as error:
            return self.util.buildDBResponse(self.globals.Error, None, None,
                                             error)

    def buildMarkerForSql(self, sqlText, argList, marker):
        sqlArgMarker = ', '.join(list(map(lambda x: marker, args)))
        return sqlText % sqlArgMarker

    def buildDynaSql(self, colList, tableList, criteria):
        if colList:
            mySql = colList

            # adding tables to sql
            for indx, table in enumerate(tableList):
                if indx > 0:
                    # found more than one table, we need to add ',' in from clause to seperate tables
                    mySql = ''.join([mySql, ' , ', table])
                else:
                    mySql = ''.join([mySql, ' from ', table])

            # adding where clause
            if criteria:
                mySql = ''.join([mySql, ' where ', criteria])

            return mySql
예제 #14
0
class InterfaceUtil(object, metaclass=Singleton):
    def __init__(self):
        self.utility = Utility()
        self.globals = Global()
        self.infra = RestInfra()
        self.logger = self.infra.Logger

        self.repDB = MysqlUtil(self.logger)
        self.repConn = self.repDB.getRepDBConnection()

    def getAvgScore(self):
        '''
		Description: Returns overall average CIS scan score
		Return: <average_score>
		'''
        try:
            # Average score
            dbResult = self.repDB.execSelectSql(\
             Conn = self.repConn, SqlText = self.globals.avgScoreSql, SqlArgs=None, SqlOutput = self.globals.SqlOutput['Dict'])

            myData = float()

            if dbResult['Status'] == self.globals.Success:
                myData = round(dbResult['Data'][0]['AVG_SCORE'], 2)
                myResponse = self.utility.buildResponse(
                    self.globals.Success, self.globals.Success, myData)
            else:
                myResponse = self.utility.buildResponse(
                    self.globals.UnSuccess, dbResult['Data'])

            return myResponse

        except Exception as error:
            raise

    def getAvgLocScore(self):
        '''
		Description: Returns average CIS scan score for each location
		Return: [{"<LOCATION>" : <average_score>}]
		'''
        try:
            # Average location score
            dbResult = self.repDB.execSelectSql(\
             Conn = self.repConn, SqlText = self.globals.locAvgScoreSql, SqlArgs = None, SqlOutput = self.globals.SqlOutput['Dict'])
            myData = []

            if dbResult['Status'] == self.globals.Success:
                for loc in dbResult['Data']:
                    myData.append(
                        {loc['LOCATION']: round(loc['AVG_SCORE'], 2)})
                myResponse = self.utility.buildResponse(
                    self.globals.Success, self.globals.Success, myData)
            else:
                myResponse = self.utility.buildResponse(
                    self.globals.UnSuccess, dbResult['Data'])

            return myResponse

        except Exception as error:
            raise

    def getAvgHostScore(self):
        '''
		Description: Returns average CIS scan score for all host
		Return: [{"<HOST>" : <average_score>, "LOCATION" : <location>}]
		'''
        try:
            # Average location score
            dbResult = self.repDB.execSelectSql(\
             Conn = self.repConn, SqlText = self.globals.hostScoreSql, SqlArgs = None, SqlOutput = self.globals.SqlOutput['Dict'])

            myData = list()

            if dbResult['Status'] == self.globals.Success:
                for host in dbResult['Data']:
                    myData.append({
                        host['HOST']: round(host['AVG_SCORE'], 2),
                        "Location": host['LOCATION'],
                        "LastScan": host["LAST_SCAN"],
                        "LastScanTime": host["LAST_SCAN_TIME"]
                    })

                myResponse = self.utility.buildResponse(
                    self.globals.Success, self.globals.Success, myData)
            else:
                myResponse = self.utility.buildResponse(
                    self.globals.UnSuccess, dbResult['Data'])

            return myResponse

        except Exception as error:
            raise

    def getAvgVendorScore(self):
        '''
		Description: Returns average CIS scan score for all Venodr and its product
		Return: [{"<vendor>" : {<product> : <score>} }]
		'''
        try:
            # Average location score
            dbResult = self.repDB.execSelectSql(\
             Conn = self.repConn, SqlText = self.globals.vendorProdAvgScoreSql, SqlArgs = None, SqlOutput = self.globals.SqlOutput['Dict'])

            myData = list()

            if dbResult['Status'] == self.globals.Success:
                for vendorProdList in dbResult['Data']:
                    #checking if vendor exists
                    vendor = [
                        indx for indx, val in enumerate(myData)
                        if vendorProdList['VENDOR'] in val.keys()
                    ]
                    if not vendor:
                        # vendor doesnt exist, adding new vendor and product
                        myData.append({
                            vendorProdList['VENDOR']: [{
                                vendorProdList['PRODUCT']:
                                round(vendorProdList['AVG_SCORE'], 2)
                            }]
                        })
                    else:
                        # vendor exist, adding product
                        myData[vendor[0]][vendorProdList['VENDOR']].append({
                            vendorProdList['PRODUCT']:
                            round(vendorProdList['AVG_SCORE'], 2)
                        })

                myResponse = self.utility.buildResponse(
                    self.globals.Success, self.globals.Success, myData)
            else:
                myResponse = self.utility.buildResponse(
                    self.globals.UnSuccess, dbResult['Data'])

            return myResponse

        except Exception as error:
            raise

    def getAvgLocVendorScore(self):
        '''
		Description: Returns average CIS scan score for all Venodr and its product
		Return: [{"<location>" : {<vendor> : {<product> : <score> }}}]
		'''
        try:
            # Average location score
            dbResult = self.repDB.execSelectSql(\
             Conn = self.repConn, SqlText = self.globals.locVendorAvgScoreSql, SqlArgs = None, SqlOutput = self.globals.SqlOutput['Dict'])

            myData = list()

            if dbResult['Status'] == self.globals.Success:
                for locVendor in dbResult['Data']:
                    #checking if Location exisit
                    location = [
                        indx for indx, val in enumerate(myData)
                        if locVendor['LOCATION'] in val.keys()
                    ]

                    if not location:
                        # Location does not exist, will add new record
                        #print('location not found', locVendor)
                        myData.append({
                            locVendor['LOCATION']: [{
                                locVendor['VENDOR']: [{
                                    locVendor['PRODUCT']:
                                    round(locVendor['AVG_SCORE'], 2)
                                }]
                            }]
                        })
                        #print('location not found', myData)
                    else:
                        location = location[0]
                        #print('location found',locVendor['LOCATION'])
                        # we found location, will check if vendor exists
                        #print('location:',locVendor['LOCATION'])
                        #print('mydata:',myData)
                        #print('location indx:',location)
                        #print('mydata loc:',myData[location][locVendor['LOCATION']])
                        #print('before finding vendor',myData[location][locVendor['LOCATION']])
                        vendor = [
                            indx for indx, val in enumerate(myData[location][
                                locVendor['LOCATION']])
                            if locVendor['VENDOR'] in val.keys()
                        ]

                        if not vendor:
                            #vendor not found for this location, will add new vendor
                            #print('vendor not found',myData[location][locVendor['LOCATION']])
                            myData[location][locVendor['LOCATION']].append({
                                locVendor['VENDOR']: [{
                                    locVendor['PRODUCT']:
                                    round(locVendor['AVG_SCORE'])
                                }]
                            })
                        else:
                            vendor = vendor[0]
                            #print('vendor found',location,locVendor['LOCATION'],vendor,myData)
                            # vendor for this location found, add product to this vendor
                            myData[location][locVendor['LOCATION']][vendor][
                                locVendor['VENDOR']].append({
                                    locVendor['PRODUCT']:
                                    round(locVendor['AVG_SCORE'])
                                })
                myResponse = self.utility.buildResponse(
                    self.globals.Success, self.globals.Success, myData)
            else:
                myResponse = self.utility.buildResponse(
                    self.globals.UnSuccess, dbResult['Data'])

            return myResponse

        except Exception as error:
            raise

    def getTenantScan(self, TenantId, ScanId='Latest'):
        '''
		Description: Get latest tenant scan for a tenant id
		Args: 
			TenantId : TenantId
			ScanId : optional (default is 'Latest')
		'''
        try:
            if scanId == self.globals.latest:
                latestScan = self.getLatestTenantScan(TenantId)
                if dbResult['Status'] == self.globals.Success:
                    if dbResult['Data']:
                        myScanId = dbResult['Data']['SCAN_ID']
                        myScanSeqId = dbResult['Data']['SEQ_ID']
                else:
                    raise ValueError('')
                myScanId = dbResult['Data']['ScanId']
            else:
                myScanId = scanId

                dbResult = self.repDB.execSelectSql(
                    self.repConn, self.globals.getTenantScanSummarySql)
        except Exception as e:
            raise e

    def getLastTenantScan(self, TenantId):
        '''
		Description: Get latest scan id for a tenant
		Args: 
			TenantId : TenantId
			ScanId : optional (default is 'Latest')
		Returns: last scan id and scan seq id
		'''
        try:
            mySqlArgs = {'TenantId': TenantId}
            dbResult = self.repDB.execSelectSql(\
             Conn = self.repConn, SqlText = self.globals.getLatestTenantScansSql, SqlArgs = TenantId, SqlOutput = self.globals.SqlOutput['Dict'])
            if dbResult['Status'] == self.globals.Success:
                # if we got data, we need to extract dict from tuple
                if dbResult['Data']:
                    dbResult['Data'] = dbResult['Data'][0]

            return dbResult

        except Exception as e:
            raise e

    '''
예제 #15
0
from flask import Flask, flash, session, redirect, url_for, escape, request, jsonify, json
from datetime import timedelta
from com.port8.core.singleton import Singleton
from com.port8.bpm.factory import Factory
from com.port8.core.utility import Utility
from com.port8.core.infrastructure import RestInfra
from flask_cors import CORS

import logging

#'\x95\x8d\xe3\xab\x18\xc2\xc6\xeb\xd4+\x11H<\xdc\xd8m\xaf\xae0\xcfb\xdc\x84\x92\xc5\xb2\xado\x98\xc5\x08\xa9\xbb/\x95\xe9/\xda\x10\xaa\x1f\xb7k\x956SLCIj\r7v\xdbm\\\x1e\xdc\xf3M&$\xb0\xce\xdb\x18\xd6\xa3\x13\x85\xd0m\r\x1a]\xbe\xf8\xd8Q\xcf\xed\xaf\x0b\x827TB\xb7'
myLogger = logging.getLogger('uConnect')
util = Utility()
factory = Factory()
rest = RestInfra()

app = Flask(__name__)
CORS(app)
app.secret_key = '\x95\x8d\xe3\xab\x18\xc2\xc6\xeb\xd4+\x11H<\xdc\xd8m\xaf\xae0\xcfb\xdc\x84\x92\xc5\xb2\xado\x98\xc5\x08\xa9\xbb/\x95\xe9/\xda\x10\xaa\x1f\xb7k\x956SLCIj\r7v\xdbm\\\x1e\xdc\xf3M&$\xb0\xce\xdb\x18\xd6\xa3\x13\x85\xd0m\r\x1a]\xbe\xf8\xd8Q\xcf\xed\xaf\x0b\x827TB\xb7'
#session = {}


@app.before_request
def make_session_permanent():
    myModuleLogger = logging.getLogger('uConnect.' + str(__name__))
    myModuleLogger.debug("initializing session information ...")
    session.permanent = True
    app.permanent_session_lifetime = timedelta(minutes=5)


@app.route('/port8', methods=['GET', 'POST'])
예제 #16
0
파일: factory.py 프로젝트: myuconnect/Port8
 def __init__(self):
     #self.util = Utility.Instance()
     self.globals = Global()
     self.util = Utility()
     self.infra = RestInfra()
     self.logger = self.infra.logger
예제 #17
0
파일: factory.py 프로젝트: myuconnect/Port8
class Factory(object, metaclass=Singleton):
    '''
    This is Factory class, this will execute a BO process as mapped in config/FactoryMetadata.json
    '''
    def __init__(self):
        #self.util = Utility.Instance()
        self.globals = Global()
        self.util = Utility()
        self.infra = RestInfra()
        self.logger = self.infra.logger
        #self.myClass = self.__class__.__name__

    def processRequest(self, argRequestDict):
        ''' 
            Description:    Update key in dictDocument for a given collection, this is a private method
            argCollection:  Collection name
            argDictDocument:Dict documents
            usage:          <processRequest(argRequestDict)
        '''
        try:

            self.logger.debug(
                "arg received [{args}]".format(args=argRequestDict))
            myMainArgData = self.util.getACopy(argRequestDict)
            ''' Validating argumemt received '''
            isValidRequest = self.util.isValidRestRequest(myMainArgData)

            if not (isValidRequest):
                raise com.port8.core.error.InvalidArguments(
                    "Arg validation error {arg}".format(arg=myMainArgData))
            else:
                self.logger.debug(
                    'validated request, its a valid request >>> {req}'.format(
                        req=str(argRequestDict)))
            #fi

            myPageId = myMainArgData['Page']
            myActionId = myMainArgData['Action']
            myArguments = myMainArgData['Arguments']

            myResult = self.__findBPSProcess(myPageId, myActionId)
            if myResult['Status'] == self.globals.Success:
                myBPMProcess = myResult['Data']
            else:
                # we did not find BPM process, returing result
                return myResult

            # extracting tuple value returned from above method

            #myLibrary, myClass, myMethod = bpsProcessVal
            if myBPMProcess:
                self.logger.debug("found, bpm process [{bpmproc}]".format(
                    bpmproc=myBPMProcess))
                myResponse = self.__executeBPSPRocess(myBPMProcess['lib'],
                                                      myBPMProcess['cls'],
                                                      myBPMProcess['method'],
                                                      myArguments)
                #myRquestStatus = self.util.getRequestStatus(self.globals.Success)
            else:
                self.logger.debug("did not find mapped bpm process")
                myRquestStatus = self.util.getRequestStatus(\
                    self.globals.UnSuccess,'Invalid Page [{page}] Action [{action}]'.format(page=myPageId, action=myActionId))
                myResponse = self.util.buildResponseData(
                    myRquestStatus, 'Error')

            self.logger.debug(
                'myResponse data >>> {response}'.format(response=myResponse))
            return myResponse

        except Exception as err:
            myRequestStatus = self.util.logError()
            myResponse = self.util.buildResponseData(myRequestStatus, 'Error')
            return myResponse

    def __findBPSProcess(self, argPageId, argActionId):
        ''' 
            Description:This is private methd called internally in Factry class; Find BMP method call for a given request (pageid, actionid) 
            Arguments:  pageid and action id (pageid, actionid)
            Usage:      <__findBPSProcess(<pageid>,<actionid>)>
            Return:     BPM_CALL_JSON dict {'cls': <class>, lib : <library>, method : <mehtod>, args : <arguments needed>} 
        '''
        try:

            self.logger.debug("arg received [{page},{action}]".format(
                page=argPageId, action=argActionId))

            page = [
                page for page in self.infra.factoryMetadata
                if page['PAGE_ID'] == argPageId
                and page['PAGE_STATUS'] == 'ACTIVE'
            ]
            # if we found multiple page, will log the details
            if len(page) > 1:
                self.logger.ERROR(
                    'Expecting 1 entry per page, found {found}, Factory metadata >> {factoryData}'
                    .format(found=len(page),
                            factoryData=self.infra.factoryData))

                myResponse = self.util.buildResponse(\
                    self.globals.UnSuccess,
                    'Metadata is corrupted (found [{cnt}] occurrance of page [{page}]'\
                        .format(cnt = len(page), page=argPageId))
                return myResponse

            if len(page) == 0:
                # we did not find page
                self.logger.debug(
                    'did not find requested page >>> {page}'.format(
                        page=argPageId))

                myResponse = self.util.buildResponse(\
                    self.globals.UnSuccess,
                    'Missing requested page in metadata >>> {page}'.format(page=argPageId))
                return myResponse

            # we found one page, get the 1st item from list
            self.logger.debug(
                'found matching page [{page}] in factory metdata'.format(
                    page=argPageId))
            page = page[0]
            pageActions = page['ACTIONS']

            if pageActions:
                actionIndx = [
                    indx for indx, val in enumerate(pageActions)
                    if pageActions[indx]['ACTION_ID'] == argActionId
                ]
                #print("found ??? >>>",actionIndx, pageActions)
                if actionIndx:
                    self.logger.debug(
                        'found matching action [{action}] on page [{page}] in factory metdata'
                        .format(action=argActionId, page=argPageId))
                    if isinstance(actionIndx, list):
                        actionIndx = actionIndx[0]
                    #myBPMCallJson = self.infra.factoryMetadata[argPageId][actionIndx][argActionId]
                    #print("found action >>>",actionIndx, pageActions, pageActions[actionIndx] )
                    myBPMCallJson = pageActions[actionIndx]['BPM_CALL_JSON']
                    self.logger.debug('BPM Json call found >>> {bpm}'.format(
                        bpm=str(myBPMCallJson)))
                    myResponse = self.util.buildResponse(\
                        self.globals.Success, self.globals.Success,myBPMCallJson)
                    return myResponse
                    #return myBPMCallJson
                else:
                    self.logger.debug(
                        'action [{action}] not found for page [{page}] in factory metdata'
                        .format(action=argActionId, page=argPageId))
                    myResponse = self.util.buildResponse(\
                        self.globals.UnSuccess,
                        'Missing requested action [{action}] for page >>> {page}'.format(action = argActionId, page=argPageId))
                    return myResponse
            else:
                myResponse = self.util.buildResponse(\
                    self.globals.UnSuccess,
                    'Empty actions for page in metadata >>> {page}'.format(page=argPageId))
                return myResponse

        except Exception as err:
            #myRequestStatus = self.util.extractLogError()
            raise

    def __executeBPSPRocess(self, argLib, argCls, argMethod, arguments):
        ''' 
            Description:Private method, internally called in Factory method, execute Factory bpm process
            Arguments:  lib, class, method, arguments
            usage:      <__executeBPSPRocess(<lib>,<cls>,<method>,<arguments>) >
            Return:     Return results in array
        '''
        try:

            self.logger.debug(
                "arg received [{lib},{cls},{method},{args}]".format(
                    lib=argLib, cls=argCls, method=argMethod, args=arguments))

            myModule = importlib.import_module(argLib)
            myClass = getattr(myModule, argCls)
            # if singleton, get an instance else instantiate the class
            if hasattr(myModule, 'Singleton') and hasattr(myClass, 'Instance'):
                myCallableClass = myClass.Instance()
            else:
                myCallableClass = myClass()

            # get the method from this class
            myMethod = getattr(myCallableClass, argMethod)
            ''' Only MainArg need to be passed  '''
            #myMainArg = {'MainArg':self.util.extMainArgFromReq(argReqJsonDict)}
            ''' need to mark that this response is external '''
            #myMainArg['MainArg'].update({'ResponseMode':self.globalInstance._Global__ExternalRequest})

            # execute the method
            if arguments:
                self.logger.info(
                    "executing method with arguments >>>[{lib}.{cls}.{method} {args}]"
                    .format(lib=argLib,
                            cls=argCls,
                            method=argMethod,
                            args=arguments))
                myResults = myMethod(arguments)
            else:
                self.logger.info(
                    "executing method w/o arguments >>>[{lib}.{cls}.{method}]".
                    format(lib=argLib, cls=argCls, method=argMethod))
                myResults = myMethod()

            self.logger.debug('results after execution >>> {data}'.format(
                data=str(myResults)))
            return (myResults)

        except Exception as err:
            #myRequestStatus = self.util.extractLogError()
            #myResponse = self.util.buildResponseData(myMainArgData['ResponseMode'], myRequestStatus, 'Error')
            raise
예제 #18
0
class Interface(object, metaclass=Singleton):
	def __init__(self):
		self.utility = Utility()
		self.globals = Global()
		self.infra = RestInfra()
		self.logger = self.infra.Logger
			
		self.ui_util = InterfaceUtil()
		self.security = Security()

	def getLandingPageData(self):
		'''
		Description: Returns data for landing page, this method should be called after credential authentication
		Arguments: Security key
		Returns: Response ...
			{
				"Status" : "", 
				"Message" : "",
				"Data" : 
					[
						"AvgScore" : 75.00,
						"LocAvgScore" : [],
						"LocVendorScore" : [],
						"LocHostTenant" : [],
						"LocHostTenant" : []
					]
			}
		'''
		try:
			# initialize
			myLandingPageData = self.utility.getACopy(self.globals.Template['LandingPage'])

			# Get all the data
			#Avg overall Score
			dbResult = self.ui_util._InterfaceUtil__getAvgScore()
			if dbResult['Status'] != self.globals.Success:
				# build error response and return
				return
			else:
				avgScore = dbResult['Data']

			#Avg loc score
			dbResult = self.ui_util._InterfaceUtil__getAvgLocScore()
			if dbResult['Status'] != self.globals.Success:
				# build error response and return
				return
			else:
				avgLocScore = dbResult['Data']

			#Avg Vendor score
			dbResult = self.ui_util._InterfaceUtil__getAvgVendorScore()
			if dbResult['Status'] != self.globals.Success:
				return
			else:
				avgVendorScore = dbResult['Data']

			#Avg Loc Vendor score
			dbResult = self.ui_util._InterfaceUtil__getAvgLocVendorScore()
			if dbResult['Status'] != self.globals.Success:
				return
			else:
				avgLocVendorScore = dbResult['Data']

 			#Avg Host score
			dbResult = self.ui_util._InterfaceUtil__getAvgHostScore()
			if dbResult['Status'] != self.globals.Success:
				# build error response and return
				return
			else:
				avgHostScore = dbResult['Data']

 			#Host tenant score
			dbResult = self.ui_util._InterfaceUtil__getAllTenantScore()

			if dbResult['Status'] != self.globals.Success:
				# build error response and return
				return
			else:
				hostTenantScore = dbResult['Data']

			# Rearrange the data
			myLandingPageData['AvgScore'] = avgScore
			myLandingPageData['Location'] = avgLocScore
			myLandingPageData['Vendor'] = avgVendorScore
			#print('Location >>>', avgLocScore)
			#print('Vendor >>>', avgVendorScore)
			myLandingPageData['Vendor'] = avgVendorScore
			myLandingPageData['LocVendor'] = avgLocVendorScore 
			myLandingPageData['LocHost'] = avgHostScore
			myLandingPageData['HostTenant'] = hostTenantScore

			#print('LocVendor >>>',avgLocVendorScore)
			#print('Host >>>',avgHostScore)
			return myLandingPageData
		except Exception as error:
			raise error

	def getOverallAvgScore(self, args):
		try:
			#validating security credential
			myScore = self.ui_util._InterfaceUtil__getAvgScore(args)
			return myScore

		except Exception as error:
			raise error

	def getLocAvgScore(self, args):
		try:
			#validating security credential
			dbResult = self.ui_util._InterfaceUtil__getAvgLocScore(args)
			return dbResult

		except Exception as error:
			raise error

	def getVendorAvgScore(self, args):
		try:
			#validating security credential
			#Avg Vendor score
			dbResult = self.ui_util._InterfaceUtil__getAvgVendorScore(args)
			return dbResult

		except Exception as error:
			raise error

	def getLocVendorAvgScore(self, args):
		try:
			#validating security credential
			#Avg Loc Vendor score
			dbResult = self.ui_util._InterfaceUtil__getAvgLocVendorScore(args)
			return dbResult

		except Exception as error:
			raise error

	def getHostInfo(self, args):
		try:
			#validating security credential
			#Avg Host score
			dbResult = self.ui_util._InterfaceUtil__getAvgHostInfo(args)
			return dbResult

		except Exception as error:
			raise error

	def getHostAvgScore(self, args):
		try:
			#validating security credential
			#Avg Host score
			dbResult = self.ui_util._InterfaceUtil__getAvgHostScore(args)
			return dbResult

		except Exception as error:
			raise error

	def getHostInfo(self, args):
		try:
			#validating security credential
			#get host information
			dbResult = self.ui_util._InterfaceUtil__getHostInfo(args)
			return dbResult

		except Exception as error:
			raise error

	def getScanOverview(self, request):
		try:
			#we are expecting security key as request

			# Average score
			dbResult = self.repDB.execSelectSql(\
				Conn = self.repConn, SqlText = self.globals.avgScoreSql, SqlArgs=None, SqlOutput = self.globals.SqlOutput['Dict'])

			if dbResult['Status'] != self.globals.Success:
				return dbResult

			myAvgScore = dbResult['Data']

			# All location average score
			dbResult = self.repDB.execSelectSql(\
				Conn = self.repConn, SqlText = self.globals.allLocAvgScoreSql, SqlArgs = None, SqlOutput = self.globals.SqlOutput['Dict'])
			
			if dbResult['Status'] != self.globals.Success:
				return dbResult

			myAllLocAvgScore = dbResult['Data']

			# All host average score
			dbResult = self.repDB.execSelectSql(\
				Conn = self.repConn, SqlText = self.globals.allHostScoreSql, SqlArgs = None, SqlOutput = self.globals.SqlOutput['Dict'])

			if dbResult['Status'] != self.globals.Success:
				return dbResult

			myAllHostAvgScore = dbResult['Data']

			# All host tenant score
			dbResult = self.repDB.execSelectSql(\
				Conn = self.repConn, SqlText = self.globals.allHostTenantScoreSql, SqlArgs = None, SqlOutput = self.globals.SqlOutput['Dict'])

			if dbResult['Status'] != self.globals.Success:
				return dbResult

			myAllHostTenantScore = dbResult['Data'] # we need to add this in formating

			# All tenant average score
			dbResult = self.repDB.execSelectSql(\
				Conn = self.repConn, SqlText = self.globals.allTenantAvgScoreSql, SqlArgs = None, SqlOutput = self.globals.SqlOutput['Dict'])

			if dbResult['Status'] != self.globals.Success:
				return dbResult

			myAllTenantAvgScore = dbResult['Data']

			# All location tenant average score
			dbResult = self.repDB.execSelectSql(\
				Conn = self.repConn, SqlText = self.globals.allLocTenantAvgScoreSql, SqlArgs = None, SqlOutput = self.globals.SqlOutput['Dict'])

			if dbResult['Status'] != self.globals.Success:
				return dbResult

			myAllLocTenantAvgScore = dbResult['Data']

			# Building response
			myResponse = self.utility.buildSummScanResponse(myAvgScore[0], myAllLocAvgScore, myAllHostAvgScore, myAllTenantAvgScore, myAllLocTenantAvgScore)

			return myResponse
		except Exception as e:
			raise e

	def getAllHost(self, request):
		'''
		dc_info = 'data center'
		os = 'RHEL/ms/oel'
		os_ver = 'version'
		location = 'location'
		'''
		try:
			myReqdArgs = []
			self.utility.validateArgs(request)

		except Exception as error:
			raise error

	def getAllTenants(self, request):
		try:
			pass
		except Exception as error:
			raise e

	def getTenantScan(self, **kwargs):
		pass

	def getScanScore(self, **kwargs):
		pass

	def getTenantConfig(self, **kwargs):
		pass

	def getHostConfig(self, **kwargs):
		pass

	def addCtrlException(self, **kwargs):
		pass

	def addCtrlExclusion(self, **kwargs):
		pass
예제 #19
0
class InterfaceUtil(object, metaclass=Singleton):
    def __init__(self):
        self.utility = Utility()
        self.globals = Global()
        self.infra = RestInfra()
        self.logger = self.infra.logger

        self.mySqlUtil = MysqlUtil(self.logger)
        self.repConn = self.mySqlUtil.getRepDBConnection()

    def __getAvgScore(self, args):
        '''
		Description: Returns overall average CIS scan score
		Return: <average_score>
		'''
        try:
            # initializing
            myMandatoryArgs = ["SecurityToken"]

            # Validaring arguments
            myValResult = self.utility.valRequiredArg(args, myMandatoryArgs)

            if myValResult[0] == self.globals.UnSuccess:
                myResponse = self.utility.buildResponse(
                    self.globals.UnSuccess, myValResult[2])
                return myResponse

            # Average score
            dbResult = self.mySqlUtil.execSelectSql(\
             Conn = self.repConn, SqlText = self.globals.avgScoreSql, SqlArgs=None, SqlOutput = self.globals.SqlOutput['Dict'])

            myScore = float()

            if dbResult['Status'] == self.globals.Success:
                myScore = round(dbResult['Data'][0]['AVG_SCORE'], 2)
                myData = [{'AvgScore': myScore}]
                myResponse = self.utility.buildResponse(
                    self.globals.Success, self.globals.Success, myData)
            else:
                myResponse = self.utility.buildResponse(
                    self.globals.UnSuccess, dbResult['Data'])

            return myResponse

        except Exception as error:
            raise

    def __getAvgLocScore(self, args):
        '''
		Description: Returns average CIS scan score for each location
		Return: [{"<LOCATION>" : <average_score>}]
		'''
        try:
            # initializing
            myMandatoryArgs = ["SecurityToken"]
            myOptionalArgs = ["Location"]

            # Validaring arguments
            myValResult = self.utility.valRequiredArg(args, myMandatoryArgs)

            if myValResult[0] == self.globals.UnSuccess:
                myResponse = self.utility.buildResponse(
                    self.globals.UnSuccess, myValResult[2])
                return myResponse
                #raise InvalidArguments(myValResult[2])

            if not ('Location' in args):
                dbResult = self.mySqlUtil.execSelectSql(\
                 Conn = self.repConn, SqlText = self.globals.getAllLocAvgScoreSql, SqlArgs = args, SqlOutput = self.globals.SqlOutput['Dict'])
            else:
                # will pass args
                dbResult = self.mySqlUtil.execSelectSql(\
                 Conn = self.repConn, SqlText = self.globals.getALocAvgScoreSql, SqlArgs = args, SqlOutput = self.globals.SqlOutput['Dict'])
            myData = []

            if dbResult['Status'] == self.globals.Success:
                for loc in dbResult['Data']:
                    myData.append({
                        'Location': loc['LOCATION'],
                        'AvgScore': round(loc['AVG_SCORE'], 2)
                    })

                myResponse = self.utility.buildResponse(
                    self.globals.Success, self.globals.Success, myData)
            else:
                myResponse = self.utility.buildResponse(
                    self.globals.UnSuccess, dbResult['Data'])

            return myResponse

        except Exception as error:
            raise

    def __getHostInfo(self, args):
        '''
		Description: Returns average CIS scan score for all host
		Arguments: Optional; HostName, HostId,OS, PhysicalMem, Location
			# OS = [], 0 --> OS name, 1--> os ver
			# PhysMemory [] --> 0 --> Operator, 1--> Value
			# Location

		Return: [{"<HOST>" : <average_score>, "LOCATION" : <location>}]
		'''
        try:
            # initializing
            myMandatoryArgs = ["SecurityToken"]
            myOptionalArgs = [
                "Location", "VendorId", "HostId", "HostName", "OS",
                "PhysMemory"
            ]
            myCriteria = myDynaSql = myDynaSqlWhereClause = myDynaSqlGroupByClause = myDynaSqlOrderByClause = ""

            # Validaring arguments
            myArguments = self.utility.removeEmptyKeyFromDict(args)
            myValResult = self.utility.valRequiredArg(myArguments,
                                                      myMandatoryArgs)
            print('arg recvd', myArguments)

            if myValResult[0] == self.globals.UnSuccess:
                myResponse = self.utility.buildResponse(
                    self.globals.UnSuccess, myValResult[2])
                return myResponse

            mySelectColList = self.globals.getHostDetailsCol
            mySelectTabList = self.globals.getHostDetailsFromClause

            if self.utility.isKeyInDict(myArguments,
                                        'HostId') or self.utility.isKeyInDict(
                                            myArguments, 'HostName'):
                #if 'HostName' or 'HostId' in myArguments:
                if 'HostName' in myArguments:
                    myCriteria = ''.join([' host_name = %(HostName)s'])

                elif 'HostId' in myArguments:
                    myCriteria = ''.join([' host_id = %(HostId)s'])
            else:
                print(
                    'Host arg is not found, looking for other arguments ..., table list >>>',
                    mySelectTabList)
                # building dynamic sql (adding table and column to main sql as argument passed)
                if 'VendorId' in myArguments:
                    if not self.utility.getValCntInList(
                            mySelectTabList, 'p$ht_tenant tenant'):
                        mySelectTabList.append('p$ht_tenant tenant')
                        mySelectColList = ''.join([
                            mySelectColList,
                            ', tenant.tenant_version TENANT_VERSION, \
							tenant.tenant_name TENANT_NAME, tenant.tenant_vendor TENANT_VENDOR, tenant.vendor_prod_name VENDOR_PRODUCT'
                        ])

                    myCriteria = ''.join(
                        [' tenant.tenant_vendor = %(VendorId)s'])

                # Vendor product criteria
                if 'Product' in myArguments:
                    if not self.utility.getValCntInList(
                            mySelectTabList, 'p$ht_tenant tenant'):
                        mySelectTabList.append('p$ht_tenant tenant')
                        mySelectColList = ''.join([
                            mySelectColList,
                            ', tenant.tenant_version TENANT_VERSION, \
							tenant.tenant_name TENANT_NAME, tenant.tenant_vendor TENANT_VENDOR, tenant.vendor_prod_name VENDOR_PRODUCT'
                        ])

                    if myCriteria:
                        myCriteria = ''.join([
                            myCriteria, ' and ',
                            ' tenant.vendor_prod_name = %(Product)s'
                        ])
                    else:
                        myCriteria = ''.join([
                            myCriteria,
                            ' tenant.vendor_prod_name = %(Product)s'
                        ])

                # Location/DC_INFO criteria
                if 'Location' in myArguments:
                    if myCriteria:
                        myCriteria = ''.join(
                            [myCriteria, ' and ', 'dc_info = %(Location)s'])
                    else:
                        myCriteria = ''.join(['dc_info = %(Location)s'])
                    #myArguments.update({'Location' : myArguments['Location']})

                #OS criteria
                if 'OS' in myArguments:
                    myOS = myOSVer = None

                    if (isinstance(myArguments['OS'], list)
                            or isinstance(myArguments['OS'], tuple)) and len(
                                myArguments['OS'] == 2):
                        myOS = myArguments['OS'][0]
                        myOSVer = myArguments['OS'][1]
                        myArguments.update({'OS': myOS, 'OSVersion': myOSVer})
                    elif (isinstance(myArguments['OS'], list)
                          or isinstance(myArguments['OS'], tuple)) and len(
                              myArguments['OS'] == 1):
                        myOS = myArguments['OS'][0]
                        myArguments.update({'OS': myOS})
                    else:
                        myOS = myArguments['OS']
                        myArguments.update({'OS': myOS})

                    if myCriteria:
                        if myOS:
                            myCriteria = ''.join(
                                [myCriteria, ' and ', 'OS = %(OS)s'])

                        if myOSVer:
                            myCriteria = ''.join([
                                myCriteria, ' and ',
                                'OSVersion = %(OSVersion)s'
                            ])
                    else:
                        if myOS:
                            myCriteria = ''.join(['OS = %(OS)s'])

                        if myOSVer:
                            myCriteria = ''.join([
                                myCriteria, ' and ',
                                'OSVersion = %(OSVersion)s'
                            ])

                if 'PhysicalMem' in myArguments:
                    myPhysMem = myPhysMemOper = None

                    if len(myArguments['PhysicalMem'] == 2):
                        myPhysMem = myArguments['PhysicalMem'][0]
                        myPhysMemOper = myArguments['OS'][1]
                    elif len(myArguments['OS'] == 1):
                        myPhysMem = myArguments['OS'][0]
                        myPhysMemOper = ' = '
                    else:
                        myPhysMem = myArguments['OS']
                        myPhysMemOper = ' = '

                    myArguments.update({'PhyscialMem': myPhysMem})
                    if myCriteria:
                        myCriteria = ''.join([
                            myCriteria, ' and ', 'physical_memory_mb ',
                            myPhysMemOper, ' %(PhysicalMem)s'
                        ])
                    else:
                        myCriteria = ''.join([
                            'physical_memory_mb ', myPhysMemOper,
                            ' %(PhysicalMem)s'
                        ])

            print('Criteria >>>', myCriteria)
            myDynaSql = self.mySqlUtil.buildDynaSql(mySelectColList,
                                                    mySelectTabList,
                                                    myCriteria)

            print('dynamic sql', myDynaSql, myArguments)
            dbResult = self.mySqlUtil.execSelectSql(\
             Conn = self.repConn, SqlText = myDynaSql, SqlArgs = myArguments, SqlOutput = self.globals.SqlOutput['Dict'])

            myHostData = []

            if dbResult['Status'] == self.globals.Success:
                for host in dbResult['Data']:
                    myData = {
                        'HostId': host['HOST_ID'],
                        'Host': host['HOST'],
                        "Location": host['LOCATION'],
                        "PhysicalLoc": host['PHYSICAL_LOC'],
                        "OS": host['OS'],
                        "OSVersion": host['OSVersion'],
                        "OSRelease": host['OS_RELEASE'],
                        "PhysicalMemMB": host['PHYSICAL_MEMORY_MB'],
                        "SwapMemMB": host['SWAP_MEMORY_MB'],
                        "IPAddress": host['IP_ADDRESSES'],
                        'AvgScore': round(host['AVG_SCORE'], 2),
                        "LastScan": host["LAST_SCAN"],
                        "LastScanTime": host["LAST_SCAN_TIME"]
                    }

                    if 'TENANT_VENDOR' in host:
                        myData.update({'TenantVendor': host['TENANT_VENDOR']})
                    if 'VENDOR_PRODUCT' in host:
                        myData.update(
                            {'VendorProduct': host['VENDOR_PRODUCT']})
                    if 'TENANT_NAME' in host:
                        myData.update({'TenantName': host['TENANT_NAME']})
                    if 'TENANT_VERSION' in host:
                        myData.update(
                            {'TenantVersion': host['TENANT_VERSION']})

                    myHostData.append(myData)
                myResponse = self.utility.buildResponse(
                    self.globals.Success, self.globals.Success, myHostData)
            else:
                myResponse = self.utility.buildResponse(
                    self.globals.UnSuccess, dbResult['Data'])

            return myResponse

        except Exception as error:
            raise
        finally:
            del myMandatoryArgs
            del myOptionalArgs
            del myCriteria
            del myDynaSql
            del myDynaSqlWhereClause
            del myDynaSqlGroupByClause
            del myDynaSqlOrderByClause
            del myArguments
            del myValResult
            del myResponse
            del mySelectColList
            del mySelectTabList

    def __getAvgHostScore(self, args):
        '''
		Description: Returns average CIS scan score for all host
		Arguments: Optional; HostId, Location, VendorId
		Return: [{"<HOST>" : <average_score>, "LOCATION" : <location>}]
		'''
        try:
            # initializing
            myMandatoryArgs = ["SecurityToken"]
            myOptionalArgs = ["Location", "VendorId", "HostId"]

            # Validaring arguments
            myArguments = self.utility.removeEmptyKeyFromDict(args)
            myValResult = self.utility.valRequiredArg(myArguments,
                                                      myMandatoryArgs)

            if myValResult[0] == self.globals.UnSuccess:
                myResponse = self.utility.buildResponse(
                    self.globals.UnSuccess, myValResult[2])
                return myResponse

            Location = Vendor = host = ''

            if 'HostId' in myArguments:
                # get host avg scroe for a host
                print('Host id passed, ignoring rest of criteria')
                mySql = self.globals.getAHostScoreSql
                #myArgs = self.utility.getACopy(myArguments)

            elif 'Location' in myArguments and 'VendorId' in myArguments:
                print('Loc/Venodr id passed, ignoring rest of criteria')
                mySql = self.globals.getLocVendorHostScoreSql
                #myArgs = self.utility.getACopy(myArguments)

            elif 'Location' in myArguments:
                print('Loc id passed, ignoring rest of criteria')
                mySql = self.globals.getLocHostScoreSql
                #myArgs = self.utility.getACopy(myArguments)

            elif 'VendorId' in myArguments:
                print('Vendor id passed, ignoring rest of criteria')
                mySql = self.globals.getVendorHostScoreSql
                #myArgs = self.utility.getACopy(myArguments)

            else:
                # no argument passed, returning all hosts
                print('No arguments passed, getting all host')
                mySql = self.globals.getAllHostScoreSql
                myArguments = None

            print('Sql, args >>> ', mySql, myArguments)
            dbResult = self.mySqlUtil.execSelectSql(\
             Conn = self.repConn, SqlText = mySql, SqlArgs = myArguments, SqlOutput = self.globals.SqlOutput['Dict'])

            myHostData = []

            if dbResult['Status'] == self.globals.Success:
                for host in dbResult['Data']:
                    myData = {
                        'HostId': host['HOST_ID'],
                        'Host': host['HOST'],
                        'AvgScore': round(host['AVG_SCORE'], 2),
                        "Location": host['LOCATION'],
                        "LastScan": host["LAST_SCAN"],
                        "LastScanTime": host["LAST_SCAN_TIME"]
                    }
                    if 'VENDOR' in host:
                        myData.update({"Vendor": host['VENDOR']})
                    if 'VENDOR_PRODUCT' in host:
                        myData.update(
                            {"VendorProduct": host['VENDOR_PRODUCT']})
                    myHostData.append(myData)
                myResponse = self.utility.buildResponse(
                    self.globals.Success, self.globals.Success, myHostData)
            else:
                myResponse = self.utility.buildResponse(
                    self.globals.UnSuccess, dbResult['Data'])

            return myResponse

        except Exception as error:
            raise

    def __getAllTenantScore(self):
        try:
            # Average location score
            dbResult = self.mySqlUtil.execSelectSql(\
             Conn = self.repConn, SqlText = self.globals.hostTenantScoreSql, SqlArgs = None, SqlOutput = self.globals.SqlOutput['Dict'])

            myData = list()
            #print(dbResult['Data'])
            if dbResult['Status'] == self.globals.Success:
                for tenant in dbResult['Data']:
                    #checking if this is new venodr
                    host = [
                        indx for indx, val in enumerate(myData)
                        if tenant['HOST'] in val.keys()
                    ]
                    if not host:
                        # vendor doesnt exist, adding new vendor and product
                        myData.append({
                            tenant['HOST']: [{
                                tenant['TENANT_NAME']:
                                round(tenant['LAST_SCORE'], 2),
                                "TYPE":
                                tenant["TYPE"],
                                "VENDOR":
                                tenant["VENDOR"],
                                "PRODUCT":
                                tenant["PRODUCT"],
                                "VERSION":
                                tenant["VERSION"]
                            }]
                        })
                    else:
                        # vendor exist, adding product
                        myData[host[0]][tenant['HOST']].append({
                            tenant['TENANT_NAME']:
                            round(tenant['LAST_SCORE'], 2),
                            "TYPE":
                            tenant["TYPE"],
                            "VENDOR":
                            tenant["VENDOR"],
                            "PRODUCT":
                            tenant["PRODUCT"],
                            "VERSION":
                            tenant["VERSION"]
                        })
                        #myData[host[0]][tenant['HOST']].append({tenant['HOST'] : [{tenant['TENANT_NAME'] : round(tenant['LAST_SCORE'],2)}, "TYPE" : tenant["TYPE"], "VENDOR" : tenant["VENDOR"], "PRODUCT" : tenant["PRODUCT"], "VERSION" : tenant["VERSION"]]} )

                myResponse = self.utility.buildResponse(
                    self.globals.Success, self.globals.Success, myData)
            else:
                myResponse = self.utility.buildResponse(
                    self.globals.UnSuccess, dbResult['Data'])

            #print(myResponse)
            return myResponse

        except Exception as error:
            raise

    def __getAvgVendorScore(self, args):
        '''
		Description: Returns average CIS scan score for all Venodr and its product
		Return: [{"<vendor>" : {<product> : <score>} }]
		'''
        try:
            # initializing
            myMandatoryArgs = ["SecurityToken"]
            myOptionalArgs = ["VendorId"]

            # Validaring arguments
            myArguments = self.utility.removeEmptyKeyFromDict(args)
            myValResult = self.utility.valRequiredArg(myArguments,
                                                      myMandatoryArgs)

            if myValResult[0] == self.globals.UnSuccess:
                myResponse = self.utility.buildResponse(
                    self.globals.UnSuccess, myValResult[2])
                return myResponse

            if 'VendorId' in myArguments:
                #found vendor, will return score for this vendor
                mySql = self.globals.getAVendorProdAvgScoreSql
                #myArgs = self.utility.getACopy(args)

            else:
                # will return score for all vendor
                mySql = self.globals.getAllVendorProdAvgScoreSql
                myArguments = None

            dbResult = self.mySqlUtil.execSelectSql(\
             Conn = self.repConn, SqlText = mySql, SqlArgs = myArguments, SqlOutput = self.globals.SqlOutput['Dict'])

            myData = []

            if dbResult['Status'] == self.globals.Success:
                for vendorProdList in dbResult['Data']:
                    #checking if vendor exists
                    #vendor = [indx for indx, val in enumerate(myData) if vendorProdList['VENDOR'] in val.keys()]
                    vendorIndx = [
                        indx for indx, val in enumerate(myData)
                        if myData[indx]['Vendor'] == vendorProdList['VENDOR']
                    ]
                    if not vendorIndx:
                        # vendor doesnt exist, adding new vendor and product
                        myData.append({
                            'Vendor':
                            vendorProdList['VENDOR'],
                            'Products': [{
                                'Product':
                                vendorProdList['PRODUCT'],
                                'AvgScore':
                                round(vendorProdList['AVG_SCORE'], 2)
                            }]
                        })
                    else:
                        # vendor exist, adding product
                        #print('Vendor >>> ',myData)
                        vendorIndx = vendorIndx[0]
                        myData[vendorIndx]['Products'].append({
                            'Product':
                            vendorProdList['PRODUCT'],
                            'AvgScore':
                            round(vendorProdList['AVG_SCORE'], 2)
                        })

                myResponse = self.utility.buildResponse(
                    self.globals.Success, self.globals.Success, myData)
            else:
                myResponse = self.utility.buildResponse(
                    self.globals.UnSuccess, dbResult['Data'])

            return myResponse

        except Exception as error:
            raise

    def __getAvgLocVendorScore(self, args):
        '''
		Description: Returns average CIS scan score for all Venodr and its product
		Return: [{"<location>" : {<vendor> : {<product> : <score> }}}]
		'''
        try:
            # Average location score
            # initializing
            myMandatoryArgs = ["SecurityToken"]
            myOptionalArgs = ["Location", "VendorId"]

            # Validaring arguments
            myArguments = self.utility.removeEmptyKeyFromDict(args)
            myValResult = self.utility.valRequiredArg(args, myMandatoryArgs)

            if myValResult[0] == self.globals.UnSuccess:
                myResponse = self.utility.buildResponse(
                    self.globals.UnSuccess, myValResult[2])
                return myResponse

            if 'Location' in myArguments and 'VendorId' in myArguments:
                mySql = self.globals.getALocAVendorAvgScoreSql
                #myArgs = self.utility.getACopy(myArguments)

            elif 'Location' in myArguments:
                mySql = self.globals.getALocAllVendorAvgScoreSql
                #myArgs = self.utility.getACopy(myArguments)

            elif 'VendorId' in myArguments:
                mySql = self.globals.getAllLocAVendorAvgScoreSql
                #myArgs = self.utility.getACopy(myArguments)
            else:
                mySql = self.globals.getAllLocVendorAvgScoreSql
                myArguments = None

            dbResult = self.mySqlUtil.execSelectSql(\
             Conn = self.repConn, SqlText = mySql, SqlArgs = myArguments, SqlOutput = self.globals.SqlOutput['Dict'])

            myData = []

            if dbResult['Status'] == self.globals.Success:
                for locVendor in dbResult['Data']:
                    #checking if Location exisit
                    locIndx = [
                        indx for indx, val in enumerate(myData)
                        if locVendor['LOCATION'] == myData[indx]['Location']
                    ]

                    if not locIndx:
                        # Location does not exist, will add new record
                        #print('location not found', locVendor)
                        myData.append({
                            'Location':
                            locVendor['LOCATION'],
                            'Vendors': [{
                                'Vendor':
                                locVendor['VENDOR'],
                                'Products': [{
                                    'Product':
                                    locVendor['PRODUCT'],
                                    'AvgScore':
                                    round(locVendor['AVG_SCORE'], 2)
                                }]
                            }]
                        })
                        #print('location not found', myData)
                    else:
                        locIndx = locIndx[0]
                        #print('location found',locVendor['LOCATION'])
                        # we found location, will check if vendor exists
                        #print('location:',locVendor['LOCATION'])
                        #print('mydata:',myData)
                        #print('location indx:',location)
                        #print('mydata loc:',myData[location][locVendor['LOCATION']])
                        #print('before finding vendor',myData[location][locVendor['LOCATION']])
                        vendorIndx = [
                            indx for indx, val in enumerate(myData[locIndx]
                                                            ['Vendors'])
                            if locVendor['VENDOR'] == myData[locIndx]
                            ['Vendors'][indx]['Vendor']
                        ]

                        if not vendorIndx:
                            #vendor not found for this location, will add new vendor along with its 1st product
                            #print('vendor not found',myData[location][locVendor['LOCATION']])
                            myData[locIndx]['Vendors'].append({
                                'Vendor':
                                locVendor['VENDOR'],
                                'Products': [{
                                    'Product':
                                    locVendor['PRODUCT'],
                                    'AvgScore':
                                    round(locVendor['AVG_SCORE'])
                                }]
                            })
                        else:
                            # vendor found, will add the product to this vendor
                            vendorIndx = vendorIndx[0]
                            myData[locIndx]['Vendors'][vendorIndx][
                                'Products'].append({
                                    'Product':
                                    locVendor['PRODUCT'],
                                    'AvgScore':
                                    round(locVendor['AVG_SCORE'])
                                })
                            '''
							#print('vendor found',location,locVendor['LOCATION'],vendor,myData)
							# vendor for this location found, add product to this vendor
							prodIndx = [indx for indx, val in enumerate(myData[locIndx]['Vendors'][vendorIndx]['Products']) if locVendor['PRODUCT'] == myData[locIndx]['Vendors'][vendorIndx][indx]['Product'] ]
							
							if not prodIndx:
								# product not found, will add product to current vendor
								myData[locIndx]['Vendors'][vendorIndx].update({'Products' : [{
										'Product' : locVendor['Product'],
										'AvgScore' : round(locVendor['AVG_SCORE'])
									}]
								})
							else:
								prodIndx = prodIndx[0]
								myData[locIndx]['Vendors'][vendorIndx]['Products'].append({
										'Product' : locVendor['Product'],
										'AvgScore' : round(locVendor['AVG_SCORE'])
									})

							myData[location][locVendor['LOCATION']][vendor][locVendor['VENDOR']].append({locVendor['PRODUCT'] : round(locVendor['AVG_SCORE'])})
							myData[locIndx]['Vendors'][vendorIndx][vendor][locVendor['VENDOR']].append({locVendor['PRODUCT'] : round(locVendor['AVG_SCORE'])})
							'''
                myResponse = self.utility.buildResponse(
                    self.globals.Success, self.globals.Success, myData)
            else:
                myResponse = self.utility.buildResponse(
                    self.globals.UnSuccess, dbResult['Data'])

            return myResponse

        except Exception as error:
            raise

    def __getTenantScan(self, TenantId, ScanId='Latest'):
        '''
		Description: Get latest tenant scan for a tenant id
		Args: 
			TenantId : TenantId
			ScanId : optional (default is 'Latest')
		'''
        try:
            if ScanId == self.globals.latest:
                dbResult = self.__getLastTenantScan(TenantId)
                if dbResult['Status'] == self.globals.Success:
                    if dbResult['Data']:
                        myScanId = dbResult['Data']['SCAN_ID']
                        myScanSeqId = dbResult['Data']['SEQ_ID']
                else:
                    raise ValueError('')
                myScanId = dbResult['Data']['ScanId']
            else:
                myScanId = ScanId

                dbResult = self.mySqlUtil.execSelectSql(
                    self.repConn, self.globals.getTenantScanSummarySql)
        except Exception as e:
            raise e

    def __getLastTenantScan(self, TenantId):
        '''
		Description: Get latest scan id for a tenant
		Args: 
			TenantId : TenantId
			ScanId : optional (default is 'Latest')
		Returns: last scan id and scan seq id
		'''
        try:
            mySqlArgs = {'TenantId': TenantId}
            dbResult = self.mySqlUtil.execSelectSql(\
             Conn = self.repConn, SqlText = self.globals.getLatestTenantScansSql, SqlArgs = TenantId, SqlOutput = self.globals.SqlOutput['Dict'])
            if dbResult['Status'] == self.globals.Success:
                # if we got data, we need to extract dict from tuple
                if dbResult['Data']:
                    dbResult['Data'] = dbResult['Data'][0]

            return dbResult

        except Exception as e:
            raise e
예제 #20
0
import os
from flask import Flask, flash, session, redirect, url_for, escape, request, jsonify, json
from datetime import timedelta
#from com.port8.core.singleton import Singleton
from com.port8.bpm.factory import Factory
from com.port8.core.utility import Utility
from com.port8.core.infrastructure import RestInfra
from flask_cors import CORS

import logging

#print("Hello")
util = Utility()
factory = Factory()
infra = RestInfra()

#print(os.environ['APP_SETTINGS'])
app = Flask(__name__)
app.env = 'DEVELOPMENT'
app.config.from_object(os.environ['APP_SETTINGS'])

#print('1st',app.config)
CORS(app)

#session = {}

@app.before_request
def make_session_permanent():
    session.permanent = True
    app.permanent_session_lifetime = timedelta(minutes=5)