Update tests and remove unused Storage methods
[platform/core/security/cynara.git] / src / service / storage / Storage.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 /*
17  * @file        Storage.cpp
18  * @author      Lukasz Wojciechowski <l.wojciechow@partner.samsung.com>
19  * @author      Aleksander Zdyb <a.zdyb@partner.samsung.com>
20  * @version     1.0
21  * @brief       This file implements policy rules storage procedures
22  */
23
24 #include "Storage.h"
25 #include "StorageBackend.h"
26 #include "types/pointers.h"
27 #include "types/PolicyType.h"
28 #include "exceptions/NotImplementedException.h"
29 #include "exceptions/DefaultBucketDeletionException.h"
30
31 #include <iostream>
32 #include <memory>
33
34
35 namespace Cynara {
36
37 PolicyResult Storage::checkPolicy(const PolicyKey &key) {
38     auto policies = m_backend.searchDefaultBucket(key);
39     return minimalPolicy(policies, key);
40 };
41
42 PolicyResult Storage::minimalPolicy(const PolicyBucket &bucket, const PolicyKey &key) {
43     bool hasMinimal = false;
44     PolicyResult minimal = bucket.defaultPolicy();
45
46     const auto &policies = bucket.policyCollection();
47
48     auto proposeMinimal = [&minimal, &hasMinimal](const PolicyResult &candidate) {
49         if (hasMinimal == false) {
50             minimal = candidate;
51         } else if (candidate < minimal) {
52             minimal = candidate;
53         }
54         hasMinimal = true;
55     };
56
57     for (const auto &policyRecord : policies) {
58         const auto &policyResult = policyRecord->result();
59
60         switch (policyResult.policyType()) {
61         case PredefinedPolicyType::DENY:
62             return policyResult; // Do not expect lower value than DENY
63             break;
64         case PredefinedPolicyType::BUCKET: {
65                 auto bucketResults = m_backend.searchBucket(policyResult.metadata(), key);
66                 auto minimumOfBucket = minimalPolicy(bucketResults, key);
67                 proposeMinimal(minimumOfBucket);
68                 continue;
69             }
70             break;
71         case PredefinedPolicyType::ALLOW:
72         default:
73             break;
74         }
75
76         proposeMinimal(policyResult);
77     }
78
79     return minimal;
80 }
81
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());
90                 }
91                 m_backend.insertPolicy(bucketId, policyPtr);
92         }
93     }
94 }
95
96 void Storage::addOrUpdateBucket(const PolicyBucketId &bucketId, const PolicyResult &defaultBucketPolicy) {
97     if (m_backend.hasBucket(bucketId)) {
98         m_backend.updateBucket(bucketId, defaultBucketPolicy);
99     } else {
100         m_backend.createBucket(bucketId, defaultBucketPolicy);
101     }
102 }
103
104 void Storage::deleteBucket(const PolicyBucketId &bucketId) {
105     // TODO: Check if bucket exists
106
107     if (bucketId == defaultPolicyBucketId) {
108         throw DefaultBucketDeletionException();
109     }
110
111     m_backend.deleteLinking(bucketId);
112     m_backend.deleteBucket(bucketId);
113 }
114
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);
120         }
121     }
122 }
123
124 } // namespace Cynara