Пример #1
0
    def resolveFiles(self, fileSignature):
        """
        @return: list(asm_file_system.ResolvedConfigFile)
        """
        fileLocations = fileSignature.FileLocations if hasattr(fileSignature, 'FileLocations') else None
        filename = fileSignature.name
        fileContentSignature = fileSignature.FileContent if hasattr(fileSignature, 'FileContent') else None
        files = []
        _, undefinedVariables = ExpressionResolver.checkVariable(self.variableResolver, filename)
        if undefinedVariables:
            logger.debug('Skip configuration file %s because following variable%s no value:' %
                         (filename, 's have' if len(undefinedVariables) > 1 else ' has'), ', '.join(undefinedVariables))
        elif fileLocations:
            paths = fileLocations.children
            for path in paths:
                if not OSType.match(path.os, self.shell):
                    logger.debug('Skip path %s because expect os type %s but actual is %s' %
                                 (path.text, path.os, OSType.fromShell(self.shell)))
                    continue
                for resolvedPath, resolvedFilename in self.resolvePath(path.text, filename):
                    found = asm_file_system.getConfigFilesFromPath(self.shell, resolvedPath, resolvedFilename, path.includeSub)
                    if found:
                        files.extend(found)
        elif fileContentSignature:
            fileContent = ExpressionResolver.resolve(self.variableResolver, fileContentSignature.text)
            file = asm_file_system.ResolvedConfigFile(None, filename, fileContent)
            files.append(file)

        return files
Пример #2
0
def processRegex(regExpSignature, input, variableResolver):
    pattern = ExpressionResolver.resolve(variableResolver, regExpSignature.expr, ExpressionResolver.REGULAR_EXPRESSION)
    if not pattern:
        logger.debug('Skip ', regExpSignature)
        return
    flags = getFlags(regExpSignature)
    try:
        compiledRegex = re.compile('(%s)' % pattern, flags)
    except Exception, e:
        logger.debug('Skip %s because regex %s is invalid:' % (regExpSignature, ExpressionResolver.quote(pattern)), e)
        return
Пример #3
0
 def _process(cls, variableSignature, variableResolver, input):
     processor, context = input
     xpath = ExpressionResolver.resolve(variableResolver, getattr(variableSignature, ATTR_XPATH, None) or variableSignature.relativePath,
                                        ExpressionResolver.XPATH)
     if not xpath:
         logger.debug('Skip ', variableSignature)
         return
     try:
         result = processor.evaluate(xpath, context)
         if result:
             logger.debug('Found result for xpath %s:' % ExpressionResolver.quote(xpath), result)
         else:
             logger.debug('No matches for xpath:', xpath)
             result = ''
         return result
     except Exception, e:
         logger.debug('Skip %s because fail to evaluate xpath %s:' % (variableSignature, ExpressionResolver.quote(xpath)), e)
Пример #4
0
    def resolvePath(self, filePath, filename):
        """
        @type path: str
        @type filename: str
        @return: list(str,str)
        """
        resolvedPaths = []
        _, undefinedVariables = ExpressionResolver.checkVariable(self.variableResolver, filePath)
        if undefinedVariables:
            logger.debug('Skip path %s because following variable%s was not defined:' %
                         (filePath, 's' if len(undefinedVariables) > 1 else ''), ', '.join(undefinedVariables))
        else:
            fullPath = '|'.join((filePath, filename))
            paths = ExpressionResolver.resolve(self.variableResolver, fullPath, ExpressionResolver.STRING_LITERAL_LIST)
            for path in paths:
                resolvedPaths.append(path.split('|'))

        return resolvedPaths
Пример #5
0
 def process(self, fileSignature, configFile):
     """
     @type configFile: asm_file_system.ResolvedConfigFile
     """
     fileContent = configFile.getContent()
     try:
         self._prepareFile(fileContent)
     except:
         e = sys.exc_info()[1]
         logger.debug('Error occurred when preparing config file %s:' % ExpressionResolver.quote(configFile.getName()), e)
         return
     logger.debug('Processing configuration file %s found in path %s' % (
         ExpressionResolver.quote(configFile.getName()), ExpressionResolver.quote(configFile.getPath())))
     for signature in fileSignature.children:
         if signature.getType() == TAG_REGEX:
             processRegex(signature, fileContent, self.variableResolver)
         elif signature.getType() == TAG_SYSTEM_VARIABLE or signature.getType() == TAG_PYTHON_VARIABLE:
             processVariable(signature, self.variableResolver, self.shell, False)
         elif signature.getType() != TAG_FILE_LOCATIONS:
             self._processVariable(signature)
Пример #6
0
 def _processVariable(self, signature):
     if signature.getType() == TAG_XPATH:
         xpaths = ExpressionResolver.resolve(self.variableResolver, signature.xpath, ExpressionResolver.STRING_LITERAL_LIST)
         if not xpaths:
             logger.debug('Skip ', signature)
             return
         for xpath in xpaths:
             items = []
             try:
                 items = self.processor.evaluateItem(xpath)
             except Exception, e:
                 logger.debug('Fail to evaluate xpath %s:' % ExpressionResolver.quote(xpath), e)
                 return
             if not items:
                 logger.debug('No matches for xpath:', xpath)
             for item in items:
                 logger.debug('Found result for xpath %s:' % ExpressionResolver.quote(xpath), item)
                 for var in signature.children:
                     if var.getType() == TAG_REGEX:
                         if hasattr(var, ATTR_RELATIVE_PATH):
                             relativePath = ExpressionResolver.resolve(self.variableResolver, var.relativePath, ExpressionResolver.XPATH)
                             if not relativePath:
                                 logger.debug('Skip ', var)
                                 continue
                             try:
                                 values = self.processor.evaluate(relativePath, item)
                             except Exception, e:
                                 logger.debug('Fail to evaluate relative xpath %s:' % ExpressionResolver.quote(relativePath), e)
                                 continue
                             if items:
                                 logger.debug('Found result for relative xpath %s:' % ExpressionResolver.quote(relativePath), values)
                             else:
                                 logger.debug('No matches for relative xpath:', relativePath)
                         else:
                             values = [item.getStringValue()]
                         for value in values:
                             processRegex(var, value, self.variableResolver)
                     elif var.getType() == TAG_VARIABLE:
                         XPathVariableProcessor.process(var, self.variableResolver, (self.processor, item), True)
