def __init__(self, initializer=None, age=None, check_id=None, target=None, match=None, method=None, hint=None): super(Check, self).__init__(initializer=initializer, age=age) self.check_id = check_id self.match = MatchStrToList(match) self.hint = Hint(hint, reformat=False) self.target = target if method is None: method = [] self.triggers = triggers.Triggers() self.matcher = checks.Matcher(self.match, self.hint) for cfg in method: # Use the value of "target" as a default for each method, if defined. # Targets defined in methods or probes override this default value. if hint: cfg["hint"] = hints.Overlay(child=cfg.get("hint", {}), parent=hint) if target: cfg.setdefault("target", target) # Create the method and add its triggers to the check. m = Method(**cfg) self.method.append(m) self.triggers.Update(m.triggers, callback=m) self.artifacts = set([t.artifact for t in self.triggers.conditions])
def __init__(self, initializer=None, age=None, **kwargs): if isinstance(initializer, dict): conf = initializer initializer = None else: conf = kwargs super(Method, self).__init__(initializer=initializer, age=age) probe = conf.get("probe", {}) resource = conf.get("resource", {}) hint = conf.get("hint", {}) target = conf.get("target", {}) if hint: # Add the hint to children. for cfg in probe: cfg["hint"] = hints.Overlay(child=cfg.get("hint", {}), parent=hint) self.probe = [Probe(**cfg) for cfg in probe] self.hint = Hint(hint, reformat=False) self.match = MatchStrToList(kwargs.get("match")) self.matcher = checks.Matcher(self.match, self.hint) self.resource = [rdfvalue.Dict(**r) for r in resource] self.target = Target(**target) self.triggers = triggers.Triggers() for p in self.probe: # If the probe has a target, use it. Otherwise, use the method's target. target = p.target or self.target self.triggers.Add(p.artifact, target, p)
def testCheckAny(self): """ANY operations should not return anomalies if there are results.""" matcher = checks.Matcher(["ANY"], self.hint) for baseline in self.baselines: for result in [self.one, self.some]: self.assertIsInstance(matcher.Detect(baseline, result), rdfvalue.CheckResult) self.assertFalse(matcher.Detect(baseline, self.none))
def testCheckSome(self): """SOME operations should return anomalies if there is >1 result.""" matcher = checks.Matcher(["SOME"], self.hint) for baseline in self.baselines: self.assertIsInstance(matcher.Detect(baseline, self.some), rdfvalue.CheckResult) for result in [self.none, self.one]: self.assertFalse(matcher.Detect(baseline, result))
def testCheckNone(self): """NONE returns an anomaly if there are no results.""" matcher = checks.Matcher(["NONE"], self.hint) for baseline in self.baselines: self.assertIsInstance(matcher.Detect(baseline, self.none), rdfvalue.CheckResult) for result in [self.one, self.some]: self.assertFalse(matcher.Detect(baseline, result))
def testMultipleMatch(self): """Checks with multiple match methods emit results if any methods fire.""" matcher = checks.Matcher(["NONE", "ONE"], self.hint) for baseline in self.baselines: for result in [self.none, self.one]: self.assertIsInstance(matcher.Detect(baseline, result), rdfvalue.CheckResult) self.assertFalse(matcher.Detect(baseline, self.some))
def testCheckOne(self): """ONE operations should return anomalies if there is not one result.""" matcher = checks.Matcher(["ONE"], self.hint) for baseline in self.baselines: self.assertIsInstance( matcher.Detect(baseline, self.one), checks.CheckResult) for result in [self.none, self.some]: self.assertFalse(matcher.Detect(baseline, result))
def testCheckAll(self): """ALL operations return anomalies if input and result counts differ.""" matcher = checks.Matcher(["ALL"], self.hint) will_detect = [(self.one, self.one), (self.some, self.some)] not_detect = [(self.none, self.none), (self.some, self.one), (self.some, self.none)] will_raise = [(self.none, self.one), (self.one, self.some), (self.none, self.some)] for base, result in will_detect: self.assertIsInstance(matcher.Detect(base, result), checks.CheckResult) for base, result in not_detect: self.assertFalse(matcher.Detect(base, result)) for base, result in will_raise: self.assertRaises(checks.ProcessingError, matcher.Detect, base, result)
def __init__(self, initializer=None, age=None, **kwargs): if isinstance(initializer, dict): conf = initializer initializer = None else: conf = kwargs conf["match"] = MatchStrToList(kwargs.get("match")) super(Probe, self).__init__(initializer=initializer, age=age, **conf) if self.filters: handler = filters.GetHandler(mode=self.mode) else: handler = filters.GetHandler() self.baseliner = handler(artifact=self.artifact, filters=self.baseline) self.handler = handler(artifact=self.artifact, filters=self.filters) hinter = Hint(conf.get("hint", {}), reformat=False) self.matcher = checks.Matcher(conf["match"], hinter)