Example #1
0
    def add_service(self, service):
        """Add the passed service to this wallet"""
        from Acquire.Service import Service as _Service
        from Acquire.ObjectStore import string_to_safestring \
            as _string_to_safestring

        s = _Service.resolve(service, fetch=True)
        service = s["service"]

        service_dir = Wallet._get_service_dir(service.uid())
        service_file = "%s/service_%s.json" % (
            service_dir, _string_to_safestring(service.canonical_url()))
        _write_service(service=service, filename=service_file)
Example #2
0
    def remove_service(self, service):
        """Remove the cached service info for the passed service"""
        if isinstance(service, str):
            service_url = service
        else:
            service_url = service.canonical_url()

        from Acquire.ObjectStore import string_to_safestring \
            as _string_to_safestring

        service_file = "%s/service_%s" % (
            self._wallet_dir,
            _string_to_safestring(service_url))

        import os as _os

        if _os.path.exists(service_file):
            _os.unlink(service_file)
Example #3
0
    def remove_service(self, service):
        """Remove the cached service info for the passed service"""
        if isinstance(service, str):
            try:
                service = self.get_service(service)
            except:
                return

        service_url = service.canonical_url()

        from Acquire.ObjectStore import string_to_safestring \
            as _string_to_safestring

        service_dir = Wallet._get_service_dir(service.uid())
        service_file = "%s/service_%s.json" % (
            service_dir, _string_to_safestring(service_url))

        import os as _os

        if _os.path.exists(service_file):
            _os.unlink(service_file)
Example #4
0
    def _get_service_from_registry(self, service_url=None, service_uid=None):
        """Return the service fetched from the registry"""
        from Acquire.Registry import get_trusted_registry_service \
            as _get_trusted_registry_service
        from Acquire.ObjectStore import string_to_safestring \
            as _string_to_safestring

        _output("Connecting to registry...")
        _flush_output()

        registry = _get_trusted_registry_service(service_uid=service_uid,
                                                 service_url=service_url)

        _output("...connected to registry %s" % registry)
        _flush_output()

        # ensure we cache this registry...
        service_dir = Wallet._get_service_dir(registry.uid())
        registry_file = "%s/service_%s.json" % (
            service_dir, _string_to_safestring(registry.canonical_url()))
        _write_service(service=registry, filename=registry_file)

        if service_url is not None:
            _output("Securely fetching keys for %s..." % service_url)
            _flush_output()
        else:
            _output("Securely fetching keys for UID %s..." % service_uid)
            _flush_output()

        service = registry.get_service(service_url=service_url,
                                       service_uid=service_uid)

        _output("...success.\nFetched %s" % service)
        _flush_output()

        return service
Example #5
0
    def get_service(self, service=None, service_url=None, service_uid=None,
                    service_type=None, autofetch=True):
        """Return the service at either 'service_url', or that
           has UID 'service_uid'. This will return the
           cached service if it exists, or will add a new service if
           we are able to validate it from a trusted registry
        """
        from Acquire.ObjectStore import string_to_safestring \
            as _string_to_safestring
        from Acquire.Service import Service as _Service

        if service is not None:
            s = _Service.resolve(service, fetch=False)

            if s["service"] is not None:
                self.add_service(s["service"])
                return s["service"]

            service_uid = s["service_uid"]
            service_url = s["service_url"]

        service = None
        import glob as _glob

        if service_url is None:
            if service_uid is None:
                raise PermissionError(
                    "You need to specify one of service_uid or service_url")

            # we need to look up the name...
            service_dir = Wallet._get_service_dir(service_uid)
            service_files = _glob.glob("%s/service_*.json" % service_dir)

            for service_file in service_files:
                s = _read_service(service_file)
                if s.uid() == service_uid:
                    service = s
                    break
        else:
            from Acquire.Service import Service as _Service
            service_url = _Service.get_canonical_url(service_url,
                                                     service_type=service_type)

            service_files = _glob.glob("%s/*/service_%s.json" % (
                                       self._wallet_dir,
                                       _string_to_safestring(service_url)))

            for service_file in service_files:
                s = _read_service(service_file)
                if s.canonical_url() == service_url:
                    service = s
                    break

        must_write = False

        if service is None:
            if not autofetch:
                from Acquire.Service import ServiceError
                raise ServiceError("No service at %s:%s" %
                                   (service_url, service_uid))

            # we need to look this service up from the registry
            service = self._get_service_from_registry(service_url=service_url,
                                                      service_uid=service_uid)
            must_write = True

        # check if the keys need rotating - if they do, load up
        # the new keys and save them to the service file...
        elif service.should_refresh_keys():
            try:
                service.refresh_keys()
                must_write = True
            except:
                # something went wrong refreshing keys - go back to the
                # registry...
                _output("Something went wrong refreshing keys...")
                _output("Refreshing service from the registry.")
                service = self._get_service_from_registry(
                                                service_url=service_url,
                                                service_uid=service_uid)
                must_write = True

        if service_uid is not None:
            if service.uid() != service_uid:
                raise PermissionError(
                    "Disagreement over the service UID for '%s' (%s)" %
                    (service, service_uid))

        if must_write:
            self.add_service(service)

        return service
