Search in sources :

Example 1 with SakuliCipherException

use of org.sakuli.exceptions.SakuliCipherException in project sakuli by ConSol.

the class CipherUtilsTest method testChipherException.

@Test
public void testChipherException() throws Throwable {
    try {
        ActionProperties props = new ActionProperties();
        props.setEncryptionInterfaceAutodetect(false);
        props.setEncryptionInterface("etNOVALID");
        testling = new CipherUtil(props);
        testling.scanNetworkInterfaces();
        Assert.assertTrue(false, "Error, no exception is thrown");
    } catch (SakuliCipherException e) {
        Assert.assertNotNull(e.interfaceLog);
        Assert.assertTrue(e.getMessage().contains(e.interfaceLog));
    }
}
Also used : SakuliCipherException(org.sakuli.exceptions.SakuliCipherException) ActionProperties(org.sakuli.datamodel.properties.ActionProperties) CipherUtil(org.sakuli.actions.environment.CipherUtil) Test(org.testng.annotations.Test)

Example 2 with SakuliCipherException

use of org.sakuli.exceptions.SakuliCipherException in project sakuli by ConSol.

the class CipherUtil method decrypt.

/**
     * Decrypts a String to the secret. The decryption must be take place on the same physical machine like the encryption, see {@link #encrypt(String)}.
     *
     * @param strToDecrypt String to encrypt
     * @return the decrypted secret
     * @throws SakuliCipherException if the decryption fails.
     */
public String decrypt(String strToDecrypt) throws SakuliCipherException {
    try {
        Cipher cipher = Cipher.getInstance(algorithm);
        cipher.init(Cipher.DECRYPT_MODE, getKey());
        return new String(cipher.doFinal(Base64.decodeBase64(strToDecrypt)));
    } catch (IllegalBlockSizeException e) {
        throw new SakuliCipherException("Maybe this secret hasn't been encrypted correctly! Maybe encrypt it again!", interfaceLog, e);
    } catch (Exception e) {
        throw new SakuliCipherException(e, interfaceLog);
    }
}
Also used : SakuliCipherException(org.sakuli.exceptions.SakuliCipherException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) Cipher(javax.crypto.Cipher) SakuliCipherException(org.sakuli.exceptions.SakuliCipherException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException)

Example 3 with SakuliCipherException

use of org.sakuli.exceptions.SakuliCipherException in project sakuli by ConSol.

the class CipherUtil method scanNetworkInterfaces.

/**
     * fetch the local network interfaceLog and reads out the MAC of the chosen encryption interface.
     * Must be called before the methods {@link #encrypt(String)} or {@link #decrypt(String)}.
     *
     * @throws SakuliCipherException for wrong interface names and MACs.
     */
@PostConstruct
public void scanNetworkInterfaces() throws SakuliCipherException {
    Enumeration<NetworkInterface> networkInterfaces;
    try {
        interfaceName = checkEthInterfaceName();
        networkInterfaces = getNetworkInterfaces();
        while (networkInterfaces.hasMoreElements()) {
            NetworkInterface anInterface = networkInterfaces.nextElement();
            if (anInterface.getHardwareAddress() != null) {
                interfaceLog = interfaceLog + "\nNET-Interface " + anInterface.getIndex() + " - Name: " + anInterface.getName() + "\t MAC: " + formatMAC(anInterface.getHardwareAddress()) + "\t VirtualAdapter: " + anInterface.isVirtual() + "\t Loopback: " + anInterface.isLoopback() + "\t Desc.: " + anInterface.getDisplayName();
            }
            if (anInterface.getName().equals(interfaceName)) {
                macOfEncryptionInterface = anInterface.getHardwareAddress();
            }
        }
        if (macOfEncryptionInterface == null) {
            throw new SakuliCipherException("Cannot resolve MAC address ... please check your config of the property: " + ActionProperties.ENCRYPTION_INTERFACE + "=" + interfaceName, interfaceLog);
        }
    } catch (Exception e) {
        throw new SakuliCipherException(e, interfaceLog);
    }
}
Also used : SakuliCipherException(org.sakuli.exceptions.SakuliCipherException) NetworkInterface(java.net.NetworkInterface) SakuliCipherException(org.sakuli.exceptions.SakuliCipherException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) PostConstruct(javax.annotation.PostConstruct)

Example 4 with SakuliCipherException

use of org.sakuli.exceptions.SakuliCipherException in project sakuli by ConSol.

the class CipherUtil method encrypt.

/**
     * Encrypts the secret into a encrypted {@link String}, based on the MAC address of the first network interface of a machine.
     * Therewith it should be secured, that an encrypted secret is only valid on one physical machine.
     *
     * @param strToEncrypt the secret
     * @return a encrypted String, which is coupled to one physical machine
     * @throws SakuliCipherException if the encryption fails.
     */
public String encrypt(String strToEncrypt) throws SakuliCipherException {
    try {
        Cipher cipher = Cipher.getInstance(algorithm);
        cipher.init(Cipher.ENCRYPT_MODE, getKey());
        return Base64.encodeBase64String(cipher.doFinal(strToEncrypt.getBytes()));
    } catch (Exception e) {
        throw new SakuliCipherException(e, interfaceLog);
    }
}
Also used : SakuliCipherException(org.sakuli.exceptions.SakuliCipherException) Cipher(javax.crypto.Cipher) SakuliCipherException(org.sakuli.exceptions.SakuliCipherException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException)

Aggregations

SakuliCipherException (org.sakuli.exceptions.SakuliCipherException)4 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)3 Cipher (javax.crypto.Cipher)2 NetworkInterface (java.net.NetworkInterface)1 PostConstruct (javax.annotation.PostConstruct)1 CipherUtil (org.sakuli.actions.environment.CipherUtil)1 ActionProperties (org.sakuli.datamodel.properties.ActionProperties)1 Test (org.testng.annotations.Test)1