Skip to content

Commit fbf7797

Browse files
authored
Fix: Allow disabling the login attempts mechanism for disabling users (#6254)
* Fix: Allow disabling the login attempts mechanism for disabling users * Refactor
1 parent 42a92dc commit fbf7797

File tree

3 files changed

+46
-11
lines changed

3 files changed

+46
-11
lines changed

server/src/main/java/com/cloud/configuration/Config.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -984,7 +984,7 @@ public enum Config {
984984
Integer.class,
985985
"incorrect.login.attempts.allowed",
986986
"5",
987-
"Incorrect login attempts allowed before the user is disabled",
987+
"Incorrect login attempts allowed before the user is disabled (when value > 0). If value <=0 users are not disabled after failed login attempts",
988988
null),
989989
// Ovm
990990
OvmPublicNetwork("Hidden", ManagementServer.class, String.class, "ovm.public.network.device", null, "Specify the public bridge on host for public network", null),

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

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2547,16 +2547,7 @@ private UserAccount getUserAccount(String username, String password, Long domain
25472547
if (userAccount.getState().equalsIgnoreCase(Account.State.enabled.toString())) {
25482548
if (!isInternalAccount(userAccount.getId())) {
25492549
// Internal accounts are not disabled
2550-
int attemptsMade = userAccount.getLoginAttempts() + 1;
2551-
if (updateIncorrectLoginCount) {
2552-
if (attemptsMade < _allowedLoginAttempts) {
2553-
updateLoginAttempts(userAccount.getId(), attemptsMade, false);
2554-
s_logger.warn("Login attempt failed. You have " + (_allowedLoginAttempts - attemptsMade) + " attempt(s) remaining");
2555-
} else {
2556-
updateLoginAttempts(userAccount.getId(), _allowedLoginAttempts, true);
2557-
s_logger.warn("User " + userAccount.getUsername() + " has been disabled due to multiple failed login attempts." + " Please contact admin.");
2558-
}
2559-
}
2550+
updateLoginAttemptsWhenIncorrectLoginAttemptsEnabled(userAccount, updateIncorrectLoginCount, _allowedLoginAttempts);
25602551
}
25612552
} else {
25622553
s_logger.info("User " + userAccount.getUsername() + " is disabled/locked");
@@ -2565,6 +2556,23 @@ private UserAccount getUserAccount(String username, String password, Long domain
25652556
}
25662557
}
25672558

2559+
protected void updateLoginAttemptsWhenIncorrectLoginAttemptsEnabled(UserAccount account, boolean updateIncorrectLoginCount,
2560+
int allowedLoginAttempts) {
2561+
int attemptsMade = account.getLoginAttempts() + 1;
2562+
if (allowedLoginAttempts <= 0 || !updateIncorrectLoginCount) {
2563+
return;
2564+
}
2565+
if (attemptsMade < allowedLoginAttempts) {
2566+
updateLoginAttempts(account.getId(), attemptsMade, false);
2567+
s_logger.warn("Login attempt failed. You have " +
2568+
(allowedLoginAttempts - attemptsMade) + " attempt(s) remaining");
2569+
} else {
2570+
updateLoginAttempts(account.getId(), allowedLoginAttempts, true);
2571+
s_logger.warn("User " + account.getUsername() +
2572+
" has been disabled due to multiple failed login attempts." + " Please contact admin.");
2573+
}
2574+
}
2575+
25682576
@Override
25692577
public Pair<User, Account> findUserByApiKey(String apiKey) {
25702578
return _accountDao.findUserAccountByApiKey(apiKey);

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -710,4 +710,31 @@ public void validateCurrentPasswordTestUserAuthenticatedWithProvidedCurrentPassw
710710
Mockito.verify(authenticatorMock2, Mockito.times(1)).authenticate(username, currentPassword, domainId, null);
711711
}
712712

713+
@Test
714+
public void testUpdateLoginAttemptsDisableMechanism() {
715+
accountManagerImpl.updateLoginAttemptsWhenIncorrectLoginAttemptsEnabled(userAccountVO, true, 0);
716+
Mockito.verify(accountManagerImpl, Mockito.never()).updateLoginAttempts(Mockito.anyLong(), Mockito.anyInt(), Mockito.anyBoolean());
717+
}
718+
719+
@Test
720+
public void testUpdateLoginAttemptsEnableMechanismAttemptsLeft() {
721+
int attempts = 2;
722+
int allowedAttempts = 5;
723+
Long accountId = 1L;
724+
Mockito.when(userAccountVO.getLoginAttempts()).thenReturn(attempts);
725+
Mockito.when(userAccountVO.getId()).thenReturn(accountId);
726+
accountManagerImpl.updateLoginAttemptsWhenIncorrectLoginAttemptsEnabled(userAccountVO, true, allowedAttempts);
727+
Mockito.verify(accountManagerImpl).updateLoginAttempts(Mockito.eq(accountId), Mockito.eq(attempts + 1), Mockito.eq(false));
728+
}
729+
730+
@Test
731+
public void testUpdateLoginAttemptsEnableMechanismNoAttemptsLeft() {
732+
int attempts = 5;
733+
int allowedAttempts = 5;
734+
Long accountId = 1L;
735+
Mockito.when(userAccountVO.getLoginAttempts()).thenReturn(attempts);
736+
Mockito.when(userAccountVO.getId()).thenReturn(accountId);
737+
accountManagerImpl.updateLoginAttemptsWhenIncorrectLoginAttemptsEnabled(userAccountVO, true, allowedAttempts);
738+
Mockito.verify(accountManagerImpl).updateLoginAttempts(Mockito.eq(accountId), Mockito.eq(allowedAttempts), Mockito.eq(true));
739+
}
713740
}

0 commit comments

Comments
 (0)