Ejemplo n.º 1
0
    def testVfsNotificationIsParsedCorrectly(self):
        n = self.InitFromObj_(
            rdf_objects.UserNotification.Type.TYPE_VFS_FILE_COLLECTED,
            rdf_objects.ObjectReference(
                reference_type=rdf_objects.ObjectReference.Type.VFS_FILE,
                vfs_file=rdf_objects.VfsFileReference(
                    client_id=self.client_id,
                    path_type=rdf_objects.PathInfo.PathType.OS,
                    path_components=["foo", "bar"])))

        self.assertEqual(n.reference.type, "VFS")
        self.assertEqual(n.reference.vfs.client_id.ToString(), self.client_id)
        self.assertEqual(n.reference.vfs.vfs_path, "fs/os/foo/bar")
Ejemplo n.º 2
0
    def testVfsNotificationWithInvalidReferenceIsParsedDefensively(self):
        n = self.InitFromObj_(
            rdf_objects.UserNotification.Type.TYPE_VFS_FILE_COLLECTED,
            rdf_objects.ObjectReference(
                reference_type=rdf_objects.ObjectReference.Type.VFS_FILE,
                vfs_file=rdf_objects.VfsFileReference(
                    client_id=self.client_id,
                    # UNSET path type is an invalid value here:
                    # it make it impossible to find the file.
                    path_type=rdf_objects.PathInfo.PathType.UNSET,
                    path_components=["foo", "bar"])))

        self.assertEqual(n.reference.type, "UNSET")
Ejemplo n.º 3
0
def _SendNotifications(username, client_id):
    with test_lib.FakeTime(42):
        notification.Notify(
            username,
            rdf_objects.UserNotification.Type.TYPE_CLIENT_INTERROGATED,
            "<some message>",
            rdf_objects.ObjectReference(
                reference_type=rdf_objects.ObjectReference.Type.CLIENT,
                client=rdf_objects.ClientReference(
                    client_id=client_id.Basename())))

    with test_lib.FakeTime(44):
        notification.Notify(
            username,
            rdf_objects.UserNotification.Type.TYPE_VFS_FILE_COLLECTION_FAILED,
            "<some other message>",
            rdf_objects.ObjectReference(
                reference_type=rdf_objects.ObjectReference.Type.VFS_FILE,
                vfs_file=rdf_objects.VfsFileReference(
                    client_id=client_id.Basename(),
                    path_type=rdf_objects.PathInfo.PathType.OS,
                    path_components=["foo"])))
Ejemplo n.º 4
0
  def NotifyCreatorOfError(self):
    if self.ShouldSendNotifications():
      client_id = self.rdf_flow.client_id
      flow_id = self.rdf_flow.flow_id

      flow_ref = None
      flow_ref = rdf_objects.FlowReference(client_id=client_id, flow_id=flow_id)
      notification_lib.Notify(
          self.creator, rdf_objects.UserNotification.Type.TYPE_FLOW_RUN_FAILED,
          "Flow %s on %s terminated due to error" % (flow_id, client_id),
          rdf_objects.ObjectReference(
              reference_type=rdf_objects.ObjectReference.Type.FLOW,
              flow=flow_ref))
Ejemplo n.º 5
0
  def testVfsListDirectoryCompletedIsParsedCorrectly(self):
    n = self.InitFromObj_(
        rdf_objects.UserNotification.Type.TYPE_VFS_LIST_DIRECTORY_COMPLETED,
        rdf_objects.ObjectReference(
            reference_type=rdf_objects.ObjectReference.Type.VFS_FILE,
            vfs_file=rdf_objects.VfsFileReference(
                client_id=self.client_id.Basename(),
                path_type=rdf_objects.PathInfo.PathType.OS,
                path_components=["foo", "bar"])))

    self.assertEqual(n.reference.type, "VFS")
    self.assertEqual(n.reference.vfs.client_id.ToClientURN(), self.client_id)
    self.assertEqual(n.reference.vfs.vfs_path, "fs/os/foo/bar")
