Dripline-Cpp  v2.10.11
Dripline Implementation in C++
dripline_config.cc
Go to the documentation of this file.
1 /*
2  * dripline_config.cc
3  *
4  * Created on: Jun 26, 2019
5  * Author: N.S. Oblath
6  */
7 
8 #define DRIPLINE_API_EXPORTS
9 
10 #include "dripline_config.hh"
11 
12 #include "application.hh"
13 #include "param_codec.hh"
14 #include "param_helpers_impl.hh"
15 #include "path.hh"
16 
17 #include "logger.hh"
18 
19 using scarab::param_node;
21 
22 LOGGER( dlog, "dripline_config" );
23 
24 namespace dripline
25 {
26 
27  dripline_config::dripline_config( bool a_read_mesh_file )
28  {
29  // default dripline configuration
30  add( "broker_port", 5672 );
31  add( "broker", "localhost" );
32  add( "requests_exchange", "requests" );
33  add( "alerts_exchange", "alerts" );
34  add( "max_payload_size", DL_MAX_PAYLOAD_SIZE );
35  add( "heartbeat_routing_key", "heartbeat" );
36  add( "max_connection_attempts", 10 );
37 
38  //LWARN( dlog, "in dripline_config constructor" );
39  if( a_read_mesh_file )
40  {
41  scarab::path t_file_path = scarab::path(getenv("HOME")) / scarab::path(".dripline_mesh.yaml");
42  if( boost::filesystem::exists( t_file_path ) )
43  {
44  LDEBUG( dlog, "Loading mesh file " << t_file_path );
45  scarab::param_translator t_translator;
46  scarab::param_ptr_t t_config_from_file = t_translator.read_file(t_file_path.native());
47  merge( t_config_from_file->as_node() );
48  }
49  else
50  {
51  LDEBUG( dlog, "Mesh file is not present: " << t_file_path );
52  }
53  }
54  }
55 
56  void add_dripline_options( scarab::main_app& an_app )
57  {
58  LDEBUG( dlog, "Adding dripline config options" );
59 
60  // Fix two hyphen-->underscore issues
61  an_app.auth_file_key() = "auth_file";
62  an_app.auth_groups_key() = "auth_groups";
63 
64  // Add auth spec (must be after fixing the keys above)
65  add_dripline_auth_spec( an_app );
66 
67  // authentication for the broker
68  an_app.add_config_option< std::string >( "-u,--username", "auth_groups.dripline.username.value", "Specify the username for the rabbitmq broker" );
69  an_app.add_config_option< std::string >( "--password", "auth_groups.dripline.password.value", "Specify a password for the rabbitmq broker -- NOTE: this will be plain text on the command line and may end up in your command history!" );
70  an_app.add_config_option< std::string >( "--password-file", "auth_groups.dripline.password.file", "Specify a file (e.g. a secrets file) to be read in as the rabbitmq broker password" );
71  an_app.add_config_option< std::string >( "--auth-file", "auth_file", "Set the authentication file path" );
72 
73  // other dripline things
74  an_app.add_config_option< std::string >( "-b,--broker", "dripline_mesh.broker", "Set the dripline broker address" );
75  an_app.add_config_option< unsigned >( "-p,--port", "dripline_mesh.broker_port", "Set the port for communication with the dripline broker" );
76  an_app.add_config_option< std::string >( "--requests-exchange", "dripline_mesh.requests_exchange", "Set the name of the requests exchange" );
77  an_app.add_config_option< std::string >( "--alerts-exchange", "dripline_mesh.alerts_exchange", "Set the name of the alerts exchange" );
78  an_app.add_config_option< unsigned >( "--max-payload", "dripline_mesh.max_payload_size", "Set the maximum payload size (in bytes)" );
79  an_app.add_config_option< std::string >( "--heartbeat-routing-key", "dripline_mesh.heartbeat_routing_key", "Set the first token of heartbeat routing keys: [token].[origin]" );
80  an_app.add_config_option< unsigned >( "--max-connection-attempts", "dripline_mesh.max_connection_attempts", "Maximum number of times to attempt to connect to the broker" );
81 
82  return;
83  }
84 
85  scarab::param_node create_dripline_auth_spec()
86  {
87  return scarab::param_node(
88  "username"_a=scarab::param_node(
89  "default"_a="guest",
90  "env"_a="DRIPLINE_USER"
91  ),
92  "password"_a=scarab::param_node(
93  "default"_a="guest",
94  "env"_a="DRIPLINE_PASSWORD"
95  )
96  );
97  }
98 
99  scarab::authentication create_auth_with_dripline( bool a_process_spec )
100  {
101  scarab::param_node t_spec;
102  t_spec.add( "dripline", std::move(create_dripline_auth_spec()) );
103 
104  scarab::authentication t_auth;
105  t_auth.add_groups( t_spec );
106 
107  if( a_process_spec )
108  {
109  t_auth.process_spec();
110  }
111 
112  return t_auth;
113  }
114 
115  void add_dripline_auth_spec( scarab::main_app& an_app, bool a_use_auth_file )
116  {
117  LDEBUG( dlog, "Adding dripline auth spec" );
118 
119  // This is setup as an either-or feature:
120  // You're defaults are either the auth-specification or an auth file
121  // The use of an auth file is being maintained for backwards compatibility,
122  // but is not the preferred method of handling authentication
123  if( a_use_auth_file )
124  {
125  an_app.set_default_auth_file( "authentications.json" );
126  }
127  else
128  {
129  an_app.add_default_auth_spec_group( "dripline", create_dripline_auth_spec() );
130  }
131  return;
132  }
133 
134 } /* namespace dripline */
dripline_config(bool a_read_mesh_file=true)
Creates the mesh config object with default values merged (optionally) with anything in $HOME/....
static ::scarab::logger_type< ::scarab::spd_initializer_async_stdout_color_mt > dlog("dripline_config", __FILE_NAME__, __LINE__)
using_param_args_and_kwargs
#define DL_MAX_PAYLOAD_SIZE
void add_dripline_auth_spec(scarab::main_app &an_app, bool a_use_auth_file)
scarab::authentication create_auth_with_dripline(bool a_process_spec)
Create an authentication object with the default dripline authentication specification.
static ::scarab::logger_type< ::scarab::spd_initializer_async_stdout_color_mt > dlog("agent", __FILE_NAME__, __LINE__)
void add_dripline_options(scarab::main_app &an_app)
Add dripline mesh CL options to an app object.
scarab::param_node create_dripline_auth_spec()
Create a param_node with the default dripline authentication specification.