Add translation methods between agent<->cynara<->plugin 42/31042/7
authorZofia Abramowska <z.abramowska@samsung.com>
Mon, 1 Dec 2014 15:58:10 +0000 (16:58 +0100)
committerLukasz Wojciechowski <l.wojciechow@partner.samsung.com>
Mon, 15 Dec 2014 10:17:20 +0000 (02:17 -0800)
Change-Id: Ifd24baca36d9afed2b5201b44054d03584216f22

packaging/askuser.spec
src/common/CMakeLists.txt
src/common/translator/Translator.cpp [new file with mode: 0644]
src/common/translator/Translator.h [new file with mode: 0644]
src/common/types/AgentErrorMsg.cpp [new file with mode: 0644]
src/common/types/AgentErrorMsg.h [new file with mode: 0644]
src/common/types/RequestData.h [new file with mode: 0644]
src/common/types/SupportedTypes.h [new file with mode: 0644]

index 27b9edf..632cd78 100644 (file)
@@ -11,6 +11,9 @@ BuildRequires: cmake
 BuildRequires: zip
 BuildRequires: pkgconfig(libsystemd-daemon)
 BuildRequires: pkgconfig(libsystemd-journal)
+BuildRequires: pkgconfig(cynara-plugin)
+BuildRequires: pkgconfig(cynara-agent)
+
 %{?systemd_requires}
 
 %if !%{defined build_type}
index c85ecae..2512cc5 100644 (file)
 # @file        CMakeLists.txt
 # @author      Adam Malinowski <a.malinowsk2@partner.samsung.com>
 #
+PKG_CHECK_MODULES(COMMON_DEP
+    REQUIRED
+    cynara-plugin
+    cynara-agent
+    )
 
 SET(ASKUSER_COMMON_VERSION_MAJOR 0)
 SET(ASKUSER_COMMON_VERSION ${ASKUSER_COMMON_VERSION_MAJOR}.0.1)
