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 namespace SecurityManager {
32 CynaraAdminPolicy::CynaraAdminPolicy(const std::string &client, const std::string &user,
33 const std::string &privilege, Operation operation,
34 const std::string &bucket)
36 this->client = strdup(client.c_str());
37 this->user = strdup(user.c_str());
38 this->privilege = strdup(privilege.c_str());
39 this->bucket = strdup(bucket.c_str());
41 if (this->bucket == nullptr || this->client == nullptr ||
42 this->user == nullptr || this->privilege == nullptr) {
46 free(this->privilege);
47 throw std::bad_alloc();
50 this->result = static_cast<int>(operation);
51 this->result_extra = nullptr;
54 CynaraAdminPolicy::CynaraAdminPolicy(const std::string &client, const std::string &user,
55 const std::string &privilege, const std::string &goToBucket,
56 const std::string &bucket)
58 this->bucket = strdup(bucket.c_str());
59 this->client = strdup(client.c_str());
60 this->user = strdup(user.c_str());
61 this->privilege = strdup(privilege.c_str());
62 this->result_extra = strdup(goToBucket.c_str());
63 this->result = CYNARA_ADMIN_BUCKET;
65 if (this->bucket == nullptr || this->client == nullptr ||
66 this->user == nullptr || this->privilege == nullptr ||
67 this->result_extra == nullptr) {
71 free(this->privilege);
72 free(this->result_extra);
73 throw std::bad_alloc();
77 CynaraAdminPolicy::~CynaraAdminPolicy()
82 free(this->privilege);
83 free(this->result_extra);
86 static void checkCynaraAdminError(int result, const std::string &msg)
89 case CYNARA_ADMIN_API_SUCCESS:
91 case CYNARA_ADMIN_API_OUT_OF_MEMORY:
92 ThrowMsg(CynaraException::OutOfMemory, msg);
93 case CYNARA_ADMIN_API_INVALID_PARAM:
94 ThrowMsg(CynaraException::InvalidParam, msg);
95 case CYNARA_ADMIN_API_SERVICE_NOT_AVAILABLE:
96 ThrowMsg(CynaraException::ServiceNotAvailable, msg);
98 ThrowMsg(CynaraException::UnknownError, msg);
102 CynaraAdmin::CynaraAdmin()
104 checkCynaraAdminError(
105 cynara_admin_initialize(&m_CynaraAdmin),
106 "Cannot connect to Cynara administrative interface.");
109 CynaraAdmin::~CynaraAdmin()
111 cynara_admin_finish(m_CynaraAdmin);
114 void CynaraAdmin::SetPolicies(const std::vector<CynaraAdminPolicy> &policies)
116 std::vector<const struct cynara_admin_policy *> pp_policies(policies.size() + 1);
118 for (std::size_t i = 0; i < policies.size(); ++i)
119 pp_policies[i] = static_cast<const struct cynara_admin_policy *>(&policies[i]);
121 pp_policies[policies.size()] = nullptr;
123 checkCynaraAdminError(
124 cynara_admin_set_policies(m_CynaraAdmin, pp_policies.data()),
125 "Error while updating Cynara policy.");
128 } // namespace SecurityManager