Skip to content

Commit a56631b

Browse files
Sakshamsnitinmeh
authored andcommitted
bug CS-15278: Added global setting login.attempts.allowed which defines the maximum incorrect password attempts allowed.
Also after the maximum attempts are reached the user account is disabled
1 parent 41f6127 commit a56631b

6 files changed

Lines changed: 78 additions & 4 deletions

File tree

‎core/src/com/cloud/user/UserAccountVO.java‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ public class UserAccountVO implements UserAccount {
8383
@Column(name="is_registered")
8484
boolean registered;
8585

86+
@Column (name="incorrect_login_attempts")
87+
int loginAttempts;
88+
8689
@Column(name="account_name", table="account", insertable=false, updatable=false)
8790
private String accountName = null;
8891

@@ -269,4 +272,12 @@ public boolean isRegistered() {
269272
public void setRegistered(boolean registered) {
270273
this.registered = registered;
271274
}
272-
}
275+
276+
public void setLoginAttempts(int loginAttempts) {
277+
this.loginAttempts = loginAttempts;
278+
}
279+
280+
public int getLoginAttempts() {
281+
return loginAttempts;
282+
}
283+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ public enum Config {
229229
EnableEC2API("Advanced", ManagementServer.class, Boolean.class, "enable.ec2.api", "false", "enable EC2 API on CloudStack", null),
230230
EnableS3API("Advanced", ManagementServer.class, Boolean.class, "enable.s3.api", "false", "enable Amazon S3 API on CloudStack", null),
231231
RecreateSystemVmEnabled("Advanced", ManagementServer.class, Boolean.class, "recreate.systemvm.enabled", "false", "If true, will recreate system vm root disk whenever starting system vm", "true,false"),
232-
232+
IncorrectLoginAttemptsAllowed("Advanced", ManagementServer.class, Integer.class, "incorrect.login.attempts.allowed", "5", "Incorrect login attempts allowed before the user is disabled", null),
233233
// Ovm
234234
OvmPublicNetwork("Hidden", ManagementServer.class, String.class, "ovm.public.network.device", null, "Specify the public bridge on host for public network", null),
235235
OvmPrivateNetwork("Hidden", ManagementServer.class, String.class, "ovm.private.network.device", null, "Specify the private bridge on host for private network", null),

‎server/src/com/cloud/configuration/ConfigurationManagerImpl.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,7 @@ private void populateConfigValuesForValidationSet() {
274274
configValuesForValidation.add("storage.cleanup.interval");
275275
configValuesForValidation.add("wait");
276276
configValuesForValidation.add("xen.heartbeat.interval");
277+
configValuesForValidation.add("incorrect.login.attempts.allowed");
277278
}
278279

279280
@Override

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

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,8 @@ public class AccountManagerImpl implements AccountManager, AccountService, Manag
224224

225225
private final ScheduledExecutorService _executor = Executors.newScheduledThreadPool(1, new NamedThreadFactory("AccountChecker"));
226226

227+
int _allowedLoginAttempts;
228+
227229
UserVO _systemUser;
228230
AccountVO _systemAccount;
229231
@Inject(adapter = SecurityChecker.class)
@@ -248,6 +250,9 @@ public boolean configure(final String name, final Map<String, Object> params) th
248250
ConfigurationDao configDao = locator.getDao(ConfigurationDao.class);
249251
Map<String, String> configs = configDao.getConfiguration(params);
250252

253+
String loginAttempts = configs.get(Config.IncorrectLoginAttemptsAllowed.key());
254+
_allowedLoginAttempts = NumbersUtil.parseInt(loginAttempts, 5);
255+
251256
String value = configs.get(Config.AccountCleanupInterval.key());
252257
_cleanupInterval = NumbersUtil.parseInt(value, 60 * 60 * 24); // 1 day.
253258

@@ -302,6 +307,13 @@ public boolean isResourceDomainAdmin(short accountType) {
302307
return (accountType == Account.ACCOUNT_TYPE_RESOURCE_DOMAIN_ADMIN);
303308
}
304309

310+
public boolean isInternalAccount(short accountType) {
311+
if (isRootAdmin(accountType) || (accountType == Account.ACCOUNT_ID_SYSTEM)) {
312+
return true;
313+
}
314+
return false;
315+
}
316+
305317
@Override
306318
public void checkAccess(Account caller, Domain domain) throws PermissionDeniedException {
307319
for (SecurityChecker checker : _securityCheckers) {
@@ -420,6 +432,25 @@ private Long getZoneIdForAccount(Account account) {
420432

421433
}
422434

435+
@DB
436+
public void updateLoginAttempts(Long id, int attempts, boolean toDisable) {
437+
Transaction txn = Transaction.currentTxn();
438+
txn.start();
439+
try {
440+
UserAccountVO user = null;
441+
user = _userAccountDao.lockRow(id, true);
442+
user.setLoginAttempts(attempts);
443+
if(toDisable) {
444+
user.setState(State.disabled.toString());
445+
}
446+
_userAccountDao.update(id, user);
447+
txn.commit();
448+
} catch (Exception e) {
449+
s_logger.error("Failed to update login attempts for user with id " + id );
450+
}
451+
txn.close();
452+
}
453+
423454
private boolean doSetUserStatus(long userId, State state) {
424455
UserVO userForUpdate = _userDao.createForUpdate();
425456
userForUpdate.setState(state);
@@ -732,15 +763,15 @@ public UserAccount createUserAccount(String userName, String password, String fi
732763
if (domainId == null) {
733764
domainId = DomainVO.ROOT_DOMAIN;
734765
}
735-
766+
736767
if (userName.isEmpty()) {
737768
throw new InvalidParameterValueException("Username is empty");
738769
}
739770

740771
if (firstName.isEmpty()) {
741772
throw new InvalidParameterValueException("Firstname is empty");
742773
}
743-
774+
744775
if (lastName.isEmpty()) {
745776
throw new InvalidParameterValueException("Lastname is empty");
746777
}
@@ -1002,6 +1033,8 @@ public UserAccount enableUser(long userId) {
10021033
txn.commit();
10031034

10041035
if (success) {
1036+
// whenever the user is successfully enabled, reset the login attempts to zero
1037+
updateLoginAttempts(userId, 0, false);
10051038
return _userAccountDao.findById(userId);
10061039
} else {
10071040
throw new CloudRuntimeException("Unable to enable user " + userId);
@@ -1818,11 +1851,36 @@ private UserAccount getUserAccount(String username, String password, Long domain
18181851
throw new CloudAuthenticationException("User " + username + " in domain " + domainName + " is disabled/locked (or account is disabled/locked)");
18191852
// return null;
18201853
}
1854+
// Whenever the user is able to log in successfully, reset the login attempts to zero
1855+
if(!isInternalAccount(userAccount.getType()))
1856+
updateLoginAttempts(userAccount.getId(), 0, false);
1857+
18211858
return userAccount;
18221859
} else {
18231860
if (s_logger.isDebugEnabled()) {
18241861
s_logger.debug("Unable to authenticate user with username " + username + " in domain " + domainId);
18251862
}
1863+
1864+
UserAccount userAccount = _userAccountDao.getUserAccount(username, domainId);
1865+
UserAccountVO user = _userAccountDao.findById(userAccount.getId());
1866+
if (user != null) {
1867+
if ((user.getState().toString()).equals("enabled")) {
1868+
if (!isInternalAccount(user.getType())) {
1869+
//Internal accounts are not disabled
1870+
int attemptsMade = user.getLoginAttempts() + 1;
1871+
if (attemptsMade < _allowedLoginAttempts) {
1872+
updateLoginAttempts(userAccount.getId(), attemptsMade, false);
1873+
s_logger.warn("Login attempt failed. You have " + ( _allowedLoginAttempts - attemptsMade ) + " attempt(s) remaining");
1874+
} else {
1875+
updateLoginAttempts(userAccount.getId(), _allowedLoginAttempts, true);
1876+
s_logger.warn("User " + user.getUsername() + " has been disabled due to multiple failed login attempts." +
1877+
" Please contact admin.");
1878+
}
1879+
}
1880+
} else {
1881+
s_logger.info("User " + user.getUsername() + " is disabled/locked");
1882+
}
1883+
}
18261884
return null;
18271885
}
18281886
}

‎setup/db/create-schema.sql‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -890,6 +890,7 @@ CREATE TABLE `cloud`.`user` (
890890
`timezone` varchar(30) default NULL,
891891
`registration_token` varchar(255) default NULL,
892892
`is_registered` tinyint NOT NULL DEFAULT 0 COMMENT '1: yes, 0: no',
893+
`incorrect_login_attempts` integer unsigned NOT NULL DEFAULT 0,
893894
PRIMARY KEY (`id`),
894895
INDEX `i_user__removed`(`removed`),
895896
INDEX `i_user__secret_key_removed`(`secret_key`, `removed`),

‎setup/db/db/schema-302to40.sql‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,3 +472,6 @@ INSERT IGNORE INTO `cloud`.`configuration` VALUES ('Network', 'DEFAULT', 'manage
472472
INSERT IGNORE INTO `cloud`.`configuration` VALUES ('Network', 'DEFAULT', 'management-server', 'site2site.vpn.customergateway.subnets.limit', '10', 'The maximum number of subnets per customer gateway');
473473

474474
INSERT IGNORE INTO `cloud`.`guest_os_category` VALUES ('11','None',NULL);
475+
ALTER TABLE `cloud`.`user` ADD COLUMN `incorrect_login_attempts` integer unsigned NOT NULL DEFAULT '0';
476+
INSERT IGNORE INTO `cloud`.`configuration` VALUES ('Advanced', 'DEFAULT', 'management-server', 'incorrect.login.attempts.allowed', '5', 'Incorrect login attempts allowed before the user is disabled');
477+

0 commit comments

Comments
 (0)