Exemplo n.º 1
0
    def testCredentialGetSet(self):
        """Tests the GetCredential and SetCredential functions."""
        test_keychain = keychain.KeyChain()

        fake_path_spec = factory.Factory.NewPathSpec(
            definitions.TYPE_INDICATOR_FAKE, location='/test')
        bde_path_spec = factory.Factory.NewPathSpec(
            definitions.TYPE_INDICATOR_BDE, parent=fake_path_spec)

        with self.assertRaises(AttributeError):
            test_keychain.SetCredential(fake_path_spec, 'password', 'TEST')

        test_keychain.SetCredential(bde_path_spec, 'password', 'TEST')

        credential = test_keychain.GetCredential(fake_path_spec, 'password')
        self.assertIsNone(credential)

        credential = test_keychain.GetCredential(bde_path_spec, 'password')
        self.assertEqual(credential, 'TEST')

        credentials = test_keychain.GetCredentials(bde_path_spec)
        self.assertEqual(credentials, {'password': '******'})
Exemplo n.º 2
0
class Resolver(object):
  """Path specification resolver."""

  _resolver_context = context.Context()
  _resolver_helpers_manager = None

  key_chain = keychain.KeyChain()

  @classmethod
  def _GetResolverHelper(cls, type_indicator):
    """Retrieves the path specification resolver helper for the specified type.

    Args:
      type_indicator (str): type indicator.

    Returns:
      ResolverHelper: a resolver helper.
    """
    if not cls._resolver_helpers_manager:
      # Delay the import of the resolver helpers manager to prevent circular
      # imports.
      from dfvfs.resolver_helpers import manager

      cls._resolver_helpers_manager = manager.ResolverHelperManager

    return cls._resolver_helpers_manager.GetHelper(type_indicator)

  @classmethod
  def OpenFileEntry(cls, path_spec_object, resolver_context=None):
    """Opens a file entry object defined by path specification.

    Args:
      path_spec_object (PathSpec): path specification.
      resolver_context (Optional[Context]): resolver context, where None
          represents the built in context which is not multi process safe.

    Returns:
      FileEntry: file entry or None if the path specification could not be
          resolved.
    """
    file_system = cls.OpenFileSystem(
        path_spec_object, resolver_context=resolver_context)

    if resolver_context is None:
      resolver_context = cls._resolver_context

    file_entry = file_system.GetFileEntryByPathSpec(path_spec_object)

    # Release the file system so it will be removed from the cache
    # when the file entry is destroyed.
    resolver_context.ReleaseFileSystem(file_system)

    return file_entry

  @classmethod
  def OpenFileObject(cls, path_spec_object, resolver_context=None):
    """Opens a file-like object defined by path specification.

    Args:
      path_spec_object (PathSpec): path specification.
      resolver_context (Optional[Context]): resolver context, where None
          represents the built in context which is not multi process safe.

    Returns:
      FileIO: file-like object or None if the path specification could not
          be resolved.

    Raises:
      PathSpecError: if the path specification is incorrect.
      TypeError: if the path specification type is unsupported.
    """
    if not isinstance(path_spec_object, path_spec.PathSpec):
      raise TypeError('Unsupported path specification type.')

    if resolver_context is None:
      resolver_context = cls._resolver_context

    if path_spec_object.type_indicator == definitions.TYPE_INDICATOR_MOUNT:
      if path_spec_object.HasParent():
        raise errors.PathSpecError(
            'Unsupported mount path specification with parent.')

      mount_point = getattr(path_spec_object, 'identifier', None)
      if not mount_point:
        raise errors.PathSpecError(
            'Unsupported path specification without mount point identifier.')

      path_spec_object = mount_manager.MountPointManager.GetMountPoint(
          mount_point)
      if not path_spec_object:
        raise errors.MountPointError(
            'No such mount point: {0:s}'.format(mount_point))

    file_object = resolver_context.GetFileObject(path_spec_object)
    if not file_object:
      resolver_helper = cls._GetResolverHelper(path_spec_object.type_indicator)
      file_object = resolver_helper.NewFileObject(resolver_context)

    file_object.open(path_spec=path_spec_object)
    return file_object

  @classmethod
  def OpenFileSystem(cls, path_spec_object, resolver_context=None):
    """Opens a file system object defined by path specification.

    Args:
      path_spec_object (PathSpec): path specification.
      resolver_context (Optional[Context]): resolver context, where None
          represents the built in context which is not multi process safe.

    Returns:
      FileSystem: file system or None if the path specification could not
          be resolved or has no file system object.

    Raises:
      AccessError: if the access to open the file system was denied.
      BackEndError: if the file system cannot be opened.
      PathSpecError: if the path specification is incorrect.
      TypeError: if the path specification type is unsupported.
    """
    if not isinstance(path_spec_object, path_spec.PathSpec):
      raise TypeError('Unsupported path specification type.')

    if resolver_context is None:
      resolver_context = cls._resolver_context

    if path_spec_object.type_indicator == definitions.TYPE_INDICATOR_MOUNT:
      if path_spec_object.HasParent():
        raise errors.PathSpecError(
            'Unsupported mount path specification with parent.')

      mount_point = getattr(path_spec_object, 'identifier', None)
      if not mount_point:
        raise errors.PathSpecError(
            'Unsupported path specification without mount point identifier.')

      path_spec_object = mount_manager.MountPointManager.GetMountPoint(
          mount_point)
      if not path_spec_object:
        raise errors.MountPointError(
            'No such mount point: {0:s}'.format(mount_point))

    file_system = resolver_context.GetFileSystem(path_spec_object)
    if not file_system:
      resolver_helper = cls._GetResolverHelper(path_spec_object.type_indicator)
      file_system = resolver_helper.NewFileSystem(resolver_context)

    try:
      file_system.Open(path_spec_object)
    except (errors.AccessError, errors.PathSpecError):
      raise
    except (IOError, ValueError) as exception:
      raise errors.BackEndError(
          'Unable to open file system with error: {0:s}'.format(exception))

    return file_system