Пример #7
0
def buildSCP(scpSignature, variableResolver):
    scpData = {}
    sizes = {}

    for attr in SCP_ATTRIBUTES.keys():
        value = []
        if hasattr(scpSignature, attr):
            value.extend(ExpressionResolver.resolve(variableResolver, getattr(scpSignature, attr), ExpressionResolver.STRING_LITERAL_LIST))
        if not value and attr != 'context':
            logger.debug('Skip %s because %s is required' % (scpSignature, attr))
            return []
        scpData[attr] = value
        sizes[attr] = len(value)

    maxCount = max(sizes.values())

    # Check length of attribute value lists
    for attr in SCP_ATTRIBUTES.keys():
        size = sizes[attr]
        if size > 1 and size != maxCount:
            # check size. the list length should be same or less than 2
            logger.debug('Skip %s because the size of attributes mismatch:' % scpSignature,
                         ', '.join(['%s[%d]' % (attr, sizes[attr]) for attr in SCP_ATTRIBUTES]))
            return []

    scps = []
    for index in range(maxCount):
        params = {}
        # Fill scp attributes
        for attr in SCP_ATTRIBUTES.keys():
            size = sizes[attr]
            if size == 0:
                value = None
            elif size == 1:
                value = scpData[attr][0]
            else:
                value = scpData[attr][index]
            params[attr] = value

        for attr in SCP_ATTRIBUTES.keys():
            if not params[attr] and SCP_ATTRIBUTES[attr]:
                logger.debug('Skip SCP %s because attribute "%s" is required' % (params, attr))
                params = None
                break

        if params:
            logger.debug('Build Service Connection Point:', params)
            scps.append(params)

    return scps
Пример #8
0
def processExecuteCommand(shell, execSignature, variableResolver):
    cmdline = ExpressionResolver.resolve(variableResolver, execSignature.cmdline)
    if not cmdline:
        logger.debug('Skip ', execSignature)
        return
    if not OSType.match(execSignature.os, shell):
        logger.debug('Skip command execution %s because expect os type "%s" but actual is "%s"' %
                     (cmdline, execSignature.os, OSType.fromShell(shell)))
        return
    result = None
    error = None
    try:
        result = shell.execCmd(cmdline)
        if shell.getLastCmdReturnCode():
            error = 'error code:%d' % shell.getLastCmdReturnCode()
    except Exception, e:
        error = e
Пример #9
0
def processCommandLine(cmdlineSignature, application, shell, processMap, variableResolver):
    processes = list(application.getProcesses())
    if cmdlineSignature.includeParentProcesses:
        for process in processes:
            processes.extend(getParentProcesses(process, processMap))

    for signature in cmdlineSignature.children:
        if signature.getType() == TAG_REGEX:
            regex = signature.expr
            if not OSType.match(signature.os, shell):
                logger.debug('Skip command line pattern %s because expect os type "%s" but actual is %s' %
                             (ExpressionResolver.quote(regex), signature.os, OSType.fromShell(shell)))
                continue  # ignore the expression because os type mismatch
            for process in processes:
                logger.debug('Check process command line %s with pattern:' % process.commandLine, regex)
                processRegex(signature, process.commandLine, variableResolver)
        elif TAG_VARIABLE in signature.getType():
            processVariable(signature, variableResolver, shell, False)
        elif signature.getType() == TAG_EXECUTE:
            processExecuteCommand(shell, signature, variableResolver)
Пример #10
0
 def _process(cls, variableSignature, variableResolver, input):
     key = ExpressionResolver.resolve(variableResolver, variableSignature.key, ExpressionResolver.STRING_LITERAL)
     if not key:
         logger.debug('Skip ', variableSignature)
         return
     return input.getProperty(key) or ''
Пример #11
0

def processRegex(regExpSignature, input, variableResolver):
    pattern = ExpressionResolver.resolve(variableResolver, regExpSignature.expr, ExpressionResolver.REGULAR_EXPRESSION)
    if not pattern:
        logger.debug('Skip ', regExpSignature)
        return
    flags = getFlags(regExpSignature)
    try:
        compiledRegex = re.compile('(%s)' % pattern, flags)
    except Exception, e:
        logger.debug('Skip %s because regex %s is invalid:' % (regExpSignature, ExpressionResolver.quote(pattern)), e)
        return
    match = compiledRegex.findall(input)
    if match:
        logger.debug('Found match string for regex %s:' % ExpressionResolver.quote(pattern), ', '.join([m[0] for m in match]))
        for variable in regExpSignature.children:
            RegexVariableProcessor.process(variable, variableResolver, match, len(regExpSignature.children) > 1)
    else:
        logger.debug('No match string found for regex %s' % ExpressionResolver.quote(pattern))


def getFlags(regExpSignature):
    flags = 0
    flagAttrValue = getattr(regExpSignature, ATTR_REGEX_FLAG, None)
    if flagAttrValue:
        for flag in flagAttrValue.split(','):
            flagValue = getattr(re, flag.strip().upper(), None)
            if flagValue:
                flags |= flagValue
            else: