From: Remi Gacogne Date: Wed, 17 Oct 2018 15:28:58 +0000 (+0200) Subject: dnsdist: Add a 'creationOrder' field to rules X-Git-Tag: dnsdist-1.3.3~14^2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=refs%2Fpull%2F7078%2Fhead;p=thirdparty%2Fpdns.git dnsdist: Add a 'creationOrder' field to rules --- diff --git a/pdns/dnsdist-lua-actions.cc b/pdns/dnsdist-lua-actions.cc index f59ec3353f..6e92336347 100644 --- a/pdns/dnsdist-lua-actions.cc +++ b/pdns/dnsdist-lua-actions.cc @@ -1004,11 +1004,12 @@ static void addAction(GlobalStateHolder > *someRulActions, luadnsrule_ setLuaSideEffect(); boost::uuids::uuid uuid; - parseRuleParams(params, uuid); + uint64_t creationOrder; + parseRuleParams(params, uuid, creationOrder); auto rule=makeRule(var); - someRulActions->modify([rule, action, uuid](vector& rulactions){ - rulactions.push_back({rule, action, uuid}); + someRulActions->modify([rule, action, uuid, creationOrder](vector& rulactions){ + rulactions.push_back({rule, action, uuid, creationOrder}); }); } @@ -1016,10 +1017,11 @@ void setupLuaActions() { g_lua.writeFunction("newRuleAction", [](luadnsrule_t dnsrule, std::shared_ptr action, boost::optional params) { boost::uuids::uuid uuid; - parseRuleParams(params, uuid); + uint64_t creationOrder; + parseRuleParams(params, uuid, creationOrder); auto rule=makeRule(dnsrule); - DNSDistRuleAction ra({rule, action, uuid}); + DNSDistRuleAction ra({rule, action, uuid, creationOrder}); return std::make_shared(ra); }); diff --git a/pdns/dnsdist-lua-rules.cc b/pdns/dnsdist-lua-rules.cc index 973047ae7f..28630b1fe7 100644 --- a/pdns/dnsdist-lua-rules.cc +++ b/pdns/dnsdist-lua-rules.cc @@ -68,8 +68,10 @@ static boost::uuids::uuid makeRuleID(std::string& id) return gen(id); } -void parseRuleParams(boost::optional params, boost::uuids::uuid& uuid) +void parseRuleParams(boost::optional params, boost::uuids::uuid& uuid, uint64_t& creationOrder) { + static uint64_t s_creationOrder = 0; + string uuidStr; if (params) { @@ -79,6 +81,7 @@ void parseRuleParams(boost::optional params, boost::uuids::uuid } uuid = makeRuleID(uuidStr); + creationOrder = s_creationOrder++; } typedef std::unordered_map > > > ruleparams_t; @@ -101,11 +104,11 @@ static void showRules(GlobalStateHolder > *someRulActions, boost::opti auto rules = someRulActions->getLocal(); if (showUUIDs) { - boost::format fmt("%-3d %-38s %9d %-56s %s\n"); - g_outputBuffer += (fmt % "#" % "UUID" % "Matches" % "Rule" % "Action").str(); + boost::format fmt("%-3d %-38s %9d %9d %-56s %s\n"); + g_outputBuffer += (fmt % "#" % "UUID" % "Cr. Order" % "Matches" % "Rule" % "Action").str(); for(const auto& lim : *rules) { string name = lim.d_rule->toString().substr(0, truncateRuleWidth); - g_outputBuffer += (fmt % num % boost::uuids::to_string(lim.d_id) % lim.d_rule->d_matches % name % lim.d_action->toString()).str(); + g_outputBuffer += (fmt % num % boost::uuids::to_string(lim.d_id) % lim.d_creationOrder % lim.d_rule->d_matches % name % lim.d_action->toString()).str(); ++num; } } diff --git a/pdns/dnsdist-lua.hh b/pdns/dnsdist-lua.hh index b14dde8f44..7ab5d963ca 100644 --- a/pdns/dnsdist-lua.hh +++ b/pdns/dnsdist-lua.hh @@ -80,7 +80,7 @@ private: typedef boost::variant>, std::shared_ptr, DNSName, vector > > luadnsrule_t; std::shared_ptr makeRule(const luadnsrule_t& var); typedef std::unordered_map > luaruleparams_t; -void parseRuleParams(boost::optional params, boost::uuids::uuid& uuid); +void parseRuleParams(boost::optional params, boost::uuids::uuid& uuid, uint64_t& creationOrder); typedef NetmaskTree nmts_t; diff --git a/pdns/dnsdist-web.cc b/pdns/dnsdist-web.cc index 810a4b9433..44b5977f05 100644 --- a/pdns/dnsdist-web.cc +++ b/pdns/dnsdist-web.cc @@ -231,6 +231,7 @@ static json11::Json::array someResponseRulesToJson(GlobalStateHolder>* for(const auto& a : *localResponseRules) { Json::object rule{ {"id", num++}, + {"creationOrder", (double)a.d_creationOrder}, {"uuid", boost::uuids::to_string(a.d_id)}, {"matches", (double)a.d_rule->d_matches}, {"rule", a.d_rule->toString()}, @@ -589,6 +590,7 @@ static void connectionThread(int sock, ComboAddress remote, string password, str for(const auto& a : *localRules) { Json::object rule{ {"id", num++}, + {"creationOrder", (double)a.d_creationOrder}, {"uuid", boost::uuids::to_string(a.d_id)}, {"matches", (double)a.d_rule->d_matches}, {"rule", a.d_rule->toString()}, diff --git a/pdns/dnsdist.hh b/pdns/dnsdist.hh index 08393675e8..e457b58066 100644 --- a/pdns/dnsdist.hh +++ b/pdns/dnsdist.hh @@ -896,6 +896,7 @@ struct DNSDistRuleAction std::shared_ptr d_rule; std::shared_ptr d_action; boost::uuids::uuid d_id; + uint64_t d_creationOrder; }; struct DNSDistResponseRuleAction @@ -903,6 +904,7 @@ struct DNSDistResponseRuleAction std::shared_ptr d_rule; std::shared_ptr d_action; boost::uuids::uuid d_id; + uint64_t d_creationOrder; }; extern GlobalStateHolder> g_dynblockSMT; diff --git a/regression-tests.dnsdist/test_API.py b/regression-tests.dnsdist/test_API.py index 5d0483e10d..b5d2a167a0 100644 --- a/regression-tests.dnsdist/test_API.py +++ b/regression-tests.dnsdist/test_API.py @@ -78,21 +78,15 @@ class TestAPIBasics(DNSDistTest): self.assertEquals(content['daemon_type'], 'dnsdist') - rule_groups = ['response-rules', 'cache-hit-response-rules', 'self-answered-response-rules'] - for key in ['version', 'acl', 'local', 'rules', 'servers', 'frontends', 'pools'] + rule_groups: + rule_groups = ['response-rules', 'cache-hit-response-rules', 'self-answered-response-rules', 'rules'] + for key in ['version', 'acl', 'local', 'servers', 'frontends', 'pools'] + rule_groups: self.assertIn(key, content) - for rule in content['rules']: - for key in ['id', 'matches', 'rule', 'action', 'uuid']: - self.assertIn(key, rule) - for key in ['id', 'matches']: - self.assertTrue(rule[key] >= 0) - for rule_group in rule_groups: for rule in content[rule_group]: - for key in ['id', 'matches', 'rule', 'action', 'uuid']: + for key in ['id', 'creationOrder', 'matches', 'rule', 'action', 'uuid']: self.assertIn(key, rule) - for key in ['id', 'matches']: + for key in ['id', 'creationOrder', 'matches']: self.assertTrue(rule[key] >= 0) for server in content['servers']: