Search in sources :

Example 21 with MessageDigest

use of org.exist.util.crypto.digest.MessageDigest in project exist by eXist-db.

the class BlobStoreRecoveryTest method addNoCommit.

/**
 * Add Blob (DO NOT commit), then stop (either by shutdown or simulated crash).
 *
 * Expect that the Blob is NOT present after recovery.
 */
@Test
public void addNoCommit() throws IOException, BrokerPoolServiceException, EXistException {
    final Path blobDbx = temporaryFolder.getRoot().toPath().resolve("blob.dbx");
    final Path blobDir = temporaryFolder.newFolder("blob").toPath();
    final Tuple2<byte[], MessageDigest> tempBin1 = generateTestFile();
    // write the data
    try (final BlobDb blobDb = newBlobDb(temporaryFolder.getRoot().toPath(), blobDbx, blobDir)) {
        blobDb.blobStore.open();
        addNoCommit(blobDb.transactionManager, blobDb.blobStore, tempBin1._1);
    }
    // test the recovery
    try (final BlobDb blobDb = newBlobDb(temporaryFolder.getRoot().toPath(), blobDbx, blobDir)) {
        blobDb.blobStore.open();
        final BlobId blobId = new BlobId(tempBin1._2.getValue());
        final Tuple2<byte[], MessageDigest> actual = getCommit(blobDb.transactionManager, blobDb.blobStore, blobId);
        assertNull(actual);
        if (blobDb.blobStore instanceof BlobStoreImpl) {
            assertNull(((BlobStoreImpl) blobDb.blobStore).getReferenceCount(blobId));
        }
    }
}
Also used : Path(java.nio.file.Path) MessageDigest(org.exist.util.crypto.digest.MessageDigest) Test(org.junit.Test)

Example 22 with MessageDigest

use of org.exist.util.crypto.digest.MessageDigest in project exist by eXist-db.

the class BlobStoreRecoveryTest method addCommit_addNoCommit.

/**
 * Add Blob and commit, re-add same Blob (DO NOT commit), then stop (either by shutdown or simulated crash).
 *
 * Expect that the Blob is present after recovery.
 */
@Test
public void addCommit_addNoCommit() throws IOException, BrokerPoolServiceException, EXistException {
    final Path blobDbx = temporaryFolder.getRoot().toPath().resolve("blob.dbx");
    final Path blobDir = temporaryFolder.newFolder("blob").toPath();
    final Tuple2<byte[], MessageDigest> tempBin1 = generateTestFile();
    // write the data
    try (final BlobDb blobDb = newBlobDb(temporaryFolder.getRoot().toPath(), blobDbx, blobDir)) {
        blobDb.blobStore.open();
        addCommit(blobDb.transactionManager, blobDb.blobStore, tempBin1._1);
        addNoCommit(blobDb.transactionManager, blobDb.blobStore, tempBin1._1);
    }
    // test the recovery
    try (final BlobDb blobDb = newBlobDb(temporaryFolder.getRoot().toPath(), blobDbx, blobDir)) {
        blobDb.blobStore.open();
        final BlobId blobId = new BlobId(tempBin1._2.getValue());
        final Tuple2<byte[], MessageDigest> actual = getCommit(blobDb.transactionManager, blobDb.blobStore, blobId);
        assertNotNull(actual);
        assertEquals(tempBin1._2, tempBin1._2);
        assertArrayEquals(tempBin1._1, tempBin1._1);
        if (blobDb.blobStore instanceof BlobStoreImpl) {
            assertEquals(1, ((BlobStoreImpl) blobDb.blobStore).getReferenceCount(blobId).intValue());
        }
    }
}
Also used : Path(java.nio.file.Path) MessageDigest(org.exist.util.crypto.digest.MessageDigest) Test(org.junit.Test)

Example 23 with MessageDigest

use of org.exist.util.crypto.digest.MessageDigest in project exist by eXist-db.

the class BlobStoreRecoveryTest method addCommit_addCommit_removeNoCommit.

/**
 * Add Blob and commit, re-add same Blob and commit, Remove Blob (DO NOT commit), then stop (either by shutdown or simulated crash).
 *
 * Expect that the Blob is present after recovery.
 */
