Skip to content

Commit a857e18

Browse files
bernardodemarcodhslove
authored andcommitted
Prevent password updates for SAML and LDAP users (apache#9999)
1 parent dc5c282 commit a857e18

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1510,6 +1510,8 @@ public UserAccount updateUser(UpdateUserCmd updateUserCmd) {
15101510
* <ul>
15111511
* <li> If 'password' is blank, we throw an {@link InvalidParameterValueException};
15121512
* <li> If 'current password' is not provided and user is not an Admin, we throw an {@link InvalidParameterValueException};
1513+
* <li> If the user whose password is being changed has a source equal to {@link User.Source#SAML2}, {@link User.Source#SAML2DISABLED} or {@link User.Source#LDAP},
1514+
* we throw an {@link InvalidParameterValueException};
15131515
* <li> If a normal user is calling this method, we use {@link #validateCurrentPassword(UserVO, String)} to check if the provided old password matches the database one;
15141516
* </ul>
15151517
*
@@ -1524,6 +1526,12 @@ public void validateUserPasswordAndUpdateIfNeeded(String newPassword, UserVO use
15241526
throw new InvalidParameterValueException("Password cannot be empty or blank.");
15251527
}
15261528

1529+
User.Source userSource = user.getSource();
1530+
if (userSource == User.Source.SAML2 || userSource == User.Source.SAML2DISABLED || userSource == User.Source.LDAP) {
1531+
s_logger.warn(String.format("Unable to update the password for user [%d], as its source is [%s].", user.getId(), user.getSource().toString()));
1532+
throw new InvalidParameterValueException("CloudStack does not support updating passwords for SAML or LDAP users. Please contact your cloud administrator for assistance.");
1533+
}
1534+
15271535
passwordPolicy.verifyIfPasswordCompliesWithPasswordPolicies(newPassword, user.getUsername(), getAccount(user.getAccountId()).getDomainId());
15281536

15291537
Account callingAccount = getCurrentCallingAccount();

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -874,6 +874,36 @@ public void validateUserPasswordAndUpdateIfNeededTestIfVerifyIfPasswordCompliesW
874874
accountManagerImpl.validateUserPasswordAndUpdateIfNeeded(newPassword, userVoMock, currentPassword, false);
875875
}
876876

877+
@Test(expected = InvalidParameterValueException.class)
878+
public void validateUserPasswordAndUpdateIfNeededTestSaml2UserShouldNotBeAllowedToUpdateTheirPassword() {
879+
String newPassword = "newPassword";
880+
String currentPassword = "theCurrentPassword";
881+
882+
Mockito.when(userVoMock.getSource()).thenReturn(User.Source.SAML2);
883+
884+
accountManagerImpl.validateUserPasswordAndUpdateIfNeeded(newPassword, userVoMock, currentPassword);
885+
}
886+
887+
@Test(expected = InvalidParameterValueException.class)
888+
public void validateUserPasswordAndUpdateIfNeededTestSaml2DisabledUserShouldNotBeAllowedToUpdateTheirPassword() {
889+
String newPassword = "newPassword";
890+
String currentPassword = "theCurrentPassword";
891+
892+
Mockito.when(userVoMock.getSource()).thenReturn(User.Source.SAML2DISABLED);
893+
894+
accountManagerImpl.validateUserPasswordAndUpdateIfNeeded(newPassword, userVoMock, currentPassword);
895+
}
896+
897+
@Test(expected = InvalidParameterValueException.class)
898+
public void validateUserPasswordAndUpdateIfNeededTestLdapUserShouldNotBeAllowedToUpdateTheirPassword() {
899+
String newPassword = "newPassword";
900+
String currentPassword = "theCurrentPassword";
901+
902+
Mockito.when(userVoMock.getSource()).thenReturn(User.Source.LDAP);
903+
904+
accountManagerImpl.validateUserPasswordAndUpdateIfNeeded(newPassword, userVoMock, currentPassword);
905+
}
906+
877907
private String configureUserMockAuthenticators(String newPassword) {
878908
accountManagerImpl._userPasswordEncoders = new ArrayList<>();
879909
UserAuthenticator authenticatorMock1 = Mockito.mock(UserAuthenticator.class);

0 commit comments

Comments
 (0)