From 2e27fc1b022111eb56a161e7a562c6e98a744cb8 Mon Sep 17 00:00:00 2001 From: Lukasz Wojciechowski Date: Mon, 7 Jul 2014 16:09:50 +0200 Subject: [PATCH] Add cache interface and NoCache stub Add interface defining API for cache, that will be used to speed up checks. Add NoCache class, that implements this interface with very simple version, which always enforces checks on Cynara server. Add usage of NoCache in Logic class. Change-Id: I3b799c4c6eccddfed98e8130ef1ca4cb849b8c05 --- src/client/CMakeLists.txt | 1 + src/client/cache/CacheInterface.h | 52 ++++++++++++++++++++++++++++++++++++ src/client/cache/NoCache.cpp | 39 +++++++++++++++++++++++++++ src/client/cache/NoCache.h | 56 +++++++++++++++++++++++++++++++++++++++ src/client/logic/Logic.cpp | 20 +++++--------- src/client/logic/Logic.h | 2 ++ 6 files changed, 157 insertions(+), 13 deletions(-) create mode 100644 src/client/cache/CacheInterface.h create mode 100644 src/client/cache/NoCache.cpp create mode 100644 src/client/cache/NoCache.h diff --git a/src/client/CMakeLists.txt b/src/client/CMakeLists.txt index 89fe001..72db5f1 100644 --- a/src/client/CMakeLists.txt +++ b/src/client/CMakeLists.txt @@ -23,6 +23,7 @@ SET(CYNARA_LIB_CYNARA_PATH ${CYNARA_PATH}/client) SET(LIB_CYNARA_SOURCES ${CYNARA_LIB_CYNARA_PATH}/api/client-api.cpp + ${CYNARA_LIB_CYNARA_PATH}/cache/NoCache.cpp ${CYNARA_LIB_CYNARA_PATH}/logic/Logic.cpp ) diff --git a/src/client/cache/CacheInterface.h b/src/client/cache/CacheInterface.h new file mode 100644 index 0000000..acfbd7e --- /dev/null +++ b/src/client/cache/CacheInterface.h @@ -0,0 +1,52 @@ +/* + * 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 CacheInterface.h + * @author Lukasz Wojciechowski + * @version 1.0 + * @brief This file contains cache interface definition. + */ + +#ifndef SRC_CLIENT_CACHE_CACHEINTERFACE_H_ +#define SRC_CLIENT_CACHE_CACHEINTERFACE_H_ + +#include +#include + +#include +#include + +#include + +namespace Cynara { + +class CacheInterface; +typedef std::shared_ptr CacheInterfacePtr; + +class CacheInterface { +public: + CacheInterface() = default; + virtual ~CacheInterface() = default; + + virtual cynara_api_result check(const std::string &session, const PolicyKey &key) = 0; + virtual cynara_api_result updateAndCheck(const std::string &session, const PolicyKey &key, + const PolicyResult &result) = 0; + virtual void clear(void) = 0; +}; + +} // namespace Cynara + +#endif /* SRC_CLIENT_CACHE_CACHEINTERFACE_H_ */ diff --git a/src/client/cache/NoCache.cpp b/src/client/cache/NoCache.cpp new file mode 100644 index 0000000..c9220e3 --- /dev/null +++ b/src/client/cache/NoCache.cpp @@ -0,0 +1,39 @@ +/* + * 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 NoCache.cpp + * @author Lukasz Wojciechowski + * @version 1.0 + * @brief This file contains implementation of NoCache class - stub for no-cache version + */ + +#include +#include + +#include "NoCache.h" + +namespace Cynara { + +cynara_api_result NoCache::updateAndCheck(const std::string &session UNUSED, + const PolicyKey &key UNUSED, + const PolicyResult &result) { + if (result.policyType() == PredefinedPolicyType::ALLOW) + return cynara_api_result::CYNARA_API_SUCCESS; + else + return cynara_api_result::CYNARA_API_ACCESS_DENIED; +} + +} // namespace Cynara diff --git a/src/client/cache/NoCache.h b/src/client/cache/NoCache.h new file mode 100644 index 0000000..c4330b1 --- /dev/null +++ b/src/client/cache/NoCache.h @@ -0,0 +1,56 @@ +/* + * 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 NoCache.h + * @author Lukasz Wojciechowski + * @version 1.0 + * @brief This file contains definition of NoCache class - stub for no-cache version + */ + +#ifndef SRC_CLIENT_CACHE_NOCACHE_H_ +#define SRC_CLIENT_CACHE_NOCACHE_H_ + +#include + +#include +#include +#include + +#include +#include + +namespace Cynara { + +class NoCache : public CacheInterface { +public: + NoCache() = default; + virtual ~NoCache() = default; + + virtual cynara_api_result check(const std::string &session UNUSED, + const PolicyKey &key UNUSED) { + return cynara_api_result::CYNARA_API_SERVICE_NOT_AVAILABLE; + } + + virtual cynara_api_result updateAndCheck(const std::string &session, const PolicyKey &key, + const PolicyResult &result); + + virtual void clear(void) { + } +}; + +} // namespace Cynara + +#endif /* SRC_CLIENT_CACHE_NOCACHE_H_ */ diff --git a/src/client/logic/Logic.cpp b/src/client/logic/Logic.cpp index 3479597..6ac6fb9 100644 --- a/src/client/logic/Logic.cpp +++ b/src/client/logic/Logic.cpp @@ -36,6 +36,7 @@ #include #include +#include #include "Logic.h" namespace Cynara { @@ -45,6 +46,7 @@ const std::string clientSocketPath("/run/cynara/cynara.socket"); Logic::Logic() { m_socketClient = std::make_shared(clientSocketPath, std::make_shared()); + m_cache = std::make_shared(); } ProtocolFrameSequenceNumber generateSequenceNumber(void) { @@ -57,8 +59,9 @@ cynara_api_result Logic::check(const std::string &client, const std::string &ses { PolicyKey key(client, user, privilege); - //todo Check if answer can be get from cache. Update cache. - //todo m_cache->check(session, key); + auto cacheResponse = m_cache->check(session, key); + if(cacheResponse != cynara_api_result::CYNARA_API_SERVICE_NOT_AVAILABLE) + return cacheResponse; ProtocolFrameSequenceNumber sequenceNumber = generateSequenceNumber(); @@ -86,20 +89,11 @@ cynara_api_result Logic::check(const std::string &client, const std::string &ses return cynara_api_result::CYNARA_API_ACCESS_DENIED; } - PolicyResult result = checkResponse->m_resultRef; - //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; + return m_cache->updateAndCheck(session, key, checkResponse->m_resultRef); } void Logic::onDisconnected(void) { - //todo run special actions when disconnected from cynara service - // like cleaning cache + m_cache->clear(); } } // namespace Cynara diff --git a/src/client/logic/Logic.h b/src/client/logic/Logic.h index e16a451..06950d8 100644 --- a/src/client/logic/Logic.h +++ b/src/client/logic/Logic.h @@ -28,12 +28,14 @@ #include #include +#include namespace Cynara { class Logic : public ApiInterface { private: SocketClientPtr m_socketClient; + CacheInterfacePtr m_cache; void onDisconnected(void); -- 2.7.4