From 7fae914529b83ed9c2062b3de079e2141f752cef Mon Sep 17 00:00:00 2001 From: Aleksander Zdyb Date: Fri, 13 Jun 2014 08:27:58 +0200 Subject: [PATCH] Refactorize code to make it easier testable Change-Id: I60f18fa2d811cb2d94e0a220f4e4131af1491b0a --- .../bootstrap/cynara-client-bootstrap.cpp | 4 +- .../bootstrap/cynara-client-bootstrap.h | 4 +- src/client/client-api.cpp | 4 +- src/client/cynara-client-interface.h | 4 +- src/common/common.h | 3 +- .../exceptions/BucketAlreadyExistsException.h | 43 ++++++ .../exceptions/BucketNotExistsException.h | 45 ++++++ .../DefaultBucketDeletionException.h | 36 +++++ .../exceptions/NotImplementedException.h | 37 +++++ src/common/types/Policy.h | 41 +++++- src/common/types/PolicyBucket.cpp | 42 ++++++ src/common/types/PolicyBucket.h | 52 ++++++- src/common/types/PolicyBucketId.h | 33 +++++ ...xtendedPolicyType.h => PolicyCollection.h} | 27 ++-- src/common/types/PolicyKey.h | 25 +++- src/common/types/PolicyResult.h | 55 ++++++++ src/common/types/PolicyType.h | 11 +- src/common/types/pointers.h | 37 +++++ src/service/storage/Storage.cpp | 130 ++++++++++++------ src/service/storage/Storage.h | 51 ++++--- src/service/storage/StorageBackend.h | 54 ++++++++ 21 files changed, 645 insertions(+), 93 deletions(-) create mode 100644 src/common/exceptions/BucketAlreadyExistsException.h create mode 100644 src/common/exceptions/BucketNotExistsException.h create mode 100644 src/common/exceptions/DefaultBucketDeletionException.h create mode 100644 src/common/exceptions/NotImplementedException.h create mode 100644 src/common/types/PolicyBucket.cpp create mode 100644 src/common/types/PolicyBucketId.h rename src/common/types/{ExtendedPolicyType.h => PolicyCollection.h} (61%) create mode 100644 src/common/types/PolicyResult.h create mode 100644 src/common/types/pointers.h create mode 100644 src/service/storage/StorageBackend.h diff --git a/src/client/bootstrap/cynara-client-bootstrap.cpp b/src/client/bootstrap/cynara-client-bootstrap.cpp index f3d2befb..ecd72071 100644 --- a/src/client/bootstrap/cynara-client-bootstrap.cpp +++ b/src/client/bootstrap/cynara-client-bootstrap.cpp @@ -29,8 +29,8 @@ CynaraClientBootstrap :: CynaraClientBootstrap() { CynaraClientBootstrap :: ~CynaraClientBootstrap() { } -cynara_api_result CynaraClientBootstrap :: check(const std::string& client, - const std::string& session UNUSED, const std::string& user UNUSED, const std::string& privilege) +cynara_api_result CynaraClientBootstrap :: check(const std::string &client, + const std::string &session UNUSED, const std::string &user UNUSED, const std::string &privilege) { int is_enabled = 0; diff --git a/src/client/bootstrap/cynara-client-bootstrap.h b/src/client/bootstrap/cynara-client-bootstrap.h index c398346c..8c720fc4 100644 --- a/src/client/bootstrap/cynara-client-bootstrap.h +++ b/src/client/bootstrap/cynara-client-bootstrap.h @@ -32,8 +32,8 @@ class CynaraClientBootstrap : public CynaraClientInterface { public: CynaraClientBootstrap(); virtual ~CynaraClientBootstrap(); - virtual cynara_api_result check(const std::string& client, const std::string& session, - const std::string& user, const std::string& privilege); + virtual cynara_api_result check(const std::string &client, const std::string &session, + const std::string &user, const std::string &privilege); }; #endif /* CYNARA_CLIENT_BOOTSTRAP_H */ diff --git a/src/client/client-api.cpp b/src/client/client-api.cpp index 0c477f97..e0e53cf6 100644 --- a/src/client/client-api.cpp +++ b/src/client/client-api.cpp @@ -27,7 +27,7 @@ #include struct cynara { - CynaraClientInterface* impl; + CynaraClientInterface *impl; cynara(CynaraClientInterface *_impl) : impl(_impl) { } @@ -44,7 +44,7 @@ int cynara_initialize(cynara **pp_cynara, const cynara_configuration *p_conf UNU try { *pp_cynara = new cynara(new CynaraClientBootstrap); - } catch (std::bad_alloc& ex) { + } catch (std::bad_alloc &ex) { return cynara_api_result::CYNARA_API_OUT_OF_MEMORY; } diff --git a/src/client/cynara-client-interface.h b/src/client/cynara-client-interface.h index cebf1bc7..f93958cd 100644 --- a/src/client/cynara-client-interface.h +++ b/src/client/cynara-client-interface.h @@ -31,8 +31,8 @@ class CynaraClientInterface { public: virtual ~CynaraClientInterface() { } - virtual cynara_api_result check(const std::string& client, const std::string& session, - const std::string& user, const std::string& privilege) = 0; + virtual cynara_api_result check(const std::string &client, const std::string &session, + const std::string &user, const std::string &privilege) = 0; }; #endif /* CYNARA_CLIENT_INTERFACE_H */ diff --git a/src/common/common.h b/src/common/common.h index 2039658f..c8502cce 100644 --- a/src/common/common.h +++ b/src/common/common.h @@ -30,13 +30,12 @@ #include "result/Result.h" #include "types/ClientId.h" -#include "types/ExtendedPolicyType.h" #include "types/Policy.h" #include "types/PolicyBucket.h" #include "types/PolicyKey.h" #include "types/PolicyType.h" #include "types/PolicyTypeExtension.h" -#include "types/PolicyVector.h" +#include "types/PolicyCollection.h" #include "types/PrivilegeId.h" #include "types/UserId.h" diff --git a/src/common/exceptions/BucketAlreadyExistsException.h b/src/common/exceptions/BucketAlreadyExistsException.h new file mode 100644 index 00000000..b9fd5694 --- /dev/null +++ b/src/common/exceptions/BucketAlreadyExistsException.h @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/* + * @file BucketAlreadyExistsException.h + * @author Aleksander Zdyb + * @version 1.0 + * @brief Implementation of BucketAlreadyExistsException + */ + +#ifndef BUCKETALREADYEXISTS_H +#define BUCKETALREADYEXISTS_H + +#include "types/PolicyBucketId.h" + +#include + +namespace Cynara { + +class BucketAlreadyExistsException : public std::exception { +public: + BucketAlreadyExistsException(const PolicyBucketId &bucketId) : m_bucketId(bucketId) { + } + +private: + PolicyBucketId m_bucketId; +}; + +} /* namespace Cynara */ + +#endif // BUCKETALREADYEXISTS_H diff --git a/src/common/exceptions/BucketNotExistsException.h b/src/common/exceptions/BucketNotExistsException.h new file mode 100644 index 00000000..4f1fbe9a --- /dev/null +++ b/src/common/exceptions/BucketNotExistsException.h @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/* + * @file BucketNotExistsException.h + * @author Aleksander Zdyb + * @version 1.0 + * @brief Implementation of BucketNotExistsException + */ + +#ifndef BUCKETNOTEXISTSEXCEPTION_H_ +#define BUCKETNOTEXISTSEXCEPTION_H_ + +#include "types/PolicyBucketId.h" + +#include + +namespace Cynara { + +class BucketNotExistsException : public std::exception { +public: + BucketNotExistsException(const PolicyBucketId &bucketId) : m_bucketId(bucketId) { + + } + +private: + PolicyBucketId m_bucketId; +}; + +} /* namespace Cynara */ + + +#endif /* BUCKETNOTEXISTSEXCEPTION_H_ */ diff --git a/src/common/exceptions/DefaultBucketDeletionException.h b/src/common/exceptions/DefaultBucketDeletionException.h new file mode 100644 index 00000000..af054756 --- /dev/null +++ b/src/common/exceptions/DefaultBucketDeletionException.h @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/* + * @file DefaultBucketDeletionException.h + * @author Aleksander Zdyb + * @version 1.0 + * @brief Implementation of DefaultBucketDeletionException + */ + +#ifndef DEFAULTBUCKETDELETIONEXCEPTION_H +#define DEFAULTBUCKETDELETIONEXCEPTION_H + +#include + +namespace Cynara { + +class DefaultBucketDeletionException : public std::exception { + +}; + +} /* namespace Cynara */ + +#endif // DEFAULTBUCKETDELETIONEXCEPTION_H diff --git a/src/common/exceptions/NotImplementedException.h b/src/common/exceptions/NotImplementedException.h new file mode 100644 index 00000000..12995d50 --- /dev/null +++ b/src/common/exceptions/NotImplementedException.h @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/* + * @file NotImplementedException.h + * @author Aleksander Zdyb + * @version 1.0 + * @brief Implementation of NotImplementedException + */ + +#ifndef NOTIMPLEMENTEDEXCEPTION_H_ +#define NOTIMPLEMENTEDEXCEPTION_H_ + +#include + +namespace Cynara { + +class NotImplementedException : public std::exception { + +}; + + +} + +#endif /* NOTIMPLEMENTEDEXCEPTION_H_ */ diff --git a/src/common/types/Policy.h b/src/common/types/Policy.h index aaf947e5..47faad93 100644 --- a/src/common/types/Policy.h +++ b/src/common/types/Policy.h @@ -16,6 +16,7 @@ /* * @file Policy.h * @author Lukasz Wojciechowski + * @author Aleksander Zdyb * @version 1.0 * @brief This file defines Policy type - used to describe single policy rule identified by policy key @@ -25,12 +26,44 @@ #define CYNARA_COMMON_TYPES_POLICY_H #include "PolicyKey.h" -#include "ExtendedPolicyType.h" +#include "PolicyResult.h" +#include "types/pointers.h" +#include "types/PolicyType.h" +#include "types/PolicyBucketId.h" -struct Policy +#include + +namespace Cynara { + +class Policy { - PolicyKey m_key; - ExtendedPolicyType m_extType; +public: + Policy(const PolicyKey &key, const PolicyResult &result) : + m_key(key), m_result(result) {} + + static PolicyPtr simpleWithKey(const PolicyKey &key, const PolicyType &type) { + return std::make_shared(key, PolicyResult(type)); + } + + static PolicyPtr bucketWithKey(const PolicyKey &key, const PolicyBucketId &bucketId) { + auto result = PolicyResult(PolicyType::BUCKET, bucketId); + return std::make_shared(key, result); + } + +private: + PolicyKey m_key; + PolicyResult m_result; + +public: + const PolicyKey &key() const { + return m_key; + } + + const PolicyResult &result() const { + return m_result; + } }; +} // namespace Cynara + #endif /* CYNARA_COMMON_TYPES_POLICY_H */ diff --git a/src/common/types/PolicyBucket.cpp b/src/common/types/PolicyBucket.cpp new file mode 100644 index 00000000..4aea4912 --- /dev/null +++ b/src/common/types/PolicyBucket.cpp @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/* + * @file PolicyBucket.cpp + * @author Aleksander Zdyb + * @version 1.0 + * @brief Implementation of Cynara::PolicyBucket methods + */ + + +#include "PolicyBucket.h" + +namespace Cynara { + +PolicyBucket PolicyBucket::filtered(const PolicyKey &key) const { + PolicyBucket result; + + const auto &policies = policyCollection(); + std::copy_if(policies.begin(), policies.end(), std::back_inserter(result.policyCollection()), + [=] (PolicyCollection::value_type policy) { + return policy->key() == key; + }); + + // Inherit original policy + result.setDefaultPolicy(defaultPolicy()); + return result; +} + +} // namespace Cynara diff --git a/src/common/types/PolicyBucket.h b/src/common/types/PolicyBucket.h index 21ab9491..f0e4bf27 100644 --- a/src/common/types/PolicyBucket.h +++ b/src/common/types/PolicyBucket.h @@ -16,6 +16,7 @@ /* * @file PolicyBucket.h * @author Lukasz Wojciechowski + * @author Aleksander Zdyb * @version 1.0 * @brief This file defines PolicyBucket type - a policy aggregation entity name @@ -24,9 +25,56 @@ #ifndef CYNARA_COMMON_TYPES_POLICYBUCKET_H #define CYNARA_COMMON_TYPES_POLICYBUCKET_H +#include "PolicyCollection.h" +#include "PolicyKey.h" +#include "Policy.h" +#include "exceptions/NotImplementedException.h" +#include "types/pointers.h" +#include "types/PolicyType.h" +#include "PolicyBucketId.h" + #include -typedef std::string PolicyBucket; +#include +#include + +namespace Cynara { + +// TODO: Move this const somewhere else +const PolicyBucketId defaultPolicyBucketId(""); + +class PolicyBucket { +public: + + PolicyBucket() : m_defaultPolicy(PolicyResult(PolicyType::DENY)) {} + + PolicyBucket filtered(const PolicyKey &key) const; + +private: + PolicyCollection m_policyCollection; + PolicyResult m_defaultPolicy; + PolicyBucketId m_id; + +public: + const PolicyResult &defaultPolicy() const { + return m_defaultPolicy; + } + + const PolicyBucketId &id() const { + return m_id; + } + + PolicyCollection &policyCollection() { + return m_policyCollection; + } + + const PolicyCollection &policyCollection() const { + return m_policyCollection; + } -const PolicyBucket defaultPolicyBucket(""); + void setDefaultPolicy(const PolicyResult &defaultPolicy) { + m_defaultPolicy = defaultPolicy; + } +}; +} /* namespace Cynara */ #endif /* CYNARA_COMMON_TYPES_POLICYBUCKET_H */ diff --git a/src/common/types/PolicyBucketId.h b/src/common/types/PolicyBucketId.h new file mode 100644 index 00000000..e80c21e9 --- /dev/null +++ b/src/common/types/PolicyBucketId.h @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/* + * @file PolicBucketId.h + * @author Aleksander Zdyb + * @version 1.0 + * @brief Definition of Cynara::PolicyBucketId type + */ +#ifndef POLICBUCKETID_H_ +#define POLICBUCKETID_H_ + + +namespace Cynara { + +typedef std::string PolicyBucketId; + +} // namespace Cynara + + +#endif /* POLICBUCKETID_H_ */ diff --git a/src/common/types/ExtendedPolicyType.h b/src/common/types/PolicyCollection.h similarity index 61% rename from src/common/types/ExtendedPolicyType.h rename to src/common/types/PolicyCollection.h index 9f4b87ee..b4ec92ac 100644 --- a/src/common/types/ExtendedPolicyType.h +++ b/src/common/types/PolicyCollection.h @@ -14,23 +14,24 @@ * limitations under the License. */ /* - * @file ExtendedPolicyType.h + * @file PolicyCollection.h * @author Lukasz Wojciechowski + * @author Aleksander Zdyb * @version 1.0 - * @brief This file defines ExtendedPolicyType - used to describe policy - type with extended data (that may be used by plugins) + * @brief This file defines a collection of policies */ -#ifndef CYNARA_COMMON_TYPES_EXTENDEDPOLICYTYPE_H -#define CYNARA_COMMON_TYPES_EXTENDEDPOLICYTYPE_H +#ifndef CYNARA_COMMON_TYPES_POLICYCOLLECTION_H +#define CYNARA_COMMON_TYPES_POLICYCOLLECTION_H -#include "PolicyType.h" -#include "PolicyTypeExtension.h" +#include "types/pointers.h" -struct ExtendedPolicyType -{ - PolicyType m_type; - PolicyTypeExtension m_ext; -}; +#include -#endif /* CYNARA_COMMON_TYPES_EXTENDEDPOLICYTYPE_H */ +namespace Cynara { + +typedef std::vector PolicyCollection; + +} // namespace Cynara + +#endif /* CYNARA_COMMON_TYPES_POLICYCOLLECTION_H */ diff --git a/src/common/types/PolicyKey.h b/src/common/types/PolicyKey.h index 90f1ade7..8435747a 100644 --- a/src/common/types/PolicyKey.h +++ b/src/common/types/PolicyKey.h @@ -16,8 +16,9 @@ /* * @file PolicyKey.h * @author Lukasz Wojciechowski + * @author Aleksander Zdyb * @version 1.0 - * @brief This file defines PolicyKey - tripple that defines a single + * @brief This file defines PolicyKey - triple, which defines a single policy rule */ @@ -28,11 +29,27 @@ #include "UserId.h" #include "PrivilegeId.h" +#include + +namespace Cynara { + struct PolicyKey { - ClientId m_client; - UserId m_user; - PrivilegeId m_privilege; +public: + PolicyKey() {}; + PolicyKey(const ClientId &clientId, const UserId &userId, const PrivilegeId &privilegeId) + : m_client(clientId), m_user(userId), m_privilege(privilegeId) {}; + + bool operator==(const PolicyKey &other) const { + return std::tie(m_client, m_user, m_privilege) + == std::tie(other.m_client, other.m_user, other.m_privilege); + } + +private: + ClientId m_client; + UserId m_user; + PrivilegeId m_privilege; }; +} #endif /* CYNARA_COMMON_TYPES_POLICYKEY_H */ diff --git a/src/common/types/PolicyResult.h b/src/common/types/PolicyResult.h new file mode 100644 index 00000000..1067c020 --- /dev/null +++ b/src/common/types/PolicyResult.h @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/* + * @file PolicyResult.h + * @author Aleksander Zdyb + * @version 1.0 + * @brief Definitions of PolicyResult and friends + */ + +#ifndef POLICYRESULT_H_ +#define POLICYRESULT_H_ + +#include "types/PolicyType.h" + +namespace Cynara { + +class PolicyResult { +public: + typedef std::string PolicyMetadata; + +public: + PolicyResult(const PolicyType &policyType) : m_type(policyType) {} + PolicyResult(const PolicyType &policyType, const PolicyMetadata &metadata) + : m_type(policyType), m_metaData(metadata) {} + +private: + PolicyType m_type; + PolicyMetadata m_metaData; + +public: + PolicyType policyType() const { return m_type; } + const PolicyMetadata& metaData() const { return m_metaData; } + + bool operator <(const PolicyResult& other) const { + return this->m_type < other.m_type; + } +}; + +} // namespace Cynara + + +#endif /* POLICYRESULT_H_ */ diff --git a/src/common/types/PolicyType.h b/src/common/types/PolicyType.h index 15d820b5..5336746e 100644 --- a/src/common/types/PolicyType.h +++ b/src/common/types/PolicyType.h @@ -16,6 +16,7 @@ /* * @file PolicyType.h * @author Lukasz Wojciechowski + * @author Aleksander Zdyb * @version 1.0 * @brief This file defines PolicyType e.g. ALLOW or DENY */ @@ -23,10 +24,14 @@ #ifndef CYNARA_COMMON_TYPES_POLICYTYPE_H #define CYNARA_COMMON_TYPES_POLICYTYPE_H -typedef int PolicyType; +namespace Cynara { -const int PolicyType_DENY = 0; +enum class PolicyType : std::uint16_t { + DENY = 0, + BUCKET = 0xFFFE, + ALLOW = 0xFFFF +}; -const int PolicyType_ALLOW = 0xFFFF; +} // namespace Cynara #endif /* CYNARA_COMMON_TYPES_POLICYTYPE_H */ diff --git a/src/common/types/pointers.h b/src/common/types/pointers.h new file mode 100644 index 00000000..4ed373c7 --- /dev/null +++ b/src/common/types/pointers.h @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/* + * @file pointers.h + * @author Aleksander Zdyb + * @version 1.0 + * @brief Typedefs for smart pointers of common types + */ + +#ifndef POINTERS_H_ +#define POINTERS_H_ + +#include + +namespace Cynara { + +class Policy; + +typedef std::shared_ptr PolicyPtr; + + +} /* namespace Cynara */ + +#endif /* POINTERS_H_ */ diff --git a/src/service/storage/Storage.cpp b/src/service/storage/Storage.cpp index 077b89a8..7ed01446 100644 --- a/src/service/storage/Storage.cpp +++ b/src/service/storage/Storage.cpp @@ -16,59 +16,109 @@ /* * @file Storage.cpp * @author Lukasz Wojciechowski + * @author Aleksander Zdyb * @version 1.0 * @brief This file implements policy rules storage procedures */ #include "Storage.h" +#include "StorageBackend.h" +#include "types/pointers.h" +#include "types/PolicyType.h" +#include "exceptions/NotImplementedException.h" +#include "exceptions/BucketAlreadyExistsException.h" +#include "exceptions/DefaultBucketDeletionException.h" -Result Storage::checkPolicy(const PolicyKey& key, ExtendedPolicyType& result) -{ - //todo - TODO_USE_ME(key); - TODO_USE_ME(result); - return RESULT_OK; -} +#include +#include + + +namespace Cynara { + +PolicyResult Storage::checkPolicy(const PolicyKey &key) { + auto policies = m_backend.searchDefaultBucket(key); + return minimalPolicy(policies); +}; + +PolicyResult Storage::minimalPolicy(const PolicyBucket &bucket) { + bool hasMinimal = false; + PolicyResult minimal = bucket.defaultPolicy(); + + const auto &policies = bucket.policyCollection(); + + auto proposeMinimal = [&minimal, &hasMinimal](const PolicyResult &candidate) { + if(hasMinimal == false) { + minimal = candidate; + } else if(candidate < minimal) { + minimal = candidate; + } + hasMinimal = true; + }; + + for(const auto &policyRecord : policies) { + const auto &policyResult = policyRecord->result(); -Result Storage::insertOrUpdatePolicy(const PolicyVector& policyVector, const PolicyBucket& bucket) -{ - //todo - TODO_USE_ME(policyVector); - TODO_USE_ME(bucket); - return RESULT_OK; + switch(policyResult.policyType()) { + case PolicyType::DENY: + return policyResult; // Do not expect lower value than DENY + break; + case PolicyType::BUCKET: { + auto bucketResults = m_backend.searchBucket(policyResult.metaData(), + policyRecord->key()); + auto minimumOfBucket = minimalPolicy(bucketResults); + proposeMinimal(minimumOfBucket); + continue; + } + break; + case PolicyType::ALLOW: + default: + break; + } + + proposeMinimal(policyResult); + } + + return minimal; } -Result Storage::insertOrUpdateBucket(const PolicyBucket& newBucket, const PolicyKey& key, const ExtendedPolicyType& defaultBucketPolicy, const PolicyBucket& bucket) -{ - //todo - TODO_USE_ME(newBucket); - TODO_USE_ME(key); - TODO_USE_ME(defaultBucketPolicy); - TODO_USE_ME(bucket); - return RESULT_OK; +void Storage::insertPolicies(const std::vector &policies) { + for(const auto &policyTuple : policies) { + PolicyBucketId bucketId; + PolicyPtr policyPtr; + std::tie(policyPtr, bucketId) = policyTuple; + auto existingPolicies = m_backend.searchBucket(bucketId, policyPtr->key()); + for(auto existingPolicy : existingPolicies.policyCollection()) { + m_backend.deletePolicy(bucketId, existingPolicy->key()); + } + m_backend.insertPolicy(bucketId, policyPtr); + } } -Result Storage::deletePolicy(const PolicyKey& policyKey, const PolicyBucket& bucket) -{ - //todo - TODO_USE_ME(policyKey); - TODO_USE_ME(bucket); - return RESULT_OK; +void Storage::createBucket(const PolicyBucketId &newBucketId, const PolicyResult &defaultBucketPolicy) { + // TODO: Check if bucket already exists + + if (newBucketId == defaultPolicyBucketId) { + throw BucketAlreadyExistsException(newBucketId); + } + + m_backend.createBucket(newBucketId, defaultBucketPolicy); } -Result Storage::deleteBucket(const PolicyBucket& bucket) -{ - //todo - TODO_USE_ME(bucket); - return RESULT_OK; +void Storage::deleteBucket(const PolicyBucketId &bucketId) { + // TODO: Check if bucket exists + + if (bucketId == defaultPolicyBucketId) { + throw DefaultBucketDeletionException(); + } + + m_backend.deleteLinking(bucketId); + m_backend.deleteBucket(bucketId); } -/* -Result Storage::listPolicy(const PolicyKey& policyKey, PolicyVector& result) -{ - //todo - TODO_USE_ME(policyKey); - TODO_USE_ME(result);; - return RESULT_OK; +void Storage::deletePolicies(const std::vector &policies) { + for(const auto &policy : policies) { + m_backend.deletePolicy(std::get<1>(policy), std::get<0>(policy)); + } } -*/ + +} // namespace Cynara diff --git a/src/service/storage/Storage.h b/src/service/storage/Storage.h index 800fcaed..d8110eec 100644 --- a/src/service/storage/Storage.h +++ b/src/service/storage/Storage.h @@ -16,6 +16,7 @@ /* * @file Storage.h * @author Lukasz Wojciechowski + * @author Aleksander Zdyb * @version 1.0 * @brief This file is the implementation file of log system */ @@ -23,27 +24,43 @@ #ifndef CYNARA_SERVICE_STORAGE_STORAGE_H #define CYNARA_SERVICE_STORAGE_STORAGE_H -#include +#include "types/pointers.h" +#include "types/PolicyBucketId.h" +#include "types/PolicyResult.h" +#include "types/PolicyKey.h" + +#include +#include +#include + +namespace Cynara { + +class StorageBackend; +class PolicyBucket; class Storage { public: -//CHECKING policies -//checks policies matching key and returns minimal policy type as result - Result checkPolicy(const PolicyKey& key, ExtendedPolicyType& result); - -//ADMINISTRATION of polices -//insert (or update) new policies defined in policyVector into bucket - Result insertOrUpdatePolicy(const PolicyVector& policyVector, const PolicyBucket& bucket = defaultPolicyBucket); -//insert (or update) new bucket (newBucket) in bucket for key with defaultPolicyKey - Result insertOrUpdateBucket(const PolicyBucket& newBucket, const PolicyKey& key, const ExtendedPolicyType& defaultBucketPolicy, const PolicyBucket& bucket = defaultPolicyBucket); -//remove single policy rule - Result deletePolicy(const PolicyKey& policyKey, const PolicyBucket& bucket = defaultPolicyBucket); -//remove bucket and all policy rules related (inside or pointing to) bucket - Result deleteBucket(const PolicyBucket& bucket); - -//listing will be defined later -// Result listPolicy(const PolicyKey& policyKey, PolicyVector& result); + // TODO: These tuples are ugly -- refactorize + typedef std::tuple PolicyPolicyBucket; + typedef std::tuple PolicyKeyBucket; + + Storage(StorageBackend &backend) : m_backend(backend) {} + + PolicyResult checkPolicy(const PolicyKey &key); + + void insertPolicies(const std::vector &policies); + void createBucket(const PolicyBucketId &newBucketId, const PolicyResult &defaultBucketPolicy); + void deletePolicies(const std::vector &policies); + void deleteBucket(const PolicyBucketId &bucketId); + +protected: + PolicyResult minimalPolicy(const PolicyBucket &bucket); + +private: + StorageBackend &m_backend; // backend strategy }; +} // namespace Cynara + #endif /* CYNARA_SERVICE_STORAGE_STORAGE_H */ diff --git a/src/service/storage/StorageBackend.h b/src/service/storage/StorageBackend.h new file mode 100644 index 00000000..06a3a74b --- /dev/null +++ b/src/service/storage/StorageBackend.h @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/* + * @file StorageBackend.h + * @author Aleksander Zdyb + * @version 1.0 + * @brief Headers for StorageBackend base class + */ + +#ifndef STORAGEBACKEND_H_ +#define STORAGEBACKEND_H_ + + +#include "types/pointers.h" +#include "types/PolicyBucket.h" +#include "types/PolicyBucketId.h" + +#include + +namespace Cynara { + +class PolicyKey; + +class StorageBackend { +public: + virtual ~StorageBackend() {} + + virtual PolicyBucket searchDefaultBucket(const PolicyKey &key) = 0; + virtual PolicyBucket searchBucket(const PolicyBucketId &bucket, const PolicyKey &key) = 0; + + virtual void insertPolicy(const PolicyBucketId &bucket, PolicyPtr policy) = 0; + + virtual void createBucket(const PolicyBucketId &bucketId, const PolicyResult &defaultPolicy) = 0; + virtual void deleteBucket(const PolicyBucketId &bucketId) = 0; + + virtual void deletePolicy(const PolicyBucketId &bucketId, const PolicyKey &key) = 0; + virtual void deleteLinking(const PolicyBucketId &bucket) = 0; +}; + +} /* namespace Cynara */ +#endif /* STORAGEBACKEND_H_ */ -- 2.34.1