def __init__(self, session=None, **kwargs):
        cache_dir = session.GetParameter("cache_dir")

        if not cache_dir:
            raise io_manager.IOManagerError(
                "Local profile cache is not configured - "
                "add a cache_dir parameter to ~/.rekallrc.")

        # Cache dir may be specified relative to the home directory.
        if config.GetHomeDir():
            cache_dir = os.path.join(config.GetHomeDir(), cache_dir)

        if not os.access(cache_dir, os.F_OK | os.R_OK | os.W_OK | os.X_OK):
            try:
                os.makedirs(cache_dir)
            except (IOError, OSError):
                raise io_manager.IOManagerError(
                    "Unable to create or access cache directory %s" %
                    cache_dir)

        # We use an IO manager to manage the cache directory directly.
        self.cache_io_manager = io_manager.DirectoryIOManager(urn=cache_dir,
                                                              session=session)
        self.url_manager = self.DELEGATE(session=session, **kwargs)

        self.CheckUpstreamRepository()

        super(CachingManager, self).__init__(session=session, **kwargs)
Example #2
0
 def __init__(self, spec=None, root="./", manager=None, **kwargs):
     super(BuildIndex, self).__init__(**kwargs)
     self.spec = spec
     if manager is None:
         manager = io_manager.DirectoryIOManager(
             root, session=self.session)
     self.io_manager = manager
Example #3
0
    def __init__(self, session=None, **kwargs):
        super(CachingManager, self).__init__(session=session, **kwargs)

        cache_dir = cache.GetCacheDir(session)

        # We use an IO manager to manage the cache directory directly.
        self.cache_io_manager = io_manager.DirectoryIOManager(urn=cache_dir,
                                                              session=session)
        self.url_manager = self.DELEGATE(session=session, **kwargs)

        self.CheckUpstreamRepository()
Example #4
0
    def testDirectoryIOManager(self):
        manager = io_manager.DirectoryIOManager(self.temp_directory,
                                                session=self.MakeUserSession())

        # Cant decode from json.
        self.assertEqual(manager.GetData("foo"), None)
        self.assertEqual(manager.GetData("foo", raw=True), "hello")

        # Test ListFiles().
        self.assertListEqual(sorted(manager.ListFiles()), ["bar", "foo"])

        # Storing a data structure.
        data = dict(a=1)
        manager.StoreData("baz", data)
        self.assertDictEqual(manager.GetData("baz"), data)

        self.assertTrue(
            isinstance(manager.GetData("baz", raw=True), basestring))
Example #5
0
    def repository_managers(self):
        """The IO managers that are used to fetch profiles from the profile
        repository.

        """
        if self._repository_managers:
            return self._repository_managers

        # The profile path is specified in search order.
        repository_path = (self.GetParameter("repository_path")
                           or self.GetParameter("profile_path") or [])

        for path in repository_path:
            # TODO(scudette): remove this hack for 1.6 release.  Github has
            # changed their static URL access. If the user is using an old URL
            # we warn and correct it.
            if path in constants.OLD_DEPRECATED_URLS:
                self.logging.warn(
                    "Rekall's profile repository is pointing to deprecated URL "
                    "(%s). Please update your ~/.rekallrc file.", path)
                path = constants.PROFILE_REPOSITORIES[0]

            try:
                self._repository_managers.append(
                    (path, io_manager.Factory(path, session=self)))
            except ValueError:
                pass

        if not self._repository_managers:
            try:
                self.logging.warn(
                    "No usable repositories were found. "
                    "Rekall Will attempt to use the local cache. "
                    "This is likely to fail if profiles are missing locally!")
                self._repository_managers = [
                    (None,
                     io_manager.DirectoryIOManager(urn=cache.GetCacheDir(self),
                                                   session=self))
                ]
            except IOError:
                self._repository_managers = []

        return self._repository_managers
Example #6
0
    def LoadProfile(self, name, use_cache=True):
        """Try to load a profile directly by its name.

        Args:

          name: A string which represents the canonical name for the profile. We
              ask all repositories in the repository_path to resolve this name
              into a profile.

        Returns:
          a Profile() instance or a NoneObject()

        """
        if not name:
            return obj.NoneObject("No filename")

        if isinstance(name, obj.Profile):
            return name

        # We only want to deal with unix paths.
        name = name.replace("\\", "/")

        try:
            if use_cache:
                cached_profile = self.profile_cache[name]
                if cached_profile:
                    return cached_profile

                else:
                    return obj.NoneObject(
                        "Unable to load profile %s from any repository." %
                        name)

        except KeyError:
            pass

        result = None
        try:
            # If the name is a path we try to open it directly:
            container = io_manager.DirectoryIOManager(os.path.dirname(name),
                                                      version=None,
                                                      session=self)
            data = container.GetData(os.path.basename(name))
            if data == None:
                raise IOError("Not found.")

            result = obj.Profile.LoadProfileFromData(data, self, name=name)
        except IOError:
            pass

        # Traverse the profile path until one works.
        if not result:
            # Add the last supported repository as the last fallback path.
            for path, manager in self.repository_managers:
                try:
                    # The inventory allows us to fail fetching the profile
                    # quickly - without making the round trip.
                    if not manager.CheckInventory(name):
                        self.logging.debug(
                            "Skipped profile %s from %s (Not in inventory)",
                            name, path)
                        continue

                    now = time.time()
                    result = obj.Profile.LoadProfileFromData(
                        manager.GetData(name), session=self, name=name)
                    if result:
                        self.logging.info(
                            "Loaded profile %s from %s (in %s sec)", name,
                            manager,
                            time.time() - now)
                        break

                except (IOError, KeyError) as e:
                    result = obj.NoneObject(e)
                    self.logging.debug("Could not find profile %s in %s: %s",
                                       name, path, e)

                    continue

        # Cache it for later. Note that this also caches failures so we do not
        # retry again.
        self.profile_cache[name] = result
        if result == None:
            return obj.NoneObject(
                "Unable to load profile %s from any repository." % name)

        return result