|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | +package org.apache.cloudstack.api.command; |
| 18 | + |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.List; |
| 21 | + |
| 22 | +import javax.inject.Inject; |
| 23 | + |
| 24 | +import org.apache.cloudstack.api.*; |
| 25 | +import org.apache.cloudstack.api.response.LDAPConfigResponse; |
| 26 | +import org.apache.cloudstack.api.response.LdapConfigurationResponse; |
| 27 | +import org.apache.cloudstack.api.response.ListResponse; |
| 28 | +import org.apache.cloudstack.framework.config.dao.ConfigurationDao; |
| 29 | +import org.apache.cloudstack.framework.config.impl.ConfigurationVO; |
| 30 | +import org.apache.cloudstack.ldap.LdapConfiguration; |
| 31 | +import org.apache.cloudstack.ldap.LdapConfigurationVO; |
| 32 | +import org.apache.cloudstack.ldap.LdapManager; |
| 33 | +import org.apache.commons.lang.StringEscapeUtils; |
| 34 | +import org.apache.log4j.Logger; |
| 35 | + |
| 36 | +import com.cloud.exception.*; |
| 37 | +import com.cloud.user.Account; |
| 38 | +import com.cloud.utils.Pair; |
| 39 | + |
| 40 | +/** |
| 41 | + * @deprecated as of 4.3 use the new api {@link LdapAddConfigurationCmd} |
| 42 | + */ |
| 43 | +@Deprecated |
| 44 | +@APICommand(name = "ldapConfig", description = "Configure the LDAP context for this site.", responseObject = LDAPConfigResponse.class, since = "3.0.0") |
| 45 | +public class LDAPConfigCmd extends BaseCmd { |
| 46 | + public static final Logger s_logger = Logger.getLogger(LDAPConfigCmd.class.getName()); |
| 47 | + |
| 48 | + private static final String s_name = "ldapconfigresponse"; |
| 49 | + |
| 50 | + @Inject |
| 51 | + private ConfigurationDao _configDao; |
| 52 | + |
| 53 | + @Inject |
| 54 | + private LdapManager _ldapManager; |
| 55 | + |
| 56 | + @Inject |
| 57 | + private LdapConfiguration _ldapConfiguration; |
| 58 | + |
| 59 | + ///////////////////////////////////////////////////// |
| 60 | + //////////////// API parameters ///////////////////// |
| 61 | + ///////////////////////////////////////////////////// |
| 62 | + @Parameter(name = ApiConstants.LIST_ALL, type = CommandType.BOOLEAN, description = "If true return current LDAP configuration") |
| 63 | + private Boolean listAll; |
| 64 | + |
| 65 | + @Parameter(name = ApiConstants.HOST_NAME, type = CommandType.STRING, description = "Hostname or ip address of the ldap server eg: my.ldap.com") |
| 66 | + private String hostname; |
| 67 | + |
| 68 | + @Parameter(name = ApiConstants.PORT, type = CommandType.INTEGER, description = "Specify the LDAP port if required, default is 389.") |
| 69 | + private Integer port = 0; |
| 70 | + |
| 71 | + @Parameter(name = ApiConstants.USE_SSL, type = CommandType.BOOLEAN, description = "Check Use SSL if the external LDAP server is configured for LDAP over SSL.") |
| 72 | + private Boolean useSSL; |
| 73 | + |
| 74 | + @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.") |
| 75 | + private String searchBase; |
| 76 | + |
| 77 | + @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.") |
| 78 | + private String queryFilter; |
| 79 | + |
| 80 | + @Parameter(name = ApiConstants.BIND_DN, type = CommandType.STRING, description = "Specify the distinguished name of a user with the search permission on the directory.") |
| 81 | + private String bindDN; |
| 82 | + |
| 83 | + @Parameter(name = ApiConstants.BIND_PASSWORD, type = CommandType.STRING, description = "Enter the password.") |
| 84 | + private String bindPassword; |
| 85 | + |
| 86 | + @Parameter(name = ApiConstants.TRUST_STORE, type = CommandType.STRING, description = "Enter the path to trust certificates store.") |
| 87 | + private String trustStore; |
| 88 | + |
| 89 | + @Parameter(name = ApiConstants.TRUST_STORE_PASSWORD, type = CommandType.STRING, description = "Enter the password for trust store.") |
| 90 | + private String trustStorePassword; |
| 91 | + |
| 92 | + public Boolean getListAll() { |
| 93 | + return listAll == null ? Boolean.FALSE : listAll; |
| 94 | + } |
| 95 | + |
| 96 | + public String getBindPassword() { |
| 97 | + return bindPassword; |
| 98 | + } |
| 99 | + |
| 100 | + public String getBindDN() { |
| 101 | + return bindDN; |
| 102 | + } |
| 103 | + |
| 104 | + public void setBindDN(String bdn) { |
| 105 | + this.bindDN = bdn; |
| 106 | + } |
| 107 | + |
| 108 | + public String getQueryFilter() { |
| 109 | + return queryFilter; |
| 110 | + } |
| 111 | + |
| 112 | + public void setQueryFilter(String queryFilter) { |
| 113 | + this.queryFilter = StringEscapeUtils.unescapeHtml(queryFilter); |
| 114 | + } |
| 115 | + |
| 116 | + public String getSearchBase() { |
| 117 | + return searchBase; |
| 118 | + } |
| 119 | + |
| 120 | + public void setSearchBase(String searchBase) { |
| 121 | + this.searchBase = searchBase; |
| 122 | + } |
| 123 | + |
| 124 | + public Boolean getUseSSL() { |
| 125 | + return useSSL == null ? Boolean.FALSE : useSSL; |
| 126 | + } |
| 127 | + |
| 128 | + public void setUseSSL(Boolean useSSL) { |
| 129 | + this.useSSL = useSSL; |
| 130 | + } |
| 131 | + |
| 132 | + public String getHostname() { |
| 133 | + return hostname; |
| 134 | + } |
| 135 | + |
| 136 | + public void setHostname(String hostname) { |
| 137 | + this.hostname = hostname; |
| 138 | + } |
| 139 | + |
| 140 | + public Integer getPort() { |
| 141 | + return port <= 0 ? 389 : port; |
| 142 | + } |
| 143 | + |
| 144 | + public void setPort(Integer port) { |
| 145 | + this.port = port; |
| 146 | + } |
| 147 | + |
| 148 | + public String getTrustStore() { |
| 149 | + return trustStore; |
| 150 | + } |
| 151 | + |
| 152 | + public void setTrustStore(String trustStore) { |
| 153 | + this.trustStore = trustStore; |
| 154 | + } |
| 155 | + |
| 156 | + public String getTrustStorePassword() { |
| 157 | + return trustStorePassword; |
| 158 | + } |
| 159 | + |
| 160 | + ///////////////////////////////////////////////////// |
| 161 | + /////////////// API Implementation/////////////////// |
| 162 | + ///////////////////////////////////////////////////// |
| 163 | + |
| 164 | + @Override |
| 165 | + public void execute() throws ResourceUnavailableException, InsufficientCapacityException, ServerApiException, ConcurrentOperationException, ResourceAllocationException { |
| 166 | + if (getListAll()) { |
| 167 | + // return the existing conf |
| 168 | + |
| 169 | + LdapListConfigurationCmd listConfigurationCmd = new LdapListConfigurationCmd(_ldapManager); |
| 170 | + Pair<List<? extends LdapConfigurationVO>, Integer> result = _ldapManager.listConfigurations(listConfigurationCmd); |
| 171 | + ListResponse<LDAPConfigResponse> response = new ListResponse<LDAPConfigResponse>(); |
| 172 | + List<LDAPConfigResponse> responses = new ArrayList<LDAPConfigResponse>(); |
| 173 | + |
| 174 | + if (result.second() > 0) { |
| 175 | + boolean useSSlConfig = _ldapConfiguration.getSSLStatus(); |
| 176 | + String searchBaseConfig = _ldapConfiguration.getBaseDn(); |
| 177 | + String bindDnConfig = _ldapConfiguration.getBindPrincipal(); |
| 178 | + for (LdapConfigurationVO ldapConfigurationVO : result.first()) { |
| 179 | + responses.add(this.createLDAPConfigResponse(ldapConfigurationVO.getHostname(), ldapConfigurationVO.getPort(), useSSlConfig, null, searchBaseConfig, |
| 180 | + bindDnConfig)); |
| 181 | + } |
| 182 | + } |
| 183 | + response.setResponses(responses); |
| 184 | + response.setResponseName(getCommandName()); |
| 185 | + this.setResponseObject(response); |
| 186 | + } else if (getHostname() == null || getPort() == null) { |
| 187 | + throw new InvalidParameterValueException("You need to provide hostname, port to configure your LDAP server"); |
| 188 | + } else { |
| 189 | + boolean result = this.updateLDAP(); |
| 190 | + if (result) { |
| 191 | + LDAPConfigResponse lr = this.createLDAPConfigResponse(getHostname(), getPort(), getUseSSL(), getQueryFilter(), getSearchBase(), getBindDN()); |
| 192 | + lr.setResponseName(getCommandName()); |
| 193 | + this.setResponseObject(lr); |
| 194 | + } |
| 195 | + } |
| 196 | + |
| 197 | + } |
| 198 | + |
| 199 | + private LDAPConfigResponse createLDAPConfigResponse(String hostname, Integer port, Boolean useSSL, String queryFilter, String searchBase, String bindDN) { |
| 200 | + LDAPConfigResponse lr = new LDAPConfigResponse(); |
| 201 | + lr.setHostname(hostname); |
| 202 | + lr.setPort(port.toString()); |
| 203 | + lr.setUseSSL(useSSL.toString()); |
| 204 | + lr.setQueryFilter(queryFilter); |
| 205 | + lr.setBindDN(bindDN); |
| 206 | + lr.setSearchBase(searchBase); |
| 207 | + lr.setObjectName("ldapconfig"); |
| 208 | + return lr; |
| 209 | + } |
| 210 | + |
| 211 | + private boolean updateLDAP() { |
| 212 | + LdapConfigurationResponse response = _ldapManager.addConfiguration(hostname, port); |
| 213 | + |
| 214 | + /** |
| 215 | + * There is no query filter now. It is derived from ldap.user.object and ldap.search.group.principle |
| 216 | + */ |
| 217 | +// ConfigurationVO cvo = _configDao.findByName(LDAPParams.queryfilter.toString()); |
| 218 | +// _configDao.update(cvo.getName(),cvo.getCategory(),getQueryFilter()); |
| 219 | + |
| 220 | + ConfigurationVO cvo = _configDao.findByName("ldap.basedn"); |
| 221 | + _configDao.update(cvo.getName(), cvo.getCategory(), getSearchBase()); |
| 222 | + |
| 223 | + /** |
| 224 | + * There is no ssl now. it is derived from the presence of trust store and password |
| 225 | + */ |
| 226 | +// cvo = _configDao.findByName(LDAPParams.usessl.toString()); |
| 227 | +// _configDao.update(cvo.getName(),cvo.getCategory(),getUseSSL().toString()); |
| 228 | + |
| 229 | + cvo = _configDao.findByName("ldap.bind.principal"); |
| 230 | + _configDao.update(cvo.getName(), cvo.getCategory(), getBindDN()); |
| 231 | + |
| 232 | + cvo = _configDao.findByName("ldap.bind.password"); |
| 233 | + _configDao.update(cvo.getName(), cvo.getCategory(), getBindPassword()); |
| 234 | + |
| 235 | + cvo = _configDao.findByName("ldap.truststore"); |
| 236 | + _configDao.update(cvo.getName(), cvo.getCategory(), getTrustStore()); |
| 237 | + |
| 238 | + cvo = _configDao.findByName("ldap.truststore.password"); |
| 239 | + _configDao.update(cvo.getName(), cvo.getCategory(), getTrustStorePassword()); |
| 240 | + |
| 241 | + return true; |
| 242 | + } |
| 243 | + |
| 244 | + private List<? extends LdapConfigurationVO> listLDAPConfig() { |
| 245 | + |
| 246 | + LdapListConfigurationCmd listConfigurationCmd = new LdapListConfigurationCmd(_ldapManager); |
| 247 | + Pair<List<? extends LdapConfigurationVO>, Integer> result = _ldapManager.listConfigurations(listConfigurationCmd); |
| 248 | + return result.first(); |
| 249 | + } |
| 250 | + |
| 251 | + @Override |
| 252 | + public String getCommandName() { |
| 253 | + return s_name; |
| 254 | + } |
| 255 | + |
| 256 | + @Override |
| 257 | + public long getEntityOwnerId() { |
| 258 | + return Account.ACCOUNT_ID_SYSTEM; |
| 259 | + } |
| 260 | + |
| 261 | +} |
0 commit comments