markap14 commented on a change in pull request #3594: NIFI-3833 Support for 
Encrypted Flow File Repositories
URL: https://github.com/apache/nifi/pull/3594#discussion_r314032222
 
 

 ##########
 File path: 
nifi-commons/nifi-write-ahead-log/src/main/java/org/apache/nifi/wali/SimpleCipherInputStream.java
 ##########
 @@ -0,0 +1,104 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.nifi.wali;
+
+import org.bouncycastle.crypto.io.CipherInputStream;
+import org.bouncycastle.crypto.modes.AEADBlockCipher;
+
+import javax.crypto.SecretKey;
+import java.io.IOException;
+import java.io.InputStream;
+
+/**
+ * This class extends {@link CipherInputStream} with a static factory method 
for constructing
+ * an input stream with an AEAD block cipher.
+ */
+public class SimpleCipherInputStream extends CipherInputStream {
+    protected AEADBlockCipher cipher;
+
+    /**
+     * Constructs an {@link InputStream} from an existing {@link InputStream} 
and block cipher.
+     *
+     * @param in input stream to wrap.
+     * @param cipher block cipher, initialized for decryption.
+     */
+    public SimpleCipherInputStream(InputStream in, AEADBlockCipher cipher) {
+        super(in, cipher);
+        this.cipher = cipher;
+    }
+
+    /**
+     * Static factory for wrapping an input stream with a block cipher.
+     *
+     * NB:  this function eagerly reads the initial cipher values from the 
plain input stream before returning the cipher stream.
+     *
+     * @param in input stream to wrap.
+     * @param key cipher key.
+     * @return wrapped input stream.
+     * @throws IOException if the stream cannot be read eagerly, or if the 
cipher cannot be initialized.
+     */
+    public static InputStream wrapWithKey(InputStream in, SecretKey key) 
throws IOException {
+        if (key == null) {
+            return in;
+        }
+
+        if (in.markSupported()) {
+            in.mark(0);
+        }
+
+        // Read the marker, the iv, and the aad in the same order as they're 
written in the SimpleCipherOutputStream:
+        try {
+            final int marker = in.read();
+            if (marker != SimpleCipherUtil.MARKER_BYTE) {
 
 Review comment:
   This feels off to me. If the first byte read is not what we expect, it will 
attempt to roll back that byte. Regardless of whether or not it's successfully, 
it will return the InputStream given to it. So if I pass in an InputStream, I 
may get back the same InputStream or I may get back a different one. If I get 
back the same stream I passed in, it may have some data consumed from it, or it 
may not.
   
   I think it makes more sense to require that the stream supports mark/reset 
or else throw an Exception. Also, if the first byte is not what it should be, 
it should probably throw an Exception, because what it is returning is not an 
InputStream that is wrapped with a key - it is the original InputStream.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to