@Test
public void addCommit_addCommit_removeNoCommit() throws IOException, BrokerPoolServiceException, EXistException {
    final Path blobDbx = temporaryFolder.getRoot().toPath().resolve("blob.dbx");
    final Path blobDir = temporaryFolder.newFolder("blob").toPath();
    final Tuple2<byte[], MessageDigest> tempBin1 = generateTestFile();
    // write the data
    try (final BlobDb blobDb = newBlobDb(temporaryFolder.getRoot().toPath(), blobDbx, blobDir)) {
        blobDb.blobStore.open();
        addCommit(blobDb.transactionManager, blobDb.blobStore, tempBin1._1);
        addCommit(blobDb.transactionManager, blobDb.blobStore, tempBin1._1);
        removeNoCommit(blobDb.transactionManager, blobDb.blobStore, tempBin1._1);
    }
    // test the recovery
    try (final BlobDb blobDb = newBlobDb(temporaryFolder.getRoot().toPath(), blobDbx, blobDir)) {
        blobDb.blobStore.open();
        final BlobId blobId = new BlobId(tempBin1._2.getValue());
        final Tuple2<byte[], MessageDigest> actual = getCommit(blobDb.transactionManager, blobDb.blobStore, blobId);
        assertNotNull(actual);
        assertEquals(tempBin1._2, tempBin1._2);
        assertArrayEquals(tempBin1._1, tempBin1._1);
        if (blobDb.blobStore instanceof BlobStoreImpl) {
            assertEquals(2, ((BlobStoreImpl) blobDb.blobStore).getReferenceCount(blobId).intValue());
        }
    }
}
Also used : Path(java.nio.file.Path) MessageDigest(org.exist.util.crypto.digest.MessageDigest) Test(org.junit.Test)

Example 24 with MessageDigest

use of org.exist.util.crypto.digest.MessageDigest in project exist by eXist-db.

the class BlobStoreRecoveryTest method addCommit_addCommit.

/**
 * Add Blob and commit, re-add same Blob and commit, then stop (either by shutdown or simulated crash).
 *
 * Expect that the Blob is present after recovery.
 */
@Test
public void addCommit_addCommit() throws IOException, BrokerPoolServiceException, EXistException {
    final Path blobDbx = temporaryFolder.getRoot().toPath().resolve("blob.dbx");
    final Path blobDir = temporaryFolder.newFolder("blob").toPath();
    final Tuple2<byte[], MessageDigest> tempBin1 = generateTestFile();
    // write the data
    try (final BlobDb blobDb = newBlobDb(temporaryFolder.getRoot().toPath(), blobDbx, blobDir)) {
        blobDb.blobStore.open();
        addCommit(blobDb.transactionManager, blobDb.blobStore, tempBin1._1);
        addCommit(blobDb.transactionManager, blobDb.blobStore, tempBin1._1);
    }
    // test the recovery
    try (final BlobDb blobDb = newBlobDb(temporaryFolder.getRoot().toPath(), blobDbx, blobDir)) {
        blobDb.blobStore.open();
        final BlobId blobId = new BlobId(tempBin1._2.getValue());
        final Tuple2<byte[], MessageDigest> actual = getCommit(blobDb.transactionManager, blobDb.blobStore, blobId);
        assertNotNull(actual);
        assertEquals(tempBin1._2, tempBin1._2);
        assertArrayEquals(tempBin1._1, tempBin1._1);
        if (blobDb.blobStore instanceof BlobStoreImpl) {
            assertEquals(2, ((BlobStoreImpl) blobDb.blobStore).getReferenceCount(blobId).intValue());
        }
    }
}
Also used : Path(java.nio.file.Path) MessageDigest(org.exist.util.crypto.digest.MessageDigest) Test(org.junit.Test)

Example 25 with MessageDigest

use of org.exist.util.crypto.digest.MessageDigest in project exist by eXist-db.

the class ClientFrame method setPermAction.

