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: tao $'
11
 *     '$Date: 2015-07-15 11:07:27 -0700 (Wed, 15 Jul 2015) $'
12
 * '$Revision: 9249 $'
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.v2.CNode;
44
import org.dataone.client.v2.itk.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.AccessRule;
54
import org.dataone.service.types.v1.Identifier;
55
import org.dataone.service.types.v1.NodeReference;
56
import org.dataone.service.types.v1.ObjectFormatIdentifier;
57
import org.dataone.service.types.v1.ObjectInfo;
58
import org.dataone.service.types.v1.ObjectList;
59
import org.dataone.service.types.v1.Permission;
60
import org.dataone.service.types.v1.Session;
61
import org.dataone.service.types.v1.Subject;
62
import org.dataone.service.types.v2.SystemMetadata;
63

    
64
import edu.ucsb.nceas.metacat.AccessionNumberException;
65
import edu.ucsb.nceas.metacat.IdentifierManager;
66
import edu.ucsb.nceas.metacat.McdbDocNotFoundException;
67
import edu.ucsb.nceas.metacat.accesscontrol.AccessControlException;
68
import edu.ucsb.nceas.metacat.properties.PropertyService;
69
import edu.ucsb.nceas.metacat.shared.ServiceException;
70
import edu.ucsb.nceas.utilities.GeneralPropertyException;
71
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
72
import edu.ucsb.nceas.utilities.SortedProperties;
73

    
74
public class SyncAccessPolicy {
75

    
76
	private static Logger logMetacat = Logger.getLogger(SyncAccessPolicy.class);
77

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

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

    
112
		CNode cn = null;
113

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

    
125
		for (int i = objList.getStart(); i < objList.getCount(); i++) {
126

    
127
			objInfo = objList.getObjectInfo(i);
128
			pid = objInfo.getIdentifier();
129

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

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

    
149
			mnAccessPolicy = mnSysMeta.getAccessPolicy();
150

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

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

    
212
		return syncedIds;
213
	}
214

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

    
242
		int start = 0;
243
		int count = 0; // guidsToSync.size();
244

    
245
		objList.setStart(start);
246

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

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

    
272
		int total = count;
273
		objList.setCount(count);
274
		objList.setTotal(total);
275

    
276
		syncedPids = sync(objList);
277
		return syncedPids;
278
	}
279

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

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

    
300
		// return syncedIds;
301
	}
302

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

    
310
		@Override