Ejemplo n.º 6
0
  def testHuntApprovalNotificationIsParsedCorrectly(self):
    n = self.InitFromObj_(
        rdf_objects.UserNotification.Type.TYPE_HUNT_APPROVAL_REQUESTED,
        rdf_objects.ObjectReference(
            reference_type=rdf_objects.ObjectReference.Type.APPROVAL_REQUEST,
            approval_request=rdf_objects.ApprovalRequestReference(
                approval_type=rdf_objects.ApprovalRequest.ApprovalType
                .APPROVAL_TYPE_HUNT,
                approval_id="foo-bar",
                subject_id="H:123456",
                requestor_username=self.context.username)))

    self.assertEqual(n.reference.type, "HUNT_APPROVAL")
    self.assertEqual(n.reference.hunt_approval.hunt_id, "H:123456")
    self.assertEqual(n.reference.hunt_approval.username, self.context.username)
    self.assertEqual(n.reference.hunt_approval.approval_id, "foo-bar")
Ejemplo n.º 7
0
  def NotifyAboutEnd(self):
    """Send out a final notification about the end of this flow."""
    flow_ref = None
    if self.runner_args.client_id:
      flow_ref = rdf_objects.FlowReference(
          client_id=self.client_id, flow_id=self.urn.Basename())

    num_results = len(self.ResultCollection())
    notification_lib.Notify(
        self.creator, rdf_objects.UserNotification.Type.TYPE_FLOW_RUN_COMPLETED,
        "Flow %s completed with %d %s" % (self.__class__.__name__, num_results,
                                          num_results == 1 and "result" or
                                          "results"),
        rdf_objects.ObjectReference(
            reference_type=rdf_objects.ObjectReference.Type.FLOW,
            flow=flow_ref))
Ejemplo n.º 8
0
 def NotifyAboutEnd(self):
   # Sum up number of replies to write with the number of already
   # written results.
   num_results = (
       len(self.replies_to_write) + data_store.REL_DB.CountFlowResults(
           self.rdf_flow.client_id, self.rdf_flow.flow_id))
   flow_ref = rdf_objects.FlowReference(
       client_id=self.rdf_flow.client_id, flow_id=self.rdf_flow.flow_id)
   notification_lib.Notify(
       self.creator, rdf_objects.UserNotification.Type.TYPE_FLOW_RUN_COMPLETED,
       "Flow %s completed with %d %s" % (self.__class__.__name__, num_results,
                                         num_results == 1 and "result" or
                                         "results"),
       rdf_objects.ObjectReference(
           reference_type=rdf_objects.ObjectReference.Type.FLOW,
           flow=flow_ref))
Ejemplo n.º 9
0
    def Error(self, backtrace, client_id=None, status_code=None):
        """Terminates this flow with an error."""
        try:
            self.queue_manager.DestroyFlowStates(self.session_id)
        except queue_manager.MoreDataException:
            pass

        if not self.IsRunning():
            return

        # Set an error status
        reply = rdf_flows.GrrStatus()
        if status_code is None:
            reply.status = rdf_flows.GrrStatus.ReturnedStatus.GENERIC_ERROR
        else:
            reply.status = status_code

        client_id = client_id or self.runner_args.client_id

        if backtrace:
            reply.error_message = backtrace
            logging.error("Error in flow %s (%s). Trace: %s", self.session_id,
                          client_id, backtrace)
            self.context.backtrace = backtrace
        else:
            logging.error("Error in flow %s (%s).", self.session_id, client_id)

        self._SendTerminationMessage(reply)

        self.context.state = rdf_flow_runner.FlowContext.State.ERROR

        if self.ShouldSendNotifications():
            flow_ref = None
            if client_id:
                flow_ref = rdf_objects.FlowReference(
                    client_id=client_id.Basename(),
                    flow_id=self.session_id.Basename())
            notification_lib.Notify(
                self.token.username,
                rdf_objects.UserNotification.Type.TYPE_FLOW_RUN_FAILED,
                "Flow (%s) terminated due to error" % self.session_id,
                rdf_objects.ObjectReference(
                    reference_type=rdf_objects.ObjectReference.Type.FLOW,
                    flow=flow_ref))

        self.flow_obj.Flush()
