1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
// Copyright (C) 2022 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Kea Hooks Basic
// Commercial End User License Agreement v2.0. See COPYING file in the premium/
// directory.

#include <config.h>

#include <rbac.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <rbac_config.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <cc/command_interpreter.h>
#include <hooks/callout_manager.h>
#include <hooks/hooks.h>
#include <http/post_request_json.h>
#include <http/response_json.h>
#include <testutils/log_utils.h>
#include <cstdlib><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.

using namespace isc;
using namespace isc::config;
using namespace isc::data;
using namespace isc::dhcp::test;
using namespace isc::hooks;
using namespace isc::http;
using namespace isc::rbac;
using namespace std;

/// @brief Declarations of callout points.
extern "C" {
    extern int auth(CalloutHandle& handle);
    extern int response(CalloutHandle& handle);
}

namespace {

/// @brief RBAC configurations.
const char* CONFIGS[] = {
    // configuration 0: remote address, accept only list-commands on 127.0.0.1.
    "{\n"
    "    \"assign-role-method\": \"remote-address\",\n"
    "    \"roles\":\n"
    "    [\n"
    "        {\n"
    "            \"name\": \"127.0.0.1\",\n"
    "            \"accept-commands\":\n"
    "            {\n"
    "                \"commands\":\n"
    "                [\n"
    "                    \"list-commands\"\n"
    "                ]\n"
    "            },\n"
    "            \"other-commands\": \"reject\"\n"
    "        }\n"
    "    ]\n"
    "}\n",

    // configuration 1: same with other-commands to accept.
    "{\n"
    "    \"assign-role-method\": \"remote-address\",\n"
    "    \"roles\":\n"
    "    [\n"
    "        {\n"
    "            \"name\": \"127.0.0.1\",\n"
    "            \"accept-commands\":\n"
    "            {\n"
    "                \"commands\":\n"
    "                [\n"
    "                    \"list-commands\"\n"
    "                ]\n"
    "            },\n"
    "            \"other-commands\": \"accept\"\n"
    "        }\n"
    "    ]\n"
    "}\n",

    // configuration 2: now with require-tls.
    "{\n"
    "    \"assign-role-method\": \"remote-address\",\n"
    "    \"require-tls\": true,\n"
    "    \"roles\":\n"
    "    [\n"
    "        {\n"
    "            \"name\": \"127.0.0.1\",\n"
    "            \"accept-commands\":\n"
    "            {\n"
    "                \"commands\":\n"
    "                [\n"
    "                    \"list-commands\"\n"
    "                ]\n"
    "            },\n"
    "            \"other-commands\": \"reject\"\n"
    "        }\n"
    "    ]\n"
    "}\n",

    // configuration 3: now with the list-commands response filter.
    "{\n"
    "    \"assign-role-method\": \"remote-address\",\n"
    "    \"roles\":\n"
    "    [\n"
    "        {\n"
    "            \"name\": \"127.0.0.1\",\n"
    "            \"accept-commands\":\n"
    "            {\n"
    "                \"commands\":\n"
    "                [\n"
    "                    \"list-commands\",\n"
    "                    \"config-get\",\n"
    "                    \"build-report\"\n"
    "                ]\n"
    "            },\n"
    "            \"other-commands\": \"reject\",\n"
    "            \"response-filters\":\n"
    "            [\n"
    "                \"list-commands\"\n"
    "            ]\n"
    "        }\n"
    "    ]\n"
    "}\n"

};

/// @brief Test fixture for checking RBAC callouts.
class CalloutTest : public LogContentTest {
public:

    /// @brief Constructor.
    CalloutTest() : co_manager_(new CalloutManager(1)) {
        roleAssign.reset();
        roleConfigTable.clear();
        defaultRoleConfig.reset();
        unknownRoleConfig.reset();
        aclTable.clear();
        apiTable.clear();
        apiAccesses.clear();
        apiHooks.clear();
        responseFilterTable.clear();
        rbacConfig.init();
    }