311
		public void run() {
312
			
313
			// For the following query parameters - null indicates that the
314
			// query
315
			// will not be
316
			// constrained by the parameter.
317
			Date startTime = null;
318
			Date endTime = null;
319
			ObjectFormatIdentifier objectFormatId = null;
320
			//Boolean replicaStatus = false; // return only pids for which this mn
321
			NodeReference thisMN = new NodeReference();
322
			try {
323
			    String currentNodeId = PropertyService.getInstance().getProperty("dataone.nodeId"); // return only pids for which this mn
324
			    thisMN.setValue(currentNodeId);
325
			} catch (Exception e) {
326
			    logMetacat.error("SyncAccessPolicy.run - can't get the node id of this member node from the metacat property file since :"+e.getMessage());
327
			    return;
328
			}
329
							// is
330
			ObjectList objsToSync = null;
331
			Integer count = 0;
332
			Integer start = 0;
333
			Integer total = 0;
334
			List<Identifier> tmpIds = null;
335
			// If even one sync error encounted, don't set property that will disable
336
			// "syncAll" button in admin/replication web page.
337
			boolean syncError = false;
338
			List<Identifier> syncedIds = new ArrayList<Identifier>();
339
						
340
			try {
341
				count = Integer.valueOf(PropertyService
342
						.getProperty("database.webResultsetSize"));
343
			} catch (NumberFormatException e1) {
344
				logMetacat
345
						.error("Error in  propery file for format of database.webResultsetSize, will use 1000");
346
				e1.printStackTrace();
347
				count = 1000;
348
			} catch (PropertyNotFoundException e1) {
349
				logMetacat
350
						.error("Error reading propery file for database.webResultsetSize, will use 1000");
351
				e1.printStackTrace();
352
				count = 1000;
353
			}
354

    
355
			// Get the total count of guids before we start syncing
356
			Identifier id = null;
357
            boolean isSid = false;
358
			try {
359
			    
360
				objsToSync = IdentifierManager.getInstance()
361
						.querySystemMetadata(startTime, endTime,
362
								objectFormatId, thisMN, start, count, id, isSid);
363

    
364
				logMetacat.debug("syncTask total # of guids: "
365
						+ objsToSync.getTotal() + ", count for this page: "
366
						+ objsToSync.getCount());
367
			} catch (Exception e) {
368
				logMetacat.error("Error syncing ids");
369
			}
370

    
371
			total = objsToSync.getTotal();
372

    
373
			// The first loop might have fewer results than the requested count
374
			// value from the properties file,
375
			// so in this case use count returned from IdentiferManger for the
376
			// loop count/increment (loop will only execute once).
377
			if (objsToSync.getCount() < count)
378
				count = objsToSync.getCount();
379

    
380
			for (int i = 0; (i + count - 1) < total; i += count) {
381
				try {
382
					logMetacat.debug("syncTask # requested: " + count
383
							+ ", start: " + start + ", total: " + total
384
							+ ", count: " + objsToSync.getCount());
385
					tmpIds = sync(objsToSync);
386
					syncedIds.addAll(tmpIds);
387

    
388
					// Set start for the next db retrieval, loop interation
389
					start += objsToSync.getCount();
390
					if (start >= total)
391
						break;
392
					objsToSync = IdentifierManager
393
							.getInstance()
394
							.querySystemMetadata(startTime, endTime,
395
									objectFormatId, thisMN, start, count, id, isSid);
396
				} catch (Exception e) {
397
					logMetacat.error("Error syncing ids");
398
					syncError = true;
399
					break;
400
				}
401
			}
402
			logMetacat
403
					.debug("syncTask thread completed. Number of guids synced: "
404
							+ syncedIds.size());
405
			if (!syncError) {
406
				try {
407
					PropertyService.setProperty(
408
							"dataone.syncaccesspolicies.synced",
409
							Boolean.TRUE.toString());
410
				} catch (GeneralPropertyException e) {
411
					logMetacat
412
							.error("Unable to update property dataone.syncaccesspolicies.synced=true");
413
				}
414
			}
415
		}
416
	}
417

    
418
	/**
419
	 * Compare two d1 system metadata access policies for equivalence.
420
	 * 
421
	 * @param ap1
422
	 *            - first access policy in the comparison
423
	 * @param ap2
424
	 *            - second access policy in the comparison
425
	 * @return boolean - true if access policies are equivalent
426
	 */
