Skip to content

Commit 3674aff

Browse files
committed
Merge branch '4.16'
2 parents c794127 + fbf7797 commit 3674aff

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
@@ -2555,16 +2555,7 @@ private UserAccount getUserAccount(String username, String password, Long domain
25552555
if (userAccount.getState().equalsIgnoreCase(Account.State.ENABLED.toString())) {
25562556
if (!isInternalAccount(userAccount.getId())) {
25572557
// Internal accounts are not disabled
2558-
int attemptsMade = userAccount.getLoginAttempts() + 1;
2559-
if (updateIncorrectLoginCount) {
2560-
if (attemptsMade < _allowedLoginAttempts) {
2561-
updateLoginAttempts(userAccount.getId(), attemptsMade, false);
2562-
s_logger.warn("Login attempt failed. You have " + (_allowedLoginAttempts - attemptsMade) + " attempt(s) remaining");
2563-
} else {
2564-
updateLoginAttempts(userAccount.getId(), _allowedLoginAttempts, true);
2565-
s_logger.warn("User " + userAccount.getUsername() + " has been disabled due to multiple failed login attempts." + " Please contact admin.");
2566-
}
2567-
}
2558+
updateLoginAttemptsWhenIncorrectLoginAttemptsEnabled(userAccount, updateIncorrectLoginCount, _allowedLoginAttempts);
25682559
}
25692560
} else {
25702561
s_logger.info("User " + userAccount.getUsername() + " is disabled/locked");
@@ -2573,6 +2564,23 @@ private UserAccount getUserAccount(String username, String password, Long domain
25732564
}
25742565
}
25752566

2567+
protected void updateLoginAttemptsWhenIncorrectLoginAttemptsEnabled(UserAccount account, boolean updateIncorrectLoginCount,
2568+
int allowedLoginAttempts) {
2569+
int attemptsMade = account.getLoginAttempts() + 1;
2570+
if (allowedLoginAttempts <= 0 || !updateIncorrectLoginCount) {
2571+
return;
2572+
}
2573+
if (attemptsMade < allowedLoginAttempts) {
2574+
updateLoginAttempts(account.getId(), attemptsMade, false);
2575+
s_logger.warn("Login attempt failed. You have " +
2576+
(allowedLoginAttempts - attemptsMade) + " attempt(s) remaining");
2577+
} else {
2578+
updateLoginAttempts(account.getId(), allowedLoginAttempts, true);
2579+
s_logger.warn("User " + account.getUsername() +
2580+
" has been disabled due to multiple failed login attempts." + " Please contact admin.");
2581+
}
2582+
}
2583+
25762584
@Override
25772585
public Pair<User, Account> findUserByApiKey(String apiKey) {
25782586
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)