    /// @brief Destructor.
    virtual ~CalloutTest() {
        if (roleAssign) {
            roleAssign->setup(false);
        }
        roleAssign.reset();
        roleConfigTable.clear();
        defaultRoleConfig.reset();
        unknownRoleConfig.reset();
        aclTable.clear();
        apiTable.clear();
        apiAccesses.clear();
        apiHooks.clear();
        responseFilterTable.clear();
    }

    /// @brief Configure.
    ///
    /// @param config The configuration.
    void configure(const string& config) {
        ElementPtr json;
        EXPECT_NO_THROW(json = Element::fromJSON(config));
        ASSERT_TRUE(json);
        json->set("api-files", Element::create(string(API_DIR)));
        EXPECT_NO_THROW(Config::parse(json));
    }

    /// @brief Fetches the callout manager instance.
    boost::shared_ptr<CalloutManager>getCalloutManager() {
        return (co_manager_);
    }

    /// @brief Test auth callout.
    ///
    /// @param request The request.
    /// @param response The response.
    void authCalloutCall(HttpRequestPtr request,
                         HttpResponseJsonPtr& response) {
        // Get callout handle.
        CalloutHandle handle(getCalloutManager());

        handle.setArgument("request", request);
        handle.setArgument("response", response);

        int ret;
        ASSERT_NO_THROW(ret = auth(handle));
        EXPECT_EQ(0, ret);

        handle.getArgument("response", response);
    }

    /// @brief Test chained callouts.
    ///
    /// @param request The request.
    /// @param http_response The response.
    void calloutCalls(HttpRequestPtr request,
                      HttpResponseJsonPtr http_response) {
        // Get callout handle.
        CalloutHandle handle(getCalloutManager());

        handle.setArgument("request", request);
        HttpResponseJsonPtr auth_response;
        handle.setArgument("response", auth_response);

        int ret;
        ASSERT_NO_THROW(ret = auth(handle));
        EXPECT_EQ(0, ret);

        handle.getArgument("response", auth_response);
        EXPECT_FALSE(auth_response);

        // response requires a post processing response.
        handle.setArgument("response", http_response);

        ASSERT_NO_THROW(ret = response(handle));
        EXPECT_EQ(0, ret);
    }

    /// @brief Create HTTP Request.
    ///
    /// @param command The command name.
    /// @param service The service (can be empty).
    ///
    /// @return The HTTP request.
    HttpRequestPtr createHttpRequest(const string& command,
                                     const string& service) {
        PostHttpRequestJsonPtr request(new PostHttpRequestJson(
                HttpRequest::Method::HTTP_POST, "/",
                HttpVersion::HTTP_11()));
        const ConstElementPtr& body = createCommand(command, service);
        request->setBodyAsJson(body);
        request->finalize();
        return (request);
    }

    /// @brief Create HTTP Response.
    ///
    /// @param answer JSON answer.
    ///
    /// @return The HTTP response.
    HttpResponseJsonPtr createHttpResponse(ConstElementPtr answer) {
        HttpResponseJsonPtr response(new HttpResponseJson(
                HttpVersion::HTTP_11(),
                HttpStatusCode::OK));
        response->setBodyAsJson(answer);
        response->finalize();
        return (response);
    }