Ejemplo n.º 10
0
  def testCronJobNotificationIsShownAndClickable(self):
    notification.Notify(
        self.token.username,
        rdf_objects.UserNotification.Type.TYPE_CRON_JOB_APPROVAL_GRANTED,
        "Test CronJob notification",
        rdf_objects.ObjectReference(
            reference_type=rdf_objects.ObjectReference.Type.CRON_JOB,
            cron_job=rdf_objects.CronJobReference(cron_job_id=u"OSBreakDown")))

    self.Open("/")

    self.Click("css=button[id=notification_button]")
    self.Click("css=a:contains('Test CronJob notification')")

    self.WaitUntil(self.IsElementPresent,
                   "css=tr.row-selected td:contains('OSBreakDown')")
    self.WaitUntil(self.IsElementPresent, "css=dd:contains('OSBreakDown')")
Ejemplo n.º 11
0
  def testCronJobApprovalNotificationIsParsedCorrectly(self):
    n = self.InitFromObj_(
        rdf_objects.UserNotification.Type.TYPE_CRON_JOB_APPROVAL_REQUESTED,
        rdf_objects.ObjectReference(
            reference_type=rdf_objects.ObjectReference.Type.APPROVAL_REQUEST,
            approval_request=rdf_objects.ApprovalRequestReference(
                approval_type=rdf_objects.ApprovalRequest.ApprovalType
                .APPROVAL_TYPE_CRON_JOB,
                approval_id="foo-bar",
                subject_id="FooBar",
                requestor_username=self.context.username)))

    self.assertEqual(n.reference.type, "CRON_JOB_APPROVAL")
    self.assertEqual(n.reference.cron_job_approval.cron_job_id, "FooBar")
    self.assertEqual(n.reference.cron_job_approval.username,
                     self.context.username)
    self.assertEqual(n.reference.cron_job_approval.approval_id, "foo-bar")
Ejemplo n.º 12
0
  def testClientApprovalNotificationIsParsedCorrectly(self):
    n = self.InitFromObj_(
        rdf_objects.UserNotification.Type.TYPE_CLIENT_APPROVAL_REQUESTED,
        rdf_objects.ObjectReference(
            reference_type=rdf_objects.ObjectReference.Type.APPROVAL_REQUEST,
            approval_request=rdf_objects.ApprovalRequestReference(
                approval_type=rdf_objects.ApprovalRequest.ApprovalType
                .APPROVAL_TYPE_CLIENT,
                approval_id="foo-bar",
                subject_id=self.client_id,
                requestor_username=self.context.username)))

    self.assertEqual(n.reference.type, "CLIENT_APPROVAL")

    client_approval = n.reference.client_approval
    self.assertEqual(client_approval.client_id.ToString(), self.client_id)
    self.assertEqual(client_approval.username, self.context.username)
    self.assertEqual(client_approval.approval_id, "foo-bar")
Ejemplo n.º 13
0
    def NotifyAboutEnd(self):
        """Sends a notification that this flow is done."""
        if not self.state.urn:
            super(ListDirectoryMixin, self).NotifyAboutEnd()
            return

        components = self.state.urn.Split()
        file_ref = None
        if len(components) > 3:
            file_ref = rdf_objects.VfsFileReference(
                client_id=components[0],
                path_type=components[2].upper(),
                path_components=components[3:])
        notification.Notify(
            self.token.username, rdf_objects.UserNotification.Type.
            TYPE_VFS_LIST_DIRECTORY_COMPLETED,
            "Listed {0}".format(self.args.pathspec.CollapsePath()),
            rdf_objects.ObjectReference(
                reference_type=rdf_objects.ObjectReference.Type.VFS_FILE,
                vfs_file=file_ref))
