]> granicus.if.org Git - icinga2/commitdiff
Implement the ApiUser object type
authorMichael Friedrich <michael.friedrich@netways.de>
Mon, 22 Jun 2015 15:51:11 +0000 (17:51 +0200)
committerMichael Friedrich <michael.friedrich@netways.de>
Wed, 24 Jun 2015 12:28:52 +0000 (14:28 +0200)
Hide password in GetPassword() and add CheckPassword().
Includes basic unit tests.

refs #9471

lib/remote/CMakeLists.txt
lib/remote/apiuser.cpp [new file with mode: 0644]
lib/remote/apiuser.hpp [new file with mode: 0644]
lib/remote/apiuser.ti [new file with mode: 0644]
test/CMakeLists.txt
test/remote-apiuser.cpp [new file with mode: 0644]

index e7e96e19074d806eea5990f169430a68d4bfd287..2734d82e75072d7f260ab2ab6161d63ff2b184c0 100644 (file)
 # Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
 
 mkclass_target(apilistener.ti apilistener.tcpp apilistener.thpp)
+mkclass_target(apiuser.ti apiuser.tcpp apiuser.thpp)
 mkclass_target(endpoint.ti endpoint.tcpp endpoint.thpp)
 mkclass_target(zone.ti zone.tcpp zone.thpp)
 
 set(remote_SOURCES
   apiclient.cpp apiclient-heartbeat.cpp apifunction.cpp apilistener.cpp apilistener.thpp apilistener-sync.cpp
-  authority.cpp endpoint.cpp endpoint.thpp jsonrpc.cpp
+  apiuser.cpp apiuser.thpp authority.cpp endpoint.cpp endpoint.thpp jsonrpc.cpp
   messageorigin.cpp zone.cpp zone.thpp
 )
 
