Skip to content

Commit 8deca7b

Browse files
author
Daan Hoogland
committed
cleanup
1 parent 99919fd commit 8deca7b

25 files changed

+287
-383
lines changed

plugins/user-authenticators/ldap/src/main/java/org/apache/cloudstack/api/command/LdapAddConfigurationCmd.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,9 @@ public class LdapAddConfigurationCmd extends BaseCmd {
4646
@Parameter(name = ApiConstants.PORT, type = CommandType.INTEGER, required = true, description = "Port")
4747
private int port;
4848

49-
@Parameter(name = ApiConstants.DOMAIN_ID, type = CommandType.UUID, required = false, entityType = DomainResponse.class, description = "linked domain")
49+
@Parameter(name = ApiConstants.DOMAIN_ID, type = CommandType.UUID, entityType = DomainResponse.class, description = "linked domain")
5050
private Long domainId;
5151

52-
public LdapAddConfigurationCmd() {
53-
super();
54-
}
55-
5652
public LdapAddConfigurationCmd(final LdapManager ldapManager) {
5753
super();
5854
_ldapManager = ldapManager;

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

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import com.cloud.user.AccountService;
2222
import com.cloud.user.User;
2323
import com.cloud.user.UserAccount;
24+
import com.cloud.utils.StringUtils;
2425
import org.apache.cloudstack.acl.RoleType;
2526
import org.apache.cloudstack.api.APICommand;
2627
import org.apache.cloudstack.api.ApiConstants;
@@ -39,7 +40,6 @@
3940
import org.bouncycastle.util.encoders.Base64;
4041

4142
import javax.inject.Inject;
42-
import java.io.UnsupportedEncodingException;
4343
import java.security.NoSuchAlgorithmException;
4444
import java.security.SecureRandom;
4545
import java.util.Map;
@@ -81,10 +81,6 @@ public class LdapCreateAccountCmd extends BaseCmd {
8181
@Parameter(name = ApiConstants.USER_ID, type = CommandType.STRING, description = "User UUID, required for adding account from external provisioning system")
8282
private String userUUID;
8383

84-
public LdapCreateAccountCmd() {
85-
super();
86-
}
87-
8884
public LdapCreateAccountCmd(final LdapManager ldapManager, final AccountService accountService) {
8985
super();
9086
_ldapManager = ldapManager;
@@ -107,7 +103,7 @@ public Account.Type getAccountType() {
107103
if (accountType == null) {
108104
return RoleType.getAccountTypeByRole(roleService.findRole(roleId), null);
109105
}
110-
return RoleType.getAccountTypeByRole(roleService.findRole(roleId), Account.Type.getFromValue(accountType.intValue()));
106+
return RoleType.getAccountTypeByRole(roleService.findRole(roleId), Account.Type.getFromValue(accountType));
111107
}
112108

113109
public Long getRoleId() {
@@ -158,10 +154,10 @@ public void execute() throws ServerApiException {
158154
private String generatePassword() throws ServerApiException {
159155
try {
160156
final SecureRandom randomGen = SecureRandom.getInstance("SHA1PRNG");
161-
final byte bytes[] = new byte[20];
157+
final byte[] bytes = new byte[20];
162158
randomGen.nextBytes(bytes);
163-
return new String(Base64.encode(bytes), "UTF-8");
164-
} catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
159+
return new String(Base64.encode(bytes), StringUtils.getPreferredCharset());
160+
} catch (NoSuchAlgorithmException e) {
165161
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to generate random password");
166162
}
167163
}
@@ -180,7 +176,7 @@ public long getEntityOwnerId() {
180176
return Account.ACCOUNT_ID_SYSTEM;
181177
}
182178

183-
private boolean validateUser(final LdapUser user) throws ServerApiException {
179+
private void validateUser(final LdapUser user) throws ServerApiException {
184180
if (user.getEmail() == null) {
185181
throw new ServerApiException(ApiErrorCode.RESOURCE_UNAVAILABLE_ERROR, username + " has no email address set within LDAP");
186182
}
@@ -190,6 +186,5 @@ private boolean validateUser(final LdapUser user) throws ServerApiException {
190186
if (user.getLastname() == null) {
191187
throw new ServerApiException(ApiErrorCode.RESOURCE_UNAVAILABLE_ERROR, username + " has no lastname set within LDAP");
192188
}
193-
return true;
194189
}
195190
}

plugins/user-authenticators/ldap/src/main/java/org/apache/cloudstack/api/command/LdapDeleteConfigurationCmd.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,12 @@ public class LdapDeleteConfigurationCmd extends BaseCmd {
4444
@Parameter(name = ApiConstants.HOST_NAME, type = CommandType.STRING, required = true, description = "Hostname")
4545
private String hostname;
4646

47-
@Parameter(name = ApiConstants.PORT, type = CommandType.INTEGER, required = false, description = "port")
47+
@Parameter(name = ApiConstants.PORT, type = CommandType.INTEGER, description = "port")
4848
private int port;
4949

50-
@Parameter(name = ApiConstants.DOMAIN_ID, type = CommandType.UUID, required = false, entityType = DomainResponse.class, description = "linked domain")
50+
@Parameter(name = ApiConstants.DOMAIN_ID, type = CommandType.UUID, entityType = DomainResponse.class, description = "linked domain")
5151
private Long domainId;
5252

53-
public LdapDeleteConfigurationCmd() {
54-
super();
55-
}
56-
5753
public LdapDeleteConfigurationCmd(final LdapManager ldapManager) {
5854
super();
5955
_ldapManager = ldapManager;

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

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
// under the License.
1717
package org.apache.cloudstack.api.command;
1818

19-
import java.io.UnsupportedEncodingException;
2019
import java.security.NoSuchAlgorithmException;
2120
import java.security.SecureRandom;
2221
import java.util.ArrayList;
@@ -41,7 +40,6 @@
4140
import org.apache.cloudstack.ldap.LdapManager;
4241
import org.apache.cloudstack.ldap.LdapUser;
4342
import org.apache.cloudstack.ldap.NoLdapUserMatchingQueryException;
44-
import org.apache.commons.lang3.StringUtils;
4543
import org.bouncycastle.util.encoders.Base64;
4644

4745
import com.cloud.domain.Domain;
@@ -56,6 +54,7 @@
5654
import com.cloud.user.DomainService;
5755
import com.cloud.user.User;
5856
import com.cloud.user.UserAccount;
57+
import com.cloud.utils.StringUtils;
5958

6059
@APICommand(name = "importLdapUsers", description = "Import LDAP users", responseObject = LdapUserResponse.class, since = "4.3.0", requestHasSensitiveInfo = false, responseHasSensitiveInfo = false)
6160
public class LdapImportUsersCmd extends BaseListCmd {
@@ -92,10 +91,6 @@ public class LdapImportUsersCmd extends BaseListCmd {
9291
@Inject
9392
private LdapManager _ldapManager;
9493

95-
public LdapImportUsersCmd() {
96-
super();
97-
}
98-
9994
public LdapImportUsersCmd(final LdapManager ldapManager, final DomainService domainService, final AccountService accountService) {
10095
super();
10196
_ldapManager = ldapManager;
@@ -106,14 +101,14 @@ public LdapImportUsersCmd(final LdapManager ldapManager, final DomainService dom
106101
private void createCloudstackUserAccount(LdapUser user, String accountName, Domain domain) {
107102
Account account = _accountService.getActiveAccountByName(accountName, domain.getId());
108103
if (account == null) {
109-
logger.debug("No account exists with name: " + accountName + " creating the account and an user with name: " + user.getUsername() + " in the account");
104+
logger.debug("No account exists with name: {} creating the account and an user with name: {} in the account", accountName, user.getUsername());
110105
_accountService.createUserAccount(user.getUsername(), generatePassword(), user.getFirstname(), user.getLastname(), user.getEmail(), timezone, accountName, getAccountType(), getRoleId(),
111106
domain.getId(), domain.getNetworkDomain(), details, UUID.randomUUID().toString(), UUID.randomUUID().toString(), User.Source.LDAP);
112107
} else {
113108
// check if the user exists. if yes, call update
114109
UserAccount csuser = _accountService.getActiveUserAccount(user.getUsername(), domain.getId());
115110
if (csuser == null) {
116-
logger.debug("No user exists with name: " + user.getUsername() + " creating a user in the account: " + accountName);
111+
logger.debug("No user exists with name: {} creating a user in the account: {}", user.getUsername(), accountName);
117112
_accountService.createUser(user.getUsername(), generatePassword(), user.getFirstname(), user.getLastname(), user.getEmail(), timezone, accountName, domain.getId(),
118113
UUID.randomUUID().toString(), User.Source.LDAP);
119114
} else {
@@ -145,21 +140,21 @@ public void execute()
145140
users = _ldapManager.getUsers(domainId);
146141
}
147142
} catch (NoLdapUserMatchingQueryException ex) {
148-
users = new ArrayList<LdapUser>();
149-
logger.info("No Ldap user matching query. " + " ::: " + ex.getMessage());
143+
users = new ArrayList<>();
144+
logger.info("No Ldap user matching query. ::: {}", ex.getMessage());
150145
}
151146

152-
List<LdapUser> addedUsers = new ArrayList<LdapUser>();
147+
List<LdapUser> addedUsers = new ArrayList<>();
153148
for (LdapUser user : users) {
154149
Domain domain = getDomain(user);
155150
try {
156151
createCloudstackUserAccount(user, getAccountName(user), domain);
157152
addedUsers.add(user);
158153
} catch (InvalidParameterValueException ex) {
159-
logger.error("Failed to create user with username: " + user.getUsername() + " ::: " + ex.getMessage());
154+
logger.error("Failed to create user with username: {} ::: {}", user.getUsername(), ex.getMessage());
160155
}
161156
}
162-
ListResponse<LdapUserResponse> response = new ListResponse<LdapUserResponse>();
157+
ListResponse<LdapUserResponse> response = new ListResponse<>();
163158
response.setResponses(createLdapUserResponse(addedUsers));
164159
response.setResponseName(getCommandName());
165160
setResponseObject(response);
@@ -169,7 +164,7 @@ public Account.Type getAccountType() {
169164
if (accountType == null) {
170165
return RoleType.getAccountTypeByRole(roleService.findRole(roleId), null);
171166
}
172-
return RoleType.getAccountTypeByRole(roleService.findRole(roleId), Account.Type.getFromValue(accountType.intValue()));
167+
return RoleType.getAccountTypeByRole(roleService.findRole(roleId), Account.Type.getFromValue(accountType));
173168
}
174169

175170
public Long getRoleId() {
@@ -202,11 +197,11 @@ private Domain getDomainForName(String name) {
202197
private Domain getDomain(LdapUser user) {
203198
Domain domain;
204199
if (_domain != null) {
205-
//this means either domain id or groupname is passed and this will be same for all the users in this call. hence returning it.
200+
//this means either domain id or group name is passed and this will be same for all the users in this call. hence returning it.
206201
domain = _domain;
207202
} else {
208203
if (domainId != null) {
209-
// a domain Id is passed. use it for this user and all the users in the same api call (by setting _domain)
204+
// a domain ID is passed. use it for this user and all the users in the same api call (by setting _domain)
210205
domain = _domain = _domainService.getDomain(domainId);
211206
} else {
212207
// a group name is passed. use it for this user and all the users in the same api call(by setting _domain)
@@ -225,7 +220,7 @@ private Domain getDomain(LdapUser user) {
225220
}
226221

227222
private List<LdapUserResponse> createLdapUserResponse(List<LdapUser> users) {
228-
final List<LdapUserResponse> ldapResponses = new ArrayList<LdapUserResponse>();
223+
final List<LdapUserResponse> ldapResponses = new ArrayList<>();
229224
for (final LdapUser user : users) {
230225
final LdapUserResponse ldapResponse = _ldapManager.createLdapUserResponse(user);
231226
ldapResponse.setObjectName("LdapUser");
@@ -242,10 +237,10 @@ public String getCommandName() {
242237
private String generatePassword() throws ServerApiException {
243238
try {
244239
final SecureRandom randomGen = SecureRandom.getInstance("SHA1PRNG");
245-
final byte bytes[] = new byte[20];
240+
final byte[] bytes = new byte[20];
246241
randomGen.nextBytes(bytes);
247-
return new String(Base64.encode(bytes), "UTF-8");
248-
} catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
242+
return new String(Base64.encode(bytes), com.cloud.utils.StringUtils.getPreferredCharset());
243+
} catch (NoSuchAlgorithmException e) {
249244
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to generate random password");
250245
}
251246
}

plugins/user-authenticators/ldap/src/main/java/org/apache/cloudstack/api/command/LdapListConfigurationCmd.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,19 @@ public class LdapListConfigurationCmd extends BaseListCmd {
4444
@Inject
4545
private LdapManager _ldapManager;
4646

47-
@Parameter(name = ApiConstants. HOST_NAME, type = CommandType.STRING, required = false, description = "Hostname")
47+
@Parameter(name = ApiConstants. HOST_NAME, type = CommandType.STRING, description = "Hostname")
4848
private String hostname;
4949

50-
@Parameter(name = ApiConstants.PORT, type = CommandType.INTEGER, required = false, description = "Port")
50+
@Parameter(name = ApiConstants.PORT, type = CommandType.INTEGER, description = "Port")
5151
private int port;
5252

53-
@Parameter(name = ApiConstants.DOMAIN_ID, type = CommandType.UUID, required = false, entityType = DomainResponse.class, description = "linked domain")
53+
@Parameter(name = ApiConstants.DOMAIN_ID, type = CommandType.UUID, entityType = DomainResponse.class,
54+
description = "linked domain")
5455
private Long domainId;
5556

56-
@Parameter(name = ApiConstants.LIST_ALL, type = CommandType.BOOLEAN, description = "If set to true, "
57-
+ " and no domainid specified, list all LDAP configurations irrespective of the linked domain", since = "4.13.2")
57+
@Parameter(name = ApiConstants.LIST_ALL, type = CommandType.BOOLEAN,
58+
description = "If set to true, and no `domainid` specified, list all LDAP configurations irrespective of the linked domain",
59+
since = "4.13.2")
5860
private Boolean listAll;
5961

6062
public LdapListConfigurationCmd() {
@@ -67,7 +69,7 @@ public LdapListConfigurationCmd(final LdapManager ldapManager) {
6769
}
6870

6971
private List<LdapConfigurationResponse> createLdapConfigurationResponses(final List<? extends LdapConfigurationVO> configurations) {
70-
final List<LdapConfigurationResponse> responses = new ArrayList<LdapConfigurationResponse>();
72+
final List<LdapConfigurationResponse> responses = new ArrayList<>();
7173
for (final LdapConfigurationVO resource : configurations) {
7274
final LdapConfigurationResponse configurationResponse = _ldapManager.createLdapConfigurationResponse(resource);
7375
configurationResponse.setObjectName("LdapConfiguration");
@@ -80,7 +82,7 @@ private List<LdapConfigurationResponse> createLdapConfigurationResponses(final L
8082
public void execute() {
8183
final Pair<List<? extends LdapConfigurationVO>, Integer> result = _ldapManager.listConfigurations(this);
8284
final List<LdapConfigurationResponse> responses = createLdapConfigurationResponses(result.first());
83-
final ListResponse<LdapConfigurationResponse> response = new ListResponse<LdapConfigurationResponse>();
85+
final ListResponse<LdapConfigurationResponse> response = new ListResponse<>();
8486
response.setResponses(responses, result.second());
8587
response.setResponseName(getCommandName());
8688
setResponseObject(response);

0 commit comments

Comments
 (0)