From 14f5986aa52c8e6568907e14f90805f90075f531 Mon Sep 17 00:00:00 2001 From: Michael Friedrich Date: Mon, 22 Jun 2015 17:51:11 +0200 Subject: [PATCH] Implement the ApiUser object type Hide password in GetPassword() and add CheckPassword(). Includes basic unit tests. refs #9471 --- lib/remote/CMakeLists.txt | 3 ++- lib/remote/apiuser.cpp | 40 +++++++++++++++++++++++++++++++ lib/remote/apiuser.hpp | 45 +++++++++++++++++++++++++++++++++++ lib/remote/apiuser.ti | 31 ++++++++++++++++++++++++ test/CMakeLists.txt | 4 +++- test/remote-apiuser.cpp | 50 +++++++++++++++++++++++++++++++++++++++ 6 files changed, 171 insertions(+), 2 deletions(-) create mode 100644 lib/remote/apiuser.cpp create mode 100644 lib/remote/apiuser.hpp create mode 100644 lib/remote/apiuser.ti create mode 100644 test/remote-apiuser.cpp diff --git a/lib/remote/CMakeLists.txt b/lib/remote/CMakeLists.txt index e7e96e190..2734d82e7 100644 --- a/lib/remote/CMakeLists.txt +++ b/lib/remote/CMakeLists.txt @@ -16,12 +16,13 @@ # 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 index 000000000..d0c49c15f --- /dev/null +++ b/lib/remote/apiuser.cpp @@ -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 index 000000000..005da3de2 --- /dev/null +++ b/lib/remote/apiuser.hpp @@ -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 +{ +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 index 000000000..05471ad29 --- /dev/null +++ b/lib/remote/apiuser.ti @@ -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; +}; + +} diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index dc4653f08..adab1dd7c 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -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 index 000000000..b641780d8 --- /dev/null +++ b/test/remote-apiuser.cpp @@ -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 + +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()