Skip to content

Commit 0833cf1

Browse files
authored
server: fix potential NPE while ldap authentication (#3418)
This fixes a potential NPE when a mapped account is not found and moving of user to the mapped account is performed. This will now throw a more information exception than NPE. Fixes #2853 Signed-off-by: Rohit Yadav <rohit.yadav@shapeblue.com>
1 parent f653e61 commit 0833cf1

5 files changed

Lines changed: 15 additions & 10 deletions

File tree

plugins/network-elements/juniper-contrail/src/test/java/org/apache/cloudstack/network/contrail/management/MockAccountManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ public boolean moveUser(MoveUserCmd moveUserCmd) {
316316
}
317317

318318
@Override
319-
public boolean moveUser(long id, Long domainId, long accountId) {
319+
public boolean moveUser(long id, Long domainId, Account account) {
320320
return false;
321321
}
322322

plugins/user-authenticators/ldap/src/main/java/org/apache/cloudstack/ldap/LdapAuthenticator.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import com.cloud.user.dao.UserAccountDao;
3636
import com.cloud.utils.Pair;
3737
import com.cloud.utils.component.AdapterBase;
38+
import com.cloud.utils.exception.CloudRuntimeException;
3839

3940
public class LdapAuthenticator extends AdapterBase implements UserAuthenticator {
4041
private static final Logger s_logger = Logger.getLogger(LdapAuthenticator.class.getName());
@@ -135,7 +136,11 @@ private Pair<Boolean, ActionOnFailedAuthentication> authenticate(String username
135136
} else {
136137
// not a new user, check if mapped group has changed
137138
if(userAccount.getAccountId() != mapping.getAccountId()) {
138-
_accountManager.moveUser(userAccount.getId(),userAccount.getDomainId(),mapping.getAccountId());
139+
final Account mappedAccount = _accountManager.getAccount(mapping.getAccountId());
140+
if (mappedAccount == null || mappedAccount.getRemoved() != null) {
141+
throw new CloudRuntimeException("Mapped account for users does not exist. Please contact your administrator.");
142+
}
143+
_accountManager.moveUser(userAccount.getId(), userAccount.getDomainId(), mappedAccount);
139144
}
140145
// else { the user hasn't changed in ldap, the ldap group stayed the same, hurray, pass, fun thou self a lot of fun }
141146
}

server/src/main/java/com/cloud/user/AccountManager.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -180,11 +180,12 @@ void buildACLViewSearchCriteria(SearchCriteria<? extends ControlledViewEntity> s
180180

181181
List<String> listAclGroupsByAccount(Long accountId);
182182

183-
public static final String MESSAGE_ADD_ACCOUNT_EVENT = "Message.AddAccount.Event";
183+
String MESSAGE_ADD_ACCOUNT_EVENT = "Message.AddAccount.Event";
184184

185-
public static final String MESSAGE_REMOVE_ACCOUNT_EVENT = "Message.RemoveAccount.Event";
186-
public static final ConfigKey<Boolean> UseSecretKeyInResponse = new ConfigKey<Boolean>("Advanced", Boolean.class, "use.secret.key.in.response", "false",
185+
String MESSAGE_REMOVE_ACCOUNT_EVENT = "Message.RemoveAccount.Event";
186+
187+
ConfigKey<Boolean> UseSecretKeyInResponse = new ConfigKey<Boolean>("Advanced", Boolean.class, "use.secret.key.in.response", "false",
187188
"This parameter allows the users to enable or disable of showing secret key as a part of response for various APIs. By default it is set to false.", true);
188189

189-
boolean moveUser(long id, Long domainId, long accountId);
190+
boolean moveUser(long id, Long domainId, Account newAccount);
190191
}

server/src/main/java/com/cloud/user/AccountManagerImpl.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1817,13 +1817,12 @@ public boolean moveUser(MoveUserCmd cmd) {
18171817
}
18181818

18191819
@Override
1820-
public boolean moveUser(long id, Long domainId, long accountId) {
1820+
public boolean moveUser(long id, Long domainId, Account newAccount) {
18211821
UserVO user = getValidUserVO(id);
18221822
Account oldAccount = _accountDao.findById(user.getAccountId());
18231823
checkAccountAndAccess(user, oldAccount);
1824-
Account newAccount = _accountDao.findById(accountId);
18251824
checkIfNotMovingAcrossDomains(domainId, newAccount);
1826-
return moveUser(user, accountId);
1825+
return moveUser(user, newAccount.getId());
18271826
}
18281827

18291828
private boolean moveUser(UserVO user, long newAccountId) {

server/src/test/java/com/cloud/user/MockAccountManagerImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ public boolean moveUser(MoveUserCmd moveUserCmd) {
129129
}
130130

131131
@Override
132-
public boolean moveUser(long id, Long domainId, long accountId) {
132+
public boolean moveUser(long id, Long domainId, Account account) {
133133
return false;
134134
}
135135

0 commit comments

Comments
 (0)