Implement the ApiUser object type

Hide password in GetPassword() and add CheckPassword().
Includes basic unit tests.

refs #9471
This commit is contained in:
Michael Friedrich 2015-06-22 17:51:11 +02:00
parent 99a0c7b157
commit 14f5986aa5
6 changed files with 171 additions and 2 deletions

View File

@ -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
)

40
lib/remote/apiuser.cpp Normal file
View File

@ -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();
}

45
lib/remote/apiuser.hpp Normal file
View File

@ -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 */

31
lib/remote/apiuser.ti Normal file
View File

@ -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;
};
}

View 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)

50
test/remote-apiuser.cpp Normal file
View File

@ -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()