Project

General

Profile

1
package edu.ucsb.nceas.metacat.dataone;
2

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

    
29
import java.math.BigInteger;
30
import java.sql.SQLException;
31
import java.util.ArrayList;
32
import java.util.Arrays;
33
import java.util.Date;
34
import java.util.HashMap;
35
import java.util.HashSet;
36
import java.util.List;
37
import java.util.Map;
38
import java.util.Set;
39
import java.util.concurrent.ExecutorService;
40
import java.util.concurrent.Executors;
41

    
42
import org.apache.log4j.Logger;
43
import org.dataone.client.CNode;
44
import org.dataone.client.D1Client;
45
import org.dataone.service.exceptions.InvalidRequest;
46
import org.dataone.service.exceptions.InvalidToken;
47
import org.dataone.service.exceptions.NotAuthorized;
48
import org.dataone.service.exceptions.NotFound;
49
import org.dataone.service.exceptions.NotImplemented;
50
import org.dataone.service.exceptions.ServiceFailure;
51
import org.dataone.service.exceptions.VersionMismatch;
52
import org.dataone.service.types.v1.AccessPolicy;
53
import org.dataone.service.types.v1.Identifier;
54
import org.dataone.service.types.v1.ObjectFormatIdentifier;
55
import org.dataone.service.types.v1.ObjectInfo;
56
import org.dataone.service.types.v1.ObjectList;
57
import org.dataone.service.types.v1.Permission;
58
import org.dataone.service.types.v1.Session;
59
import org.dataone.service.types.v1.Subject;
60
import org.dataone.service.types.v1.SystemMetadata;
61

    
62
import edu.ucsb.nceas.metacat.AccessionNumberException;
63
import edu.ucsb.nceas.metacat.IdentifierManager;
64
import edu.ucsb.nceas.metacat.McdbDocNotFoundException;
65
import edu.ucsb.nceas.metacat.accesscontrol.AccessControlException;
66
import edu.ucsb.nceas.metacat.admin.AdminException;
67
import edu.ucsb.nceas.metacat.properties.PropertyService;
68
import edu.ucsb.nceas.metacat.shared.ServiceException;
69
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
70
import edu.ucsb.nceas.utilities.SortedProperties;
71

    
72
public class SyncAccessPolicy {
73

    
74
	private static Logger logMetacat = Logger.getLogger(SyncAccessPolicy.class);
75

    
76
	/**
77
	 * Synchronize access policy (from system metadata) of d1 member node with
78
	 * the corresponding controlling node.
79
	 * 
80
	 * @param objList
81
	 *            list of d1 objects to be synced
82
	 * @return syncedIds a list of pids that were synced with the CN
83
	 * @throws ServiceFailure
84
	 * @throws InvalidToken
85
	 * @throws NotAuthorized
86
	 * @throws NotFound
87
	 * @throws NotImplemented
88
	 * @throws McdbDocNotFoundException
89
	 * @throws InvalidRequest
90
	 * @throws VersionMismatch
91
	 * @throws SQLException
92
	 * @throws AccessionNumberException
93
	 * @throws NumberFormatException
94
	 */
95
	private List<Identifier> sync(ObjectList objList) throws ServiceFailure,
96
			InvalidToken, NotAuthorized, NotFound, NotImplemented,
97
			McdbDocNotFoundException, InvalidRequest, VersionMismatch,
98
			NumberFormatException, AccessionNumberException, SQLException,
99
			Exception {
100

    
101
		AccessPolicy cnAccessPolicy = null;
102
		AccessPolicy mnAccessPolicy = null;
103
		Identifier pid = new Identifier();
104
		ObjectInfo objInfo = null;
105
		Session session = null;
106
		List<Identifier> syncedIds = new ArrayList<Identifier>();
107
		SystemMetadata cnSysMeta = null;
108
		SystemMetadata mnSysMeta = null;
109

    
110
		CNode cn = null;
111

    
112
		try {
113
			cn = D1Client.getCN();
114
			logMetacat.debug("Will sync access policies to CN id: "
115
					+ cn.getNodeId() + " with info: " + cn.toString());
116
		} catch (ServiceFailure sf) {
117
			logMetacat
118
					.error("Unable to get Coordinating node name for this MN");
119
			throw new AccessControlException(
120
					"Unable to get Coordinating node name for this MN");
121
		}
122

    
123
		for (int i = objList.getStart(); i < objList.getCount(); i++) {
124

    
125
			objInfo = objList.getObjectInfo(i);
126
			pid = objInfo.getIdentifier();
127

    
128
			logMetacat.debug("Getting SM for pid: " + pid.getValue() + " i: "
129
					+ i);
130
			try {
131
				// Get sm, access policy for requested localId
132
				mnSysMeta = IdentifierManager.getInstance().getSystemMetadata(
133
						pid.getValue());
134
			} catch (McdbDocNotFoundException e) {
135
				logMetacat.error("Error syncing access policy of pid: "
136
						+ pid.getValue() + " pid not found: " + e.getMessage());
137
				continue;
138
			} catch (Exception e) {
139
				logMetacat.error("Error syncing access policy of pid: "
140
						+ pid.getValue() + ". Message: " + e.getMessage());
141
				continue;
142
			}
143

    
144
			logMetacat
145
					.debug("Getting access policy for pid: " + pid.getValue());
146

    
147
			mnAccessPolicy = mnSysMeta.getAccessPolicy();
148

    
149
			// Get sm, access policy for requested pid from the CN
150
			try {
151
				cnSysMeta = cn.getSystemMetadata(pid);
152
			} catch (Exception e) {
153
				logMetacat.error("Error getting system metadata for pid: "
154
						+ pid.getValue() + " from cn: " + e.getMessage());
155
				continue;
156
			}
157
			logMetacat.debug("Getting access policy from CN for pid: "
158
					+ pid.getValue());
159
			cnAccessPolicy = cnSysMeta.getAccessPolicy();
160
			logMetacat.debug("Diffing access policies (MN,CN) for pid: "
161
					+ pid.getValue());
162

    
163
			// Compare access policies of MN and CN, and update if different.
164
			if (!isEqual(mnAccessPolicy, cnAccessPolicy)) {
165
				try {
166
					BigInteger serialVersion = cnSysMeta.getSerialVersion();
167
					logMetacat
168
							.debug("Requesting CN to set access policy for pid: "
169
									+ pid.getValue()
170
									+ ", serial version: "
171
									+ serialVersion.toString());
172
					cn.setAccessPolicy(session, pid, mnAccessPolicy,
173
							serialVersion.longValue());
174
					logMetacat.debug("Successfully set access policy");
175
					// Add this pid to the list of pids that were successfully
176
					// synced
177
					syncedIds.add(pid);
178
				} catch (NotAuthorized na) {
179
					logMetacat
180
							.error("Error syncing CN with access policy of pid: "
181
									+ pid.getValue()
182
									+ " user not authorized: "
183
									+ na.getMessage());
184
					// throw na;
185
					continue;
186
				} catch (ServiceFailure sf) {
187
					logMetacat
188
							.error("Error syncing CN with access policy of pid: "
189
									+ pid.getValue()
190
									+ " Service failure: "
191
									+ "'" + sf.getDescription() + "'");
192
					sf.printStackTrace();
193
					logMetacat.debug("Cause: " + "'" + sf.getCause() + "'");
194
					// throw sf;
195
					continue;
196
				} catch (Exception e) {
197
					logMetacat
198
							.error("Error syncing CN with access policy of pid: "
199
									+ pid.getValue() + e.getMessage());
200
					// throw e;
201
					continue;
202
				}
203
			} else {
204
				logMetacat.warn("Skipping pid: " + pid.getValue());
205
			}
206
			logMetacat.debug("Done syncing access policy for pid: "
207
					+ pid.getValue());
208
		}
209

    
210
		return syncedIds;
211
	}
212

    
213
	/**
214
	 * Convenience function that accepts a list of guids to sync
215
	 * 
216
	 * @param guidsToSync
217
	 *            list of guids to have access policy synced for
218
	 * @return syncedPids - list of pids that were actually synced with the CN
219
	 * @throws NumberFormatException
220
	 * @throws ServiceFailure
221
	 * @throws InvalidToken
222
	 * @throws NotAuthorized
223
	 * @throws NotFound
224
	 * @throws NotImplemented
225
	 * @throws McdbDocNotFoundException
226
	 * @throws InvalidRequest
227
	 * @throws VersionMismatch
228
	 * @throws AccessionNumberException
229
	 * @throws SQLException
230
	 */
231
	public List<Identifier> sync(List<String> guidsToSync)
232
			throws NumberFormatException, ServiceFailure, InvalidToken,
233
			NotAuthorized, NotFound, NotImplemented, McdbDocNotFoundException,
234
			InvalidRequest, VersionMismatch, AccessionNumberException,
235
			SQLException, Exception {
236
		List<Identifier> syncedPids = null;
237
		ObjectList objList = new ObjectList();
238
		SystemMetadata sm = new SystemMetadata();
239

    
240
		int start = 0;
241
		int count = 0; // guidsToSync.size();
242

    
243
		objList.setStart(start);
244

    
245
		// Convert the guids to d1 objects, as this is what
246
		// IdentifierManager.getInstance().querySystemMetadata returns in
247
		// syncAll, and
248
		// what sync(ObjectList...) expects
249
		for (String guid : guidsToSync) {
250
			try {
251
				sm = IdentifierManager.getInstance().getSystemMetadata(guid);
252
				count++;
253
			} catch (Exception e) {
254
				logMetacat.error("Error syncing access policy of pid: " + guid
255
						+ ". Message: " + e.getMessage());
256
				continue;
257
			}
258

    
259
			ObjectInfo oi = new ObjectInfo();
260
			Identifier id = new Identifier();
261
			id.setValue(guid);
262
			oi.setIdentifier(id);
263
			oi.setDateSysMetadataModified(sm.getDateSysMetadataModified());
264
			oi.setChecksum(sm.getChecksum());
265
			oi.setFormatId(sm.getFormatId());
266
			oi.setSize(sm.getSize());
267
			objList.addObjectInfo(oi);
268
		}
269

    
270
		int total = count;
271
		objList.setCount(count);
272
		objList.setTotal(total);
273

    
274
		syncedPids = sync(objList);
275
		return syncedPids;
276
	}
277

    
278
	/**
279
	 * For all guids for which current MN is authoritative, check that access
280
	 * policy is synced with CN.
281
	 * 
282
	 * @return void
283
	 */
284
	public void syncAll() throws ServiceFailure, InvalidToken, NotAuthorized,
285
			NotFound, NotImplemented, McdbDocNotFoundException, InvalidRequest,
286
			VersionMismatch, NumberFormatException, AccessionNumberException,
287
			SQLException, PropertyNotFoundException, ServiceException,
288
			Exception {
289

    
290
		SyncTask st = new SyncTask();
291
		// Create a single thread to run the sync of all guids in
292
		ExecutorService executor = Executors.newSingleThreadExecutor();
293
		logMetacat.debug("syncAll starting thread");
294
		executor.execute(st);
295
		// Only one task will run on this thread
296
		executor.shutdown();
297

    
298
		// return syncedIds;
299
	}
300

    
301
	/**
302
	 * Perform syncAll in a single thread.
303
	 * 
304
	 * @return void
305
	 */
306
	private class SyncTask implements Runnable {
307

    
308
		@Override
309
		public void run() {
310
			// For the following query parameters - null indicates that the
311
			// query
312
			// will not be
313
			// constrained by the parameter.
314
			Date startTime = null;
315
			Date endTime = null;
316
			ObjectFormatIdentifier objectFormatId = null;
317
			Boolean replicaStatus = false; // return only pids for which this mn
318
											// is
319

    
320
			ObjectList objsToSync = null;
321
			Integer count = 0;
322
			Integer start = 0;
323
			Integer total = 0;
324
			List<Identifier> tmpIds = null;
325
			List<Identifier> syncedIds = new ArrayList<Identifier>();
326

    
327
			try {
328
				count = Integer.valueOf(PropertyService
329
						.getProperty("database.webResultsetSize"));
330
			} catch (NumberFormatException e1) {
331
				logMetacat
332
						.error("Error in  propery file for format of database.webResultsetSize, will use 1000");
333
				e1.printStackTrace();
334
				count = 1000;
335
			} catch (PropertyNotFoundException e1) {
336
				logMetacat
337
						.error("Error reading propery file for database.webResultsetSize, will use 1000");
338
				e1.printStackTrace();
339
				count = 1000;
340
			}
341

    
342
			// Get the total count of guids before we start syncing
343
			try {
344
				objsToSync = IdentifierManager.getInstance()
345
						.querySystemMetadata(startTime, endTime,
346
								objectFormatId, replicaStatus, start, count);
347

    
348
				logMetacat.debug("syncTask total # of guids: "
349
						+ objsToSync.getTotal() + ", count for this page: "
350
						+ objsToSync.getCount());
351
			} catch (Exception e) {
352
				logMetacat.error("Error syncing ids");
353
			}
354

    
355
			total = objsToSync.getTotal();
356

    
357
			// The first loop might have fewer results than the requested count
358
			// value from the properties file,
359
			// so in this case use count returned from IdentiferManger for the
360
			// loop count/increment (loop will only execute once).
361
			if (objsToSync.getCount() < count)
362
				count = objsToSync.getCount();
363

    
364
			for (int i = 0; (i + count - 1) < total; i += count) {
365
				try {
366
					logMetacat.debug("syncTask # requested: " + count
367
							+ ", start: " + start + ", total: " + total
368
							+ ", count: " + objsToSync.getCount());
369
					tmpIds = sync(objsToSync);
370
					syncedIds.addAll(tmpIds);
371

    
372
					// Set start for the next db retrieval, loop interation
373
					start += objsToSync.getCount();
374
					if (start >= total)
375
						break;
376
					objsToSync = IdentifierManager
377
							.getInstance()
378
							.querySystemMetadata(startTime, endTime,
379
									objectFormatId, replicaStatus, start, count);
380
				} catch (Exception e) {
381
					logMetacat.error("Error syncing ids");
382
					break;
383
				}
384
			}
385
			logMetacat
386
					.debug("syncTask thread completed. Number of guids synced: "
387
							+ syncedIds.size());
388
		}
389
        //PropertyService.setProperty("dataone.systemmetadata.generated", Boolean.TRUE.toString());
390

    
391
	}
392

    
393
	/**
394
	 * Compare two d1 system metadata access policies for equivalence.
395
	 * 
396
	 * @param ap1
397
	 *            - first access policy in the comparison
398
	 * @param ap2
399
	 *            - second access policy in the comparison
400
	 * @return boolean - true if access policies are equivalent
401
	 */
402
	private boolean isEqual(AccessPolicy ap1, AccessPolicy ap2) {
403

    
404
		// Access Policy -> Access Rule -> (Subject, Permission)
405
		// i.e. Subject="slaughter", Permission="read,write,changePermission"
406
		// Get the list of access rules for each access policy
407
		List<org.dataone.service.types.v1.AccessRule> allowList1 = ap1
408
				.getAllowList();
409
		List<org.dataone.service.types.v1.AccessRule> allowList2 = ap2
410
				.getAllowList();
411

    
412
		HashMap<Subject, Set<Permission>> userPerms1 = new HashMap<Subject, Set<Permission>>();
413
		HashMap<Subject, Set<Permission>> userPerms2 = new HashMap<Subject, Set<Permission>>();
414

    
415
		// Load the permissions from the access rules into a hash of sets, i.e.,
416
		// so that we end up with this:
417
		// hash key: set of permissions, i.e.
418
		// ----------------------------
419
		// user1: read, write
420
		// user2: read
421
		// user3: read, write, change permissions
422
		// With the permissions in this structure, they can be easily compared
423
		Set<Permission> perms = null;
424
		// Process first access policy
425
		// Loop through access rules of this allowList
426
		for (org.dataone.service.types.v1.AccessRule accessRule : allowList1) {
427
			for (Subject s : accessRule.getSubjectList()) {
428
				if (userPerms1.containsKey(s)) {
429
					perms = userPerms1.get(s);
430
				} else {
431
					perms = new HashSet<Permission>();
432
				}
433
				for (Permission p : accessRule.getPermissionList()) {
434
					perms.add(p);
435
				}
436
				userPerms1.put(s, perms);
437
			}
438
		}
439

    
440
		// Process second access policy
441
		for (org.dataone.service.types.v1.AccessRule accessRule : allowList2) {
442
			for (Subject s : accessRule.getSubjectList()) {
443
				if (userPerms2.containsKey(s)) {
444
					perms = userPerms2.get(s);
445
				} else {
446
					perms = new HashSet<Permission>();
447
				}
448
				for (Permission p : accessRule.getPermissionList()) {
449
					perms.add(p);
450
				}
451
				userPerms2.put(s, perms);
452
			}
453
		}
454

    
455
		// Check if the number of access rules is the same for mn and cn. If not
456
		// then consider them not equal, without performing diff of each access
457
		// rule.
458
		if (userPerms1.entrySet().size() != userPerms2.entrySet().size())
459
			return false;
460

    
461
		// Now perform the comparison of each access rule of access policy 1 to
462
		// ap 2.
463
		// This test assumes that the mn perms are more complete than the cn
464
		// perms.
465
		logMetacat.debug("Performing comparison of access policies");
466
		for (Map.Entry<Subject, Set<Permission>> entry : userPerms1.entrySet()) {
467
			// User name
468
			Subject s1 = entry.getKey();
469
			// Perms that the user holds
470
			Set<Permission> p1 = entry.getValue();
471
			logMetacat
472
					.debug("Checking access policy of user: " + s1.getValue());
473

    
474
			// Does this user exist in both access policies?
475
			if (userPerms2.containsKey(s1)) {
476
				if (!p1.equals(userPerms2.get(s1))) {
477
					logMetacat.debug("User access policies not equal");
478
					return false;
479
				}
480
			} else {
481
				logMetacat.debug("User access policy not found on CN");
482
				return false;
483
			}
484
		}
485

    
486
		// All comparisons have been passed, so the two access policies are
487
		// equivalent
488
		logMetacat.debug("Access policies are the same");
489
		return true;
490
	}
491

    
492
	/**
493
	 * Run pid synch script on the given pids Each argument is an individual pid
494
	 * because pids cannot contain whitespace.
495
	 * 
496
	 * @param args
497
	 * @throws Exception
498
	 */
499
	public static void main(String[] args) throws Exception {
500

    
501
		// set up the properties based on the test/deployed configuration of the
502
		// workspace
503
		SortedProperties testProperties = new SortedProperties(
504
				"test/test.properties");
505
		testProperties.load();
506
		String metacatContextDir = testProperties
507
				.getProperty("metacat.contextDir");
508
		PropertyService.getInstance(metacatContextDir + "/WEB-INF");
509

    
510
		ArrayList<String> guids = null;
511
		SyncAccessPolicy syncAP = new SyncAccessPolicy();
512

    
513
		if (args.length > 0) {
514
			try {
515
				guids = new ArrayList<String>(Arrays.asList(args));
516
				logMetacat.warn("Trying to syncing access policy for "
517
						+ args.length + " pids");
518
				List<Identifier> synchedPids = syncAP.sync(guids);
519
				logMetacat.warn("Sunk access policies for "
520
						+ synchedPids.size() + " pids");
521
			} catch (Exception e) {
522
				logMetacat.error(
523
						"Error syncing pids, message: " + e.getMessage(), e);
524
				System.exit(1);
525
			}
526
		}
527
	}
528
}
(6-6/7)