mirror of
https://github.com/apache/cloudstack.git
synced 2025-10-26 08:42:29 +01:00
Fix codestyle/formatting within plugins/userauthenticators/ldap
This commit is contained in:
parent
917ea33ba9
commit
31758ed8d0
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -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<String, String> details;
|
||||
@Parameter(name = ApiConstants.ACCOUNT_DETAILS, type = CommandType.MAP, description = "details for account used to store specific parameters")
|
||||
private Map<String, String> 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;
|
||||
}
|
||||
}
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@ -58,12 +58,12 @@ public class LdapImportUsersCmd extends BaseListCmd {
|
||||
private Map<String, String> 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<LdapUser> 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());
|
||||
|
||||
@ -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<LdapConfigurationResponse> createLdapConfigurationResponses(
|
||||
final List<? extends LdapConfigurationVO> configurations) {
|
||||
final List<LdapConfigurationResponse> responses = new ArrayList<LdapConfigurationResponse>();
|
||||
for (final LdapConfigurationVO resource : configurations) {
|
||||
final LdapConfigurationResponse configurationResponse = _ldapManager
|
||||
.createLdapConfigurationResponse(resource);
|
||||
configurationResponse.setObjectName("LdapConfiguration");
|
||||
responses.add(configurationResponse);
|
||||
}
|
||||
return responses;
|
||||
}
|
||||
private List<LdapConfigurationResponse> createLdapConfigurationResponses(
|
||||
final List<? extends LdapConfigurationVO> configurations) {
|
||||
final List<LdapConfigurationResponse> responses = new ArrayList<LdapConfigurationResponse>();
|
||||
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<List<? extends LdapConfigurationVO>, Integer> result = _ldapManager
|
||||
.listConfigurations(this);
|
||||
final List<LdapConfigurationResponse> responses = createLdapConfigurationResponses(result
|
||||
.first());
|
||||
final ListResponse<LdapConfigurationResponse> response = new ListResponse<LdapConfigurationResponse>();
|
||||
response.setResponses(responses, result.second());
|
||||
response.setResponseName(getCommandName());
|
||||
setResponseObject(response);
|
||||
}
|
||||
@Override
|
||||
public void execute() {
|
||||
final Pair<List<? extends LdapConfigurationVO>, Integer> result = _ldapManager
|
||||
.listConfigurations(this);
|
||||
final List<LdapConfigurationResponse> responses = createLdapConfigurationResponses(result
|
||||
.first());
|
||||
final ListResponse<LdapConfigurationResponse> response = new ListResponse<LdapConfigurationResponse>();
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
@ -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<LdapUserResponse> createLdapUserResponse(
|
||||
final List<LdapUser> users) {
|
||||
final List<LdapUserResponse> ldapResponses = new ArrayList<LdapUserResponse>();
|
||||
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<LdapUserResponse> createLdapUserResponse(
|
||||
final List<LdapUser> users) {
|
||||
final List<LdapUserResponse> ldapResponses = new ArrayList<LdapUserResponse>();
|
||||
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<LdapUserResponse> ldapResponses = null;
|
||||
final ListResponse<LdapUserResponse> response = new ListResponse<LdapUserResponse>();
|
||||
try {
|
||||
final List<LdapUser> users = _ldapManager.getUsers();
|
||||
ldapResponses = createLdapUserResponse(users);
|
||||
} catch (final NoLdapUserMatchingQueryException ex) {
|
||||
ldapResponses = new ArrayList<LdapUserResponse>();
|
||||
} finally {
|
||||
response.setResponses(ldapResponses);
|
||||
response.setResponseName(getCommandName());
|
||||
setResponseObject(response);
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void execute() throws ServerApiException {
|
||||
List<LdapUserResponse> ldapResponses = null;
|
||||
final ListResponse<LdapUserResponse> response = new ListResponse<LdapUserResponse>();
|
||||
try {
|
||||
final List<LdapUser> users = _ldapManager.getUsers();
|
||||
ldapResponses = createLdapUserResponse(users);
|
||||
} catch (final NoLdapUserMatchingQueryException ex) {
|
||||
ldapResponses = new ArrayList<LdapUserResponse>();
|
||||
} 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<UserResponse> response = _queryService
|
||||
.searchForUsers(new ListUsersCmd());
|
||||
final List<UserResponse> 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<UserResponse> response = _queryService
|
||||
.searchForUsers(new ListUsersCmd());
|
||||
final List<UserResponse> 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;
|
||||
}
|
||||
}
|
||||
@ -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<LdapUserResponse> createLdapUserResponse(
|
||||
final List<LdapUser> users) {
|
||||
final List<LdapUserResponse> ldapUserResponses = new ArrayList<LdapUserResponse>();
|
||||
if (users != null) {
|
||||
for (final LdapUser user : users) {
|
||||
final LdapUserResponse ldapUserResponse = _ldapManager
|
||||
.createLdapUserResponse(user);
|
||||
ldapUserResponse.setObjectName("LdapUser");
|
||||
ldapUserResponses.add(ldapUserResponse);
|
||||
}
|
||||
}
|
||||
return ldapUserResponses;
|
||||
}
|
||||
private List<LdapUserResponse> createLdapUserResponse(
|
||||
final List<LdapUser> users) {
|
||||
final List<LdapUserResponse> ldapUserResponses = new ArrayList<LdapUserResponse>();
|
||||
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<LdapUserResponse> response = new ListResponse<LdapUserResponse>();
|
||||
List<LdapUser> users = null;
|
||||
@Override
|
||||
public void execute() {
|
||||
final ListResponse<LdapUserResponse> response = new ListResponse<LdapUserResponse>();
|
||||
List<LdapUser> 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<LdapUserResponse> ldapUserResponses = createLdapUserResponse(users);
|
||||
final List<LdapUserResponse> 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;
|
||||
}
|
||||
}
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
@ -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<String, Object[]> requestParameters) {
|
||||
@Override
|
||||
public boolean authenticate(final String username, final String password,
|
||||
final Long domainId, final Map<String, Object[]> 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;
|
||||
}
|
||||
}
|
||||
|
||||
@ -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<List<? extends LdapConfigurationVO>, 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<List<? extends LdapConfigurationVO>, 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";
|
||||
}
|
||||
}
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
@ -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<String, String> environment) {
|
||||
final boolean sslStatus = _ldapConfiguration.getSSLStatus();
|
||||
private void enableSSL(final Hashtable<String, String> 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<String, String> 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<String, String> 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<String, String> environment = new Hashtable<String, String>();
|
||||
final Hashtable<String, String> environment = new Hashtable<String, String>();
|
||||
|
||||
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<String, String> environment,
|
||||
final boolean isSystemContext) {
|
||||
final String authentication = _ldapConfiguration.getAuthentication();
|
||||
private void setAuthentication(final Hashtable<String, String> 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -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<LdapUser> getUsers() throws NoLdapUserMatchingQueryException;
|
||||
List<LdapUser> getUsers() throws NoLdapUserMatchingQueryException;
|
||||
|
||||
List<LdapUser> getUsersInGroup(String groupName) throws NoLdapUserMatchingQueryException;
|
||||
|
||||
boolean isLdapEnabled();
|
||||
boolean isLdapEnabled();
|
||||
|
||||
Pair<List<? extends LdapConfigurationVO>, Integer> listConfigurations(
|
||||
LdapListConfigurationCmd cmd);
|
||||
Pair<List<? extends LdapConfigurationVO>, Integer> listConfigurations(
|
||||
LdapListConfigurationCmd cmd);
|
||||
|
||||
List<LdapUser> searchUsers(String query)
|
||||
throws NoLdapUserMatchingQueryException;
|
||||
List<LdapUser> searchUsers(String query)
|
||||
throws NoLdapUserMatchingQueryException;
|
||||
}
|
||||
@ -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<Class<?>> getCommands() {
|
||||
final List<Class<?>> cmdList = new ArrayList<Class<?>>();
|
||||
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<LdapUser> 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<LdapUser> 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<List<? extends LdapConfigurationVO>, Integer> listConfigurations(
|
||||
final LdapListConfigurationCmd cmd) {
|
||||
final String hostname = cmd.getHostname();
|
||||
final int port = cmd.getPort();
|
||||
final Pair<List<LdapConfigurationVO>, Integer> result = _ldapConfigurationDao
|
||||
.searchConfigurations(hostname, port);
|
||||
return new Pair<List<? extends LdapConfigurationVO>, 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<LdapUser> 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<Class<?>> getCommands() {
|
||||
final List<Class<?>> cmdList = new ArrayList<Class<?>>();
|
||||
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<LdapUser> 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<LdapUser> 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<List<? extends LdapConfigurationVO>, Integer> listConfigurations(
|
||||
final LdapListConfigurationCmd cmd) {
|
||||
final String hostname = cmd.getHostname();
|
||||
final int port = cmd.getPort();
|
||||
final Pair<List<LdapConfigurationVO>, Integer> result = _ldapConfigurationDao
|
||||
.searchConfigurations(hostname, port);
|
||||
return new Pair<List<? extends LdapConfigurationVO>, Integer>(
|
||||
result.first(), result.second());
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<LdapUser> 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -17,65 +17,65 @@
|
||||
package org.apache.cloudstack.ldap;
|
||||
|
||||
public class LdapUser implements Comparable<LdapUser> {
|
||||
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();
|
||||
}
|
||||
}
|
||||
@ -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<SearchResult> result = searchUsers(username, context);
|
||||
if (result.hasMoreElements()) {
|
||||
return createUser(result.nextElement());
|
||||
} else {
|
||||
throw new NamingException("No user found for username " + username);
|
||||
}
|
||||
final NamingEnumeration<SearchResult> result = searchUsers(username, context);
|
||||
if (result.hasMoreElements()) {
|
||||
return createUser(result.nextElement());
|
||||
} else {
|
||||
throw new NamingException("No user found for username " + username);
|
||||
}
|
||||
}
|
||||
|
||||
public List<LdapUser> getUsers(final DirContext context) throws NamingException {
|
||||
return getUsers(null, context);
|
||||
return getUsers(null, context);
|
||||
}
|
||||
|
||||
public List<LdapUser> getUsers(final String username, final DirContext context) throws NamingException {
|
||||
final NamingEnumeration<SearchResult> results = searchUsers(username, context);
|
||||
final NamingEnumeration<SearchResult> results = searchUsers(username, context);
|
||||
|
||||
final List<LdapUser> users = new ArrayList<LdapUser>();
|
||||
final List<LdapUser> users = new ArrayList<LdapUser>();
|
||||
|
||||
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<LdapUser> 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<SearchResult> result = context.search(_ldapConfiguration.getBaseDn(), generateGroupSearchFilter(groupName), controls);
|
||||
NamingEnumeration<SearchResult> result = context.search(_ldapConfiguration.getBaseDn(), generateGroupSearchFilter(groupName), controls);
|
||||
|
||||
final List<LdapUser> users = new ArrayList<LdapUser>();
|
||||
//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<LdapUser> users = new ArrayList<LdapUser>();
|
||||
//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<SearchResult> 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<SearchResult> 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<SearchResult> searchUsers(final DirContext context) throws NamingException {
|
||||
return searchUsers(null, context);
|
||||
return searchUsers(null, context);
|
||||
}
|
||||
|
||||
public NamingEnumeration<SearchResult> 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);
|
||||
}
|
||||
}
|
||||
@ -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() {
|
||||
}
|
||||
}
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@ -24,9 +24,9 @@ import com.cloud.utils.Pair;
|
||||
import com.cloud.utils.db.GenericDao;
|
||||
|
||||
public interface LdapConfigurationDao extends
|
||||
GenericDao<LdapConfigurationVO, Long> {
|
||||
LdapConfigurationVO findByHostname(String hostname);
|
||||
GenericDao<LdapConfigurationVO, Long> {
|
||||
LdapConfigurationVO findByHostname(String hostname);
|
||||
|
||||
Pair<List<LdapConfigurationVO>, Integer> searchConfigurations(
|
||||
String hostname, int port);
|
||||
Pair<List<LdapConfigurationVO>, Integer> searchConfigurations(
|
||||
String hostname, int port);
|
||||
}
|
||||
@ -32,41 +32,41 @@ import com.cloud.utils.db.SearchCriteria.Op;
|
||||
@Component
|
||||
@Local(value = { LdapConfigurationDao.class })
|
||||
public class LdapConfigurationDaoImpl extends
|
||||
GenericDaoBase<LdapConfigurationVO, Long> implements
|
||||
LdapConfigurationDao {
|
||||
private final SearchBuilder<LdapConfigurationVO> hostnameSearch;
|
||||
private final SearchBuilder<LdapConfigurationVO> listAllConfigurationsSearch;
|
||||
GenericDaoBase<LdapConfigurationVO, Long> implements
|
||||
LdapConfigurationDao {
|
||||
private final SearchBuilder<LdapConfigurationVO> hostnameSearch;
|
||||
private final SearchBuilder<LdapConfigurationVO> 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<LdapConfigurationVO> sc = hostnameSearch.create();
|
||||
sc.setParameters("hostname", hostname);
|
||||
return findOneBy(sc);
|
||||
}
|
||||
@Override
|
||||
public LdapConfigurationVO findByHostname(final String hostname) {
|
||||
final SearchCriteria<LdapConfigurationVO> sc = hostnameSearch.create();
|
||||
sc.setParameters("hostname", hostname);
|
||||
return findOneBy(sc);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Pair<List<LdapConfigurationVO>, Integer> searchConfigurations(
|
||||
final String hostname, final int port) {
|
||||
final SearchCriteria<LdapConfigurationVO> sc = listAllConfigurationsSearch
|
||||
.create();
|
||||
if (hostname != null) {
|
||||
sc.setParameters("hostname", hostname);
|
||||
}
|
||||
return searchAndCount(sc, null);
|
||||
}
|
||||
@Override
|
||||
public Pair<List<LdapConfigurationVO>, Integer> searchConfigurations(
|
||||
final String hostname, final int port) {
|
||||
final SearchCriteria<LdapConfigurationVO> sc = listAllConfigurationsSearch
|
||||
.create();
|
||||
if (hostname != null) {
|
||||
sc.setParameters("hostname", hostname);
|
||||
}
|
||||
return searchAndCount(sc, null);
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user