Skip to content

Commit 602faec

Browse files
committed
Revertng based on mail thread discussion "Fix findbug issues within LDAP authenticator"
This reverts commit 92b4f66.
1 parent 523cbdd commit 602faec

6 files changed

Lines changed: 152 additions & 137 deletions

File tree

‎plugins/user-authenticators/ldap/src/org/apache/cloudstack/api/command/LDAPConfigCmd.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ private LDAPConfigResponse createLDAPConfigResponse(String hostname, Integer por
209209
}
210210

211211
private boolean updateLDAP() {
212-
_ldapManager.addConfiguration(hostname, port);
212+
LdapConfigurationResponse response = _ldapManager.addConfiguration(hostname, port);
213213

214214
/**
215215
* There is no query filter now. It is derived from ldap.user.object and ldap.search.group.principle

‎plugins/user-authenticators/ldap/src/org/apache/cloudstack/api/command/LdapCreateAccountCmd.java‎

Lines changed: 122 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -35,133 +35,133 @@
3535
import org.apache.cloudstack.ldap.LdapManager;
3636
import org.apache.cloudstack.ldap.LdapUser;
3737
import org.apache.log4j.Logger;
38+
import org.bouncycastle.util.encoders.Base64;
3839

3940
import com.cloud.user.Account;
4041
import com.cloud.user.AccountService;
4142
import com.cloud.user.UserAccount;
4243

4344
@APICommand(name = "ldapCreateAccount", description = "Creates an account from an LDAP user", responseObject = AccountResponse.class, since = "4.2.0")
4445
public class LdapCreateAccountCmd extends BaseCmd {
45-
public static final Logger s_logger = Logger
46-
.getLogger(LdapCreateAccountCmd.class.getName());
47-
private static final String s_name = "createaccountresponse";
48-
49-
@Inject
50-
private LdapManager _ldapManager;
51-
52-
@Parameter(name = ApiConstants.ACCOUNT, type = CommandType.STRING, description = "Creates the user under the specified account. If no account is specified, the username will be used as the account name.")
53-
private String accountName;
54-
55-
@Parameter(name = ApiConstants.ACCOUNT_TYPE, type = CommandType.SHORT, required = true, description = "Type of the account. Specify 0 for user, 1 for root admin, and 2 for domain admin")
56-
private Short accountType;
57-
58-
@Parameter(name = ApiConstants.DOMAIN_ID, type = CommandType.UUID, entityType = DomainResponse.class, description = "Creates the user under the specified domain.")
59-
private Long domainId;
60-
61-
@Parameter(name = ApiConstants.TIMEZONE, type = CommandType.STRING, description = "Specifies a timezone for this command. For more information on the timezone parameter, see Time Zone Format.")
62-
private String timezone;
63-
64-
@Parameter(name = ApiConstants.USERNAME, type = CommandType.STRING, required = true, description = "Unique username.")
65-
private String username;
66-
67-
@Parameter(name = ApiConstants.NETWORK_DOMAIN, type = CommandType.STRING, description = "Network domain for the account's networks")
68-
private String networkDomain;
69-
70-
@Parameter(name = ApiConstants.ACCOUNT_DETAILS, type = CommandType.MAP, description = "details for account used to store specific parameters")
71-
private Map<String, String> details;
72-
73-
@Parameter(name = ApiConstants.ACCOUNT_ID, type = CommandType.STRING, description = "Account UUID, required for adding account from external provisioning system")
74-
private String accountUUID;
75-
76-
@Parameter(name = ApiConstants.USER_ID, type = CommandType.STRING, description = "User UUID, required for adding account from external provisioning system")
77-
private String userUUID;
78-
79-
public LdapCreateAccountCmd() {
80-
super();
81-
}
82-
83-
public LdapCreateAccountCmd(final LdapManager ldapManager,
84-
final AccountService accountService) {
85-
super();
86-
_ldapManager = ldapManager;
87-
_accountService = accountService;
88-
}
89-
90-
UserAccount createCloudstackUserAccount(final LdapUser user) {
91-
return _accountService.createUserAccount(username, generatePassword(),
92-
user.getFirstname(), user.getLastname(), user.getEmail(),
93-
timezone, accountName, accountType, domainId, networkDomain,
94-
details, accountUUID, userUUID);
95-
}
96-
97-
@Override
98-
public void execute() throws ServerApiException {
99-
final CallContext callContext = getCurrentContext();
100-
callContext.setEventDetails("Account Name: " + accountName
101-
+ ", Domain Id:" + domainId);
102-
try {
103-
final LdapUser user = _ldapManager.getUser(username);
104-
validateUser(user);
105-
final UserAccount userAccount = createCloudstackUserAccount(user);
106-
if (userAccount != null) {
107-
final AccountResponse response = _responseGenerator
108-
.createUserAccountResponse(userAccount);
109-
response.setResponseName(getCommandName());
110-
setResponseObject(response);
111-
} else {
112-
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR,
113-
"Failed to create a user account");
114-
}
115-
} catch (final NamingException e) {
116-
throw new ServerApiException(
117-
ApiErrorCode.RESOURCE_UNAVAILABLE_ERROR,
118-
"No LDAP user exists with the username of " + username);
119-
}
120-
}
121-
122-
private String generatePassword() throws ServerApiException {
123-
final SecureRandom random = new SecureRandom();
124-
final int length = 20;
125-
final String characters = "abcdefghjkmnpqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ23456789!@£$%^&*()_+=";
126-
127-
String password = "";
128-
for (int i = 0; i < length; i++) {
129-
int index = (int) (random.nextDouble() * characters.length());
130-
password += characters.charAt(index);
131-
}
132-
return password;
133-
}
134-
135-
@Override
136-
public String getCommandName() {
137-
return s_name;
138-
}
139-
140-
CallContext getCurrentContext() {
141-
return CallContext.current();
142-
}
143-
144-
@Override
145-
public long getEntityOwnerId() {
146-
return Account.ACCOUNT_ID_SYSTEM;
147-
}
148-
149-
private boolean validateUser(final LdapUser user) throws ServerApiException {
150-
if (user.getEmail() == null) {
151-
throw new ServerApiException(
152-
ApiErrorCode.RESOURCE_UNAVAILABLE_ERROR, username
153-
+ " has no email address set within LDAP");
154-
}
155-
if (user.getFirstname() == null) {
156-
throw new ServerApiException(
157-
ApiErrorCode.RESOURCE_UNAVAILABLE_ERROR, username
158-
+ " has no firstname set within LDAP");
159-
}
160-
if (user.getLastname() == null) {
161-
throw new ServerApiException(
162-
ApiErrorCode.RESOURCE_UNAVAILABLE_ERROR, username
163-
+ " has no lastname set within LDAP");
164-
}
165-
return true;
166-
}
46+
public static final Logger s_logger = Logger
47+
.getLogger(LdapCreateAccountCmd.class.getName());
48+
private static final String s_name = "createaccountresponse";
49+
50+
@Inject
51+
private LdapManager _ldapManager;
52+
53+
@Parameter(name = ApiConstants.ACCOUNT, type = CommandType.STRING, description = "Creates the user under the specified account. If no account is specified, the username will be used as the account name.")
54+
private String accountName;
55+
56+
@Parameter(name = ApiConstants.ACCOUNT_TYPE, type = CommandType.SHORT, required = true, description = "Type of the account. Specify 0 for user, 1 for root admin, and 2 for domain admin")
57+
private Short accountType;
58+
59+
@Parameter(name = ApiConstants.DOMAIN_ID, type = CommandType.UUID, entityType = DomainResponse.class, description = "Creates the user under the specified domain.")
60+
private Long domainId;
61+
62+
@Parameter(name = ApiConstants.TIMEZONE, type = CommandType.STRING, description = "Specifies a timezone for this command. For more information on the timezone parameter, see Time Zone Format.")
63+
private String timezone;
64+
65+
@Parameter(name = ApiConstants.USERNAME, type = CommandType.STRING, required = true, description = "Unique username.")
66+
private String username;
67+
68+
@Parameter(name = ApiConstants.NETWORK_DOMAIN, type = CommandType.STRING, description = "Network domain for the account's networks")
69+
private String networkDomain;
70+
71+
@Parameter(name = ApiConstants.ACCOUNT_DETAILS, type = CommandType.MAP, description = "details for account used to store specific parameters")
72+
private Map<String, String> details;
73+
74+
@Parameter(name = ApiConstants.ACCOUNT_ID, type = CommandType.STRING, description = "Account UUID, required for adding account from external provisioning system")
75+
private String accountUUID;
76+
77+
@Parameter(name = ApiConstants.USER_ID, type = CommandType.STRING, description = "User UUID, required for adding account from external provisioning system")
78+
private String userUUID;
79+
80+
public LdapCreateAccountCmd() {
81+
super();
82+
}
83+
84+
public LdapCreateAccountCmd(final LdapManager ldapManager,
85+
final AccountService accountService) {
86+
super();
87+
_ldapManager = ldapManager;
88+
_accountService = accountService;
89+
}
90+
91+
UserAccount createCloudstackUserAccount(final LdapUser user) {
92+
return _accountService.createUserAccount(username, generatePassword(),
93+
user.getFirstname(), user.getLastname(), user.getEmail(),
94+
timezone, accountName, accountType, domainId, networkDomain,
95+
details, accountUUID, userUUID);
96+
}
97+
98+
@Override
99+
public void execute() throws ServerApiException {
100+
final CallContext callContext = getCurrentContext();
101+
callContext.setEventDetails("Account Name: " + accountName
102+
+ ", Domain Id:" + domainId);
103+
try {
104+
final LdapUser user = _ldapManager.getUser(username);
105+
validateUser(user);
106+
final UserAccount userAccount = createCloudstackUserAccount(user);
107+
if (userAccount != null) {
108+
final AccountResponse response = _responseGenerator
109+
.createUserAccountResponse(userAccount);
110+
response.setResponseName(getCommandName());
111+
setResponseObject(response);
112+
} else {
113+
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR,
114+
"Failed to create a user account");
115+
}
116+
} catch (final NamingException e) {
117+
throw new ServerApiException(
118+
ApiErrorCode.RESOURCE_UNAVAILABLE_ERROR,
119+
"No LDAP user exists with the username of " + username);
120+
}
121+
}
122+
123+
private String generatePassword() throws ServerApiException {
124+
try {
125+
final SecureRandom randomGen = SecureRandom.getInstance("SHA1PRNG");
126+
final byte bytes[] = new byte[20];
127+
randomGen.nextBytes(bytes);
128+
return Base64.encode(bytes).toString();
129+
} catch (final NoSuchAlgorithmException e) {
130+
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR,
131+
"Failed to generate random password");
132+
}
133+
}
134+
135+
@Override
136+
public String getCommandName() {
137+
return s_name;
138+
}
139+
140+
CallContext getCurrentContext() {
141+
return CallContext.current();
142+
}
143+
144+
@Override
145+
public long getEntityOwnerId() {
146+
return Account.ACCOUNT_ID_SYSTEM;
147+
}
148+
149+
private boolean validateUser(final LdapUser user) throws ServerApiException {
150+
if (user.getEmail() == null) {
151+
throw new ServerApiException(
152+
ApiErrorCode.RESOURCE_UNAVAILABLE_ERROR, username
153+
+ " has no email address set within LDAP");
154+
}
155+
if (user.getFirstname() == null) {
156+
throw new ServerApiException(
157+
ApiErrorCode.RESOURCE_UNAVAILABLE_ERROR, username
158+
+ " has no firstname set within LDAP");
159+
}
160+
if (user.getLastname() == null) {
161+
throw new ServerApiException(
162+
ApiErrorCode.RESOURCE_UNAVAILABLE_ERROR, username
163+
+ " has no lastname set within LDAP");
164+
}
165+
return true;
166+
}
167167
}

‎plugins/user-authenticators/ldap/src/org/apache/cloudstack/api/command/LdapImportUsersCmd.java‎

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.apache.cloudstack.ldap.NoLdapUserMatchingQueryException;
3535
import org.apache.commons.lang.StringUtils;
3636
import org.apache.log4j.Logger;
37+
import org.bouncycastle.util.encoders.Base64;
3738

3839
import com.cloud.domain.Domain;
3940
import com.cloud.exception.*;
@@ -170,15 +171,13 @@ public String getCommandName() {
170171
}
171172

172173
private String generatePassword() throws ServerApiException {
173-
final SecureRandom random = new SecureRandom();
174-
final int length = 20;
175-
final String characters = "abcdefghjkmnpqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ23456789!@£$%^&*()_+=";
176-
177-
String password = "";
178-
for (int i = 0; i < length; i++) {
179-
int index = (int) (random.nextDouble() * characters.length());
180-
password += characters.charAt(index);
174+
try {
175+
final SecureRandom randomGen = SecureRandom.getInstance("SHA1PRNG");
176+
final byte bytes[] = new byte[20];
177+
randomGen.nextBytes(bytes);
178+
return Base64.encode(bytes).toString();
179+
} catch (final NoSuchAlgorithmException e) {
180+
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to generate random password");
181181
}
182-
return password;
183182
}
184183
}

‎plugins/user-authenticators/ldap/src/org/apache/cloudstack/ldap/LdapContextFactory.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ private Hashtable<String, String> getEnvironment(final String principal,
9595

9696
environment.put(Context.INITIAL_CONTEXT_FACTORY, factory);
9797
environment.put(Context.PROVIDER_URL, url);
98+
environment.put("com.sun.jndi.ldap.read.timeout", "500");
99+
environment.put("com.sun.jndi.ldap.connect.pool", "true");
98100

99101
enableSSL(environment);
100102
setAuthentication(environment, isSystemContext);

‎plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/LdapAuthenticatorSpec.groovy‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class LdapAuthenticatorSpec extends spock.lang.Specification {
3434
when: "A user authentications"
3535
def result = ldapAuthenticator.authenticate("rmurphy", "password", 0, null)
3636
then: "their authentication fails"
37-
result.first() == false
37+
result == false
3838
}
3939

4040
def "Test failed authentication due to ldap bind being unsuccessful"() {
@@ -51,7 +51,7 @@ class LdapAuthenticatorSpec extends spock.lang.Specification {
5151
def result = ldapAuthenticator.authenticate("rmurphy", "password", 0, null)
5252

5353
then: "their authentication fails"
54-
result.first() == false
54+
result == false
5555
}
5656

5757
def "Test failed authentication due to ldap not being configured"() {
@@ -66,7 +66,7 @@ class LdapAuthenticatorSpec extends spock.lang.Specification {
6666
when: "The user authenticates"
6767
def result = ldapAuthenticator.authenticate("rmurphy", "password", 0, null)
6868
then: "their authentication fails"
69-
result.first() == false
69+
result == false
7070
}
7171

7272
def "Test successful authentication"() {
@@ -83,7 +83,7 @@ class LdapAuthenticatorSpec extends spock.lang.Specification {
8383
def result = ldapAuthenticator.authenticate("rmurphy", "password", 0, null)
8484

8585
then: "their authentication passes"
86-
result.first() == true
86+
result == true
8787
}
8888

8989
def "Test that encode doesn't change the input"() {

‎plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/LdapManagerImplSpec.groovy‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,16 +297,30 @@ class LdapManagerImplSpec extends spock.lang.Specification {
297297
thrown InvalidParameterValueException
298298
}
299299

300+
def supportedLdapCommands() {
301+
List<Class<?>> cmdList = new ArrayList<Class<?>>();
302+
cmdList.add(LdapUserSearchCmd.class);
303+
cmdList.add(LdapListUsersCmd.class);
304+
cmdList.add(LdapAddConfigurationCmd.class);
305+
cmdList.add(LdapDeleteConfigurationCmd.class);
306+
cmdList.add(LdapListConfigurationCmd.class);
307+
cmdList.add(LdapCreateAccountCmd.class);
308+
cmdList.add(LdapImportUsersCmd.class);
309+
return cmdList
310+
}
311+
300312
def "Test that getCommands isn't empty"() {
301313
given: "We have an LdapConfigurationDao, LdapContextFactory, LdapUserManager and LdapManager"
302314
def ldapConfigurationDao = Mock(LdapConfigurationDaoImpl)
303315
def ldapContextFactory = Mock(LdapContextFactory)
304316
def ldapUserManager = Mock(LdapUserManager)
317+
final List<Class<?>> cmdList = supportedLdapCommands()
305318
def ldapManager = new LdapManagerImpl(ldapConfigurationDao, ldapContextFactory, ldapUserManager)
306319
when: "Get commands is called"
307320
def result = ldapManager.getCommands()
308-
then: "it must contain commands"
321+
then: "it must return all the commands"
309322
result.size() > 0
323+
result == cmdList
310324
}
311325

312326
def "Testing of listConfigurations"() {

0 commit comments

Comments
 (0)