Project

General

Profile

1
/**
2
 *  '$RCSfile$'
3
 *    Purpose: A Class that implements ldap configuration methods
4
 *  Copyright: 2008 Regents of the University of California and the
5
 *             National Center for Ecological Analysis and Synthesis
6
 *    Authors: Michael Daigle
7
 *
8
 *   '$Author: daigle $'
9
 *     '$Date: 2009-10-06 10:55:18 -0700 (Tue, 06 Oct 2009) $'
10
 * '$Revision: 5076 $'
11
 *
12
 * This program is free software; you can redistribute it and/or modify
13
 * it under the terms of the GNU General Public License as published by
14
 * the Free Software Foundation; either version 2 of the License, or
15
 * (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU General Public License
23
 * along with this program; if not, write to the Free Software
24
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
25
 */
26

    
27
package edu.ucsb.nceas.metacat.admin;
28

    
29
import java.net.ConnectException;
30
import java.util.Set;
31
import java.util.SortedMap;
32
import java.util.Vector;
33

    
34
import javax.servlet.http.HttpServletRequest;
35
import javax.servlet.http.HttpServletResponse;
36

    
37
import org.apache.log4j.Logger;
38

    
39
import edu.ucsb.nceas.metacat.AuthSession;
40
import edu.ucsb.nceas.metacat.properties.PropertyService;
41
import edu.ucsb.nceas.metacat.shared.MetacatUtilException;
42
import edu.ucsb.nceas.metacat.util.RequestUtil;
43
import edu.ucsb.nceas.utilities.GeneralPropertyException;
44
import edu.ucsb.nceas.utilities.MetaDataProperty;
45
import edu.ucsb.nceas.utilities.PropertiesMetaData;
46
import edu.ucsb.nceas.utilities.SortedProperties;
47
import edu.ucsb.nceas.utilities.StringUtil;
48

    
49
/**
50
 * Control the display of the Authentication configuration page and the processing
51
 * of the configuration values.
52
 */
