def test_get_amid_unknown(self, request_mock):
        import requests
        from autopush.utils import get_amid

        request_mock.side_effect = requests.HTTPError
        result = get_amid()
        assert result is None
示例#2
0
    def from_argparse(cls, ns, **kwargs):
        # type: (Namespace, **Any) -> AutopushConfig
        """Create an instance from argparse/additional kwargs"""
        router_conf = {}
        if ns.key_hash:
            db.key_hash = ns.key_hash
        if ns.apns_creds:
            # if you have the critical elements for each external
            # router, create it
            try:
                router_conf["apns"] = json.loads(ns.apns_creds)
            except (ValueError, TypeError):
                raise InvalidConfig(
                    "Invalid JSON specified for APNS config options")
        if ns.senderid_list:
            # Create a common gcmclient
            try:
                sender_ids = json.loads(ns.senderid_list)
            except (ValueError, TypeError):
                raise InvalidConfig("Invalid JSON specified for senderid_list")
            try:
                # This is an init check to verify that things are
                # configured correctly. Otherwise errors may creep in
                # later that go unaccounted.
                sender_ids[sender_ids.keys()[0]]
            except (IndexError, TypeError):
                raise InvalidConfig("No GCM SenderIDs specified or found.")
            router_conf["gcm"] = {"ttl": ns.gcm_ttl,
                                  "dryrun": ns.gcm_dryrun,
                                  "max_data": ns.max_data,
                                  "collapsekey": ns.gcm_collapsekey,
                                  "senderIDs": sender_ids,
                                  "endpoint": ns.gcm_endpoint}
        client_certs = None
        # endpoint only
        if getattr(ns, 'client_certs', None):
            try:
                client_certs_arg = json.loads(ns.client_certs)
            except (ValueError, TypeError):
                raise InvalidConfig("Invalid JSON specified for client_certs")
            if client_certs_arg:
                if not ns.ssl_key:
                    raise InvalidConfig("client_certs specified without SSL "
                                        "enabled (no ssl_key specified)")
                client_certs = {}
                for name, sigs in client_certs_arg.iteritems():
                    if not isinstance(sigs, list):
                        raise InvalidConfig(
                            "Invalid JSON specified for client_certs")
                    for sig in sigs:
                        sig = sig.upper()
                        if (not name or not CLIENT_SHA256_RE.match(sig) or
                                sig in client_certs):
                            raise InvalidConfig(
                                "Invalid client_certs argument")
                        client_certs[sig] = name

        if ns.fcm_creds:
            try:
                router_conf["fcm"] = {
                    "version": ns.fcm_version,
                    "ttl": ns.fcm_ttl,
                    "dryrun": ns.fcm_dryrun,
                    "max_data": ns.max_data,
                    "collapsekey": ns.fcm_collapsekey,
                    "creds": json.loads(ns.fcm_creds)
                }
                if not router_conf["fcm"]["creds"]:
                    raise InvalidConfig(
                        "Empty credentials for FCM config options"
                    )
                for creds in router_conf["fcm"]["creds"].values():
                    if "auth" not in creds:
                        raise InvalidConfig(
                            "Missing auth for FCM config options"
                        )
            except (ValueError, TypeError):
                raise InvalidConfig(
                    "Invalid JSON specified for FCM config options"
                )

        if ns.adm_creds:
            # Create a common admclient
            try:
                router_conf["adm"] = json.loads(ns.adm_creds)
            except (ValueError, TypeError):
                raise InvalidConfig(
                    "Invalid JSON specified for ADM config options")

        ami_id = None
        # Not a fan of double negatives, but this makes more
        # understandable args
        if not ns.no_aws:
            ami_id = get_amid() or "Unknown"

        allow_table_rotation = not ns.no_table_rotation
        return cls(
            crypto_key=ns.crypto_key,
            datadog_api_key=ns.datadog_api_key,
            datadog_app_key=ns.datadog_app_key,
            datadog_flush_interval=ns.datadog_flush_interval,
            hostname=ns.hostname,
            statsd_host=ns.statsd_host,
            statsd_port=ns.statsd_port,
            router_conf=router_conf,
            resolve_hostname=ns.resolve_hostname,
            ami_id=ami_id,
            client_certs=client_certs,
            msg_limit=ns.msg_limit,
            connect_timeout=ns.connection_timeout,
            memusage_port=ns.memusage_port,
            use_cryptography=ns.use_cryptography,
            no_sslcontext_cache=ns._no_sslcontext_cache,
            router_table=dict(
                tablename=ns.router_tablename,
                read_throughput=ns.router_read_throughput,
                write_throughput=ns.router_write_throughput
            ),
            message_table=dict(
                tablename=ns.message_tablename,
                read_throughput=ns.message_read_throughput,
                write_throughput=ns.message_write_throughput
            ),
            ssl=dict(
                key=ns.ssl_key,
                cert=ns.ssl_cert,
                dh_param=ns.ssl_dh_param
            ),
            sts_max_age=ns.sts_max_age,
            allow_table_rotation=allow_table_rotation,
            **kwargs
        )
