Example #1
0
  def AddLogToHunt(self, hunt_id, client_id, message):
    if isinstance(client_id, rdfvalue.RDFURN):
      client_id = client_id.Basename()

    if isinstance(hunt_id, rdfvalue.RDFURN):
      hunt_id = hunt_id.Basename()

    if data_store.RelationalDBEnabled():
      flow_id = self._EnsureClientHasHunt(client_id, hunt_id)

      data_store.REL_DB.WriteFlowLogEntries([
          rdf_flow_objects.FlowLogEntry(
              client_id=client_id,
              flow_id=flow_id,
              hunt_id=hunt_id,
              message=message)
      ])
    else:
      hunt_obj = aff4.FACTORY.Open(rdfvalue.RDFURN("hunts").Add(hunt_id))
      logs_collection_urn = hunt_obj.logs_collection_urn

      log_entry = rdf_flows.FlowLog(
          client_id=client_id,
          urn=rdf_client.ClientURN(client_id).Add(hunt_id),
          flow_name=hunt_obj.__class__.__name__,
          log_message=message)
      with data_store.DB.GetMutationPool() as pool:
        grr_collections.LogCollection.StaticAdd(
            logs_collection_urn, log_entry, mutation_pool=pool)
Example #2
0
def ProcessHuntFlowLog(flow_obj, log_msg):
    """Process log message from a given hunt-induced flow."""

    hunt_urn = rdfvalue.RDFURN("hunts").Add(flow_obj.parent_hunt_id)
    flow_urn = hunt_urn.Add(flow_obj.flow_id)
    log_entry = rdf_flows.FlowLog(client_id=flow_obj.client_id,
                                  urn=flow_urn,
                                  flow_name=flow_obj.flow_class_name,
                                  log_message=log_msg)
    with data_store.DB.GetMutationPool() as pool:
        grr_collections.LogCollection.StaticAdd(hunt_urn.Add("Logs"),
                                                log_entry,
                                                mutation_pool=pool)
Example #3
0
    def Log(self, format_str, *args):
        """Logs the message using the flow's standard logging.

    Args:
      format_str: Format string
      *args: arguments to the format string

    Raises:
      ValueError: on parent missing logs_collection
    """
        format_str = utils.SmartUnicode(format_str)

        status = format_str
        if args:
            try:
                # The status message is always in unicode
                status = format_str % args
            except TypeError:
                logging.error(
                    "Tried to log a format string with the wrong number "
                    "of arguments: %s", format_str)

        logging.info("%s: %s", self.session_id, status)

        self.context.status = utils.SmartUnicode(status)

        log_entry = rdf_flows.FlowLog(
            client_id=self.runner_args.client_id,
            urn=self.session_id,
            flow_name=self.flow_obj.__class__.__name__,
            log_message=status)
        logs_collection_urn = self._GetLogCollectionURN(
            self.runner_args.logs_collection_urn)
        with data_store.DB.GetMutationPool() as pool:
            grr_collections.LogCollection.StaticAdd(logs_collection_urn,
                                                    log_entry,
                                                    mutation_pool=pool)
    def Run(self):
        if data_store.RelationalDBEnabled():
            with test_lib.FakeTime(42):
                hunt_id = self.CreateHunt()

            client_id = self.SetupClient(0).Basename()
            flow_id = flow_test_lib.StartFlow(flows_processes.ListProcesses,
                                              client_id=client_id,
                                              parent_hunt_id=hunt_id)

            with test_lib.FakeTime(52):
                data_store.REL_DB.WriteFlowLogEntries([
                    rdf_flow_objects.FlowLogEntry(
                        client_id=client_id,
                        flow_id=flow_id,
                        hunt_id=hunt_id,
                        message="Sample message: foo")
                ])

            with test_lib.FakeTime(55):
                data_store.REL_DB.WriteFlowLogEntries([
                    rdf_flow_objects.FlowLogEntry(
                        client_id=client_id,
                        flow_id=flow_id,
                        hunt_id=hunt_id,
                        message="Sample message: bar")
                ])
        else:
            with test_lib.FakeTime(42):
                client_id = self.SetupClient(0)
                flow_id = "H:123456"
                with self.CreateHunt(description="the hunt") as hunt_obj:
                    hunt_id = hunt_obj.urn.Basename()
                    logs_collection_urn = hunt_obj.logs_collection_urn

                log_entry = rdf_flows.FlowLog(
                    client_id=client_id,
                    urn=client_id.Add(flow_id),
                    flow_name=hunt_obj.__class__.__name__,
                    log_message="Sample message: foo")
                with test_lib.FakeTime(52):
                    with data_store.DB.GetMutationPool() as pool:
                        grr_collections.LogCollection.StaticAdd(
                            logs_collection_urn, log_entry, mutation_pool=pool)

                log_entry = rdf_flows.FlowLog(
                    client_id=client_id,
                    urn=client_id.Add(flow_id),
                    flow_name=hunt_obj.__class__.__name__,
                    log_message="Sample message: bar")
                with test_lib.FakeTime(55):
                    with data_store.DB.GetMutationPool() as pool:
                        grr_collections.LogCollection.StaticAdd(
                            logs_collection_urn, log_entry, mutation_pool=pool)

        self.Check("ListHuntLogs",
                   args=hunt_plugin.ApiListHuntLogsArgs(hunt_id=hunt_id),
                   replace={hunt_id: "H:123456"})
        self.Check("ListHuntLogs",
                   args=hunt_plugin.ApiListHuntLogsArgs(hunt_id=hunt_id,
                                                        count=1),
                   replace={hunt_id: "H:123456"})
        self.Check("ListHuntLogs",
                   args=hunt_plugin.ApiListHuntLogsArgs(hunt_id=hunt_id,
                                                        offset=1,
                                                        count=1),
                   replace={hunt_id: "H:123456"})