You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@iceberg.apache.org by "rdblue (via GitHub)" <gi...@apache.org> on 2023/04/02 18:35:57 UTC

[GitHub] [iceberg] rdblue commented on a diff in pull request #6450: Core: Key metadata in Avro format

rdblue commented on code in PR #6450:
URL: https://github.com/apache/iceberg/pull/6450#discussion_r1155356581


##########
core/src/main/java/org/apache/iceberg/encryption/KeyMetadata.java:
##########
@@ -0,0 +1,148 @@
+/*
+ * 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 static org.apache.iceberg.types.Types.NestedField.optional;
+import static org.apache.iceberg.types.Types.NestedField.required;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import org.apache.avro.generic.IndexedRecord;
+import org.apache.iceberg.Schema;
+import org.apache.iceberg.avro.AvroSchemaUtil;
+import org.apache.iceberg.data.GenericRecord;
+import org.apache.iceberg.data.Record;
+import org.apache.iceberg.types.Types;
+
+public class KeyMetadata implements EncryptionKeyMetadata, IndexedRecord {
+  private static final ThreadLocal<KeyMetadataEncoder<Record>> ENCODER = new ThreadLocal<>();
+  private static final ThreadLocal<KeyMetadataDecoder<Record>> DECODER = new ThreadLocal<>();
+
+  private static final String encryptionKeyField = "encryption_key";
+  private static final String wrappingKeyIdField = "wrapping_key_id";
+  private static final String aadPrefixField = "aad_prefix";
+
+  static final byte V1 = 1;
+  private static final Schema SCHEMA_V1 =
+      new Schema(
+          required(0, encryptionKeyField, Types.BinaryType.get()),
+          optional(1, wrappingKeyIdField, Types.StringType.get()),
+          optional(2, aadPrefixField, Types.BinaryType.get()));
+
+  private String wrappingKeyId;
+  private ByteBuffer encryptionKey;
+  private ByteBuffer aadPrefix;
+
+  KeyMetadata(ByteBuffer encryptionKey, String wrappingKeyId, ByteBuffer aadPrefix) {
+    this.wrappingKeyId = wrappingKeyId;
+    this.encryptionKey = encryptionKey;
+    this.aadPrefix = aadPrefix;
+  }
+
+  public String wrappingKeyId() {
+    return wrappingKeyId;
+  }
+
+  public ByteBuffer encryptionKey() {
+    return encryptionKey;
+  }
+
+  public ByteBuffer aadPrefix() {
+    return aadPrefix;
+  }
+
+  public static KeyMetadata parse(ByteBuffer buffer) {
+    KeyMetadataDecoder<Record> decoder = DECODER.get();
+    if (decoder == null) {
+      decoder = new KeyMetadataDecoder<>(V1, SCHEMA_V1);
+      DECODER.set(decoder);
+    }
+    try {
+      Record rec = decoder.decode(buffer);
+      return new KeyMetadata(
+          (ByteBuffer) rec.getField(encryptionKeyField),
+          (String) rec.getField(wrappingKeyIdField),
+          (ByteBuffer) rec.getField(aadPrefixField));
+    } catch (IOException e) {
+      throw new RuntimeException("Failed to parse envelope encryption metadata", e);
+    }
+  }
+
+  @Override
+  public ByteBuffer buffer() {
+    Record rec = GenericRecord.create(SCHEMA_V1.asStruct());
+    rec.setField(encryptionKeyField, encryptionKey);
+    rec.setField(wrappingKeyIdField, wrappingKeyId);
+    rec.setField(aadPrefixField, aadPrefix);

Review Comment:
   This should not copy the record. It should directly encode or decode because this implements `IndexedRecord`



-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org