Add base class for external plugins 99/28899/9
authorZofia Abramowska <z.abramowska@samsung.com>
Thu, 16 Oct 2014 14:53:14 +0000 (16:53 +0200)
committerZofia Abramowska <z.abramowska@samsung.com>
Fri, 14 Nov 2014 18:33:46 +0000 (19:33 +0100)
* Make ExternalPluginInterface a base class providing only pure
virtual getSupportedPolicyTypes() and publish its header
* Change ExternalPluginInterface definition to ServicePluginInterface
class and make it inherit after ExternalPluginInterface and publish
its header as cynara-client-plugin.h
* Rename InterpreterInterface to ClientServiceInterface, make it
inherit after ExternalPluginInterface

Change-Id: Ia572e2adb8a4486705f89903b31433d70d733381

17 files changed:
packaging/cynara.spec
src/client-async/logic/Logic.cpp
src/client-common/CMakeLists.txt
src/client-common/cache/CacheInterface.h
src/client-common/cache/CapacityCache.cpp
src/client-common/plugins/NaiveInterpreter.cpp [new file with mode: 0644]
src/client-common/plugins/NaiveInterpreter.h
src/client-common/plugins/PluginInterface.h [deleted file]
src/client/logic/Logic.cpp
src/common/CMakeLists.txt
src/common/plugin/ExternalPluginInterface.h [new file with mode: 0644]
src/include/CMakeLists.txt
src/include/cynara-client-plugin.h [new file with mode: 0644]
src/include/cynara-plugin.h
src/service/logic/Logic.cpp
src/service/plugin/PluginManager.cpp
src/service/plugin/PluginManager.h

index 68d52ce..743f2a7 100644 (file)
@@ -482,12 +482,14 @@ fi
 
 %files -n libcynara-commons-devel
 %{_includedir}/cynara/cynara-policy-types.h
+%{_includedir}/cynara/plugin/ExternalPluginInterface.h
 %{_includedir}/cynara/types/PolicyResult.h
 %{_includedir}/cynara/types/PolicyType.h
 %{_libdir}/libcynara-commons.so
 
 %files -n libcynara-plugin-devel
 %{_includedir}/cynara/cynara-plugin.h
+%{_includedir}/cynara/cynara-client-plugin.h
 %{_libdir}/pkgconfig/cynara-plugin.pc
 
 %files -n cynara-tests
index 921c2d6..bfb494c 100644 (file)
@@ -49,8 +49,9 @@ Logic::Logic(cynara_status_callback callback, void *userStatusData)
 
     m_cache = std::make_shared<CapacityCache>();
     auto naiveInterpreter = std::make_shared<NaiveInterpreter>();
-    m_cache->registerPlugin(PredefinedPolicyType::ALLOW, naiveInterpreter);
-    m_cache->registerPlugin(PredefinedPolicyType::DENY, naiveInterpreter);
+    for (auto &type : naiveInterpreter->getSupportedPolicyTypes()) {
+        m_cache->registerPlugin(type, naiveInterpreter);
+    }
 }
 
 Logic::~Logic() {
index 90e09d7..5436884 100644 (file)
@@ -28,6 +28,7 @@ INCLUDE_DIRECTORIES(
 
 SET(LIB_CYNARA_COMMON_SOURCES
     ${LIB_CYNARA_COMMON_PATH}/cache/CapacityCache.cpp
+    ${LIB_CYNARA_COMMON_PATH}/plugins/NaiveInterpreter.cpp
     )
 
 ADD_DEFINITIONS("-fvisibility=default")
index 57455b8..03b7624 100644 (file)
@@ -29,7 +29,7 @@
 #include <string>
 
 #include <cynara-client.h>
-#include <plugins/PluginInterface.h>
+#include <cynara-client-plugin.h>
 #include <types/ClientSession.h>
 #include <types/PolicyKey.h>
 #include <types/PolicyResult.h>
@@ -48,7 +48,7 @@ public:
                        const PolicyKey &key,
                        const PolicyResult &result) = 0;
 
-    void registerPlugin(const PolicyType policyType, InterpreterInterfacePtr plugin) {
+    void registerPlugin(const PolicyType policyType, ClientPluginInterfacePtr plugin) {
         m_plugins[policyType] = plugin;
     }
 
@@ -59,7 +59,7 @@ public:
     virtual ~PluginCache() {};
 
 protected:
-    std::map<PolicyType, InterpreterInterfacePtr> m_plugins;
+    std::map<PolicyType, ClientPluginInterfacePtr> m_plugins;
 };
 
 } // namespace Cynara
