diff --git a/plugins/user-authenticators/ldap/src/org/apache/cloudstack/api/command/LdapAddConfigurationCmd.java b/plugins/user-authenticators/ldap/src/org/apache/cloudstack/api/command/LdapAddConfigurationCmd.java index 37376179229..4082623cb2d 100644 --- a/plugins/user-authenticators/ldap/src/org/apache/cloudstack/api/command/LdapAddConfigurationCmd.java +++ b/plugins/user-authenticators/ldap/src/org/apache/cloudstack/api/command/LdapAddConfigurationCmd.java @@ -33,67 +33,67 @@ import com.cloud.user.Account; @APICommand(name = "addLdapConfiguration", description = "Add a new Ldap Configuration", responseObject = LdapConfigurationResponse.class, since = "4.2.0") public class LdapAddConfigurationCmd extends BaseCmd { - public static final Logger s_logger = Logger - .getLogger(LdapAddConfigurationCmd.class.getName()); - private static final String s_name = "ldapconfigurationresponse"; + public static final Logger s_logger = Logger + .getLogger(LdapAddConfigurationCmd.class.getName()); + private static final String s_name = "ldapconfigurationresponse"; - @Inject - private LdapManager _ldapManager; + @Inject + private LdapManager _ldapManager; - @Parameter(name = "hostname", type = CommandType.STRING, required = true, description = "Hostname") - private String hostname; + @Parameter(name = "hostname", type = CommandType.STRING, required = true, description = "Hostname") + private String hostname; - @Parameter(name = "port", type = CommandType.INTEGER, required = true, description = "Port") - private int port; + @Parameter(name = "port", type = CommandType.INTEGER, required = true, description = "Port") + private int port; - public LdapAddConfigurationCmd() { - super(); - } + public LdapAddConfigurationCmd() { + super(); + } - public LdapAddConfigurationCmd(final LdapManager ldapManager) { - super(); - _ldapManager = ldapManager; - } + public LdapAddConfigurationCmd(final LdapManager ldapManager) { + super(); + _ldapManager = ldapManager; + } - @Override - public void execute() throws ServerApiException { - try { - final LdapConfigurationResponse response = _ldapManager - .addConfiguration(hostname, port); - response.setObjectName("LdapAddConfiguration"); - response.setResponseName(getCommandName()); - setResponseObject(response); - } catch (final InvalidParameterValueException e) { - throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, - e.toString()); - } + @Override + public void execute() throws ServerApiException { + try { + final LdapConfigurationResponse response = _ldapManager + .addConfiguration(hostname, port); + response.setObjectName("LdapAddConfiguration"); + response.setResponseName(getCommandName()); + setResponseObject(response); + } catch (final InvalidParameterValueException e) { + throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, + e.toString()); + } - } + } - @Override - public String getCommandName() { - return s_name; - } + @Override + public String getCommandName() { + return s_name; + } - @Override - public long getEntityOwnerId() { - return Account.ACCOUNT_ID_SYSTEM; - } + @Override + public long getEntityOwnerId() { + return Account.ACCOUNT_ID_SYSTEM; + } - public String getHostname() { - return hostname; - } + public String getHostname() { + return hostname; + } - public int getPort() { - return port; - } + public int getPort() { + return port; + } - public void setHostname(final String hostname) { - this.hostname = hostname; - } + public void setHostname(final String hostname) { + this.hostname = hostname; + } - public void setPort(final int port) { - this.port = port; - } + public void setPort(final int port) { + this.port = port; + } } diff --git a/plugins/user-authenticators/ldap/src/org/apache/cloudstack/api/command/LdapCreateAccountCmd.java b/plugins/user-authenticators/ldap/src/org/apache/cloudstack/api/command/LdapCreateAccountCmd.java index 981e72e64e1..931fd3203cc 100644 --- a/plugins/user-authenticators/ldap/src/org/apache/cloudstack/api/command/LdapCreateAccountCmd.java +++ b/plugins/user-authenticators/ldap/src/org/apache/cloudstack/api/command/LdapCreateAccountCmd.java @@ -43,125 +43,125 @@ import com.cloud.user.UserAccount; @APICommand(name = "ldapCreateAccount", description = "Creates an account from an LDAP user", responseObject = AccountResponse.class, since = "4.2.0") public class LdapCreateAccountCmd extends BaseCmd { - public static final Logger s_logger = Logger - .getLogger(LdapCreateAccountCmd.class.getName()); - private static final String s_name = "createaccountresponse"; + public static final Logger s_logger = Logger + .getLogger(LdapCreateAccountCmd.class.getName()); + private static final String s_name = "createaccountresponse"; - @Inject - private LdapManager _ldapManager; + @Inject + private LdapManager _ldapManager; - @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.") - private String accountName; + @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.") + private String accountName; - @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") - private Short accountType; + @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") + private Short accountType; - @Parameter(name = ApiConstants.DOMAIN_ID, type = CommandType.UUID, entityType = DomainResponse.class, description = "Creates the user under the specified domain.") - private Long domainId; + @Parameter(name = ApiConstants.DOMAIN_ID, type = CommandType.UUID, entityType = DomainResponse.class, description = "Creates the user under the specified domain.") + private Long domainId; - @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.") - private String timezone; + @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.") + private String timezone; - @Parameter(name = ApiConstants.USERNAME, type = CommandType.STRING, required = true, description = "Unique username.") - private String username; + @Parameter(name = ApiConstants.USERNAME, type = CommandType.STRING, required = true, description = "Unique username.") + private String username; - @Parameter(name = ApiConstants.NETWORK_DOMAIN, type = CommandType.STRING, description = "Network domain for the account's networks") - private String networkDomain; + @Parameter(name = ApiConstants.NETWORK_DOMAIN, type = CommandType.STRING, description = "Network domain for the account's networks") + private String networkDomain; - @Parameter(name = ApiConstants.ACCOUNT_DETAILS, type = CommandType.MAP, description = "details for account used to store specific parameters") - private Map details; + @Parameter(name = ApiConstants.ACCOUNT_DETAILS, type = CommandType.MAP, description = "details for account used to store specific parameters") + private Map details; - @Parameter(name = ApiConstants.ACCOUNT_ID, type = CommandType.STRING, description = "Account UUID, required for adding account from external provisioning system") - private String accountUUID; + @Parameter(name = ApiConstants.ACCOUNT_ID, type = CommandType.STRING, description = "Account UUID, required for adding account from external provisioning system") + private String accountUUID; - @Parameter(name = ApiConstants.USER_ID, type = CommandType.STRING, description = "User UUID, required for adding account from external provisioning system") - private String userUUID; + @Parameter(name = ApiConstants.USER_ID, type = CommandType.STRING, description = "User UUID, required for adding account from external provisioning system") + private String userUUID; - public LdapCreateAccountCmd() { - super(); - } + public LdapCreateAccountCmd() { + super(); + } - public LdapCreateAccountCmd(final LdapManager ldapManager, - final AccountService accountService) { - super(); - _ldapManager = ldapManager; - _accountService = accountService; - } + public LdapCreateAccountCmd(final LdapManager ldapManager, + final AccountService accountService) { + super(); + _ldapManager = ldapManager; + _accountService = accountService; + } - UserAccount createCloudstackUserAccount(final LdapUser user) { - return _accountService.createUserAccount(username, generatePassword(), - user.getFirstname(), user.getLastname(), user.getEmail(), - timezone, accountName, accountType, domainId, networkDomain, - details, accountUUID, userUUID); - } + UserAccount createCloudstackUserAccount(final LdapUser user) { + return _accountService.createUserAccount(username, generatePassword(), + user.getFirstname(), user.getLastname(), user.getEmail(), + timezone, accountName, accountType, domainId, networkDomain, + details, accountUUID, userUUID); + } - @Override - public void execute() throws ServerApiException { - final CallContext callContext = getCurrentContext(); - callContext.setEventDetails("Account Name: " + accountName - + ", Domain Id:" + domainId); - try { - final LdapUser user = _ldapManager.getUser(username); - validateUser(user); - final UserAccount userAccount = createCloudstackUserAccount(user); - if (userAccount != null) { - final AccountResponse response = _responseGenerator - .createUserAccountResponse(userAccount); - response.setResponseName(getCommandName()); - setResponseObject(response); - } else { - throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, - "Failed to create a user account"); - } - } catch (final NamingException e) { - throw new ServerApiException( - ApiErrorCode.RESOURCE_UNAVAILABLE_ERROR, - "No LDAP user exists with the username of " + username); - } - } + @Override + public void execute() throws ServerApiException { + final CallContext callContext = getCurrentContext(); + callContext.setEventDetails("Account Name: " + accountName + + ", Domain Id:" + domainId); + try { + final LdapUser user = _ldapManager.getUser(username); + validateUser(user); + final UserAccount userAccount = createCloudstackUserAccount(user); + if (userAccount != null) { + final AccountResponse response = _responseGenerator + .createUserAccountResponse(userAccount); + response.setResponseName(getCommandName()); + setResponseObject(response); + } else { + throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, + "Failed to create a user account"); + } + } catch (final NamingException e) { + throw new ServerApiException( + ApiErrorCode.RESOURCE_UNAVAILABLE_ERROR, + "No LDAP user exists with the username of " + username); + } + } - private String generatePassword() throws ServerApiException { - try { - final SecureRandom randomGen = SecureRandom.getInstance("SHA1PRNG"); - final byte bytes[] = new byte[20]; - randomGen.nextBytes(bytes); - return Base64.encode(bytes).toString(); - } catch (final NoSuchAlgorithmException e) { - throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, - "Failed to generate random password"); - } - } + private String generatePassword() throws ServerApiException { + try { + final SecureRandom randomGen = SecureRandom.getInstance("SHA1PRNG"); + final byte bytes[] = new byte[20]; + randomGen.nextBytes(bytes); + return Base64.encode(bytes).toString(); + } catch (final NoSuchAlgorithmException e) { + throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, + "Failed to generate random password"); + } + } - @Override - public String getCommandName() { - return s_name; - } + @Override + public String getCommandName() { + return s_name; + } - CallContext getCurrentContext() { - return CallContext.current(); - } + CallContext getCurrentContext() { + return CallContext.current(); + } - @Override - public long getEntityOwnerId() { - return Account.ACCOUNT_ID_SYSTEM; - } + @Override + public long getEntityOwnerId() { + return Account.ACCOUNT_ID_SYSTEM; + } - private boolean validateUser(final LdapUser user) throws ServerApiException { - if (user.getEmail() == null) { - throw new ServerApiException( - ApiErrorCode.RESOURCE_UNAVAILABLE_ERROR, username - + " has no email address set within LDAP"); - } - if (user.getFirstname() == null) { - throw new ServerApiException( - ApiErrorCode.RESOURCE_UNAVAILABLE_ERROR, username - + " has no firstname set within LDAP"); - } - if (user.getLastname() == null) { - throw new ServerApiException( - ApiErrorCode.RESOURCE_UNAVAILABLE_ERROR, username - + " has no lastname set within LDAP"); - } - return true; - } + private boolean validateUser(final LdapUser user) throws ServerApiException { + if (user.getEmail() == null) { + throw new ServerApiException( + ApiErrorCode.RESOURCE_UNAVAILABLE_ERROR, username + + " has no email address set within LDAP"); + } + if (user.getFirstname() == null) { + throw new ServerApiException( + ApiErrorCode.RESOURCE_UNAVAILABLE_ERROR, username + + " has no firstname set within LDAP"); + } + if (user.getLastname() == null) { + throw new ServerApiException( + ApiErrorCode.RESOURCE_UNAVAILABLE_ERROR, username + + " has no lastname set within LDAP"); + } + return true; + } } \ No newline at end of file diff --git a/plugins/user-authenticators/ldap/src/org/apache/cloudstack/api/command/LdapDeleteConfigurationCmd.java b/plugins/user-authenticators/ldap/src/org/apache/cloudstack/api/command/LdapDeleteConfigurationCmd.java index d57c9a39985..a759c44ac6b 100644 --- a/plugins/user-authenticators/ldap/src/org/apache/cloudstack/api/command/LdapDeleteConfigurationCmd.java +++ b/plugins/user-authenticators/ldap/src/org/apache/cloudstack/api/command/LdapDeleteConfigurationCmd.java @@ -32,47 +32,47 @@ import com.cloud.user.Account; @APICommand(name = "deleteLdapConfiguration", description = "Remove an Ldap Configuration", responseObject = LdapConfigurationResponse.class, since = "4.2.0") public class LdapDeleteConfigurationCmd extends BaseCmd { - public static final Logger s_logger = Logger - .getLogger(LdapDeleteConfigurationCmd.class.getName()); - private static final String s_name = "ldapconfigurationresponse"; + public static final Logger s_logger = Logger + .getLogger(LdapDeleteConfigurationCmd.class.getName()); + private static final String s_name = "ldapconfigurationresponse"; - @Inject - private LdapManager _ldapManager; + @Inject + private LdapManager _ldapManager; - @Parameter(name = "hostname", type = CommandType.STRING, required = true, description = "Hostname") - private String hostname; + @Parameter(name = "hostname", type = CommandType.STRING, required = true, description = "Hostname") + private String hostname; - public LdapDeleteConfigurationCmd() { - super(); - } + public LdapDeleteConfigurationCmd() { + super(); + } - public LdapDeleteConfigurationCmd(final LdapManager ldapManager) { - super(); - _ldapManager = ldapManager; - } + public LdapDeleteConfigurationCmd(final LdapManager ldapManager) { + super(); + _ldapManager = ldapManager; + } - @Override - public void execute() throws ServerApiException { - try { - final LdapConfigurationResponse response = _ldapManager - .deleteConfiguration(hostname); - response.setObjectName("LdapDeleteConfiguration"); - response.setResponseName(getCommandName()); - setResponseObject(response); - } catch (final InvalidParameterValueException e) { - throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, - e.toString()); - } + @Override + public void execute() throws ServerApiException { + try { + final LdapConfigurationResponse response = _ldapManager + .deleteConfiguration(hostname); + response.setObjectName("LdapDeleteConfiguration"); + response.setResponseName(getCommandName()); + setResponseObject(response); + } catch (final InvalidParameterValueException e) { + throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, + e.toString()); + } - } + } - @Override - public String getCommandName() { - return s_name; - } + @Override + public String getCommandName() { + return s_name; + } - @Override - public long getEntityOwnerId() { - return Account.ACCOUNT_ID_SYSTEM; - } + @Override + public long getEntityOwnerId() { + return Account.ACCOUNT_ID_SYSTEM; + } } diff --git a/plugins/user-authenticators/ldap/src/org/apache/cloudstack/api/command/LdapImportUsersCmd.java b/plugins/user-authenticators/ldap/src/org/apache/cloudstack/api/command/LdapImportUsersCmd.java index 1855d5d41c5..24fb9463d52 100644 --- a/plugins/user-authenticators/ldap/src/org/apache/cloudstack/api/command/LdapImportUsersCmd.java +++ b/plugins/user-authenticators/ldap/src/org/apache/cloudstack/api/command/LdapImportUsersCmd.java @@ -58,12 +58,12 @@ public class LdapImportUsersCmd extends BaseListCmd { private Map details; @Parameter(name = ApiConstants.DOMAIN_ID, type = CommandType.UUID, entityType = DomainResponse.class, description = "Specifies the domain to which the ldap users are to be " - + "imported. If no domain is specified, a domain will created using group parameter. If the group is also not specified, a domain name based on the OU information will be " - + "created. If no OU hierarchy exists, will be defaulted to ROOT domain") + + "imported. If no domain is specified, a domain will created using group parameter. If the group is also not specified, a domain name based on the OU information will be " + + "created. If no OU hierarchy exists, will be defaulted to ROOT domain") private Long domainId; @Parameter(name = ApiConstants.GROUP, type = CommandType.STRING, description = "Specifies the group name from which the ldap users are to be imported. " - + "If no group is specified, all the users will be imported.") + + "If no group is specified, all the users will be imported.") private String groupName; private Domain _domain; @@ -84,7 +84,7 @@ public class LdapImportUsersCmd extends BaseListCmd { @Override public void execute() throws ResourceUnavailableException, InsufficientCapacityException, ServerApiException, ConcurrentOperationException, ResourceAllocationException, - NetworkRuleConflictException { + NetworkRuleConflictException { List users; try { @@ -103,7 +103,7 @@ public class LdapImportUsersCmd extends BaseListCmd { Domain domain = getDomain(user); try { _accountService.createUserAccount(user.getUsername(), generatePassword(), user.getFirstname(), user.getLastname(), user.getEmail(), timezone, user.getUsername(), - accountType, domain.getId(), domain.getNetworkDomain(), details, UUID.randomUUID().toString(), UUID.randomUUID().toString()); + accountType, domain.getId(), domain.getNetworkDomain(), details, UUID.randomUUID().toString(), UUID.randomUUID().toString()); addedUsers.add(user); } catch (InvalidParameterValueException ex) { s_logger.error("Failed to create user with username: " + user.getUsername() +" ::: "+ex.getMessage()); diff --git a/plugins/user-authenticators/ldap/src/org/apache/cloudstack/api/command/LdapListConfigurationCmd.java b/plugins/user-authenticators/ldap/src/org/apache/cloudstack/api/command/LdapListConfigurationCmd.java index 3d9323da6f9..2f582b1166a 100644 --- a/plugins/user-authenticators/ldap/src/org/apache/cloudstack/api/command/LdapListConfigurationCmd.java +++ b/plugins/user-authenticators/ldap/src/org/apache/cloudstack/api/command/LdapListConfigurationCmd.java @@ -35,76 +35,76 @@ import com.cloud.utils.Pair; @APICommand(name = "listLdapConfigurations", responseObject = LdapConfigurationResponse.class, description = "Lists all LDAP configurations", since = "4.2.0") public class LdapListConfigurationCmd extends BaseListCmd { - public static final Logger s_logger = Logger - .getLogger(LdapListConfigurationCmd.class.getName()); + public static final Logger s_logger = Logger + .getLogger(LdapListConfigurationCmd.class.getName()); - private static final String s_name = "ldapconfigurationresponse"; + private static final String s_name = "ldapconfigurationresponse"; - @Inject - private LdapManager _ldapManager; + @Inject + private LdapManager _ldapManager; - @Parameter(name = "hostname", type = CommandType.STRING, required = false, description = "Hostname") - private String hostname; + @Parameter(name = "hostname", type = CommandType.STRING, required = false, description = "Hostname") + private String hostname; - @Parameter(name = "port", type = CommandType.INTEGER, required = false, description = "Port") - private int port; + @Parameter(name = "port", type = CommandType.INTEGER, required = false, description = "Port") + private int port; - public LdapListConfigurationCmd() { - super(); - } + public LdapListConfigurationCmd() { + super(); + } - public LdapListConfigurationCmd(final LdapManager ldapManager) { - super(); - _ldapManager = ldapManager; - } + public LdapListConfigurationCmd(final LdapManager ldapManager) { + super(); + _ldapManager = ldapManager; + } - private List createLdapConfigurationResponses( - final List configurations) { - final List responses = new ArrayList(); - for (final LdapConfigurationVO resource : configurations) { - final LdapConfigurationResponse configurationResponse = _ldapManager - .createLdapConfigurationResponse(resource); - configurationResponse.setObjectName("LdapConfiguration"); - responses.add(configurationResponse); - } - return responses; - } + private List createLdapConfigurationResponses( + final List configurations) { + final List responses = new ArrayList(); + for (final LdapConfigurationVO resource : configurations) { + final LdapConfigurationResponse configurationResponse = _ldapManager + .createLdapConfigurationResponse(resource); + configurationResponse.setObjectName("LdapConfiguration"); + responses.add(configurationResponse); + } + return responses; + } - @Override - public void execute() { - final Pair, Integer> result = _ldapManager - .listConfigurations(this); - final List responses = createLdapConfigurationResponses(result - .first()); - final ListResponse response = new ListResponse(); - response.setResponses(responses, result.second()); - response.setResponseName(getCommandName()); - setResponseObject(response); - } + @Override + public void execute() { + final Pair, Integer> result = _ldapManager + .listConfigurations(this); + final List responses = createLdapConfigurationResponses(result + .first()); + final ListResponse response = new ListResponse(); + response.setResponses(responses, result.second()); + response.setResponseName(getCommandName()); + setResponseObject(response); + } - @Override - public String getCommandName() { - return s_name; - } + @Override + public String getCommandName() { + return s_name; + } - @Override - public long getEntityOwnerId() { - return Account.ACCOUNT_ID_SYSTEM; - } + @Override + public long getEntityOwnerId() { + return Account.ACCOUNT_ID_SYSTEM; + } - public String getHostname() { - return hostname; - } + public String getHostname() { + return hostname; + } - public int getPort() { - return port; - } + public int getPort() { + return port; + } - public void setHostname(final String hostname) { - this.hostname = hostname; - } + public void setHostname(final String hostname) { + this.hostname = hostname; + } - public void setPort(final int port) { - this.port = port; - } + public void setPort(final int port) { + this.port = port; + } } diff --git a/plugins/user-authenticators/ldap/src/org/apache/cloudstack/api/command/LdapListUsersCmd.java b/plugins/user-authenticators/ldap/src/org/apache/cloudstack/api/command/LdapListUsersCmd.java index 18e36f56425..65168cd212b 100644 --- a/plugins/user-authenticators/ldap/src/org/apache/cloudstack/api/command/LdapListUsersCmd.java +++ b/plugins/user-authenticators/ldap/src/org/apache/cloudstack/api/command/LdapListUsersCmd.java @@ -40,84 +40,84 @@ import com.cloud.user.Account; @APICommand(name = "listLdapUsers", responseObject = LdapUserResponse.class, description = "Lists all LDAP Users", since = "4.2.0") public class LdapListUsersCmd extends BaseListCmd { - public static final Logger s_logger = Logger - .getLogger(LdapListUsersCmd.class.getName()); - private static final String s_name = "ldapuserresponse"; - @Inject - private LdapManager _ldapManager; + public static final Logger s_logger = Logger + .getLogger(LdapListUsersCmd.class.getName()); + private static final String s_name = "ldapuserresponse"; + @Inject + private LdapManager _ldapManager; - @Inject - private QueryService _queryService; + @Inject + private QueryService _queryService; - @Parameter(name = "listtype", type = CommandType.STRING, required = false, description = "Determines whether all ldap users are returned or just non-cloudstack users") - private String listType; + @Parameter(name = "listtype", type = CommandType.STRING, required = false, description = "Determines whether all ldap users are returned or just non-cloudstack users") + private String listType; - public LdapListUsersCmd() { - super(); - } + public LdapListUsersCmd() { + super(); + } - public LdapListUsersCmd(final LdapManager ldapManager, - final QueryService queryService) { - super(); - _ldapManager = ldapManager; - _queryService = queryService; - } + public LdapListUsersCmd(final LdapManager ldapManager, + final QueryService queryService) { + super(); + _ldapManager = ldapManager; + _queryService = queryService; + } - private List createLdapUserResponse( - final List users) { - final List ldapResponses = new ArrayList(); - for (final LdapUser user : users) { - if (getListType().equals("all") || !isACloudstackUser(user)) { - final LdapUserResponse ldapResponse = _ldapManager - .createLdapUserResponse(user); - ldapResponse.setObjectName("LdapUser"); - ldapResponses.add(ldapResponse); - } - } - return ldapResponses; - } + private List createLdapUserResponse( + final List users) { + final List ldapResponses = new ArrayList(); + for (final LdapUser user : users) { + if (getListType().equals("all") || !isACloudstackUser(user)) { + final LdapUserResponse ldapResponse = _ldapManager + .createLdapUserResponse(user); + ldapResponse.setObjectName("LdapUser"); + ldapResponses.add(ldapResponse); + } + } + return ldapResponses; + } - @Override - public void execute() throws ServerApiException { - List ldapResponses = null; - final ListResponse response = new ListResponse(); - try { - final List users = _ldapManager.getUsers(); - ldapResponses = createLdapUserResponse(users); - } catch (final NoLdapUserMatchingQueryException ex) { - ldapResponses = new ArrayList(); - } finally { - response.setResponses(ldapResponses); - response.setResponseName(getCommandName()); - setResponseObject(response); - } - } + @Override + public void execute() throws ServerApiException { + List ldapResponses = null; + final ListResponse response = new ListResponse(); + try { + final List users = _ldapManager.getUsers(); + ldapResponses = createLdapUserResponse(users); + } catch (final NoLdapUserMatchingQueryException ex) { + ldapResponses = new ArrayList(); + } finally { + response.setResponses(ldapResponses); + response.setResponseName(getCommandName()); + setResponseObject(response); + } + } - @Override - public String getCommandName() { - return s_name; - } + @Override + public String getCommandName() { + return s_name; + } - @Override - public long getEntityOwnerId() { - return Account.ACCOUNT_ID_SYSTEM; - } + @Override + public long getEntityOwnerId() { + return Account.ACCOUNT_ID_SYSTEM; + } - private String getListType() { - return listType == null ? "all" : listType; - } + private String getListType() { + return listType == null ? "all" : listType; + } - private boolean isACloudstackUser(final LdapUser ldapUser) { - final ListResponse response = _queryService - .searchForUsers(new ListUsersCmd()); - final List cloudstackUsers = response.getResponses(); - if (cloudstackUsers != null && cloudstackUsers.size() != 0) { - for (final UserResponse cloudstackUser : response.getResponses()) { - if (ldapUser.getUsername().equals(cloudstackUser.getUsername())) { - return true; - } - } - } - return false; - } + private boolean isACloudstackUser(final LdapUser ldapUser) { + final ListResponse response = _queryService + .searchForUsers(new ListUsersCmd()); + final List cloudstackUsers = response.getResponses(); + if (cloudstackUsers != null && cloudstackUsers.size() != 0) { + for (final UserResponse cloudstackUser : response.getResponses()) { + if (ldapUser.getUsername().equals(cloudstackUser.getUsername())) { + return true; + } + } + } + return false; + } } \ No newline at end of file diff --git a/plugins/user-authenticators/ldap/src/org/apache/cloudstack/api/command/LdapUserSearchCmd.java b/plugins/user-authenticators/ldap/src/org/apache/cloudstack/api/command/LdapUserSearchCmd.java index e72371c113e..be01da5fb31 100644 --- a/plugins/user-authenticators/ldap/src/org/apache/cloudstack/api/command/LdapUserSearchCmd.java +++ b/plugins/user-authenticators/ldap/src/org/apache/cloudstack/api/command/LdapUserSearchCmd.java @@ -36,63 +36,63 @@ import com.cloud.user.Account; @APICommand(name = "searchLdap", responseObject = LdapUserResponse.class, description = "Searches LDAP based on the username attribute", since = "4.2.0") public class LdapUserSearchCmd extends BaseListCmd { - public static final Logger s_logger = Logger - .getLogger(LdapUserSearchCmd.class.getName()); - private static final String s_name = "ldapuserresponse"; - @Inject - private LdapManager _ldapManager; + public static final Logger s_logger = Logger + .getLogger(LdapUserSearchCmd.class.getName()); + private static final String s_name = "ldapuserresponse"; + @Inject + private LdapManager _ldapManager; - @Parameter(name = "query", type = CommandType.STRING, entityType = LdapUserResponse.class, required = true, description = "query to search using") - private String query; + @Parameter(name = "query", type = CommandType.STRING, entityType = LdapUserResponse.class, required = true, description = "query to search using") + private String query; - public LdapUserSearchCmd() { - super(); - } + public LdapUserSearchCmd() { + super(); + } - public LdapUserSearchCmd(final LdapManager ldapManager) { - super(); - _ldapManager = ldapManager; - } + public LdapUserSearchCmd(final LdapManager ldapManager) { + super(); + _ldapManager = ldapManager; + } - private List createLdapUserResponse( - final List users) { - final List ldapUserResponses = new ArrayList(); - if (users != null) { - for (final LdapUser user : users) { - final LdapUserResponse ldapUserResponse = _ldapManager - .createLdapUserResponse(user); - ldapUserResponse.setObjectName("LdapUser"); - ldapUserResponses.add(ldapUserResponse); - } - } - return ldapUserResponses; - } + private List createLdapUserResponse( + final List users) { + final List ldapUserResponses = new ArrayList(); + if (users != null) { + for (final LdapUser user : users) { + final LdapUserResponse ldapUserResponse = _ldapManager + .createLdapUserResponse(user); + ldapUserResponse.setObjectName("LdapUser"); + ldapUserResponses.add(ldapUserResponse); + } + } + return ldapUserResponses; + } - @Override - public void execute() { - final ListResponse response = new ListResponse(); - List users = null; + @Override + public void execute() { + final ListResponse response = new ListResponse(); + List users = null; - try { - users = _ldapManager.searchUsers(query); - } catch (final NoLdapUserMatchingQueryException e) { - s_logger.debug(e.getMessage()); - } + try { + users = _ldapManager.searchUsers(query); + } catch (final NoLdapUserMatchingQueryException e) { + s_logger.debug(e.getMessage()); + } - final List ldapUserResponses = createLdapUserResponse(users); + final List ldapUserResponses = createLdapUserResponse(users); - response.setResponses(ldapUserResponses); - response.setResponseName(getCommandName()); - setResponseObject(response); - } + response.setResponses(ldapUserResponses); + response.setResponseName(getCommandName()); + setResponseObject(response); + } - @Override - public String getCommandName() { - return s_name; - } + @Override + public String getCommandName() { + return s_name; + } - @Override - public long getEntityOwnerId() { - return Account.ACCOUNT_ID_SYSTEM; - } + @Override + public long getEntityOwnerId() { + return Account.ACCOUNT_ID_SYSTEM; + } } \ No newline at end of file diff --git a/plugins/user-authenticators/ldap/src/org/apache/cloudstack/api/response/LdapConfigurationResponse.java b/plugins/user-authenticators/ldap/src/org/apache/cloudstack/api/response/LdapConfigurationResponse.java index 39d5baeae84..312e52b4cb0 100644 --- a/plugins/user-authenticators/ldap/src/org/apache/cloudstack/api/response/LdapConfigurationResponse.java +++ b/plugins/user-authenticators/ldap/src/org/apache/cloudstack/api/response/LdapConfigurationResponse.java @@ -22,41 +22,41 @@ import com.cloud.serializer.Param; import com.google.gson.annotations.SerializedName; public class LdapConfigurationResponse extends BaseResponse { - @SerializedName("hostname") - @Param(description = "hostname") - private String hostname; + @SerializedName("hostname") + @Param(description = "hostname") + private String hostname; - @SerializedName("port") - @Param(description = "port") - private int port; + @SerializedName("port") + @Param(description = "port") + private int port; - public LdapConfigurationResponse() { - super(); - } + public LdapConfigurationResponse() { + super(); + } - public LdapConfigurationResponse(final String hostname) { - super(); - this.hostname = hostname; - } + public LdapConfigurationResponse(final String hostname) { + super(); + this.hostname = hostname; + } - public LdapConfigurationResponse(final String hostname, final int port) { - this.hostname = hostname; - this.port = port; - } + public LdapConfigurationResponse(final String hostname, final int port) { + this.hostname = hostname; + this.port = port; + } - public String getHostname() { - return hostname; - } + public String getHostname() { + return hostname; + } - public int getPort() { - return port; - } + public int getPort() { + return port; + } - public void setHostname(final String hostname) { - this.hostname = hostname; - } + public void setHostname(final String hostname) { + this.hostname = hostname; + } - public void setPort(final int port) { - this.port = port; - } + public void setPort(final int port) { + this.port = port; + } } \ No newline at end of file diff --git a/plugins/user-authenticators/ldap/src/org/apache/cloudstack/api/response/LdapUserResponse.java b/plugins/user-authenticators/ldap/src/org/apache/cloudstack/api/response/LdapUserResponse.java index 1672703fd89..e91524af3fe 100644 --- a/plugins/user-authenticators/ldap/src/org/apache/cloudstack/api/response/LdapUserResponse.java +++ b/plugins/user-authenticators/ldap/src/org/apache/cloudstack/api/response/LdapUserResponse.java @@ -46,64 +46,64 @@ public class LdapUserResponse extends BaseResponse { private String domain; public LdapUserResponse() { - super(); + super(); } public LdapUserResponse(final String username, final String email, final String firstname, final String lastname, final String principal, String domain) { - super(); - this.username = username; - this.email = email; - this.firstname = firstname; - this.lastname = lastname; - this.principal = principal; - this.domain = domain; + super(); + this.username = username; + this.email = email; + this.firstname = firstname; + this.lastname = lastname; + this.principal = principal; + this.domain = domain; } public String getEmail() { - return email; + return email; } public String getFirstname() { - return firstname; + return firstname; } public String getLastname() { - return lastname; + return lastname; } public String getPrincipal() { - return principal; + return principal; } public String getUsername() { - return username; + return username; } public String getDomain() { - return domain; + return domain; } public void setEmail(final String email) { - this.email = email; + this.email = email; } public void setFirstname(final String firstname) { - this.firstname = firstname; + this.firstname = firstname; } public void setLastname(final String lastname) { - this.lastname = lastname; + this.lastname = lastname; } public void setPrincipal(final String principal) { - this.principal = principal; + this.principal = principal; } public void setUsername(final String username) { - this.username = username; + this.username = username; } public void setDomain(String domain) { - this.domain = domain; + this.domain = domain; } } \ No newline at end of file diff --git a/plugins/user-authenticators/ldap/src/org/apache/cloudstack/ldap/LdapAuthenticator.java b/plugins/user-authenticators/ldap/src/org/apache/cloudstack/ldap/LdapAuthenticator.java index 559a9794b2d..348ecd9a8c3 100644 --- a/plugins/user-authenticators/ldap/src/org/apache/cloudstack/ldap/LdapAuthenticator.java +++ b/plugins/user-authenticators/ldap/src/org/apache/cloudstack/ldap/LdapAuthenticator.java @@ -27,45 +27,45 @@ import com.cloud.user.UserAccount; import com.cloud.user.dao.UserAccountDao; public class LdapAuthenticator extends DefaultUserAuthenticator { - private static final Logger s_logger = Logger - .getLogger(LdapAuthenticator.class.getName()); + private static final Logger s_logger = Logger + .getLogger(LdapAuthenticator.class.getName()); - @Inject - private LdapManager _ldapManager; - @Inject - private UserAccountDao _userAccountDao; + @Inject + private LdapManager _ldapManager; + @Inject + private UserAccountDao _userAccountDao; - public LdapAuthenticator() { - super(); - } + public LdapAuthenticator() { + super(); + } - public LdapAuthenticator(final LdapManager ldapManager, - final UserAccountDao userAccountDao) { - super(); - _ldapManager = ldapManager; - _userAccountDao = userAccountDao; - } + public LdapAuthenticator(final LdapManager ldapManager, + final UserAccountDao userAccountDao) { + super(); + _ldapManager = ldapManager; + _userAccountDao = userAccountDao; + } - @Override - public boolean authenticate(final String username, final String password, - final Long domainId, final Map requestParameters) { + @Override + public boolean authenticate(final String username, final String password, + final Long domainId, final Map requestParameters) { - final UserAccount user = _userAccountDao.getUserAccount(username, - domainId); + final UserAccount user = _userAccountDao.getUserAccount(username, + domainId); - if (user == null) { - s_logger.debug("Unable to find user with " + username - + " in domain " + domainId); - return false; - } else if (_ldapManager.isLdapEnabled()) { - return _ldapManager.canAuthenticate(username, password); - } else { - return false; - } - } + if (user == null) { + s_logger.debug("Unable to find user with " + username + + " in domain " + domainId); + return false; + } else if (_ldapManager.isLdapEnabled()) { + return _ldapManager.canAuthenticate(username, password); + } else { + return false; + } + } - @Override - public String encode(final String password) { - return password; - } + @Override + public String encode(final String password) { + return password; + } } diff --git a/plugins/user-authenticators/ldap/src/org/apache/cloudstack/ldap/LdapConfiguration.java b/plugins/user-authenticators/ldap/src/org/apache/cloudstack/ldap/LdapConfiguration.java index 7db55f74a08..38a66b7221f 100644 --- a/plugins/user-authenticators/ldap/src/org/apache/cloudstack/ldap/LdapConfiguration.java +++ b/plugins/user-authenticators/ldap/src/org/apache/cloudstack/ldap/LdapConfiguration.java @@ -27,133 +27,134 @@ import org.apache.cloudstack.framework.config.dao.ConfigurationDao; import com.cloud.utils.Pair; public class LdapConfiguration { - private final static String factory = "com.sun.jndi.ldap.LdapCtxFactory"; + private final static String factory = "com.sun.jndi.ldap.LdapCtxFactory"; - private final static int scope = SearchControls.SUBTREE_SCOPE; + private final static int scope = SearchControls.SUBTREE_SCOPE; - @Inject - private ConfigurationDao _configDao; + @Inject + private ConfigurationDao _configDao; - @Inject - private LdapManager _ldapManager; + @Inject + private LdapManager _ldapManager; - public LdapConfiguration() { - } + public LdapConfiguration() { + } - public LdapConfiguration(final ConfigurationDao configDao, - final LdapManager ldapManager) { - _configDao = configDao; - _ldapManager = ldapManager; - } + public LdapConfiguration(final ConfigurationDao configDao, + final LdapManager ldapManager) { + _configDao = configDao; + _ldapManager = ldapManager; + } - public String getAuthentication() { - if ((getBindPrincipal() == null) && (getBindPassword() == null)) { - return "none"; - } else { - return "simple"; - } - } + public String getAuthentication() { + if ((getBindPrincipal() == null) && (getBindPassword() == null)) { + return "none"; + } else { + return "simple"; + } + } - public String getBaseDn() { - return _configDao.getValue("ldap.basedn"); - } + public String getBaseDn() { + return _configDao.getValue("ldap.basedn"); + } - public String getBindPassword() { - return _configDao.getValue("ldap.bind.password"); - } + public String getBindPassword() { + return _configDao.getValue("ldap.bind.password"); + } - public String getBindPrincipal() { - return _configDao.getValue("ldap.bind.principal"); - } + public String getBindPrincipal() { + return _configDao.getValue("ldap.bind.principal"); + } - public String getEmailAttribute() { - final String emailAttribute = _configDao - .getValue("ldap.email.attribute"); - return emailAttribute == null ? "mail" : emailAttribute; - } + public String getEmailAttribute() { + final String emailAttribute = _configDao + .getValue("ldap.email.attribute"); + return emailAttribute == null ? "mail" : emailAttribute; + } - public String getFactory() { - return factory; - } + public String getFactory() { + return factory; + } - public String getFirstnameAttribute() { - final String firstnameAttribute = _configDao - .getValue("ldap.firstname.attribute"); - return firstnameAttribute == null ? "givenname" : firstnameAttribute; - } + public String getFirstnameAttribute() { + final String firstnameAttribute = _configDao + .getValue("ldap.firstname.attribute"); + return firstnameAttribute == null ? "givenname" : firstnameAttribute; + } - public String getLastnameAttribute() { - final String lastnameAttribute = _configDao - .getValue("ldap.lastname.attribute"); - return lastnameAttribute == null ? "sn" : lastnameAttribute; - } + public String getLastnameAttribute() { + final String lastnameAttribute = _configDao + .getValue("ldap.lastname.attribute"); + return lastnameAttribute == null ? "sn" : lastnameAttribute; + } - public String getProviderUrl() { - final String protocol = getSSLStatus() == true ? "ldaps://" : "ldap://"; - final Pair, Integer> result = _ldapManager - .listConfigurations(new LdapListConfigurationCmd(_ldapManager)); - final StringBuilder providerUrls = new StringBuilder(); - String delim = ""; - for (final LdapConfigurationVO resource : result.first()) { - final String providerUrl = protocol + resource.getHostname() + ":" - + resource.getPort(); - providerUrls.append(delim).append(providerUrl); - delim = " "; - } - return providerUrls.toString(); - } + public String getProviderUrl() { + final String protocol = getSSLStatus() == true ? "ldaps://" : "ldap://"; + final Pair, Integer> result = _ldapManager + .listConfigurations(new LdapListConfigurationCmd(_ldapManager)); + final StringBuilder providerUrls = new StringBuilder(); + String delim = ""; + for (final LdapConfigurationVO resource : result.first()) { + final String providerUrl = protocol + resource.getHostname() + ":" + + resource.getPort(); + providerUrls.append(delim).append(providerUrl); + delim = " "; + } + return providerUrls.toString(); + } - public String[] getReturnAttributes() { - return new String[] { getUsernameAttribute(), getEmailAttribute(), - getFirstnameAttribute(), getLastnameAttribute(), getCommonNameAttribute() }; - } + public String[] getReturnAttributes() { + return new String[] { getUsernameAttribute(), getEmailAttribute(), + getFirstnameAttribute(), getLastnameAttribute(), getCommonNameAttribute() + }; + } - public int getScope() { - return scope; - } + public int getScope() { + return scope; + } - public String getSearchGroupPrinciple() { - return _configDao.getValue("ldap.search.group.principle"); - } + public String getSearchGroupPrinciple() { + return _configDao.getValue("ldap.search.group.principle"); + } - public boolean getSSLStatus() { - boolean sslStatus = false; - if (getTrustStore() != null && getTrustStorePassword() != null) { - sslStatus = true; - } - return sslStatus; - } + public boolean getSSLStatus() { + boolean sslStatus = false; + if (getTrustStore() != null && getTrustStorePassword() != null) { + sslStatus = true; + } + return sslStatus; + } - public String getTrustStore() { - return _configDao.getValue("ldap.truststore"); - } + public String getTrustStore() { + return _configDao.getValue("ldap.truststore"); + } - public String getTrustStorePassword() { - return _configDao.getValue("ldap.truststore.password"); - } + public String getTrustStorePassword() { + return _configDao.getValue("ldap.truststore.password"); + } - public String getUsernameAttribute() { - final String usernameAttribute = _configDao - .getValue("ldap.username.attribute"); - return usernameAttribute == null ? "uid" : usernameAttribute; - } + public String getUsernameAttribute() { + final String usernameAttribute = _configDao + .getValue("ldap.username.attribute"); + return usernameAttribute == null ? "uid" : usernameAttribute; + } - public String getUserObject() { - final String userObject = _configDao.getValue("ldap.user.object"); - return userObject == null ? "inetOrgPerson" : userObject; - } + public String getUserObject() { + final String userObject = _configDao.getValue("ldap.user.object"); + return userObject == null ? "inetOrgPerson" : userObject; + } public String getGroupObject() { - final String groupObject = _configDao.getValue("ldap.group.object"); - return groupObject == null ? "groupOfUniqueNames" : groupObject; + final String groupObject = _configDao.getValue("ldap.group.object"); + return groupObject == null ? "groupOfUniqueNames" : groupObject; } public String getGroupUniqueMemeberAttribute() { - final String uniqueMemberAttribute = _configDao.getValue("ldap.group.user.uniquemember"); - return uniqueMemberAttribute == null ? "uniquemember" : uniqueMemberAttribute; + final String uniqueMemberAttribute = _configDao.getValue("ldap.group.user.uniquemember"); + return uniqueMemberAttribute == null ? "uniquemember" : uniqueMemberAttribute; } public String getCommonNameAttribute() { - return "cn"; + return "cn"; } } \ No newline at end of file diff --git a/plugins/user-authenticators/ldap/src/org/apache/cloudstack/ldap/LdapConfigurationVO.java b/plugins/user-authenticators/ldap/src/org/apache/cloudstack/ldap/LdapConfigurationVO.java index 98e42f8af40..488e7f44485 100644 --- a/plugins/user-authenticators/ldap/src/org/apache/cloudstack/ldap/LdapConfigurationVO.java +++ b/plugins/user-authenticators/ldap/src/org/apache/cloudstack/ldap/LdapConfigurationVO.java @@ -28,39 +28,39 @@ import org.apache.cloudstack.api.InternalIdentity; @Entity @Table(name = "ldap_configuration") public class LdapConfigurationVO implements InternalIdentity { - @Column(name = "hostname") - private String hostname; + @Column(name = "hostname") + private String hostname; - @Id - @GeneratedValue(strategy = GenerationType.IDENTITY) - @Column(name = "id") - private Long id; + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "id") + private Long id; - @Column(name = "port") - private int port; + @Column(name = "port") + private int port; - public LdapConfigurationVO() { - } + public LdapConfigurationVO() { + } - public LdapConfigurationVO(final String hostname, final int port) { - this.hostname = hostname; - this.port = port; - } + public LdapConfigurationVO(final String hostname, final int port) { + this.hostname = hostname; + this.port = port; + } - public String getHostname() { - return hostname; - } + public String getHostname() { + return hostname; + } - @Override - public long getId() { - return id; - } + @Override + public long getId() { + return id; + } - public int getPort() { - return port; - } + public int getPort() { + return port; + } - public void setId(final long id) { - this.id = id; - } + public void setId(final long id) { + this.id = id; + } } \ No newline at end of file diff --git a/plugins/user-authenticators/ldap/src/org/apache/cloudstack/ldap/LdapContextFactory.java b/plugins/user-authenticators/ldap/src/org/apache/cloudstack/ldap/LdapContextFactory.java index ceeed6862fb..c511e1611bf 100644 --- a/plugins/user-authenticators/ldap/src/org/apache/cloudstack/ldap/LdapContextFactory.java +++ b/plugins/user-authenticators/ldap/src/org/apache/cloudstack/ldap/LdapContextFactory.java @@ -27,110 +27,110 @@ import javax.naming.directory.InitialDirContext; import org.apache.log4j.Logger; public class LdapContextFactory { - private static final Logger s_logger = Logger - .getLogger(LdapContextFactory.class.getName()); + private static final Logger s_logger = Logger + .getLogger(LdapContextFactory.class.getName()); - @Inject - private LdapConfiguration _ldapConfiguration; + @Inject + private LdapConfiguration _ldapConfiguration; - public LdapContextFactory() { - } + public LdapContextFactory() { + } - public LdapContextFactory(final LdapConfiguration ldapConfiguration) { - _ldapConfiguration = ldapConfiguration; - } + public LdapContextFactory(final LdapConfiguration ldapConfiguration) { + _ldapConfiguration = ldapConfiguration; + } - public DirContext createBindContext() throws NamingException { - return createBindContext(null); - } + public DirContext createBindContext() throws NamingException { + return createBindContext(null); + } - public DirContext createBindContext(final String providerUrl) - throws NamingException { - final String bindPrincipal = _ldapConfiguration.getBindPrincipal(); - final String bindPassword = _ldapConfiguration.getBindPassword(); - return createInitialDirContext(bindPrincipal, bindPassword, - providerUrl, true); - } + public DirContext createBindContext(final String providerUrl) + throws NamingException { + final String bindPrincipal = _ldapConfiguration.getBindPrincipal(); + final String bindPassword = _ldapConfiguration.getBindPassword(); + return createInitialDirContext(bindPrincipal, bindPassword, + providerUrl, true); + } - private DirContext createInitialDirContext(final String principal, - final String password, final boolean isSystemContext) - throws NamingException { - return createInitialDirContext(principal, password, null, - isSystemContext); - } + private DirContext createInitialDirContext(final String principal, + final String password, final boolean isSystemContext) + throws NamingException { + return createInitialDirContext(principal, password, null, + isSystemContext); + } - private DirContext createInitialDirContext(final String principal, - final String password, final String providerUrl, - final boolean isSystemContext) throws NamingException { - return new InitialDirContext(getEnvironment(principal, password, - providerUrl, isSystemContext)); - } + private DirContext createInitialDirContext(final String principal, + final String password, final String providerUrl, + final boolean isSystemContext) throws NamingException { + return new InitialDirContext(getEnvironment(principal, password, + providerUrl, isSystemContext)); + } - public DirContext createUserContext(final String principal, - final String password) throws NamingException { - return createInitialDirContext(principal, password, false); - } + public DirContext createUserContext(final String principal, + final String password) throws NamingException { + return createInitialDirContext(principal, password, false); + } - private void enableSSL(final Hashtable environment) { - final boolean sslStatus = _ldapConfiguration.getSSLStatus(); + private void enableSSL(final Hashtable environment) { + final boolean sslStatus = _ldapConfiguration.getSSLStatus(); - if (sslStatus) { - s_logger.info("LDAP SSL enabled."); - environment.put(Context.SECURITY_PROTOCOL, "ssl"); - System.setProperty("javax.net.ssl.trustStore", - _ldapConfiguration.getTrustStore()); - System.setProperty("javax.net.ssl.trustStorePassword", - _ldapConfiguration.getTrustStorePassword()); - } - } + if (sslStatus) { + s_logger.info("LDAP SSL enabled."); + environment.put(Context.SECURITY_PROTOCOL, "ssl"); + System.setProperty("javax.net.ssl.trustStore", + _ldapConfiguration.getTrustStore()); + System.setProperty("javax.net.ssl.trustStorePassword", + _ldapConfiguration.getTrustStorePassword()); + } + } - private Hashtable getEnvironment(final String principal, - final String password, final String providerUrl, - final boolean isSystemContext) { - final String factory = _ldapConfiguration.getFactory(); - final String url = providerUrl == null ? _ldapConfiguration - .getProviderUrl() : providerUrl; + private Hashtable getEnvironment(final String principal, + final String password, final String providerUrl, + final boolean isSystemContext) { + final String factory = _ldapConfiguration.getFactory(); + final String url = providerUrl == null ? _ldapConfiguration + .getProviderUrl() : providerUrl; - final Hashtable environment = new Hashtable(); + final Hashtable environment = new Hashtable(); - environment.put(Context.INITIAL_CONTEXT_FACTORY, factory); - environment.put(Context.PROVIDER_URL, url); - environment.put("com.sun.jndi.ldap.read.timeout", "500"); - environment.put("com.sun.jndi.ldap.connect.pool", "true"); + environment.put(Context.INITIAL_CONTEXT_FACTORY, factory); + environment.put(Context.PROVIDER_URL, url); + environment.put("com.sun.jndi.ldap.read.timeout", "500"); + environment.put("com.sun.jndi.ldap.connect.pool", "true"); - enableSSL(environment); - setAuthentication(environment, isSystemContext); + enableSSL(environment); + setAuthentication(environment, isSystemContext); - if (principal != null) { - environment.put(Context.SECURITY_PRINCIPAL, principal); - } + if (principal != null) { + environment.put(Context.SECURITY_PRINCIPAL, principal); + } - if (password != null) { - environment.put(Context.SECURITY_CREDENTIALS, password); - } + if (password != null) { + environment.put(Context.SECURITY_CREDENTIALS, password); + } - return environment; - } + return environment; + } - private void setAuthentication(final Hashtable environment, - final boolean isSystemContext) { - final String authentication = _ldapConfiguration.getAuthentication(); + private void setAuthentication(final Hashtable environment, + final boolean isSystemContext) { + final String authentication = _ldapConfiguration.getAuthentication(); - if ("none".equals(authentication) && !isSystemContext) { - environment.put(Context.SECURITY_AUTHENTICATION, "simple"); - } else { - environment.put(Context.SECURITY_AUTHENTICATION, authentication); - } - } + if ("none".equals(authentication) && !isSystemContext) { + environment.put(Context.SECURITY_AUTHENTICATION, "simple"); + } else { + environment.put(Context.SECURITY_AUTHENTICATION, authentication); + } + } - public void testConnection(final String providerUrl) throws NamingException { - try { - createBindContext(providerUrl); - s_logger.info("LDAP Connection was successful"); - } catch (final NamingException e) { - s_logger.warn("LDAP Connection failed"); - s_logger.error(e.getMessage(), e); - throw e; - } - } + public void testConnection(final String providerUrl) throws NamingException { + try { + createBindContext(providerUrl); + s_logger.info("LDAP Connection was successful"); + } catch (final NamingException e) { + s_logger.warn("LDAP Connection failed"); + s_logger.error(e.getMessage(), e); + throw e; + } + } } \ No newline at end of file diff --git a/plugins/user-authenticators/ldap/src/org/apache/cloudstack/ldap/LdapManager.java b/plugins/user-authenticators/ldap/src/org/apache/cloudstack/ldap/LdapManager.java index 2c99d080b9a..903a4cb2db0 100644 --- a/plugins/user-authenticators/ldap/src/org/apache/cloudstack/ldap/LdapManager.java +++ b/plugins/user-authenticators/ldap/src/org/apache/cloudstack/ldap/LdapManager.java @@ -30,30 +30,30 @@ import com.cloud.utils.component.PluggableService; public interface LdapManager extends PluggableService { - LdapConfigurationResponse addConfiguration(String hostname, int port) - throws InvalidParameterValueException; + LdapConfigurationResponse addConfiguration(String hostname, int port) + throws InvalidParameterValueException; - boolean canAuthenticate(String username, String password); + boolean canAuthenticate(String username, String password); - LdapConfigurationResponse createLdapConfigurationResponse( - LdapConfigurationVO configuration); + LdapConfigurationResponse createLdapConfigurationResponse( + LdapConfigurationVO configuration); - LdapUserResponse createLdapUserResponse(LdapUser user); + LdapUserResponse createLdapUserResponse(LdapUser user); - LdapConfigurationResponse deleteConfiguration(String hostname) - throws InvalidParameterValueException; + LdapConfigurationResponse deleteConfiguration(String hostname) + throws InvalidParameterValueException; - LdapUser getUser(final String username) throws NamingException; + LdapUser getUser(final String username) throws NamingException; - List getUsers() throws NoLdapUserMatchingQueryException; + List getUsers() throws NoLdapUserMatchingQueryException; List getUsersInGroup(String groupName) throws NoLdapUserMatchingQueryException; - boolean isLdapEnabled(); + boolean isLdapEnabled(); - Pair, Integer> listConfigurations( - LdapListConfigurationCmd cmd); + Pair, Integer> listConfigurations( + LdapListConfigurationCmd cmd); - List searchUsers(String query) - throws NoLdapUserMatchingQueryException; + List searchUsers(String query) + throws NoLdapUserMatchingQueryException; } \ No newline at end of file diff --git a/plugins/user-authenticators/ldap/src/org/apache/cloudstack/ldap/LdapManagerImpl.java b/plugins/user-authenticators/ldap/src/org/apache/cloudstack/ldap/LdapManagerImpl.java index 891d62538ab..1ce7378490e 100644 --- a/plugins/user-authenticators/ldap/src/org/apache/cloudstack/ldap/LdapManagerImpl.java +++ b/plugins/user-authenticators/ldap/src/org/apache/cloudstack/ldap/LdapManagerImpl.java @@ -38,205 +38,205 @@ import com.cloud.utils.Pair; @Component @Local(value = LdapManager.class) public class LdapManagerImpl implements LdapManager, LdapValidator { - private static final Logger s_logger = Logger - .getLogger(LdapManagerImpl.class.getName()); + private static final Logger s_logger = Logger + .getLogger(LdapManagerImpl.class.getName()); - @Inject - private LdapConfigurationDao _ldapConfigurationDao; + @Inject + private LdapConfigurationDao _ldapConfigurationDao; - @Inject - private LdapContextFactory _ldapContextFactory; + @Inject + private LdapContextFactory _ldapContextFactory; - @Inject - private LdapUserManager _ldapUserManager; + @Inject + private LdapUserManager _ldapUserManager; - public LdapManagerImpl() { - super(); - } + public LdapManagerImpl() { + super(); + } - public LdapManagerImpl(final LdapConfigurationDao ldapConfigurationDao, - final LdapContextFactory ldapContextFactory, - final LdapUserManager ldapUserManager) { - super(); - _ldapConfigurationDao = ldapConfigurationDao; - _ldapContextFactory = ldapContextFactory; - _ldapUserManager = ldapUserManager; - } - - @Override - public LdapConfigurationResponse addConfiguration(final String hostname, - final int port) throws InvalidParameterValueException { - LdapConfigurationVO configuration = _ldapConfigurationDao - .findByHostname(hostname); - if (configuration == null) { - try { - final String providerUrl = "ldap://" + hostname + ":" + port; - _ldapContextFactory.createBindContext(providerUrl); - configuration = new LdapConfigurationVO(hostname, port); - _ldapConfigurationDao.persist(configuration); - s_logger.info("Added new ldap server with hostname: " - + hostname); - return new LdapConfigurationResponse(hostname, port); - } catch (final NamingException e) { - throw new InvalidParameterValueException( - "Unable to bind to the given LDAP server"); - } - } else { - throw new InvalidParameterValueException("Duplicate configuration"); - } - } - - @Override - public boolean canAuthenticate(final String username, final String password) { - final String escapedUsername = LdapUtils - .escapeLDAPSearchFilter(username); - try { - final LdapUser user = getUser(escapedUsername); - final String principal = user.getPrincipal(); - final DirContext context = _ldapContextFactory.createUserContext( - principal, password); - closeContext(context); - return true; - } catch (final NamingException e) { - s_logger.info("Failed to authenticate user: " + username - + ". incorrect password."); - return false; - } - } - - private void closeContext(final DirContext context) { - try { - if (context != null) { - context.close(); - } - } catch (final NamingException e) { - s_logger.warn(e.getMessage()); - } - } - - @Override - public LdapConfigurationResponse createLdapConfigurationResponse( - final LdapConfigurationVO configuration) { - final LdapConfigurationResponse response = new LdapConfigurationResponse(); - response.setHostname(configuration.getHostname()); - response.setPort(configuration.getPort()); - return response; - } - - @Override - public LdapUserResponse createLdapUserResponse(final LdapUser user) { - final LdapUserResponse response = new LdapUserResponse(); - response.setUsername(user.getUsername()); - response.setFirstname(user.getFirstname()); - response.setLastname(user.getLastname()); - response.setEmail(user.getEmail()); - response.setPrincipal(user.getPrincipal()); - response.setDomain(user.getDomain()); - return response; - } - - @Override - public LdapConfigurationResponse deleteConfiguration(final String hostname) - throws InvalidParameterValueException { - final LdapConfigurationVO configuration = _ldapConfigurationDao - .findByHostname(hostname); - if (configuration == null) { - throw new InvalidParameterValueException( - "Cannot find configuration with hostname " + hostname); - } else { - _ldapConfigurationDao.remove(configuration.getId()); - s_logger.info("Removed ldap server with hostname: " + hostname); - return new LdapConfigurationResponse(configuration.getHostname(), - configuration.getPort()); - } - } - - @Override - public List> getCommands() { - final List> cmdList = new ArrayList>(); - cmdList.add(LdapUserSearchCmd.class); - cmdList.add(LdapListUsersCmd.class); - cmdList.add(LdapAddConfigurationCmd.class); - cmdList.add(LdapDeleteConfigurationCmd.class); - cmdList.add(LdapListConfigurationCmd.class); - cmdList.add(LdapCreateAccountCmd.class); - cmdList.add(LdapImportUsersCmd.class); - return cmdList; - } - - @Override - public LdapUser getUser(final String username) throws NamingException { - DirContext context = null; - try { - context = _ldapContextFactory.createBindContext(); - - final String escapedUsername = LdapUtils - .escapeLDAPSearchFilter(username); - return _ldapUserManager.getUser(escapedUsername, context); - - } catch (final NamingException e) { - throw e; - } finally { - closeContext(context); - } - } - - @Override - public List getUsers() throws NoLdapUserMatchingQueryException { - DirContext context = null; - try { - context = _ldapContextFactory.createBindContext(); - return _ldapUserManager.getUsers(context); - } catch (final NamingException e) { - throw new NoLdapUserMatchingQueryException("*"); - } finally { - closeContext(context); - } - } - - @Override - public List getUsersInGroup(String groupName) throws NoLdapUserMatchingQueryException { - DirContext context = null; - try { - context = _ldapContextFactory.createBindContext(); - return _ldapUserManager.getUsersInGroup(groupName, context); - } catch (final NamingException e) { - throw new NoLdapUserMatchingQueryException("groupName=" + groupName); - } finally { - closeContext(context); - } + public LdapManagerImpl(final LdapConfigurationDao ldapConfigurationDao, + final LdapContextFactory ldapContextFactory, + final LdapUserManager ldapUserManager) { + super(); + _ldapConfigurationDao = ldapConfigurationDao; + _ldapContextFactory = ldapContextFactory; + _ldapUserManager = ldapUserManager; } @Override - public boolean isLdapEnabled() { - return listConfigurations(new LdapListConfigurationCmd(this)).second() > 0; - } + public LdapConfigurationResponse addConfiguration(final String hostname, + final int port) throws InvalidParameterValueException { + LdapConfigurationVO configuration = _ldapConfigurationDao + .findByHostname(hostname); + if (configuration == null) { + try { + final String providerUrl = "ldap://" + hostname + ":" + port; + _ldapContextFactory.createBindContext(providerUrl); + configuration = new LdapConfigurationVO(hostname, port); + _ldapConfigurationDao.persist(configuration); + s_logger.info("Added new ldap server with hostname: " + + hostname); + return new LdapConfigurationResponse(hostname, port); + } catch (final NamingException e) { + throw new InvalidParameterValueException( + "Unable to bind to the given LDAP server"); + } + } else { + throw new InvalidParameterValueException("Duplicate configuration"); + } + } - @Override - public Pair, Integer> listConfigurations( - final LdapListConfigurationCmd cmd) { - final String hostname = cmd.getHostname(); - final int port = cmd.getPort(); - final Pair, Integer> result = _ldapConfigurationDao - .searchConfigurations(hostname, port); - return new Pair, Integer>( - result.first(), result.second()); - } + @Override + public boolean canAuthenticate(final String username, final String password) { + final String escapedUsername = LdapUtils + .escapeLDAPSearchFilter(username); + try { + final LdapUser user = getUser(escapedUsername); + final String principal = user.getPrincipal(); + final DirContext context = _ldapContextFactory.createUserContext( + principal, password); + closeContext(context); + return true; + } catch (final NamingException e) { + s_logger.info("Failed to authenticate user: " + username + + ". incorrect password."); + return false; + } + } - @Override - public List searchUsers(final String username) - throws NoLdapUserMatchingQueryException { - DirContext context = null; - try { - context = _ldapContextFactory.createBindContext(); - final String escapedUsername = LdapUtils - .escapeLDAPSearchFilter(username); - return _ldapUserManager.getUsers("*" + escapedUsername + "*", - context); - } catch (final NamingException e) { - throw new NoLdapUserMatchingQueryException(username); - } finally { - closeContext(context); - } - } + private void closeContext(final DirContext context) { + try { + if (context != null) { + context.close(); + } + } catch (final NamingException e) { + s_logger.warn(e.getMessage()); + } + } + + @Override + public LdapConfigurationResponse createLdapConfigurationResponse( + final LdapConfigurationVO configuration) { + final LdapConfigurationResponse response = new LdapConfigurationResponse(); + response.setHostname(configuration.getHostname()); + response.setPort(configuration.getPort()); + return response; + } + + @Override + public LdapUserResponse createLdapUserResponse(final LdapUser user) { + final LdapUserResponse response = new LdapUserResponse(); + response.setUsername(user.getUsername()); + response.setFirstname(user.getFirstname()); + response.setLastname(user.getLastname()); + response.setEmail(user.getEmail()); + response.setPrincipal(user.getPrincipal()); + response.setDomain(user.getDomain()); + return response; + } + + @Override + public LdapConfigurationResponse deleteConfiguration(final String hostname) + throws InvalidParameterValueException { + final LdapConfigurationVO configuration = _ldapConfigurationDao + .findByHostname(hostname); + if (configuration == null) { + throw new InvalidParameterValueException( + "Cannot find configuration with hostname " + hostname); + } else { + _ldapConfigurationDao.remove(configuration.getId()); + s_logger.info("Removed ldap server with hostname: " + hostname); + return new LdapConfigurationResponse(configuration.getHostname(), + configuration.getPort()); + } + } + + @Override + public List> getCommands() { + final List> cmdList = new ArrayList>(); + cmdList.add(LdapUserSearchCmd.class); + cmdList.add(LdapListUsersCmd.class); + cmdList.add(LdapAddConfigurationCmd.class); + cmdList.add(LdapDeleteConfigurationCmd.class); + cmdList.add(LdapListConfigurationCmd.class); + cmdList.add(LdapCreateAccountCmd.class); + cmdList.add(LdapImportUsersCmd.class); + return cmdList; + } + + @Override + public LdapUser getUser(final String username) throws NamingException { + DirContext context = null; + try { + context = _ldapContextFactory.createBindContext(); + + final String escapedUsername = LdapUtils + .escapeLDAPSearchFilter(username); + return _ldapUserManager.getUser(escapedUsername, context); + + } catch (final NamingException e) { + throw e; + } finally { + closeContext(context); + } + } + + @Override + public List getUsers() throws NoLdapUserMatchingQueryException { + DirContext context = null; + try { + context = _ldapContextFactory.createBindContext(); + return _ldapUserManager.getUsers(context); + } catch (final NamingException e) { + throw new NoLdapUserMatchingQueryException("*"); + } finally { + closeContext(context); + } + } + + @Override + public List getUsersInGroup(String groupName) throws NoLdapUserMatchingQueryException { + DirContext context = null; + try { + context = _ldapContextFactory.createBindContext(); + return _ldapUserManager.getUsersInGroup(groupName, context); + } catch (final NamingException e) { + throw new NoLdapUserMatchingQueryException("groupName=" + groupName); + } finally { + closeContext(context); + } + } + + @Override + public boolean isLdapEnabled() { + return listConfigurations(new LdapListConfigurationCmd(this)).second() > 0; + } + + @Override + public Pair, Integer> listConfigurations( + final LdapListConfigurationCmd cmd) { + final String hostname = cmd.getHostname(); + final int port = cmd.getPort(); + final Pair, Integer> result = _ldapConfigurationDao + .searchConfigurations(hostname, port); + return new Pair, Integer>( + result.first(), result.second()); + } + + @Override + public List searchUsers(final String username) + throws NoLdapUserMatchingQueryException { + DirContext context = null; + try { + context = _ldapContextFactory.createBindContext(); + final String escapedUsername = LdapUtils + .escapeLDAPSearchFilter(username); + return _ldapUserManager.getUsers("*" + escapedUsername + "*", + context); + } catch (final NamingException e) { + throw new NoLdapUserMatchingQueryException(username); + } finally { + closeContext(context); + } + } } \ No newline at end of file diff --git a/plugins/user-authenticators/ldap/src/org/apache/cloudstack/ldap/LdapUser.java b/plugins/user-authenticators/ldap/src/org/apache/cloudstack/ldap/LdapUser.java index 592459eb815..af718cd9f24 100644 --- a/plugins/user-authenticators/ldap/src/org/apache/cloudstack/ldap/LdapUser.java +++ b/plugins/user-authenticators/ldap/src/org/apache/cloudstack/ldap/LdapUser.java @@ -17,65 +17,65 @@ package org.apache.cloudstack.ldap; public class LdapUser implements Comparable { - private final String email; - private final String principal; - private final String firstname; - private final String lastname; - private final String username; + private final String email; + private final String principal; + private final String firstname; + private final String lastname; + private final String username; private final String domain; - public LdapUser(final String username, final String email, final String firstname, final String lastname, final String principal, String domain) { - this.username = username; - this.email = email; - this.firstname = firstname; - this.lastname = lastname; - this.principal = principal; - this.domain = domain; - } - - @Override - public int compareTo(final LdapUser other) { - return getUsername().compareTo(other.getUsername()); - } - - @Override - public boolean equals(final Object other) { - if (this == other) { - return true; - } - if (other instanceof LdapUser) { - final LdapUser otherLdapUser = (LdapUser) other; - return getUsername().equals(otherLdapUser.getUsername()); - } - return false; - } - - public String getEmail() { - return email; - } - - public String getFirstname() { - return firstname; - } - - public String getLastname() { - return lastname; - } - - public String getPrincipal() { - return principal; - } - - public String getUsername() { - return username; - } - - public String getDomain() { - return domain; + public LdapUser(final String username, final String email, final String firstname, final String lastname, final String principal, String domain) { + this.username = username; + this.email = email; + this.firstname = firstname; + this.lastname = lastname; + this.principal = principal; + this.domain = domain; } @Override - public int hashCode() { - return getUsername().hashCode(); - } + public int compareTo(final LdapUser other) { + return getUsername().compareTo(other.getUsername()); + } + + @Override + public boolean equals(final Object other) { + if (this == other) { + return true; + } + if (other instanceof LdapUser) { + final LdapUser otherLdapUser = (LdapUser) other; + return getUsername().equals(otherLdapUser.getUsername()); + } + return false; + } + + public String getEmail() { + return email; + } + + public String getFirstname() { + return firstname; + } + + public String getLastname() { + return lastname; + } + + public String getPrincipal() { + return principal; + } + + public String getUsername() { + return username; + } + + public String getDomain() { + return domain; + } + + @Override + public int hashCode() { + return getUsername().hashCode(); + } } \ No newline at end of file diff --git a/plugins/user-authenticators/ldap/src/org/apache/cloudstack/ldap/LdapUserManager.java b/plugins/user-authenticators/ldap/src/org/apache/cloudstack/ldap/LdapUserManager.java index 59a41dee75f..e84c90488b9 100644 --- a/plugins/user-authenticators/ldap/src/org/apache/cloudstack/ldap/LdapUserManager.java +++ b/plugins/user-authenticators/ldap/src/org/apache/cloudstack/ldap/LdapUserManager.java @@ -34,153 +34,153 @@ public class LdapUserManager { } public LdapUserManager(final LdapConfiguration ldapConfiguration) { - _ldapConfiguration = ldapConfiguration; + _ldapConfiguration = ldapConfiguration; } private LdapUser createUser(final SearchResult result) throws NamingException { - final Attributes attributes = result.getAttributes(); + final Attributes attributes = result.getAttributes(); - final String username = LdapUtils.getAttributeValue(attributes, _ldapConfiguration.getUsernameAttribute()); - final String email = LdapUtils.getAttributeValue(attributes, _ldapConfiguration.getEmailAttribute()); - final String firstname = LdapUtils.getAttributeValue(attributes, _ldapConfiguration.getFirstnameAttribute()); - final String lastname = LdapUtils.getAttributeValue(attributes, _ldapConfiguration.getLastnameAttribute()); - final String principal = result.getNameInNamespace(); + final String username = LdapUtils.getAttributeValue(attributes, _ldapConfiguration.getUsernameAttribute()); + final String email = LdapUtils.getAttributeValue(attributes, _ldapConfiguration.getEmailAttribute()); + final String firstname = LdapUtils.getAttributeValue(attributes, _ldapConfiguration.getFirstnameAttribute()); + final String lastname = LdapUtils.getAttributeValue(attributes, _ldapConfiguration.getLastnameAttribute()); + final String principal = result.getNameInNamespace(); - String domain = principal.replace("cn="+LdapUtils.getAttributeValue(attributes,_ldapConfiguration.getCommonNameAttribute())+",", ""); - domain = domain.replace(","+_ldapConfiguration.getBaseDn(), ""); - domain = domain.replace("ou=",""); + String domain = principal.replace("cn="+LdapUtils.getAttributeValue(attributes,_ldapConfiguration.getCommonNameAttribute())+",", ""); + domain = domain.replace(","+_ldapConfiguration.getBaseDn(), ""); + domain = domain.replace("ou=",""); - return new LdapUser(username, email, firstname, lastname, principal, domain); + return new LdapUser(username, email, firstname, lastname, principal, domain); } private String generateSearchFilter(final String username) { - final StringBuilder userObjectFilter = new StringBuilder(); - userObjectFilter.append("(objectClass="); - userObjectFilter.append(_ldapConfiguration.getUserObject()); - userObjectFilter.append(")"); + final StringBuilder userObjectFilter = new StringBuilder(); + userObjectFilter.append("(objectClass="); + userObjectFilter.append(_ldapConfiguration.getUserObject()); + userObjectFilter.append(")"); - final StringBuilder usernameFilter = new StringBuilder(); - usernameFilter.append("("); - usernameFilter.append(_ldapConfiguration.getUsernameAttribute()); - usernameFilter.append("="); - usernameFilter.append((username == null ? "*" : username)); - usernameFilter.append(")"); + final StringBuilder usernameFilter = new StringBuilder(); + usernameFilter.append("("); + usernameFilter.append(_ldapConfiguration.getUsernameAttribute()); + usernameFilter.append("="); + usernameFilter.append((username == null ? "*" : username)); + usernameFilter.append(")"); - final StringBuilder memberOfFilter = new StringBuilder(); - if (_ldapConfiguration.getSearchGroupPrinciple() != null) { - memberOfFilter.append("(memberof="); - memberOfFilter.append(_ldapConfiguration.getSearchGroupPrinciple()); - memberOfFilter.append(")"); - } + final StringBuilder memberOfFilter = new StringBuilder(); + if (_ldapConfiguration.getSearchGroupPrinciple() != null) { + memberOfFilter.append("(memberof="); + memberOfFilter.append(_ldapConfiguration.getSearchGroupPrinciple()); + memberOfFilter.append(")"); + } - final StringBuilder result = new StringBuilder(); - result.append("(&"); - result.append(userObjectFilter); - result.append(usernameFilter); - result.append(memberOfFilter); - result.append(")"); + final StringBuilder result = new StringBuilder(); + result.append("(&"); + result.append(userObjectFilter); + result.append(usernameFilter); + result.append(memberOfFilter); + result.append(")"); - return result.toString(); + return result.toString(); } private String generateGroupSearchFilter(final String groupName) { - final StringBuilder groupObjectFilter = new StringBuilder(); - groupObjectFilter.append("(objectClass="); - groupObjectFilter.append(_ldapConfiguration.getGroupObject()); - groupObjectFilter.append(")"); + final StringBuilder groupObjectFilter = new StringBuilder(); + groupObjectFilter.append("(objectClass="); + groupObjectFilter.append(_ldapConfiguration.getGroupObject()); + groupObjectFilter.append(")"); - final StringBuilder groupNameFilter = new StringBuilder(); - groupNameFilter.append("("); - groupNameFilter.append(_ldapConfiguration.getCommonNameAttribute()); - groupNameFilter.append("="); - groupNameFilter.append((groupName == null ? "*" : groupName)); - groupNameFilter.append(")"); + final StringBuilder groupNameFilter = new StringBuilder(); + groupNameFilter.append("("); + groupNameFilter.append(_ldapConfiguration.getCommonNameAttribute()); + groupNameFilter.append("="); + groupNameFilter.append((groupName == null ? "*" : groupName)); + groupNameFilter.append(")"); - final StringBuilder result = new StringBuilder(); - result.append("(&"); - result.append(groupObjectFilter); - result.append(groupNameFilter); - result.append(")"); + final StringBuilder result = new StringBuilder(); + result.append("(&"); + result.append(groupObjectFilter); + result.append(groupNameFilter); + result.append(")"); - return result.toString(); + return result.toString(); } public LdapUser getUser(final String username, final DirContext context) throws NamingException { - final NamingEnumeration result = searchUsers(username, context); - if (result.hasMoreElements()) { - return createUser(result.nextElement()); - } else { - throw new NamingException("No user found for username " + username); - } + final NamingEnumeration result = searchUsers(username, context); + if (result.hasMoreElements()) { + return createUser(result.nextElement()); + } else { + throw new NamingException("No user found for username " + username); + } } public List getUsers(final DirContext context) throws NamingException { - return getUsers(null, context); + return getUsers(null, context); } public List getUsers(final String username, final DirContext context) throws NamingException { - final NamingEnumeration results = searchUsers(username, context); + final NamingEnumeration results = searchUsers(username, context); - final List users = new ArrayList(); + final List users = new ArrayList(); - while (results.hasMoreElements()) { - final SearchResult result = results.nextElement(); - users.add(createUser(result)); - } + while (results.hasMoreElements()) { + final SearchResult result = results.nextElement(); + users.add(createUser(result)); + } - Collections.sort(users); + Collections.sort(users); - return users; + return users; } public List getUsersInGroup(String groupName, DirContext context) throws NamingException { - String attributeName = _ldapConfiguration.getGroupUniqueMemeberAttribute(); - final SearchControls controls = new SearchControls(); - controls.setSearchScope(_ldapConfiguration.getScope()); - controls.setReturningAttributes(new String[]{attributeName}); + String attributeName = _ldapConfiguration.getGroupUniqueMemeberAttribute(); + final SearchControls controls = new SearchControls(); + controls.setSearchScope(_ldapConfiguration.getScope()); + controls.setReturningAttributes(new String[] {attributeName}); - NamingEnumeration result = context.search(_ldapConfiguration.getBaseDn(), generateGroupSearchFilter(groupName), controls); + NamingEnumeration result = context.search(_ldapConfiguration.getBaseDn(), generateGroupSearchFilter(groupName), controls); - final List users = new ArrayList(); - //Expecting only one result which has all the users - if (result.hasMoreElements()) { - Attribute attribute = result.nextElement().getAttributes().get(attributeName); - NamingEnumeration values = attribute.getAll(); + final List users = new ArrayList(); + //Expecting only one result which has all the users + if (result.hasMoreElements()) { + Attribute attribute = result.nextElement().getAttributes().get(attributeName); + NamingEnumeration values = attribute.getAll(); - while (values.hasMoreElements()) { - String userdn = String.valueOf(values.nextElement()); - users.add(getUserForDn(userdn,context)); - } - } + while (values.hasMoreElements()) { + String userdn = String.valueOf(values.nextElement()); + users.add(getUserForDn(userdn,context)); + } + } - Collections.sort(users); + Collections.sort(users); - return users; + return users; } private LdapUser getUserForDn(String userdn, DirContext context) throws NamingException { - final SearchControls controls = new SearchControls(); - controls.setSearchScope(_ldapConfiguration.getScope()); - controls.setReturningAttributes(_ldapConfiguration.getReturnAttributes()); + final SearchControls controls = new SearchControls(); + controls.setSearchScope(_ldapConfiguration.getScope()); + controls.setReturningAttributes(_ldapConfiguration.getReturnAttributes()); - NamingEnumeration result = context.search(userdn, "(objectClass="+_ldapConfiguration.getUserObject()+")", controls); - if (result.hasMoreElements()) { - return createUser(result.nextElement()); - } else { - throw new NamingException("No user found for dn " + userdn); - } + NamingEnumeration result = context.search(userdn, "(objectClass="+_ldapConfiguration.getUserObject()+")", controls); + if (result.hasMoreElements()) { + return createUser(result.nextElement()); + } else { + throw new NamingException("No user found for dn " + userdn); + } } public NamingEnumeration searchUsers(final DirContext context) throws NamingException { - return searchUsers(null, context); + return searchUsers(null, context); } public NamingEnumeration searchUsers(final String username, final DirContext context) throws NamingException { - final SearchControls controls = new SearchControls(); + final SearchControls controls = new SearchControls(); - controls.setSearchScope(_ldapConfiguration.getScope()); - controls.setReturningAttributes(_ldapConfiguration.getReturnAttributes()); + controls.setSearchScope(_ldapConfiguration.getScope()); + controls.setReturningAttributes(_ldapConfiguration.getReturnAttributes()); - return context.search(_ldapConfiguration.getBaseDn(), generateSearchFilter(username), controls); + return context.search(_ldapConfiguration.getBaseDn(), generateSearchFilter(username), controls); } } \ No newline at end of file diff --git a/plugins/user-authenticators/ldap/src/org/apache/cloudstack/ldap/LdapUtils.java b/plugins/user-authenticators/ldap/src/org/apache/cloudstack/ldap/LdapUtils.java index e1f999b72df..983a3d352a3 100644 --- a/plugins/user-authenticators/ldap/src/org/apache/cloudstack/ldap/LdapUtils.java +++ b/plugins/user-authenticators/ldap/src/org/apache/cloudstack/ldap/LdapUtils.java @@ -21,42 +21,42 @@ import javax.naming.directory.Attribute; import javax.naming.directory.Attributes; public final class LdapUtils { - public static String escapeLDAPSearchFilter(final String filter) { - final StringBuilder sb = new StringBuilder(); - for (final char character : filter.toCharArray()) { - switch (character) { - case '\\': - sb.append("\\5c"); - break; - case '*': - sb.append("\\2a"); - break; - case '(': - sb.append("\\28"); - break; - case ')': - sb.append("\\29"); - break; - case '\u0000': - sb.append("\\00"); - break; - default: - sb.append(character); - } - } - return sb.toString(); - } + public static String escapeLDAPSearchFilter(final String filter) { + final StringBuilder sb = new StringBuilder(); + for (final char character : filter.toCharArray()) { + switch (character) { + case '\\': + sb.append("\\5c"); + break; + case '*': + sb.append("\\2a"); + break; + case '(': + sb.append("\\28"); + break; + case ')': + sb.append("\\29"); + break; + case '\u0000': + sb.append("\\00"); + break; + default: + sb.append(character); + } + } + return sb.toString(); + } - public static String getAttributeValue(final Attributes attributes, - final String attributeName) throws NamingException { - final Attribute attribute = attributes.get(attributeName); - if (attribute != null) { - final Object value = attribute.get(); - return String.valueOf(value); - } - return null; - } + public static String getAttributeValue(final Attributes attributes, + final String attributeName) throws NamingException { + final Attribute attribute = attributes.get(attributeName); + if (attribute != null) { + final Object value = attribute.get(); + return String.valueOf(value); + } + return null; + } - private LdapUtils() { - } + private LdapUtils() { + } } \ No newline at end of file diff --git a/plugins/user-authenticators/ldap/src/org/apache/cloudstack/ldap/NoLdapUserMatchingQueryException.java b/plugins/user-authenticators/ldap/src/org/apache/cloudstack/ldap/NoLdapUserMatchingQueryException.java index d7a3744221d..0f510c39a1a 100644 --- a/plugins/user-authenticators/ldap/src/org/apache/cloudstack/ldap/NoLdapUserMatchingQueryException.java +++ b/plugins/user-authenticators/ldap/src/org/apache/cloudstack/ldap/NoLdapUserMatchingQueryException.java @@ -17,16 +17,16 @@ package org.apache.cloudstack.ldap; public class NoLdapUserMatchingQueryException extends Exception { - private static final long serialVersionUID = 7124360347208388174L; + private static final long serialVersionUID = 7124360347208388174L; - private final String query; + private final String query; - public NoLdapUserMatchingQueryException(final String query) { - super("No users matching: " + query); - this.query = query; - } + public NoLdapUserMatchingQueryException(final String query) { + super("No users matching: " + query); + this.query = query; + } - public String getQuery() { - return query; - } + public String getQuery() { + return query; + } } \ No newline at end of file diff --git a/plugins/user-authenticators/ldap/src/org/apache/cloudstack/ldap/NoSuchLdapUserException.java b/plugins/user-authenticators/ldap/src/org/apache/cloudstack/ldap/NoSuchLdapUserException.java index 91279ae893c..d9bf13f6e49 100644 --- a/plugins/user-authenticators/ldap/src/org/apache/cloudstack/ldap/NoSuchLdapUserException.java +++ b/plugins/user-authenticators/ldap/src/org/apache/cloudstack/ldap/NoSuchLdapUserException.java @@ -17,15 +17,15 @@ package org.apache.cloudstack.ldap; public class NoSuchLdapUserException extends Exception { - private static final long serialVersionUID = 6782938919658010900L; - private final String username; + private static final long serialVersionUID = 6782938919658010900L; + private final String username; - public NoSuchLdapUserException(final String username) { - super("No such user: " + username); - this.username = username; - } + public NoSuchLdapUserException(final String username) { + super("No such user: " + username); + this.username = username; + } - public String getUsername() { - return username; - } + public String getUsername() { + return username; + } } diff --git a/plugins/user-authenticators/ldap/src/org/apache/cloudstack/ldap/dao/LdapConfigurationDao.java b/plugins/user-authenticators/ldap/src/org/apache/cloudstack/ldap/dao/LdapConfigurationDao.java index f74bb9cd8f9..6774ab226c3 100644 --- a/plugins/user-authenticators/ldap/src/org/apache/cloudstack/ldap/dao/LdapConfigurationDao.java +++ b/plugins/user-authenticators/ldap/src/org/apache/cloudstack/ldap/dao/LdapConfigurationDao.java @@ -24,9 +24,9 @@ import com.cloud.utils.Pair; import com.cloud.utils.db.GenericDao; public interface LdapConfigurationDao extends - GenericDao { - LdapConfigurationVO findByHostname(String hostname); + GenericDao { + LdapConfigurationVO findByHostname(String hostname); - Pair, Integer> searchConfigurations( - String hostname, int port); + Pair, Integer> searchConfigurations( + String hostname, int port); } \ No newline at end of file diff --git a/plugins/user-authenticators/ldap/src/org/apache/cloudstack/ldap/dao/LdapConfigurationDaoImpl.java b/plugins/user-authenticators/ldap/src/org/apache/cloudstack/ldap/dao/LdapConfigurationDaoImpl.java index 862206ddf23..4ed0a373db6 100644 --- a/plugins/user-authenticators/ldap/src/org/apache/cloudstack/ldap/dao/LdapConfigurationDaoImpl.java +++ b/plugins/user-authenticators/ldap/src/org/apache/cloudstack/ldap/dao/LdapConfigurationDaoImpl.java @@ -32,41 +32,41 @@ import com.cloud.utils.db.SearchCriteria.Op; @Component @Local(value = { LdapConfigurationDao.class }) public class LdapConfigurationDaoImpl extends - GenericDaoBase implements - LdapConfigurationDao { - private final SearchBuilder hostnameSearch; - private final SearchBuilder listAllConfigurationsSearch; + GenericDaoBase implements + LdapConfigurationDao { + private final SearchBuilder hostnameSearch; + private final SearchBuilder listAllConfigurationsSearch; - public LdapConfigurationDaoImpl() { - super(); - hostnameSearch = createSearchBuilder(); - hostnameSearch.and("hostname", hostnameSearch.entity().getHostname(), - SearchCriteria.Op.EQ); - hostnameSearch.done(); + public LdapConfigurationDaoImpl() { + super(); + hostnameSearch = createSearchBuilder(); + hostnameSearch.and("hostname", hostnameSearch.entity().getHostname(), + SearchCriteria.Op.EQ); + hostnameSearch.done(); - listAllConfigurationsSearch = createSearchBuilder(); - listAllConfigurationsSearch.and("hostname", listAllConfigurationsSearch - .entity().getHostname(), Op.EQ); - listAllConfigurationsSearch.and("port", listAllConfigurationsSearch - .entity().getPort(), Op.EQ); - listAllConfigurationsSearch.done(); - } + listAllConfigurationsSearch = createSearchBuilder(); + listAllConfigurationsSearch.and("hostname", listAllConfigurationsSearch + .entity().getHostname(), Op.EQ); + listAllConfigurationsSearch.and("port", listAllConfigurationsSearch + .entity().getPort(), Op.EQ); + listAllConfigurationsSearch.done(); + } - @Override - public LdapConfigurationVO findByHostname(final String hostname) { - final SearchCriteria sc = hostnameSearch.create(); - sc.setParameters("hostname", hostname); - return findOneBy(sc); - } + @Override + public LdapConfigurationVO findByHostname(final String hostname) { + final SearchCriteria sc = hostnameSearch.create(); + sc.setParameters("hostname", hostname); + return findOneBy(sc); + } - @Override - public Pair, Integer> searchConfigurations( - final String hostname, final int port) { - final SearchCriteria sc = listAllConfigurationsSearch - .create(); - if (hostname != null) { - sc.setParameters("hostname", hostname); - } - return searchAndCount(sc, null); - } + @Override + public Pair, Integer> searchConfigurations( + final String hostname, final int port) { + final SearchCriteria sc = listAllConfigurationsSearch + .create(); + if (hostname != null) { + sc.setParameters("hostname", hostname); + } + return searchAndCount(sc, null); + } } \ No newline at end of file