Ejemplo n.º 1
0
    def test_purge_on_startup_default_is_false(self):

        original = [{
            "id": "thisIsAnID",
            "created": "2016-03-02T20:23:00.000Z",
            "max_age": 604800,
            "authid": "example.authid",
            "authrole": "example.authrole",
            "authmethod": "example.authmethod"
        }, {
            "id": "thisIsAnID",
            "modified": "2016-03-02T20:24:00.000Z",
            "max_age": 604800,
            "authid": "example.second.authid",
            "authrole": "example.second.authrole",
            "authmethod": "example.second.authmethod"
        }, {
            "id": "thisIsAnotherID",
            "created": "2016-03-02T20:23:30.000Z",
            "max_age": 604800,
            "authid": "example.other.authid",
            "authrole": "example.other.authrole",
            "authmethod": "example.other.authmethod"
        }]

        with tempfile.NamedTemporaryFile() as fp:
            self.write_cookies_to_file(original, fp)

            config = {'store': {'type': 'file', 'filename': fp.name}}

            CookieStoreFileBacked(fp.name, config)

            actual = self.read_cookies_from_file(fp)

            self.assertEqual(actual, original)
Ejemplo n.º 2
0
    def test_purge_on_startup_delete_expired_cookies(self):
        max_age = 300
        now = time.time()
        valid_time = util.utcstr(datetime.fromtimestamp(now))
        expired_time = util.utcstr(datetime.fromtimestamp(now - max_age - 10))

        original = [{
            "id": "thisIsAnID",
            "created": expired_time,
            "max_age": max_age,
            "authid": "example.authid",
            "authrole": "example.authrole",
            "authrealm": "example.authrealm",
            "authmethod": "example.authmethod"
        }, {
            "id": "thisIsAnotherID",
            "created": valid_time,
            "max_age": max_age,
            "authid": "example.other.authid",
            "authrole": "example.other.authrole",
            "authrealm": "example.other.authrealm",
            "authmethod": "example.other.authmethod"
        }, {
            "id": "thisIsAnID",
            "modified": valid_time,
            "max_age": max_age,
            "authid": "example.second.authid",
            "authrole": "example.second.authrole",
            "authrealm": "example.second.authrealm",
            "authmethod": "example.second.authmethod"
        }]

        expected = [
            {
                "id": "thisIsAnotherID",
                "created": valid_time,
                "max_age": max_age,
                "authid": "example.other.authid",
                "authrole": "example.other.authrole",
                "authrealm": "example.other.authrealm",
                "authmethod": "example.other.authmethod"
            },
        ]

        with tempfile.NamedTemporaryFile() as fp:
            self.write_cookies_to_file(original, fp)

            config = {
                'store': {
                    'type': 'file',
                    'filename': fp.name,
                    'purge_on_startup': True
                }
            }

            CookieStoreFileBacked(fp.name, config)

            actual = self.read_cookies_from_file(fp)
            self.assertEqual(actual, expected)
Ejemplo n.º 3
0
    def test_purge_on_startup(self):

        created_time = util.utcnow()

        original = [{
            "id": "thisIsAnID",
            "created": created_time,
            "max_age": 604800,
            "authid": "example.authid",
            "authrole": "example.authrole",
            "authmethod": "example.authmethod"
        }, {
            "id": "thisIsAnotherID",
            "created": created_time,
            "max_age": 604800,
            "authid": "example.other.authid",
            "authrole": "example.other.authrole",
            "authmethod": "example.other.authmethod"
        }, {
            "id": "thisIsAnID",
            "modified": created_time,
            "max_age": 604800,
            "authid": "example.second.authid",
            "authrole": "example.second.authrole",
            "authmethod": "example.second.authmethod"
        }]

        expected = [{
            "id": "thisIsAnID",
            "created": created_time,
            "max_age": 604800,
            "authid": "example.second.authid",
            "authrole": "example.second.authrole",
            "authmethod": "example.second.authmethod"
        }, {
            "id": "thisIsAnotherID",
            "created": created_time,
            "max_age": 604800,
            "authid": "example.other.authid",
            "authrole": "example.other.authrole",
            "authmethod": "example.other.authmethod"
        }]

        with tempfile.NamedTemporaryFile() as fp:
            self.write_cookies_to_file(original, fp)

            config = {
                'store': {
                    'type': 'file',
                    'filename': fp.name,
                    'purge_on_startup': True
                }
            }

            CookieStoreFileBacked(fp.name, config)

            actual = self.read_cookies_from_file(fp)
            self.assertEqual(actual, expected)
