Example #1
0
  def testStopping(self):
    """Tests if we can stop a hunt."""

    foreman = aff4.FACTORY.Open("aff4:/foreman", mode="rw", token=self.token)
    rules = foreman.Get(foreman.Schema.RULES)

    # Make sure there are no rules yet.
    self.assertEqual(len(rules), 0)
    now = rdfvalue.RDFDatetime.Now()
    expires = rdfvalue.Duration("1h").Expiry()
    # Add some rules.
    rules = [
        rdf_foreman.ForemanRule(
            created=now, expires=expires, description="Test rule1"),
        rdf_foreman.ForemanRule(
            created=now, expires=expires, description="Test rule2")
    ]
    self.AddForemanRules(rules)

    client_rule_set = rdf_foreman.ForemanClientRuleSet(rules=[
        rdf_foreman.ForemanClientRule(
            rule_type=rdf_foreman.ForemanClientRule.Type.REGEX,
            regex=rdf_foreman.ForemanRegexClientRule(
                attribute_name="GRR client", attribute_regex="HUNT")),
        rdf_foreman.ForemanClientRule(
            rule_type=rdf_foreman.ForemanClientRule.Type.INTEGER,
            integer=rdf_foreman.ForemanIntegerClientRule(
                attribute_name="Clock",
                operator=rdf_foreman.ForemanIntegerClientRule.Operator.
                GREATER_THAN,
                value=1336650631137737))
    ])

    hunt = implementation.GRRHunt.StartHunt(
        hunt_name=standard.SampleHunt.__name__,
        client_rule_set=client_rule_set,
        client_rate=0,
        token=self.token)

    with hunt:
      runner = hunt.GetRunner()
      runner.Start()

      # Add some more rules.
      rules = [
          rdf_foreman.ForemanRule(
              created=now, expires=expires, description="Test rule3"),
          rdf_foreman.ForemanRule(
              created=now, expires=expires, description="Test rule4")
      ]
      self.AddForemanRules(rules)

      foreman = aff4.FACTORY.Open("aff4:/foreman", mode="rw", token=self.token)
      rules = foreman.Get(foreman.Schema.RULES)
      self.assertEqual(len(rules), 5)

      # It should be running.
      self.assertTrue(runner.IsHuntStarted())

      # Now we stop the hunt.
      hunt.Stop()

    foreman = aff4.FACTORY.Open("aff4:/foreman", mode="rw", token=self.token)
    rules = foreman.Get(foreman.Schema.RULES)
    # The rule for this hunt should be deleted but the rest should be there.
    self.assertEqual(len(rules), 4)

    # And the hunt should report no outstanding requests any more.
    with hunt:
      self.assertFalse(hunt.GetRunner().IsHuntStarted())
Example #2
0
    def Start(self):
        self.state.hunt_id = None
        self.state.client_ids_failures = set()
        self.state.client_ids_result_reported = set()

        # TODO(user): Figure out if this can just be a set. Add a tap test to
        # meaningfully exercise this code.
        self.state.client_ids = list(
            base.GetClientTestTargets(token=self.token))

        if not self.state.client_ids:
            self.Log("No clients to test on, define them in "
                     "Test.end_to_end_client_ids")
            return

        # SetUID is required to run a hunt on the configured end-to-end client
        # targets without an approval.
        token = access_control.ACLToken(
            username="******", reason="Running endtoend tests.").SetUID()
        runner_args = rdf_flows.FlowRunnerArgs(
            flow_name=flows_endtoend.EndToEndTestFlow.__name__)

        flow_request = hunts_standard.FlowRequest(
            client_ids=self.state.client_ids,
            args=flows_endtoend.EndToEndTestFlowArgs(),
            runner_args=runner_args)

        bogus_rule = rdf_foreman.ForemanRegexClientRule(
            attribute_name="System", attribute_regex="Does not match anything")

        client_rule_set = rdf_foreman.ForemanClientRuleSet(rules=[
            rdf_foreman.ForemanClientRule(
                rule_type=rdf_foreman.ForemanClientRule.Type.REGEX,
                regex=bogus_rule)
        ])

        hunt_args = hunts_standard.VariableGenericHuntArgs(
            flows=[flow_request])

        hunt_args.output_plugins = self.GetOutputPlugins()

        with hunts_implementation.GRRHunt.StartHunt(
                hunt_name=hunts_standard.VariableGenericHunt.__name__,
                args=hunt_args,
                client_rule_set=client_rule_set,
                client_rate=0,
                expiry_time="1d",
                token=token) as hunt:

            self.state.hunt_id = hunt.session_id
            hunt.SetDescription("EndToEnd tests run by cron")
            hunt.Run()
            hunt.ManuallyScheduleClients(token=token)

        # Set a callback to check the results after 50 minutes.  This should be
        # plenty of time for the clients to receive the hunt and run the tests, but
        # not so long that the flow lease will expire.

        wait_duration = rdfvalue.Duration(
            config.CONFIG.Get("Test.end_to_end_result_check_wait"))
        completed_time = rdfvalue.RDFDatetime.Now() + wait_duration

        self.CallState(next_state="CheckResults", start_time=completed_time)
