Beispiel #1
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
Beispiel #2
0
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
Beispiel #3
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)
Beispiel #4
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