Search in sources :

Example 66 with HazelcastException

use of com.hazelcast.core.HazelcastException in project Payara by payara.

the class HazelcastSymmetricEncryptor method decode.

public static byte[] decode(byte[] encryptedTextBytes) {
    byte[] decryptedTextBytes;
    try {
        Cipher cipher = Cipher.getInstance(AES_ALGORITHM);
        // Strip off the salt and IV
        ByteBuffer buffer = ByteBuffer.wrap(encryptedTextBytes);
        byte[] saltBytes = new byte[20];
        buffer.get(saltBytes, 0, saltBytes.length);
        byte[] ivBytes = new byte[cipher.getBlockSize()];
        buffer.get(ivBytes, 0, ivBytes.length);
        encryptedTextBytes = new byte[buffer.capacity() - saltBytes.length - ivBytes.length];
        buffer.get(encryptedTextBytes);
        cipher.init(Cipher.DECRYPT_MODE, secretKey, new IvParameterSpec(ivBytes));
        decryptedTextBytes = cipher.doFinal(encryptedTextBytes);
    } catch (BadPaddingException exception) {
        // BadPaddingException -> Wrong key
        throw new HazelcastException("BadPaddingException caught decoding data, " + "this can be caused by an incorrect or changed key: ", exception);
    } catch (IllegalBlockSizeException | NoSuchAlgorithmException | InvalidAlgorithmParameterException | InvalidKeyException | NoSuchPaddingException exception) {
        throw new HazelcastException(exception);
    }
    return decryptedTextBytes;
}
Also used : HazelcastException(com.hazelcast.core.HazelcastException) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) BadPaddingException(javax.crypto.BadPaddingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) ByteBuffer(java.nio.ByteBuffer)

Example 67 with HazelcastException

use of com.hazelcast.core.HazelcastException in project Payara by payara.

the class HazelcastSymmetricEncryptor method readAndDecryptSecretKey.

private static SecretKey readAndDecryptSecretKey() {
    ServerEnvironment serverEnvironment = Globals.getDefaultBaseServiceLocator().getService(ServerEnvironment.class);
    char[] masterPassword = Globals.getDefaultBaseServiceLocator().getService(MasterPasswordImpl.class).getMasterPassword();
    byte[] encryptedBytes = null;
    try {
        encryptedBytes = Files.readAllBytes(new File(serverEnvironment.getConfigDirPath() + File.separator + DATAGRID_KEY_FILE).toPath());
    } catch (IOException ioe) {
        Logger.getLogger(HazelcastSymmetricEncryptor.class.getName()).log(Level.SEVERE, "Error reading datagrid key, please check if it's accessible at expected location: " + serverEnvironment.getConfigDirPath() + File.separator + DATAGRID_KEY_FILE);
        throw new HazelcastException("Error reading encrypted key", ioe);
    }
    try {
        Cipher cipher = Cipher.getInstance(AES_ALGORITHM);
        // Strip off the salt and IV
        ByteBuffer buffer = ByteBuffer.wrap(encryptedBytes);
        byte[] saltBytes = new byte[20];
        buffer.get(saltBytes, 0, saltBytes.length);
        byte[] ivBytes = new byte[cipher.getBlockSize()];
        buffer.get(ivBytes, 0, ivBytes.length);
        byte[] encryptedTextBytes = new byte[buffer.capacity() - saltBytes.length - ivBytes.length];
        buffer.get(encryptedTextBytes);
        // Deriving the key
        SecretKeyFactory factory = SecretKeyFactory.getInstance(PBKDF_ALGORITHM);
        PBEKeySpec spec = new PBEKeySpec(masterPassword, saltBytes, ITERATION_COUNT, KEYSIZE);
        SecretKey secretKeySpec = new SecretKeySpec(factory.generateSecret(spec).getEncoded(), AES);
        cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, new IvParameterSpec(ivBytes));
        return new SecretKeySpec(cipher.doFinal(encryptedTextBytes), "AES");
    } catch (BadPaddingException bpe) {
        throw new HazelcastException("BadPaddingException caught decrypting data grid key " + "- likely caused by an incorrect or changed master password", bpe);
    } catch (IllegalBlockSizeException | NoSuchAlgorithmException | InvalidAlgorithmParameterException | InvalidKeyException | InvalidKeySpecException | NoSuchPaddingException exception) {
        throw new HazelcastException(exception);
    }
}
Also used : MasterPasswordImpl(com.sun.enterprise.security.ssl.impl.MasterPasswordImpl) PBEKeySpec(javax.crypto.spec.PBEKeySpec) HazelcastException(com.hazelcast.core.HazelcastException) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) IOException(java.io.IOException) BadPaddingException(javax.crypto.BadPaddingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) ByteBuffer(java.nio.ByteBuffer) SecretKey(javax.crypto.SecretKey) SecretKeySpec(javax.crypto.spec.SecretKeySpec) ServerEnvironment(org.glassfish.api.admin.ServerEnvironment) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) File(java.io.File) SecretKeyFactory(javax.crypto.SecretKeyFactory)

