Project

General

Profile

1 4080 daigle
/**
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$'
9
 *     '$Date$'
10
 * '$Revision$'
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.io.IOException;
30 4547 daigle
import java.net.ConnectException;
31 4080 daigle
import java.util.Set;
32
import java.util.SortedMap;
33
import java.util.Vector;
34
35
import javax.servlet.ServletException;
36
import javax.servlet.http.HttpServletRequest;
37
import javax.servlet.http.HttpServletResponse;
38
39
import org.apache.log4j.Logger;
40
41 4590 daigle
import edu.ucsb.nceas.metacat.AuthSession;
42 4080 daigle
import edu.ucsb.nceas.metacat.service.PropertyService;
43
import edu.ucsb.nceas.metacat.util.RequestUtil;
44
import edu.ucsb.nceas.utilities.GeneralPropertyException;
45
import edu.ucsb.nceas.utilities.MetaDataProperty;
46
import edu.ucsb.nceas.utilities.PropertiesMetaData;
47
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
48
import edu.ucsb.nceas.utilities.SortedProperties;
49 4547 daigle
import edu.ucsb.nceas.utilities.StringUtil;
50 4080 daigle
51
/**
52 4590 daigle
 * Control the display of the Authentication configuration page and the processing
53 4080 daigle
 * of the configuration values.
54
 */