diff --git a/lib/remote/apiuser.cpp b/lib/remote/apiuser.cpp
new file mode 100644 (file)
index 0000000..d0c49c1
--- /dev/null
@@ -0,0 +1,40 @@
+/******************************************************************************
+ * Icinga 2                                                                   *
+ * Copyright (C) 2012-2015 Icinga Development Team (http://www.icinga.org)    *
+ *                                                                            *
+ * This program is free software; you can redistribute it and/or              *
+ * modify it under the terms of the GNU General Public License                *
+ * as published by the Free Software Foundation; either version 2             *
+ * of the License, or (at your option) any later version.                     *
+ *                                                                            *
+ * This program is distributed in the hope that it will be useful,            *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
+ * GNU General Public License for more details.                               *
+ *                                                                            *
+ * You should have received a copy of the GNU General Public License          *
+ * along with this program; if not, write to the Free Software Foundation     *
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.             *
+ ******************************************************************************/
+
+#include "remote/apiuser.hpp"
+#include "remote/apiuser.tcpp"
+
+using namespace icinga;
+
+REGISTER_TYPE(ApiUser);
+
+String ApiUser::GetPassword(void) const
+{
+       return "*****";
+}
+
+void ApiUser::SetPassword(const String& password)
+{
+       SetPasswordRaw(password);
+}
+
+bool ApiUser::CheckPassword(const String& password)
+{
+       return password == GetPasswordRaw();
+}
diff --git a/lib/remote/apiuser.hpp b/lib/remote/apiuser.hpp
new file mode 100644 (file)
index 0000000..005da3d
--- /dev/null
@@ -0,0 +1,45 @@
+/******************************************************************************
+ * Icinga 2                                                                   *
+ * Copyright (C) 2012-2015 Icinga Development Team (http://www.icinga.org)    *
+ *                                                                            *
+ * This program is free software; you can redistribute it and/or              *
+ * modify it under the terms of the GNU General Public License                *
+ * as published by the Free Software Foundation; either version 2             *
+ * of the License, or (at your option) any later version.                     *
+ *                                                                            *
+ * This program is distributed in the hope that it will be useful,            *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
+ * GNU General Public License for more details.                               *
+ *                                                                            *
+ * You should have received a copy of the GNU General Public License          *
+ * along with this program; if not, write to the Free Software Foundation     *
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.             *
+ ******************************************************************************/
+
+#ifndef APIUSER_H
+#define APIUSER_H
+
+#include "remote/i2-remote.hpp"
+#include "remote/apiuser.thpp"
+
+namespace icinga
+{
+
+/**
+ * @ingroup remote
+ */
+class I2_REMOTE_API ApiUser : public ObjectImpl<ApiUser>
+{
+public:
+       DECLARE_OBJECT(ApiUser);
+       DECLARE_OBJECTNAME(ApiUser);
+
+       String GetPassword(void) const;
+       void SetPassword(const String& password);
+       bool CheckPassword(const String& password);
+};
+
+}
+
+#endif /* APIUSER_H */
diff --git a/lib/remote/apiuser.ti b/lib/remote/apiuser.ti
new file mode 100644 (file)
index 0000000..05471ad
--- /dev/null
@@ -0,0 +1,31 @@
+/******************************************************************************
+ * Icinga 2                                                                   *
+ * Copyright (C) 2012-2015 Icinga Development Team (http://www.icinga.org)    *
+ *                                                                            *
+ * This program is free software; you can redistribute it and/or              *
+ * modify it under the terms of the GNU General Public License                *
+ * as published by the Free Software Foundation; either version 2             *
+ * of the License, or (at your option) any later version.                     *
+ *                                                                            *
+ * This program is distributed in the hope that it will be useful,            *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
+ * GNU General Public License for more details.                               *
+ *                                                                            *
+ * You should have received a copy of the GNU General Public License          *
+ * along with this program; if not, write to the Free Software Foundation     *
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.             *
+ ******************************************************************************/
+
+#include "base/dynamicobject.hpp"
+
+namespace icinga
+{
+
+class ApiUser : DynamicObject
+{
+       [config, protected] String password (PasswordRaw);
+       [config] String client_cn;
+};
+
+}
index dc4653f080371f503f021f4cf61b6470619bdff9..adab1dd7cf20290c11c0fc6db2be313933a9b7df 100644 (file)
@@ -23,7 +23,7 @@ set(base_test_SOURCES
   base-serialize.cpp base-shellescape.cpp base-stacktrace.cpp
   base-stream.cpp base-string.cpp base-timer.cpp base-type.cpp
   base-value.cpp config-ops.cpp icinga-macros.cpp icinga-perfdata.cpp
-  test.cpp
+  remote-apiuser.cpp test.cpp
 )
 
 set(livestatus_test_SOURCES
@@ -106,6 +106,8 @@ add_boost_test(base
        icinga_perfdata/ignore_invalid_warn_crit_min_max
        icinga_perfdata/invalid
        icinga_perfdata/multi
+       remote_apiuser/get_password
+       remote_apiuser/check_password
 )
 
 if(ICINGA2_WITH_LIVESTATUS)
diff --git a/test/remote-apiuser.cpp b/test/remote-apiuser.cpp
new file mode 100644 (file)
index 0000000..b641780
--- /dev/null
@@ -0,0 +1,50 @@
+/******************************************************************************
+ * Icinga 2                                                                   *
+ * Copyright (C) 2012-2015 Icinga Development Team (http://www.icinga.org)    *
+ *                                                                            *
+ * This program is free software; you can redistribute it and/or              *
+ * modify it under the terms of the GNU General Public License                *
+ * as published by the Free Software Foundation; either version 2             *
+ * of the License, or (at your option) any later version.                     *
+ *                                                                            *
+ * This program is distributed in the hope that it will be useful,            *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
+ * GNU General Public License for more details.                               *
+ *                                                                            *
+ * You should have received a copy of the GNU General Public License          *
+ * along with this program; if not, write to the Free Software Foundation     *
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.             *
+ ******************************************************************************/
+
+#include "remote/apiuser.hpp"
+#include <boost/test/unit_test.hpp>
+
+using namespace icinga;
+
+BOOST_AUTO_TEST_SUITE(remote_apiuser)
+
+BOOST_AUTO_TEST_CASE(construct)
+{
+       ApiUser::Ptr apiuser = new ApiUser();
+       BOOST_CHECK(apiuser);
+}
+
+BOOST_AUTO_TEST_CASE(get_password)
+{
+       ApiUser::Ptr apiuser = new ApiUser();
+       apiuser->SetPassword("icingar0xx");
+
+       BOOST_CHECK(apiuser->GetPassword() == "*****");
+}
+
+BOOST_AUTO_TEST_CASE(check_password)
+{
+       ApiUser::Ptr apiuser = new ApiUser();
+       apiuser->SetPassword("icingar0xx");
+
+       BOOST_CHECK(apiuser->CheckPassword("1cing4r0xx") == false);
+       BOOST_CHECK(apiuser->CheckPassword("icingar0xx") == true);
+}
+
+BOOST_AUTO_TEST_SUITE_END()