Example #6
0
    def get_service(self, service_url=None, service_uid=None,
                    service_type=None, autofetch=True):
        """Return the service at either 'service_url', or that
           has UID 'service_uid'. This will return the
           cached service if it exists, or will add a new service if
           we are able to validate it from a trusted registry
        """
        from Acquire.ObjectStore import string_to_safestring \
            as _string_to_safestring

        service = None

        if service_url is None:
            if service_uid is None:
                raise PermissionError(
                    "You need to specify one of service_uid or service_url")

            # we need to look up the name...
            import glob as _glob
            service_files = _glob.glob("%s/service_*" % self._wallet_dir)

            for service_file in service_files:
                s = _read_service(service_file)
                if s.uid() == service_uid:
                    service = s
                    break
        else:
            from Acquire.Service import Service as _Service
            service_url = _Service.get_canonical_url(service_url,
                                                     service_type=service_type)

            service_file = "%s/service_%s" % (
                self._wallet_dir,
                _string_to_safestring(service_url))

            try:
                service = _read_service(service_file)
            except:
                pass

        must_write = False

        if service is None:
            if not autofetch:
                from Acquire.Service import ServiceError
                raise ServiceError("No service at %s:%s" %
                                   (service_url, service_uid))

            # we need to look this service up from the registry
            from Acquire.Registry import get_trusted_registry_service \
                as _get_trusted_registry_service

            _output("Connecting to registry...")
            _flush_output()

            registry = _get_trusted_registry_service(service_uid=service_uid,
                                                     service_url=service_url)

            _output("...connected to registry %s" % registry)
            _flush_output()

            # ensure we cache this registry...
            registry_file = "%s/service_%s" % (
                self._wallet_dir,
                _string_to_safestring(registry.canonical_url()))
            _write_service(service=registry, filename=registry_file)

            if service_url is not None:
                _output("Securely fetching keys for %s..." % service_url)
                _flush_output()
            else:
                _output("Securely fetching keys for UID %s..." % service_uid)
                _flush_output()

            service = registry.get_service(service_url=service_url,
                                           service_uid=service_uid)

            _output("...success.\nFetched %s" % service)
            _flush_output()

            must_write = True

        # check if the keys need rotating - if they do, load up
        # the new keys and save them to the service file...
        if service.should_refresh_keys():
            service.refresh_keys()
            must_write = True

        if service_uid is not None:
            if service.uid() != service_uid:
                raise PermissionError(
                    "Disagreement over the service UID for '%s' (%s)" %
                    (service, service_uid))

        if must_write:
            service_file = "%s/service_%s" % (
                self._wallet_dir,
                _string_to_safestring(service.canonical_url()))
            _write_service(service=service, filename=service_file)

        return service