55 4590 daigle
public class AuthAdmin extends MetaCatAdmin {
56 4080 daigle
57 4590 daigle
	private static AuthAdmin authAdmin = null;
58
	private static Logger logMetacat = Logger.getLogger(AuthAdmin.class);
59 4080 daigle
60
	/**
61
	 * private constructor since this is a singleton
62
	 */
63 4590 daigle
	private AuthAdmin() {}
64 4080 daigle
65
	/**
66
	 * Get the single instance of the MetaCatConfig.
67
	 *
68
	 * @return the single instance of MetaCatConfig
69
	 */
70 4590 daigle
	public static AuthAdmin getInstance() {
71
		if (authAdmin == null) {
72
			authAdmin = new AuthAdmin();
73 4080 daigle
		}
74 4590 daigle
		return authAdmin;
75 4080 daigle
	}
76
77
	/**
78 4590 daigle
	 * Handle configuration of the Authentication properties
79 4080 daigle
	 *
80
	 * @param request
81
	 *            the http request information
82
	 * @param response
83
	 *            the http response to be sent back to the client
84
	 */
85 4590 daigle
	public void configureAuth(HttpServletRequest request,
86 4080 daigle
			HttpServletResponse response) throws AdminException {
87
88
		String processForm = request.getParameter("processForm");
89
		String formErrors = (String) request.getAttribute("formErrors");
90
91
		if (processForm == null || !processForm.equals("true") || formErrors != null) {
92
			// The servlet configuration parameters have not been set, or there
93
			// were form errors on the last attempt to configure, so redirect to
94
			// the web form for configuring metacat
95
96
			try {
97
				// Load the properties metadata file so that the JSP page can
98 4159 daigle
				// use the metadata to construct the editing form
99 4590 daigle
				PropertiesMetaData metadata = PropertyService.getAuthMetaData();
100 4080 daigle
				request.setAttribute("metadata", metadata);
101
				request.setAttribute("groupMap", metadata.getGroups());
102
103 4590 daigle
				// add the list of auth options and their values to the request
104
				Vector<String> propertyNames = PropertyService.getPropertyNamesByGroup("auth");
105 4080 daigle
				for (String name : propertyNames) {
106
					request.setAttribute(name, PropertyService.getProperty(name));
107
				}
108
109
				// Check for any backup properties and apply them to the request.
110
				// These are properties from previous configurations. They keep
111
				// the user from having to re-enter all values when upgrading.
112
				// If this is a first time install, getBackupProperties will return
113
				// null.
114 4590 daigle
				SortedProperties backupProperties = PropertyService.getAuthBackupProperties();
115 4080 daigle
				if (backupProperties != null) {
116
					Vector<String> backupKeys = backupProperties.getPropertyNames();
117
					for (String key : backupKeys) {
118
						String value = backupProperties.getProperty(key);
119
						if (value != null) {
120
							request.setAttribute(key, value);
121
						}
122
					}
123
				}
124
				// Forward the request to the JSP page
125
				RequestUtil.forwardRequest(request, response,
126 4590 daigle
						"/admin/auth-configuration.jsp");
127 4080 daigle
			} catch (PropertyNotFoundException pnfe) {
128
				throw new AdminException("Problem getting property while initializing "
129
						+ "LDAP properties page: " + pnfe.getMessage());
130
			} catch (IOException ioe) {
131
				throw new AdminException("IO problem while initializing "
132
						+ "LDAP properties page:" + ioe.getMessage());
133
			} catch (ServletException se) {
134
				throw new AdminException("problem forwarding request while "
135
						+ "initializing LDAP properties page: " + se.getMessage());
136
			}
137
		} else {
138
			// The configuration form is being submitted and needs to be
139
			// processed.
140
			Vector<String> processingSuccess = new Vector<String>();
141
			Vector<String> processingErrors = new Vector<String>();
142
			Vector<String> validationErrors = new Vector<String>();
143
144
			try {
145
				// For each property, check if it is changed and save it
146 4590 daigle
				PropertiesMetaData authMetaData = PropertyService
147
						.getAuthMetaData();
148 4080 daigle
149
				// process the fields for the global options (group 1)
150 4590 daigle
				SortedMap<Integer, MetaDataProperty> globalPropertyMap = authMetaData
151 4080 daigle
						.getPropertiesInGroup(1);
152
				Set<Integer> globalPropertyIndexes = globalPropertyMap.keySet();
153
				for (Integer globalPropertyIndex : globalPropertyIndexes) {
154
					String globalPropertyKey = globalPropertyMap.get(
155
							globalPropertyIndex).getKey();
156
					PropertyService.checkAndSetProperty(request,
157
							globalPropertyKey);
158
				}
159
160
				// we need to write the options from memory to the properties
161
				// file
162
				PropertyService.persistProperties();
163
164
				// Validate that the options provided are legitimate. Note that
165
				// we've allowed them to persist their entries. As of this point
166
				// there is no other easy way to go back to the configure form
167
				// and preserve their entries.
168
				validationErrors.addAll(validateOptions(request));
169
170
171
				// Write out the configurable properties to a backup file
172
				// outside the install directory.  Note that we allow them to
173
				// do this even if they have validation errors.  They will
174
				// need to go back and fix the errors before they can run metacat.
175 4705 daigle
176
				// This is a special case, since it is possible that the backup directory
177
				// may not have been specified yet.  If not, authentication values need to be
178
				// persisted by the BackupAdmin when the backup directory is specified.
179
				String backupDir = PropertyService.getProperty("application.backupDir");
180
				if (backupDir != null) {
181
					PropertyService.persistAuthBackupProperties(request.getSession()
182 4080 daigle
						.getServletContext());
183 4705 daigle
				}
184 4080 daigle
185
			} catch (GeneralPropertyException gpe) {
186
				String errorMessage = "Problem getting or setting property while "
187
					+ "processing LDAP properties page: " + gpe.getMessage();
188
				logMetacat.error(errorMessage);
189
				processingErrors.add(errorMessage);
190 4159 daigle
			}
191 4080 daigle
192
			try {
193
				if (validationErrors.size() > 0 || processingErrors.size() > 0) {
194
					RequestUtil.clearRequestMessages(request);
195
					RequestUtil.setRequestFormErrors(request, validationErrors);
196
					RequestUtil.setRequestErrors(request, processingErrors);
197
					RequestUtil.forwardRequest(request, response, "/admin");
198
				} else {
199
					// Now that the options have been set, change the
200 4590 daigle
					// 'authConfigured' option to 'true'
201
					PropertyService.setProperty("configutil.authConfigured",
202 4080 daigle
							PropertyService.CONFIGURED);
203
204
					// Reload the main metacat configuration page
205 4590 daigle
					processingSuccess.add("Authentication successfully configured");
206 4080 daigle
					RequestUtil.clearRequestMessages(request);
207
					RequestUtil.setRequestSuccess(request, processingSuccess);
208
					RequestUtil.forwardRequest(request, response,
209
							"/admin?configureType=configure&processForm=false");
210
				}
211
			} catch (ServletException se) {
212
				throw new AdminException("problem forwarding request while "
213
						+ "processing LDAP properties page: " + se.getMessage());
214
			} catch (IOException ioe) {
215 4590 daigle
				throw new AdminException("IO problem while processing Authentication "
216 4080 daigle
						+ "properties page: " + ioe.getMessage());
217 4084 daigle
			} catch (GeneralPropertyException gpe) {
218
				String errorMessage = "Problem getting or setting property while "
219 4590 daigle
					+ "processing Authentication properties page: " + gpe.getMessage();
220 4084 daigle
				logMetacat.error(errorMessage);
221
				processingErrors.add(errorMessage);
222 4080 daigle
			}
223
		}
224
	}
225
226
	/**
227
	 * Validate the most important configuration options submitted by the user.
228
	 *
229
	 * @return a vector holding error message for any fields that fail
230
	 *         validation.
231
	 */
232
	protected Vector<String> validateOptions(HttpServletRequest request) {
233
		Vector<String> errorVector = new Vector<String>();
234
235 4590 daigle
		String adminUsers = request.getParameter("auth.administrators");
236 4547 daigle
		Vector<String> adminUserList = StringUtil.toVector(adminUsers, ':');
237 4080 daigle
238 4547 daigle
		try {
239 4590 daigle
			AuthSession authSession = new AuthSession();
240 4547 daigle
			for (String adminUser : adminUserList) {
241
				try {
242 4590 daigle
					authSession.getAttributes(adminUser);
243 4547 daigle
				} catch (ConnectException ce) {
244
					if (ce.getMessage() != null
245
							&& ce.getMessage().contains("NameNotFoundException")) {
246
						errorVector.add("User : " + adminUser + " is not in LDAP.");
247
					} else {
248
						errorVector.add("Connection error while verifying Metacat " +
249
								"Administrators : " + ce.getMessage());
250
					}
251
				}
252
			}
253
		} catch (InstantiationException ie) {
254 4590 daigle
			errorVector.add("Instantiation error while verifying Metacat Administrators : "
255 4547 daigle
							+ ie.getMessage());
256 4590 daigle
		} catch (Exception e) {
257
			errorVector.add("Error while verifying Metacat Administrators : "
258
					+ e.getMessage());
259 4547 daigle
		}
260
261 4080 daigle
		return errorVector;
262
	}
263
}