Minor refactoring. Unit tests for policy enforcement groups added.
[platform/core/security/suspicious-activity-monitor.git] / device-agent / samonitor / dpm / policy_enforce.cpp
1 /**
2  * Samsung Ukraine R&D Center (SRK under a contract between)
3  * LLC "Samsung Electronics Co", Ltd (Seoul, Republic of Korea)
4  * Copyright (C) 2018 Samsung Electronics Co., Ltd. All rights reserved.
5  */
6 /**
7  * @file   policy_enforce.cpp
8  * @brief  Policy group agregator
9  * @date   Created Jul 01, 2016
10  * @author Mail to: <A HREF="mailto:a.volkov@samsung.com">Aleksey Volkov, a.volkov@samsung.com</A>
11  */
12
13 #include "policy_enforce.h"
14 #include "tvext_enforce.h"
15 #include "logging.h"
16 #include "samonitor_tag.h"
17 #include <jsoncpp/json/reader.h>
18 #include <iostream>
19
20 namespace core
21 {
22
23 PolicyEnforce::PolicyEnforce()
24     : m_policyGroups()
25 {
26 }
27
28 PolicyEnforce::Result
29 PolicyEnforce::ParsePolicy(const std::string& jsonString)
30 {
31     LOG_D(TAG, "PolicyEnforce::ParsePolicy");
32     Result result = Result::SUCCESS;
33
34     try {
35         if (jsonString.empty()) {
36             return Result::ERROR_PARSING; //TODO
37         }
38
39         Json::Value policyGroupList;
40         Json::Reader reader;
41
42         bool parsingSuccessful = reader.parse(jsonString.c_str(), policyGroupList);
43
44         if (!parsingSuccessful || policyGroupList.empty()) {
45             return Result::ERROR_PARSING; //TODO
46         }
47
48         for (auto& node : policyGroupList) {
49             std::string groupName = node.get("group", "").asString();
50
51             if (groupName.empty()) {
52                 LOG_E(TAG, "No group node in class");
53                 return Result::ERROR_PARSING;
54             }
55
56             PolicyGroupMap::iterator it = m_policyGroups.find(groupName);
57
58             if (it == m_policyGroups.end()) {
59                 //FIXME Set error if error was occured
60                 LOG_E(TAG, "Group \"%s\" not found", groupName.c_str());
61                 continue; //TODO
62             }
63
64             IPolicyGroupEnforce& groupEnforce = *(it->second);
65
66             if (!groupEnforce.Init(/*m_context*/)) {
67                 LOG_E(TAG, "Group \"%s\" init failed", groupName.c_str());
68                 result = Result::ERROR_GROUP;
69                 continue; //TODO
70             }
71
72             Json::Value policiesList = node.get("policies", "");
73
74             if (policiesList.empty()) {
75                 LOG_E(TAG, "No group node in class");
76                 return Result::ERROR_PARSING;
77             }
78
79             if (!groupEnforce.ParseGroup(policiesList)) {
80                 LOG_E(TAG, "Failed to apply policy group: %s", groupName.c_str());
81                 result = Result::ERROR_GROUP;
82             }
83
84             groupEnforce.Deinit();
85         }
86     } catch (std::exception& e) {
87         LOG_E(TAG, "Failed to apply policy, exception: %s", e.what());
88         LOG_E(TAG, "Policy which caused this exception: %s", jsonString.c_str());
89         result = Result::ERROR_PARSING;
90     }
91
92     return result;
93 }
94
95 PolicyEnforce& PolicyEnforce::GetInstance()
96 {
97     static PolicyEnforce policyEnforce;
98
99     return policyEnforce;
100 }
101
102 void PolicyEnforce::RegisterGroup(IPolicyGroupEnforcePtr groupEnforce, const std::string& groupName)
103 {
104     m_policyGroups[groupName] = groupEnforce;
105 }
106
107 } //namespace core