Skip to content

Commit f2b97db

Browse files
author
Abhinandan Prateek
committed
CLOUDSTACK-1172: LDAP enhancements
1 parent c94a75c commit f2b97db

6 files changed

Lines changed: 99 additions & 17 deletions

File tree

‎api/src/com/cloud/configuration/ConfigurationService.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,8 @@ public interface ConfigurationService {
264264

265265
boolean removeLDAP(LDAPRemoveCmd cmd);
266266

267+
LDAPConfigCmd listLDAPConfig(LDAPConfigCmd cmd);
268+
267269
/**
268270
* @param offering
269271
* @return

‎api/src/org/apache/cloudstack/api/command/admin/ldap/LDAPConfigCmd.java‎

Lines changed: 54 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import com.cloud.exception.InsufficientCapacityException;
3232
import com.cloud.exception.ResourceAllocationException;
3333
import com.cloud.exception.ResourceUnavailableException;
34+
import com.cloud.exception.InvalidParameterValueException;
3435
import com.cloud.user.Account;
3536

3637
@APICommand(name = "ldapConfig", description="Configure the LDAP context for this site.", responseObject=LDAPConfigResponse.class, since="3.0.0")
@@ -43,7 +44,10 @@ public class LDAPConfigCmd extends BaseCmd {
4344
//////////////// API parameters /////////////////////
4445
/////////////////////////////////////////////////////
4546

46-
@Parameter(name=ApiConstants.HOST_NAME, type=CommandType.STRING, required=true, description="Hostname or ip address of the ldap server eg: my.ldap.com")
47+
@Parameter(name=ApiConstants.LIST_ALL, type=CommandType.STRING, description="Hostname or ip address of the ldap server eg: my.ldap.com")
48+
private String listall;
49+
50+
@Parameter(name=ApiConstants.HOST_NAME, type=CommandType.STRING, description="Hostname or ip address of the ldap server eg: my.ldap.com")
4751
private String hostname;
4852

4953
@Parameter(name=ApiConstants.PORT, type=CommandType.INTEGER, description="Specify the LDAP port if required, default is 389.")
@@ -52,10 +56,10 @@ public class LDAPConfigCmd extends BaseCmd {
5256
@Parameter(name=ApiConstants.USE_SSL, type=CommandType.BOOLEAN, description="Check Use SSL if the external LDAP server is configured for LDAP over SSL.")
5357
private Boolean useSSL;
5458

55-
@Parameter(name=ApiConstants.SEARCH_BASE, type=CommandType.STRING, required=true, description="The search base defines the starting point for the search in the directory tree Example: dc=cloud,dc=com.")
59+
@Parameter(name=ApiConstants.SEARCH_BASE, type=CommandType.STRING, description="The search base defines the starting point for the search in the directory tree Example: dc=cloud,dc=com.")
5660
private String searchBase;
5761

58-
@Parameter(name=ApiConstants.QUERY_FILTER, type=CommandType.STRING, required=true, description="You specify a query filter here, which narrows down the users, who can be part of this domain.")
62+
@Parameter(name=ApiConstants.QUERY_FILTER, type=CommandType.STRING, description="You specify a query filter here, which narrows down the users, who can be part of this domain.")
5963
private String queryFilter;
6064

6165
@Parameter(name=ApiConstants.BIND_DN, type=CommandType.STRING, description="Specify the distinguished name of a user with the search permission on the directory.")
@@ -74,6 +78,10 @@ public class LDAPConfigCmd extends BaseCmd {
7478
/////////////////// Accessors ///////////////////////
7579
/////////////////////////////////////////////////////
7680

81+
public String getListAll() {
82+
return listall == null ? "false" : listall;
83+
}
84+
7785
public String getBindPassword() {
7886
return bindPassword;
7987
}
@@ -82,30 +90,56 @@ public String getBindDN() {
8290
return bindDN;
8391
}
8492

93+
public void setBindDN(String bdn) {
94+
this.bindDN=bdn;
95+
}
96+
8597
public String getQueryFilter() {
8698
return queryFilter;
8799
}
88100

101+
public void setQueryFilter(String queryFilter) {
102+
this.queryFilter=queryFilter;
103+
}
89104
public String getSearchBase() {
90105
return searchBase;
91106
}
92107

108+
public void setSearchBase(String searchBase) {
109+
this.searchBase=searchBase;
110+
}
111+
93112
public Boolean getUseSSL() {
94-
return useSSL == null ? Boolean.FALSE : Boolean.TRUE;
113+
return useSSL == null ? Boolean.FALSE : useSSL;
114+
}
115+
116+
public void setUseSSL(Boolean useSSL) {
117+
this.useSSL=useSSL;
95118
}
96119

97120
public String getHostname() {
98121
return hostname;
99122
}
100123

124+
public void setHostname(String hostname) {
125+
this.hostname=hostname;
126+
}
127+
101128
public Integer getPort() {
102129
return port <= 0 ? 389 : port;
103130
}
104131

132+
public void setPort(Integer port) {
133+
this.port=port;
134+
}
135+
105136
public String getTrustStore() {
106137
return trustStore;
107138
}
108139

140+
public void setTrustStore(String trustStore) {
141+
this.trustStore=trustStore;
142+
}
109143

110144
public String getTrustStorePassword() {
111145
return trustStorePassword;
@@ -122,12 +156,25 @@ public void execute() throws ResourceUnavailableException,
122156
InsufficientCapacityException, ServerApiException,
123157
ConcurrentOperationException, ResourceAllocationException {
124158
try {
125-
boolean result = _configService.updateLDAP(this);
126-
if (result){
127-
LDAPConfigResponse lr = _responseGenerator.createLDAPConfigResponse(getHostname(), getPort(), getUseSSL(), getQueryFilter(), getSearchBase(), getBindDN());
159+
if ("true".equalsIgnoreCase(getListAll())){
160+
// return the existing conf
161+
LDAPConfigCmd cmd = _configService.listLDAPConfig(this);
162+
LDAPConfigResponse lr = _responseGenerator.createLDAPConfigResponse(cmd.getHostname(), cmd.getPort(), cmd.getUseSSL(),
163+
cmd.getQueryFilter(), cmd.getSearchBase(), cmd.getBindDN());
128164
lr.setResponseName(getCommandName());
129165
this.setResponseObject(lr);
130166
}
167+
else if (getHostname()==null || getSearchBase() == null || getQueryFilter() == null) {
168+
throw new InvalidParameterValueException("You need to provide hostname, serachbase and queryfilter to configure your LDAP server");
169+
}
170+
else {
171+
boolean result = _configService.updateLDAP(this);
172+
if (result){
173+
LDAPConfigResponse lr = _responseGenerator.createLDAPConfigResponse(getHostname(), getPort(), getUseSSL(), getQueryFilter(), getSearchBase(), getBindDN());
174+
lr.setResponseName(getCommandName());
175+
this.setResponseObject(lr);
176+
}
177+
}
131178
}
132179
catch (NamingException ne){
133180
ne.printStackTrace();

‎api/src/org/apache/cloudstack/api/response/LDAPConfigResponse.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public class LDAPConfigResponse extends BaseResponse {
3030
@SerializedName(ApiConstants.PORT) @Param(description="Specify the LDAP port if required, default is 389")
3131
private String port;
3232

33-
@SerializedName(ApiConstants.PORT) @Param(description="Check Use SSL if the external LDAP server is configured for LDAP over SSL")
33+
@SerializedName(ApiConstants.USE_SSL) @Param(description="Check Use SSL if the external LDAP server is configured for LDAP over SSL")
3434
private String useSSL;
3535

3636
@SerializedName(ApiConstants.SEARCH_BASE) @Param(description="The search base defines the starting point for the search in the directory tree Example: dc=cloud,dc=com")

‎plugins/user-authenticators/ldap/src/com/cloud/server/auth/LDAPUserAuthenticator.java‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public boolean authenticate(String username, String password, Long domainId, Map
6666
String port = _configDao.getValue(LDAPParams.port.toString());
6767
String queryFilter = _configDao.getValue(LDAPParams.queryfilter.toString());
6868
String searchBase = _configDao.getValue(LDAPParams.searchbase.toString());
69-
String useSSL = _configDao.getValue(LDAPParams.usessl.toString());
69+
Boolean useSSL = Boolean.valueOf(_configDao.getValue(LDAPParams.usessl.toString()));
7070
String bindDN = _configDao.getValue(LDAPParams.dn.toString());
7171
String bindPasswd = _configDao.getValue(LDAPParams.passwd.toString());
7272
String trustStore = _configDao.getValue(LDAPParams.truststore.toString());
@@ -77,7 +77,7 @@ public boolean authenticate(String username, String password, Long domainId, Map
7777
Hashtable<String, String> env = new Hashtable<String, String>(11);
7878
env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
7979
String protocol = "ldap://" ;
80-
if (new Boolean(useSSL)){
80+
if (useSSL){
8181
env.put(Context.SECURITY_PROTOCOL, "ssl");
8282
protocol="ldaps://" ;
8383
System.setProperty("javax.net.ssl.trustStore", trustStore);
@@ -123,7 +123,7 @@ public boolean authenticate(String username, String password, Long domainId, Map
123123
env = new Hashtable<String, String>(11);
124124
env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
125125
protocol = "ldap://" ;
126-
if (new Boolean(useSSL)){
126+
if (useSSL){
127127
env.put(Context.SECURITY_PROTOCOL, "ssl");
128128
protocol="ldaps://" ;
129129
}
@@ -135,8 +135,7 @@ public boolean authenticate(String username, String password, Long domainId, Map
135135
ctx.close();
136136

137137
} catch (NamingException ne) {
138-
ne.printStackTrace();
139-
s_logger.warn("Authentication failed due to " + ne.getMessage());
138+
s_logger.warn("Authentication Failed ! " + ne.getMessage() + (ne.getCause() != null ? ("; Caused by:" + ne.getCause().getMessage()) : ""));
140139
return false;
141140
}
142141
catch (Exception e){

‎server/src/com/cloud/configuration/ConfigurationManagerImpl.java‎

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1246,6 +1246,27 @@ public boolean removeLDAP(LDAPRemoveCmd cmd) {
12461246
return true;
12471247
}
12481248

1249+
1250+
@Override
1251+
@DB
1252+
public LDAPConfigCmd listLDAPConfig(LDAPConfigCmd cmd) {
1253+
String hostname = _configDao.getValue(LDAPParams.hostname.toString());
1254+
cmd.setHostname(hostname == null ? "" : hostname);
1255+
String port = _configDao.getValue(LDAPParams.port.toString());
1256+
cmd.setPort(port == null ? 0 : Integer.valueOf(port));
1257+
String queryFilter = _configDao.getValue(LDAPParams.queryfilter.toString());
1258+
cmd.setQueryFilter(queryFilter == null ? "" : queryFilter);
1259+
String searchBase = _configDao.getValue(LDAPParams.searchbase.toString());
1260+
cmd.setSearchBase(searchBase == null ? "" : searchBase);
1261+
String useSSL = _configDao.getValue(LDAPParams.usessl.toString());
1262+
cmd.setUseSSL(useSSL == null ? Boolean.FALSE : Boolean.valueOf(useSSL));
1263+
String binddn = _configDao.getValue(LDAPParams.dn.toString());
1264+
cmd.setBindDN(binddn == null ? "" : binddn);
1265+
String truststore = _configDao.getValue(LDAPParams.truststore.toString());
1266+
cmd.setTrustStore(truststore == null ? "" : truststore);
1267+
return cmd;
1268+
}
1269+
12491270
@Override
12501271
@DB
12511272
public boolean updateLDAP(LDAPConfigCmd cmd) {
@@ -1265,11 +1286,16 @@ public boolean updateLDAP(LDAPConfigCmd cmd) {
12651286
throw new InvalidParameterValueException("If you specify a bind name then you need to provide bind password too.");
12661287
}
12671288

1289+
// check query filter if it contains valid substitution
1290+
if (!queryFilter.contains("%u") && !queryFilter.contains("%n") && !queryFilter.contains("%e")){
1291+
throw new InvalidParameterValueException("QueryFilter should contain at least one of the substitutions: %u, %n or %e: " + queryFilter);
1292+
}
1293+
12681294
// check if the info is correct
12691295
Hashtable<String, String> env = new Hashtable<String, String>(11);
12701296
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
12711297
String protocol = "ldap://";
1272-
if (new Boolean(useSSL)) {
1298+
if (useSSL) {
12731299
env.put(Context.SECURITY_PROTOCOL, "ssl");
12741300
protocol = "ldaps://";
12751301
if (trustStore == null || trustStorePassword == null) {
@@ -1288,7 +1314,7 @@ public boolean updateLDAP(LDAPConfigCmd cmd) {
12881314
DirContext ctx = new InitialDirContext(env);
12891315
ctx.close();
12901316

1291-
// store the result in DB COnfiguration
1317+
// store the result in DB Configuration
12921318
ConfigurationVO cvo = _configDao.findByName(LDAPParams.hostname.toString());
12931319
if (cvo == null) {
12941320
cvo = new ConfigurationVO("Hidden", "DEFAULT", "management-server", LDAPParams.hostname.toString(), null, "Hostname or ip address of the ldap server eg: my.ldap.com");
@@ -1356,8 +1382,7 @@ public boolean updateLDAP(LDAPConfigCmd cmd) {
13561382

13571383
s_logger.debug("The ldap server is configured: " + hostname);
13581384
} catch (NamingException ne) {
1359-
ne.printStackTrace();
1360-
throw new InvalidParameterValueException("Naming Exception, check you ldap data ! " + ne.getMessage() + (ne.getCause() != null ? ("Caused by:" + ne.getCause().getMessage()) : ""));
1385+
throw new InvalidParameterValueException("Naming Exception, check you ldap data ! " + ne.getMessage() + (ne.getCause() != null ? ("; Caused by:" + ne.getCause().getMessage()) : ""));
13611386
}
13621387
return true;
13631388
}

‎server/test/com/cloud/vpc/MockConfigurationManagerImpl.java‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,15 @@ public boolean removeLDAP(LDAPRemoveCmd cmd) {
365365
return false;
366366
}
367367

368+
/* (non-Javadoc)
369+
* @see com.cloud.configuration.ConfigurationService#listLDAPConfig(org.apache.cloudstack.api.commands.LDAPConfigCmd)
370+
*/
371+
@Override
372+
public LDAPConfigCmd listLDAPConfig(LDAPConfigCmd cmd) {
373+
// TODO Auto-generated method stub
374+
return null;
375+
}
376+
368377
/* (non-Javadoc)
369378
* @see com.cloud.configuration.ConfigurationService#isOfferingForVpc(com.cloud.offering.NetworkOffering)
370379
*/

0 commit comments

Comments
 (0)