Rule(Action action = Action::Always, Filter filter = Filter::Exit);
virtual ~Rule();
- Rule(Rule &&) = delete;
Rule(const std::vector<char> &rule);
Rule(const Rule &rule);
Rule &operator = (const Rule &rule) = default;
return *this;
}
- bool operator == (const Rule &rule)
+ bool operator == (const Rule &rule) const
{
return (data() == rule.data());
}
* See the License for the specific language governing permissions and
* limitations under the License
*/
+#include <algorithm>
#include "rule-apply-engine.h"
namespace {
{
}
-void RuleApplyEngine::addRule(Audit &audit, const std::vector<char> &data)
+void RuleApplyEngine::addRule(Audit &audit, const Rule &rule)
{
- Rule rule(data);
+ //TODO: add verifier of rule
for (auto &r : adminList) {
if (r == rule)
return;
}
- audit.addRule(data);
- audit.removeRule(data);
-
adminList.emplace_back(rule);
removeAll(audit);
addAll(audit);
}
-void RuleApplyEngine::removeRule(Audit &audit, const std::vector<char> &data)
+void RuleApplyEngine::removeRule(Audit &audit, const Rule &rule)
{
- Rule rule(data);
- bool removed = false;
- for (auto r = adminList.begin(); r != adminList.end(); r++) {
- if (*r == rule) {
- adminList.erase(r);
- removed = true;
- break;
- }
- }
+ auto it = std::find_if(adminList.begin(), adminList.end(), [&](const Rule &r) {
+ return rule == r;});
- if (!removed)
- throw runtime::Exception("Failed to remove rule");
+ if (it != adminList.end())
+ adminList.erase(it);
+ else
+ throw runtime::Exception("The rule does not exist");
removeAll(audit);
optimizedList.clear();
addNeverRules();
- for (auto r : adminList) {
+ for (const auto &r : adminList) {
optimize(r);
}
}
}
-void RuleApplyEngine::optimize(Rule &rule)
+void RuleApplyEngine::optimize(const Rule &rule)
{
- rule.unsetCondition(FieldType::Tag);
+ Rule result(rule);
+ result.unsetCondition(FieldType::Tag);
for (auto r = optimizedList.begin()+1; r != optimizedList.end(); r++) {
- if (*r == rule)
+ if (*r == result)
return;
- bool ret = Rule::combine(*r, rule);
+ bool ret = Rule::combine(*r, result);
if (ret && r->getMask().empty())
optimizedList.erase(r);
}
- if (!rule.getMask().empty())
- optimizedList.emplace_back(rule);
+ if (!result.getMask().empty())
+ optimizedList.emplace_back(result);
//TODO: relocate optimizedList
for (auto r = optimizedList.begin()+1; r != optimizedList.end(); r++) {
RuleApplyEngine();
~RuleApplyEngine();
- void addRule(Audit &audit, const std::vector<char> &data);
- void removeRule(Audit &audit, const std::vector<char> &data);
+ void addRule(Audit &audit, const Rule &rule);
+ void removeRule(Audit &audit, const Rule &rule);
RuleList getRules() const;
private:
void removeAll(Audit &audit);
void addAll(Audit &audit);
- void optimize(Rule &r);
+ void optimize(const Rule &rule);
void addNeverRules();
void applyNeverRules();
private:
int RuleManagement::addRule(std::vector<char> data)
{
try {
- context.getRuleApplyEngine().addRule(context.getAudit(), data);
+ context.getRuleApplyEngine().addRule(context.getAudit(), Rule{data});
} catch (runtime::Exception &e) {
ERROR("Failed to add rule : " + std::string(e.what()));
return -1;
int RuleManagement::removeRule(std::vector<char> data)
{
try {
- context.getRuleApplyEngine().removeRule(context.getAudit(), data);
+ context.getRuleApplyEngine().removeRule(context.getAudit(), Rule{data});
} catch (runtime::Exception &e) {
ERROR("Failed to remove rule : " + std::string(e.what()));
return -1;