Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
http_client_plugin.cpp
Go to the documentation of this file.
3#include <boost/algorithm/string/predicate.hpp>
4#include <fstream>
5
6namespace sysio {
7
10
11void http_client_plugin::set_program_options(options_description&, options_description& cfg) {
12 cfg.add_options()
13 ("https-client-root-cert", boost::program_options::value<vector<string>>()->composing()->multitoken(),
14 "PEM encoded trusted root certificate (or path to file containing one) used to validate any TLS connections made. (may specify multiple times)\n")
15 ("https-client-validate-peers", boost::program_options::value<bool>()->default_value(true),
16 "true: validate that the peer certificates are valid and trusted, false: ignore cert errors")
17 ;
18
19}
20
21void http_client_plugin::plugin_initialize(const variables_map& options) {
22 try {
23 if( options.count( "https-client-root-cert" )) {
24 const std::vector<std::string> root_pems = options["https-client-root-cert"].as<std::vector<std::string>>();
25 for( const auto& root_pem : root_pems ) {
26 std::string pem_str = root_pem;
27 if( !boost::algorithm::starts_with( pem_str, "-----BEGIN CERTIFICATE-----\n" )) {
28 try {
29 auto infile = std::ifstream( pem_str );
30 std::stringstream sstr;
31 sstr << infile.rdbuf();
32 pem_str = sstr.str();
33 SYS_ASSERT( boost::algorithm::starts_with( pem_str, "-----BEGIN CERTIFICATE-----\n" ),
34 chain::invalid_http_client_root_cert,
35 "File does not appear to be a PEM encoded certificate" );
36 } catch ( const std::bad_alloc& ) {
37 throw;
38 } catch ( const boost::interprocess::bad_alloc& ) {
39 throw;
40 } catch ( const fc::exception& e ) {
41 elog( "Failed to read PEM ${f} : ${e}", ("f", root_pem)( "e", e.to_detail_string()));
42 } catch ( const std::exception& e ) {
43 elog( "Failed to read PEM ${f} : ${e}", ("f", root_pem)( "e", fc::std_exception_wrapper::from_current_exception(e).to_detail_string()));
44 }
45 }
46
47 try {
48 my->add_cert( pem_str );
49 } catch ( const std::bad_alloc& ) {
50 throw;
51 } catch ( const boost::interprocess::bad_alloc& ) {
52 throw;
53 } catch ( const fc::exception& e ) {
54 elog( "Failed to read PEM : ${e} \n${pem}\n", ("pem", pem_str)( "e", e.to_detail_string()));
55 } catch ( const std::exception& e ) {
56 elog( "Failed to read PEM : ${e} \n${pem}\n", ("pem", pem_str)( "e", fc::std_exception_wrapper::from_current_exception(e).to_detail_string()));
57 }
58 }
59 }
60
61 my->set_verify_peers( options.at( "https-client-validate-peers" ).as<bool>());
63}
64
68
72
73}
#define SYS_ASSERT(expr, exc_type, FORMAT,...)
Definition exceptions.hpp:7
Used to generate a useful error report when an exception is thrown.
Definition exception.hpp:58
std::string to_detail_string(log_level ll=log_level::all) const
static std_exception_wrapper from_current_exception(const std::exception &e)
virtual void set_program_options(options_description &, options_description &cfg) override
void plugin_initialize(const variables_map &options)
#define FC_LOG_AND_RETHROW()
#define elog(FORMAT,...)
Definition logger.hpp:130