Exemplo n.º 3
0
class Resolver(object):
    """Class that implements the path specification resolver."""

    _resolver_context = context.Context()
    _resolver_helpers = {}

    key_chain = keychain.KeyChain()

    @classmethod
    def DeregisterHelper(cls, resolver_helper):
        """Deregisters a path specification resolver helper.

    Args:
      resolver_helper: the resolver helper object (instance of
                       ResolverHelper).

    Raises:
      KeyError: if resolver helper object is not set for the corresponding
                type indicator.
    """
        if resolver_helper.type_indicator not in cls._resolver_helpers:
            raise KeyError(
                u'Resolver helper object not set for type indicator: {0:s}.'.
                format(resolver_helper.type_indicator))

        del cls._resolver_helpers[resolver_helper.type_indicator]

    @classmethod
    def OpenFileEntry(cls, path_spec_object, resolver_context=None):
        """Opens a file entry object defined by path specification.

    Args:
      path_spec_object: the path specification (instance of PathSpec).
      resolver_context: the optional resolver context (instance of
                        resolver.Context). The default is None which will use
                        the built in context which is not multi process safe.

    Returns:
      The file entry object (instance of vfs.FileEntry) or None if the path
      specification could not be resolved.
    """
        file_system = cls.OpenFileSystem(path_spec_object,
                                         resolver_context=resolver_context)

        if resolver_context is None:
            resolver_context = cls._resolver_context

        file_entry = file_system.GetFileEntryByPathSpec(path_spec_object)

        # Release the file system so it will be removed from the cache
        # when the file entry is destroyed.
        resolver_context.ReleaseFileSystem(file_system)

        return file_entry

    @classmethod
    def OpenFileObject(cls, path_spec_object, resolver_context=None):
        """Opens a file-like object defined by path specification.

    Args:
      path_spec_object: the path specification (instance of PathSpec).
      resolver_context: the optional resolver context (instance of
                        resolver.Context). The default is None which will use
                        the built in context which is not multi process safe.

    Returns:
      The file-like object (instance of file.FileIO) or None if the path
      specification could not be resolved.

    Raises:
      KeyError: if resolver helper object is not set for the corresponding
                type indicator.
      PathSpecError: if the path specification is incorrect.
      TypeError: if the path specification type is unsupported.
    """
        if not isinstance(path_spec_object, path_spec.PathSpec):
            raise TypeError(u'Unsupported path specification type.')

        if resolver_context is None:
            resolver_context = cls._resolver_context

        if path_spec_object.type_indicator == definitions.TYPE_INDICATOR_MOUNT:
            if path_spec_object.HasParent():
                raise errors.PathSpecError(
                    u'Unsupported mount path specification with parent.')

            mount_point = getattr(path_spec_object, u'identifier', None)
            if not mount_point:
                raise errors.PathSpecError(
                    u'Unsupported path specification without mount point identifier.'
                )

            path_spec_object = mount_manager.MountPointManager.GetMountPoint(
                mount_point)
            if not path_spec_object:
                raise errors.MountPointError(
                    u'No such mount point: {0:s}'.format(mount_point))

        file_object = resolver_context.GetFileObject(path_spec_object)
        if not file_object:
            if path_spec_object.type_indicator not in cls._resolver_helpers:
                raise KeyError(
                    (u'Resolver helper object not set for type indicator: '
                     u'{0:s}.').format(path_spec_object.type_indicator))

            resolver_helper = cls._resolver_helpers[
                path_spec_object.type_indicator]
            file_object = resolver_helper.NewFileObject(resolver_context)

        file_object.open(path_spec=path_spec_object)
        return file_object

    @classmethod
    def OpenFileSystem(cls, path_spec_object, resolver_context=None):
        """Opens a file system object defined by path specification.

    Args:
      path_spec_object: the path specification (instance of PathSpec).
      resolver_context: the optional resolver context (instance of
                        resolver.Context). The default is None which will use
                        the built in context which is not multi process safe.

    Returns:
      The file system object (instance of vfs.FileSystem) or None if the path
      specification could not be resolved or has no file system object.

    Raises:
      AccessError: if the access to open the file system was denied.
      BackEndError: if the file system cannot be opened.
      KeyError: if resolver helper object is not set for the corresponding
                type indicator.
      PathSpecError: if the path specification is incorrect.
      TypeError: if the path specification type is unsupported.
    """
        if not isinstance(path_spec_object, path_spec.PathSpec):
            raise TypeError(u'Unsupported path specification type.')

        if resolver_context is None:
            resolver_context = cls._resolver_context

        if path_spec_object.type_indicator == definitions.TYPE_INDICATOR_MOUNT:
            if path_spec_object.HasParent():
                raise errors.PathSpecError(
                    u'Unsupported mount path specification with parent.')

            mount_point = getattr(path_spec_object, u'identifier', None)
            if not mount_point:
                raise errors.PathSpecError(
                    u'Unsupported path specification without mount point identifier.'
                )

            path_spec_object = mount_manager.MountPointManager.GetMountPoint(
                mount_point)
            if not path_spec_object:
                raise errors.MountPointError(
                    u'No such mount point: {0:s}'.format(mount_point))

        file_system = resolver_context.GetFileSystem(path_spec_object)
        if not file_system:
            if path_spec_object.type_indicator not in cls._resolver_helpers:
                raise KeyError(
                    (u'Resolver helper object not set for type indicator: '
                     u'{0:s}.').format(path_spec_object.type_indicator))

            resolver_helper = cls._resolver_helpers[
                path_spec_object.type_indicator]
            file_system = resolver_helper.NewFileSystem(resolver_context)

        try:
            file_system.Open(path_spec_object)
        except (errors.AccessError, errors.PathSpecError):
            raise
        except (IOError, ValueError) as exception:
            raise errors.BackEndError(
                u'Unable to open file system with error: {0:s}'.format(
                    exception))

        return file_system

    @classmethod
    def RegisterHelper(cls, resolver_helper):
        """Registers a path specification resolver helper.

    Args:
      resolver_helper: the resolver helper object (instance of
                       ResolverHelper).

    Raises:
      KeyError: if resolver helper object is already set for the corresponding
                type indicator.
    """
        if resolver_helper.type_indicator in cls._resolver_helpers:
            raise KeyError(
                (u'Resolver helper object already set for type indicator: '
                 u'{0!s}.').format(resolver_helper.type_indicator))

        cls._resolver_helpers[resolver_helper.type_indicator] = resolver_helper