Ejemplo n.º 4
0
    def __init__(self, factory, cbdir, config, templates):
        """

        :param factory: WAMP session factory.
        :type factory: An instance of ..

        :param cbdir: The Crossbar.io node directory.
        :type cbdir: str

        :param config: Crossbar transport configuration.
        :type config: dict

        :param templates:
        :type templates:
        """
        self.debug_traffic = config.get('debug_traffic', False)

        options = config.get('options', {})

        # announce Crossbar.io server version
        #
        self.showServerVersion = options.get('show_server_version',
                                             self.showServerVersion)
        if self.showServerVersion:
            server = "Crossbar/{}".format(crossbar.__version__)
        else:
            # do not disclose crossbar version
            server = "Crossbar"

        # external (public) listening port (eg when running behind a reverse proxy)
        #
        externalPort = options.get('external_port', None)

        # explicit list of WAMP serializers
        #
        if 'serializers' in config:
            serializers = []
            sers = set(config['serializers'])

            if 'flatbuffers' in sers:
                # try FlatBuffers WAMP serializer
                try:
                    from autobahn.wamp.serializer import FlatBuffersSerializer
                    serializers.append(FlatBuffersSerializer(batched=True))
                    serializers.append(FlatBuffersSerializer())
                except ImportError('FlatBuffersSerializer'):
                    self.log.warn(
                        "Warning: could not load WAMP-FlatBuffers serializer")
                else:
                    sers.discard('flatbuffers')

            if 'cbor' in sers:
                # try CBOR WAMP serializer
                try:
                    from autobahn.wamp.serializer import CBORSerializer
                    serializers.append(CBORSerializer(batched=True))
                    serializers.append(CBORSerializer())
                except ImportError('CBORSerializer'):
                    self.log.warn(
                        "Warning: could not load WAMP-CBOR serializer")
                else:
                    sers.discard('cbor')

            if 'msgpack' in sers:
                # try MsgPack WAMP serializer
                try:
                    from autobahn.wamp.serializer import MsgPackSerializer
                    serializers.append(MsgPackSerializer(batched=True))
                    serializers.append(MsgPackSerializer())
                except ImportError('MsgPackSerializer'):
                    self.log.warn(
                        "Warning: could not load WAMP-MsgPack serializer")
                else:
                    sers.discard('msgpack')

            if 'ubjson' in sers:
                # try UBJSON WAMP serializer
                try:
                    from autobahn.wamp.serializer import UBJSONSerializer
                    serializers.append(UBJSONSerializer(batched=True))
                    serializers.append(UBJSONSerializer())
                except ImportError('UBJSONSerializer'):
                    self.log.warn(
                        "Warning: could not load WAMP-UBJSON serializer")
                else:
                    sers.discard('ubjson')

            if 'json' in sers:
                # try JSON WAMP serializer
                try:
                    from autobahn.wamp.serializer import JsonSerializer
                    serializers.append(JsonSerializer(batched=True))
                    serializers.append(JsonSerializer())
                except ImportError('JsonSerializer'):
                    self.log.warn(
                        "Warning: could not load WAMP-JSON serializer")
                else:
                    sers.discard('json')

            if not serializers:
                raise Exception("no valid WAMP serializers specified")

            if len(sers) > 0:
                raise Exception(
                    "invalid WAMP serializers specified (the following were unprocessed) {}"
                    .format(sers))

        else:
            serializers = None

        websocket.WampWebSocketServerFactory.__init__(
            self,
            factory,
            serializers=serializers,
            url=config.get('url', None),
            server=server,
            externalPort=externalPort)

        # Crossbar.io node directory
        self._cbdir = cbdir

        # transport configuration
        self._config = config

        # Jinja2 templates for 404 etc
        self._templates = templates

        # enable cookie tracking if a cookie store is configured
        if 'cookie' in config:
            # cookie store configuration item
            cookie_config = config['cookie']

            # cookie store
            cookie_store_config = cookie_config['store']
            cookie_store_type = cookie_store_config['type']

            # setup ephemeral, memory-backed cookie store
            if cookie_store_type == 'memory':
                self._cookiestore = CookieStoreMemoryBacked(cookie_config)
                self.log.info("Memory-backed cookie store active.")

            # setup persistent, file-backed cookie store
            elif cookie_store_type == 'file':
                cookie_store_file = os.path.abspath(
                    os.path.join(self._cbdir, cookie_store_config['filename']))
                self._cookiestore = CookieStoreFileBacked(
                    cookie_store_file, cookie_config)
                self.log.info(
                    "File-backed cookie store active {cookie_store_file}",
                    cookie_store_file=hlval(cookie_store_file))

            # setup persistent, database-backed cookie store
            elif cookie_store_type == 'database':
                cookie_dbpath = os.path.abspath(
                    os.path.join(self._cbdir, cookie_store_config['path']))
                self._cookiestore = CookieStoreDatabaseBacked(
                    cookie_dbpath, cookie_config)
                self.log.info(
                    "Database-backed cookie store active! [cookiestore={cookiestore}]",
                    cookiestore=hltype(CookieStoreDatabaseBacked))

            else:
                # should not arrive here as the config should have been checked before
                raise NotImplementedError(
                    '{}: implementation of cookiestore of type "{}" missing'.
                    format(self.__class__.__name__, cookie_store_type))
        else:
            # this disables cookie tracking (both with or without WAMP-cookie authentication)
            self._cookiestore = None

        # set WebSocket options
        set_websocket_options(self, options)