Example #3
0
 def GenerateSample(number=0):
   # Wrap the operating system rule sample generator
   return rdf_foreman.ForemanClientRule(
       rule_type=rdf_foreman.ForemanClientRule.Type.OS,
       os=ForemanOsClientRuleTest.GenerateSample(number))
Example #4
0
    def testCopyHuntRespectsUserChanges(self):
        self.CreateSampleHunt("model hunt", token=self.token)

        self.Open("/#main=ManageHunts")
        self.Click("css=tr:contains('model hunt')")
        self.Click("css=button[name=CopyHunt]:not([disabled])")

        # Wait until dialog appears and then click through.
        self.WaitUntil(self.IsTextPresent, "What to run?")

        # Change values in the flow configuration.
        self.Type(
            "css=grr-new-hunt-wizard-form label:contains('Path') "
            "~ * input:text", "/tmp/very-evil.txt")

        self.Select(
            "css=grr-new-hunt-wizard-form label:contains('Pathtype') "
            "~ * select", "OS")

        self.Type(
            "css=grr-new-hunt-wizard-form label:contains('Description') "
            "~ * input:text", "my personal copy")

        self.Type(
            "css=grr-new-hunt-wizard-form label:contains('Client rate') "
            "~ * input", "42")

        # Click on "Next" button.
        self.Click("css=grr-new-hunt-wizard-form button.Next")
        self.WaitUntil(self.IsTextPresent, "Output Processing")

        # Change output plugin and add another one.
        self.Click("css=grr-new-hunt-wizard-form button[name=Add]")
        self.Select("css=grr-configure-output-plugins-page select:eq(0)",
                    "DummyOutputPlugin")
        self.Type(
            "css=grr-configure-output-plugins-page "
            "label:contains('Filename Regex'):eq(0) ~ * input:text", "foobar!")

        # Click on "Next" button.
        self.Click("css=grr-new-hunt-wizard-form button.Next")
        self.WaitUntil(self.IsTextPresent, "Where to run?")

        # Replace a rule with another one.
        self.Click("css=grr-configure-rules-page button[name=Remove]")
        self.Click("css=grr-configure-rules-page button[name=Add]")
        self.Click(
            "css=grr-configure-rules-page label:contains('Os darwin') ~ * "
            "input[type=checkbox]")

        # Click on "Next" button.
        self.Click("css=grr-new-hunt-wizard-form button.Next")
        self.WaitUntil(self.IsTextPresent, "Review")

        # Check that expected values are shown in the review.
        self.WaitUntil(self.IsTextPresent, "OS")
        self.WaitUntil(self.IsTextPresent, "/tmp/very-evil.txt")
        self.WaitUntil(self.IsTextPresent, transfer.GetFile.__name__)
        self.WaitUntil(self.IsTextPresent, "DummyOutputPlugin")
        self.WaitUntil(self.IsTextPresent, "foobar!")
        self.WaitUntil(self.IsTextPresent, "blah!")
        self.WaitUntil(self.IsTextPresent, "my personal copy")
        self.WaitUntil(self.IsTextPresent, "Os darwin")
        self.WaitUntil(self.IsTextPresent, "42")

        # Click on "Run" button.
        self.Click("css=grr-new-hunt-wizard-form button.Next")
        self.WaitUntil(self.IsTextPresent, "Created Hunt")

        hunts_root = aff4.FACTORY.Open("aff4:/hunts", token=self.token)
        hunts_list = sorted(list(hunts_root.ListChildren()),
                            key=lambda x: x.age)

        self.assertEqual(len(hunts_list), 2)
        last_hunt = aff4.FACTORY.Open(hunts_list[-1], token=self.token)

        self.assertEqual(last_hunt.args.flow_args.pathspec.path,
                         "/tmp/very-evil.txt")
        self.assertEqual(last_hunt.args.flow_args.pathspec.pathtype, "OS")
        self.assertEqual(last_hunt.args.flow_runner_args.flow_name,
                         transfer.GetFile.__name__)

        self.assertEqual(len(last_hunt.runner_args.output_plugins), 2)
        self.assertEqual(last_hunt.runner_args.output_plugins[0].plugin_name,
                         "DummyOutputPlugin")
        self.assertEqual(
            last_hunt.runner_args.output_plugins[0].plugin_args.filename_regex,
            "foobar!")
        self.assertEqual(
            last_hunt.runner_args.output_plugins[0].plugin_args.fetch_binaries,
            False)
        self.assertEqual(last_hunt.runner_args.output_plugins[1].plugin_name,
                         "DummyOutputPlugin")
        self.assertEqual(
            last_hunt.runner_args.output_plugins[1].plugin_args.filename_regex,
            "blah!")
        self.assertEqual(
            last_hunt.runner_args.output_plugins[1].plugin_args.fetch_binaries,
            True)

        runner_args = last_hunt.runner_args
        self.assertAlmostEqual(runner_args.client_rate, 42)
        self.assertEqual(runner_args.description, "my personal copy")
        self.assertEqual(
            runner_args.client_rule_set,
            rdf_foreman.ForemanClientRuleSet(rules=[
                rdf_foreman.ForemanClientRule(
                    os=rdf_foreman.ForemanOsClientRule(os_darwin=True))
            ]))