From 79fac257d851578b1b61274cdc1fae339b527387 Mon Sep 17 00:00:00 2001 From: Rafal Krypa Date: Thu, 3 Jul 2014 14:07:35 +0200 Subject: [PATCH 01/16] Add CYNARA_API_SERVICE_NOT_AVAILABLE This is new external API result code, which is returned, when Cynara service is not available (a connection to service cannot be set via UNIX socket) Change-Id: I53c006f1795c975b1e36a1c20753f090beb603f0 --- src/include/cynara-client.h | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/include/cynara-client.h b/src/include/cynara-client.h index d07c72b..3ceeaa6 100644 --- a/src/include/cynara-client.h +++ b/src/include/cynara-client.h @@ -43,7 +43,10 @@ enum cynara_api_result CYNARA_API_OUT_OF_MEMORY, /*! \brief indicating the API's parameter is malformed */ - CYNARA_API_INVALID_PARAM + CYNARA_API_INVALID_PARAM, + +/*! \brief service not available */ + CYNARA_API_SERVICE_NOT_AVAILABLE }; /** @}*/ -- 2.7.4 From 4bea1064888fa7dfeba410e1ea871626226bca8f Mon Sep 17 00:00:00 2001 From: Lukasz Wojciechowski Date: Mon, 30 Jun 2014 19:59:07 +0200 Subject: [PATCH 02/16] Implement Logic::check() method using SocketClient Implementation always checks privilege by sending request to Cynara service. Features not implemented: * No cache is used; * Session parameter is unused; * No plugin support is implemented. Change-Id: Ifdfdd56cceb967a8490f383ae396c5066e8a97fd --- src/client/logic/Logic.cpp | 63 ++++++++++++++++++++++-- src/client/logic/Logic.h | 11 ++++- src/common/exceptions/UnexpectedErrorException.h | 6 +++ 3 files changed, 74 insertions(+), 6 deletions(-) diff --git a/src/client/logic/Logic.cpp b/src/client/logic/Logic.cpp index cebfb28..4a46c33 100644 --- a/src/client/logic/Logic.cpp +++ b/src/client/logic/Logic.cpp @@ -20,17 +20,72 @@ * @brief This file contains implementation of Logic class - main libcynara-client class */ +#include + #include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include #include "Logic.h" namespace Cynara { -cynara_api_result Logic::check(const std::string &client UNUSED, const std::string &session UNUSED, - const std::string &user UNUSED, const std::string &privilege UNUSED) +Logic::Logic() { + m_socketClient = std::make_shared(); +} + +cynara_api_result Logic::check(const std::string &client, const std::string &session UNUSED, + const std::string &user, const std::string &privilege) noexcept { - //todo - this is a stub - return cynara_api_result::CYNARA_API_ACCESS_DENIED; + //todo Handle session parameter. + //todo Check if answer can be get from cache. Update cache. + + //Ask cynara service + PolicyResult result(PredefinedPolicyType::DENY); + try { + RequestPtr request = std::make_shared(PolicyKey(client, user, privilege)); + ResponsePtr response = m_socketClient->askCynaraServer(request); + if (!response) { + LOGW("Disconnected by cynara server."); + onDisconnected(); + return cynara_api_result::CYNARA_API_SERVICE_NOT_AVAILABLE; + } + CheckResponsePtr checkResponse = std::dynamic_pointer_cast(response); + if (!checkResponse) { + LOGC("Critical error. Casting Response to CheckResponse failed."); + throw UnexpectedErrorException("Error casting Response to CheckResponse"); + } + result = checkResponse->m_resultRef; + } catch (const ServerConnectionErrorException &ex) { + LOGE("Cynara service not available."); + onDisconnected(); + return cynara_api_result::CYNARA_API_SERVICE_NOT_AVAILABLE; + } catch (const std::exception &ex) { + LOGE("Error during check of privilege: %s", ex.what()); + return cynara_api_result::CYNARA_API_ACCESS_DENIED; + } + + //todo Interprete result. + //todo Update cache. + + //todo return result after more detailed interpretation. + if (result.policyType() == PredefinedPolicyType::ALLOW) + return cynara_api_result::CYNARA_API_SUCCESS; + else + return cynara_api_result::CYNARA_API_ACCESS_DENIED; +} + +void Logic::onDisconnected(void) { + //todo run special actions when disconnected from cynara service + // like cleaning cache } } // namespace Cynara diff --git a/src/client/logic/Logic.h b/src/client/logic/Logic.h index 445d940..512b313 100644 --- a/src/client/logic/Logic.h +++ b/src/client/logic/Logic.h @@ -24,17 +24,24 @@ #define SRC_CLIENT_LOGIC_LOGIC_H_ #include + #include +#include namespace Cynara { class Logic : public ApiInterface { +private: + SocketClientPtr m_socketClient; + + void onDisconnected(void); + public: - Logic() = default; + Logic(); virtual ~Logic() = default; virtual cynara_api_result check(const std::string &client, const std::string &session, - const std::string &user, const std::string &privilege); + const std::string &user, const std::string &privilege) noexcept; }; } // namespace Cynara diff --git a/src/common/exceptions/UnexpectedErrorException.h b/src/common/exceptions/UnexpectedErrorException.h index 9d858f4..0b85ad4 100644 --- a/src/common/exceptions/UnexpectedErrorException.h +++ b/src/common/exceptions/UnexpectedErrorException.h @@ -42,6 +42,12 @@ public: stream << errorMsg << ">"; m_whatMessage = stream.str(); } + UnexpectedErrorException(const char *errorMsg) { + std::ostringstream stream; + stream << "UnexpectedErrorException with message <"; + stream << errorMsg << ">"; + m_whatMessage = stream.str(); + } virtual ~UnexpectedErrorException() = default; -- 2.7.4 From f13fd58385950141fa8ba39f6f715959be67b823 Mon Sep 17 00:00:00 2001 From: Lukasz Wojciechowski Date: Mon, 30 Jun 2014 20:12:53 +0200 Subject: [PATCH 03/16] Remove "m_fd" - unused field of RequestContext Change-Id: I869ff96dea13b9eeac216d9af2159ebb3aaecedf --- src/client/sockets/SocketClient.cpp | 3 +-- src/common/request/RequestContext.h | 5 ++--- src/service/sockets/SocketManager.cpp | 2 +- 3 files changed, 4 insertions(+), 6 deletions(-) diff --git a/src/client/sockets/SocketClient.cpp b/src/client/sockets/SocketClient.cpp index 7d47d48..9bab9b1 100644 --- a/src/client/sockets/SocketClient.cpp +++ b/src/client/sockets/SocketClient.cpp @@ -45,8 +45,7 @@ SocketClient::SocketClient() : m_socket(clientSocketPath), ResponsePtr SocketClient::askCynaraServer(RequestPtr request) { //pass request to protocol - RequestContextPtr context = std::make_shared(-1, ResponseTakerPtr(), - m_writeQueue); + RequestContextPtr context = std::make_shared(ResponseTakerPtr(), m_writeQueue); request->execute(request, m_protocol, context); //send request to cynara diff --git a/src/common/request/RequestContext.h b/src/common/request/RequestContext.h index dd003c8..b5e518d 100644 --- a/src/common/request/RequestContext.h +++ b/src/common/request/RequestContext.h @@ -34,13 +34,12 @@ namespace Cynara { class RequestContext { private: - int m_desc; ResponseTakerWeakPtr m_responseTaker; BinaryQueue &m_responseQueue; public: - RequestContext(int desc, ResponseTakerPtr responseTaker, BinaryQueue &responseQueue) - : m_desc(desc), m_responseTaker(responseTaker), m_responseQueue(responseQueue) { + RequestContext(ResponseTakerPtr responseTaker, BinaryQueue &responseQueue) + : m_responseTaker(responseTaker), m_responseQueue(responseQueue) { } void returnResponse(RequestContextPtr self, ResponsePtr response) const { diff --git a/src/service/sockets/SocketManager.cpp b/src/service/sockets/SocketManager.cpp index e1633d9..7c7adeb 100644 --- a/src/service/sockets/SocketManager.cpp +++ b/src/service/sockets/SocketManager.cpp @@ -227,7 +227,7 @@ bool SocketManager::handleRead(int fd, const RawBuffer &readbuffer) { LOGD("request extracted"); //build context - auto context = std::make_shared(fd, desc.responseTaker(), + auto context = std::make_shared(desc.responseTaker(), desc.writeQueue()); //pass request to request taker req->execute(req, requestTaker(), context); -- 2.7.4 From 745319609cbdcfbde8e0ee1c7c4f5b0be988ced8 Mon Sep 17 00:00:00 2001 From: Aleksander Zdyb Date: Wed, 2 Jul 2014 12:52:09 +0200 Subject: [PATCH 04/16] Fix wildcard checks Fixed bug, where consecutive buckets were filtered with wildcard keys instead of original key being checked Change-Id: I97499f41c796a0262e9f8d2a57908cc8a4fc3014 --- src/service/storage/Storage.cpp | 9 +++--- src/service/storage/Storage.h | 2 +- test/storage/storage/check.cpp | 61 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+), 6 deletions(-) diff --git a/src/service/storage/Storage.cpp b/src/service/storage/Storage.cpp index 525bfa1..a35da0c 100644 --- a/src/service/storage/Storage.cpp +++ b/src/service/storage/Storage.cpp @@ -37,10 +37,10 @@ namespace Cynara { PolicyResult Storage::checkPolicy(const PolicyKey &key) { auto policies = m_backend.searchDefaultBucket(key); - return minimalPolicy(policies); + return minimalPolicy(policies, key); }; -PolicyResult Storage::minimalPolicy(const PolicyBucket &bucket) { +PolicyResult Storage::minimalPolicy(const PolicyBucket &bucket, const PolicyKey &key) { bool hasMinimal = false; PolicyResult minimal = bucket.defaultPolicy(); @@ -63,9 +63,8 @@ PolicyResult Storage::minimalPolicy(const PolicyBucket &bucket) { return policyResult; // Do not expect lower value than DENY break; case PredefinedPolicyType::BUCKET: { - auto bucketResults = m_backend.searchBucket(policyResult.metadata(), - policyRecord->key()); - auto minimumOfBucket = minimalPolicy(bucketResults); + auto bucketResults = m_backend.searchBucket(policyResult.metadata(), key); + auto minimumOfBucket = minimalPolicy(bucketResults, key); proposeMinimal(minimumOfBucket); continue; } diff --git a/src/service/storage/Storage.h b/src/service/storage/Storage.h index 8309092..e16d60c 100644 --- a/src/service/storage/Storage.h +++ b/src/service/storage/Storage.h @@ -55,7 +55,7 @@ public: void deleteBucket(const PolicyBucketId &bucketId); protected: - PolicyResult minimalPolicy(const PolicyBucket &bucket); + PolicyResult minimalPolicy(const PolicyBucket &bucket, const PolicyKey &key); private: StorageBackend &m_backend; // backend strategy diff --git a/test/storage/storage/check.cpp b/test/storage/storage/check.cpp index 426c9f3..129eb8d 100644 --- a/test/storage/storage/check.cpp +++ b/test/storage/storage/check.cpp @@ -119,3 +119,64 @@ TEST(storage, checkBucket) { defaultBucket.policyCollection().push_back(Policy::simpleWithKey(pk, PredefinedPolicyType::DENY)); ASSERT_EQ(PredefinedPolicyType::DENY, storage.checkPolicy(pk).policyType()); } + +// Catch a bug, where consecutive buckets were filtered with wildcard policies' keys +// instead of original key being checked +TEST(storage, checkBucketWildcard) { + using ::testing::Return; + using ::testing::ReturnPointee; + + const PolicyBucketId additionalBucketId = "additional-bucket"; + const PolicyKey defaultBucketKey = PolicyKey("c", "*", "p"); + const PolicyKey checkKey = PolicyKey("c", "u1", "p"); + + PolicyBucket defaultBucket(PolicyCollection({ + Policy::bucketWithKey(defaultBucketKey, additionalBucketId) + })); + + FakeStorageBackend backend; + Cynara::Storage storage(backend); + + EXPECT_CALL(backend, searchDefaultBucket(checkKey)) + .WillRepeatedly(ReturnPointee(&defaultBucket)); + + EXPECT_CALL(backend, searchBucket(defaultPolicyBucketId, checkKey)) + .WillRepeatedly(ReturnPointee(&defaultBucket)); + + // Check, if next bucket is filtered with original key + EXPECT_CALL(backend, searchBucket(additionalBucketId, checkKey)) + .WillRepeatedly(Return(PolicyBucket())); // additional bucket would yield no records + + // Should return additional bucket's default policy + ASSERT_EQ(PredefinedPolicyType::DENY, storage.checkPolicy(checkKey)); +} + +TEST(storage, checkBucketWildcardOtherDefault) { + using ::testing::ReturnPointee; + + const PolicyBucketId additionalBucketId = "additional-bucket"; + const PolicyKey defaultBucketKey = PolicyKey("c", "*", "p"); + const PolicyKey checkKey = PolicyKey("c", "u1", "p"); + + PolicyBucket defaultBucket(PolicyCollection({ + Policy::bucketWithKey(defaultBucketKey, additionalBucketId) + })); + + PolicyBucket additionalBucket(additionalBucketId, PredefinedPolicyType::ALLOW); + + FakeStorageBackend backend; + Cynara::Storage storage(backend); + + EXPECT_CALL(backend, searchDefaultBucket(checkKey)) + .WillRepeatedly(ReturnPointee(&defaultBucket)); + + EXPECT_CALL(backend, searchBucket(defaultPolicyBucketId, checkKey)) + .WillRepeatedly(ReturnPointee(&defaultBucket)); + + // Check, if next bucket is filtered with original key + EXPECT_CALL(backend, searchBucket(additionalBucketId, checkKey)) + .WillRepeatedly(ReturnPointee(&additionalBucket)); + + // Should return additional bucket's default policy + ASSERT_EQ(PredefinedPolicyType::ALLOW, storage.checkPolicy(checkKey)); +} -- 2.7.4 From 945f87a9273da38889a739d64e39479cc2863d40 Mon Sep 17 00:00:00 2001 From: Krzysztof Jackiewicz Date: Thu, 3 Jul 2014 11:36:48 +0200 Subject: [PATCH 05/16] Use proper include paths Change-Id: I34973bde473c2f7cfa35b00b04168c42c238726b --- src/service/storage/StorageDeserializer.cpp | 6 +++--- src/service/storage/StorageDeserializer.h | 4 ++-- test/storage/serializer/deserialize.cpp | 4 ++-- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/service/storage/StorageDeserializer.cpp b/src/service/storage/StorageDeserializer.cpp index 655f4a4..f1a433b 100644 --- a/src/service/storage/StorageDeserializer.cpp +++ b/src/service/storage/StorageDeserializer.cpp @@ -20,11 +20,11 @@ * @brief Implementation for Cynara::StorageDeserializer */ -#include +#include #include -#include -#include +#include +#include #include #include diff --git a/src/service/storage/StorageDeserializer.h b/src/service/storage/StorageDeserializer.h index 32310ac..6ef5c4e 100644 --- a/src/service/storage/StorageDeserializer.h +++ b/src/service/storage/StorageDeserializer.h @@ -22,8 +22,8 @@ #ifndef SRC_SERVICE_STORAGE_STORAGEDESERIALIZER_H_ #define SRC_SERVICE_STORAGE_STORAGEDESERIALIZER_H_ -#include -#include +#include +#include #include #include diff --git a/test/storage/serializer/deserialize.cpp b/test/storage/serializer/deserialize.cpp index 0ab39f6..bcd5052 100644 --- a/test/storage/serializer/deserialize.cpp +++ b/test/storage/serializer/deserialize.cpp @@ -24,8 +24,8 @@ #include #include -#include -#include +#include +#include #include #include -- 2.7.4 From da717ef7dda74cdf834b8a6baf7afb4c4d3eff98 Mon Sep 17 00:00:00 2001 From: Krzysztof Jackiewicz Date: Wed, 2 Jul 2014 15:18:31 +0200 Subject: [PATCH 06/16] Cleanup tests cmake file Requires: https://mcdsrvbld02.digital.local/review/#/c/15770/ Change-Id: I41e82319aa1ecaf647c303345fe30fb84010e568 --- CMakeLists.txt | 1 + test/CMakeLists.txt | 20 +++++--------------- test/common/exceptions/bucketrecordcorrupted.cpp | 2 +- test/common/types/policybucket.cpp | 6 +++--- test/helpers.cpp | 2 +- test/helpers.h | 4 ++-- test/storage/inmemorystoragebackend/buckets.cpp | 4 ++-- .../fakeinmemorystoragebackend.h | 2 +- .../inmemeorystoragebackendfixture.h | 6 +++--- .../inmemorystoragebackend.cpp | 18 +++++++++--------- test/storage/inmemorystoragebackend/search.cpp | 2 +- test/storage/serializer/bucket_load.cpp | 2 +- test/storage/serializer/dump.cpp | 10 +++++----- test/storage/storage/buckets.cpp | 18 +++++++++--------- test/storage/storage/check.cpp | 18 +++++++++--------- test/storage/storage/policies.cpp | 18 +++++++++--------- test/types/policykey.cpp | 2 +- 17 files changed, 63 insertions(+), 72 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 98a2f1f..7923083 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -57,6 +57,7 @@ SET(TARGET_CYNARA "cynara") SET(TARGET_LIB_CYNARA "cynara-client") SET(TARGET_LIB_CYNARA_ADMIN "cynara-admin") SET(TARGET_CYNARA_COMMON "cynara-commons") +SET(TARGET_CYNARA_TESTS "cynara-tests") ADD_SUBDIRECTORY(src) ADD_SUBDIRECTORY(build) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 0b53fe5..5828e62 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -14,15 +14,12 @@ # # @file CMakeLists.txt # @author Aleksander Zdyb -# @brief Temporary cmake for tests +# @brief Cmake for tests # -CMAKE_MINIMUM_REQUIRED(VERSION 2.8.3) -PROJECT("cynara-tests") - PKG_CHECK_MODULES(PKGS REQUIRED gmock_main) -SET(CYNARA_SRC ${PROJECT_SOURCE_DIR}/../src) +SET(CYNARA_SRC ${PROJECT_SOURCE_DIR}/src) SET(CYNARA_SOURCES_FOR_TESTS ${CYNARA_SRC}/service/storage/Storage.cpp @@ -53,24 +50,17 @@ SET(CYNARA_TESTS_SOURCES INCLUDE_DIRECTORIES( ${PKGS_INCLUDE_DIRS} - ${CYNARA_SRC} ${CYNARA_SRC}/common ${CYNARA_SRC}/include ${CYNARA_SRC}/service ) -ADD_EXECUTABLE(cynara-tests +ADD_EXECUTABLE(${TARGET_CYNARA_TESTS} ${CYNARA_SOURCES_FOR_TESTS} ${CYNARA_TESTS_SOURCES} ) -SET(CMAKE_CXX_FLAGS "-std=c++0x -O0 -g -gdwarf-2") - -FIND_PACKAGE(Threads) - -TARGET_LINK_LIBRARIES(cynara-tests - ${CMAKE_THREAD_LIBS_INIT} +TARGET_LINK_LIBRARIES(${TARGET_CYNARA_TESTS} ${PKGS_LDFLAGS} ${PKGS_LIBRARIES} ) - -INSTALL(TARGETS cynara-tests DESTINATION bin) +INSTALL(TARGETS ${TARGET_CYNARA_TESTS} DESTINATION bin) diff --git a/test/common/exceptions/bucketrecordcorrupted.cpp b/test/common/exceptions/bucketrecordcorrupted.cpp index 80aaadb..ddc6039 100644 --- a/test/common/exceptions/bucketrecordcorrupted.cpp +++ b/test/common/exceptions/bucketrecordcorrupted.cpp @@ -24,7 +24,7 @@ #include #include -#include "common/exceptions/BucketRecordCorruptedException.h" +#include "exceptions/BucketRecordCorruptedException.h" using namespace Cynara; diff --git a/test/common/types/policybucket.cpp b/test/common/types/policybucket.cpp index b48283f..db6c761 100644 --- a/test/common/types/policybucket.cpp +++ b/test/common/types/policybucket.cpp @@ -23,9 +23,9 @@ #include #include -#include "common/types/PolicyBucket.h" -#include "common/types/PolicyKey.h" -#include "common/types/PolicyCollection.h" +#include "types/PolicyBucket.h" +#include "types/PolicyKey.h" +#include "types/PolicyCollection.h" #include "../../helpers.h" diff --git a/test/helpers.cpp b/test/helpers.cpp index 17676cf..b6c5bd9 100644 --- a/test/helpers.cpp +++ b/test/helpers.cpp @@ -22,7 +22,7 @@ #include "helpers.h" -#include "common/types/PolicyKey.h" +#include "types/PolicyKey.h" namespace Cynara { namespace Helpers { diff --git a/test/helpers.h b/test/helpers.h index a3ca9ea..1f678d5 100644 --- a/test/helpers.h +++ b/test/helpers.h @@ -23,8 +23,8 @@ #ifndef HELPERS_H #define HELPERS_H -#include "common/types/PolicyKey.h" -#include "common/types/PolicyBucketId.h" +#include "types/PolicyKey.h" +#include "types/PolicyBucketId.h" namespace Cynara { namespace Helpers { diff --git a/test/storage/inmemorystoragebackend/buckets.cpp b/test/storage/inmemorystoragebackend/buckets.cpp index bcdca54..bf2c87c 100644 --- a/test/storage/inmemorystoragebackend/buckets.cpp +++ b/test/storage/inmemorystoragebackend/buckets.cpp @@ -25,8 +25,8 @@ #include "inmemeorystoragebackendfixture.h" #include "fakeinmemorystoragebackend.h" -#include "common/types/PolicyResult.h" -#include "common/types/PolicyBucket.h" +#include "types/PolicyResult.h" +#include "types/PolicyBucket.h" using namespace Cynara; diff --git a/test/storage/inmemorystoragebackend/fakeinmemorystoragebackend.h b/test/storage/inmemorystoragebackend/fakeinmemorystoragebackend.h index 60e3ed4..52e046f 100644 --- a/test/storage/inmemorystoragebackend/fakeinmemorystoragebackend.h +++ b/test/storage/inmemorystoragebackend/fakeinmemorystoragebackend.h @@ -23,7 +23,7 @@ #ifndef FAKEINMEMORYSTORAGEBACKEND_H_ #define FAKEINMEMORYSTORAGEBACKEND_H_ -#include "service/storage/InMemoryStorageBackend.h" +#include "storage/InMemoryStorageBackend.h" class FakeInMemoryStorageBackend : public Cynara::InMemoryStorageBackend { public: diff --git a/test/storage/inmemorystoragebackend/inmemeorystoragebackendfixture.h b/test/storage/inmemorystoragebackend/inmemeorystoragebackendfixture.h index ed07ebe..21ae4fc 100644 --- a/test/storage/inmemorystoragebackend/inmemeorystoragebackendfixture.h +++ b/test/storage/inmemorystoragebackend/inmemeorystoragebackendfixture.h @@ -25,9 +25,9 @@ #include "gmock/gmock.h" -#include "common/types/PolicyBucket.h" -#include "common/types/PolicyCollection.h" -#include "service/storage/InMemoryStorageBackend.h" +#include "types/PolicyBucket.h" +#include "types/PolicyCollection.h" +#include "storage/InMemoryStorageBackend.h" class InMemeoryStorageBackendFixture : public ::testing::Test { diff --git a/test/storage/inmemorystoragebackend/inmemorystoragebackend.cpp b/test/storage/inmemorystoragebackend/inmemorystoragebackend.cpp index 9cfe8cb..bd682b3 100644 --- a/test/storage/inmemorystoragebackend/inmemorystoragebackend.cpp +++ b/test/storage/inmemorystoragebackend/inmemorystoragebackend.cpp @@ -23,15 +23,15 @@ #include #include -#include "common/types/PolicyType.h" -#include "common/types/PolicyKey.h" -#include "common/types/PolicyResult.h" -#include "common/types/PolicyCollection.h" -#include "common/exceptions/DefaultBucketDeletionException.h" -#include "common/exceptions/BucketAlreadyExistsException.h" -#include "common/exceptions/BucketNotExistsException.h" -#include "service/storage/StorageBackend.h" -#include "service/storage/InMemoryStorageBackend.h" +#include "types/PolicyType.h" +#include "types/PolicyKey.h" +#include "types/PolicyResult.h" +#include "types/PolicyCollection.h" +#include "exceptions/DefaultBucketDeletionException.h" +#include "exceptions/BucketAlreadyExistsException.h" +#include "exceptions/BucketNotExistsException.h" +#include "storage/StorageBackend.h" +#include "storage/InMemoryStorageBackend.h" #include "../../helpers.h" #include "fakeinmemorystoragebackend.h" diff --git a/test/storage/inmemorystoragebackend/search.cpp b/test/storage/inmemorystoragebackend/search.cpp index 705c075..65c9057 100644 --- a/test/storage/inmemorystoragebackend/search.cpp +++ b/test/storage/inmemorystoragebackend/search.cpp @@ -26,7 +26,7 @@ #include "inmemeorystoragebackendfixture.h" #include "fakeinmemorystoragebackend.h" -#include "common/types/PolicyBucket.h" +#include "types/PolicyBucket.h" #include "../../helpers.h" diff --git a/test/storage/serializer/bucket_load.cpp b/test/storage/serializer/bucket_load.cpp index 87d6fb0..b43d12f 100644 --- a/test/storage/serializer/bucket_load.cpp +++ b/test/storage/serializer/bucket_load.cpp @@ -25,7 +25,7 @@ #include #include -#include +#include #include #include "../../helpers.h" diff --git a/test/storage/serializer/dump.cpp b/test/storage/serializer/dump.cpp index 9d53e17..b75928e 100644 --- a/test/storage/serializer/dump.cpp +++ b/test/storage/serializer/dump.cpp @@ -23,11 +23,11 @@ #include #include -#include "service/storage/StorageSerializer.h" -#include "common/types/PolicyBucket.h" -#include "common/types/PolicyType.h" -#include "common/types/PolicyKey.h" -#include "common/types/Policy.h" +#include "storage/StorageSerializer.h" +#include "types/PolicyBucket.h" +#include "types/PolicyType.h" +#include "types/PolicyKey.h" +#include "types/Policy.h" #include "../../helpers.h" diff --git a/test/storage/storage/buckets.cpp b/test/storage/storage/buckets.cpp index e34c67a..44e4cfd 100644 --- a/test/storage/storage/buckets.cpp +++ b/test/storage/storage/buckets.cpp @@ -24,15 +24,15 @@ #include #include -#include "common/types/PolicyType.h" -#include "common/types/PolicyKey.h" -#include "common/types/PolicyResult.h" -#include "common/types/PolicyCollection.h" -#include "common/types/pointers.h" -#include "common/exceptions/DefaultBucketDeletionException.h" -#include "common/exceptions/BucketAlreadyExistsException.h" -#include "service/storage/Storage.h" -#include "service/storage/StorageBackend.h" +#include "types/PolicyType.h" +#include "types/PolicyKey.h" +#include "types/PolicyResult.h" +#include "types/PolicyCollection.h" +#include "types/pointers.h" +#include "exceptions/DefaultBucketDeletionException.h" +#include "exceptions/BucketAlreadyExistsException.h" +#include "storage/Storage.h" +#include "storage/StorageBackend.h" #include "fakestoragebackend.h" diff --git a/test/storage/storage/check.cpp b/test/storage/storage/check.cpp index 129eb8d..c91cd1f 100644 --- a/test/storage/storage/check.cpp +++ b/test/storage/storage/check.cpp @@ -23,15 +23,15 @@ #include #include -#include "common/types/PolicyType.h" -#include "common/types/PolicyKey.h" -#include "common/types/PolicyResult.h" -#include "common/types/PolicyCollection.h" -#include "common/types/pointers.h" -#include "common/exceptions/DefaultBucketDeletionException.h" -#include "common/exceptions/BucketAlreadyExistsException.h" -#include "service/storage/Storage.h" -#include "service/storage/StorageBackend.h" +#include "types/PolicyType.h" +#include "types/PolicyKey.h" +#include "types/PolicyResult.h" +#include "types/PolicyCollection.h" +#include "types/pointers.h" +#include "exceptions/DefaultBucketDeletionException.h" +#include "exceptions/BucketAlreadyExistsException.h" +#include "storage/Storage.h" +#include "storage/StorageBackend.h" #include "fakestoragebackend.h" #include "../../helpers.h" diff --git a/test/storage/storage/policies.cpp b/test/storage/storage/policies.cpp index fc067be..d43d918 100644 --- a/test/storage/storage/policies.cpp +++ b/test/storage/storage/policies.cpp @@ -23,15 +23,15 @@ #include #include -#include "common/types/PolicyType.h" -#include "common/types/PolicyKey.h" -#include "common/types/PolicyResult.h" -#include "common/types/PolicyCollection.h" -#include "common/types/pointers.h" -#include "common/exceptions/DefaultBucketDeletionException.h" -#include "common/exceptions/BucketAlreadyExistsException.h" -#include "service/storage/Storage.h" -#include "service/storage/StorageBackend.h" +#include "types/PolicyType.h" +#include "types/PolicyKey.h" +#include "types/PolicyResult.h" +#include "types/PolicyCollection.h" +#include "types/pointers.h" +#include "exceptions/DefaultBucketDeletionException.h" +#include "exceptions/BucketAlreadyExistsException.h" +#include "storage/Storage.h" +#include "storage/StorageBackend.h" #include "fakestoragebackend.h" diff --git a/test/types/policykey.cpp b/test/types/policykey.cpp index 4ed7fd2..0c8bd3b 100644 --- a/test/types/policykey.cpp +++ b/test/types/policykey.cpp @@ -27,7 +27,7 @@ #include "../helpers.h" -#include "common/types/PolicyKey.h" +#include "types/PolicyKey.h" using namespace Cynara; -- 2.7.4 From 867ee8bbfae554120c68f204c16e3dd0b4b16be5 Mon Sep 17 00:00:00 2001 From: Rafal Krypa Date: Thu, 3 Jul 2014 14:14:14 +0200 Subject: [PATCH 07/16] Move cynara-tests packaging to a separate directory No gmock dependency in main spec file Cynara-tests package can be build by passing "--packaging-dir packaging_tests" to gbs build Change-Id: I786ad78bae0bceca0ae6423f7bf33981435bd189 --- CMakeLists.txt | 2 -- packaging/cynara.spec | 17 +----------- .../cynara-tests.manifest | 0 packaging_tests/cynara-tests.spec | 31 ++++++++++++++++++++++ test/CMakeLists.txt | 27 ++++++++++++++++++- 5 files changed, 58 insertions(+), 19 deletions(-) rename {packaging => packaging_tests}/cynara-tests.manifest (100%) create mode 100644 packaging_tests/cynara-tests.spec diff --git a/CMakeLists.txt b/CMakeLists.txt index 7923083..85bcfdb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -57,9 +57,7 @@ SET(TARGET_CYNARA "cynara") SET(TARGET_LIB_CYNARA "cynara-client") SET(TARGET_LIB_CYNARA_ADMIN "cynara-admin") SET(TARGET_CYNARA_COMMON "cynara-commons") -SET(TARGET_CYNARA_TESTS "cynara-tests") ADD_SUBDIRECTORY(src) ADD_SUBDIRECTORY(build) ADD_SUBDIRECTORY(systemd) -ADD_SUBDIRECTORY(test) diff --git a/packaging/cynara.spec b/packaging/cynara.spec index 8ab687f..1d7b8c8 100644 --- a/packaging/cynara.spec +++ b/packaging/cynara.spec @@ -8,7 +8,6 @@ Source0: %{name}-%{version}.tar.gz Source1001: cynara.manifest Source1002: libcynara-client.manifest Source1003: libcynara-admin.manifest -Source1004: cynara-tests.manifest BuildRequires: cmake BuildRequires: zip BuildRequires: pkgconfig(libsystemd-daemon) @@ -74,21 +73,11 @@ Requires: cynara = %{version}-%{release} %description -n cynara-devel service (devel version) -####################################################### -%package -n cynara-tests -Summary: Cynara tests -BuildRequires: pkgconfig(gmock) - -%description -n cynara-tests -cynara tests - - %prep %setup -q cp -a %{SOURCE1001} . cp -a %{SOURCE1002} . cp -a %{SOURCE1003} . -cp -a %{SOURCE1004} . %build %if 0%{?sec_build_binary_debug_enable} @@ -202,9 +191,5 @@ fi %files -n libcynara-admin-devel %defattr(-,root,root,-) %{_includedir}/cynara/cynara-admin.h -%{_libdir}/pkgconfig/cynara-admin.pc %{_libdir}/libcynara-admin.so - -%files -n cynara-tests -%manifest cynara-tests.manifest -%attr(755,root,root) /usr/bin/cynara-tests +%{_libdir}/pkgconfig/cynara-admin.pc diff --git a/packaging/cynara-tests.manifest b/packaging_tests/cynara-tests.manifest similarity index 100% rename from packaging/cynara-tests.manifest rename to packaging_tests/cynara-tests.manifest diff --git a/packaging_tests/cynara-tests.spec b/packaging_tests/cynara-tests.spec new file mode 100644 index 0000000..8b1de31 --- /dev/null +++ b/packaging_tests/cynara-tests.spec @@ -0,0 +1,31 @@ +Name: cynara-tests +Summary: Cynara tests +Version: 0.0.1 +Release: 1 +Group: Development/Testing +License: Apache-2.0 +Source0: %{name}-%{version}.tar.gz +Source1001: cynara-tests.manifest +BuildRequires: cmake +BuildRequires: pkgconfig(gmock) + +%description +Cynara tests + +%global build_type %{?build_type:%build_type}%{!?build_type:RELEASE} + +%prep +%setup -q +cp -a %{SOURCE1001} . + +%build +%cmake test -DCMAKE_BUILD_TYPE=%{?build_type} \ + -DCMAKE_VERBOSE_MAKEFILE=ON +make %{?jobs:-j%jobs} + +%install +%make_install + +%files +%manifest cynara-tests.manifest +%attr(755,root,root) /usr/bin/cynara-tests diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 5828e62..0b6f913 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -17,9 +17,34 @@ # @brief Cmake for tests # +############################# Check minimum CMake version ##################### + +CMAKE_MINIMUM_REQUIRED(VERSION 2.8.3) +PROJECT("cynara-tests") + +############################# cmake packages ################################## + +INCLUDE(FindPkgConfig) + +############################# compiler flags ################################## + +SET(CMAKE_C_FLAGS_DEBUG "-g -O0 -ggdb") +SET(CMAKE_CXX_FLAGS_DEBUG "-g -std=c++0x -O0 -ggdb -Wp,-U_FORTIFY_SOURCE") +SET(CMAKE_C_FLAGS_RELEASE "-g -O2") +SET(CMAKE_CXX_FLAGS_RELEASE "-g -std=c++0x -O2") + +# Set compiler warning flags +ADD_DEFINITIONS("-Werror") # Make all warnings into errors. +ADD_DEFINITIONS("-Wall") # Generate all warnings +ADD_DEFINITIONS("-Wextra") # Generate even more extra warnings + +MESSAGE(STATUS "CMAKE_BUILD_TYPE: ${CMAKE_BUILD_TYPE}") + +SET(TARGET_CYNARA_TESTS "cynara-tests") + PKG_CHECK_MODULES(PKGS REQUIRED gmock_main) -SET(CYNARA_SRC ${PROJECT_SOURCE_DIR}/src) +SET(CYNARA_SRC ${PROJECT_SOURCE_DIR}/../src) SET(CYNARA_SOURCES_FOR_TESTS ${CYNARA_SRC}/service/storage/Storage.cpp -- 2.7.4 From 919aff64e5a7e8767a03e65d8935abade71928d2 Mon Sep 17 00:00:00 2001 From: Lukasz Wojciechowski Date: Tue, 1 Jul 2014 21:28:09 +0200 Subject: [PATCH 08/16] Define libcynara-admin API Change-Id: I9d3708ad4a69336e893bcb683d69c3bc613e3054 --- src/include/cynara-admin.h | 208 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 203 insertions(+), 5 deletions(-) diff --git a/src/include/cynara-admin.h b/src/include/cynara-admin.h index 3eb9167..23325a9 100644 --- a/src/include/cynara-admin.h +++ b/src/include/cynara-admin.h @@ -13,21 +13,219 @@ * See the License for the specific language governing permissions and * limitations under the License */ -/* - * @file cynara-admin.h - * @author Lukasz Wojciechowski - * @version 1.0 - * @brief This file contains administration APIs of Cynara available with libcynara-admin. +/** + * \file cynara-admin.h + * \author Lukasz Wojciechowski + * \version 1.0 + * \brief This file contains administration APIs of cynara available with libcynara-admin. */ #ifndef CYNARA_ADMIN_H #define CYNARA_ADMIN_H +/** + * \name Return Codes + * exported by the foundation API. + * result codes begin with the start error code and extend into negative direction. + * @{ +*/ + +/*! \brief indicating the result of the one specific API is successful or access is allowed */ +#define CYNARA_ADMIN_API_SUCCESS 0 + +/*! \brief indicating system is running out of memory state */ +#define CYNARA_ADMIN_API_OUT_OF_MEMORY -1 + +/*! \brief indicating the API's parameter is malformed */ +#define CYNARA_ADMIN_API_INVALID_PARAM -2 + +/*! \brief service not available */ +#define CYNARA_ADMIN_API_SERVICE_NOT_AVAILABLE -3 +/** @}*/ + #ifdef __cplusplus extern "C" { #endif +//todo comment +const char *CYNARA_ADMIN_WILDCARD = "*"; + +//todo comment +const char *CYNARA_ADMIN_DEFAULT_BUCKET = ""; + +//todo comments +#define CYNARA_ADMIN_DELETE -1 +#define CYNARA_ADMIN_DENY 0 +#define CYNARA_ADMIN_ALLOW 1 +#define CYNARA_ADMIN_BUCKET 2 + +//todo comments +struct cynara_admin_policy { + const char *bucket; + + const char *client; + const char *user; + const char *privilege; + + int result; + const char *result_extra; +}; + +/** + * \par Description: + * Initialize cynara-admin library. + * Creates structure used in following API calls. + * + * \par Purpose: + * This function must be invoked prior to other admin API calls. It creates structure needed by + * other cynara-admin library API functions. + * + * \par Typical use case: + * Once before a service can call other cynara-admin library functions. + * + * \par Method of function operation: + * This API initializes inner library structures and in case of success creates cynara_admin + * structure and stores pointer to this structure at memory address passed in pp_cynara_admin + * parameter. + * + * \par Sync (or) async: + * This is a synchronous API. + * + * \par Important notes: + * Structure cynara_admin created by cynara_admin_initialize call should be released with + * cynara_admin_finish. + * + * \param[out] pp_cynara_admin address of pointer for created cynara_admin structure. + * + * \return CYNARA_ADMIN_API_SUCCESS on success, or error code otherwise. + * + * \brief Initialize cynara-admin library. + */ +int cynara_admin_initialize(struct cynara_admin **pp_cynara_admin); + +/** + * \par Description: + * Releases cynara-admin library and destroys structure created with cynara_admin_initialize + * function. + * + * \par Purpose: + * This API should be used to clean up after usage of cynara-admin library. + * + * \par Typical use case: + * Function should be called once, when done with cynara-admin library API usage. + * + * \par Method of function operation: + * This API releases inner library structures and destroys cynara_admin structure. + * + * \par Sync (or) Async: + * This is a Synchronous API. + * + * \par Important notes: + * No invocations of cynara-admin library API functions are allowed after call to + * cynara_admin_finish. + * + * \param[in] p_cynara_admin cynara_admin structure created in cynara_admin_initialize. + * + * \return CYNARA_ADMIN_API_SUCCESS on success, or error code otherwise. + * + * \brief Release cynara-admin library. + */ +int cynara_admin_finish(cynara_admin *p_cynara_admin); + +/** + * \par Description: + * Manages policies in cynara. + * + * \par Purpose: + * This API should be used to insert, update or delete policies in cynara. + * + * \par Typical use case: + * Enables privileged services to alter policies by adding, updating or removing records. + * + * \par Method of function operation: + * Policies are arranged into buckets. Every policy is defined in context of some bucket identified + * with bucket field (string). A bucket consists of policies identified with tripple: (client, user, + * privilege), which is a (unique) key within considered bucket. + * + * Every policy can be one of two types: simple or bucket-pointing policy. + * Simple policies have result field with value of CYNARA_ADMIN_DENY or CYNARA_ADMIN_ALLOW. + * result_extra field should be NULL in this case. + * Bucket-pointing policies have result field with value of CYNARA_ADMIN_BUCKET and name of bucket + * they point to in result_extra field. + * + * Type of operation, which is run for every record (single policy) is defined by result field in + * cynara_admin_policy structure. + * In case of CYNARA_ADMIN_DENY or CYNARA_ADMIN_ALLOW a simple policy is updated or inserted into + * cynara database. + * In case of CYNARA_ADMIN_BUCKET, a bucket-pointing policy is updated or inserted into cynara + * database. + * In case of CYNARA_ADMIN_DELETE, a policy is removed from cynara database. + * One call of cynara_admin_insert_policies can manage many different policies in different buckets. + * + * However, considered buckets must exist before referring to them in policies. + * + * \par Sync (or) Async: + * This is a Synchronous API. + * + * \par Important notes: + * When plugin API will be specified, there will be more valid types to pass as result. + * Numerical values of defines CYNARA_ADMIN_... may change, so usage of defines names is strongly + * recommended. + * + * In case of error, database may end up in a state, where changes are partially applied. This is + * going to be fixed along with introduction of transactions in future releases. + * + * \param[in] p_cynara_admin cynara admin structure. + * \param[in] policies NULL terminated array of policy structures + * + * \return CYNARA_ADMIN_API_SUCCESS on success, or error code otherwise. + * + * \brief Insert, update or delete policies in cynara database. + */ +int cynara_admin_set_policies(cynara_admin *p_cynara_admin, const cynara_admin_policy *policies); + +/** + * \par Description: + * Adds new, updates or removes existing bucket for policies in cynara. + * + * \par Purpose: + * This API should be used to add, remove or update buckets. + * + * \par Typical use case: + * Enables privileged services to alter policies database by adding, updating or removing buckets. + * + * \par Method of function operation: + * Every bucket has a default policy. During search, if no policy matches the searched key (client, + * user, privilege), default policy is returned. + + * Operation run on a single bucket defined with bucket parameter. + + * Operation parameter defines what should happen with bucket. In case of: + * CYNARA_ADMIN_DENY, a bucket is inserted or updated with CYNARA_ADMIN_DENY default policy; + * CYNARA_ADMIN_ALLOW, a bucket is inserted or updated with CYNARA_ADMIN_ALLOW default policy; + * CYNARA_ADMIN_DELETE, a bucket is removed with all policies that were kept in it. + * + * \par Sync (or) Async: + * This is a Synchronous API. + * + * \par Important notes: + * When plugin API will be specified, there will be more valid types to pass as operation / default + * policy. Numerical values of defines CYNARA_ADMIN_... may change, so usages of provided consts is + * strongly recommended. + * + * Default bucket identified with CYNARA_ADMIN_DEFAULT_BUCKET exists always. Its default policy + * is preset to DENY (can be altered, however). Default bucket cannot be removed. + * + * \param[in] p_cynara_admin cynara admin structure. + * \param[in] bucket bucket name + * \param[in] operation type of operation (default policy or CYNARA_ADMIN_DELETE) + * + * \return CYNARA_ADMIN_API_SUCCESS on success, or error code otherwise. + * + * \brief Add, remove or update buckets in cynara database. + */ +int cynara_admin_set_bucket(cynara_admin *p_cynara_admin, const char *bucket, int operation); #ifdef __cplusplus } -- 2.7.4 From 46390d326e6659323090c0043229e4461ee5ace4 Mon Sep 17 00:00:00 2001 From: Lukasz Wojciechowski Date: Wed, 2 Jul 2014 09:28:35 +0200 Subject: [PATCH 09/16] Make SocketClient independent of specific protocol and path Add protocol and socket path to SocketClient constructor. Pass path to client socket and ProtocolClient object to SocketClient in libcynara-client Logic class. Change-Id: I45e991b8309616876248289558265e5df915012d --- src/client/logic/Logic.cpp | 7 ++++++- src/client/sockets/SocketClient.cpp | 7 ++----- src/client/sockets/SocketClient.h | 2 +- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/client/logic/Logic.cpp b/src/client/logic/Logic.cpp index 4a46c33..6b04973 100644 --- a/src/client/logic/Logic.cpp +++ b/src/client/logic/Logic.cpp @@ -26,6 +26,8 @@ #include #include #include +#include +#include #include #include #include @@ -38,8 +40,11 @@ namespace Cynara { +const std::string clientSocketPath("/run/cynara/cynara.socket"); + Logic::Logic() { - m_socketClient = std::make_shared(); + m_socketClient = std::make_shared(clientSocketPath, + std::make_shared()); } cynara_api_result Logic::check(const std::string &client, const std::string &session UNUSED, diff --git a/src/client/sockets/SocketClient.cpp b/src/client/sockets/SocketClient.cpp index 9bab9b1..90f89de 100644 --- a/src/client/sockets/SocketClient.cpp +++ b/src/client/sockets/SocketClient.cpp @@ -25,7 +25,6 @@ #include #include -#include #include #include #include @@ -37,10 +36,8 @@ namespace Cynara { -const std::string clientSocketPath("/run/cynara/cynara.socket"); - -SocketClient::SocketClient() : m_socket(clientSocketPath), - m_protocol(std::make_shared()) { +SocketClient::SocketClient(const std::string &socketPath, ProtocolPtr protocol) + : m_socket(socketPath), m_protocol(protocol) { } ResponsePtr SocketClient::askCynaraServer(RequestPtr request) { diff --git a/src/client/sockets/SocketClient.h b/src/client/sockets/SocketClient.h index f0af703..d3e16a1 100644 --- a/src/client/sockets/SocketClient.h +++ b/src/client/sockets/SocketClient.h @@ -46,7 +46,7 @@ private: BinaryQueue m_writeQueue; public: - SocketClient(); + SocketClient(const std::string &socketPath, ProtocolPtr protocol); virtual ~SocketClient() = default; //returns pointer to response -- 2.7.4 From 62d0e2190cdd6d2feb1f25efb4a1add2aa99d3c4 Mon Sep 17 00:00:00 2001 From: Lukasz Wojciechowski Date: Wed, 2 Jul 2014 09:34:08 +0200 Subject: [PATCH 10/16] Raw move of client socket classes to common library This will allow usage of same classes in libcynara-admin library. This patch does not compile. Please verify it with following patch. Change-Id: Ib0d2032e2e843ad9bdb34e6a087037436ff8a12f --- src/{client => common}/sockets/Socket.cpp | 0 src/{client => common}/sockets/Socket.h | 0 src/{client => common}/sockets/SocketClient.cpp | 0 src/{client => common}/sockets/SocketClient.h | 0 4 files changed, 0 insertions(+), 0 deletions(-) rename src/{client => common}/sockets/Socket.cpp (100%) rename src/{client => common}/sockets/Socket.h (100%) rename src/{client => common}/sockets/SocketClient.cpp (100%) rename src/{client => common}/sockets/SocketClient.h (100%) diff --git a/src/client/sockets/Socket.cpp b/src/common/sockets/Socket.cpp similarity index 100% rename from src/client/sockets/Socket.cpp rename to src/common/sockets/Socket.cpp diff --git a/src/client/sockets/Socket.h b/src/common/sockets/Socket.h similarity index 100% rename from src/client/sockets/Socket.h rename to src/common/sockets/Socket.h diff --git a/src/client/sockets/SocketClient.cpp b/src/common/sockets/SocketClient.cpp similarity index 100% rename from src/client/sockets/SocketClient.cpp rename to src/common/sockets/SocketClient.cpp diff --git a/src/client/sockets/SocketClient.h b/src/common/sockets/SocketClient.h similarity index 100% rename from src/client/sockets/SocketClient.h rename to src/common/sockets/SocketClient.h -- 2.7.4 From 918289136936220302a30cc3f1e75e3b32e66c2c Mon Sep 17 00:00:00 2001 From: Lukasz Wojciechowski Date: Wed, 2 Jul 2014 09:49:41 +0200 Subject: [PATCH 11/16] Adjust client socket classes after move to common library Fix #ifndef guards, include paths and CMakeLists. This patch should be verified together with previous one (Raw move). Change-Id: I81640c5297c11435e2fe0e8b18c9d37ded5318a3 --- src/client/CMakeLists.txt | 2 -- src/client/logic/Logic.cpp | 2 +- src/client/logic/Logic.h | 3 ++- src/common/CMakeLists.txt | 2 ++ src/common/sockets/Socket.cpp | 2 ++ src/common/sockets/Socket.h | 8 ++++---- src/common/sockets/SocketClient.cpp | 1 - src/common/sockets/SocketClient.h | 9 ++++----- 8 files changed, 15 insertions(+), 14 deletions(-) diff --git a/src/client/CMakeLists.txt b/src/client/CMakeLists.txt index 2f51764..89fe001 100644 --- a/src/client/CMakeLists.txt +++ b/src/client/CMakeLists.txt @@ -24,8 +24,6 @@ SET(CYNARA_LIB_CYNARA_PATH ${CYNARA_PATH}/client) SET(LIB_CYNARA_SOURCES ${CYNARA_LIB_CYNARA_PATH}/api/client-api.cpp ${CYNARA_LIB_CYNARA_PATH}/logic/Logic.cpp - ${CYNARA_LIB_CYNARA_PATH}/sockets/Socket.cpp - ${CYNARA_LIB_CYNARA_PATH}/sockets/SocketClient.cpp ) INCLUDE_DIRECTORIES( diff --git a/src/client/logic/Logic.cpp b/src/client/logic/Logic.cpp index 6b04973..aa62511 100644 --- a/src/client/logic/Logic.cpp +++ b/src/client/logic/Logic.cpp @@ -32,10 +32,10 @@ #include #include #include +#include #include #include -#include #include "Logic.h" namespace Cynara { diff --git a/src/client/logic/Logic.h b/src/client/logic/Logic.h index 512b313..e16a451 100644 --- a/src/client/logic/Logic.h +++ b/src/client/logic/Logic.h @@ -25,9 +25,10 @@ #include -#include #include +#include + namespace Cynara { class Logic : public ApiInterface { diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index dbc5c3a..a689435 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -32,6 +32,8 @@ SET(COMMON_SOURCES ${COMMON_PATH}/request/RequestTaker.cpp ${COMMON_PATH}/response/CheckResponse.cpp ${COMMON_PATH}/response/ResponseTaker.cpp + ${COMMON_PATH}/sockets/Socket.cpp + ${COMMON_PATH}/sockets/SocketClient.cpp ${COMMON_PATH}/types/PolicyBucket.cpp ${COMMON_PATH}/types/PolicyKey.cpp ) diff --git a/src/common/sockets/Socket.cpp b/src/common/sockets/Socket.cpp index cddd05f..81be8d4 100644 --- a/src/common/sockets/Socket.cpp +++ b/src/common/sockets/Socket.cpp @@ -31,6 +31,8 @@ #include #include +#include +#include #include #include #include diff --git a/src/common/sockets/Socket.h b/src/common/sockets/Socket.h index 7c6a286..7ea8c61 100644 --- a/src/common/sockets/Socket.h +++ b/src/common/sockets/Socket.h @@ -21,12 +21,12 @@ * @brief This file contains definition of UNIX client socket class */ -#ifndef SRC_CLIENT_SOCKETS_SOCKET_H_ -#define SRC_CLIENT_SOCKETS_SOCKET_H_ +#ifndef SRC_COMMON_SOCKETS_SOCKET_H_ +#define SRC_COMMON_SOCKETS_SOCKET_H_ #include -#include +#include namespace Cynara { @@ -76,4 +76,4 @@ public: } // namespace Cynara -#endif /* SRC_CLIENT_SOCKETS_SOCKET_H_ */ +#endif /* SRC_COMMON_SOCKETS_SOCKET_H_ */ diff --git a/src/common/sockets/SocketClient.cpp b/src/common/sockets/SocketClient.cpp index 90f89de..6eff9f5 100644 --- a/src/common/sockets/SocketClient.cpp +++ b/src/common/sockets/SocketClient.cpp @@ -29,7 +29,6 @@ #include #include #include - #include #include "SocketClient.h" diff --git a/src/common/sockets/SocketClient.h b/src/common/sockets/SocketClient.h index d3e16a1..04f0436 100644 --- a/src/common/sockets/SocketClient.h +++ b/src/common/sockets/SocketClient.h @@ -20,17 +20,16 @@ * @brief This file contains definition of cynara's socket client */ -#ifndef SRC_CLIENT_SOCKETS_SOCKETCLIENT_H_ -#define SRC_CLIENT_SOCKETS_SOCKETCLIENT_H_ +#ifndef SRC_COMMON_SOCKETS_SOCKETCLIENT_H_ +#define SRC_COMMON_SOCKETS_SOCKETCLIENT_H_ #include -#include +#include #include #include #include #include - #include namespace Cynara { @@ -56,4 +55,4 @@ public: } // namespace Cynara -#endif /* SRC_CLIENT_SOCKETS_SOCKETCLIENT_H_ */ +#endif /* SRC_COMMON_SOCKETS_SOCKETCLIENT_H_ */ -- 2.7.4 From 06e6660091c44b06164538add59e21b15b235b1c Mon Sep 17 00:00:00 2001 From: Adam Malinowski Date: Thu, 26 Jun 2014 10:04:17 +0200 Subject: [PATCH 12/16] Add protocol deserialialization mechanism Change-Id: Id3b24f67a639f22cfd2995880ad3b21593318822 --- src/client/logic/Logic.cpp | 4 +- src/common/CMakeLists.txt | 3 + src/common/containers/BinaryQueue.h | 2 +- src/common/exceptions/InvalidProtocolException.h | 75 ++++++++++++ src/common/protocol/Protocol.h | 8 ++ src/common/protocol/ProtocolClient.cpp | 50 +++++++- src/common/protocol/ProtocolClient.h | 8 ++ src/common/protocol/ProtocolFrame.cpp | 44 +++++++ src/common/protocol/ProtocolFrame.h | 67 +++++++++++ src/common/protocol/ProtocolFrameHeader.cpp | 47 ++++++++ src/common/protocol/ProtocolFrameHeader.h | 114 ++++++++++++++++++ src/common/protocol/ProtocolFrameSerializer.cpp | 78 +++++++++++++ src/common/protocol/ProtocolFrameSerializer.h | 43 +++++++ src/common/protocol/ProtocolOpCode.h | 48 ++++++++ src/common/protocol/ProtocolSerialization.h | 141 ++++++++++++++++++----- src/common/request/CheckRequest.h | 3 +- src/common/request/Request.h | 13 ++- src/common/request/RequestTaker.cpp | 3 + src/common/types/ProtocolFields.h | 39 +++++++ 19 files changed, 753 insertions(+), 37 deletions(-) create mode 100644 src/common/exceptions/InvalidProtocolException.h create mode 100644 src/common/protocol/ProtocolFrame.cpp create mode 100644 src/common/protocol/ProtocolFrame.h create mode 100644 src/common/protocol/ProtocolFrameHeader.cpp create mode 100644 src/common/protocol/ProtocolFrameHeader.h create mode 100644 src/common/protocol/ProtocolFrameSerializer.cpp create mode 100644 src/common/protocol/ProtocolFrameSerializer.h create mode 100644 src/common/protocol/ProtocolOpCode.h create mode 100644 src/common/types/ProtocolFields.h diff --git a/src/client/logic/Logic.cpp b/src/client/logic/Logic.cpp index aa62511..597c8e4 100644 --- a/src/client/logic/Logic.cpp +++ b/src/client/logic/Logic.cpp @@ -56,7 +56,9 @@ cynara_api_result Logic::check(const std::string &client, const std::string &ses //Ask cynara service PolicyResult result(PredefinedPolicyType::DENY); try { - RequestPtr request = std::make_shared(PolicyKey(client, user, privilege)); + // todo handle sequence number correctly + ProtocolFrameSequenceNumber sequenceNumber = 0; + RequestPtr request = std::make_shared(PolicyKey(client, user, privilege), sequenceNumber); ResponsePtr response = m_socketClient->askCynaraServer(request); if (!response) { LOGW("Disconnected by cynara server."); diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index a689435..5e6308a 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -26,6 +26,9 @@ SET(COMMON_SOURCES ${COMMON_PATH}/log/log.cpp ${COMMON_PATH}/protocol/ProtocolAdmin.cpp ${COMMON_PATH}/protocol/ProtocolClient.cpp + ${COMMON_PATH}/protocol/ProtocolFrame.cpp + ${COMMON_PATH}/protocol/ProtocolFrameHeader.cpp + ${COMMON_PATH}/protocol/ProtocolFrameSerializer.cpp ${COMMON_PATH}/protocol/ProtocolSerialization.cpp ${COMMON_PATH}/protocol/ProtocolSignal.cpp ${COMMON_PATH}/request/CheckRequest.cpp diff --git a/src/common/containers/BinaryQueue.h b/src/common/containers/BinaryQueue.h index ada9a3e..ccfbffe 100644 --- a/src/common/containers/BinaryQueue.h +++ b/src/common/containers/BinaryQueue.h @@ -32,7 +32,7 @@ namespace Cynara { * Binary queue auto pointer */ class BinaryQueue; -typedef std::auto_ptr BinaryQueueAutoPtr; +typedef std::shared_ptr BinaryQueuePtr; /** * Binary stream implemented as constant size bucket list diff --git a/src/common/exceptions/InvalidProtocolException.h b/src/common/exceptions/InvalidProtocolException.h new file mode 100644 index 0000000..8b1317b --- /dev/null +++ b/src/common/exceptions/InvalidProtocolException.h @@ -0,0 +1,75 @@ +/* + * 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 InvalidProtocolException.h + * @author Adam Malinowski + * @version 1.0 + * @brief Implementation of InvalidProtocolException + */ + +#ifndef SRC_COMMON_EXCEPTIONS_INVALIDPROTOCOLEXCEPTION_H_ +#define SRC_COMMON_EXCEPTIONS_INVALIDPROTOCOLEXCEPTION_H_ + +#include +#include + +#include "Exception.h" + +namespace Cynara { + +class InvalidProtocolException : public Exception { +public: + enum ExceptionType { + InvalidSignature, + WrongOpCode, + Other + }; + +private: + std::string m_whatMessage; + ExceptionType m_exceptionType; + +public: + InvalidProtocolException(ExceptionType exceptionType) : + m_exceptionType(exceptionType) { + switch(m_exceptionType) { + case InvalidSignature: + m_whatMessage = "No valid signature found"; + break; + case WrongOpCode: + m_whatMessage = "Wrong request code"; + break; + case Other: + m_whatMessage = "Unknown problem"; + break; + } + + } + + virtual ~InvalidProtocolException() = default; + + virtual const char *what(void) const noexcept { + return m_whatMessage.c_str(); + } + + ExceptionType exceptionTyp(void) const { + return m_exceptionType; + } +}; + +} // namespace Cynara + +#endif /* SRC_COMMON_EXCEPTIONS_INVALIDPROTOCOLEXCEPTION_H_ */ diff --git a/src/common/protocol/Protocol.h b/src/common/protocol/Protocol.h index 56ee51e..daf001c 100644 --- a/src/common/protocol/Protocol.h +++ b/src/common/protocol/Protocol.h @@ -26,6 +26,7 @@ #include #include +#include #include #include #include @@ -45,6 +46,13 @@ public: virtual RequestPtr extractRequestFromBuffer(BinaryQueue &bufferQueue) = 0; virtual ResponsePtr extractResponseFromBuffer(BinaryQueue &bufferQueue) = 0; + + ProtocolFrameHeader &frameHeader(void) { + return m_frameHeader; + } + +protected: + ProtocolFrameHeader m_frameHeader; }; } // namespace Cynara diff --git a/src/common/protocol/ProtocolClient.cpp b/src/common/protocol/ProtocolClient.cpp index 4e98d53..2671eb7 100644 --- a/src/common/protocol/ProtocolClient.cpp +++ b/src/common/protocol/ProtocolClient.cpp @@ -16,12 +16,23 @@ /* * @file ProtocolClient.cpp * @author Lukasz Wojciechowski + * @author Adam Malinowski * @version 1.0 * @brief This file implements protocol class for communication with client */ #include #include + +#include +#include +#include +#include +#include +#include +#include +#include + #include "ProtocolClient.h" namespace Cynara { @@ -36,9 +47,33 @@ ProtocolPtr ProtocolClient::clone(void) { return std::make_shared(); } +RequestPtr ProtocolClient::deserializeCheckRequest(ProtocolFrameHeader &frame) { + std::string clientId, userId, privilegeId; + ProtocolDeserialization::deserialize(frame, clientId); + ProtocolDeserialization::deserialize(frame, userId); + ProtocolDeserialization::deserialize(frame, privilegeId); + return std::make_shared(PolicyKey(clientId, userId, privilegeId), + frame.sequenceNumber()); +} + RequestPtr ProtocolClient::extractRequestFromBuffer(BinaryQueue &bufferQueue) { - TODO_USE_ME(bufferQueue); - return RequestPtr(nullptr); + ProtocolFrameSerializer::deserializeHeader(m_frameHeader, bufferQueue); + + if (m_frameHeader.isFrameComplete()) { + ProtocolOpCode requestId; + + m_frameHeader.resetState(); + ProtocolDeserialization::deserialize(m_frameHeader, requestId); + switch (requestId) { + case OpCheckPolicy: + return deserializeCheckRequest(m_frameHeader); + default: + throw InvalidProtocolException(InvalidProtocolException::WrongOpCode); + break; + } + } + + return nullptr; } ResponsePtr ProtocolClient::extractResponseFromBuffer(BinaryQueue &bufferQueue) { @@ -46,4 +81,15 @@ ResponsePtr ProtocolClient::extractResponseFromBuffer(BinaryQueue &bufferQueue) return ResponsePtr(nullptr); } +void ProtocolClient::execute(RequestContextPtr context, CheckRequestPtr request) { + ProtocolFramePtr frame = ProtocolFrameSerializer::startSerialization(request->sequenceNumber()); + + ProtocolSerialization::serialize(*frame, OpCheckPolicy); + ProtocolSerialization::serialize(*frame, request->key().client().value()); + ProtocolSerialization::serialize(*frame, request->key().user().value()); + ProtocolSerialization::serialize(*frame, request->key().privilege().value()); + + ProtocolFrameSerializer::finishSerialization(frame, context->responseQueue()); +} + } // namespace Cynara diff --git a/src/common/protocol/ProtocolClient.h b/src/common/protocol/ProtocolClient.h index 0b5cdb9..1cd608b 100644 --- a/src/common/protocol/ProtocolClient.h +++ b/src/common/protocol/ProtocolClient.h @@ -23,6 +23,9 @@ #ifndef SRC_COMMON_PROTOCOL_PROTOCOLCLIENT_H_ #define SRC_COMMON_PROTOCOL_PROTOCOLCLIENT_H_ +#include +#include + #include "Protocol.h" namespace Cynara { @@ -36,6 +39,11 @@ public: virtual RequestPtr extractRequestFromBuffer(BinaryQueue &bufferQueue); virtual ResponsePtr extractResponseFromBuffer(BinaryQueue &bufferQueue); + + virtual void execute(RequestContextPtr context, CheckRequestPtr request); + +private: + RequestPtr deserializeCheckRequest(ProtocolFrameHeader &frame); }; } // namespace Cynara diff --git a/src/common/protocol/ProtocolFrame.cpp b/src/common/protocol/ProtocolFrame.cpp new file mode 100644 index 0000000..0ca0be6 --- /dev/null +++ b/src/common/protocol/ProtocolFrame.cpp @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved + * + * Contact: Lukasz Wojciechowski + * + * 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 ProtocolFrame.cpp + * @author Adam Malinowski + * @version 1.0 + * @brief Implementation of ProtocolFrame class. + */ + +#include + +#include "ProtocolFrame.h" + +namespace Cynara { + +ProtocolFrame::ProtocolFrame(ProtocolFrameHeaderPtr frameHeader, BinaryQueuePtr data) : + m_frameHeader(frameHeader), m_frameBodyContent(data) { +} + +void ProtocolFrame::read(size_t num, void *bytes) { + m_frameBodyContent->flattenConsume(bytes, num); +} + +void ProtocolFrame::write(size_t num, const void *bytes) { + m_frameBodyContent->appendCopy(bytes, num); + m_frameHeader->increaseFrameLength(num); +} + +} /* namespace Cynara */ diff --git a/src/common/protocol/ProtocolFrame.h b/src/common/protocol/ProtocolFrame.h new file mode 100644 index 0000000..709196e --- /dev/null +++ b/src/common/protocol/ProtocolFrame.h @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved + * + * Contact: Lukasz Wojciechowski + * + * 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 ProtocolFrame.h + * @author Adam Malinowski + * @version 1.0 + * @brief Header for ProtocolFrame class. + */ + +#ifndef SRC_COMMON_PROTOCOL_PROTOCOLFRAME_H_ +#define SRC_COMMON_PROTOCOL_PROTOCOLFRAME_H_ + +#include +#include + +#include +#include +#include + +namespace Cynara { + +class ProtocolFrameSerializer; + +class ProtocolFrame: public IStream { + +public: + ProtocolFrame(ProtocolFrameHeaderPtr frameHeader, BinaryQueuePtr headerContent); + virtual ~ProtocolFrame() = default; + + ProtocolFrameHeaderPtr frameHeader(void) { + return m_frameHeader; + } + + virtual void read(size_t num, void *bytes); + virtual void write(size_t num, const void *bytes); + +private: + ProtocolFrameHeaderPtr m_frameHeader; + BinaryQueuePtr m_frameBodyContent; + + BinaryQueue &bodyContent(void) { + return *m_frameBodyContent; + } + + friend class ProtocolFrameSerializer; +}; + +typedef std::shared_ptr ProtocolFramePtr; + +} /* namespace Cynara */ + +#endif /* SRC_COMMON_PROTOCOL_PROTOCOLFRAME_H_ */ diff --git a/src/common/protocol/ProtocolFrameHeader.cpp b/src/common/protocol/ProtocolFrameHeader.cpp new file mode 100644 index 0000000..b8d5a37 --- /dev/null +++ b/src/common/protocol/ProtocolFrameHeader.cpp @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved + * + * Contact: Lukasz Wojciechowski + * + * 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 ProtocolFrameHeader.cpp + * @author Adam Malinowski + * @version 1.0 + * @brief Implementation of protocol frame header (de)serializer class. + */ + +#include +#include + +#include "ProtocolFrameHeader.h" + +namespace Cynara { + +const ProtocolFrameSignature ProtocolFrameHeader::m_signature = "CPv1"; + +ProtocolFrameHeader::ProtocolFrameHeader(BinaryQueuePtr headerContent) : + m_frameHeaderContent(headerContent), m_frameLength(0), m_sequenceNumber(0), + m_headerComplete(false), m_bodyComplete(false) { +} + +void ProtocolFrameHeader::read(size_t num, void *bytes) { + m_frameHeaderContent->flattenConsume(bytes, num); +} + +void ProtocolFrameHeader::write(size_t num, const void *bytes) { + m_frameHeaderContent->appendCopy(bytes, num); +} + +} /* namespace Cynara */ diff --git a/src/common/protocol/ProtocolFrameHeader.h b/src/common/protocol/ProtocolFrameHeader.h new file mode 100644 index 0000000..0db0d6b --- /dev/null +++ b/src/common/protocol/ProtocolFrameHeader.h @@ -0,0 +1,114 @@ +/* + * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved + * + * Contact: Lukasz Wojciechowski + * + * 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 ProtocolFrameHeader.h + * @author Adam Malinowski + * @version 1.0 + * @brief Header file for protocol frame header (de)serializer class. + */ + +#ifndef SRC_COMMON_PROTOCOL_PROTOCOLFRAMEHEADER_H_ +#define SRC_COMMON_PROTOCOL_PROTOCOLFRAMEHEADER_H_ + +#include + +#include +#include +#include + +namespace Cynara { + +class ProtocolFrameSerializer; + +class ProtocolFrameHeader: public IStream { +private: + static const ProtocolFrameSignature m_signature; + static size_t frameHeaderLength(void) { + return m_signature.size() + + sizeof(ProtocolFrameLength) + + sizeof(ProtocolFrameSequenceNumber); + } + +public: + ProtocolFrameHeader(BinaryQueuePtr headerContent = nullptr); + virtual ~ProtocolFrameHeader() = default; + + virtual void read(size_t num, void *bytes); + virtual void write(size_t num, const void *bytes); + + ProtocolFrameSequenceNumber sequenceNumber(void) { + return m_sequenceNumber; + } + + bool isHeaderComplete(void) { + return m_headerComplete; + } + + bool isFrameComplete(void) { + return m_headerComplete && m_bodyComplete; + } + + void resetState(void) { + m_headerComplete = false; + m_bodyComplete = false; + } + + ProtocolFrameLength frameLength(void) { + return m_frameLength; + } + +private: + BinaryQueuePtr m_frameHeaderContent; + ProtocolFrameLength m_frameLength; + ProtocolFrameSequenceNumber m_sequenceNumber; + bool m_headerComplete; + bool m_bodyComplete; + + void setSequenceNumber(ProtocolFrameSequenceNumber sequenceNumber) { + m_sequenceNumber = sequenceNumber; + } + + void increaseFrameLength(ProtocolFrameLength size) { + m_frameLength += size; + } + + BinaryQueue &headerContent(void) { + return *m_frameHeaderContent; + } + + void setHeaderContent(BinaryQueuePtr headerContent) { + m_frameHeaderContent = headerContent; + } + + void setHeaderComplete(void) { + m_headerComplete = true; + } + + void setBodyComplete(void) { + m_bodyComplete = true; + } + + friend class ProtocolFrame; + friend class ProtocolFrameSerializer; +}; + +typedef std::shared_ptr ProtocolFrameHeaderPtr; + +} /* namespace Cynara */ + +#endif /* SRC_COMMON_PROTOCOL_PROTOCOLFRAMEHEADER_H_ */ diff --git a/src/common/protocol/ProtocolFrameSerializer.cpp b/src/common/protocol/ProtocolFrameSerializer.cpp new file mode 100644 index 0000000..25a5649 --- /dev/null +++ b/src/common/protocol/ProtocolFrameSerializer.cpp @@ -0,0 +1,78 @@ +/* + * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved + * + * Contact: Lukasz Wojciechowski + * + * 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 ProtocolSerializer.cpp + * @author Adam Malinowski + * @version 1.0 + * @brief Implementation of protocol frame (de)serializer class. + */ + +#include +#include + +#include "ProtocolFrameSerializer.h" + +namespace Cynara { + +void ProtocolFrameSerializer::deserializeHeader(ProtocolFrameHeader &frameHeader, + BinaryQueue &data) { + if (!frameHeader.isHeaderComplete()) { + if ((data.size() < ProtocolFrameHeader::frameHeaderLength())) { + return; + } + + frameHeader.setHeaderContent(BinaryQueuePtr(&data, [=] (BinaryQueue *) {})); + + ProtocolFrameSignature signature; + ProtocolDeserialization::deserialize(frameHeader, frameHeader.m_signature.length(), + signature); + if (ProtocolFrameHeader::m_signature != signature) { + throw InvalidProtocolException(InvalidProtocolException::InvalidSignature); + } + + ProtocolDeserialization::deserialize(frameHeader, frameHeader.m_frameLength); + ProtocolDeserialization::deserialize(frameHeader, frameHeader.m_sequenceNumber); + + frameHeader.setHeaderComplete(); + } + + if (data.size() >= (frameHeader.frameLength() - ProtocolFrameHeader::frameHeaderLength())) { + frameHeader.setBodyComplete(); + } +} + +ProtocolFramePtr ProtocolFrameSerializer::startSerialization(ProtocolFrameSequenceNumber sequenceNumber) { + BinaryQueuePtr headerQueue = std::make_shared(); + BinaryQueuePtr bodyQueue = std::make_shared(); + ProtocolFrameHeaderPtr header = std::make_shared(headerQueue); + header->setSequenceNumber(sequenceNumber); + header->increaseFrameLength(ProtocolFrameHeader::frameHeaderLength()); + return std::make_shared(header, bodyQueue); +} + +void ProtocolFrameSerializer::finishSerialization(ProtocolFramePtr frame, BinaryQueue &data) { + ProtocolFrameHeader &frameHeader = *frame->frameHeader(); + ProtocolSerialization::serializeNoSize(frameHeader, ProtocolFrameHeader::m_signature); + ProtocolSerialization::serialize(frameHeader, frameHeader.m_frameLength); + ProtocolSerialization::serialize(frameHeader, frameHeader.m_sequenceNumber); + + data.appendMoveFrom(frameHeader.headerContent()); + data.appendMoveFrom(frame->bodyContent()); +} + +} /* namespace Cynara */ diff --git a/src/common/protocol/ProtocolFrameSerializer.h b/src/common/protocol/ProtocolFrameSerializer.h new file mode 100644 index 0000000..8af23ba --- /dev/null +++ b/src/common/protocol/ProtocolFrameSerializer.h @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved + * + * Contact: Lukasz Wojciechowski + * + * 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 ProtocolFrameSerializer.h + * @author Adam Malinowski + * @version 1.0 + * @brief Header file for protocol frame (de)serialization. + */ + +#ifndef SRC_COMMON_PROTOCOL_PROTOCOLFRAMESERIALIZER_H_ +#define SRC_COMMON_PROTOCOL_PROTOCOLFRAMESERIALIZER_H_ + +#include +#include + +namespace Cynara { + +class ProtocolFrameSerializer { + +public: + static void deserializeHeader(ProtocolFrameHeader &frameHeader, BinaryQueue &data); + static ProtocolFramePtr startSerialization(ProtocolFrameSequenceNumber sequenceNumber); + static void finishSerialization(ProtocolFramePtr frame, BinaryQueue &data); +}; + +} /* namespace Cynara */ + +#endif /* SRC_COMMON_PROTOCOL_PROTOCOLFRAMESERIALIZER_H_ */ diff --git a/src/common/protocol/ProtocolOpCode.h b/src/common/protocol/ProtocolOpCode.h new file mode 100644 index 0000000..d797fee --- /dev/null +++ b/src/common/protocol/ProtocolOpCode.h @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved + * + * Contact: Lukasz Wojciechowski + * + * 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 ProtocolOpCode.h + * @author Adam Malinowski + * @version 1.0 + * @brief Decalaration of protocol frame operation codes. + */ + +#ifndef SRC_COMMON_TYPES_PROTOCOLOPCODE_H_ +#define SRC_COMMON_TYPES_PROTOCOLOPCODE_H_ + +#include + +namespace Cynara { + +enum ProtocolOpCode : uint8_t { + /** Client operations */ + OpCheckPolicy = 0, + + /** Opcodes 1 - 19 are reserved for future use */ + + /** Admin operations */ + OpInsertPolicy = 20, + OpDeletePolicy, + OpListPolicies, + OpBeginTransaction, + OpEndTransaction +}; + +} /* namespace Cynara */ + +#endif /* SRC_COMMON_TYPES_PROTOCOLOPCODE_H_ */ diff --git a/src/common/protocol/ProtocolSerialization.h b/src/common/protocol/ProtocolSerialization.h index ffa22a0..532f78b 100644 --- a/src/common/protocol/ProtocolSerialization.h +++ b/src/common/protocol/ProtocolSerialization.h @@ -16,17 +16,21 @@ /** * @file ProtocolSerialization.h * @author Tomasz Swierczek (t.swierczek@samsung.com) + * @author Adam Malinowski (a.malinowsk2@samsung.com) * @version 1.0 * @brief Interfaces and templates used for data serialization. */ #ifndef SRC_COMMON_PROTOCOL_PROTOCOLSERIALIZATION_H_ #define SRC_COMMON_PROTOCOL_PROTOCOLSERIALIZATION_H_ -#include -#include +#include #include #include #include +#include +#include + +#include namespace Cynara { // Abstract data stream buffer @@ -72,20 +76,44 @@ struct ProtocolSerialization { stream.write(sizeof(*value), value); } - // unsigned int - static void serialize(IStream &stream, const unsigned value) { - stream.write(sizeof(value), &value); + // unsigned 16-bit int + static void serialize(IStream &stream, const uint16_t value) { + uint16_t _value = htole16(value); + stream.write(sizeof(value), &_value); } - static void serialize(IStream &stream, const unsigned* const value) { - stream.write(sizeof(*value), value); + static void serialize(IStream &stream, const uint16_t * const value) { + uint16_t _value = htole16(*value); + stream.write(sizeof(*value), &_value); } - // int - static void serialize(IStream &stream, const int value) { - stream.write(sizeof(value), &value); + // 16-bit int + static void serialize(IStream &stream, const int16_t value) { + int16_t _value = htole16(value); + stream.write(sizeof(value), &_value); } - static void serialize(IStream &stream, const int * const value) { - stream.write(sizeof(*value), value); + static void serialize(IStream &stream, const int16_t * const value) { + int16_t _value = htole16(*value); + stream.write(sizeof(*value), &_value); + } + + // unsigned 32-bit int + static void serialize(IStream &stream, const uint32_t value) { + uint32_t _value = htole32(value); + stream.write(sizeof(value), &_value); + } + static void serialize(IStream &stream, const uint32_t * const value) { + uint32_t _value = htole32(*value); + stream.write(sizeof(*value), &_value); + } + + // 32-bit int + static void serialize(IStream &stream, const int32_t value) { + int32_t _value = htole32(value); + stream.write(sizeof(value), &_value); + } + static void serialize(IStream &stream, const int32_t * const value) { + int32_t _value = htole32(*value); + stream.write(sizeof(*value), &_value); } // bool @@ -104,6 +132,14 @@ struct ProtocolSerialization { stream.write(sizeof(*value), value); } + // ProtocolOpCode + static void serialize(IStream &stream, const ProtocolOpCode value) { + stream.write(sizeof(value), &value); + } + static void serialize(IStream &stream, const ProtocolOpCode * const value) { + stream.write(sizeof(*value), value); + } + // std::string static void serialize(IStream &stream, const std::string &str) { int length = str.size(); @@ -115,6 +151,14 @@ struct ProtocolSerialization { stream.write(sizeof(length), &length); stream.write(length, str->c_str()); } + static void serializeNoSize(IStream &stream, const std::string &str) { + int length = str.size(); + stream.write(length, str.c_str()); + } + static void serializeNoSize(IStream &stream, const std::string * const str) { + int length = str->size(); + stream.write(length, str->c_str()); + } // STL templates @@ -213,22 +257,48 @@ struct ProtocolDeserialization { stream.read(sizeof(*value), value); } - // unsigned int - static void deserialize(IStream &stream, unsigned &value) { + // 16-bit int + static void deserialize(IStream &stream, int16_t &value) { stream.read(sizeof(value), &value); + value = le16toh(value); } - static void deserialize(IStream &stream, unsigned *&value) { - value = new unsigned; + static void deserialize(IStream &stream, int16_t *&value) { + value = new int16_t; stream.read(sizeof(*value), value); + value = le16toh(value); } - // int - static void deserialize(IStream &stream, int &value) { + // unsigned 16-bit int + static void deserialize(IStream &stream, uint16_t &value) { stream.read(sizeof(value), &value); + value = le16toh(value); } - static void deserialize(IStream &stream, int *&value) { - value = new int; + static void deserialize(IStream &stream, uint16_t *&value) { + value = new uint16_t; stream.read(sizeof(*value), value); + value = le16toh(value); + } + + // 32-bit int + static void deserialize(IStream &stream, int32_t &value) { + stream.read(sizeof(value), &value); + value = le32toh(value); + } + static void deserialize(IStream &stream, int32_t *&value) { + value = new int32_t; + stream.read(sizeof(*value), value); + value = le32toh(value); + } + + // unsigned 32-bit int + static void deserialize(IStream &stream, uint32_t &value) { + stream.read(sizeof(value), &value); + value = le32toh(value); + } + static void deserialize(IStream &stream, uint32_t *&value) { + value = new uint32_t; + stream.read(sizeof(*value), value); + value = le32toh(value); } // bool @@ -249,24 +319,35 @@ struct ProtocolDeserialization { stream.read(sizeof(*value), value); } + // PrtocolOpCode + static void deserialize(IStream &stream, ProtocolOpCode &value) { + stream.read(sizeof(value), &value); + } + static void deserialize(IStream &stream, ProtocolOpCode *&value) { + value = new ProtocolOpCode; + stream.read(sizeof(*value), value); + } + // std::string static void deserialize(IStream &stream, std::string &str) { int length; stream.read(sizeof(length), &length); - char *buf = new char[length + 1]; - stream.read(length, buf); - buf[length] = 0; - str = std::string(buf); - delete[] buf; + str.resize(length); + stream.read(length, &str[0]); } static void deserialize(IStream &stream, std::string *&str) { int length; stream.read(sizeof(length), &length); - char *buf = new char[length + 1]; - stream.read(length, buf); - buf[length] = 0; - str = new std::string(buf); - delete[] buf; + str = new std::string(length, '\0'); + stream.read(length, &str[0]); + } + static void deserialize(IStream &stream, int length, std::string &str) { + str.resize(length); + stream.read(length, &str[0]); + } + static void deserialize(IStream &stream, int length, std::string *&str) { + str = new std::string(length, '\0'); + stream.read(length, &str[0]); } // STL templates diff --git a/src/common/request/CheckRequest.h b/src/common/request/CheckRequest.h index cc4e4f2..03135c7 100644 --- a/src/common/request/CheckRequest.h +++ b/src/common/request/CheckRequest.h @@ -36,7 +36,8 @@ private: PolicyKey m_key; public: - CheckRequest(const PolicyKey &key) : m_key(key) { + CheckRequest(const PolicyKey &key, ProtocolFrameSequenceNumber sequenceNumber) : + Request(sequenceNumber), m_key(key) { } virtual ~CheckRequest() = default; diff --git a/src/common/request/Request.h b/src/common/request/Request.h index 5dc0ac9..ac92398 100644 --- a/src/common/request/Request.h +++ b/src/common/request/Request.h @@ -25,16 +25,25 @@ #include #include +#include namespace Cynara { class Request { public: - Request() = default; + Request(ProtocolFrameSequenceNumber sequenceNumber) : m_sequenceNumber(sequenceNumber) { + } virtual ~Request() = default; virtual void execute(RequestPtr self, RequestTakerPtr taker, - RequestContextPtr context) const = 0; + RequestContextPtr context) const = 0; + + ProtocolFrameSequenceNumber sequenceNumber(void) const { + return m_sequenceNumber; + } + +private: + ProtocolFrameSequenceNumber m_sequenceNumber; }; } // namespace Cynara diff --git a/src/common/request/RequestTaker.cpp b/src/common/request/RequestTaker.cpp index 13d2ee9..1725049 100644 --- a/src/common/request/RequestTaker.cpp +++ b/src/common/request/RequestTaker.cpp @@ -16,12 +16,15 @@ /* * @file RequestTaker.cpp * @author Lukasz Wojciechowski + * @author Adam Malinowski * @version 1.0 * @brief This file implements RequestTaker class */ #include #include +#include +#include #include "RequestTaker.h" diff --git a/src/common/types/ProtocolFields.h b/src/common/types/ProtocolFields.h new file mode 100644 index 0000000..e370ba4 --- /dev/null +++ b/src/common/types/ProtocolFields.h @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved + * + * Contact: Lukasz Wojciechowski + * + * 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 ProtocolFields.h + * @author Adam Malinowski + * @version 1.0 + * @brief Types definition for protocol frame fields. + */ + +#ifndef SRC_COMMON_TYPES_PROTOCOLFIELDS_H_ +#define SRC_COMMON_TYPES_PROTOCOLFIELDS_H_ + +#include +#include + +namespace Cynara { + +typedef std::string ProtocolFrameSignature; +typedef uint32_t ProtocolFrameLength; +typedef uint16_t ProtocolFrameSequenceNumber; + +} /* namespace Cynara */ + +#endif /* SRC_COMMON_TYPES_PROTOCOLFIELDS_H_ */ -- 2.7.4 From 9f23bd041799ecb0f9c9132d1f46adeebd341e05 Mon Sep 17 00:00:00 2001 From: Adam Malinowski Date: Wed, 2 Jul 2014 15:51:35 +0200 Subject: [PATCH 13/16] Remove unused PolicyVector.h file Change-Id: Ic4c43d1e1afbd1c506a47cefc8cb78c47c92170d --- src/common/types/PolicyVector.h | 31 ------------------------------- 1 file changed, 31 deletions(-) delete mode 100644 src/common/types/PolicyVector.h diff --git a/src/common/types/PolicyVector.h b/src/common/types/PolicyVector.h deleted file mode 100644 index b23c617..0000000 --- a/src/common/types/PolicyVector.h +++ /dev/null @@ -1,31 +0,0 @@ -/* - * 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 PolicyVector.h - * @author Lukasz Wojciechowski - * @version 1.0 - * @brief This file defines vector of policies - */ - -#ifndef CYNARA_COMMON_TYPES_POLICYVECTOR_H -#define CYNARA_COMMON_TYPES_POLICYVECTOR_H - -#include -#include "Policy.h" - -typedef std::vector PolicyVector; - -#endif /* CYNARA_COMMON_TYPES_POLICYVECTOR_H */ -- 2.7.4 From 843d880197bd8e82d4adab19a8f5b0b30ce11ceb Mon Sep 17 00:00:00 2001 From: Rafal Krypa Date: Thu, 3 Jul 2014 14:14:53 +0200 Subject: [PATCH 14/16] Remove usage of unwanted binutils library Change-Id: I616494d5280b87a6059149a8005911e4f7062fa3 --- packaging/cynara.spec | 2 - src/CMakeLists.txt | 1 - src/common/CMakeLists.txt | 3 -- src/common/log/Backtrace.cpp | 97 ++++++-------------------------------------- src/common/log/Backtrace.h | 15 +------ 5 files changed, 14 insertions(+), 104 deletions(-) diff --git a/packaging/cynara.spec b/packaging/cynara.spec index 1d7b8c8..4b68a35 100644 --- a/packaging/cynara.spec +++ b/packaging/cynara.spec @@ -22,8 +22,6 @@ BuildRequires: pkgconfig(libsystemd-journal) %if %{?build_type} == "DEBUG" BuildRequires: pkgconfig(libunwind) -BuildRequires: pkgconfig(zlib) -BuildRequires: binutils-devel %endif diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 6b908c3..852e0a5 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -25,7 +25,6 @@ IF (CMAKE_BUILD_TYPE MATCHES "DEBUG") SET(COMMON_DEPS ${COMMON_DEPS} libunwind - zlib ) ENDIF (CMAKE_BUILD_TYPE MATCHES "DEBUG") diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index 5e6308a..4962b78 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -60,9 +60,6 @@ SET_TARGET_PROPERTIES( IF (CMAKE_BUILD_TYPE MATCHES "DEBUG") SET(CYNARA_DBG_LIBRARIES ${CYNARA_DEP_LIBRARIES} - -lbfd - -liberty - -ldl ) ENDIF (CMAKE_BUILD_TYPE MATCHES "DEBUG") diff --git a/src/common/log/Backtrace.cpp b/src/common/log/Backtrace.cpp index a58c0ed..26e3e02 100644 --- a/src/common/log/Backtrace.cpp +++ b/src/common/log/Backtrace.cpp @@ -22,103 +22,36 @@ * @brief Implementation of backtrace utility class. */ -#include "Backtrace.h" - #include -#include #include #include #include +#include +#include + +#include "Backtrace.h" + namespace Cynara { Backtrace &Backtrace::getInstance(void) { - static Backtrace m_instance(NULL); + static Backtrace m_instance; return m_instance; } -Backtrace::Backtrace(bfd *abfd) : - m_bfd(abfd), m_found(false), m_pc(0), m_fileName(NULL), +Backtrace::Backtrace() : + m_fileName(NULL), m_functionName(NULL), m_lineNumber(0) { } Backtrace::~Backtrace() { - if (m_bfd) { - bfd_close(m_bfd); - LOGD("Binary file closed."); - } -} - -bool Backtrace::init(void) { - char exePath[BUFSIZ]; - readlink("/proc/self/exe", exePath, BUFSIZ); - - bfd_init(); - m_bfd = bfd_openr(exePath, NULL); - if (m_bfd) { - m_bfd->flags |= BFD_DECOMPRESS; - if (bfd_check_format_matches(m_bfd, bfd_object, 0)) { - return true; - } - bfd_close(m_bfd); - m_bfd = NULL; - LOGE("Binary file check format failed."); - } else { - LOGE("Failed to open file: %s", exePath); - } - - return false; -} - -void Backtrace::findAddressInSection(bfd *abfd, asection *section, void *data) { - bfd_vma vma; - bfd_size_type size; - - Backtrace *backtrace = static_cast(data); - - if (backtrace->m_found) { - return; - } - - if ((bfd_get_section_flags (abfd, section) & SEC_ALLOC) == 0) { - return; - } - - vma = bfd_get_section_vma(abfd, section); - if (backtrace->m_pc < vma) { - return; - } - - size = bfd_get_section_size(section); - if (backtrace->m_pc >= vma + size) { - return; - } - - backtrace->m_found = bfd_find_nearest_line(abfd, section, NULL, - backtrace->m_pc - vma, &backtrace->m_fileName, - &backtrace->m_functionName, &backtrace->m_lineNumber); } -void Backtrace::getSourceInfo(unw_word_t proc_address) { - char addr[64]; - - sprintf(addr, "0x%lx", static_cast(proc_address)); - m_pc = bfd_scan_vma(addr, NULL, 16); - m_found = false; - bfd_map_over_sections(m_bfd, findAddressInSection, this); - - if (m_found) { - while (true) { - m_found = bfd_find_inliner_info(m_bfd, &m_fileName, &m_functionName, - &m_lineNumber); - if (!m_found) - break; - } - } else { - m_fileName = "??"; - m_functionName = "??"; - m_lineNumber = 0; - } +void Backtrace::getSourceInfo(unw_word_t proc_address UNUSED) { + // TODO: extract filename and line number for symbol at given address + m_fileName = "??"; + m_functionName = "??"; + m_lineNumber = 0; } const std::string Backtrace::buildBacktrace(void) { @@ -132,10 +65,6 @@ const std::string Backtrace::buildBacktrace(void) { char *realname; int status; - if (m_bfd == NULL) { - init(); - } - unw_getcontext(&uc); // get rid of previous function: Backtrace::getBacktrace unw_init_local(&cursor, &uc); diff --git a/src/common/log/Backtrace.h b/src/common/log/Backtrace.h index 568c921..b697195 100644 --- a/src/common/log/Backtrace.h +++ b/src/common/log/Backtrace.h @@ -25,14 +25,6 @@ #ifndef SRC_COMMON_LOG_BACKTRACE_H_ #define SRC_COMMON_LOG_BACKTRACE_H_ -// Bellow two defines are needed by /usr/include/bfd.h file. -// In fact it needs config.h generated by autotools -// but we dont use autotools to build cynara so we don't have config.h -// bfd.h checks for these defines -#define PACKAGE 1 -#define PACKAGE_VERSION 1 - -#include #define UNW_LOCAL_ONLY #include #include @@ -52,21 +44,16 @@ public: private: static Backtrace &getInstance(void); - Backtrace(bfd *abfd); + Backtrace(); Backtrace(Backtrace const &) = delete; ~Backtrace(); void operator=(Backtrace const &) = delete; - bool init(void); const std::string buildBacktrace(void); - static void findAddressInSection(bfd *abfd, asection *section, void *data); void getSourceInfo(unw_word_t proc_address); private: - bfd *m_bfd; - bool m_found; - bfd_vma m_pc; const char *m_fileName; const char *m_functionName; unsigned int m_lineNumber; -- 2.7.4 From f5df6f9a745eaf01f8a545855eee602423711c7f Mon Sep 17 00:00:00 2001 From: Adam Malinowski Date: Thu, 3 Jul 2014 11:26:05 +0200 Subject: [PATCH 15/16] Add response deserialization Change-Id: Id3bb97e5cc590aa9ef3757b503d423e2c3c9a99f --- src/common/protocol/ProtocolClient.cpp | 31 +++++++++++++++++++++++++++++-- src/common/protocol/ProtocolClient.h | 2 ++ src/common/response/CheckResponse.h | 3 ++- src/common/response/Response.h | 11 ++++++++++- src/service/logic/Logic.cpp | 2 +- 5 files changed, 44 insertions(+), 5 deletions(-) diff --git a/src/common/protocol/ProtocolClient.cpp b/src/common/protocol/ProtocolClient.cpp index 2671eb7..9369b54 100644 --- a/src/common/protocol/ProtocolClient.cpp +++ b/src/common/protocol/ProtocolClient.cpp @@ -49,9 +49,11 @@ ProtocolPtr ProtocolClient::clone(void) { RequestPtr ProtocolClient::deserializeCheckRequest(ProtocolFrameHeader &frame) { std::string clientId, userId, privilegeId; + ProtocolDeserialization::deserialize(frame, clientId); ProtocolDeserialization::deserialize(frame, userId); ProtocolDeserialization::deserialize(frame, privilegeId); + return std::make_shared(PolicyKey(clientId, userId, privilegeId), frame.sequenceNumber()); } @@ -76,9 +78,34 @@ RequestPtr ProtocolClient::extractRequestFromBuffer(BinaryQueue &bufferQueue) { return nullptr; } +ResponsePtr ProtocolClient::deserializeCheckResponse(ProtocolFrameHeader &frame) { + PolicyType result; + PolicyResult::PolicyMetadata additionalInfo; + + ProtocolDeserialization::deserialize(frame, result); + ProtocolDeserialization::deserialize(frame, additionalInfo); + + return std::make_shared(PolicyResult(result, additionalInfo), frame.sequenceNumber()); +} + ResponsePtr ProtocolClient::extractResponseFromBuffer(BinaryQueue &bufferQueue) { - TODO_USE_ME(bufferQueue); - return ResponsePtr(nullptr); + ProtocolFrameSerializer::deserializeHeader(m_frameHeader, bufferQueue); + + if (m_frameHeader.isFrameComplete()) { + ProtocolOpCode requestId; + + m_frameHeader.resetState(); + ProtocolDeserialization::deserialize(m_frameHeader, requestId); + switch (requestId) { + case OpCheckPolicy: + return deserializeCheckResponse(m_frameHeader); + default: + throw InvalidProtocolException(InvalidProtocolException::WrongOpCode); + break; + } + } + + return nullptr; } void ProtocolClient::execute(RequestContextPtr context, CheckRequestPtr request) { diff --git a/src/common/protocol/ProtocolClient.h b/src/common/protocol/ProtocolClient.h index 1cd608b..5f20547 100644 --- a/src/common/protocol/ProtocolClient.h +++ b/src/common/protocol/ProtocolClient.h @@ -25,6 +25,7 @@ #include #include +#include #include "Protocol.h" @@ -44,6 +45,7 @@ public: private: RequestPtr deserializeCheckRequest(ProtocolFrameHeader &frame); + ResponsePtr deserializeCheckResponse(ProtocolFrameHeader &frame); }; } // namespace Cynara diff --git a/src/common/response/CheckResponse.h b/src/common/response/CheckResponse.h index 0cb1182..7d3ebc4 100644 --- a/src/common/response/CheckResponse.h +++ b/src/common/response/CheckResponse.h @@ -35,7 +35,8 @@ class CheckResponse : public Response { public: const PolicyResult &m_resultRef; - CheckResponse(const PolicyResult &result) : m_resultRef(result) { + CheckResponse(const PolicyResult &result, ProtocolFrameSequenceNumber sequenceNumber) : + Response(sequenceNumber), m_resultRef(result) { } virtual ~CheckResponse() = default; diff --git a/src/common/response/Response.h b/src/common/response/Response.h index 31d66bc..e85de48 100644 --- a/src/common/response/Response.h +++ b/src/common/response/Response.h @@ -25,16 +25,25 @@ #include #include +#include namespace Cynara { class Response { public: - Response() = default; + Response(ProtocolFrameSequenceNumber sequenceNumber) : m_sequenceNumber(sequenceNumber) { + }; virtual ~Response() = default; virtual void execute(ResponsePtr self, ResponseTakerPtr taker, RequestContextPtr context) const = 0; + + ProtocolFrameSequenceNumber sequenceNumber(void) const { + return m_sequenceNumber; + } + +private: + ProtocolFrameSequenceNumber m_sequenceNumber; }; } // namespace Cynara diff --git a/src/service/logic/Logic.cpp b/src/service/logic/Logic.cpp index 98d7d05..a4de76b 100644 --- a/src/service/logic/Logic.cpp +++ b/src/service/logic/Logic.cpp @@ -42,7 +42,7 @@ Logic::~Logic() { void Logic::execute(RequestContextPtr context, CheckRequestPtr request) { PolicyResult result(PredefinedPolicyType::DENY); if (check(context, request->key(), result)) { - context->returnResponse(context, std::make_shared(result)); + context->returnResponse(context, std::make_shared(result, request->sequenceNumber())); } } -- 2.7.4 From 75bf81a46330d30007287ecfed69dea10fdbe06e Mon Sep 17 00:00:00 2001 From: Lukasz Wojciechowski Date: Fri, 4 Jul 2014 14:38:22 +0200 Subject: [PATCH 16/16] Update libcynara-admin API header Change const strings from const char* to defines. Add missing struct keywords. In function cynara_admin_set_policies() change polices parameter to array of pointers. In function cynara_admin_set_bucket() add extra parameter for passing additional data to bucket's default policy. Change-Id: Id1370a202a39636572aa6a203ae29662e423c986 --- src/include/cynara-admin.h | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/include/cynara-admin.h b/src/include/cynara-admin.h index 23325a9..344ecbc 100644 --- a/src/include/cynara-admin.h +++ b/src/include/cynara-admin.h @@ -49,10 +49,10 @@ extern "C" { #endif //todo comment -const char *CYNARA_ADMIN_WILDCARD = "*"; +#define CYNARA_ADMIN_WILDCARD "*"; //todo comment -const char *CYNARA_ADMIN_DEFAULT_BUCKET = ""; +#define CYNARA_ADMIN_DEFAULT_BUCKET ""; //todo comments #define CYNARA_ADMIN_DELETE -1 @@ -131,7 +131,7 @@ int cynara_admin_initialize(struct cynara_admin **pp_cynara_admin); * * \brief Release cynara-admin library. */ -int cynara_admin_finish(cynara_admin *p_cynara_admin); +int cynara_admin_finish(struct cynara_admin *p_cynara_admin); /** * \par Description: @@ -177,13 +177,14 @@ int cynara_admin_finish(cynara_admin *p_cynara_admin); * going to be fixed along with introduction of transactions in future releases. * * \param[in] p_cynara_admin cynara admin structure. - * \param[in] policies NULL terminated array of policy structures + * \param[in] policies NULL terminated array of pointers to policy structures. * * \return CYNARA_ADMIN_API_SUCCESS on success, or error code otherwise. * * \brief Insert, update or delete policies in cynara database. */ -int cynara_admin_set_policies(cynara_admin *p_cynara_admin, const cynara_admin_policy *policies); +int cynara_admin_set_policies(struct cynara_admin *p_cynara_admin, + const cynara_admin_policy *const *policies); /** * \par Description: @@ -217,15 +218,20 @@ int cynara_admin_set_policies(cynara_admin *p_cynara_admin, const cynara_admin_p * Default bucket identified with CYNARA_ADMIN_DEFAULT_BUCKET exists always. Its default policy * is preset to DENY (can be altered, however). Default bucket cannot be removed. * + * Extra parameter will be used to pass additional data to cynara extensions to build more complex + * policies, such as ALLOW but for 5 minutes only, or ALLOW if user confirms. + * * \param[in] p_cynara_admin cynara admin structure. * \param[in] bucket bucket name * \param[in] operation type of operation (default policy or CYNARA_ADMIN_DELETE) + * \param[in] extra additional data for default policy (will be available with cynara extensions) * * \return CYNARA_ADMIN_API_SUCCESS on success, or error code otherwise. * * \brief Add, remove or update buckets in cynara database. */ -int cynara_admin_set_bucket(cynara_admin *p_cynara_admin, const char *bucket, int operation); +int cynara_admin_set_bucket(struct cynara_admin *p_cynara_admin, const char *bucket, int operation, + const char *extra); #ifdef __cplusplus } -- 2.7.4