2 * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 * @file src/service/logic/Logic.cpp
18 * @author Lukasz Wojciechowski <l.wojciechow@partner.samsung.com>
20 * @brief This file implements main class of logic layer in cynara service
25 #include <exceptions/BucketNotExistsException.h>
26 #include <exceptions/DatabaseException.h>
27 #include <exceptions/DefaultBucketDeletionException.h>
28 #include <exceptions/DefaultBucketSetNoneException.h>
29 #include <exceptions/InvalidBucketIdException.h>
30 #include <exceptions/PluginErrorException.h>
31 #include <exceptions/PluginNotFoundException.h>
35 #include <main/Cynara.h>
36 #include <request/AdminCheckRequest.h>
37 #include <request/CancelRequest.h>
38 #include <request/CheckRequest.h>
39 #include <request/InsertOrUpdateBucketRequest.h>
40 #include <request/RemoveBucketRequest.h>
41 #include <request/RequestContext.h>
42 #include <request/SetPoliciesRequest.h>
43 #include <request/SignalRequest.h>
44 #include <response/CancelResponse.h>
45 #include <response/CheckResponse.h>
46 #include <response/CodeResponse.h>
47 #include <storage/Storage.h>
49 #include <sockets/SocketManager.h>
60 void Logic::execute(RequestContextPtr context UNUSED, SignalRequestPtr request) {
61 LOGD("Processing signal: [%d]", request->signalNumber());
63 switch (request->signalNumber()) {
65 LOGI("SIGTERM received!");
66 m_socketManager->mainLoopStop();
71 void Logic::execute(RequestContextPtr context, AdminCheckRequestPtr request) {
72 PolicyResult result = m_storage->checkPolicy(request->key(), request->startBucket(),
73 request->recursive());
75 context->returnResponse(context, std::make_shared<CheckResponse>(result,
76 request->sequenceNumber()));
79 void Logic::execute(RequestContextPtr context, CancelRequestPtr request) {
81 context->returnResponse(context, std::make_shared<CancelResponse>(request->sequenceNumber()));
84 void Logic::execute(RequestContextPtr context, CheckRequestPtr request) {
85 PolicyResult result(PredefinedPolicyType::DENY);
86 if (check(context, request->key(), result)) {
87 context->returnResponse(context, std::make_shared<CheckResponse>(result,
88 request->sequenceNumber()));
92 bool Logic::check(RequestContextPtr context UNUSED, const PolicyKey &key,
93 PolicyResult& result) {
94 result = m_storage->checkPolicy(key);
96 switch (result.policyType()) {
97 case PredefinedPolicyType::ALLOW :
98 LOGD("check of policy key <%s> returned ALLOW", key.toString().c_str());
100 case PredefinedPolicyType::DENY :
101 LOGD("check of policy key <%s> returned DENY", key.toString().c_str());
105 ExternalPluginPtr plugin = m_pluginManager->getPlugin(result.policyType());
107 throw PluginNotFoundException(result);
110 AgentType requiredAgent;
111 PluginData pluginData;
113 auto ret = plugin->check(key.client().toString(), key.user().toString(),
114 key.privilege().toString(), result, requiredAgent, pluginData);
117 case ExternalPluginInterface::PluginStatus::ANSWER_READY:
119 case ExternalPluginInterface::PluginStatus::ANSWER_NOTREADY:
120 //todo send request to agent
121 //context should be saved in plugin in order to return answer when ready
124 throw PluginErrorException(key);
128 void Logic::execute(RequestContextPtr context, InsertOrUpdateBucketRequestPtr request) {
129 auto code = CodeResponse::Code::OK;
132 m_storage->addOrUpdateBucket(request->bucketId(), request->result());
134 } catch (const DatabaseException &ex) {
135 code = CodeResponse::Code::FAILED;
136 } catch (const DefaultBucketSetNoneException &ex) {
137 code = CodeResponse::Code::NOT_ALLOWED;
138 } catch (const InvalidBucketIdException &ex) {
139 code = CodeResponse::Code::NOT_ALLOWED;
142 context->returnResponse(context, std::make_shared<CodeResponse>(code,
143 request->sequenceNumber()));
146 void Logic::execute(RequestContextPtr context, RemoveBucketRequestPtr request) {
147 auto code = CodeResponse::Code::OK;
149 m_storage->deleteBucket(request->bucketId());
151 } catch (const DatabaseException &ex) {
152 code = CodeResponse::Code::FAILED;
153 } catch (const BucketNotExistsException &ex) {
154 code = CodeResponse::Code::NO_BUCKET;
155 } catch (const DefaultBucketDeletionException &ex) {
156 code = CodeResponse::Code::NOT_ALLOWED;
158 context->returnResponse(context, std::make_shared<CodeResponse>(code,
159 request->sequenceNumber()));
162 void Logic::execute(RequestContextPtr context, SetPoliciesRequestPtr request) {
163 auto code = CodeResponse::Code::OK;
165 m_storage->insertPolicies(request->policiesToBeInsertedOrUpdated());
166 m_storage->deletePolicies(request->policiesToBeRemoved());
168 } catch (const DatabaseException &ex) {
169 code = CodeResponse::Code::FAILED;
170 } catch (const BucketNotExistsException &ex) {
171 code = CodeResponse::Code::NO_BUCKET;
173 context->returnResponse(context, std::make_shared<CodeResponse>(code,
174 request->sequenceNumber()));
177 void Logic::contextClosed(RequestContextPtr context UNUSED) {
178 //We don't care now, but we will
181 void Logic::onPoliciesChanged(void) {
183 m_socketManager->disconnectAllClients();
184 //todo remove all saved contexts (if there will be any saved contexts)
187 } // namespace Cynara