index d07bb2c..7eb7e34 100644 (file)
@@ -55,7 +55,7 @@ int CapacityCache::get(const ClientSession &session, const PolicyKey &key) {
         }
 
         //Is it still usable?
-        InterpreterInterfacePtr plugin = pluginIt->second;
+        ClientPluginInterfacePtr plugin = pluginIt->second;
         auto &prevSession = std::get<1>(cachedValue);
         auto usageIt = std::get<2>(cachedValue);
         bool updateSession = false;
diff --git a/src/client-common/plugins/NaiveInterpreter.cpp b/src/client-common/plugins/NaiveInterpreter.cpp
new file mode 100644 (file)
index 0000000..7141cb7
--- /dev/null
@@ -0,0 +1,30 @@
+/*
+ *  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        src/client-common/plugins/NaiveInterpreter.cpp
+ * @author      Zofia Abramowska <z.abramowska@samsung.com>
+ * @version     1.0
+ * @brief       This file contains NaiveInterpreter supported types definition.
+ */
+
+#include <plugins/NaiveInterpreter.h>
+
+namespace Cynara {
+
+    const std::vector<PolicyType> NaiveInterpreter::s_supportedTypes =
+    {PredefinedPolicyType::ALLOW, PredefinedPolicyType::DENY};
+
+}
index e9e8ba4..8099362 100644 (file)
@@ -28,7 +28,8 @@
 
 namespace Cynara {
 
-class NaiveInterpreter : public InterpreterInterface {
+class NaiveInterpreter : public ClientPluginInterface {
+public:
     bool isUsable(const ClientSession &session UNUSED, const ClientSession &prevSession UNUSED,
                   bool &updateSession UNUSED, PolicyResult &result UNUSED) {
         return true;
@@ -43,6 +44,12 @@ class NaiveInterpreter : public InterpreterInterface {
         else
             return CYNARA_API_ACCESS_DENIED;
     }
+
+    const std::vector<PolicyType> &getSupportedPolicyTypes(void) {
+        return s_supportedTypes;
+    }
+private:
+    static const std::vector<PolicyType> s_supportedTypes;
 };
 
 } // namespace Cynara
diff --git a/src/client-common/plugins/PluginInterface.h b/src/client-common/plugins/PluginInterface.h
deleted file mode 100644 (file)
index 54bd341..0000000
+++ /dev/null
@@ -1,48 +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        src/client-common/plugins/PluginInterface.h
- * @author      Zofia Abramowska <z.abramowska@samsung.com>
- * @version     1.0
- * @brief       This file contains plugin interface definitions.
- */
-
-#ifndef SRC_CLIENT_COMMON_PLUGINS_PLUGININTERFACE_H_
-#define SRC_CLIENT_COMMON_PLUGINS_PLUGININTERFACE_H_
-
-#include <memory>
-
-#include <types/ClientSession.h>
-#include <types/PolicyResult.h>
-
-namespace Cynara {
-
-class InterpreterInterface;
-typedef std::shared_ptr<InterpreterInterface> InterpreterInterfacePtr;
-
-class InterpreterInterface {
-public:
-    virtual bool isCacheable(const ClientSession &session, const PolicyResult &result) = 0;
-    virtual bool isUsable(const ClientSession &session, const ClientSession &prevSession,
-                          bool &updateSession, PolicyResult &result) = 0;
-    virtual int toResult(const ClientSession &session, PolicyResult &result) = 0;
-
-    virtual ~InterpreterInterface() {};
-};
-
-} // namespace Cynara
-
-#endif // SRC_CLIENT_COMMON_PLUGINS_PLUGININTERFACE_H_
index e05a168..498e53c 100644 (file)
@@ -52,9 +52,9 @@ Logic::Logic() {
                                               std::make_shared<ProtocolClient>());
     m_cache = std::make_shared<CapacityCache>();
     auto naiveInterpreter = std::make_shared<NaiveInterpreter>();
-    m_cache->registerPlugin(PredefinedPolicyType::ALLOW, naiveInterpreter);
-    m_cache->registerPlugin(PredefinedPolicyType::DENY, naiveInterpreter);
-    m_cache->registerPlugin(PredefinedPolicyType::BUCKET, naiveInterpreter);
+    for (auto &type : naiveInterpreter->getSupportedPolicyTypes()) {
+        m_cache->registerPlugin(type, naiveInterpreter);
+    }
 }
 
 int Logic::check(const std::string &client, const ClientSession &session, const std::string &user,
index 61debfd..9dc48de 100644 (file)
@@ -91,3 +91,7 @@ INSTALL(FILES
     ${COMMON_PATH}/types/PolicyType.h
     DESTINATION ${INCLUDE_INSTALL_DIR}/cynara/types
     )
+INSTALL(FILES
+    ${COMMON_PATH}/plugin/ExternalPluginInterface.h
+    DESTINATION ${INCLUDE_INSTALL_DIR}/cynara/plugin
+    )
diff --git a/src/common/plugin/ExternalPluginInterface.h b/src/common/plugin/ExternalPluginInterface.h
new file mode 100644 (file)
index 0000000..bf3c7fe
--- /dev/null
@@ -0,0 +1,60 @@
+/*
+ * 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        src/common/plugin/ExternalPluginInterface.h
+ * @author      Zofia Abramowska <z.abramowska@samsung.com>
+ * @version     1.0
+ * @brief       This file defines cynara side external plugin interface
+ */
+
+#ifndef SRC_COMMON_PLUGIN_EXTERNALPLUGININTERFACE_H_
+#define SRC_COMMON_PLUGIN_EXTERNALPLUGININTERFACE_H_
+
+#include <vector>
+
+#include <types/PolicyType.h>
+
+namespace Cynara {
+
+class ExternalPluginInterface;
+
+/**
+ * Type of function used for creating objects implementing ExternalPluginInterface.
+ * Inside plugin library function with create_t signature should have symbol
+ * named "create".
+ */
+typedef ExternalPluginInterface *(*create_t)(void);
+
+/**
+ * Type of function used for destroying objects created with "create".
+ * Inside plugin library function with destroy_t signature should have symbol
+ * named "destroy".
+ */
+typedef void (*destroy_t)(ExternalPluginInterface *);
+
+class ExternalPluginInterface {
+public:
+
+    /**
+     * Policy type supported by plugin.
+     */
+    virtual const std::vector<PolicyType> &getSupportedPolicyTypes(void) = 0;
+    virtual ~ExternalPluginInterface() {}
+};
+
+} // namespace Cynara
+
+#endif /* SRC_COMMON_PLUGIN_EXTERNALPLUGININTERFACE_H_ */
index 1d672a8..b43bd1f 100644 (file)
@@ -21,6 +21,7 @@ INSTALL(FILES
     ${CYNARA_PATH}/include/cynara-admin-types.h
     ${CYNARA_PATH}/include/cynara-client.h
     ${CYNARA_PATH}/include/cynara-client-async.h
+    ${CYNARA_PATH}/include/cynara-client-plugin.h
     ${CYNARA_PATH}/include/cynara-creds-commons.h
     ${CYNARA_PATH}/include/cynara-creds-dbus.h
     ${CYNARA_PATH}/include/cynara-creds-socket.h
diff --git a/src/include/cynara-client-plugin.h b/src/include/cynara-client-plugin.h
new file mode 100644 (file)
index 0000000..3d39ad7
--- /dev/null
@@ -0,0 +1,74 @@
+/*
+ *  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        src/include/cynara-client-plugin.h
+ * @author      Zofia Abramowska <z.abramowska@samsung.com>
+ * @version     1.0
+ * @brief       This file defines cynara client side of external plugin interface -
+ *              ClientPluginInterface.
+ */
+
+#ifndef CYNARACLIENTPLUGIN_H_
+#define CYNARACLIENTPLUGIN_H_
+
+#include <memory>
+
+#include <plugin/ExternalPluginInterface.h>
+#include <types/ClientSession.h>
+#include <types/PolicyResult.h>
+
+namespace Cynara {
+
+class ClientPluginInterface;
+typedef std::shared_ptr<ClientPluginInterface> ClientPluginInterfacePtr;
+
+/**
+ * A class defining external plugins interface.
+ * These plugins work inside of cynara client library. They interpret
+ * PolicyResult returned by cynara in terms of:
+ * a) cacheability - tells, whether value should be cached (for e.g. policyType like
+ *                   ALLOW_ONCE should not be cached)
+ * b) usability - whether cache entry can still be used (for e.g. policy allowing access for
+ *                given type)
+ * c) value - translates PolicyResult to CYNARA_API_ACCESS_ALLOWED or CYNARA_API_ACCESS_DENIED
+ *
+ * Plugin implementing ClientPluginInterface must implement ExternalPluginInterface.
+ * Creation/destruction functions with signatures compatible to Cynara::create_t and
+ * Cynara::destroy_t must be provided as factories of ClientPluginInterface.
+ */
+class ClientPluginInterface : public ExternalPluginInterface {
+public:
+
+    /**
+     * Return entry cacheability
+     */
+    virtual bool isCacheable(const ClientSession &session, const PolicyResult &result) = 0;
+    /**
+     * Return entry cacheability
+     */
+    virtual bool isUsable(const ClientSession &session, const ClientSession &prevSession,
+                          bool &updateSession, PolicyResult &result) = 0;
+    /**
+     * Translate PolicyResult to CYNARA_API_ACCESS_ALLOWED or CYNARA_API_ACCESS_DENIED
+     */
+    virtual int toResult(const ClientSession &session, PolicyResult &result) = 0;
+
+    virtual ~ClientPluginInterface() {};
+};
+
+}
+
+#endif // CYNARACLIENTPLUGIN_H_
index a9d6da7..20e5e5c 100644 (file)
@@ -17,7 +17,8 @@
  * @file        src/include/cynara-plugin.h
  * @author      Zofia Abramowska <z.abramowska@samsung.com>
  * @version     1.0
- * @brief       This file defines cynara side external plugin interface
+ * @brief       This file defines cynara service side  of external plugin interface -
+ *              ServicePluginInterface
  */
 
 #ifndef CYNARA_PLUGIN_H_
 #include <string>
 #include <vector>
 
+#include <plugin/ExternalPluginInterface.h>
 #include <types/PolicyResult.h>
 #include <types/PolicyType.h>
 
 namespace Cynara {
 
-class ExternalPluginInterface;
-
-/**
- * Type of function used for creating objects implementing ExternalPluginInterface.
- * Inside plugin library function with create_t signature should have symbol
- * named "create".
- */
-typedef ExternalPluginInterface *(*create_t)(void);
-/**
- * Type of function used for destroying objects created with "create".
- * Inside plugin library function with destroy_t signature should have symbol
- * named "destroy".
- */
-typedef void (*destroy_t)(ExternalPluginInterface *);
-
-// These typedefs will be defined in external headers
+//These typedefs will be defined in external headers
 typedef std::string PluginData;
 typedef std::string AgentType;
-typedef std::vector<PolicyType> PolicyTypes;
+
+class ServicePluginInterface;
+typedef std::shared_ptr<ServicePluginInterface> ServicePluginInterfacePtr;
 
 /**
  * A class defining external plugins interface.
@@ -58,9 +47,13 @@ typedef std::vector<PolicyType> PolicyTypes;
  * response through check instantly or require communication
  * with given type of agent. Agent must be registered through
  * cynara-agent API.
+ *
+ * Plugin implementing ServicePluginInterface must implement ExternalPluginInterface.
+ * Creation/destruction functions with signatures compatible to Cynara::create_t and
+ * Cynara::destroy_t must be provided as factories of ServicePluginInterface.
  */
 
-class ExternalPluginInterface {
+class ServicePluginInterface : public ExternalPluginInterface {
 public:
     /**
      * Enum indicating status of calling plugin method.
@@ -74,11 +67,6 @@ public:
     };
 
     /**
-     * Policy type supported by plugin.
-     */
-    virtual PolicyTypes getSupportedPolicyTypes(void) = 0;
-
-    /**
      * Asks plugin, what kind of permission does client, user and privilege has.
      *
      * @param[in] client
@@ -107,7 +95,7 @@ public:
                                 const std::string &privilege, const PluginData &agentData,
                                 PolicyResult &result) noexcept = 0;
 
-    virtual ~ExternalPluginInterface() {};
+    virtual ~ServicePluginInterface() {};
 
 };
 
index ca56d64..d4cc634 100644 (file)
@@ -20,6 +20,8 @@
  * @brief       This file implements main class of logic layer in cynara service
  */
 
+#include <signal.h>
+
 #include <log/log.h>
 #include <common.h>
 #include <exceptions/BucketNotExistsException.h>
@@ -29,8 +31,9 @@
 #include <exceptions/InvalidBucketIdException.h>
 #include <exceptions/PluginErrorException.h>
 #include <exceptions/PluginNotFoundException.h>
+#include <exceptions/UnexpectedErrorException.h>
 
-#include <signal.h>
+#include <cynara-plugin.h>
 
 #include <main/Cynara.h>
 #include <request/AdminCheckRequest.h>
@@ -44,9 +47,8 @@
 #include <response/CancelResponse.h>
 #include <response/CheckResponse.h>
 #include <response/CodeResponse.h>
-#include <storage/Storage.h>
-
 #include <sockets/SocketManager.h>
+#include <storage/Storage.h>
 
 #include "Logic.h"
 
@@ -107,16 +109,22 @@ bool Logic::check(RequestContextPtr context UNUSED, const PolicyKey &key,
         throw PluginNotFoundException(result);
     }
 
+    ServicePluginInterfacePtr servicePlugin =
+            std::dynamic_pointer_cast<ServicePluginInterface>(plugin);
+    if (!plugin) {
+        throw PluginNotFoundException(result);
+    }
+
     AgentType requiredAgent;
     PluginData pluginData;
 
-    auto ret = plugin->check(key.client().toString(), key.user().toString(),
-                             key.privilege().toString(), result, requiredAgent, pluginData);
+    auto ret = servicePlugin->check(key.client().toString(), key.user().toString(),
+                                    key.privilege().toString(), result, requiredAgent, pluginData);
 
     switch (ret) {
-        case ExternalPluginInterface::PluginStatus::ANSWER_READY:
+        case ServicePluginInterface::PluginStatus::ANSWER_READY:
             return true;
-        case ExternalPluginInterface::PluginStatus::ANSWER_NOTREADY:
+        case ServicePluginInterface::PluginStatus::ANSWER_NOTREADY:
             //todo send request to agent
             //context should be saved in plugin in order to return answer when ready
             return false;
index c591805..8062683 100644 (file)
@@ -119,7 +119,7 @@ void PluginManager::openPlugin(const std::string &path) {
         return;
     }
 
-    PolicyTypes policies = pluginPtr->getSupportedPolicyTypes();
+    auto policies = pluginPtr->getSupportedPolicyTypes();
     if (policies.empty()) {
         LOGE("Plugin <%s> does not support any type!", path.c_str());
         return;
index 1abc6e6..c2372e6 100644 (file)
@@ -29,7 +29,7 @@
 #include <memory>
 #include <string>
 
-#include <cynara-plugin.h>
+#include <plugin/ExternalPluginInterface.h>
 
 namespace Cynara {
 typedef std::shared_ptr<ExternalPluginInterface> ExternalPluginPtr;