@@ -34,8 +39,15 @@ INCLUDE_DIRECTORIES(SYSTEM
 
 SET(COMMON_PATH ${ASKUSER_PATH}/common)
 
+INCLUDE_DIRECTORIES(
+    ${COMMON_DEP_INCLUDE_DIRS}
+    ${COMMON_PATH}
+    )
+
 SET(COMMON_SOURCES
     ${COMMON_PATH}/log/log.cpp
+    ${COMMON_PATH}/translator/Translator.cpp
+    ${COMMON_PATH}/types/AgentErrorMsg.cpp
     )
 
 ADD_DEFINITIONS("-fvisibility=default")
diff --git a/src/common/translator/Translator.cpp b/src/common/translator/Translator.cpp
new file mode 100644 (file)
index 0000000..9dc59dc
--- /dev/null
@@ -0,0 +1,95 @@
+/*
+ *  Copyright (c) 2014 Samsung Electronics Co.
+ *
+ *  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        Translator.cpp
+ * @author      Zofia Abramowska <z.abramowska@samsung.com>
+ * @brief       Implementation of Translator methods
+ */
+
+#include "Translator.h"
+
+#include <types/AgentErrorMsg.h>
+
+#include <limits>
+#include <stdexcept>
+#include <sstream>
+
+namespace AskUser {
+namespace Translator {
+namespace Agent {
+
+RequestData dataToRequest(const Cynara::PluginData &data) {
+    std::stringstream stream(data);
+    std::size_t strSize;
+    std::string members[3];
+
+    for (auto &member : members) {
+        stream >> strSize;
+        std::vector<char> buffer(strSize, '\0');
+        char separator;
+        //Consume separator
+        stream.read(&separator, 1);
+        stream.read(buffer.data(), strSize);
+        //read doesn't append null
+        member.assign(buffer.begin(), buffer.end());
+    }
+    return RequestData{members[0], members[1], members[2]};
+}
+
+Cynara::PluginData answerToData(Cynara::PolicyType answer, const std::string &errMsg) {
+    if (errMsg.empty())
+        return std::to_string(answer);
+    else
+        return errMsg;
+}
+
+} //namespace Agent
+
+namespace Plugin {
+
+Cynara::PolicyType dataToAnswer(const Cynara::PluginData &data) {
+    // data is an error string
+    if (data == AgentErrorMsg::Error || data == AgentErrorMsg::Timeout)
+        return Cynara::PredefinedPolicyType::DENY;
+    // data is policy type
+    long long policyType;
+    try {
+        policyType = std::stoll(data);
+    } catch (const std::exception &e) {
+        throw TranslateErrorException("Could not convert response to PolicyType : " +
+                                      data);
+    }
+    auto maxPolicyType = std::numeric_limits<Cynara::PolicyType>::max();
+    if (policyType > maxPolicyType) {
+        throw TranslateErrorException("Value of response exceeds max value of PolicyType : "
+                                      + std::to_string(policyType));
+    }
+    return static_cast<Cynara::PolicyType>(policyType);
+}
+
+Cynara::PluginData requestToData(const std::string &client,
+                                 const std::string &user,
+                                 const std::string &privilege)
+{
+    const char separator = ' ';
+    return std::to_string(client.length()) + separator + client + separator
+            + std::to_string(user.length()) + separator + user + separator
+            + std::to_string(privilege.length()) + separator + privilege + separator;
+}
+
+} //namespace Plugin
+} //namespace Translator
+} //namespace AskUser
diff --git a/src/common/translator/Translator.h b/src/common/translator/Translator.h
new file mode 100644 (file)
index 0000000..89140fe
--- /dev/null
@@ -0,0 +1,58 @@
+/*
+ *  Copyright (c) 2014 Samsung Electronics Co.
+ *
+ *  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        Translator.h
+ * @author      Zofia Abramowska <z.abramowska@samsung.com>
+ * @brief       Definition of Translator methods and TranslateErrorException class
+ */
+
+#pragma once
+
+#include <types/RequestData.h>
+#include <types/SupportedTypes.h>
+#include <cynara-plugin.h>
+
+#include <exception>
+#include <string>
+
+namespace AskUser {
+namespace Translator {
+
+class TranslateErrorException : std::exception {
+public:
+    TranslateErrorException(const std::string &msg) : m_what(msg) {};
+    virtual const char* what() const noexcept {
+        return m_what.c_str();
+    }
+private:
+    std::string m_what;
+};
+
+namespace Agent {
+    RequestData dataToRequest(const Cynara::PluginData &data);
+    Cynara::PluginData answerToData(Cynara::PolicyType answer, const std::string &errMsg);
+} // namespace Agent
+
+namespace Plugin {
+    Cynara::PolicyType dataToAnswer(const Cynara::PluginData &data);
+    Cynara::PluginData requestToData(const std::string &client,
+                                     const std::string &user,
+                                     const std::string &privilege);
+} // namespace Plugin
+
+} // namespace Translator
+} // namespace AskUser
+
diff --git a/src/common/types/AgentErrorMsg.cpp b/src/common/types/AgentErrorMsg.cpp
new file mode 100644 (file)
index 0000000..983a84f
--- /dev/null
@@ -0,0 +1,31 @@
+/*
+ *  Copyright (c) 2014 Samsung Electronics Co.
+ *
+ *  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        AgentErrorMsg.cpp
+ * @author      Zofia Abramowska <z.abramowska@samsung.com>
+ * @brief       Definition of agent error messages passed to cynara plugin
+ */
+
+#include "AgentErrorMsg.h"
+
+namespace AskUser {
+namespace AgentErrorMsg {
+
+const std::string Error = "ERROR";
+const std::string Timeout = "TIMEOUT";
+
+} // namespace AgentErrorMsg
+} // namespace AskUser
diff --git a/src/common/types/AgentErrorMsg.h b/src/common/types/AgentErrorMsg.h
new file mode 100644 (file)
index 0000000..6b7dcaa
--- /dev/null
@@ -0,0 +1,33 @@
+/*
+ *  Copyright (c) 2014 Samsung Electronics Co.
+ *
+ *  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        AgentErrorMsg.h
+ * @author      Zofia Abramowska <z.abramowska@samsung.com>
+ * @brief       Agent error messages passed to cynara plugin
+ */
+
+#pragma once
+
+#include <string>
+
+namespace AskUser {
+namespace AgentErrorMsg {
+
+extern const std::string Error;
+extern const std::string Timeout;
+
+} // namespace AgentErrorMsg
+} // namespace AskUser
diff --git a/src/common/types/RequestData.h b/src/common/types/RequestData.h
new file mode 100644 (file)
index 0000000..db93ac5
--- /dev/null
@@ -0,0 +1,34 @@
+/*
+ *  Copyright (c) 2014 Samsung Electronics Co.
+ *
+ *  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        RequestData.h
+ * @author      Zofia Abramowska <z.abramowska@samsung.com>
+ * @brief       Definition of RequestData structure
+ */
+
+#pragma once
+
+#include <string>
+
+namespace AskUser {
+
+struct RequestData {
+    std::string client;
+    std::string user;
+    std::string privilege;
+};
+
+} // namespace AskUser
diff --git a/src/common/types/SupportedTypes.h b/src/common/types/SupportedTypes.h
new file mode 100644 (file)
index 0000000..8baf196
--- /dev/null
@@ -0,0 +1,44 @@
+/*
+ *  Copyright (c) 2014 Samsung Electronics Co.
+ *
+ *  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        SupportedTypes.h
+ * @author      Zofia Abramowska <z.abramowska@samsung.com>
+ * @brief       Definition of plugin supported types and agent type
+ */
+
+#pragma once
+
+#include <types/PolicyType.h>
+
+namespace AskUser {
+namespace SupportedTypes {
+
+namespace Agent {
+const char* const AgentType = "AskUser";
+} //namespace Agent
+
+namespace Service {
+const Cynara::PolicyType ASK_USER = 10;
+} //namespace Service
+
+namespace Client {
+const Cynara::PolicyType ALLOW_ONCE = 11;
+const Cynara::PolicyType ALLOW_PER_SESSION = 12;
+const Cynara::PolicyType ALLOW_PER_LIFE = 13;
+} //namespace Client
+
+} //namespace SupportedTypes
+} //namespace AskUser