Example #1
0
def get_ca_env_configuration(ca, ca_key_path):
    ca_env_configuration = dict(
        ca=ca,
        tmp_env_init_kwargs_base=dict(
            ssl_conf=ca.ssl_config,
            index_attr=DEFAULT_INDEX_ATTR_CONTENT,
            ca_cert=ca.certificate,
        ),
    )
    if ca_key_path.startswith('pkcs11:'):
        (_, pkcs11_dynamic_path, pkcs11_module_path,
         pkcs11_additional_openssl_cmd_args) = ca_key_path.split(':', 3)
        ca_env_configuration['tmp_env_init_kwargs_base'][
            'pkcs11_opts_dict'] = {
                'pkcs11_dynamic_path':
                pkcs11_dynamic_path,
                'pkcs11_module_path':
                pkcs11_module_path,
                'pkcs11_additional_openssl_cmd_arg_list':
                pkcs11_additional_openssl_cmd_args.split(),
            }
    else:
        ca_env_configuration['tmp_env_init_kwargs_base']['ca_key'] = read_file(
            ca_key_path)
    return ca_env_configuration
Example #2
0
    def test_success(self):
        with AtomicallySavedFile(self.tmp_file_path, 'w') as file:
            file.write('changed content')

        actual_content = read_file(self.tmp_file_path)
        expected_content = 'changed content'
        self.assertEqual(expected_content, actual_content)
Example #3
0
 def _publish(self, payload_filepath, payload_info_dict):
     payload = read_file(payload_filepath)
     meta_headers = self._get_meta_headers(payload_info_dict)
     (output_rk, output_data_body,
      output_prop_kwargs) = self.get_output_components(
          data_body=payload, meta_headers=meta_headers)
     self.publish_output(routing_key=output_rk,
                         body=output_data_body,
                         prop_kwargs=output_prop_kwargs)
Example #4
0
    def test(self):
        expected_data = 'foo bar\nspam'
        open_mock = mock_open(read_data=expected_data)
        with patch('builtins.open', open_mock):
            actual_data = read_file('/some/file.txt', 'r+', some_kw_arg=1)
        self.assertEqual(actual_data, expected_data)
        self.assertEqual(open_mock.mock_calls, [
            call('/some/file.txt', 'r+', some_kw_arg=1, encoding='utf-8'),
            call().__enter__(),
            call().read(),
            call().__exit__(None, None, None),
        ])

        expected_data = 'foo bar\nspam'
        encoding = 'Foo Bar'
        open_mock = mock_open(read_data=expected_data)
        with patch('builtins.open', open_mock):
            actual_data = read_file('/some/file.txt',
                                    'r+',
                                    some_kw_arg=1,
                                    encoding=encoding)
        self.assertEqual(actual_data, expected_data)
        self.assertEqual(open_mock.mock_calls, [
            call('/some/file.txt', 'r+', some_kw_arg=1, encoding=encoding),
            call().__enter__(),
            call().read(),
            call().__exit__(None, None, None),
        ])

        expected_data = b'foo bar\nspam'
        open_mock = mock_open(read_data=expected_data)
        with patch('builtins.open', open_mock):
            actual_data = read_file('/some/file.txt', 'r+b', some_kw_arg=1)
        self.assertEqual(actual_data, expected_data)
        self.assertEqual(
            open_mock.mock_calls,
            [
                call('/some/file.txt', 'r+b',
                     some_kw_arg=1),  # Note: no `encoding='utf-8'`
                call().__enter__(),
                call().read(),
                call().__exit__(None, None, None),
            ])
Example #5
0
    def test_exception_while_writing_to_the_file(self):
        try:
            with AtomicallySavedFile(self.tmp_file_path, 'w') as file:
                file.write('changed content')
                raise Exception('Exception occurred while writing to the file')
        except Exception:
            pass

        actual_content = read_file(self.tmp_file_path)
        expected_content = 'original content'
        self.assertEqual(expected_content, actual_content)
Example #6
0
    def test_exception_before_writing_to_the_file(self):
        invalid_path = 'foo/bar/baz.txt'
        try:
            with AtomicallySavedFile(invalid_path, 'w') as file:
                pass
        except OSError:
            pass

        actual_content = read_file(self.tmp_file_path)
        expected_content = 'original content'
        self.assertEqual(expected_content, actual_content)
Example #7
0
 def test(self):
     expected_data = 'foo bar\nspam'
     open_mock = mock_open(read_data=expected_data)
     with patch('__builtin__.open', open_mock):
         actual_data = read_file('/some/file.txt', 'r+', 1)
     self.assertEqual(actual_data, expected_data)
     self.assertEqual(open_mock.mock_calls, [
         call('/some/file.txt', 'r+', 1),
         call().__enter__(),
         call().read(),
         call().__exit__(None, None, None),
     ])
Example #8
0
 def execute_crl_generation(self):
     self._execute_command([
         'openssl',
         'ca',
         '-config',
         self.ssl_conf.path,
         '-gencrl',
         '-out',
         self.ca_crl.path,
         '-batch',
     ] + self._get_pkcs11_openssl_command_args())
     return read_file(self.ca_crl.path)
Example #9
0
 def execute_cert_generation(self, additional_openssl_command_args):
     self._execute_command([
         'openssl',
         'ca',
         '-config',
         self.ssl_conf.path,
         '-notext',
         '-in',
         self.csr.path,
         '-out',
         self.gen_cert.path,
         '-batch',
     ] + self._get_pkcs11_openssl_command_args() +
                           list(additional_openssl_command_args))
     return read_file(self.gen_cert.path)
Example #10
0
 def _memoized_read_file(file_path):
     return read_file(file_path)