Esempio n. 1
0
    def testHandleMessage(self):
        """Test handling of a normal request with a response."""
        args = rdf_client.LogMessage(data="hello")
        # Push a request on it
        message = rdf_flows.GrrMessage(
            name="MockAction",
            session_id=self.session_id,
            auth_state=rdf_flows.GrrMessage.AuthorizationState.AUTHENTICATED,
            payload=args,
            request_id=1,
            generate_task_id=True)

        with mock.patch.object(client_actions, "REGISTRY",
                               {"MockAction": MockAction}):
            self.context.HandleMessage(message)

        # Check the response - one data and one status

        message_list = self.context.Drain().job

        self.assertEqual(message_list[0].session_id, self.session_id)
        self.assertEqual(message_list[0].response_id, 1)
        self.assertIn("hello", message_list[0].payload.data)
        self.assertEqual(message_list[1].response_id, 2)
        self.assertEqual(message_list[1].type,
                         rdf_flows.GrrMessage.Type.STATUS)
Esempio n. 2
0
 def MockSendReply(unused_self, reply=None, **kwargs):
     results.append(reply or rdf_client.LogMessage(**kwargs))
Esempio n. 3
0
    def Run(self, args):
        """Delete all the GRR temp files in path.

    If path is a directory, look in the top level for filenames beginning with
    Client.tempfile_prefix, and delete them.

    If path is a regular file and starts with Client.tempfile_prefix delete it.

    Args:
      args: pathspec pointing to directory containing temp files to be
            deleted, or a single file to be deleted.
    Returns:
      deleted: array of filename strings that were deleted
    Raises:
      ErrorBadPath: if path doesn't exist or is not a regular file or directory
    """

        allowed_temp_dirs = [
            GetTempDirForRoot(root)
            for root in config.CONFIG["Client.tempdir_roots"]
        ]

        if args.path:
            # Normalize the path, so DeleteGRRTempFile can correctly check if
            # it is within Client.tempdir.
            normal_path = utils.NormalizePath(args.path)
            local_path = client_utils.CanonicalPathToLocalPath(normal_path)

            paths = [local_path]
        else:
            paths = allowed_temp_dirs

        deleted = []
        errors = []
        for path in paths:
            if os.path.isdir(path):
                for filename in os.listdir(path):
                    abs_filename = os.path.join(path, filename)

                    try:
                        DeleteGRRTempFile(abs_filename)
                        deleted.append(abs_filename)
                    except Exception as e:  # pylint: disable=broad-except
                        # The error we are most likely to get is ErrorNotTempFile but
                        # especially on Windows there might be locking issues that raise
                        # various WindowsErrors so we just catch them all and continue
                        # deleting all other temp files in this directory.
                        errors.append(e)

            elif os.path.isfile(path):
                DeleteGRRTempFile(path)
                deleted = [path]

            elif path not in allowed_temp_dirs:
                if not os.path.exists(path):
                    raise ErrorBadPath("File %s does not exist" % path)
                else:
                    raise ErrorBadPath("Not a regular file or directory: %s" %
                                       path)

        reply = ""
        if deleted:
            reply = "Deleted: %s." % deleted
        else:
            reply = "Nothing deleted."
        if errors:
            reply += "\n%s" % errors

        self.SendReply(rdf_client.LogMessage(data=reply))
Esempio n. 4
0
  def Run(self, args):
    """Delete all the GRR temp files in path.

    If path is a directory, look in the top level for filenames beginning with
    Client.tempfile_prefix, and delete them.

    If path is a regular file and starts with Client.tempfile_prefix delete it.

    Args:
      args: pathspec pointing to directory containing temp files to be
            deleted, or a single file to be deleted.
    Returns:
      deleted: array of filename strings that were deleted
    Raises:
      ErrorBadPath: if path doesn't exist or is not a regular file or directory
    """

    allowed_temp_dirs = [
        GetTempDirForRoot(root)
        for root in config.CONFIG["Client.tempdir_roots"]
    ]

    if args.path:
      # Normalize the path, so DeleteGRRTempFile can correctly check if
      # it is within Client.tempdir.
      path = utils.NormalizePath(args.path)
      if platform.system() == "Windows":
        # TODO: On non-Windows systems `CanonicalPathToLocalPath`
        # is equivalent to `SmartStr`, so it does nothing except for breaking
        # the types. However, a lot of code actually depends on this behaviour
        # so we cannot easily change it. As a workaround for now we simply do
        # not call it on Linux and macOS but ideally we should get rid of this
        # `SmartStr` call and not branch here.
        path = client_utils.CanonicalPathToLocalPath(path)

      paths = [path]
    else:
      paths = allowed_temp_dirs

    deleted = []
    errors = []
    for path in paths:
      if os.path.isdir(path):
        for filename in os.listdir(path):
          abs_filename = os.path.join(path, filename)

          try:
            DeleteGRRTempFile(abs_filename)
            deleted.append(abs_filename)
          except Exception as e:  # pylint: disable=broad-except
            # The error we are most likely to get is ErrorNotTempFile but
            # especially on Windows there might be locking issues that raise
            # various WindowsErrors so we just catch them all and continue
            # deleting all other temp files in this directory.
            errors.append(e)

      elif os.path.isfile(path):
        DeleteGRRTempFile(path)
        deleted = [path]

      elif path not in allowed_temp_dirs:
        if not os.path.exists(path):
          raise ErrorBadPath("File %s does not exist" % path)
        else:
          raise ErrorBadPath("Not a regular file or directory: %s" % path)

    reply = ""
    if deleted:
      reply = "Deleted: %s." % deleted
    else:
      reply = "Nothing deleted."
    if errors:
      reply += "\n%s" % errors

    self.SendReply(rdf_client.LogMessage(data=reply))