rdblue commented on code in PR #3231: URL: https://github.com/apache/iceberg/pull/3231#discussion_r1278605737
########## core/src/main/java/org/apache/iceberg/encryption/AesGcmInputStream.java: ########## @@ -0,0 +1,274 @@ +/* + * 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.iceberg.encryption; + +import java.io.EOFException; +import java.io.IOException; +import java.io.UncheckedIOException; +import java.nio.ByteBuffer; +import java.nio.ByteOrder; +import java.util.Arrays; +import org.apache.iceberg.io.IOUtil; +import org.apache.iceberg.io.SeekableInputStream; +import org.apache.iceberg.relocated.com.google.common.base.Preconditions; + +public class AesGcmInputStream extends SeekableInputStream { + private final SeekableInputStream sourceStream; + private final Ciphers.AesGcmDecryptor gcmDecryptor; + private final byte[] cipherBlockBuffer; + private final int cipherBlockSize; + private final int plainBlockSize; + private final int numberOfBlocks; + private final int lastCipherBlockSize; + private final long plainStreamSize; + private final byte[] fileAADPrefix; + + private long plainStreamPosition; + private int currentBlockIndex; + private int currentOffsetInPlainBlock; + private byte[] currentDecryptedBlock; + private int currentDecryptedBlockIndex; + + AesGcmInputStream( + SeekableInputStream sourceStream, long sourceLength, byte[] aesKey, byte[] fileAADPrefix) + throws IOException { + long netSourceLength = netSourceFileLength(sourceLength); + boolean emptyCipherStream = (0 == netSourceLength); + this.sourceStream = sourceStream; + byte[] headerBytes = new byte[Ciphers.GCM_STREAM_HEADER_LENGTH]; + IOUtil.readFully(sourceStream, headerBytes, 0, headerBytes.length); Review Comment: This should avoid reading in the constructor. Otherwise, this needs to throw IOException and it is harder for the caller to ensure that resources are closed if anything goes wrong. For example, if this throws `IOException` then `newStream` isn't going to return anything. This stream is never constructed, but this is the owner of `sourceFile.newStream()` that is passed in as `sourceStream`. As a result, `sourceStream` is leaked. Instead, this should lazily initialize and use something like the `currentBlockIndex` to keep track of whether it needs to init. -- 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. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
