use of org.apache.accumulo.core.spi.crypto.FileDecrypter in project accumulo by apache.
the class CryptoTest method decrypt.
private void decrypt(byte[] resultingBytes, Scope scope, ConfigMode configMode) throws Exception {
try (DataInputStream dataIn = new DataInputStream(new ByteArrayInputStream(resultingBytes))) {
AccumuloConfiguration conf = getAccumuloConfig(configMode);
CryptoService cs = CryptoServiceFactory.newInstance(conf, ClassloaderType.JAVA);
FileDecrypter decrypter = getFileDecrypter(cs, scope, dataIn);
try (DataInputStream decrypted = new DataInputStream(decrypter.decryptStream(dataIn))) {
String markerString = decrypted.readUTF();
int markerInt = decrypted.readInt();
assertEquals(MARKER_STRING, markerString);
assertEquals(MARKER_INT, markerInt);
}
}
}
use of org.apache.accumulo.core.spi.crypto.FileDecrypter in project accumulo by apache.
the class CryptoTest method simpleGCMTest.
@Test
public void simpleGCMTest() throws Exception {
AccumuloConfiguration conf = getAccumuloConfig(ConfigMode.CRYPTO_ON);
CryptoService cs = new AESCryptoService();
cs.init(conf.getAllPropertiesWithPrefix(Property.INSTANCE_CRYPTO_PREFIX));
CryptoEnvironment encEnv = new CryptoEnvironmentImpl(Scope.RFILE, null);
FileEncrypter encrypter = cs.getFileEncrypter(encEnv);
byte[] params = encrypter.getDecryptionParameters();
assertNotNull(params);
ByteArrayOutputStream out = new ByteArrayOutputStream();
DataOutputStream dataOut = new DataOutputStream(out);
CryptoUtils.writeParams(params, dataOut);
OutputStream encrypted = encrypter.encryptStream(dataOut);
assertNotNull(encrypted);
DataOutputStream cipherOut = new DataOutputStream(encrypted);
cipherOut.writeUTF(MARKER_STRING);
cipherOut.close();
dataOut.close();
encrypted.close();
out.close();
byte[] cipherText = out.toByteArray();
// decrypt
ByteArrayInputStream in = new ByteArrayInputStream(cipherText);
FileDecrypter decrypter = getFileDecrypter(cs, Scope.RFILE, new DataInputStream(in));
DataInputStream decrypted = new DataInputStream(decrypter.decryptStream(in));
String plainText = decrypted.readUTF();
decrypted.close();
in.close();
assertEquals(MARKER_STRING, new String(plainText));
}
use of org.apache.accumulo.core.spi.crypto.FileDecrypter in project accumulo by apache.
the class DfsLogger method getDecryptingStream.
/**
* Reads the WAL file header, and returns a decrypting stream which wraps the original stream. If
* the file is not encrypted, the original stream is returned.
*
* @throws LogHeaderIncompleteException
* if the header cannot be fully read (can happen if the tserver died before finishing)
*/
public static DataInputStream getDecryptingStream(FSDataInputStream input, AccumuloConfiguration conf) throws LogHeaderIncompleteException, IOException {
DataInputStream decryptingInput;
byte[] magic4 = DfsLogger.LOG_FILE_HEADER_V4.getBytes(UTF_8);
byte[] magic3 = DfsLogger.LOG_FILE_HEADER_V3.getBytes(UTF_8);
if (magic4.length != magic3.length)
throw new AssertionError("Always expect log file headers to be same length : " + magic4.length + " != " + magic3.length);
byte[] magicBuffer = new byte[magic4.length];
try {
input.readFully(magicBuffer);
if (Arrays.equals(magicBuffer, magic4)) {
CryptoService cryptoService = CryptoServiceFactory.newInstance(conf, ClassloaderType.ACCUMULO);
FileDecrypter decrypter = CryptoUtils.getFileDecrypter(cryptoService, Scope.WAL, input);
log.debug("Using {} for decrypting WAL", cryptoService.getClass().getSimpleName());
decryptingInput = cryptoService instanceof NoCryptoService ? input : new DataInputStream(decrypter.decryptStream(input));
} else if (Arrays.equals(magicBuffer, magic3)) {
// Read logs files from Accumulo 1.9
String cryptoModuleClassname = input.readUTF();
if (!cryptoModuleClassname.equals("NullCryptoModule")) {
throw new IllegalArgumentException("Old encryption modules not supported at this time. Unsupported module : " + cryptoModuleClassname);
}
decryptingInput = input;
} else {
throw new IllegalArgumentException("Unsupported write ahead log version " + new String(magicBuffer));
}
} catch (EOFException e) {
// A TabletServer might have died before the (complete) header was written
throw new LogHeaderIncompleteException(e);
}
return decryptingInput;
}
Aggregations