Example 68 with HazelcastException

use of com.hazelcast.core.HazelcastException in project hazelcast by hazelcast.

the class PostJoinCacheOperationTest method test_cachePostJoinOperationFails_whenJCacheNotAvailable_withCacheConfigs.

@Test
public void test_cachePostJoinOperationFails_whenJCacheNotAvailable_withCacheConfigs() throws Exception {
    // JCache is not available in classpath
    when(JCacheDetector.isJCacheAvailable(classLoader)).thenReturn(false);
    // node engine throws HazelcastException due to missing CacheService
    when(nodeEngine.getService(CacheService.SERVICE_NAME)).thenThrow(new HazelcastException("CacheService not found"));
    // some CacheConfigs are added in the PostJoinCacheOperation (so JCache is actually in use in the rest of the cluster)
    PostJoinCacheOperation postJoinCacheOperation = new PostJoinCacheOperation();
    postJoinCacheOperation.addCacheConfig(new CacheConfig("test"));
    postJoinCacheOperation.setNodeEngine(nodeEngine);
    expectedException.expect(HazelcastException.class);
    postJoinCacheOperation.run();
    verify(nodeEngine).getConfigClassLoader();
    verify(nodeEngine).getService(CacheService.SERVICE_NAME);
    verify(logger, never()).warning(anyString());
}
Also used : HazelcastException(com.hazelcast.core.HazelcastException) CacheConfig(com.hazelcast.config.CacheConfig) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 69 with HazelcastException

use of com.hazelcast.core.HazelcastException in project hazelcast by hazelcast.

the class XmlClientConfigLocator method loadClientHazelcastXmlFromClasspath.

private boolean loadClientHazelcastXmlFromClasspath() {
    URL url = Config.class.getClassLoader().getResource("hazelcast-client.xml");
    if (url == null) {
        LOGGER.finest("Could not find 'hazelcast-client.xml' in classpath.");
        return false;
    }
    LOGGER.info("Loading 'hazelcast-client.xml' from classpath.");
    in = Config.class.getClassLoader().getResourceAsStream("hazelcast-client.xml");
    if (in == null) {
        throw new HazelcastException("Could not load 'hazelcast-client.xml' from classpath");
    }
    return true;
}
Also used : HazelcastException(com.hazelcast.core.HazelcastException) Config(com.hazelcast.config.Config) URL(java.net.URL)

Example 70 with HazelcastException

use of com.hazelcast.core.HazelcastException in project hazelcast by hazelcast.

the class RingbufferContainer method addAll.

/**
     * Adds all items to the ring buffer. Sets the expiration time if TTL is configured and also attempts to store the items
     * in the data store if one is configured.
     *
     * @param items items to be stored in the ring buffer and data store
     * @return the sequence id of the last item stored in the ring buffer
     * @throws HazelcastException                                              if there was any exception thrown by the data store
     * @throws com.hazelcast.nio.serialization.HazelcastSerializationException if the ring buffer is configured to keep items
     *                                                                         in object format and the item could not be
     *                                                                         deserialized
     */
public long addAll(Data[] items) {
    long firstSequence = -1;
    long lastSequence = -1;
    for (int i = 0; i < items.length; i++) {
        lastSequence = addInternal(items[i]);
        if (i == 0) {
            firstSequence = lastSequence;
        }
    }
    if (store.isEnabled() && items.length != 0) {
        try {
            store.storeAll(firstSequence, items);
        } catch (Exception e) {
            throw new HazelcastException(e);
        }
    }
    return lastSequence;
}
Also used : HazelcastException(com.hazelcast.core.HazelcastException) HazelcastException(com.hazelcast.core.HazelcastException) IOException(java.io.IOException) StaleSequenceException(com.hazelcast.ringbuffer.StaleSequenceException)

Aggregations

HazelcastException (com.hazelcast.core.HazelcastException)123 IOException (java.io.IOException)43 QuickTest (com.hazelcast.test.annotation.QuickTest)19 Test (org.junit.Test)19 TxQueueItem (com.hazelcast.collection.impl.txnqueue.TxQueueItem)14 TransactionException (com.hazelcast.transaction.TransactionException)14 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)13 File (java.io.File)13 HazelcastInstanceNotActiveException (com.hazelcast.core.HazelcastInstanceNotActiveException)8 FileInputStream (java.io.FileInputStream)8 FileNotFoundException (java.io.FileNotFoundException)8 Data (com.hazelcast.internal.serialization.Data)7 OOfflineNodeException (com.orientechnologies.common.concur.OOfflineNodeException)6 Future (java.util.concurrent.Future)6 ONeedRetryException (com.orientechnologies.common.concur.ONeedRetryException)5 OException (com.orientechnologies.common.exception.OException)5 OIOException (com.orientechnologies.common.io.OIOException)5 ODistributedRedirectException (com.orientechnologies.orient.enterprise.channel.binary.ODistributedRedirectException)5 EOFException (java.io.EOFException)5 ArrayList (java.util.ArrayList)5