private void setPermAction(final ActionEvent ev) throws PermissionDeniedException {
    if (fileman.getSelectedRowCount() == 0) {
        return;
    }
    try {
        final Collection collection = client.getCollection();
        final UserManagementService service = getUserManagementService();
        String name = null;
        String created = null;
        String modified = null;
        String size = null;
        String messageDigestType = null;
        String messageDigestValue = null;
        String mimeType = null;
        String owner = null;
        String group = null;
        ModeDisplay mode = null;
        SimpleACLPermissionAider acl = null;
        final DateFormat dateTimeFormat = DateFormat.getDateTimeInstance();
        final List<ResourceDescriptor> selected = new ArrayList<>();
        boolean firstPerm = true;
        for (final int row : fileman.getSelectedRows()) {
            final ResourceDescriptor selectedRow = resources.getRow(row);
            selected.add(selectedRow);
            final XmldbURI thisName = selectedRow.getName();
            final String thisCreated;
            final String thisModified;
            final String thisMessageDigestType;
            final String thisMessageDigestValue;
            final String thisSize;
            final String thisMimeType;
            final Permission thisPerm;
            if (selectedRow.isCollection()) {
                final Collection coll = collection.getChildCollection(thisName.toString());
                thisCreated = dateTimeFormat.format(((EXistCollection) coll).getCreationTime());
                thisModified = NON_APPLICABLE;
                thisMimeType = COLLECTION_MIME_TYPE;
                thisMessageDigestType = NON_APPLICABLE;
                thisMessageDigestValue = NON_APPLICABLE;
                thisSize = NON_APPLICABLE;
                thisPerm = service.getPermissions(coll);
            } else {
                final Resource res = collection.getResource(thisName.toString());
                thisCreated = dateTimeFormat.format(((EXistResource) res).getCreationTime());
                thisModified = dateTimeFormat.format(((EXistResource) res).getLastModificationTime());
                thisMimeType = ((EXistResource) res).getMimeType();
                if (res instanceof EXistBinaryResource) {
                    final MessageDigest messageDigest = ((EXistBinaryResource) res).getContentDigest(DigestType.BLAKE_256);
                    thisMessageDigestType = messageDigest.getDigestType().getCommonNames()[0];
                    thisMessageDigestValue = messageDigest.toHexString();
                    thisSize = humanSize(((EXistBinaryResource) res).getContentLength());
                } else {
                    thisMessageDigestType = NON_APPLICABLE;
                    thisMessageDigestValue = NON_APPLICABLE;
                    thisSize = NON_APPLICABLE;
                }
                thisPerm = service.getPermissions(res);
            }
            name = getUpdated(name, () -> URIUtils.urlDecodeUtf8(thisName));
            created = getUpdated(created, thisCreated);
            modified = getUpdated(modified, thisModified);
            mimeType = getUpdated(mimeType, thisMimeType);
            messageDigestType = getUpdated(messageDigestType, thisMessageDigestType);
            messageDigestValue = getUpdated(messageDigestValue, thisMessageDigestValue);
            size = getUpdated(size, thisSize);
            owner = getUpdated(owner, () -> thisPerm.getOwner().getName());
            group = getUpdated(group, () -> thisPerm.getGroup().getName());
            mode = getUpdatedMode(mode, thisPerm);
            if (firstPerm) {
                if (thisPerm instanceof ACLPermission) {
                    final ACLPermission thisAcl = (ACLPermission) thisPerm;
                    acl = new SimpleACLPermissionAider();
                    for (int i = 0; i < thisAcl.getACECount(); i++) {
                        acl.addACE(thisAcl.getACEAccessType(i), thisAcl.getACETarget(i), thisAcl.getACEWho(i), thisAcl.getACEMode(i));
                    }
                } else {
                    acl = null;
                }
                firstPerm = false;
            } else {
                if (acl != null && thisPerm instanceof ACLPermission) {
                    final ACLPermission thisAcl = (ACLPermission) thisPerm;
                    if (!acl.aclEquals(thisAcl)) {
                        acl = null;
                    }
                }
            }
        }
        final EditPropertiesDialog editPropertiesDialog = new EditPropertiesDialog(service, client.getProperties().getProperty(InteractiveClient.USER), collection, name, mimeType, created, modified, size, messageDigestType, messageDigestValue, owner, group, mode, acl, selected);
        editPropertiesDialog.addWindowListener(new WindowAdapter() {

            @Override
            public void windowClosed(final WindowEvent e) {
                try {
                    client.reloadCollection();
                } catch (final XMLDBException xmldbe) {
                    // $NON-NLS-1$
                    showErrorMessage(Messages.getString("ClientFrame.197") + xmldbe.getMessage(), xmldbe);
                    xmldbe.printStackTrace();
                }
            }
        });
        editPropertiesDialog.setVisible(true);
    } catch (final XMLDBException e) {
        // $NON-NLS-1$
        showErrorMessage(Messages.getString("ClientFrame.197") + e.getMessage(), e);
        e.printStackTrace();
    }
}
Also used : SimpleACLPermissionAider(org.exist.security.internal.aider.SimpleACLPermissionAider) XMLResource(org.xmldb.api.modules.XMLResource) Resource(org.xmldb.api.base.Resource) EditPropertiesDialog(org.exist.client.security.EditPropertiesDialog) XMLDBException(org.xmldb.api.base.XMLDBException) ModeDisplay(org.exist.client.security.ModeDisplay) DateFormat(java.text.DateFormat) SimpleDateFormat(java.text.SimpleDateFormat) Collection(org.xmldb.api.base.Collection) MessageDigest(org.exist.util.crypto.digest.MessageDigest)

Aggregations

MessageDigest (org.exist.util.crypto.digest.MessageDigest)30 Path (java.nio.file.Path)20 Test (org.junit.Test)19 InputStream (java.io.InputStream)6 UnsynchronizedByteArrayInputStream (org.apache.commons.io.input.UnsynchronizedByteArrayInputStream)5 DigestInputStream (org.exist.util.crypto.digest.DigestInputStream)5 StreamableDigest (org.exist.util.crypto.digest.StreamableDigest)4 Tuple2 (com.evolvedbinary.j8fu.tuple.Tuple2)3 IOException (java.io.IOException)3 URISyntaxException (java.net.URISyntaxException)3 DigestType (org.exist.util.crypto.digest.DigestType)3 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 Nullable (javax.annotation.Nullable)2 EXistException (org.exist.EXistException)2 PermissionDeniedException (org.exist.security.PermissionDeniedException)2 Txn (org.exist.storage.txn.Txn)2 Try (com.evolvedbinary.j8fu.Try)1 TaggedTryUnchecked (com.evolvedbinary.j8fu.Try.TaggedTryUnchecked)1 Tuple (com.evolvedbinary.j8fu.tuple.Tuple.Tuple)1