    /// @brief Callout manager accessed by this CalloutHandle.
    boost::shared_ptr<CalloutManager> co_manager_;
};

/// @brief This test verifies that accept works as expected.
TEST_F(CalloutTest, accept) {<--- syntax error
    configure(CONFIGS[0]);
    HttpRequestPtr request = createHttpRequest("list-commands", "dhcp4");
    request->setRemote("127.0.0.1");
    HttpResponseJsonPtr response;
    authCalloutCall(request, response);
    EXPECT_FALSE(response);
    string expected = "RBAC_TRACE_AUTH_ACCEPT ";
    expected += "Role configuration '127.0.0.1' for role '127.0.0.1' ";
    expected += "has accepted command 'list-commands'.";
    EXPECT_EQ(1, countFile(expected));
}

/// @brief This test verifies that reject works as expected.
TEST_F(CalloutTest, reject) {
    configure(CONFIGS[0]);
    HttpRequestPtr request = createHttpRequest("status-get", "dhcp6");
    request->setRemote("127.0.0.1");
    HttpResponseJsonPtr response;
    authCalloutCall(request, response);
    EXPECT_TRUE(response);
    string expected = "RBAC_TRACE_AUTH_REJECT ";
    expected += "Role configuration '127.0.0.1' for role '127.0.0.1' ";
    expected += "has rejected command 'status-get'.";
    EXPECT_EQ(1, countFile(expected));
}

/// @brief This test verifies that other commands works as expected.
TEST_F(CalloutTest, otherCommands) {
    configure(CONFIGS[1]);
    HttpRequestPtr request = createHttpRequest("status-get", "dhcp4");
    request->setRemote("127.0.0.1");
    HttpResponseJsonPtr response;
    authCalloutCall(request, response);
    EXPECT_FALSE(response);
    string expected = "RBAC_TRACE_AUTH_ACCEPT ";
    expected += "Role configuration '127.0.0.1' for role '127.0.0.1' ";
    expected += "has accepted command 'status-get'.";
    EXPECT_EQ(1, countFile(expected));
}

/// @brief This test verifies that default role works as expected.
TEST_F(CalloutTest, defaultRole) {
    configure(CONFIGS[0]);
    HttpRequestPtr request = createHttpRequest("list-commands", "dhcp6");
    // no remote.
    HttpResponseJsonPtr response;
    authCalloutCall(request, response);
    EXPECT_TRUE(response);
    string expected = "RBAC_TRACE_AUTH_REJECT ";
    expected += "Role configuration 'default/reject' for role '' ";
    expected += "has rejected command 'list-commands'.";
    EXPECT_EQ(1, countFile(expected));
}

/// @brief This test verifies that unknown role works as expected.
TEST_F(CalloutTest, unknowntRole) {
    configure(CONFIGS[0]);
    HttpRequestPtr request = createHttpRequest("list-commands", "dhcp4");
    request->setRemote("::1");
    HttpResponseJsonPtr response;
    authCalloutCall(request, response);
    EXPECT_TRUE(response);
    string expected = "RBAC_TRACE_AUTH_REJECT ";
    expected += "Role configuration 'unknown/reject' for role '::1' ";
    expected += "has rejected command 'list-commands'.";
    EXPECT_EQ(1, countFile(expected));
}

/// @brief This test verifies that require TLS works as expected.
TEST_F(CalloutTest, requireTls) {
    configure(CONFIGS[2]);
    HttpRequestPtr request = createHttpRequest("list-commands", "dhcp6");
    // Require TLS default value is false.
    HttpResponseJsonPtr response;
    authCalloutCall(request, response);
    EXPECT_TRUE(response);
    EXPECT_EQ(1, countFile("RBAC_TRACE_AUTH_NO_TLS_REJECT"));
}

/// @brief This test verifies that list-commands response filter works
/// as expected.
TEST_F(CalloutTest, listCommands) {
    configure(CONFIGS[3]);
    HttpRequestPtr request = createHttpRequest("list-commands", "dhcp4");
    request->setRemote("127.0.0.1");

    ElementPtr answer_list = Element::createList();
    ElementPtr answer = Element::createMap();
    answer_list->add(answer);
    ElementPtr result = Element::create(CONTROL_RESULT_SUCCESS);
    answer->set(CONTROL_RESULT, result);
    ElementPtr args = Element::createList();
    answer->set(CONTROL_ARGUMENTS, args);
    args->add(Element::create(string("list-commands")));
    args->add(Element::create(string("config-get")));
    args->add(Element::create(string("build-report")));
    string expected = answer_list->str();
    args->add(Element::create(string("config-set")));
    args->add(Element::create(string("shutdown")));
    HttpResponseJsonPtr response = createHttpResponse(answer_list);

    calloutCalls(request, response);
    ConstElementPtr body = response->getBodyAsJson();
    ASSERT_TRUE(body);
    EXPECT_EQ(expected, body->str());

    expected = "RBAC_TRACE_RESPONSE_MODIFIED The response has been modified ";
    expected += "by a response filter in response callout.";
    EXPECT_EQ(1, countFile(expected));
}

} // end of anonymous namespace