Ejemplo n.º 14
0
    def _WrapContentGenerator(self, generator, args, username):
        try:
            for item in generator:
                yield item
        except Exception as e:
            path_type, components = rdf_objects.ParseCategorizedPath(
                args.file_path)
            vfs_file_ref = rdf_objects.VfsFileReference(
                client_id=args.client_id,
                path_type=path_type,
                path_components=components)
            object_reference = rdf_objects.ObjectReference(
                reference_type=rdf_objects.ObjectReference.Type.VFS_FILE,
                vfs_file=vfs_file_ref)

            notification.Notify(
                username,
                rdf_objects.UserNotification.Type.TYPE_FILE_BLOB_FETCH_FAILED,
                "File blob fetch failed for path %s on client %s: %s" %
                (args.client_id, args.file_path, e), object_reference)
            raise
Ejemplo n.º 15
0
  def NotifyAboutEnd(self):
    if not self.runner.ShouldSendNotifications():
      return

    if self.state.urn:
      components = self.state.urn.Split()
      file_ref = None
      if len(components) > 3:
        file_ref = rdf_objects.VfsFileReference(
            client_id=components[0],
            path_type=components[2].upper(),
            path_components=components[3:])
      notification.Notify(
          self.token.username,
          notification.UserNotification.Type.TYPE_VFS_LIST_DIRECTORY_COMPLETED,
          "List of {0} completed.".format(utils.SmartStr(self.args.pathspec)),
          rdf_objects.ObjectReference(
              reference_type=rdf_objects.ObjectReference.Type.VFS_FILE,
              vfs_file=file_ref))
    else:
      super(IteratedListDirectory, self).NotifyAboutEnd()
Ejemplo n.º 16
0
def StopHunt(hunt_id, reason=None):
  """Stops a hunt with a given id."""

  hunt_obj = data_store.REL_DB.ReadHuntObject(hunt_id)
  if hunt_obj.hunt_state not in [
      hunt_obj.HuntState.STARTED, hunt_obj.HuntState.PAUSED
  ]:
    raise OnlyStartedOrPausedHuntCanBeStoppedError(hunt_obj)

  data_store.REL_DB.UpdateHuntObject(
      hunt_id, hunt_state=hunt_obj.HuntState.STOPPED, hunt_state_comment=reason)
  data_store.REL_DB.RemoveForemanRule(hunt_id=hunt_obj.hunt_id)

  if (reason is not None and
      hunt_obj.creator not in access_control.SYSTEM_USERS):
    notification.Notify(
        hunt_obj.creator, rdf_objects.UserNotification.Type.TYPE_HUNT_STOPPED,
        reason,
        rdf_objects.ObjectReference(
            reference_type=rdf_objects.ObjectReference.Type.HUNT,
            hunt=rdf_objects.HuntReference(hunt_id=hunt_obj.hunt_id)))

  return data_store.REL_DB.ReadHuntObject(hunt_id)
Ejemplo n.º 17
0
 def NotifyAboutEnd(self):
   notification_lib.Notify(
       self.creator, rdf_objects.UserNotification.Type.TYPE_FLOW_RUN_COMPLETED,
       "FlowWithCustomNotifyAboutEnd completed.",
       rdf_objects.ObjectReference())
Ejemplo n.º 18
0
 def ObjectReference(self):
     return rdf_objects.ObjectReference(
         reference_type=rdf_objects.ObjectReference.Type.CLIENT,
         client=rdf_objects.ClientReference(
             client_id=utils.SmartStr(self.client_id)))
Ejemplo n.º 19
0
 def ObjectReference(self):
     return rdf_objects.ObjectReference(
         reference_type=rdf_objects.ObjectReference.Type.CRON_JOB,
         cron_job=rdf_objects.CronJobReference(
             cron_job_id=str(self.cron_job_id)))
Ejemplo n.º 20
0
 def ObjectReference(self):
   return rdf_objects.ObjectReference(
       reference_type=rdf_objects.ObjectReference.Type.HUNT,
       hunt=rdf_objects.HuntReference(hunt_id=str(self.hunt_id)))