From eaf1ed48b3a0ffe688520bcb5eb93a4548c03b01 Mon Sep 17 00:00:00 2001 From: Noah Oblath Date: Wed, 5 Jun 2024 17:10:40 -0700 Subject: [PATCH] Adding agent-configuration tests and sorting out the authentication flow --- examples/authentications.json | 3 +- library/agent_config.cc | 6 +-- library/core.cc | 2 +- library/dripline_config.cc | 16 ++++++-- library/dripline_config.hh | 8 +++- testing/integration/README.md | 2 + testing/integration/authentications.json | 3 +- testing/integration/docker-compose-test.yaml | 2 - testing/integration/docker-compose.yaml | 2 - testing/test_agent.cc | 39 ++++++++++++++++++++ 10 files changed, 64 insertions(+), 19 deletions(-) diff --git a/examples/authentications.json b/examples/authentications.json index 99b7d2d4..ee89bb2a 100644 --- a/examples/authentications.json +++ b/examples/authentications.json @@ -1,6 +1,5 @@ { - "amqp": { - "broker": "rabbit-broker", + "dripline": { "username": "dripline", "password": "dripline" } diff --git a/library/agent_config.cc b/library/agent_config.cc index 01690606..cfbc7c95 100644 --- a/library/agent_config.cc +++ b/library/agent_config.cc @@ -9,11 +9,9 @@ #include "agent_config.hh" -#include "logger.hh" +//#include "logger.hh" -using std::string; - -LOGGER( dlog, "agent_config" ); +//LOGGER( dlog, "agent_config" ); namespace dripline { diff --git a/library/core.cc b/library/core.cc index 76d61818..162fb352 100644 --- a/library/core.cc +++ b/library/core.cc @@ -51,7 +51,7 @@ namespace dripline core::core( const scarab::param_node& a_config, const scarab::authentication& a_auth, const bool a_make_connection ) : f_address( a_config.get_value("broker", "localhost") ), f_port( a_config.get_value("broker-port", 5672) ), - f_username( a_auth.get("dripline", "user", "guest") ), + f_username( a_auth.get("dripline", "username", "guest") ), f_password( a_auth.get("dripline", "password", "guest") ), f_requests_exchange( a_config.get_value("requests-exchange", "requests") ), f_alerts_exchange( a_config.get_value("alerts-exchange", "alerts") ), diff --git a/library/dripline_config.cc b/library/dripline_config.cc index aba3896a..8186255b 100644 --- a/library/dripline_config.cc +++ b/library/dripline_config.cc @@ -22,7 +22,7 @@ LOGGER( dlog, "agent_config" ); namespace dripline { - dripline_config::dripline_config( const std::string& a_auth_file ) + dripline_config::dripline_config() { // default dripline configuration add( "broker-port", 5672 ); @@ -45,7 +45,7 @@ namespace dripline { an_app.add_config_option< std::string >( "-b,--broker", "dripline.broker", "Set the dripline broker address" ); an_app.add_config_option< unsigned >( "-p,--port", "dripline.broker-port", "Set the port for communication with the dripline broker" ); - an_app.add_config_option< std::string >( "--auth-file", "dripline.auth-file", "Set the authentication file path" ); + an_app.add_config_option< std::string >( "--auth-file", "auth-file", "Set the authentication file path" ); an_app.add_config_option< std::string >( "--requests-exchange", "dripline.requests-exchange", "Set the name of the requests exchange" ); an_app.add_config_option< std::string >( "--alerts-exchange", "dripline.alerts-exchange", "Set the name of the alerts exchange" ); an_app.add_config_option< unsigned >( "--max-payload", "dripline.max-payload-size", "Set the maximum payload size (in bytes)" ); @@ -58,11 +58,19 @@ namespace dripline return; } - void add_dripline_auth_spec( scarab::main_app& an_app ) + void add_dripline_auth_spec( scarab::main_app& an_app, bool a_use_auth_file ) { + // The use of an auth file is being maintained for backwards compatibility, + // but is not the preferred method of handling authentication + if( a_use_auth_file ) + { + an_app.set_default_auth_file( "authentications.json" ); + return; + } + an_app.add_default_auth_spec_group( "dripline", scarab::param_node( - "user"_a=scarab::param_node( + "username"_a=scarab::param_node( "default"_a="guest", "env"_a="DRIPLINE_USER" ), diff --git a/library/dripline_config.hh b/library/dripline_config.hh index fd53d25a..1413e6fb 100644 --- a/library/dripline_config.hh +++ b/library/dripline_config.hh @@ -29,7 +29,7 @@ namespace dripline class DRIPLINE_API dripline_config : public scarab::param_node { public: - dripline_config( const std::string& a_auth_file = "" ); + dripline_config(); virtual ~dripline_config(); }; @@ -37,7 +37,11 @@ namespace dripline void add_dripline_options( scarab::main_app& an_app ); /// Add default authentication specification - void add_dripline_auth_spec( scarab::main_app& an_app ); + /// This can either be done with an authentication specification group or with an auth file, as determined by the a_use_auth_file flag. + /// The use of an auth file is being maintained for backwards compatibility, but is not preferred. + /// For these defaults, it will either specify an auth file or an auth-spec group, but not both. + /// If an auth-spec group is the default, but the user provides an auth file, the latter will override the former. + void add_dripline_auth_spec( scarab::main_app& an_app, bool a_use_auth_file=false ); } /* namespace dripline */ #endif /* DRIPLINE_DRIPLINE_CONFIG_HH_ */ diff --git a/testing/integration/README.md b/testing/integration/README.md index 2db1af8b..9fcb0608 100644 --- a/testing/integration/README.md +++ b/testing/integration/README.md @@ -9,6 +9,8 @@ If you build the image, we recommend you tag the image with something like `ghcr ## The Tests +The next two sub-sections detail the tests that are run. Skip to [the next section](#run-the-tests) to actually run the tests. + ### Broker Setup The first tests run demonstrate that the RabbitMQ broker has been configured correctly once the services are connected. We check that the expected exchanges and queues are in place by sending HTTP requests to the broker and validating the responses. The `newman` CLI application is used to run a Postman collection. diff --git a/testing/integration/authentications.json b/testing/integration/authentications.json index 4842ccd3..7210f8e0 100644 --- a/testing/integration/authentications.json +++ b/testing/integration/authentications.json @@ -1,6 +1,5 @@ { - "amqp": { - "broker": "rabbit-broker", + "dripline": { "username": "dripline", "password": "dripline" }, diff --git a/testing/integration/docker-compose-test.yaml b/testing/integration/docker-compose-test.yaml index 5fe7401a..71e00971 100644 --- a/testing/integration/docker-compose-test.yaml +++ b/testing/integration/docker-compose-test.yaml @@ -1,5 +1,3 @@ -version: '3' - services: test: image: ghcr.io/driplineorg/dripline-cpp:${IMG_TAG:-latest-testing} diff --git a/testing/integration/docker-compose.yaml b/testing/integration/docker-compose.yaml index 54cb4ade..18224a5f 100644 --- a/testing/integration/docker-compose.yaml +++ b/testing/integration/docker-compose.yaml @@ -1,5 +1,3 @@ -version: '3' - services: rabbit-broker: diff --git a/testing/test_agent.cc b/testing/test_agent.cc index dad5df82..a71c7134 100644 --- a/testing/test_agent.cc +++ b/testing/test_agent.cc @@ -6,9 +6,48 @@ */ #include "agent.hh" +#include "agent_config.hh" + +#include "application.hh" + +//#include "logger.hh" #include "catch.hpp" +//LOGGER( talog, "test_agent" ); + +TEST_CASE( "agent_configuration", "[agent]" ) +{ + using Catch::Matchers::Equals; + + // This setup should match what's done in dl_agent.cc + + scarab::main_app the_main; + dripline::agent the_agent; + + the_main.default_config() = dripline::agent_config(); + // Dripline authentication specification + dripline::add_dripline_auth_spec( the_main ); + + // end setup + + REQUIRE( the_main.default_config()["timeout"]().as_int() == 10 ); + REQUIRE( the_main.default_config()["dripline"].is_node() ); + + REQUIRE( the_main.default_config().has("auth-groups") ); + REQUIRE_THAT( the_main.default_config()["auth-groups"]["dripline"]["user"]["default"]().as_string(), Equals("guest") ); + REQUIRE_THAT( the_main.default_config()["auth-groups"]["dripline"]["password"]["default"]().as_string(), Equals("guest") ); + + // pre_callback() runs the configuration stages and authentication step + the_main.pre_callback(); + + REQUIRE( the_main.primary_config()["timeout"]().as_int() == 10 ); + REQUIRE( the_main.primary_config()["dripline"].is_node() ); + REQUIRE( the_main.primary_config().has("auth-groups") ); + REQUIRE_THAT( the_main.primary_config()["auth-groups"]["dripline"]["user"]["default"]().as_string(), Equals("guest") ); + REQUIRE_THAT( the_main.primary_config()["auth-groups"]["dripline"]["password"]["default"]().as_string(), Equals("guest") ); +} + TEST_CASE( "sub_agent_get", "[agent]" ) { std::unique_ptr< dripline::agent > t_agent( new dripline::agent() );