Beispiel #1
0
  def InitFromAff4Object(self,
                         flow_obj,
                         flow_id=None,
                         with_state_and_context=False):
    try:
      # TODO(user): we should be able to infer flow id from the
      # URN. Currently it's not possible due to an inconsistent way in
      # which we create symlinks and name them.
      self.flow_id = flow_id
      self.urn = flow_obj.urn

      self.name = flow_obj.runner_args.flow_name
      self.started_at = flow_obj.context.create_time
      self.last_active_at = flow_obj.Get(flow_obj.Schema.LAST)
      self.creator = flow_obj.context.creator

      if flow_obj.Get(flow_obj.Schema.CLIENT_CRASH):
        self.state = "CLIENT_CRASHED"
      elif flow_obj.Get(flow_obj.Schema.PENDING_TERMINATION):
        self.state = flow_obj.context.state = "ERROR"
        reason = flow_obj.Get(flow_obj.Schema.PENDING_TERMINATION).reason
        flow_obj.context.status = "Pending termination: %s" % reason
      else:
        self.state = flow_obj.context.state

      try:
        self.args = flow_obj.args
      except ValueError:
        # If args class name has changed, ValueError will be raised. Handling
        # this gracefully - we should still try to display some useful info
        # about the flow.
        pass

      self.runner_args = flow_obj.runner_args

      if self.runner_args.original_flow.flow_id:
        self.original_flow = ApiFlowReference().FromFlowReference(
            self.runner_args.original_flow)

      if with_state_and_context:
        try:
          self.context = flow_obj.context
        except ValueError:
          pass

        flow_state_dict = flow_obj.Get(flow_obj.Schema.FLOW_STATE_DICT)
        if flow_state_dict is not None:
          flow_state_data = flow_state_dict.ToDict()

          if flow_state_data:
            self.state_data = (
                api_call_handler_utils.ApiDataObject()
                .InitFromDataObject(flow_state_data))
    except Exception as e:  # pylint: disable=broad-except
      self.internal_error = "Error while opening flow: %s" % str(e)

    return self
Beispiel #2
0
  def Handle(self, args, token=None):
    """Retrieves the context for a hunt."""
    hunt = aff4.FACTORY.Open(HUNTS_ROOT_PATH.Add(args.hunt_id),
                             aff4_type="GRRHunt", token=token)

    context = api_call_handler_utils.ApiDataObject().InitFromDataObject(
        hunt.state.context)

    return ApiGetHuntContextResult(context=context)
Beispiel #3
0
    def InitFromAff4Object(self, flow_obj, with_state_and_context=False):
        # If the flow object is in fact a symlink, then we want to report the
        # symlink's URN as a flow's URN. Otherwise you may get unexpected
        # URNs while listing client's flows. For example, this may happend when
        # a hunt was running on a client and a flow itself is located in the
        # hunt's namespace, but was symlinked into the client's namespace:
        #
        # aff4:/hunts/H:123456/flows/H:987654 ->
        #   aff4:/C.0000111122223333/flows/H:987654
        if hasattr(flow_obj, "symlink_urn"):
            self.urn = flow_obj.symlink_urn
        else:
            self.urn = flow_obj.urn

        self.name = flow_obj.runner_args.flow_name
        self.started_at = flow_obj.context.create_time
        self.last_active_at = flow_obj.Get(flow_obj.Schema.LAST)
        self.creator = flow_obj.context.creator

        if flow_obj.Get(flow_obj.Schema.CLIENT_CRASH):
            self.state = "CLIENT_CRASHED"
        else:
            self.state = flow_obj.context.state

        try:
            self.args = flow_obj.args
        except ValueError:
            # If args class name has changed, ValueError will be raised. Handling
            # this gracefully - we should still try to display some useful info
            # about the flow.
            pass

        self.runner_args = flow_obj.runner_args

        if with_state_and_context:
            try:
                self.context = flow_obj.context
            except ValueError:
                # TODO(user): remove after old-style flows are not important
                # anymore (EOY2016).
                pass

            flow_state_dict = flow_obj.Get(flow_obj.Schema.FLOW_STATE_DICT)
            if flow_state_dict is not None:
                flow_state_data = flow_state_dict.ToDict()
            else:
                # We're dealing with old-style flow.
                # TODO(user): remove after old-style flows are not important
                # anymore (EOY2016).
                flow_state_data = flow_obj.Get(flow_obj.Schema.FLOW_STATE)

            if flow_state_data:
                self.state_data = (api_call_handler_utils.ApiDataObject().
                                   InitFromDataObject(flow_state_data))

        return self
Beispiel #4
0
    def InitFromAff4Object(self,
                           flow_obj,
                           flow_id=None,
                           with_state_and_context=False):
        # TODO(user): we should be able to infer flow id from the URN. Currently
        # it's not possible due to an inconsistent way in which we create symlinks
        # and name them.
        self.flow_id = flow_id
        self.urn = flow_obj.urn

        self.name = flow_obj.runner_args.flow_name
        self.started_at = flow_obj.context.create_time
        self.last_active_at = flow_obj.Get(flow_obj.Schema.LAST)
        self.creator = flow_obj.context.creator

        if flow_obj.Get(flow_obj.Schema.CLIENT_CRASH):
            self.state = "CLIENT_CRASHED"
        else:
            self.state = flow_obj.context.state

        try:
            self.args = flow_obj.args
        except ValueError:
            # If args class name has changed, ValueError will be raised. Handling
            # this gracefully - we should still try to display some useful info
            # about the flow.
            pass

        self.runner_args = flow_obj.runner_args

        if with_state_and_context:
            try:
                self.context = flow_obj.context
            except ValueError:
                # TODO(user): remove after old-style flows are not important
                # anymore (EOY2016).
                pass

            flow_state_dict = flow_obj.Get(flow_obj.Schema.FLOW_STATE_DICT)
            if flow_state_dict is not None:
                flow_state_data = flow_state_dict.ToDict()
            else:
                # We're dealing with old-style flow.
                # TODO(user): remove after old-style flows are not important
                # anymore (EOY2016).
                flow_state_data = flow_obj.Get(flow_obj.Schema.FLOW_STATE)

            if flow_state_data:
                self.state_data = (api_call_handler_utils.ApiDataObject().
                                   InitFromDataObject(flow_state_data))

        return self
Beispiel #5
0
  def Handle(self, args, token=None):
    """Retrieves the context for a hunt."""
    hunt = aff4.FACTORY.Open(
        args.hunt_id.ToURN(), aff4_type=implementation.GRRHunt, token=token)

    if isinstance(hunt.context, rdf_hunts.HuntContext):  # New style hunt.
      # TODO(amoser): Hunt state will go away soon, we don't render it anymore.
      state = api_call_handler_utils.ApiDataObject()
      result = ApiGetHuntContextResult(context=hunt.context, state=state)
      # Assign args last since it needs the other fields set to
      # determine the args protobuf.
      result.args = hunt.args
      return result

    else:
      # Just pack the whole context data object in the state
      # field. This contains everything for old style hunts so we at
      # least show the data somehow.
      context = api_call_handler_utils.ApiDataObject().InitFromDataObject(
          hunt.context)

      return ApiGetHuntContextResult(state=context)