我想使用nifi加密json中的属性,而不是密钥,因为我想将数据上传到mongodb服务器。有办法这样做吗?对于这个项目,我用twitter数据作为概念的证明.到目前为止,我只使用EvaluateJsonPath处理器提取tweet的文本,并且可以加密这个文本,但是得到的json不再有密钥。Nifi能否重新创建一个json,将键附加到我提取的这个属性上?有更好的方法吗?
发布于 2016-10-27 23:25:43
不幸的是,现有的Apache NiFi处理器并不很好地支持此工作流。您可能会设计一个工作流,将JSON内容拆分为属性,将每个属性拆分为单个流文件的内容,加密该内容,将流文件合并回来,并通过UpdateAttribute将现在加密的内容重新组合为属性。
我有一个新的创造了一个吉拉处理器的NiFi使这更简单。在可用之前,我的建议是使用ExecuteScript处理器来实现这一点。我提供了一个模板示例,您可以将其直接导入NiFi实例并连接到流。下面提供了ExecuteScript处理器的主体(您可以看到我如何初始化AES/GCM密码,并将算法、密钥和IV更改为所需的值)。
import javax.crypto.Cipher
import javax.crypto.SecretKey
import javax.crypto.spec.IvParameterSpec
import javax.crypto.spec.SecretKeySpec
import java.nio.charset.StandardCharsets
FlowFile flowFile = session.get()
if (!flowFile) {
return
}
try {
// Get the raw values of the attributes
String normalAttribute = flowFile.getAttribute('Normal Attribute')
String sensitiveAttribute = flowFile.getAttribute('Sensitive Attribute')
// Instantiate an encryption cipher
// Lots of additional code could go here to generate a random key, derive a key from a password, read from a file or keyring, etc.
String keyHex = "0123456789ABCDEFFEDCBA9876543210" // * 2 for 256-bit encryption
SecretKey key = new SecretKeySpec(keyHex.getBytes(StandardCharsets.UTF_8), "AES")
IvParameterSpec iv = new IvParameterSpec(keyHex[0..<16].getBytes(StandardCharsets.UTF_8))
Cipher aesGcmEncCipher = Cipher.getInstance("AES/GCM/NoPadding", "BC")
aesGcmEncCipher.init(Cipher.ENCRYPT_MODE, key, iv)
String encryptedNormalAttribute = Base64.encoder.encodeToString(aesGcmEncCipher.doFinal(normalAttribute.bytes))
String encryptedSensitiveAttribute = Base64.encoder.encodeToString(aesGcmEncCipher.doFinal(sensitiveAttribute.bytes))
// Add a new attribute with the encrypted normal attribute
flowFile = session.putAttribute(flowFile, 'Normal Attribute (encrypted)', encryptedNormalAttribute)
// Replace the sensitive attribute inline with the cipher text
flowFile = session.putAttribute(flowFile, 'Sensitive Attribute', encryptedSensitiveAttribute)
session.transfer(flowFile, REL_SUCCESS)
} catch (Exception e) {
log.error("There was an error encrypting the attributes: ${e.getMessage()}")
session.transfer(flowFile, REL_FAILURE)
}https://stackoverflow.com/questions/40294945
复制相似问题