Search in sources :

Example 1 with NullRestoreServiceTaskListener

use of org.exist.xmldb.NullRestoreServiceTaskListener in project exist by eXist-db.

the class BackupRestoreSecurityPrincipalsTest method restoreConflictingUsername.

/**
 * 1. With an empty database we create three
 *    users: 'frank', 'joe', and 'jack'.
 *
 * 2. We create a backup of the database which contains
 *    the three users from (1).
 *
 * 3. We destroy the database, restart the server,
 *    and start again with a clean database.
 *
 * 4. With an (again) empty database we create two
 *    users: 'frank', and 'jack'.
 *
 * 5. We then try and restore the database backup from (2), which
 *    contains the original 'frank', 'joe', and 'jack' users.
 *
 * frank will have the same username and user id in the current
 * database and the backup we are trying to restore.
 *
 * joe does not exist in the current database, but his user id
 * in the backup will collide with that of jack in the current database.
 *
 * jack will have a different user id in the backup when compared to the current
 * database, however he will have the same username.
 *
 * We want to make sure that after the restore, all three users are present
 * that they have distinct and expected user ids and that any resources
 * that were owned by them are still correctly owner by them (and not some other user).
 */
@Test
public void restoreConflictingUsername() throws PermissionDeniedException, EXistException, SAXException, ParserConfigurationException, IOException, URISyntaxException, XMLDBException, IllegalAccessException, ClassNotFoundException, InstantiationException {
    // creates a database with new users: 'frank(id=11)', 'joe(id=12)', and 'jack(id=13)'
    createInitialUsers(FRANK_USER, JOE_USER, JACK_USER);
    // create a backup of the database (which has the initial users)
    final Path backupFile = backupDatabase();
    // reset database to empty
    server.restart(true);
    // create new users: 'frank(id=11)' and 'jack(id=12)'
    createInitialUsers(FRANK_USER, JACK_USER);
    final String accountQuery = "declare namespace c = 'http://exist-db.org/Configuration';\n" + "for $account in //c:account\n" + "return\n" + "<user id='{$account/@id}' name='{$account/c:name}'/>";
    final XPathQueryService xqs = (XPathQueryService) server.getRoot().getService("XPathQueryService", "1.0");
    final SecurityManagerImpl sm = (SecurityManagerImpl) BrokerPool.getInstance().getSecurityManager();
    // check the current user accounts
    ResourceSet result = xqs.query(accountQuery);
    assertUser(RealmImpl.ADMIN_ACCOUNT_ID, SecurityManager.DBA_USER, ((XMLResource) result.getResource(0)).getContentAsDOM());
    assertUser(RealmImpl.GUEST_ACCOUNT_ID, SecurityManager.GUEST_USER, ((XMLResource) result.getResource(1)).getContentAsDOM());
    assertUser(SecurityManagerImpl.INITIAL_LAST_ACCOUNT_ID + 1, "frank", ((XMLResource) result.getResource(2)).getContentAsDOM());
    assertUser(SecurityManagerImpl.INITIAL_LAST_ACCOUNT_ID + 2, "jack", ((XMLResource) result.getResource(3)).getContentAsDOM());
    // check the last user id
    // last account id should be that of 'jack'
    assertEquals(SecurityManagerImpl.INITIAL_LAST_ACCOUNT_ID + 2, sm.getLastAccountId());
    // create a test collection and give everyone access
    final CollectionManagementService cms = (CollectionManagementService) server.getRoot().getService("CollectionManagementService", "1.0");
    final Collection test = cms.createCollection("test");
    final UserManagementService testUms = (UserManagementService) test.getService("UserManagementService", "1.0");
    testUms.chmod("rwxrwxrwx");
    // create and store a new document as 'frank'
    final Collection frankTest = DatabaseManager.getCollection("xmldb:exist:///db/test", FRANK_USER, FRANK_USER);
    final String FRANKS_DOCUMENT = "franks-document.xml";
    final Resource frankDoc = frankTest.createResource(FRANKS_DOCUMENT, XMLResource.RESOURCE_TYPE);
    frankDoc.setContent("<hello>frank</hello>");
    frankTest.storeResource(frankDoc);
    // create and store a new document as 'jack'
    final Collection jackTest = DatabaseManager.getCollection("xmldb:exist:///db/test", JACK_USER, JACK_USER);
    final String JACKS_DOCUMENT = "jacks-document.xml";
    final Resource jackDoc = jackTest.createResource(JACKS_DOCUMENT, XMLResource.RESOURCE_TYPE);
    jackDoc.setContent("<hello>jack</hello>");
    jackTest.storeResource(jackDoc);
    // restore the database backup
    final EXistRestoreService service = (EXistRestoreService) server.getRoot().getService("RestoreService", "1.0");
    service.restore(backupFile.normalize().toAbsolutePath().toString(), null, new NullRestoreServiceTaskListener(), false);
    // check the current user accounts after the restore
    result = xqs.query(accountQuery);
    assertUser(RealmImpl.ADMIN_ACCOUNT_ID, SecurityManager.DBA_USER, ((XMLResource) result.getResource(0)).getContentAsDOM());
    assertUser(RealmImpl.GUEST_ACCOUNT_ID, SecurityManager.GUEST_USER, ((XMLResource) result.getResource(1)).getContentAsDOM());
    assertUser(SecurityManagerImpl.INITIAL_LAST_ACCOUNT_ID + 1, FRANK_USER, ((XMLResource) result.getResource(2)).getContentAsDOM());
    assertUser(SecurityManagerImpl.INITIAL_LAST_ACCOUNT_ID + 2, JACK_USER, ((XMLResource) result.getResource(3)).getContentAsDOM());
    assertUser(SecurityManagerImpl.INITIAL_LAST_ACCOUNT_ID + 3, JOE_USER, ((XMLResource) result.getResource(4)).getContentAsDOM());
    // check the last user id after the restore
    // last account id should be that of 'joe'
    assertEquals(SecurityManagerImpl.INITIAL_LAST_ACCOUNT_ID + 3, sm.getLastAccountId());
    // check the owner of frank's document after restore
    final Resource fDoc = test.getResource(FRANKS_DOCUMENT);
    final Permission franksDocPermissions = testUms.getPermissions(fDoc);
    assertEquals(FRANK_USER, franksDocPermissions.getOwner().getName());
    // check the owner of jack's document after restore
    final Resource jDoc = test.getResource(JACKS_DOCUMENT);
    final Permission jacksDocPermissions = testUms.getPermissions(jDoc);
    assertEquals(JACK_USER, jacksDocPermissions.getOwner().getName());
}
Also used : Path(java.nio.file.Path) CollectionManagementService(org.xmldb.api.modules.CollectionManagementService) EXistRestoreService(org.exist.xmldb.EXistRestoreService) XPathQueryService(org.xmldb.api.modules.XPathQueryService) XMLResource(org.xmldb.api.modules.XMLResource) Resource(org.xmldb.api.base.Resource) NullRestoreServiceTaskListener(org.exist.xmldb.NullRestoreServiceTaskListener) Collection(org.xmldb.api.base.Collection) UserManagementService(org.exist.xmldb.UserManagementService) ResourceSet(org.xmldb.api.base.ResourceSet)

Aggregations

Path (java.nio.file.Path)1 EXistRestoreService (org.exist.xmldb.EXistRestoreService)1 NullRestoreServiceTaskListener (org.exist.xmldb.NullRestoreServiceTaskListener)1 UserManagementService (org.exist.xmldb.UserManagementService)1 Collection (org.xmldb.api.base.Collection)1 Resource (org.xmldb.api.base.Resource)1 ResourceSet (org.xmldb.api.base.ResourceSet)1 CollectionManagementService (org.xmldb.api.modules.CollectionManagementService)1 XMLResource (org.xmldb.api.modules.XMLResource)1 XPathQueryService (org.xmldb.api.modules.XPathQueryService)1