blob: c210b848e86c40a7039fd347b86479599fbb73c7 [file] [log] [blame]
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -06001#include "config.h"
Patrick Williams9638afb2021-02-22 17:16:24 -06002
Ratan Gupta37fb3fe2019-04-13 12:54:18 +05303#include "phosphor-ldap-config/ldap_config.hpp"
Ratan Guptae1f4db62019-04-11 18:57:42 +05304#include "phosphor-ldap-config/ldap_config_mgr.hpp"
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -06005
Patrick Williams9638afb2021-02-22 17:16:24 -06006#include <sys/types.h>
7
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -06008#include <phosphor-logging/elog-errors.hpp>
Patrick Williams9638afb2021-02-22 17:16:24 -06009#include <phosphor-logging/log.hpp>
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -060010#include <sdbusplus/bus.hpp>
11#include <xyz/openbmc_project/Common/error.hpp>
Ratan Guptafef57892019-04-14 13:43:09 +053012#include <xyz/openbmc_project/User/Common/error.hpp>
Ratan Gupta95a29312019-02-18 20:34:10 +053013
14#include <filesystem>
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -060015#include <fstream>
16#include <string>
Patrick Williams9638afb2021-02-22 17:16:24 -060017
18#include <gmock/gmock.h>
19#include <gtest/gtest.h>
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -060020
21namespace phosphor
22{
23namespace ldap
24{
Ratan Gupta95a29312019-02-18 20:34:10 +053025namespace fs = std::filesystem;
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -060026namespace ldap_base = sdbusplus::xyz::openbmc_project::User::Ldap::server;
Ratan Guptac5481d12019-04-12 18:31:05 +053027using NotAllowed = sdbusplus::xyz::openbmc_project::Common::Error::NotAllowed;
28using NotAllowedArgument = xyz::openbmc_project::Common::NotAllowed;
29
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -060030using Config = phosphor::ldap::Config;
Ratan Guptafef57892019-04-14 13:43:09 +053031using PrivilegeMappingExists = sdbusplus::xyz::openbmc_project::User::Common::
32 Error::PrivilegeMappingExists;
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -060033
34class TestLDAPConfig : public testing::Test
35{
36 public:
37 TestLDAPConfig() : bus(sdbusplus::bus::new_default())
Patrick Williams9638afb2021-02-22 17:16:24 -060038 {}
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -060039 void SetUp() override
40 {
41 using namespace phosphor::ldap;
42 char tmpldap[] = "/tmp/ldap_test.XXXXXX";
43 dir = fs::path(mkdtemp(tmpldap));
Zbigniew Kurzynski5d00cf22019-10-03 12:10:20 +020044 fs::path tlsCacertFilePath{TLS_CACERT_PATH};
Nan Zhou78d85042022-08-29 17:50:22 +000045 tlsCACertFile = tlsCacertFilePath.filename().c_str();
Ratan Gupta22f13f12019-04-29 15:36:40 +053046 fs::path tlsCertFilePath{TLS_CERT_FILE};
47 tlsCertFile = tlsCertFilePath.filename().c_str();
48
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -060049 fs::path confFilePath{LDAP_CONFIG_FILE};
Nan Zhou78d85042022-08-29 17:50:22 +000050 ldapConfFile = confFilePath.filename().c_str();
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -060051 std::fstream fs;
52 fs.open(dir / defaultNslcdFile, std::fstream::out);
53 fs.close();
54 fs.open(dir / nsSwitchFile, std::fstream::out);
55 fs.close();
Nan Zhou78d85042022-08-29 17:50:22 +000056 fs.open(dir / tlsCACertFile, std::fstream::out);
Ratan Gupta22f13f12019-04-29 15:36:40 +053057 fs.close();
58 fs.open(dir / tlsCertFile, std::fstream::out);
59 fs.close();
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -060060 }
61
62 void TearDown() override
63 {
64 fs::remove_all(dir);
65 }
66
67 protected:
68 fs::path dir;
Nan Zhou78d85042022-08-29 17:50:22 +000069 std::string tlsCACertFile;
Ratan Gupta22f13f12019-04-29 15:36:40 +053070 std::string tlsCertFile;
Nan Zhou78d85042022-08-29 17:50:22 +000071 std::string ldapConfFile;
Patrick Williamsb3ef4e12022-07-22 19:26:55 -050072 sdbusplus::bus_t bus;
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -060073};
74
75class MockConfigMgr : public phosphor::ldap::ConfigMgr
76{
77 public:
Patrick Williamsb3ef4e12022-07-22 19:26:55 -050078 MockConfigMgr(sdbusplus::bus_t& bus, const char* path, const char* filePath,
79 const char* dbusPersistentFile, const char* caCertFile,
80 const char* certFile) :
Ratan Gupta95a29312019-02-18 20:34:10 +053081 phosphor::ldap::ConfigMgr(bus, path, filePath, dbusPersistentFile,
Ratan Gupta22f13f12019-04-29 15:36:40 +053082 caCertFile, certFile)
Patrick Williams9638afb2021-02-22 17:16:24 -060083 {}
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -060084 MOCK_METHOD1(restartService, void(const std::string& service));
85 MOCK_METHOD1(stopService, void(const std::string& service));
Ratan Gupta27d4c012019-04-12 13:03:35 +053086 std::unique_ptr<Config>& getOpenLdapConfigPtr()
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -060087 {
Ratan Gupta27d4c012019-04-12 13:03:35 +053088 return openLDAPConfigPtr;
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -060089 }
90
Ratan Gupta3a1c2742019-03-20 06:49:42 +053091 std::string configBindPassword()
92 {
Patrick Williamse6500a42021-05-01 05:58:23 -050093 return getADConfigPtr()->ldapBindPassword;
Ratan Gupta3a1c2742019-03-20 06:49:42 +053094 }
95
Ratan Gupta27d4c012019-04-12 13:03:35 +053096 std::unique_ptr<Config>& getADConfigPtr()
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -060097 {
Ratan Gupta27d4c012019-04-12 13:03:35 +053098 return ADConfigPtr;
99 }
100 void restore()
101 {
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530102 phosphor::ldap::ConfigMgr::restore();
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600103 return;
104 }
105
Ratan Gupta27d4c012019-04-12 13:03:35 +0530106 void createDefaultObjects()
107 {
108 phosphor::ldap::ConfigMgr::createDefaultObjects();
109 }
110
Ravi Tejad5884042019-06-10 02:35:22 -0500111 bool secureLDAP()
112 {
113 return ADConfigPtr->secureLDAP;
114 }
115
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600116 friend class TestLDAPConfig;
117};
118
119TEST_F(TestLDAPConfig, testCreate)
120{
Nan Zhou78d85042022-08-29 17:50:22 +0000121 auto configFilePath = std::string(dir.c_str()) + "/" + ldapConfFile;
122 auto tlsCACertFilePath = std::string(dir.c_str()) + "/" + tlsCACertFile;
123 auto tlsCertFilePath = std::string(dir.c_str()) + "/" + tlsCertFile;
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530124 auto dbusPersistentFilePath = std::string(dir.c_str());
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600125
126 if (fs::exists(configFilePath))
127 {
128 fs::remove(configFilePath);
129 }
130 EXPECT_FALSE(fs::exists(configFilePath));
Nagaraju Goruganti3b4d06a2018-11-08 03:13:38 -0600131 MockConfigMgr manager(bus, LDAP_CONFIG_ROOT, configFilePath.c_str(),
Nan Zhou78d85042022-08-29 17:50:22 +0000132 dbusPersistentFilePath.c_str(),
133 tlsCACertFilePath.c_str(), tlsCertFilePath.c_str());
Ratan Gupta27d4c012019-04-12 13:03:35 +0530134
Ratan Guptaec117542019-04-25 18:38:29 +0530135 EXPECT_CALL(manager, stopService("nslcd.service")).Times(2);
Ratan Gupta3a1c2742019-03-20 06:49:42 +0530136 EXPECT_CALL(manager, restartService("nslcd.service")).Times(2);
Ratan Guptaec117542019-04-25 18:38:29 +0530137 EXPECT_CALL(manager, restartService("nscd.service")).Times(2);
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530138
Ratan Guptaaeaf9412019-02-11 04:41:52 -0600139 manager.createConfig(
140 "ldap://9.194.251.136/", "cn=Users,dc=com", "cn=Users,dc=corp",
141 "MyLdap12", ldap_base::Create::SearchScope::sub,
142 ldap_base::Create::Type::ActiveDirectory, "uid", "gid");
Ratan Gupta27d4c012019-04-12 13:03:35 +0530143 manager.getADConfigPtr()->enabled(true);
Ratan Gupta95a29312019-02-18 20:34:10 +0530144
Ratan Guptaec117542019-04-25 18:38:29 +0530145 manager.createConfig("ldap://9.194.251.137/", "cn=Users",
146 "cn=Users,dc=test", "MyLdap123",
147 ldap_base::Create::SearchScope::sub,
148 ldap_base::Create::Type::OpenLdap, "uid", "gid");
149 manager.getOpenLdapConfigPtr()->enabled(false);
150
151 // Below setting of username/groupname attr is to make sure
152 // that in-active config should not call the start/stop service.
153 manager.getOpenLdapConfigPtr()->userNameAttribute("abc");
154 EXPECT_EQ(manager.getOpenLdapConfigPtr()->userNameAttribute(), "abc");
155
156 manager.getOpenLdapConfigPtr()->groupNameAttribute("def");
157 EXPECT_EQ(manager.getOpenLdapConfigPtr()->groupNameAttribute(), "def");
158
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600159 EXPECT_TRUE(fs::exists(configFilePath));
Patrick Williamse6500a42021-05-01 05:58:23 -0500160 EXPECT_EQ(manager.getADConfigPtr()->ldapServerURI(),
Ratan Gupta27d4c012019-04-12 13:03:35 +0530161 "ldap://9.194.251.136/");
Patrick Williamse6500a42021-05-01 05:58:23 -0500162 EXPECT_EQ(manager.getADConfigPtr()->ldapBindDN(), "cn=Users,dc=com");
163 EXPECT_EQ(manager.getADConfigPtr()->ldapBaseDN(), "cn=Users,dc=corp");
164 EXPECT_EQ(manager.getADConfigPtr()->ldapSearchScope(),
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600165 ldap_base::Config::SearchScope::sub);
Patrick Williamse6500a42021-05-01 05:58:23 -0500166 EXPECT_EQ(manager.getADConfigPtr()->ldapType(),
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600167 ldap_base::Config::Type::ActiveDirectory);
Ratan Gupta27d4c012019-04-12 13:03:35 +0530168
169 EXPECT_EQ(manager.getADConfigPtr()->userNameAttribute(), "uid");
170 EXPECT_EQ(manager.getADConfigPtr()->groupNameAttribute(), "gid");
Patrick Williamse6500a42021-05-01 05:58:23 -0500171 EXPECT_EQ(manager.getADConfigPtr()->ldapBindDNPassword(), "");
Ratan Gupta3a1c2742019-03-20 06:49:42 +0530172 EXPECT_EQ(manager.configBindPassword(), "MyLdap12");
173 // change the password
Patrick Williamse6500a42021-05-01 05:58:23 -0500174 manager.getADConfigPtr()->ldapBindDNPassword("MyLdap14");
175 EXPECT_EQ(manager.getADConfigPtr()->ldapBindDNPassword(), "");
Ratan Gupta3a1c2742019-03-20 06:49:42 +0530176 EXPECT_EQ(manager.configBindPassword(), "MyLdap14");
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600177}
178
Ratan Gupta27d4c012019-04-12 13:03:35 +0530179TEST_F(TestLDAPConfig, testDefaultObject)
180{
Nan Zhou78d85042022-08-29 17:50:22 +0000181 auto configFilePath = std::string(dir.c_str()) + "/" + ldapConfFile;
182 auto tlsCACertFilePath = std::string(dir.c_str()) + "/" + tlsCACertFile;
183 auto tlsCertFilePath = std::string(dir.c_str()) + "/" + tlsCertFile;
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530184 auto dbusPersistentFilePath = std::string(dir.c_str());
Ratan Gupta27d4c012019-04-12 13:03:35 +0530185
186 if (fs::exists(configFilePath))
187 {
188 fs::remove(configFilePath);
189 }
190 EXPECT_FALSE(fs::exists(configFilePath));
191
192 MockConfigMgr manager(bus, LDAP_CONFIG_ROOT, configFilePath.c_str(),
Nan Zhou78d85042022-08-29 17:50:22 +0000193 dbusPersistentFilePath.c_str(),
194 tlsCACertFilePath.c_str(), tlsCertFilePath.c_str());
Ratan Gupta27d4c012019-04-12 13:03:35 +0530195
Ratan Gupta27d4c012019-04-12 13:03:35 +0530196 manager.createDefaultObjects();
197
198 EXPECT_NE(nullptr, manager.getADConfigPtr());
199 EXPECT_NE(nullptr, manager.getOpenLdapConfigPtr());
Patrick Williamse6500a42021-05-01 05:58:23 -0500200 EXPECT_EQ(manager.getADConfigPtr()->ldapType(),
Ratan Gupta27d4c012019-04-12 13:03:35 +0530201 ldap_base::Config::Type::ActiveDirectory);
Patrick Williamse6500a42021-05-01 05:58:23 -0500202 EXPECT_EQ(manager.getOpenLdapConfigPtr()->ldapType(),
Ratan Gupta27d4c012019-04-12 13:03:35 +0530203 ldap_base::Config::Type::OpenLdap);
204}
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530205
Alexander Filippov372c5662021-06-30 20:23:39 +0300206TEST_F(TestLDAPConfig, testRestoresDefault)
207{
Nan Zhou78d85042022-08-29 17:50:22 +0000208 auto configFilePath = std::string(dir.c_str()) + "/" + ldapConfFile;
209 auto tlsCACertFilePath = std::string(dir.c_str()) + "/" + tlsCACertFile;
210 auto tlsCertFilePath = std::string(dir.c_str()) + "/" + tlsCertFile;
Alexander Filippov372c5662021-06-30 20:23:39 +0300211 auto dbusPersistentFilePath = std::string(dir.c_str());
212
213 if (fs::exists(configFilePath))
214 {
215 fs::remove(configFilePath);
216 }
217 EXPECT_FALSE(fs::exists(configFilePath));
218
219 MockConfigMgr manager(bus, LDAP_CONFIG_ROOT, configFilePath.c_str(),
Nan Zhou78d85042022-08-29 17:50:22 +0000220 dbusPersistentFilePath.c_str(),
221 tlsCACertFilePath.c_str(), tlsCertFilePath.c_str());
Alexander Filippov372c5662021-06-30 20:23:39 +0300222
223 EXPECT_CALL(manager, stopService("nslcd.service")).Times(1);
224 EXPECT_CALL(manager, restartService("nslcd.service")).Times(0);
225 EXPECT_CALL(manager, restartService("nscd.service")).Times(0);
226
227 manager.restore();
228
229 EXPECT_NE(nullptr, manager.getADConfigPtr());
230 EXPECT_NE(nullptr, manager.getOpenLdapConfigPtr());
231 EXPECT_EQ(manager.getADConfigPtr()->ldapType(),
232 ldap_base::Config::Type::ActiveDirectory);
233 EXPECT_EQ(manager.getOpenLdapConfigPtr()->ldapType(),
234 ldap_base::Config::Type::OpenLdap);
235 EXPECT_FALSE(manager.getADConfigPtr()->enabled());
236 EXPECT_FALSE(manager.getOpenLdapConfigPtr()->enabled());
237}
238
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600239TEST_F(TestLDAPConfig, testRestores)
240{
Nan Zhou78d85042022-08-29 17:50:22 +0000241 auto configFilePath = std::string(dir.c_str()) + "/" + ldapConfFile;
242 auto tlsCACertFilePath = std::string(dir.c_str()) + "/" + tlsCACertFile;
243 auto tlsCertFilePath = std::string(dir.c_str()) + "/" + tlsCertFile;
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530244 auto dbusPersistentFilePath = std::string(dir.c_str());
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600245
246 if (fs::exists(configFilePath))
247 {
248 fs::remove(configFilePath);
249 }
250 EXPECT_FALSE(fs::exists(configFilePath));
Ratan Gupta22f13f12019-04-29 15:36:40 +0530251 MockConfigMgr* managerPtr =
252 new MockConfigMgr(bus, LDAP_CONFIG_ROOT, configFilePath.c_str(),
Nan Zhou78d85042022-08-29 17:50:22 +0000253 dbusPersistentFilePath.c_str(),
254 tlsCACertFilePath.c_str(), tlsCertFilePath.c_str());
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530255 EXPECT_CALL(*managerPtr, stopService("nslcd.service")).Times(1);
Alexander Filippov372c5662021-06-30 20:23:39 +0300256 EXPECT_CALL(*managerPtr, restartService("nslcd.service")).Times(2);
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530257 EXPECT_CALL(*managerPtr, restartService("nscd.service")).Times(1);
Ratan Guptaaeaf9412019-02-11 04:41:52 -0600258 managerPtr->createConfig(
259 "ldap://9.194.251.138/", "cn=Users,dc=com", "cn=Users,dc=corp",
260 "MyLdap12", ldap_base::Create::SearchScope::sub,
261 ldap_base::Create::Type::ActiveDirectory, "uid", "gid");
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530262 managerPtr->getADConfigPtr()->enabled(false);
263 EXPECT_FALSE(fs::exists(configFilePath));
264 EXPECT_FALSE(managerPtr->getADConfigPtr()->enabled());
265 managerPtr->getADConfigPtr()->enabled(true);
Ratan Gupta95a29312019-02-18 20:34:10 +0530266
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600267 EXPECT_TRUE(fs::exists(configFilePath));
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600268 // Restore from configFilePath
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530269 managerPtr->restore();
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600270 // validate restored properties
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530271 EXPECT_TRUE(managerPtr->getADConfigPtr()->enabled());
Patrick Williamse6500a42021-05-01 05:58:23 -0500272 EXPECT_EQ(managerPtr->getADConfigPtr()->ldapServerURI(),
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600273 "ldap://9.194.251.138/");
Patrick Williamse6500a42021-05-01 05:58:23 -0500274 EXPECT_EQ(managerPtr->getADConfigPtr()->ldapBindDN(), "cn=Users,dc=com");
275 EXPECT_EQ(managerPtr->getADConfigPtr()->ldapBaseDN(), "cn=Users,dc=corp");
276 EXPECT_EQ(managerPtr->getADConfigPtr()->ldapSearchScope(),
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600277 ldap_base::Config::SearchScope::sub);
Patrick Williamse6500a42021-05-01 05:58:23 -0500278 EXPECT_EQ(managerPtr->getADConfigPtr()->ldapType(),
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600279 ldap_base::Config::Type::ActiveDirectory);
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530280 EXPECT_EQ(managerPtr->getADConfigPtr()->userNameAttribute(), "uid");
281 EXPECT_EQ(managerPtr->getADConfigPtr()->groupNameAttribute(), "gid");
Patrick Williamse6500a42021-05-01 05:58:23 -0500282 EXPECT_EQ(managerPtr->getADConfigPtr()->ldapBindDNPassword(), "");
Ratan Gupta3a1c2742019-03-20 06:49:42 +0530283 EXPECT_EQ(managerPtr->configBindPassword(), "MyLdap12");
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600284 delete managerPtr;
285}
286
287TEST_F(TestLDAPConfig, testLDAPServerURI)
288{
Nan Zhou78d85042022-08-29 17:50:22 +0000289 auto configFilePath = std::string(dir.c_str()) + "/" + ldapConfFile;
290 auto tlsCACertFilePath = std::string(dir.c_str()) + "/" + tlsCACertFile;
291 auto tlsCertFilePath = std::string(dir.c_str()) + "/" + tlsCertFile;
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530292 auto dbusPersistentFilePath = std::string(dir.c_str());
Nagaraju Goruganti3b4d06a2018-11-08 03:13:38 -0600293
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600294 if (fs::exists(configFilePath))
295 {
296 fs::remove(configFilePath);
297 }
298 EXPECT_FALSE(fs::exists(configFilePath));
Ratan Gupta22f13f12019-04-29 15:36:40 +0530299 MockConfigMgr* managerPtr =
300 new MockConfigMgr(bus, LDAP_CONFIG_ROOT, configFilePath.c_str(),
Nan Zhou78d85042022-08-29 17:50:22 +0000301 dbusPersistentFilePath.c_str(),
302 tlsCACertFilePath.c_str(), tlsCertFilePath.c_str());
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530303
304 EXPECT_CALL(*managerPtr, stopService("nslcd.service")).Times(1);
Alexander Filippov372c5662021-06-30 20:23:39 +0300305 EXPECT_CALL(*managerPtr, restartService("nslcd.service")).Times(3);
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530306 EXPECT_CALL(*managerPtr, restartService("nscd.service")).Times(1);
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600307
Ratan Guptaaeaf9412019-02-11 04:41:52 -0600308 managerPtr->createConfig(
309 "ldap://9.194.251.138/", "cn=Users,dc=com", "cn=Users,dc=corp",
310 "MyLdap12", ldap_base::Create::SearchScope::sub,
311 ldap_base::Create::Type::ActiveDirectory, "attr1", "attr2");
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530312 managerPtr->getADConfigPtr()->enabled(true);
Ratan Gupta95a29312019-02-18 20:34:10 +0530313
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600314 // Change LDAP Server URI
Patrick Williamse6500a42021-05-01 05:58:23 -0500315 managerPtr->getADConfigPtr()->ldapServerURI("ldap://9.194.251.139/");
316 EXPECT_EQ(managerPtr->getADConfigPtr()->ldapServerURI(),
Nagaraju Goruganti3b4d06a2018-11-08 03:13:38 -0600317 "ldap://9.194.251.139/");
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530318
Nan Zhou78d85042022-08-29 17:50:22 +0000319 fs::remove(tlsCACertFilePath.c_str());
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530320 // Change LDAP Server URI to make it secure
Nagaraju Goruganti3b4d06a2018-11-08 03:13:38 -0600321 EXPECT_THROW(
Patrick Williamse6500a42021-05-01 05:58:23 -0500322 managerPtr->getADConfigPtr()->ldapServerURI("ldaps://9.194.251.139/"),
Nagaraju Goruganti3b4d06a2018-11-08 03:13:38 -0600323 NoCACertificate);
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600324
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530325 // check once again
Patrick Williamse6500a42021-05-01 05:58:23 -0500326 EXPECT_EQ(managerPtr->getADConfigPtr()->ldapServerURI(),
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530327 "ldap://9.194.251.139/");
328
329 managerPtr->restore();
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600330 // Check LDAP Server URI
Patrick Williamse6500a42021-05-01 05:58:23 -0500331 EXPECT_EQ(managerPtr->getADConfigPtr()->ldapServerURI(),
Nagaraju Goruganti3b4d06a2018-11-08 03:13:38 -0600332 "ldap://9.194.251.139/");
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600333 delete managerPtr;
334}
335
336TEST_F(TestLDAPConfig, testLDAPBindDN)
337{
Nan Zhou78d85042022-08-29 17:50:22 +0000338 auto configFilePath = std::string(dir.c_str()) + "/" + ldapConfFile;
339 auto tlsCACertFilePath = std::string(dir.c_str()) + "/" + tlsCACertFile;
340 auto tlsCertFilePath = std::string(dir.c_str()) + "/" + tlsCertFile;
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530341 auto dbusPersistentFilePath = std::string(dir.c_str());
Nagaraju Goruganti3b4d06a2018-11-08 03:13:38 -0600342
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600343 if (fs::exists(configFilePath))
344 {
345 fs::remove(configFilePath);
346 }
347 EXPECT_FALSE(fs::exists(configFilePath));
Ratan Gupta22f13f12019-04-29 15:36:40 +0530348 MockConfigMgr* managerPtr =
349 new MockConfigMgr(bus, LDAP_CONFIG_ROOT, configFilePath.c_str(),
Nan Zhou78d85042022-08-29 17:50:22 +0000350 dbusPersistentFilePath.c_str(),
351 tlsCACertFilePath.c_str(), tlsCertFilePath.c_str());
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530352
353 EXPECT_CALL(*managerPtr, stopService("nslcd.service")).Times(1);
Alexander Filippov372c5662021-06-30 20:23:39 +0300354 EXPECT_CALL(*managerPtr, restartService("nslcd.service")).Times(3);
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530355 EXPECT_CALL(*managerPtr, restartService("nscd.service")).Times(1);
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600356
Ratan Guptaaeaf9412019-02-11 04:41:52 -0600357 managerPtr->createConfig(
358 "ldap://9.194.251.138/", "cn=Users,dc=com", "cn=Users,dc=corp",
359 "MyLdap12", ldap_base::Create::SearchScope::sub,
360 ldap_base::Create::Type::ActiveDirectory, "attr1", "attr2");
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530361 managerPtr->getADConfigPtr()->enabled(true);
Ratan Gupta95a29312019-02-18 20:34:10 +0530362
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600363 // Change LDAP BindDN
Patrick Williamse6500a42021-05-01 05:58:23 -0500364 managerPtr->getADConfigPtr()->ldapBindDN(
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600365 "cn=Administrator,cn=Users,dc=corp,dc=ibm,dc=com");
Patrick Williamse6500a42021-05-01 05:58:23 -0500366 EXPECT_EQ(managerPtr->getADConfigPtr()->ldapBindDN(),
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600367 "cn=Administrator,cn=Users,dc=corp,dc=ibm,dc=com");
368 // Change LDAP BindDN
369 EXPECT_THROW(
370 {
371 try
372 {
Patrick Williamse6500a42021-05-01 05:58:23 -0500373 managerPtr->getADConfigPtr()->ldapBindDN("");
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600374 }
375 catch (const InvalidArgument& e)
376 {
377 throw;
378 }
379 },
380 InvalidArgument);
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600381
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530382 managerPtr->restore();
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600383 // Check LDAP BindDN after restoring
Patrick Williamse6500a42021-05-01 05:58:23 -0500384 EXPECT_EQ(managerPtr->getADConfigPtr()->ldapBindDN(),
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600385 "cn=Administrator,cn=Users,dc=corp,dc=ibm,dc=com");
386 delete managerPtr;
387}
388
389TEST_F(TestLDAPConfig, testLDAPBaseDN)
390{
Nan Zhou78d85042022-08-29 17:50:22 +0000391 auto configFilePath = std::string(dir.c_str()) + "/" + ldapConfFile;
392 auto tlsCACertFilePath = std::string(dir.c_str()) + "/" + tlsCACertFile;
393 auto tlsCertFilePath = std::string(dir.c_str()) + "/" + tlsCertFile;
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530394 auto dbusPersistentFilePath = std::string(dir.c_str());
Nagaraju Goruganti3b4d06a2018-11-08 03:13:38 -0600395
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600396 if (fs::exists(configFilePath))
397 {
398 fs::remove(configFilePath);
399 }
400 EXPECT_FALSE(fs::exists(configFilePath));
Ratan Gupta22f13f12019-04-29 15:36:40 +0530401 MockConfigMgr* managerPtr =
402 new MockConfigMgr(bus, LDAP_CONFIG_ROOT, configFilePath.c_str(),
Nan Zhou78d85042022-08-29 17:50:22 +0000403 dbusPersistentFilePath.c_str(),
404 tlsCACertFilePath.c_str(), tlsCertFilePath.c_str());
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530405 EXPECT_CALL(*managerPtr, stopService("nslcd.service")).Times(1);
Alexander Filippov372c5662021-06-30 20:23:39 +0300406 EXPECT_CALL(*managerPtr, restartService("nslcd.service")).Times(3);
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530407 EXPECT_CALL(*managerPtr, restartService("nscd.service")).Times(1);
Ratan Guptaaeaf9412019-02-11 04:41:52 -0600408 managerPtr->createConfig(
409 "ldap://9.194.251.138/", "cn=Users,dc=com", "cn=Users,dc=corp",
410 "MyLdap12", ldap_base::Create::SearchScope::sub,
411 ldap_base::Create::Type::ActiveDirectory, "attr1", "attr2");
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530412 managerPtr->getADConfigPtr()->enabled(true);
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600413 // Change LDAP BaseDN
Patrick Williamse6500a42021-05-01 05:58:23 -0500414 managerPtr->getADConfigPtr()->ldapBaseDN(
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600415 "cn=Administrator,cn=Users,dc=corp,dc=ibm,dc=com");
Patrick Williamse6500a42021-05-01 05:58:23 -0500416 EXPECT_EQ(managerPtr->getADConfigPtr()->ldapBaseDN(),
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600417 "cn=Administrator,cn=Users,dc=corp,dc=ibm,dc=com");
418 // Change LDAP BaseDN
419 EXPECT_THROW(
420 {
421 try
422 {
Patrick Williamse6500a42021-05-01 05:58:23 -0500423 managerPtr->getADConfigPtr()->ldapBaseDN("");
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600424 }
425 catch (const InvalidArgument& e)
426 {
427 throw;
428 }
429 },
430 InvalidArgument);
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600431
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530432 managerPtr->restore();
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600433 // Check LDAP BaseDN after restoring
Patrick Williamse6500a42021-05-01 05:58:23 -0500434 EXPECT_EQ(managerPtr->getADConfigPtr()->ldapBaseDN(),
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600435 "cn=Administrator,cn=Users,dc=corp,dc=ibm,dc=com");
436 delete managerPtr;
437}
438
439TEST_F(TestLDAPConfig, testSearchScope)
440{
Nan Zhou78d85042022-08-29 17:50:22 +0000441 auto configFilePath = std::string(dir.c_str()) + "/" + ldapConfFile;
442 auto tlsCACertFilePath = std::string(dir.c_str()) + "/" + tlsCACertFile;
443 auto tlsCertFilePath = std::string(dir.c_str()) + "/" + tlsCertFile;
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530444 auto dbusPersistentFilePath = std::string(dir.c_str());
Nagaraju Goruganti3b4d06a2018-11-08 03:13:38 -0600445
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600446 if (fs::exists(configFilePath))
447 {
448 fs::remove(configFilePath);
449 }
450 EXPECT_FALSE(fs::exists(configFilePath));
Ratan Gupta22f13f12019-04-29 15:36:40 +0530451 MockConfigMgr* managerPtr =
452 new MockConfigMgr(bus, LDAP_CONFIG_ROOT, configFilePath.c_str(),
Nan Zhou78d85042022-08-29 17:50:22 +0000453 dbusPersistentFilePath.c_str(),
454 tlsCACertFilePath.c_str(), tlsCertFilePath.c_str());
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530455 EXPECT_CALL(*managerPtr, stopService("nslcd.service")).Times(1);
Alexander Filippov372c5662021-06-30 20:23:39 +0300456 EXPECT_CALL(*managerPtr, restartService("nslcd.service")).Times(3);
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530457 EXPECT_CALL(*managerPtr, restartService("nscd.service")).Times(1);
Ratan Guptaaeaf9412019-02-11 04:41:52 -0600458 managerPtr->createConfig(
459 "ldap://9.194.251.138/", "cn=Users,dc=com", "cn=Users,dc=corp",
460 "MyLdap12", ldap_base::Create::SearchScope::sub,
461 ldap_base::Create::Type::ActiveDirectory, "attr1", "attr2");
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530462 managerPtr->getADConfigPtr()->enabled(true);
Ratan Gupta95a29312019-02-18 20:34:10 +0530463
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600464 // Change LDAP SearchScope
Patrick Williamse6500a42021-05-01 05:58:23 -0500465 managerPtr->getADConfigPtr()->ldapSearchScope(
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600466 ldap_base::Config::SearchScope::one);
Patrick Williamse6500a42021-05-01 05:58:23 -0500467 EXPECT_EQ(managerPtr->getADConfigPtr()->ldapSearchScope(),
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600468 ldap_base::Config::SearchScope::one);
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600469
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530470 managerPtr->restore();
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600471 // Check LDAP SearchScope after restoring
Patrick Williamse6500a42021-05-01 05:58:23 -0500472 EXPECT_EQ(managerPtr->getADConfigPtr()->ldapSearchScope(),
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600473 ldap_base::Config::SearchScope::one);
474 delete managerPtr;
475}
476
477TEST_F(TestLDAPConfig, testLDAPType)
478{
Nan Zhou78d85042022-08-29 17:50:22 +0000479 auto configFilePath = std::string(dir.c_str()) + "/" + ldapConfFile;
480 auto tlsCACertFilePath = std::string(dir.c_str()) + "/" + tlsCACertFile;
481 auto tlsCertFilePath = std::string(dir.c_str()) + "/" + tlsCertFile;
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530482 auto dbusPersistentFilePath = std::string(dir.c_str());
Nagaraju Goruganti3b4d06a2018-11-08 03:13:38 -0600483
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600484 if (fs::exists(configFilePath))
485 {
486 fs::remove(configFilePath);
487 }
488 EXPECT_FALSE(fs::exists(configFilePath));
Ratan Gupta22f13f12019-04-29 15:36:40 +0530489 MockConfigMgr* managerPtr =
490 new MockConfigMgr(bus, LDAP_CONFIG_ROOT, configFilePath.c_str(),
Nan Zhou78d85042022-08-29 17:50:22 +0000491 dbusPersistentFilePath.c_str(),
492 tlsCACertFilePath.c_str(), tlsCertFilePath.c_str());
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530493 EXPECT_CALL(*managerPtr, stopService("nslcd.service")).Times(1);
Alexander Filippov372c5662021-06-30 20:23:39 +0300494 EXPECT_CALL(*managerPtr, restartService("nslcd.service")).Times(2);
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530495 EXPECT_CALL(*managerPtr, restartService("nscd.service")).Times(1);
Ratan Guptaaeaf9412019-02-11 04:41:52 -0600496 managerPtr->createConfig(
497 "ldap://9.194.251.138/", "cn=Users,dc=com", "cn=Users,dc=corp",
498 "MyLdap12", ldap_base::Create::SearchScope::sub,
499 ldap_base::Create::Type::ActiveDirectory, "attr1", "attr2");
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530500 managerPtr->getADConfigPtr()->enabled(true);
Ratan Gupta95a29312019-02-18 20:34:10 +0530501
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600502 // Change LDAP type
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530503 // will not be changed
Patrick Williamse6500a42021-05-01 05:58:23 -0500504 EXPECT_THROW(managerPtr->getADConfigPtr()->ldapType(
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530505 ldap_base::Config::Type::OpenLdap),
506 NotAllowed);
Patrick Williamse6500a42021-05-01 05:58:23 -0500507 EXPECT_EQ(managerPtr->getADConfigPtr()->ldapType(),
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530508 ldap_base::Config::Type::ActiveDirectory);
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600509
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530510 managerPtr->restore();
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600511 // Check LDAP type after restoring
Patrick Williamse6500a42021-05-01 05:58:23 -0500512 EXPECT_EQ(managerPtr->getADConfigPtr()->ldapType(),
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530513 ldap_base::Config::Type::ActiveDirectory);
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600514 delete managerPtr;
515}
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530516
Ravi Tejad5884042019-06-10 02:35:22 -0500517TEST_F(TestLDAPConfig, testsecureLDAPRestore)
518{
Nan Zhou78d85042022-08-29 17:50:22 +0000519 auto configFilePath = std::string(dir.c_str()) + "/" + ldapConfFile;
520 auto tlsCACertFilePath = std::string(dir.c_str()) + "/" + tlsCACertFile;
521 auto tlsCertFilePath = std::string(dir.c_str()) + "/" + tlsCertFile;
Ravi Tejad5884042019-06-10 02:35:22 -0500522 auto dbusPersistentFilePath = std::string(dir.c_str());
523
524 if (fs::exists(configFilePath))
525 {
526 fs::remove(configFilePath);
527 }
528 EXPECT_FALSE(fs::exists(configFilePath));
529 MockConfigMgr* managerPtr =
530 new MockConfigMgr(bus, LDAP_CONFIG_ROOT, configFilePath.c_str(),
Nan Zhou78d85042022-08-29 17:50:22 +0000531 dbusPersistentFilePath.c_str(),
532 tlsCACertFilePath.c_str(), tlsCertFilePath.c_str());
Ravi Tejad5884042019-06-10 02:35:22 -0500533 EXPECT_CALL(*managerPtr, stopService("nslcd.service")).Times(1);
Alexander Filippov372c5662021-06-30 20:23:39 +0300534 EXPECT_CALL(*managerPtr, restartService("nslcd.service")).Times(2);
Ravi Tejad5884042019-06-10 02:35:22 -0500535 EXPECT_CALL(*managerPtr, restartService("nscd.service")).Times(1);
536 managerPtr->createConfig(
537 "ldaps://9.194.251.138/", "cn=Users,dc=com", "cn=Users,dc=corp",
538 "MyLdap12", ldap_base::Create::SearchScope::sub,
539 ldap_base::Create::Type::ActiveDirectory, "attr1", "attr2");
540 managerPtr->getADConfigPtr()->enabled(true);
541 EXPECT_TRUE(managerPtr->secureLDAP());
542 managerPtr->restore();
543 // Check secureLDAP variable value after restoring
544 EXPECT_TRUE(managerPtr->secureLDAP());
545
546 delete managerPtr;
547}
548
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530549TEST_F(TestLDAPConfig, filePermission)
550{
Nan Zhou78d85042022-08-29 17:50:22 +0000551 auto configFilePath = std::string(dir.c_str()) + "/" + ldapConfFile;
552 auto tlsCACertFilePath = std::string(dir.c_str()) + "/" + tlsCACertFile;
553 auto tlsCertFilePath = std::string(dir.c_str()) + "/" + tlsCertFile;
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530554 auto dbusPersistentFilePath = std::string(dir.c_str());
555
556 if (fs::exists(configFilePath))
557 {
558 fs::remove(configFilePath);
559 }
560 EXPECT_FALSE(fs::exists(configFilePath));
Ratan Gupta22f13f12019-04-29 15:36:40 +0530561 MockConfigMgr* managerPtr =
562 new MockConfigMgr(bus, LDAP_CONFIG_ROOT, configFilePath.c_str(),
Nan Zhou78d85042022-08-29 17:50:22 +0000563 dbusPersistentFilePath.c_str(),
564 tlsCACertFilePath.c_str(), tlsCertFilePath.c_str());
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530565 EXPECT_CALL(*managerPtr, stopService("nslcd.service")).Times(1);
566 EXPECT_CALL(*managerPtr, restartService("nslcd.service")).Times(1);
567 EXPECT_CALL(*managerPtr, restartService("nscd.service")).Times(1);
568 managerPtr->createConfig(
569 "ldap://9.194.251.138/", "cn=Users,dc=com", "cn=Users,dc=corp",
570 "MyLdap12", ldap_base::Create::SearchScope::sub,
571 ldap_base::Create::Type::ActiveDirectory, "attr1", "attr2");
572 managerPtr->getADConfigPtr()->enabled(true);
573
574 // Permission of the persistent file should be 640
575 // Others should not be allowed to read.
576 auto permission =
577 fs::perms::owner_read | fs::perms::owner_write | fs::perms::group_read;
578 auto persistFilepath = std::string(dir.c_str());
Nan Zhou78d85042022-08-29 17:50:22 +0000579 persistFilepath += adDbusObjectPath;
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530580 persistFilepath += "/config";
581
582 EXPECT_EQ(fs::status(persistFilepath).permissions(), permission);
583 delete managerPtr;
584}
585
Ratan Guptac5481d12019-04-12 18:31:05 +0530586TEST_F(TestLDAPConfig, ConditionalEnableConfig)
587{
Nan Zhou78d85042022-08-29 17:50:22 +0000588 auto configFilePath = std::string(dir.c_str()) + "/" + ldapConfFile;
589 auto tlsCACertFilePath = std::string(dir.c_str()) + "/" + tlsCACertFile;
590 auto tlsCertFilePath = std::string(dir.c_str()) + "/" + tlsCertFile;
Ratan Guptac5481d12019-04-12 18:31:05 +0530591 auto dbusPersistentFilePath = std::string(dir.c_str());
592
593 if (fs::exists(configFilePath))
594 {
595 fs::remove(configFilePath);
596 }
597 EXPECT_FALSE(fs::exists(configFilePath));
Ratan Gupta22f13f12019-04-29 15:36:40 +0530598 MockConfigMgr* managerPtr =
599 new MockConfigMgr(bus, LDAP_CONFIG_ROOT, configFilePath.c_str(),
Nan Zhou78d85042022-08-29 17:50:22 +0000600 dbusPersistentFilePath.c_str(),
601 tlsCACertFilePath.c_str(), tlsCertFilePath.c_str());
Ratan Guptac5481d12019-04-12 18:31:05 +0530602 EXPECT_CALL(*managerPtr, stopService("nslcd.service")).Times(3);
603 EXPECT_CALL(*managerPtr, restartService("nslcd.service")).Times(2);
604 EXPECT_CALL(*managerPtr, restartService("nscd.service")).Times(2);
605 managerPtr->createConfig(
606 "ldap://9.194.251.138/", "cn=Users,dc=com", "cn=Users,dc=corp",
607 "MyLdap12", ldap_base::Create::SearchScope::sub,
608 ldap_base::Create::Type::ActiveDirectory, "attr1", "attr2");
609
610 managerPtr->createConfig(
611 "ldap://9.194.251.139/", "cn=Users,dc=com, dc=ldap", "cn=Users,dc=corp",
612 "MyLdap123", ldap_base::Create::SearchScope::sub,
613 ldap_base::Create::Type::OpenLdap, "attr1", "attr2");
614
615 // Enable the AD configuration
616 managerPtr->getADConfigPtr()->enabled(true);
617
618 EXPECT_EQ(managerPtr->getADConfigPtr()->enabled(), true);
619 EXPECT_EQ(managerPtr->getOpenLdapConfigPtr()->enabled(), false);
620
621 // AS AD is already enabled so openldap can't be enabled.
622 EXPECT_THROW(
623 {
624 try
625 {
626 managerPtr->getOpenLdapConfigPtr()->enabled(true);
627 }
628 catch (const NotAllowed& e)
629 {
630 throw;
631 }
632 },
633 NotAllowed);
634 // Check the values
635 EXPECT_EQ(managerPtr->getADConfigPtr()->enabled(), true);
636 EXPECT_EQ(managerPtr->getOpenLdapConfigPtr()->enabled(), false);
637 // Let's disable the AD.
638 managerPtr->getADConfigPtr()->enabled(false);
639 EXPECT_EQ(managerPtr->getADConfigPtr()->enabled(), false);
640 EXPECT_EQ(managerPtr->getOpenLdapConfigPtr()->enabled(), false);
641 // Now enable the openldap
642 managerPtr->getOpenLdapConfigPtr()->enabled(true);
643 EXPECT_EQ(managerPtr->getOpenLdapConfigPtr()->enabled(), true);
644 EXPECT_EQ(managerPtr->getADConfigPtr()->enabled(), false);
645
646 delete managerPtr;
647}
648
Ratan Guptafef57892019-04-14 13:43:09 +0530649TEST_F(TestLDAPConfig, createPrivMapping)
650{
Nan Zhou78d85042022-08-29 17:50:22 +0000651 auto configFilePath = std::string(dir.c_str()) + "/" + ldapConfFile;
652 auto tlsCACertFilePath = std::string(dir.c_str()) + "/" + tlsCACertFile;
653 auto tlsCertFilePath = std::string(dir.c_str()) + "/" + tlsCertFile;
Ratan Guptafef57892019-04-14 13:43:09 +0530654 auto dbusPersistentFilePath = std::string(dir.c_str());
655
656 if (fs::exists(configFilePath))
657 {
658 fs::remove(configFilePath);
659 }
660 EXPECT_FALSE(fs::exists(configFilePath));
661 MockConfigMgr manager(bus, LDAP_CONFIG_ROOT, configFilePath.c_str(),
Nan Zhou78d85042022-08-29 17:50:22 +0000662 dbusPersistentFilePath.c_str(),
663 tlsCACertFilePath.c_str(), tlsCertFilePath.c_str());
Ratan Guptafef57892019-04-14 13:43:09 +0530664 manager.createDefaultObjects();
665 // Create the priv-mapping under the config.
666 manager.getADConfigPtr()->create("admin", "priv-admin");
667 // Check whether the entry has been created.
668 EXPECT_THROW(
669 {
670 try
671 {
672 manager.getADConfigPtr()->checkPrivilegeMapper("admin");
673 }
674 catch (const PrivilegeMappingExists& e)
675 {
676 throw;
677 }
678 },
679 PrivilegeMappingExists);
680}
681
682TEST_F(TestLDAPConfig, deletePrivMapping)
683{
Nan Zhou78d85042022-08-29 17:50:22 +0000684 auto configFilePath = std::string(dir.c_str()) + "/" + ldapConfFile;
685 auto tlsCACertFilePath = std::string(dir.c_str()) + "/" + tlsCACertFile;
686 auto tlsCertFilePath = std::string(dir.c_str()) + "/" + tlsCertFile;
Ratan Guptafef57892019-04-14 13:43:09 +0530687 auto dbusPersistentFilePath = std::string(dir.c_str());
688
689 if (fs::exists(configFilePath))
690 {
691 fs::remove(configFilePath);
692 }
693 EXPECT_FALSE(fs::exists(configFilePath));
694 MockConfigMgr manager(bus, LDAP_CONFIG_ROOT, configFilePath.c_str(),
Nan Zhou78d85042022-08-29 17:50:22 +0000695 dbusPersistentFilePath.c_str(),
696 tlsCACertFilePath.c_str(), tlsCertFilePath.c_str());
Ratan Guptafef57892019-04-14 13:43:09 +0530697 manager.createDefaultObjects();
698 // Create the priv-mapping under the config.
699 manager.getADConfigPtr()->create("admin", "priv-admin");
700 manager.getADConfigPtr()->create("user", "priv-user");
701 // Check whether the entry has been created.
702 EXPECT_THROW(
703 {
704 try
705 {
706 manager.getADConfigPtr()->checkPrivilegeMapper("admin");
707 manager.getADConfigPtr()->checkPrivilegeMapper("user");
708 }
709 catch (const PrivilegeMappingExists& e)
710 {
711 throw;
712 }
713 },
714 PrivilegeMappingExists);
715
716 // This would delete the admin privilege
717 manager.getADConfigPtr()->deletePrivilegeMapper(1);
718 EXPECT_NO_THROW(manager.getADConfigPtr()->checkPrivilegeMapper("admin"));
719 manager.getADConfigPtr()->deletePrivilegeMapper(2);
720 EXPECT_NO_THROW(manager.getADConfigPtr()->checkPrivilegeMapper("user"));
721}
722
723TEST_F(TestLDAPConfig, restorePrivMapping)
724{
Nan Zhou78d85042022-08-29 17:50:22 +0000725 auto configFilePath = std::string(dir.c_str()) + "/" + ldapConfFile;
726 auto tlsCACertFilePath = std::string(dir.c_str()) + "/" + tlsCACertFile;
727 auto tlsCertFilePath = std::string(dir.c_str()) + "/" + tlsCertFile;
Ratan Guptafef57892019-04-14 13:43:09 +0530728 auto dbusPersistentFilePath = std::string(dir.c_str());
729
730 if (fs::exists(configFilePath))
731 {
732 fs::remove(configFilePath);
733 }
734 EXPECT_FALSE(fs::exists(configFilePath));
735 MockConfigMgr manager(bus, LDAP_CONFIG_ROOT, configFilePath.c_str(),
Nan Zhou78d85042022-08-29 17:50:22 +0000736 dbusPersistentFilePath.c_str(),
737 tlsCACertFilePath.c_str(), tlsCertFilePath.c_str());
Ratan Guptafef57892019-04-14 13:43:09 +0530738 manager.createDefaultObjects();
739 // Create the priv-mapping under the config.
740 manager.getADConfigPtr()->create("admin", "priv-admin");
741 manager.getOpenLdapConfigPtr()->create("user", "priv-user");
742 manager.restore();
743 EXPECT_THROW(
744 {
745 try
746 {
747 manager.getADConfigPtr()->checkPrivilegeMapper("admin");
748 }
749 catch (const PrivilegeMappingExists& e)
750 {
751 throw;
752 }
753 },
754 PrivilegeMappingExists);
755
756 EXPECT_THROW(
757 {
758 try
759 {
760 manager.getOpenLdapConfigPtr()->checkPrivilegeMapper("user");
761 }
762 catch (const PrivilegeMappingExists& e)
763 {
764 throw;
765 }
766 },
767 PrivilegeMappingExists);
768}
769
770TEST_F(TestLDAPConfig, testPrivileges)
771{
Nan Zhou78d85042022-08-29 17:50:22 +0000772 auto configFilePath = std::string(dir.c_str()) + "/" + ldapConfFile;
773 auto tlsCACertFilePath = std::string(dir.c_str()) + "/" + tlsCACertFile;
774 auto tlsCertFilePath = std::string(dir.c_str()) + "/" + tlsCertFile;
Ratan Guptafef57892019-04-14 13:43:09 +0530775 auto dbusPersistentFilePath = std::string(dir.c_str());
776
777 if (fs::exists(configFilePath))
778 {
779 fs::remove(configFilePath);
780 }
781 EXPECT_FALSE(fs::exists(configFilePath));
782 MockConfigMgr manager(bus, LDAP_CONFIG_ROOT, configFilePath.c_str(),
Nan Zhou78d85042022-08-29 17:50:22 +0000783 dbusPersistentFilePath.c_str(),
784 tlsCACertFilePath.c_str(), tlsCertFilePath.c_str());
Ratan Guptafef57892019-04-14 13:43:09 +0530785 manager.createDefaultObjects();
786
787 std::string groupName = "admin";
788 std::string privilege = "priv-admin";
789 size_t entryId = 1;
790 auto dbusPath = std::string(LDAP_CONFIG_ROOT) +
791 "/active_directory/role_map/" + std::to_string(entryId);
792 dbusPersistentFilePath += dbusPath;
793
794 auto entry = std::make_unique<LDAPMapperEntry>(
795 bus, dbusPath.c_str(), dbusPersistentFilePath.c_str(), groupName,
796 privilege, *(manager.getADConfigPtr()));
797
798 EXPECT_NO_THROW(entry->privilege("priv-operator"));
799 EXPECT_NO_THROW(entry->privilege("priv-user"));
Ratan Guptafef57892019-04-14 13:43:09 +0530800}
801
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600802} // namespace ldap
803} // namespace phosphor