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

    '''
Beispiel #2
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
Beispiel #3
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