Ejemplo n.º 5
0
    def __init__(self, factory, cbdir, config, templates):
        """
        Ctor.

        :param factory: WAMP session factory.
        :type factory: An instance of ..
        :param cbdir: The Crossbar.io node directory.
        :type cbdir: str
        :param config: Crossbar transport configuration.
        :type config: dict
        """
        self.debug_traffic = config.get('debug_traffic', False)

        options = config.get('options', {})

        self.showServerVersion = options.get('show_server_version',
                                             self.showServerVersion)
        if self.showServerVersion:
            server = "Crossbar/{}".format(crossbar.__version__)
        else:
            server = "Crossbar"
        externalPort = options.get('external_port', None)

        # explicit list of WAMP serializers
        #
        if 'serializers' in config:
            serializers = []
            sers = set(config['serializers'])

            if u'cbor' in sers:
                # try CBOR WAMP serializer
                try:
                    from autobahn.wamp.serializer import CBORSerializer
                    serializers.append(CBORSerializer(batched=True))
                    serializers.append(CBORSerializer())
                except ImportError:
                    self.log.warn(
                        "Warning: could not load WAMP-CBOR serializer")
                else:
                    sers.discard(u'cbor')

            if u'msgpack' in sers:
                # try MsgPack WAMP serializer
                try:
                    from autobahn.wamp.serializer import MsgPackSerializer
                    serializers.append(MsgPackSerializer(batched=True))
                    serializers.append(MsgPackSerializer())
                except ImportError:
                    self.log.warn(
                        "Warning: could not load WAMP-MsgPack serializer")
                else:
                    sers.discard('msgpack')

            if u'ubjson' in sers:
                # try UBJSON WAMP serializer
                try:
                    from autobahn.wamp.serializer import UBJSONSerializer
                    serializers.append(UBJSONSerializer(batched=True))
                    serializers.append(UBJSONSerializer())
                except ImportError:
                    self.log.warn(
                        "Warning: could not load WAMP-UBJSON serializer")
                else:
                    sers.discard(u'ubjson')

            if u'json' in sers:
                # try JSON WAMP serializer
                try:
                    from autobahn.wamp.serializer import JsonSerializer
                    serializers.append(JsonSerializer(batched=True))
                    serializers.append(JsonSerializer())
                except ImportError:
                    self.log.warn(
                        "Warning: could not load WAMP-JSON serializer")
                else:
                    sers.discard(u'json')

            if not serializers:
                raise Exception("no valid WAMP serializers specified")

            if len(sers) > 0:
                raise Exception(
                    "invalid WAMP serializers specified (the following were unprocessed) {}"
                    .format(sers))

        else:
            serializers = None

        websocket.WampWebSocketServerFactory.__init__(
            self,
            factory,
            serializers=serializers,
            url=config.get('url', None),
            server=server,
            externalPort=externalPort)

        # Crossbar.io node directory
        self._cbdir = cbdir

        # transport configuration
        self._config = config

        # Jinja2 templates for 404 etc
        self._templates = templates

        # cookie tracking
        if 'cookie' in config:
            cookie_store_type = config['cookie']['store']['type']

            # ephemeral, memory-backed cookie store
            if cookie_store_type == 'memory':
                self._cookiestore = CookieStoreMemoryBacked(config['cookie'])
                self.log.info("Memory-backed cookie store active.")

            # persistent, file-backed cookie store
            elif cookie_store_type == 'file':
                cookie_store_file = os.path.abspath(
                    os.path.join(self._cbdir,
                                 config['cookie']['store']['filename']))
                self._cookiestore = CookieStoreFileBacked(
                    cookie_store_file, config['cookie'])
                self.log.info(
                    "File-backed cookie store active {cookie_store_file}",
                    cookie_store_file=cookie_store_file)

            else:
                # should not arrive here as the config should have been checked before
                raise Exception("logic error")
        else:
            self._cookiestore = None

        # set WebSocket options
        set_websocket_options(self, options)