blob: 6f7d1f7f75e425ea7c3595892b7bad6b04709e15 [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 Gupta95a29312019-02-18 20:34:10 +053031static constexpr const char* dbusPersistFile = "Config";
Ratan Guptafef57892019-04-14 13:43:09 +053032using PrivilegeMappingExists = sdbusplus::xyz::openbmc_project::User::Common::
33 Error::PrivilegeMappingExists;
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -060034
35class TestLDAPConfig : public testing::Test
36{
37 public:
38 TestLDAPConfig() : bus(sdbusplus::bus::new_default())
Patrick Williams9638afb2021-02-22 17:16:24 -060039 {}
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -060040 void SetUp() override
41 {
42 using namespace phosphor::ldap;
43 char tmpldap[] = "/tmp/ldap_test.XXXXXX";
44 dir = fs::path(mkdtemp(tmpldap));
Zbigniew Kurzynski5d00cf22019-10-03 12:10:20 +020045 fs::path tlsCacertFilePath{TLS_CACERT_PATH};
Ratan Gupta22f13f12019-04-29 15:36:40 +053046 tlsCacertFile = tlsCacertFilePath.filename().c_str();
47 fs::path tlsCertFilePath{TLS_CERT_FILE};
48 tlsCertFile = tlsCertFilePath.filename().c_str();
49
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -060050 fs::path confFilePath{LDAP_CONFIG_FILE};
51 ldapconfFile = confFilePath.filename().c_str();
52 std::fstream fs;
53 fs.open(dir / defaultNslcdFile, std::fstream::out);
54 fs.close();
55 fs.open(dir / nsSwitchFile, std::fstream::out);
56 fs.close();
Ratan Gupta22f13f12019-04-29 15:36:40 +053057 fs.open(dir / tlsCacertFile, std::fstream::out);
58 fs.close();
59 fs.open(dir / tlsCertFile, std::fstream::out);
60 fs.close();
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -060061 }
62
63 void TearDown() override
64 {
65 fs::remove_all(dir);
66 }
67
68 protected:
69 fs::path dir;
Ratan Gupta22f13f12019-04-29 15:36:40 +053070 std::string tlsCacertFile;
71 std::string tlsCertFile;
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -060072 std::string ldapconfFile;
Patrick Williamsb3ef4e12022-07-22 19:26:55 -050073 sdbusplus::bus_t bus;
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -060074};
75
76class MockConfigMgr : public phosphor::ldap::ConfigMgr
77{
78 public:
Patrick Williamsb3ef4e12022-07-22 19:26:55 -050079 MockConfigMgr(sdbusplus::bus_t& bus, const char* path, const char* filePath,
80 const char* dbusPersistentFile, const char* caCertFile,
81 const char* certFile) :
Ratan Gupta95a29312019-02-18 20:34:10 +053082 phosphor::ldap::ConfigMgr(bus, path, filePath, dbusPersistentFile,
Ratan Gupta22f13f12019-04-29 15:36:40 +053083 caCertFile, certFile)
Patrick Williams9638afb2021-02-22 17:16:24 -060084 {}
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -060085 MOCK_METHOD1(restartService, void(const std::string& service));
86 MOCK_METHOD1(stopService, void(const std::string& service));
Ratan Gupta27d4c012019-04-12 13:03:35 +053087 std::unique_ptr<Config>& getOpenLdapConfigPtr()
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -060088 {
Ratan Gupta27d4c012019-04-12 13:03:35 +053089 return openLDAPConfigPtr;
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -060090 }
91
Ratan Gupta3a1c2742019-03-20 06:49:42 +053092 std::string configBindPassword()
93 {
Patrick Williamse6500a42021-05-01 05:58:23 -050094 return getADConfigPtr()->ldapBindPassword;
Ratan Gupta3a1c2742019-03-20 06:49:42 +053095 }
96
Ratan Gupta27d4c012019-04-12 13:03:35 +053097 std::unique_ptr<Config>& getADConfigPtr()
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -060098 {
Ratan Gupta27d4c012019-04-12 13:03:35 +053099 return ADConfigPtr;
100 }
101 void restore()
102 {
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530103 phosphor::ldap::ConfigMgr::restore();
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600104 return;
105 }
106
Ratan Gupta27d4c012019-04-12 13:03:35 +0530107 void createDefaultObjects()
108 {
109 phosphor::ldap::ConfigMgr::createDefaultObjects();
110 }
111
Ravi Tejad5884042019-06-10 02:35:22 -0500112 bool secureLDAP()
113 {
114 return ADConfigPtr->secureLDAP;
115 }
116
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600117 friend class TestLDAPConfig;
118};
119
120TEST_F(TestLDAPConfig, testCreate)
121{
122 auto configFilePath = std::string(dir.c_str()) + "/" + ldapconfFile;
Ratan Gupta22f13f12019-04-29 15:36:40 +0530123 auto tlsCacertfile = std::string(dir.c_str()) + "/" + tlsCacertFile;
124 auto tlsCertfile = std::string(dir.c_str()) + "/" + tlsCertFile;
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530125 auto dbusPersistentFilePath = std::string(dir.c_str());
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600126
127 if (fs::exists(configFilePath))
128 {
129 fs::remove(configFilePath);
130 }
131 EXPECT_FALSE(fs::exists(configFilePath));
Nagaraju Goruganti3b4d06a2018-11-08 03:13:38 -0600132 MockConfigMgr manager(bus, LDAP_CONFIG_ROOT, configFilePath.c_str(),
Ratan Gupta22f13f12019-04-29 15:36:40 +0530133 dbusPersistentFilePath.c_str(), tlsCacertfile.c_str(),
134 tlsCertfile.c_str());
Ratan Gupta27d4c012019-04-12 13:03:35 +0530135
Ratan Guptaec117542019-04-25 18:38:29 +0530136 EXPECT_CALL(manager, stopService("nslcd.service")).Times(2);
Ratan Gupta3a1c2742019-03-20 06:49:42 +0530137 EXPECT_CALL(manager, restartService("nslcd.service")).Times(2);
Ratan Guptaec117542019-04-25 18:38:29 +0530138 EXPECT_CALL(manager, restartService("nscd.service")).Times(2);
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530139
Ratan Guptaaeaf9412019-02-11 04:41:52 -0600140 manager.createConfig(
141 "ldap://9.194.251.136/", "cn=Users,dc=com", "cn=Users,dc=corp",
142 "MyLdap12", ldap_base::Create::SearchScope::sub,
143 ldap_base::Create::Type::ActiveDirectory, "uid", "gid");
Ratan Gupta27d4c012019-04-12 13:03:35 +0530144 manager.getADConfigPtr()->enabled(true);
Ratan Gupta95a29312019-02-18 20:34:10 +0530145
Ratan Guptaec117542019-04-25 18:38:29 +0530146 manager.createConfig("ldap://9.194.251.137/", "cn=Users",
147 "cn=Users,dc=test", "MyLdap123",
148 ldap_base::Create::SearchScope::sub,
149 ldap_base::Create::Type::OpenLdap, "uid", "gid");
150 manager.getOpenLdapConfigPtr()->enabled(false);
151
152 // Below setting of username/groupname attr is to make sure
153 // that in-active config should not call the start/stop service.
154 manager.getOpenLdapConfigPtr()->userNameAttribute("abc");
155 EXPECT_EQ(manager.getOpenLdapConfigPtr()->userNameAttribute(), "abc");
156
157 manager.getOpenLdapConfigPtr()->groupNameAttribute("def");
158 EXPECT_EQ(manager.getOpenLdapConfigPtr()->groupNameAttribute(), "def");
159
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600160 EXPECT_TRUE(fs::exists(configFilePath));
Patrick Williamse6500a42021-05-01 05:58:23 -0500161 EXPECT_EQ(manager.getADConfigPtr()->ldapServerURI(),
Ratan Gupta27d4c012019-04-12 13:03:35 +0530162 "ldap://9.194.251.136/");
Patrick Williamse6500a42021-05-01 05:58:23 -0500163 EXPECT_EQ(manager.getADConfigPtr()->ldapBindDN(), "cn=Users,dc=com");
164 EXPECT_EQ(manager.getADConfigPtr()->ldapBaseDN(), "cn=Users,dc=corp");
165 EXPECT_EQ(manager.getADConfigPtr()->ldapSearchScope(),
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600166 ldap_base::Config::SearchScope::sub);
Patrick Williamse6500a42021-05-01 05:58:23 -0500167 EXPECT_EQ(manager.getADConfigPtr()->ldapType(),
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600168 ldap_base::Config::Type::ActiveDirectory);
Ratan Gupta27d4c012019-04-12 13:03:35 +0530169
170 EXPECT_EQ(manager.getADConfigPtr()->userNameAttribute(), "uid");
171 EXPECT_EQ(manager.getADConfigPtr()->groupNameAttribute(), "gid");
Patrick Williamse6500a42021-05-01 05:58:23 -0500172 EXPECT_EQ(manager.getADConfigPtr()->ldapBindDNPassword(), "");
Ratan Gupta3a1c2742019-03-20 06:49:42 +0530173 EXPECT_EQ(manager.configBindPassword(), "MyLdap12");
174 // change the password
Patrick Williamse6500a42021-05-01 05:58:23 -0500175 manager.getADConfigPtr()->ldapBindDNPassword("MyLdap14");
176 EXPECT_EQ(manager.getADConfigPtr()->ldapBindDNPassword(), "");
Ratan Gupta3a1c2742019-03-20 06:49:42 +0530177 EXPECT_EQ(manager.configBindPassword(), "MyLdap14");
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600178}
179
Ratan Gupta27d4c012019-04-12 13:03:35 +0530180TEST_F(TestLDAPConfig, testDefaultObject)
181{
182 auto configFilePath = std::string(dir.c_str()) + "/" + ldapconfFile;
Ratan Gupta22f13f12019-04-29 15:36:40 +0530183 auto tlsCacertfile = std::string(dir.c_str()) + "/" + tlsCacertFile;
184 auto tlsCertfile = std::string(dir.c_str()) + "/" + tlsCertFile;
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530185 auto dbusPersistentFilePath = std::string(dir.c_str());
Ratan Gupta27d4c012019-04-12 13:03:35 +0530186
187 if (fs::exists(configFilePath))
188 {
189 fs::remove(configFilePath);
190 }
191 EXPECT_FALSE(fs::exists(configFilePath));
192
193 MockConfigMgr manager(bus, LDAP_CONFIG_ROOT, configFilePath.c_str(),
Ratan Gupta22f13f12019-04-29 15:36:40 +0530194 dbusPersistentFilePath.c_str(), tlsCacertfile.c_str(),
195 tlsCertfile.c_str());
Ratan Gupta27d4c012019-04-12 13:03:35 +0530196
Ratan Gupta27d4c012019-04-12 13:03:35 +0530197 manager.createDefaultObjects();
198
199 EXPECT_NE(nullptr, manager.getADConfigPtr());
200 EXPECT_NE(nullptr, manager.getOpenLdapConfigPtr());
Patrick Williamse6500a42021-05-01 05:58:23 -0500201 EXPECT_EQ(manager.getADConfigPtr()->ldapType(),
Ratan Gupta27d4c012019-04-12 13:03:35 +0530202 ldap_base::Config::Type::ActiveDirectory);
Patrick Williamse6500a42021-05-01 05:58:23 -0500203 EXPECT_EQ(manager.getOpenLdapConfigPtr()->ldapType(),
Ratan Gupta27d4c012019-04-12 13:03:35 +0530204 ldap_base::Config::Type::OpenLdap);
205}
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530206
Alexander Filippov372c5662021-06-30 20:23:39 +0300207TEST_F(TestLDAPConfig, testRestoresDefault)
208{
209 auto configFilePath = std::string(dir.c_str()) + "/" + ldapconfFile;
210 auto tlsCacertfile = std::string(dir.c_str()) + "/" + tlsCacertFile;
211 auto tlsCertfile = std::string(dir.c_str()) + "/" + tlsCertFile;
212 auto dbusPersistentFilePath = std::string(dir.c_str());
213
214 if (fs::exists(configFilePath))
215 {
216 fs::remove(configFilePath);
217 }
218 EXPECT_FALSE(fs::exists(configFilePath));
219
220 MockConfigMgr manager(bus, LDAP_CONFIG_ROOT, configFilePath.c_str(),
221 dbusPersistentFilePath.c_str(), tlsCacertfile.c_str(),
222 tlsCertfile.c_str());
223
224 EXPECT_CALL(manager, stopService("nslcd.service")).Times(1);
225 EXPECT_CALL(manager, restartService("nslcd.service")).Times(0);
226 EXPECT_CALL(manager, restartService("nscd.service")).Times(0);
227
228 manager.restore();
229
230 EXPECT_NE(nullptr, manager.getADConfigPtr());
231 EXPECT_NE(nullptr, manager.getOpenLdapConfigPtr());
232 EXPECT_EQ(manager.getADConfigPtr()->ldapType(),
233 ldap_base::Config::Type::ActiveDirectory);
234 EXPECT_EQ(manager.getOpenLdapConfigPtr()->ldapType(),
235 ldap_base::Config::Type::OpenLdap);
236 EXPECT_FALSE(manager.getADConfigPtr()->enabled());
237 EXPECT_FALSE(manager.getOpenLdapConfigPtr()->enabled());
238}
239
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600240TEST_F(TestLDAPConfig, testRestores)
241{
242 auto configFilePath = std::string(dir.c_str()) + "/" + ldapconfFile;
Ratan Gupta22f13f12019-04-29 15:36:40 +0530243 auto tlsCacertfile = std::string(dir.c_str()) + "/" + tlsCacertFile;
244 auto tlsCertfile = std::string(dir.c_str()) + "/" + tlsCertFile;
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530245 auto dbusPersistentFilePath = std::string(dir.c_str());
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600246
247 if (fs::exists(configFilePath))
248 {
249 fs::remove(configFilePath);
250 }
251 EXPECT_FALSE(fs::exists(configFilePath));
Ratan Gupta22f13f12019-04-29 15:36:40 +0530252 MockConfigMgr* managerPtr =
253 new MockConfigMgr(bus, LDAP_CONFIG_ROOT, configFilePath.c_str(),
254 dbusPersistentFilePath.c_str(), tlsCacertfile.c_str(),
255 tlsCertfile.c_str());
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530256 EXPECT_CALL(*managerPtr, stopService("nslcd.service")).Times(1);
Alexander Filippov372c5662021-06-30 20:23:39 +0300257 EXPECT_CALL(*managerPtr, restartService("nslcd.service")).Times(2);
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530258 EXPECT_CALL(*managerPtr, restartService("nscd.service")).Times(1);
Ratan Guptaaeaf9412019-02-11 04:41:52 -0600259 managerPtr->createConfig(
260 "ldap://9.194.251.138/", "cn=Users,dc=com", "cn=Users,dc=corp",
261 "MyLdap12", ldap_base::Create::SearchScope::sub,
262 ldap_base::Create::Type::ActiveDirectory, "uid", "gid");
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530263 managerPtr->getADConfigPtr()->enabled(false);
264 EXPECT_FALSE(fs::exists(configFilePath));
265 EXPECT_FALSE(managerPtr->getADConfigPtr()->enabled());
266 managerPtr->getADConfigPtr()->enabled(true);
Ratan Gupta95a29312019-02-18 20:34:10 +0530267
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600268 EXPECT_TRUE(fs::exists(configFilePath));
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600269 // Restore from configFilePath
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530270 managerPtr->restore();
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600271 // validate restored properties
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530272 EXPECT_TRUE(managerPtr->getADConfigPtr()->enabled());
Patrick Williamse6500a42021-05-01 05:58:23 -0500273 EXPECT_EQ(managerPtr->getADConfigPtr()->ldapServerURI(),
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600274 "ldap://9.194.251.138/");
Patrick Williamse6500a42021-05-01 05:58:23 -0500275 EXPECT_EQ(managerPtr->getADConfigPtr()->ldapBindDN(), "cn=Users,dc=com");
276 EXPECT_EQ(managerPtr->getADConfigPtr()->ldapBaseDN(), "cn=Users,dc=corp");
277 EXPECT_EQ(managerPtr->getADConfigPtr()->ldapSearchScope(),
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600278 ldap_base::Config::SearchScope::sub);
Patrick Williamse6500a42021-05-01 05:58:23 -0500279 EXPECT_EQ(managerPtr->getADConfigPtr()->ldapType(),
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600280 ldap_base::Config::Type::ActiveDirectory);
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530281 EXPECT_EQ(managerPtr->getADConfigPtr()->userNameAttribute(), "uid");
282 EXPECT_EQ(managerPtr->getADConfigPtr()->groupNameAttribute(), "gid");
Patrick Williamse6500a42021-05-01 05:58:23 -0500283 EXPECT_EQ(managerPtr->getADConfigPtr()->ldapBindDNPassword(), "");
Ratan Gupta3a1c2742019-03-20 06:49:42 +0530284 EXPECT_EQ(managerPtr->configBindPassword(), "MyLdap12");
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600285 delete managerPtr;
286}
287
288TEST_F(TestLDAPConfig, testLDAPServerURI)
289{
290 auto configFilePath = std::string(dir.c_str()) + "/" + ldapconfFile;
Ratan Gupta22f13f12019-04-29 15:36:40 +0530291 auto tlsCacertfile = std::string(dir.c_str()) + "/" + tlsCacertFile;
292 auto tlsCertfile = std::string(dir.c_str()) + "/" + tlsCertFile;
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530293 auto dbusPersistentFilePath = std::string(dir.c_str());
Nagaraju Goruganti3b4d06a2018-11-08 03:13:38 -0600294
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600295 if (fs::exists(configFilePath))
296 {
297 fs::remove(configFilePath);
298 }
299 EXPECT_FALSE(fs::exists(configFilePath));
Ratan Gupta22f13f12019-04-29 15:36:40 +0530300 MockConfigMgr* managerPtr =
301 new MockConfigMgr(bus, LDAP_CONFIG_ROOT, configFilePath.c_str(),
302 dbusPersistentFilePath.c_str(), tlsCacertfile.c_str(),
303 tlsCertfile.c_str());
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530304
305 EXPECT_CALL(*managerPtr, stopService("nslcd.service")).Times(1);
Alexander Filippov372c5662021-06-30 20:23:39 +0300306 EXPECT_CALL(*managerPtr, restartService("nslcd.service")).Times(3);
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530307 EXPECT_CALL(*managerPtr, restartService("nscd.service")).Times(1);
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600308
Ratan Guptaaeaf9412019-02-11 04:41:52 -0600309 managerPtr->createConfig(
310 "ldap://9.194.251.138/", "cn=Users,dc=com", "cn=Users,dc=corp",
311 "MyLdap12", ldap_base::Create::SearchScope::sub,
312 ldap_base::Create::Type::ActiveDirectory, "attr1", "attr2");
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530313 managerPtr->getADConfigPtr()->enabled(true);
Ratan Gupta95a29312019-02-18 20:34:10 +0530314
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600315 // Change LDAP Server URI
Patrick Williamse6500a42021-05-01 05:58:23 -0500316 managerPtr->getADConfigPtr()->ldapServerURI("ldap://9.194.251.139/");
317 EXPECT_EQ(managerPtr->getADConfigPtr()->ldapServerURI(),
Nagaraju Goruganti3b4d06a2018-11-08 03:13:38 -0600318 "ldap://9.194.251.139/");
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530319
Ratan Gupta22f13f12019-04-29 15:36:40 +0530320 fs::remove(tlsCacertfile.c_str());
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530321 // Change LDAP Server URI to make it secure
Nagaraju Goruganti3b4d06a2018-11-08 03:13:38 -0600322 EXPECT_THROW(
Patrick Williamse6500a42021-05-01 05:58:23 -0500323 managerPtr->getADConfigPtr()->ldapServerURI("ldaps://9.194.251.139/"),
Nagaraju Goruganti3b4d06a2018-11-08 03:13:38 -0600324 NoCACertificate);
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600325
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530326 // check once again
Patrick Williamse6500a42021-05-01 05:58:23 -0500327 EXPECT_EQ(managerPtr->getADConfigPtr()->ldapServerURI(),
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530328 "ldap://9.194.251.139/");
329
330 managerPtr->restore();
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600331 // Check LDAP Server URI
Patrick Williamse6500a42021-05-01 05:58:23 -0500332 EXPECT_EQ(managerPtr->getADConfigPtr()->ldapServerURI(),
Nagaraju Goruganti3b4d06a2018-11-08 03:13:38 -0600333 "ldap://9.194.251.139/");
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600334 delete managerPtr;
335}
336
337TEST_F(TestLDAPConfig, testLDAPBindDN)
338{
339 auto configFilePath = std::string(dir.c_str()) + "/" + ldapconfFile;
Ratan Gupta22f13f12019-04-29 15:36:40 +0530340 auto tlsCacertfile = std::string(dir.c_str()) + "/" + tlsCacertFile;
341 auto tlsCertfile = std::string(dir.c_str()) + "/" + tlsCertFile;
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530342 auto dbusPersistentFilePath = std::string(dir.c_str());
Nagaraju Goruganti3b4d06a2018-11-08 03:13:38 -0600343
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600344 if (fs::exists(configFilePath))
345 {
346 fs::remove(configFilePath);
347 }
348 EXPECT_FALSE(fs::exists(configFilePath));
Ratan Gupta22f13f12019-04-29 15:36:40 +0530349 MockConfigMgr* managerPtr =
350 new MockConfigMgr(bus, LDAP_CONFIG_ROOT, configFilePath.c_str(),
351 dbusPersistentFilePath.c_str(), tlsCacertfile.c_str(),
352 tlsCertfile.c_str());
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530353
354 EXPECT_CALL(*managerPtr, stopService("nslcd.service")).Times(1);
Alexander Filippov372c5662021-06-30 20:23:39 +0300355 EXPECT_CALL(*managerPtr, restartService("nslcd.service")).Times(3);
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530356 EXPECT_CALL(*managerPtr, restartService("nscd.service")).Times(1);
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600357
Ratan Guptaaeaf9412019-02-11 04:41:52 -0600358 managerPtr->createConfig(
359 "ldap://9.194.251.138/", "cn=Users,dc=com", "cn=Users,dc=corp",
360 "MyLdap12", ldap_base::Create::SearchScope::sub,
361 ldap_base::Create::Type::ActiveDirectory, "attr1", "attr2");
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530362 managerPtr->getADConfigPtr()->enabled(true);
Ratan Gupta95a29312019-02-18 20:34:10 +0530363
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600364 // Change LDAP BindDN
Patrick Williamse6500a42021-05-01 05:58:23 -0500365 managerPtr->getADConfigPtr()->ldapBindDN(
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600366 "cn=Administrator,cn=Users,dc=corp,dc=ibm,dc=com");
Patrick Williamse6500a42021-05-01 05:58:23 -0500367 EXPECT_EQ(managerPtr->getADConfigPtr()->ldapBindDN(),
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600368 "cn=Administrator,cn=Users,dc=corp,dc=ibm,dc=com");
369 // Change LDAP BindDN
370 EXPECT_THROW(
371 {
372 try
373 {
Patrick Williamse6500a42021-05-01 05:58:23 -0500374 managerPtr->getADConfigPtr()->ldapBindDN("");
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600375 }
376 catch (const InvalidArgument& e)
377 {
378 throw;
379 }
380 },
381 InvalidArgument);
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600382
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530383 managerPtr->restore();
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600384 // Check LDAP BindDN after restoring
Patrick Williamse6500a42021-05-01 05:58:23 -0500385 EXPECT_EQ(managerPtr->getADConfigPtr()->ldapBindDN(),
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600386 "cn=Administrator,cn=Users,dc=corp,dc=ibm,dc=com");
387 delete managerPtr;
388}
389
390TEST_F(TestLDAPConfig, testLDAPBaseDN)
391{
392 auto configFilePath = std::string(dir.c_str()) + "/" + ldapconfFile;
Ratan Gupta22f13f12019-04-29 15:36:40 +0530393 auto tlsCacertfile = std::string(dir.c_str()) + "/" + tlsCacertFile;
394 auto tlsCertfile = std::string(dir.c_str()) + "/" + tlsCertFile;
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530395 auto dbusPersistentFilePath = std::string(dir.c_str());
Nagaraju Goruganti3b4d06a2018-11-08 03:13:38 -0600396
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600397 if (fs::exists(configFilePath))
398 {
399 fs::remove(configFilePath);
400 }
401 EXPECT_FALSE(fs::exists(configFilePath));
Ratan Gupta22f13f12019-04-29 15:36:40 +0530402 MockConfigMgr* managerPtr =
403 new MockConfigMgr(bus, LDAP_CONFIG_ROOT, configFilePath.c_str(),
404 dbusPersistentFilePath.c_str(), tlsCacertfile.c_str(),
405 tlsCertfile.c_str());
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530406 EXPECT_CALL(*managerPtr, stopService("nslcd.service")).Times(1);
Alexander Filippov372c5662021-06-30 20:23:39 +0300407 EXPECT_CALL(*managerPtr, restartService("nslcd.service")).Times(3);
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530408 EXPECT_CALL(*managerPtr, restartService("nscd.service")).Times(1);
Ratan Guptaaeaf9412019-02-11 04:41:52 -0600409 managerPtr->createConfig(
410 "ldap://9.194.251.138/", "cn=Users,dc=com", "cn=Users,dc=corp",
411 "MyLdap12", ldap_base::Create::SearchScope::sub,
412 ldap_base::Create::Type::ActiveDirectory, "attr1", "attr2");
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530413 managerPtr->getADConfigPtr()->enabled(true);
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600414 // Change LDAP BaseDN
Patrick Williamse6500a42021-05-01 05:58:23 -0500415 managerPtr->getADConfigPtr()->ldapBaseDN(
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600416 "cn=Administrator,cn=Users,dc=corp,dc=ibm,dc=com");
Patrick Williamse6500a42021-05-01 05:58:23 -0500417 EXPECT_EQ(managerPtr->getADConfigPtr()->ldapBaseDN(),
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600418 "cn=Administrator,cn=Users,dc=corp,dc=ibm,dc=com");
419 // Change LDAP BaseDN
420 EXPECT_THROW(
421 {
422 try
423 {
Patrick Williamse6500a42021-05-01 05:58:23 -0500424 managerPtr->getADConfigPtr()->ldapBaseDN("");
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600425 }
426 catch (const InvalidArgument& e)
427 {
428 throw;
429 }
430 },
431 InvalidArgument);
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600432
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530433 managerPtr->restore();
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600434 // Check LDAP BaseDN after restoring
Patrick Williamse6500a42021-05-01 05:58:23 -0500435 EXPECT_EQ(managerPtr->getADConfigPtr()->ldapBaseDN(),
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600436 "cn=Administrator,cn=Users,dc=corp,dc=ibm,dc=com");
437 delete managerPtr;
438}
439
440TEST_F(TestLDAPConfig, testSearchScope)
441{
442 auto configFilePath = std::string(dir.c_str()) + "/" + ldapconfFile;
Ratan Gupta22f13f12019-04-29 15:36:40 +0530443 auto tlsCacertfile = std::string(dir.c_str()) + "/" + tlsCacertFile;
444 auto tlsCertfile = std::string(dir.c_str()) + "/" + tlsCertFile;
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530445 auto dbusPersistentFilePath = std::string(dir.c_str());
Nagaraju Goruganti3b4d06a2018-11-08 03:13:38 -0600446
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600447 if (fs::exists(configFilePath))
448 {
449 fs::remove(configFilePath);
450 }
451 EXPECT_FALSE(fs::exists(configFilePath));
Ratan Gupta22f13f12019-04-29 15:36:40 +0530452 MockConfigMgr* managerPtr =
453 new MockConfigMgr(bus, LDAP_CONFIG_ROOT, configFilePath.c_str(),
454 dbusPersistentFilePath.c_str(), tlsCacertfile.c_str(),
455 tlsCertfile.c_str());
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530456 EXPECT_CALL(*managerPtr, stopService("nslcd.service")).Times(1);
Alexander Filippov372c5662021-06-30 20:23:39 +0300457 EXPECT_CALL(*managerPtr, restartService("nslcd.service")).Times(3);
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530458 EXPECT_CALL(*managerPtr, restartService("nscd.service")).Times(1);
Ratan Guptaaeaf9412019-02-11 04:41:52 -0600459 managerPtr->createConfig(
460 "ldap://9.194.251.138/", "cn=Users,dc=com", "cn=Users,dc=corp",
461 "MyLdap12", ldap_base::Create::SearchScope::sub,
462 ldap_base::Create::Type::ActiveDirectory, "attr1", "attr2");
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530463 managerPtr->getADConfigPtr()->enabled(true);
Ratan Gupta95a29312019-02-18 20:34:10 +0530464
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600465 // Change LDAP SearchScope
Patrick Williamse6500a42021-05-01 05:58:23 -0500466 managerPtr->getADConfigPtr()->ldapSearchScope(
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600467 ldap_base::Config::SearchScope::one);
Patrick Williamse6500a42021-05-01 05:58:23 -0500468 EXPECT_EQ(managerPtr->getADConfigPtr()->ldapSearchScope(),
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600469 ldap_base::Config::SearchScope::one);
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600470
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530471 managerPtr->restore();
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600472 // Check LDAP SearchScope after restoring
Patrick Williamse6500a42021-05-01 05:58:23 -0500473 EXPECT_EQ(managerPtr->getADConfigPtr()->ldapSearchScope(),
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600474 ldap_base::Config::SearchScope::one);
475 delete managerPtr;
476}
477
478TEST_F(TestLDAPConfig, testLDAPType)
479{
480 auto configFilePath = std::string(dir.c_str()) + "/" + ldapconfFile;
Ratan Gupta22f13f12019-04-29 15:36:40 +0530481 auto tlsCacertfile = std::string(dir.c_str()) + "/" + tlsCacertFile;
482 auto tlsCertfile = std::string(dir.c_str()) + "/" + tlsCertFile;
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530483 auto dbusPersistentFilePath = std::string(dir.c_str());
Nagaraju Goruganti3b4d06a2018-11-08 03:13:38 -0600484
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600485 if (fs::exists(configFilePath))
486 {
487 fs::remove(configFilePath);
488 }
489 EXPECT_FALSE(fs::exists(configFilePath));
Ratan Gupta22f13f12019-04-29 15:36:40 +0530490 MockConfigMgr* managerPtr =
491 new MockConfigMgr(bus, LDAP_CONFIG_ROOT, configFilePath.c_str(),
492 dbusPersistentFilePath.c_str(), tlsCacertfile.c_str(),
493 tlsCertfile.c_str());
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530494 EXPECT_CALL(*managerPtr, stopService("nslcd.service")).Times(1);
Alexander Filippov372c5662021-06-30 20:23:39 +0300495 EXPECT_CALL(*managerPtr, restartService("nslcd.service")).Times(2);
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530496 EXPECT_CALL(*managerPtr, restartService("nscd.service")).Times(1);
Ratan Guptaaeaf9412019-02-11 04:41:52 -0600497 managerPtr->createConfig(
498 "ldap://9.194.251.138/", "cn=Users,dc=com", "cn=Users,dc=corp",
499 "MyLdap12", ldap_base::Create::SearchScope::sub,
500 ldap_base::Create::Type::ActiveDirectory, "attr1", "attr2");
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530501 managerPtr->getADConfigPtr()->enabled(true);
Ratan Gupta95a29312019-02-18 20:34:10 +0530502
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600503 // Change LDAP type
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530504 // will not be changed
Patrick Williamse6500a42021-05-01 05:58:23 -0500505 EXPECT_THROW(managerPtr->getADConfigPtr()->ldapType(
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530506 ldap_base::Config::Type::OpenLdap),
507 NotAllowed);
Patrick Williamse6500a42021-05-01 05:58:23 -0500508 EXPECT_EQ(managerPtr->getADConfigPtr()->ldapType(),
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530509 ldap_base::Config::Type::ActiveDirectory);
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600510
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530511 managerPtr->restore();
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600512 // Check LDAP type after restoring
Patrick Williamse6500a42021-05-01 05:58:23 -0500513 EXPECT_EQ(managerPtr->getADConfigPtr()->ldapType(),
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530514 ldap_base::Config::Type::ActiveDirectory);
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600515 delete managerPtr;
516}
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530517
Ravi Tejad5884042019-06-10 02:35:22 -0500518TEST_F(TestLDAPConfig, testsecureLDAPRestore)
519{
520 auto configFilePath = std::string(dir.c_str()) + "/" + ldapconfFile;
521 auto tlsCacertfile = std::string(dir.c_str()) + "/" + tlsCacertFile;
522 auto tlsCertfile = std::string(dir.c_str()) + "/" + tlsCertFile;
523 auto dbusPersistentFilePath = std::string(dir.c_str());
524
525 if (fs::exists(configFilePath))
526 {
527 fs::remove(configFilePath);
528 }
529 EXPECT_FALSE(fs::exists(configFilePath));
530 MockConfigMgr* managerPtr =
531 new MockConfigMgr(bus, LDAP_CONFIG_ROOT, configFilePath.c_str(),
532 dbusPersistentFilePath.c_str(), tlsCacertfile.c_str(),
533 tlsCertfile.c_str());
534 EXPECT_CALL(*managerPtr, stopService("nslcd.service")).Times(1);
Alexander Filippov372c5662021-06-30 20:23:39 +0300535 EXPECT_CALL(*managerPtr, restartService("nslcd.service")).Times(2);
Ravi Tejad5884042019-06-10 02:35:22 -0500536 EXPECT_CALL(*managerPtr, restartService("nscd.service")).Times(1);
537 managerPtr->createConfig(
538 "ldaps://9.194.251.138/", "cn=Users,dc=com", "cn=Users,dc=corp",
539 "MyLdap12", ldap_base::Create::SearchScope::sub,
540 ldap_base::Create::Type::ActiveDirectory, "attr1", "attr2");
541 managerPtr->getADConfigPtr()->enabled(true);
542 EXPECT_TRUE(managerPtr->secureLDAP());
543 managerPtr->restore();
544 // Check secureLDAP variable value after restoring
545 EXPECT_TRUE(managerPtr->secureLDAP());
546
547 delete managerPtr;
548}
549
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530550TEST_F(TestLDAPConfig, filePermission)
551{
552 auto configFilePath = std::string(dir.c_str()) + "/" + ldapconfFile;
Ratan Gupta22f13f12019-04-29 15:36:40 +0530553 auto tlsCacertfile = std::string(dir.c_str()) + "/" + tlsCacertFile;
554 auto tlsCertfile = std::string(dir.c_str()) + "/" + tlsCertFile;
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530555 auto dbusPersistentFilePath = std::string(dir.c_str());
556
557 if (fs::exists(configFilePath))
558 {
559 fs::remove(configFilePath);
560 }
561 EXPECT_FALSE(fs::exists(configFilePath));
Ratan Gupta22f13f12019-04-29 15:36:40 +0530562 MockConfigMgr* managerPtr =
563 new MockConfigMgr(bus, LDAP_CONFIG_ROOT, configFilePath.c_str(),
564 dbusPersistentFilePath.c_str(), tlsCacertfile.c_str(),
565 tlsCertfile.c_str());
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530566 EXPECT_CALL(*managerPtr, stopService("nslcd.service")).Times(1);
567 EXPECT_CALL(*managerPtr, restartService("nslcd.service")).Times(1);
568 EXPECT_CALL(*managerPtr, restartService("nscd.service")).Times(1);
569 managerPtr->createConfig(
570 "ldap://9.194.251.138/", "cn=Users,dc=com", "cn=Users,dc=corp",
571 "MyLdap12", ldap_base::Create::SearchScope::sub,
572 ldap_base::Create::Type::ActiveDirectory, "attr1", "attr2");
573 managerPtr->getADConfigPtr()->enabled(true);
574
575 // Permission of the persistent file should be 640
576 // Others should not be allowed to read.
577 auto permission =
578 fs::perms::owner_read | fs::perms::owner_write | fs::perms::group_read;
579 auto persistFilepath = std::string(dir.c_str());
580 persistFilepath += ADDbusObjectPath;
581 persistFilepath += "/config";
582
583 EXPECT_EQ(fs::status(persistFilepath).permissions(), permission);
584 delete managerPtr;
585}
586
Ratan Guptac5481d12019-04-12 18:31:05 +0530587TEST_F(TestLDAPConfig, ConditionalEnableConfig)
588{
589 auto configFilePath = std::string(dir.c_str()) + "/" + ldapconfFile;
Ratan Gupta22f13f12019-04-29 15:36:40 +0530590 auto tlsCacertfile = std::string(dir.c_str()) + "/" + tlsCacertFile;
591 auto tlsCertfile = std::string(dir.c_str()) + "/" + tlsCertFile;
Ratan Guptac5481d12019-04-12 18:31:05 +0530592 auto dbusPersistentFilePath = std::string(dir.c_str());
593
594 if (fs::exists(configFilePath))
595 {
596 fs::remove(configFilePath);
597 }
598 EXPECT_FALSE(fs::exists(configFilePath));
Ratan Gupta22f13f12019-04-29 15:36:40 +0530599 MockConfigMgr* managerPtr =
600 new MockConfigMgr(bus, LDAP_CONFIG_ROOT, configFilePath.c_str(),
601 dbusPersistentFilePath.c_str(), tlsCacertfile.c_str(),
602 tlsCertfile.c_str());
Ratan Guptac5481d12019-04-12 18:31:05 +0530603 EXPECT_CALL(*managerPtr, stopService("nslcd.service")).Times(3);
604 EXPECT_CALL(*managerPtr, restartService("nslcd.service")).Times(2);
605 EXPECT_CALL(*managerPtr, restartService("nscd.service")).Times(2);
606 managerPtr->createConfig(
607 "ldap://9.194.251.138/", "cn=Users,dc=com", "cn=Users,dc=corp",
608 "MyLdap12", ldap_base::Create::SearchScope::sub,
609 ldap_base::Create::Type::ActiveDirectory, "attr1", "attr2");
610
611 managerPtr->createConfig(
612 "ldap://9.194.251.139/", "cn=Users,dc=com, dc=ldap", "cn=Users,dc=corp",
613 "MyLdap123", ldap_base::Create::SearchScope::sub,
614 ldap_base::Create::Type::OpenLdap, "attr1", "attr2");
615
616 // Enable the AD configuration
617 managerPtr->getADConfigPtr()->enabled(true);
618
619 EXPECT_EQ(managerPtr->getADConfigPtr()->enabled(), true);
620 EXPECT_EQ(managerPtr->getOpenLdapConfigPtr()->enabled(), false);
621
622 // AS AD is already enabled so openldap can't be enabled.
623 EXPECT_THROW(
624 {
625 try
626 {
627 managerPtr->getOpenLdapConfigPtr()->enabled(true);
628 }
629 catch (const NotAllowed& e)
630 {
631 throw;
632 }
633 },
634 NotAllowed);
635 // Check the values
636 EXPECT_EQ(managerPtr->getADConfigPtr()->enabled(), true);
637 EXPECT_EQ(managerPtr->getOpenLdapConfigPtr()->enabled(), false);
638 // Let's disable the AD.
639 managerPtr->getADConfigPtr()->enabled(false);
640 EXPECT_EQ(managerPtr->getADConfigPtr()->enabled(), false);
641 EXPECT_EQ(managerPtr->getOpenLdapConfigPtr()->enabled(), false);
642 // Now enable the openldap
643 managerPtr->getOpenLdapConfigPtr()->enabled(true);
644 EXPECT_EQ(managerPtr->getOpenLdapConfigPtr()->enabled(), true);
645 EXPECT_EQ(managerPtr->getADConfigPtr()->enabled(), false);
646
647 delete managerPtr;
648}
649
Ratan Guptafef57892019-04-14 13:43:09 +0530650TEST_F(TestLDAPConfig, createPrivMapping)
651{
652 auto configFilePath = std::string(dir.c_str()) + "/" + ldapconfFile;
Ratan Gupta22f13f12019-04-29 15:36:40 +0530653 auto tlsCacertfile = std::string(dir.c_str()) + "/" + tlsCacertFile;
654 auto tlsCertfile = std::string(dir.c_str()) + "/" + tlsCertFile;
Ratan Guptafef57892019-04-14 13:43:09 +0530655 auto dbusPersistentFilePath = std::string(dir.c_str());
656
657 if (fs::exists(configFilePath))
658 {
659 fs::remove(configFilePath);
660 }
661 EXPECT_FALSE(fs::exists(configFilePath));
662 MockConfigMgr manager(bus, LDAP_CONFIG_ROOT, configFilePath.c_str(),
Ratan Gupta22f13f12019-04-29 15:36:40 +0530663 dbusPersistentFilePath.c_str(), tlsCacertfile.c_str(),
664 tlsCertfile.c_str());
Ratan Guptafef57892019-04-14 13:43:09 +0530665 manager.createDefaultObjects();
666 // Create the priv-mapping under the config.
667 manager.getADConfigPtr()->create("admin", "priv-admin");
668 // Check whether the entry has been created.
669 EXPECT_THROW(
670 {
671 try
672 {
673 manager.getADConfigPtr()->checkPrivilegeMapper("admin");
674 }
675 catch (const PrivilegeMappingExists& e)
676 {
677 throw;
678 }
679 },
680 PrivilegeMappingExists);
681}
682
683TEST_F(TestLDAPConfig, deletePrivMapping)
684{
685 auto configFilePath = std::string(dir.c_str()) + "/" + ldapconfFile;
Ratan Gupta22f13f12019-04-29 15:36:40 +0530686 auto tlsCacertfile = std::string(dir.c_str()) + "/" + tlsCacertFile;
687 auto tlsCertfile = std::string(dir.c_str()) + "/" + tlsCertFile;
Ratan Guptafef57892019-04-14 13:43:09 +0530688 auto dbusPersistentFilePath = std::string(dir.c_str());
689
690 if (fs::exists(configFilePath))
691 {
692 fs::remove(configFilePath);
693 }
694 EXPECT_FALSE(fs::exists(configFilePath));
695 MockConfigMgr manager(bus, LDAP_CONFIG_ROOT, configFilePath.c_str(),
Ratan Gupta22f13f12019-04-29 15:36:40 +0530696 dbusPersistentFilePath.c_str(), tlsCacertfile.c_str(),
697 tlsCertfile.c_str());
Ratan Guptafef57892019-04-14 13:43:09 +0530698 manager.createDefaultObjects();
699 // Create the priv-mapping under the config.
700 manager.getADConfigPtr()->create("admin", "priv-admin");
701 manager.getADConfigPtr()->create("user", "priv-user");
702 // Check whether the entry has been created.
703 EXPECT_THROW(
704 {
705 try
706 {
707 manager.getADConfigPtr()->checkPrivilegeMapper("admin");
708 manager.getADConfigPtr()->checkPrivilegeMapper("user");
709 }
710 catch (const PrivilegeMappingExists& e)
711 {
712 throw;
713 }
714 },
715 PrivilegeMappingExists);
716
717 // This would delete the admin privilege
718 manager.getADConfigPtr()->deletePrivilegeMapper(1);
719 EXPECT_NO_THROW(manager.getADConfigPtr()->checkPrivilegeMapper("admin"));
720 manager.getADConfigPtr()->deletePrivilegeMapper(2);
721 EXPECT_NO_THROW(manager.getADConfigPtr()->checkPrivilegeMapper("user"));
722}
723
724TEST_F(TestLDAPConfig, restorePrivMapping)
725{
726 auto configFilePath = std::string(dir.c_str()) + "/" + ldapconfFile;
Ratan Gupta22f13f12019-04-29 15:36:40 +0530727 auto tlsCacertfile = std::string(dir.c_str()) + "/" + tlsCacertFile;
728 auto tlsCertfile = std::string(dir.c_str()) + "/" + tlsCertFile;
Ratan Guptafef57892019-04-14 13:43:09 +0530729 auto dbusPersistentFilePath = std::string(dir.c_str());
730
731 if (fs::exists(configFilePath))
732 {
733 fs::remove(configFilePath);
734 }
735 EXPECT_FALSE(fs::exists(configFilePath));
736 MockConfigMgr manager(bus, LDAP_CONFIG_ROOT, configFilePath.c_str(),
Ratan Gupta22f13f12019-04-29 15:36:40 +0530737 dbusPersistentFilePath.c_str(), tlsCacertfile.c_str(),
738 tlsCertfile.c_str());
Ratan Guptafef57892019-04-14 13:43:09 +0530739 manager.createDefaultObjects();
740 // Create the priv-mapping under the config.
741 manager.getADConfigPtr()->create("admin", "priv-admin");
742 manager.getOpenLdapConfigPtr()->create("user", "priv-user");
743 manager.restore();
744 EXPECT_THROW(
745 {
746 try
747 {
748 manager.getADConfigPtr()->checkPrivilegeMapper("admin");
749 }
750 catch (const PrivilegeMappingExists& e)
751 {
752 throw;
753 }
754 },
755 PrivilegeMappingExists);
756
757 EXPECT_THROW(
758 {
759 try
760 {
761 manager.getOpenLdapConfigPtr()->checkPrivilegeMapper("user");
762 }
763 catch (const PrivilegeMappingExists& e)
764 {
765 throw;
766 }
767 },
768 PrivilegeMappingExists);
769}
770
771TEST_F(TestLDAPConfig, testPrivileges)
772{
773 auto configFilePath = std::string(dir.c_str()) + "/" + ldapconfFile;
Ratan Gupta22f13f12019-04-29 15:36:40 +0530774 auto tlsCacertfile = std::string(dir.c_str()) + "/" + tlsCacertFile;
775 auto tlsCertfile = std::string(dir.c_str()) + "/" + tlsCertFile;
Ratan Guptafef57892019-04-14 13:43:09 +0530776 auto dbusPersistentFilePath = std::string(dir.c_str());
777
778 if (fs::exists(configFilePath))
779 {
780 fs::remove(configFilePath);
781 }
782 EXPECT_FALSE(fs::exists(configFilePath));
783 MockConfigMgr manager(bus, LDAP_CONFIG_ROOT, configFilePath.c_str(),
Ratan Gupta22f13f12019-04-29 15:36:40 +0530784 dbusPersistentFilePath.c_str(), tlsCacertfile.c_str(),
785 tlsCertfile.c_str());
Ratan Guptafef57892019-04-14 13:43:09 +0530786 manager.createDefaultObjects();
787
788 std::string groupName = "admin";
789 std::string privilege = "priv-admin";
790 size_t entryId = 1;
791 auto dbusPath = std::string(LDAP_CONFIG_ROOT) +
792 "/active_directory/role_map/" + std::to_string(entryId);
793 dbusPersistentFilePath += dbusPath;
794
795 auto entry = std::make_unique<LDAPMapperEntry>(
796 bus, dbusPath.c_str(), dbusPersistentFilePath.c_str(), groupName,
797 privilege, *(manager.getADConfigPtr()));
798
799 EXPECT_NO_THROW(entry->privilege("priv-operator"));
800 EXPECT_NO_THROW(entry->privilege("priv-user"));
Ratan Guptafef57892019-04-14 13:43:09 +0530801}
802
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600803} // namespace ldap
804} // namespace phosphor