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.
18 * @author Lukasz Wojciechowski <l.wojciechow@partner.samsung.com>
19 * @author Aleksander Zdyb <a.zdyb@partner.samsung.com>
21 * @brief This file implements policy rules storage procedures
25 #include "StorageBackend.h"
26 #include "types/pointers.h"
27 #include "types/PolicyType.h"
28 #include "exceptions/NotImplementedException.h"
29 #include "exceptions/DefaultBucketDeletionException.h"
37 PolicyResult Storage::checkPolicy(const PolicyKey &key) {
38 auto policies = m_backend.searchDefaultBucket(key);
39 return minimalPolicy(policies, key);
42 PolicyResult Storage::minimalPolicy(const PolicyBucket &bucket, const PolicyKey &key) {
43 bool hasMinimal = false;
44 PolicyResult minimal = bucket.defaultPolicy();
46 const auto &policies = bucket.policyCollection();
48 auto proposeMinimal = [&minimal, &hasMinimal](const PolicyResult &candidate) {
49 if (hasMinimal == false) {
51 } else if (candidate < minimal) {
57 for (const auto &policyRecord : policies) {
58 const auto &policyResult = policyRecord->result();
60 switch (policyResult.policyType()) {
61 case PredefinedPolicyType::DENY:
62 return policyResult; // Do not expect lower value than DENY
64 case PredefinedPolicyType::BUCKET: {
65 auto bucketResults = m_backend.searchBucket(policyResult.metadata(), key);
66 auto minimumOfBucket = minimalPolicy(bucketResults, key);
67 proposeMinimal(minimumOfBucket);
71 case PredefinedPolicyType::ALLOW:
76 proposeMinimal(policyResult);
82 void Storage::insertPolicies(const std::map<PolicyBucketId, std::vector<Policy>> &policies) {
83 for (const auto &bucket : policies) {
84 const PolicyBucketId &bucketId = bucket.first;
85 for (const auto &policy : bucket.second) {
86 PolicyPtr policyPtr = std::make_shared<Policy>(policy);
87 auto existingPolicies = m_backend.searchBucket(bucketId, policyPtr->key());
88 for (auto existingPolicy : existingPolicies.policyCollection()) {
89 m_backend.deletePolicy(bucketId, existingPolicy->key());
91 m_backend.insertPolicy(bucketId, policyPtr);
96 void Storage::addOrUpdateBucket(const PolicyBucketId &bucketId, const PolicyResult &defaultBucketPolicy) {
97 if (m_backend.hasBucket(bucketId)) {
98 m_backend.updateBucket(bucketId, defaultBucketPolicy);
100 m_backend.createBucket(bucketId, defaultBucketPolicy);
104 void Storage::deleteBucket(const PolicyBucketId &bucketId) {
105 // TODO: Check if bucket exists
107 if (bucketId == defaultPolicyBucketId) {
108 throw DefaultBucketDeletionException();
111 m_backend.deleteLinking(bucketId);
112 m_backend.deleteBucket(bucketId);
115 void Storage::deletePolicies(const std::map<PolicyBucketId, std::vector<PolicyKey>> &policies) {
116 for (const auto &bucket : policies) {
117 const PolicyBucketId &bucketId = bucket.first;
118 for (const auto &policyKey : bucket.second) {
119 m_backend.deletePolicy(bucketId, policyKey);
124 } // namespace Cynara