427
	public boolean isEqual(AccessPolicy ap1, AccessPolicy ap2) {
428

    
429
		// can't check when either is null
430
		if (ap1 == null || ap2 == null) {
431
			return false;
432
		}
433
		
434
		// Access Policy -> Access Rule -> (Subject, Permission)
435
		// i.e. Subject="slaughter", Permission="read,write,changePermission"
436
		// Get the list of access rules for each access policy
437
		List<AccessRule> allowList1 = ap1
438
				.getAllowList();
439
		List<AccessRule> allowList2 = ap2
440
				.getAllowList();
441

    
442
		HashMap<Subject, Set<Permission>> userPerms1 = new HashMap<Subject, Set<Permission>>();
443
		HashMap<Subject, Set<Permission>> userPerms2 = new HashMap<Subject, Set<Permission>>();
444

    
445
		// Load the permissions from the access rules into a hash of sets, i.e.,
446
		// so that we end up with this:
447
		// hash key: set of permissions, i.e.
448
		// ----------------------------
449
		// user1: read, write
450
		// user2: read
451
		// user3: read, write, change permissions
452
		// With the permissions in this structure, they can be easily compared
453
		Set<Permission> perms = null;
454
		// Process first access policy
455
		// Loop through access rules of this allowList
456
		for (AccessRule accessRule : allowList1) {
457
			for (Subject s : accessRule.getSubjectList()) {
458
				if (userPerms1.containsKey(s)) {
459
					perms = userPerms1.get(s);
460
				} else {
461
					perms = new HashSet<Permission>();
462
				}
463
				for (Permission p : accessRule.getPermissionList()) {
464
					perms.add(p);
465
				}
466
				userPerms1.put(s, perms);
467
			}
468
		}
469

    
470
		// Process second access policy
471
		for (AccessRule accessRule : allowList2) {
472
			for (Subject s : accessRule.getSubjectList()) {
473
				if (userPerms2.containsKey(s)) {
474
					perms = userPerms2.get(s);
475
				} else {
476
					perms = new HashSet<Permission>();
477
				}
478
				for (Permission p : accessRule.getPermissionList()) {
479
					perms.add(p);
480
				}
481
				userPerms2.put(s, perms);
482
			}
483
		}
484

    
485
		// Check if the number of access rules is the same for mn and cn. If not
486
		// then consider them not equal, without performing diff of each access
487
		// rule.
488
		if (userPerms1.entrySet().size() != userPerms2.entrySet().size())
489
			return false;
490

    
491
		// Now perform the comparison of each access rule of access policy 1 to
492
		// ap 2.
493
		// This test assumes that the mn perms are more complete than the cn
494
		// perms.
495
		logMetacat.debug("Performing comparison of access policies");
496
		for (Map.Entry<Subject, Set<Permission>> entry : userPerms1.entrySet()) {
497
			// User name
498
			Subject s1 = entry.getKey();
499
			// Perms that the user holds
500
			Set<Permission> p1 = entry.getValue();
501
			logMetacat
502
					.debug("Checking access policy of user: " + s1.getValue());
503

    
504
			// Does this user exist in both access policies?
505
			if (userPerms2.containsKey(s1)) {
506
				if (!p1.equals(userPerms2.get(s1))) {
507
					logMetacat.debug("User access policies not equal");
508
					return false;
509
				}
510
			} else {
511
				logMetacat.debug("User access policy not found on CN");
512
				return false;
513
			}
514
		}
515

    
516
		// All comparisons have been passed, so the two access policies are
517
		// equivalent
518
		logMetacat.debug("Access policies are the same");
519
		return true;
520
	}
521

    
522
	/**
523
	 * Run pid synch script on the given pids Each argument is an individual pid
524
	 * because pids cannot contain whitespace.
525
	 * 
526
	 * @param args
527
	 * @throws Exception
528
	 */
529
	public static void main(String[] args) throws Exception {
530

    
531
		// set up the properties based on the test/deployed configuration of the
532
		// workspace
533
		SortedProperties testProperties = new SortedProperties(
534
				"test/test.properties");
535
		testProperties.load();
536
		String metacatContextDir = testProperties
537
				.getProperty("metacat.contextDir");
538
		PropertyService.getInstance(metacatContextDir + "/WEB-INF");
539

    
540
		ArrayList<String> guids = null;
541
		SyncAccessPolicy syncAP = new SyncAccessPolicy();
542

    
543
		if (args.length > 0) {
544
			try {
545
				guids = new ArrayList<String>(Arrays.asList(args));
546
				logMetacat.warn("Trying to syncing access policy for "
547
						+ args.length + " pids");
548
				List<Identifier> synchedPids = syncAP.sync(guids);
549
				logMetacat.warn("Sunk access policies for "
550
						+ synchedPids.size() + " pids");
551
			} catch (Exception e) {
552
				logMetacat.error(
553
						"Error syncing pids, message: " + e.getMessage(), e);
554
				System.exit(1);
555
			}
556
		}
557
	}
558
}
(7-7/8)