53
public class AuthAdmin extends MetacatAdmin {
54

    
55
	private static AuthAdmin authAdmin = null;
56
	private static Logger logMetacat = Logger.getLogger(AuthAdmin.class);
57

    
58
	/**
59
	 * private constructor since this is a singleton
60
	 */
61
	private AuthAdmin() {}
62

    
63
	/**
64
	 * Get the single instance of the MetaCatConfig.
65
	 * 
66
	 * @return the single instance of MetaCatConfig
67
	 */
68
	public static AuthAdmin getInstance() {
69
		if (authAdmin == null) {
70
			authAdmin = new AuthAdmin();
71
		}
72
		return authAdmin;
73
	}
74
	
75
	/**
76
	 * Handle configuration of the Authentication properties
77
	 * 
78
	 * @param request
79
	 *            the http request information
80
	 * @param response
81
	 *            the http response to be sent back to the client
82
	 */
83
	public void configureAuth(HttpServletRequest request,
84
			HttpServletResponse response) throws AdminException {
85

    
86
		String processForm = request.getParameter("processForm");
87
		String formErrors = (String) request.getAttribute("formErrors");
88

    
89
		if (processForm == null || !processForm.equals("true") || formErrors != null) {
90
			// The servlet configuration parameters have not been set, or there
91
			// were form errors on the last attempt to configure, so redirect to
92
			// the web form for configuring metacat
93
			
94
			try {
95
				// Load the properties metadata file so that the JSP page can
96
				// use the metadata to construct the editing form
97
				PropertiesMetaData metadata = PropertyService.getAuthMetaData();
98
				request.setAttribute("metadata", metadata);
99
				request.setAttribute("groupMap", metadata.getGroups());
100

    
101
				// add the list of auth options and their values to the request
102
				Vector<String> propertyNames = PropertyService.getPropertyNamesByGroup("auth");
103
				for (String name : propertyNames) {
104
					request.setAttribute(name, PropertyService.getProperty(name));
105
				} 
106
				
107
				// add the list of organization options and their values to the request.  This is because
108
				// currently we use the organization.unaffiliated values to configure metacat client for 
109
				// password change and account creation.  Eventually, these should get moved to organization
110
				// level configuration.
111
				Vector<String> orgPropertyNames = PropertyService.getPropertyNamesByGroup("organization");
112
				for (String name : orgPropertyNames) {
113
					request.setAttribute(name, PropertyService.getProperty(name));
114
				} 
115

    
116
				// Check for any backup properties and apply them to the request.
117
				// These are properties from previous configurations. They keep
118
				// the user from having to re-enter all values when upgrading.
119
				// If this is a first time install, getBackupProperties will return
120
				// null.
121
				SortedProperties backupProperties = PropertyService.getAuthBackupProperties();
122
				if (backupProperties != null) {
123
					Vector<String> backupKeys = backupProperties.getPropertyNames();
124
					for (String key : backupKeys) {
125
						String value = backupProperties.getProperty(key);
126
						if (value != null) {
127
							request.setAttribute(key, value);
128
						}
129
					}
130
				}
131
				// Forward the request to the JSP page
132
				RequestUtil.forwardRequest(request, response,
133
						"/admin/auth-configuration.jsp", null);
134
			} catch (GeneralPropertyException gpe) {
135
				throw new AdminException("AuthAdmin.configureAuth - Problem getting property " + 
136
						"while initializing LDAP properties page: " + gpe.getMessage());
137
			} catch (MetacatUtilException mue) {
138
				throw new AdminException("AuthAdmin.configureAuth - Utility problem while initializing "
139
						+ "LDAP properties page:" + mue.getMessage());
140
			} 
141
		} else {
142
			// The configuration form is being submitted and needs to be
143
			// processed.
144
			Vector<String> processingSuccess = new Vector<String>();
145
			Vector<String> processingErrors = new Vector<String>();
146
			Vector<String> validationErrors = new Vector<String>();
147

    
148
			try {
149
				// For each property, check if it is changed and save it
150
				PropertiesMetaData authMetaData = PropertyService
151
						.getAuthMetaData();
152

    
153
				// process the fields for the global options (group 1)
154
				SortedMap<Integer, MetaDataProperty> globalPropertyMap = authMetaData
155
						.getPropertiesInGroup(1);
156
				Set<Integer> globalPropertyIndexes = globalPropertyMap.keySet();
157
				for (Integer globalPropertyIndex : globalPropertyIndexes) {
158
					String globalPropertyKey = globalPropertyMap.get(
159
							globalPropertyIndex).getKey();
160
					PropertyService.checkAndSetProperty(request,
161
							globalPropertyKey);
162
				}
163
				
164
				// process the fields for the global options (group 1)
165
				SortedMap<Integer, MetaDataProperty> authClientPropertyMap = authMetaData
166
						.getPropertiesInGroup(2);
167
				Set<Integer> authClientPropertyIndexes = authClientPropertyMap.keySet();
168
				for (Integer authClientPropertyIndex : authClientPropertyIndexes) {
169
					String authClientPropertyKey = authClientPropertyMap.get(
170
							authClientPropertyIndex).getKey();
171
					PropertyService.checkAndSetProperty(request,
172
							authClientPropertyKey);
173
				}
174

    
175
				// we need to write the options from memory to the properties
176
				// file
177
				PropertyService.persistProperties();
178

    
179
				// Validate that the options provided are legitimate. Note that
180
				// we've allowed them to persist their entries. As of this point
181
				// there is no other easy way to go back to the configure form
182
				// and preserve their entries.
183
				validationErrors.addAll(validateOptions(request));
184

    
185

    
186
				// Write out the configurable properties to a backup file
187
				// outside the install directory.  Note that we allow them to
188
				// do this even if they have validation errors.  They will
189
				// need to go back and fix the errors before they can run metacat.
190
				
191
				// This is a special case, since it is possible that the backup directory
192
				// may not have been specified yet.  If not, authentication values need to be
193
				// persisted by the BackupAdmin when the backup directory is specified.
194
				String backupDir = PropertyService.getProperty("application.backupDir");
195
				if (backupDir != null) {
196
					PropertyService.persistAuthBackupProperties(request.getSession()
197
						.getServletContext());
198
				}
199
			
200
			} catch (GeneralPropertyException gpe) {
201
				String errorMessage = "AuthAdmin.configureAuth - Problem getting or setting property while "
202
					+ "processing LDAP properties page: " + gpe.getMessage();
203
				logMetacat.error(errorMessage);
204
				processingErrors.add(errorMessage);
205
			} 
206
			
207
			try {
208
				if (validationErrors.size() > 0 || processingErrors.size() > 0) {
209
					RequestUtil.clearRequestMessages(request);
210
					RequestUtil.setRequestFormErrors(request, validationErrors);
211
					RequestUtil.setRequestErrors(request, processingErrors);
212
					RequestUtil.forwardRequest(request, response, "/admin", null);
213
				} else {
214
					// Now that the options have been set, change the
215
					// 'authConfigured' option to 'true'
216
					PropertyService.setProperty("configutil.authConfigured",
217
							PropertyService.CONFIGURED);
218
					
219
					// Reload the main metacat configuration page
220
					processingSuccess.add("Authentication successfully configured");
221
					RequestUtil.clearRequestMessages(request);
222
					RequestUtil.setRequestSuccess(request, processingSuccess);
223
					RequestUtil.forwardRequest(request, response,
224
							"/admin?configureType=configure&processForm=false", null);
225
				}
226
			} catch (MetacatUtilException mue) {
227
				throw new AdminException("AuthAdmin.configureAuth - utility problem forwarding request while "
228
						+ "processing LDAP properties page: " + mue.getMessage());
229
			} catch (GeneralPropertyException gpe) {
230
				String errorMessage = "AuthAdmin.configureAuth - Problem getting or setting property while "
231
					+ "processing Authentication properties page: " + gpe.getMessage();
232
				logMetacat.error(errorMessage);
233
				processingErrors.add(errorMessage);
234
			}
235
		}
236
	}
237
	
238
	/**
239
	 * Validate the most important configuration options submitted by the user.
240
	 * 
241
	 * @return a vector holding error message for any fields that fail
242
	 *         validation.
243
	 */
244
	protected Vector<String> validateOptions(HttpServletRequest request) {
245
		Vector<String> errorVector = new Vector<String>();
246

    
247
		String adminUsers = request.getParameter("auth.administrators");
248
		Vector<String> adminUserList = StringUtil.toVector(adminUsers, ':');
249

    
250
		try {
251
			AuthSession authSession = new AuthSession();
252
			for (String adminUser : adminUserList) {
253
				try {
254
					authSession.getAttributes(adminUser);
255
				} catch (ConnectException ce) {
256
					if (ce.getMessage() != null
257
							&& ce.getMessage().contains("NameNotFoundException")) {
258
						errorVector.add("User : " + adminUser + " is not in LDAP.");
259
					} else {
260
						errorVector.add("Connection error while verifying Metacat " + 
261
								"Administrators : " + ce.getMessage());
262
					}
263
				}
264
			}
265
		} catch (InstantiationException ie) {
266
			errorVector.add("AuthAdmin.validateOptions - Instantiation error while verifying Metacat Administrators : "
267
							+ ie.getMessage());
268
		} catch (Exception e) {
269
			errorVector.add("AuthAdmin.validateOptions - Error while verifying Metacat Administrators : "
270
					+ e.getMessage());
271
		}
272

    
273
		return errorVector;
274
	}
275
}
(2-2/10)