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.wojciechowski@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/BucketAlreadyExistsException.h"
30 #include "exceptions/DefaultBucketDeletionException.h"
38 PolicyResult Storage::checkPolicy(const PolicyKey &key) {
39 auto policies = m_backend.searchDefaultBucket(key);
40 return minimalPolicy(policies);
43 PolicyResult Storage::minimalPolicy(const PolicyBucket &bucket) {
44 bool hasMinimal = false;
45 PolicyResult minimal = bucket.defaultPolicy();
47 const auto &policies = bucket.policyCollection();
49 auto proposeMinimal = [&minimal, &hasMinimal](const PolicyResult &candidate) {
50 if(hasMinimal == false) {
52 } else if(candidate < minimal) {
58 for(const auto &policyRecord : policies) {
59 const auto &policyResult = policyRecord->result();
61 switch(policyResult.policyType()) {
62 case PolicyType::DENY:
63 return policyResult; // Do not expect lower value than DENY
65 case PolicyType::BUCKET: {
66 auto bucketResults = m_backend.searchBucket(policyResult.metadata(),
68 auto minimumOfBucket = minimalPolicy(bucketResults);
69 proposeMinimal(minimumOfBucket);
73 case PolicyType::ALLOW:
78 proposeMinimal(policyResult);
84 void Storage::insertPolicies(const std::vector<PolicyPolicyBucket> &policies) {
85 for(const auto &policyTuple : policies) {
86 PolicyBucketId bucketId;
88 std::tie(policyPtr, bucketId) = policyTuple;
89 auto existingPolicies = m_backend.searchBucket(bucketId, policyPtr->key());
90 for(auto existingPolicy : existingPolicies.policyCollection()) {
91 m_backend.deletePolicy(bucketId, existingPolicy->key());
93 m_backend.insertPolicy(bucketId, policyPtr);
97 void Storage::createBucket(const PolicyBucketId &newBucketId, const PolicyResult &defaultBucketPolicy) {
98 // TODO: Check if bucket already exists
100 if (newBucketId == defaultPolicyBucketId) {
101 throw BucketAlreadyExistsException(newBucketId);
104 m_backend.createBucket(newBucketId, defaultBucketPolicy);
107 void Storage::deleteBucket(const PolicyBucketId &bucketId) {
108 // TODO: Check if bucket exists
110 if (bucketId == defaultPolicyBucketId) {
111 throw DefaultBucketDeletionException();
114 m_backend.deleteLinking(bucketId);
115 m_backend.deleteBucket(bucketId);
118 void Storage::deletePolicies(const std::vector<PolicyKeyBucket> &policies) {
119 for(const auto &policy : policies) {
120 m_backend.deletePolicy(std::get<1>(policy), std::get<0>(policy));
124 } // namespace Cynara