示例#3
0
    def from_argparse(cls, ns, **kwargs):
        # type: (Namespace, **Any) -> AutopushSettings
        """Create an instance from argparse/additional kwargs"""
        router_conf = {}
        if ns.key_hash:
            db.key_hash = ns.key_hash
        # Some routers require a websocket to timeout on idle
        # (e.g. UDP)
        if ns.wake_pem is not None and ns.wake_timeout != 0:
            router_conf["simplepush"] = {
                "idle": ns.wake_timeout,
                "server": ns.wake_server,
                "cert": ns.wake_pem
            }
        if ns.apns_creds:
            # if you have the critical elements for each external
            # router, create it
            try:
                router_conf["apns"] = json.loads(ns.apns_creds)
            except (ValueError, TypeError):
                raise InvalidSettings(
                    "Invalid JSON specified for APNS config options")
        if ns.gcm_enabled:
            # Create a common gcmclient
            try:
                sender_ids = json.loads(ns.senderid_list)
            except (ValueError, TypeError):
                raise InvalidSettings(
                    "Invalid JSON specified for senderid_list")
            try:
                # This is an init check to verify that things are
                # configured correctly. Otherwise errors may creep in
                # later that go unaccounted.
                sender_ids[sender_ids.keys()[0]]
            except (IndexError, TypeError):
                raise InvalidSettings("No GCM SenderIDs specified or found.")
            router_conf["gcm"] = {
                "ttl": ns.gcm_ttl,
                "dryrun": ns.gcm_dryrun,
                "max_data": ns.max_data,
                "collapsekey": ns.gcm_collapsekey,
                "senderIDs": sender_ids
            }

        client_certs = None
        # endpoint only
        if getattr(ns, 'client_certs', None):
            try:
                client_certs_arg = json.loads(ns.client_certs)
            except (ValueError, TypeError):
                raise InvalidSettings(
                    "Invalid JSON specified for client_certs")
            if client_certs_arg:
                if not ns.ssl_key:
                    raise InvalidSettings("client_certs specified without SSL "
                                          "enabled (no ssl_key specified)")
                client_certs = {}
                for name, sigs in client_certs_arg.iteritems():
                    if not isinstance(sigs, list):
                        raise InvalidSettings(
                            "Invalid JSON specified for client_certs")
                    for sig in sigs:
                        sig = sig.upper()
                        if (not name or not CLIENT_SHA256_RE.match(sig)
                                or sig in client_certs):
                            raise InvalidSettings(
                                "Invalid client_certs argument")
                        client_certs[sig] = name

        if ns.fcm_enabled:
            # Create a common gcmclient
            if not ns.fcm_auth:
                raise InvalidSettings("No Authorization Key found for FCM")
            if not ns.fcm_senderid:
                raise InvalidSettings("No SenderID found for FCM")
            router_conf["fcm"] = {
                "ttl": ns.fcm_ttl,
                "dryrun": ns.fcm_dryrun,
                "max_data": ns.max_data,
                "collapsekey": ns.fcm_collapsekey,
                "auth": ns.fcm_auth,
                "senderid": ns.fcm_senderid
            }

        ami_id = None
        # Not a fan of double negatives, but this makes more
        # understandable args
        if not ns.no_aws:
            ami_id = get_amid()

        return cls(crypto_key=ns.crypto_key,
                   datadog_api_key=ns.datadog_api_key,
                   datadog_app_key=ns.datadog_app_key,
                   datadog_flush_interval=ns.datadog_flush_interval,
                   hostname=ns.hostname,
                   statsd_host=ns.statsd_host,
                   statsd_port=ns.statsd_port,
                   router_conf=router_conf,
                   router_tablename=ns.router_tablename,
                   storage_tablename=ns.storage_tablename,
                   storage_read_throughput=ns.storage_read_throughput,
                   storage_write_throughput=ns.storage_write_throughput,
                   message_tablename=ns.message_tablename,
                   message_read_throughput=ns.message_read_throughput,
                   message_write_throughput=ns.message_write_throughput,
                   router_read_throughput=ns.router_read_throughput,
                   router_write_throughput=ns.router_write_throughput,
                   resolve_hostname=ns.resolve_hostname,
                   wake_timeout=ns.wake_timeout,
                   ami_id=ami_id,
                   client_certs=client_certs,
                   msg_limit=ns.msg_limit,
                   connect_timeout=ns.connection_timeout,
                   memusage_port=ns.memusage_port,
                   ssl_key=ns.ssl_key,
                   ssl_cert=ns.ssl_cert,
                   ssl_dh_param=ns.ssl_dh_param,
                   **kwargs)