def test_that_argument_parses_should_fail_if_file_with_secrets_is_missing(self):
     sys.argv += [
         '--concent-cluster-host', '127.0.0.1',
         '--concent-public-key', self.concent_public_key_encoded,
         '--sentry-dsn-path', '/not_existing_path/file.txt'
     ]
     with self.assertRaises(FileNotFoundError):
         _parse_arguments()
    def test_that_argument_parser_should_fail_if_port_cannot_be_casted_to_int(self):
        sys.argv += [
            '--concent_cluster_host', '127.0.0.1',
            '--concent-public-key', self.concent_public_key_encoded,
            '--concent-cluster-port', 'abc',
            '--ethereum-private-key', self.ethereum_private_key_encoded,
            '--signing-service-private-key', self.signing_service_private_key_encoded,
        ]

        with self.assertRaises(SystemExit):
            _parse_arguments()
    def test_that_argument_parses_should_fail_gracefully_when_unable_to_decode_base64_value(self):
        sys.argv += [
            '--concent-cluster-host', '127.0.0.1',
            '--concent-public-key', self.concent_public_key_encoded[:-1],
        ]
        with self.assertRaises(Base64DecodeError) as error:
            _parse_arguments()

        self.assertIn(
            f'Unable to decode "{self.concent_public_key_encoded[:-1]}"',
            str(error.exception)
        )
    def test_that_argument_parser_should_parse_parameters_if_passed_files_exist(self):
        sentry_tmp_file = os.path.join(tempfile.gettempdir(), "sentry_tmp_file.txt")
        ethereum_private_key_tmp_file = os.path.join(tempfile.gettempdir(), "ethereum_private_key_tmp_file.txt")
        signing_service_private_key_tmp_file = os.path.join(tempfile.gettempdir(), "signing_service_private_key_tmp_file.txt")

        sys.argv += [
            '--concent-cluster-host', '127.0.0.1',
            '--concent-public-key', self.concent_public_key_encoded,
            '--ethereum-private-key-path', ethereum_private_key_tmp_file,
            '--sentry-dsn-path', sentry_tmp_file,
            '--signing-service-private-key-path', signing_service_private_key_tmp_file,
        ]

        with open(sentry_tmp_file, "w") as file:
            file.write(self.sentry_dsn)

        with open(ethereum_private_key_tmp_file, "w") as file:
            file.write(self.ethereum_private_key_encoded)

        with open(signing_service_private_key_tmp_file, "w") as file:
            file.write(self.signing_service_private_key_encoded)

        args =_parse_arguments()

        self.assertEqual(args.sentry_dsn, self.sentry_dsn)
        self.assertEqual(args.ethereum_private_key, TEST_ETHEREUM_PRIVATE_KEY)
        self.assertEqual(args.signing_service_private_key, SIGNING_SERVICE_PRIVATE_KEY)
        os.remove(sentry_tmp_file)
        os.remove(ethereum_private_key_tmp_file)
        os.remove(signing_service_private_key_tmp_file)
    def test_that_argument_parser_should_parse_correct_secrets_from_env_variables(
            self):
        sys.argv += [
            '--concent-cluster-host',
            '127.0.0.1',
            '--concent-public-key',
            self.concent_public_key_encoded,
            '--sentry-dsn-from-env',
            '--ethereum-private-key-from-env',
            '--signing-service-private-key-from-env',
        ]

        with mock.patch.dict(
                os.environ, {
                    'SENTRY_DSN':
                    self.sentry_dsn,
                    'ETHEREUM_PRIVATE_KEY':
                    self.ethereum_private_key_encoded,
                    'SIGNING_SERVICE_PRIVATE_KEY':
                    self.signing_service_private_key_encoded,
                }):
            args = _parse_arguments()

        self.assertEqual(args.sentry_dsn, self.sentry_dsn)
        self.assertEqual(args.ethereum_private_key, TEST_ETHEREUM_PRIVATE_KEY)
        self.assertEqual(args.signing_service_private_key,
                         SIGNING_SERVICE_PRIVATE_KEY)
    def test_that_argument_parser_should_parse_correct_input(self):
        sys.argv += [
            '--concent-cluster-host',
            '127.0.0.1',
            '--concent-public-key',
            self.concent_public_key_encoded,
            '--initial_reconnect_delay',
            '2',
            '--concent-cluster-port',
            '8000',
            '--sentry-dsn',
            self.sentry_dsn,
            '--ethereum-private-key',
            self.ethereum_private_key_encoded,
            '--signing-service-private-key',
            self.signing_service_private_key_encoded,
            f'--max-reconnect-attempts={self.maximum_reconnection_attempts}',
        ]

        args = _parse_arguments()

        self.assertEqual(args.concent_cluster_host, '127.0.0.1')
        self.assertEqual(args.concent_cluster_port, 8000)
        self.assertEqual(args.initial_reconnect_delay, 2)
        self.assertEqual(args.concent_public_key, CONCENT_PUBLIC_KEY)
        self.assertEqual(args.sentry_dsn, self.sentry_dsn)
        self.assertEqual(args.ethereum_private_key, TEST_ETHEREUM_PRIVATE_KEY)
        self.assertEqual(args.signing_service_private_key,
                         SIGNING_SERVICE_PRIVATE_KEY)
 def test_that_console_notifier_is_created_by_default(self):
     sys.argv += [
         '--ethereum-private-key', self.ethereum_private_key_encoded,
         '--concent-public-key', self.concent_public_key_encoded,
         '--signing-service-private-key', self.signing_service_private_key_encoded,
     ]
     args = _parse_arguments()
     notifier = get_notifier(args)
     self.assertIsInstance(notifier, ConsoleNotifier)
    def test_that_argument_parser_should_raise_exception_if_ethereum_key_cannot_be_decoded_to_string(self):
        ethereum_private_key_raw = b'\x19\xf9\x16\xcc$F/\xf3\x1e\xdb\t8\xd6d\xb45\x14\x9c\x87e\xcc\xa6\x02\x17`\xb4Zj\xc9\x9c\xa3<'

        # Proof that this particular key UnicodeDecodeError will be raised.
        try:
            ethereum_private_key_raw.decode()
            assert False
        except UnicodeDecodeError:
            pass

        ethereum_private_key_as_string = b64encode(ethereum_private_key_raw).decode()

        sys.argv += [
            '--concent-cluster-host', '127.0.0.1',
            '--concent-public-key', self.concent_public_key_encoded,
            '--ethereum-private-key', ethereum_private_key_as_string,
        ]

        with self.assertRaises(BytesToStringDecodeError):
            _parse_arguments()
    def test_that_argument_parser_should_parse_correct_input_and_use_default_values(self):
        sys.argv += [
            '--concent-public-key', self.concent_public_key_encoded,
            '--ethereum-private-key', self.ethereum_private_key_encoded,
            '--signing-service-private-key', self.signing_service_private_key_encoded,
        ]

        args = _parse_arguments()

        self.assertEqual(args.concent_cluster_port, SIGNING_SERVICE_DEFAULT_PORT)
        self.assertEqual(args.initial_reconnect_delay, SIGNING_SERVICE_DEFAULT_INITIAL_RECONNECT_DELAY)
        self.assertEqual(args.max_reconnect_attempts, SIGNING_SERVICE_DEFAULT_RECONNECT_ATTEMPTS)
 def test_that_email_notifier_is_created_when_proper_arguments_are_provided_via_command_line(self):
     from_email_address = '*****@*****.**'
     password = '******'
     recipients = [
         '*****@*****.**', '*****@*****.**',
     ]
     sys.argv += [
         '--ethereum-private-key', self.ethereum_private_key_encoded,
         '--concent-public-key', self.concent_public_key_encoded,
         '--signing-service-private-key', self.signing_service_private_key_encoded,
         'smtp-notifier',
         '--from-email-address', from_email_address,
         '--from-email-password', password,
         '--to-email-addresses', *recipients
     ]
     args = _parse_arguments()
     notifier = get_notifier(args)
     self.assertIsInstance(notifier, EmailNotifier)
Beispiel #11
0
from raven import Client

from signing_service.signing_service import _parse_arguments
from signing_service.signing_service import SigningService
from signing_service.utils import get_notifier

crash_logger = logging.getLogger('crash')

if __name__ == '__main__':

    logging.config.fileConfig(
        os.path.join(os.path.dirname(os.path.abspath(__file__)), '..',
                     'logging.ini'))

    # Parse required arguments.
    args = _parse_arguments()

    raven_client = Client(
        dsn=args.sentry_dsn,
        environment=args.sentry_environment,
        tags={
            'component': 'signing-service',
        },
    )
    crash_logger.handlers[0].client = raven_client  # type: ignore

    notifier = get_notifier(args)

    SigningService(
        args.concent_cluster_host,
        args.concent_cluster_port,