2 * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
4 * Contact: Rafal Krypa <r.krypa@samsung.com>
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License
20 * @author Rafal Krypa <r.krypa@samsung.com>
21 * @brief Wrapper class for Cynara interface
29 #include <dpl/log/log.h>
31 namespace SecurityManager {
34 CynaraAdminPolicy::CynaraAdminPolicy(const std::string &client, const std::string &user,
35 const std::string &privilege, Operation operation,
36 const std::string &bucket)
38 this->client = strdup(client.c_str());
39 this->user = strdup(user.c_str());
40 this->privilege = strdup(privilege.c_str());
41 this->bucket = strdup(bucket.c_str());
43 if (this->bucket == nullptr || this->client == nullptr ||
44 this->user == nullptr || this->privilege == nullptr) {
48 free(this->privilege);
49 ThrowMsg(CynaraException::OutOfMemory,
50 std::string("Error in CynaraAdminPolicy allocation."));
53 this->result = static_cast<int>(operation);
54 this->result_extra = nullptr;
57 CynaraAdminPolicy::CynaraAdminPolicy(const std::string &client, const std::string &user,
58 const std::string &privilege, const std::string &goToBucket,
59 const std::string &bucket)
61 this->bucket = strdup(bucket.c_str());
62 this->client = strdup(client.c_str());
63 this->user = strdup(user.c_str());
64 this->privilege = strdup(privilege.c_str());
65 this->result_extra = strdup(goToBucket.c_str());
66 this->result = CYNARA_ADMIN_BUCKET;
68 if (this->bucket == nullptr || this->client == nullptr ||
69 this->user == nullptr || this->privilege == nullptr ||
70 this->result_extra == nullptr) {
74 free(this->privilege);
75 free(this->result_extra);
76 ThrowMsg(CynaraException::OutOfMemory,
77 std::string("Error in CynaraAdminPolicy allocation."));
81 CynaraAdminPolicy::CynaraAdminPolicy(CynaraAdminPolicy &&that)
86 privilege = that.privilege;
87 result_extra = that.result_extra;
90 that.bucket = nullptr;
91 that.client = nullptr;
93 that.privilege = nullptr;
94 that.result_extra = nullptr;
97 CynaraAdminPolicy::~CynaraAdminPolicy()
102 free(this->privilege);
103 free(this->result_extra);
106 static void checkCynaraAdminError(int result, const std::string &msg)
109 case CYNARA_ADMIN_API_SUCCESS:
111 case CYNARA_ADMIN_API_OUT_OF_MEMORY:
112 ThrowMsg(CynaraException::OutOfMemory, msg);
113 case CYNARA_ADMIN_API_INVALID_PARAM:
114 ThrowMsg(CynaraException::InvalidParam, msg);
115 case CYNARA_ADMIN_API_SERVICE_NOT_AVAILABLE:
116 ThrowMsg(CynaraException::ServiceNotAvailable, msg);
118 ThrowMsg(CynaraException::UnknownError, msg);
122 CynaraAdmin::CynaraAdmin()
124 checkCynaraAdminError(
125 cynara_admin_initialize(&m_CynaraAdmin),
126 "Cannot connect to Cynara administrative interface.");
129 CynaraAdmin::~CynaraAdmin()
131 cynara_admin_finish(m_CynaraAdmin);
134 void CynaraAdmin::SetPolicies(const std::vector<CynaraAdminPolicy> &policies)
136 std::vector<const struct cynara_admin_policy *> pp_policies(policies.size() + 1);
138 LogDebug("Sending " << policies.size() << " policies to Cynara");
139 for (std::size_t i = 0; i < policies.size(); ++i) {
140 pp_policies[i] = static_cast<const struct cynara_admin_policy *>(&policies[i]);
141 LogDebug("policies[" << i << "] = {" <<
142 ".bucket = " << pp_policies[i]->bucket << ", " <<
143 ".client = " << pp_policies[i]->client << ", " <<
144 ".user = " << pp_policies[i]->user << ", " <<
145 ".privilege = " << pp_policies[i]->privilege << ", " <<
146 ".result = " << pp_policies[i]->result << ", " <<
147 ".result_extra = " << pp_policies[i]->result_extra << "}");
150 pp_policies[policies.size()] = nullptr;
152 checkCynaraAdminError(
153 cynara_admin_set_policies(m_CynaraAdmin, pp_policies.data()),
154 "Error while updating Cynara policy.");
157 void CynaraAdmin::UpdatePackagePolicy(
158 const std::string &label,
159 const std::string &user,
160 const std::vector<std::string> &oldPrivileges,
161 const std::vector<std::string> &newPrivileges)
163 CynaraAdmin cynaraAdmin;
164 std::vector<CynaraAdminPolicy> policies;
166 // Perform sort-merge join on oldPrivileges and newPrivileges.
167 // Assume that they are already sorted and without duplicates.
168 auto oldIter = oldPrivileges.begin();
169 auto newIter = newPrivileges.begin();
171 while (oldIter != oldPrivileges.end() && newIter != newPrivileges.end()) {
172 int compare = oldIter->compare(*newIter);
174 LogDebug("(user = " << user << " label = " << label << ") " <<
175 "keeping privilege " << *newIter);
179 } else if (compare < 0) {
180 LogDebug("(user = " << user << " label = " << label << ") " <<
181 "removing privilege " << *oldIter);
182 policies.push_back(CynaraAdminPolicy(label, user, *oldIter,
183 CynaraAdminPolicy::Operation::Delete));
186 LogDebug("(user = " << user << " label = " << label << ") " <<
187 "adding privilege " << *newIter);
188 policies.push_back(CynaraAdminPolicy(label, user, *newIter,
189 CynaraAdminPolicy::Operation::Allow));
194 for (; oldIter != oldPrivileges.end(); ++oldIter) {
195 LogDebug("(user = " << user << " label = " << label << ") " <<
196 "removing privilege " << *oldIter);
197 policies.push_back(CynaraAdminPolicy(label, user, *oldIter,
198 CynaraAdminPolicy::Operation::Delete));
201 for (; newIter != newPrivileges.end(); ++newIter) {
202 LogDebug("(user = " << user << " label = " << label << ") " <<
203 "adding privilege " << *newIter);
204 policies.push_back(CynaraAdminPolicy(label, user, *newIter,
205 CynaraAdminPolicy::Operation::Allow));
208 cynaraAdmin.SetPolicies(policies);
212 } // namespace SecurityManager