]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#1421] Added callout context
authorFrancis Dupont <fdupont@isc.org>
Sat, 7 Nov 2020 14:32:01 +0000 (15:32 +0100)
committerTomek Mrugalski <tomek@isc.org>
Fri, 20 Nov 2020 10:44:41 +0000 (11:44 +0100)
src/bin/agent/tests/basic_auth_library.cc
src/bin/agent/tests/ca_response_creator_unittests.cc

index f3581b6e1208ef17d78982182f24af04125aa076..9731d557f0fa7c79da0e7c0ce866f93ae354a647 100644 (file)
@@ -201,11 +201,17 @@ auth(CalloutHandle& handle) {
     }
 
     // Modify request.
+    int extra = 0;
+    ConstElementPtr extra_elem = command->get("extra");
     ElementPtr mutable_command = boost::const_pointer_cast<Element>(command);
-    if (command->contains("service")) {
-        mutable_command->remove("service");
+    if (extra_elem) {
+        if (extra_elem->getType() == Element::integer) {
+            extra = extra_elem->intValue();
+        }
+        mutable_command->remove("extra");
         request_json->setBodyAsJson(command);
     }
+    handle.setContext("extra", extra);
 
     // Perform authentication.
     response = impl->config_->checkAuth(*impl->creator_, request);
@@ -263,7 +269,15 @@ response(CalloutHandle& handle) {
         return (0);
     }
     ElementPtr mutable_answer = boost::const_pointer_cast<Element>(answer);
-    mutable_answer->set("comment", Element::create(string("got")));
+    try {
+        int extra = 0;
+        handle.getContext("extra", extra);
+        mutable_answer->set("got", Element::create(extra));
+    } catch (const NoSuchCalloutContext&) {
+        std::cerr << "can't find 'extra' context\n";
+    } catch (...) {
+        std::cerr << "getContext('extra') failed\n";
+    }
     response->setBodyAsJson(body);
 
     // Set parameters.
index c49ab858a98826183d202c3471a9d76ff1d9e702..eb3658146a6ef8b0a4c0828771dbd95153fcd479 100644 (file)
@@ -10,6 +10,7 @@
 #include <agent/ca_command_mgr.h>
 #include <agent/ca_response_creator.h>
 #include <cc/command_interpreter.h>
+#include <cryptolink/crypto_rng.h>
 #include <hooks/hooks_manager.h>
 #include <http/basic_auth_config.h>
 #include <http/post_request.h>
@@ -326,8 +327,14 @@ TEST_F(CtrlAgentResponseCreatorTest, hookNoAuth) {
     setBasicContext(request_);
 
     // Body: "list-commands" is natively supported by the command manager.
-    request_->context()->body_ = "{ \"command\": \"list-commands\","
-        " \"service\": [ ] }";
+    auto r32 = isc::cryptolink::random(4);
+    ASSERT_EQ(4, r32.size());
+    int extra = r32[0];
+    extra = (extra << 8) | r32[1];
+    extra = (extra << 8) | r32[2];
+    extra = (extra << 8) | r32[3];
+    request_->context()->body_ = "{ \"command\": \"list-commands\", ";
+    request_->context()->body_ += "\"extra\": " + std::to_string(extra) + " }";
 
     // All requests must be finalized before they can be processed.
     ASSERT_NO_THROW(request_->finalize());
@@ -352,7 +359,7 @@ TEST_F(CtrlAgentResponseCreatorTest, hookNoAuth) {
     ASSERT_NO_THROW(response = response_creator_.createHttpResponse(request_));
     ASSERT_TRUE(response);
 
-    // Request should have no service.
+    // Request should have no extra.
     EXPECT_EQ("{ \"command\": \"list-commands\" }",
               request_->context()->body_);
 
@@ -374,7 +381,18 @@ TEST_F(CtrlAgentResponseCreatorTest, hookBasicAuth) {
     setBasicContext(request_);
 
     // Body: "list-commands" is natively supported by the command manager.
-    request_->context()->body_ = "{ \"command\": \"list-commands\" }";
+    auto r32 = isc::cryptolink::random(4);
+    ASSERT_EQ(4, r32.size());
+    int extra = r32[0];
+    extra = (extra << 8) | r32[1];
+    extra = (extra << 8) | r32[2];
+    extra = (extra << 8) | r32[3];
+    if (extra == 0) {
+        extra = 1;
+    }
+    std::string extra_str = std::to_string(extra);
+    request_->context()->body_ = "{ \"command\": \"list-commands\", ";
+    request_->context()->body_ += "\"extra\": " + extra_str + " }";
 
     // Add basic HTTP authentication header.
     const BasicHttpAuth& basic_auth = BasicHttpAuth("foo", "bar");
@@ -405,6 +423,10 @@ TEST_F(CtrlAgentResponseCreatorTest, hookBasicAuth) {
     ASSERT_NO_THROW(response = response_creator_.createHttpResponse(request_));
     ASSERT_TRUE(response);
 
+    // Request should have no extra.
+    EXPECT_EQ("{ \"command\": \"list-commands\" }",
+              request_->context()->body_);
+
     // Response must be convertible to HttpResponseJsonPtr.
     HttpResponseJsonPtr response_json = boost::dynamic_pointer_cast<
         HttpResponseJson>(response);
@@ -417,7 +439,8 @@ TEST_F(CtrlAgentResponseCreatorTest, hookBasicAuth) {
     EXPECT_TRUE(response_json->toString().find("\"result\": 0") !=
                 std::string::npos);
     // Response must contain JSON body with "comment": "got".
-    EXPECT_TRUE(response_json->toString().find("\"comment\": \"got\"") !=
+    std::string expected = "\"got\": " + extra_str + ", ";
+    EXPECT_TRUE(response_json->toString().find(expected) !=
                 std::string::npos);
 }