xanderbailey commented on code in PR #2340: URL: https://github.com/apache/iceberg-rust/pull/2340#discussion_r3159373213
########## crates/iceberg/src/encryption/key_metadata.rs: ########## @@ -0,0 +1,281 @@ +// 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. + +//! Avro-serialized key metadata format compatible with Java's +//! `org.apache.iceberg.encryption.StandardKeyMetadata`. + +use std::fmt; +use std::io::Cursor; + +use apache_avro::{from_avro_datum, from_value, to_avro_datum, to_value}; + +use super::SensitiveBytes; +use crate::{Error, ErrorKind, Result}; + +/// Standard key metadata for Iceberg table encryption. +/// +/// Contains the Data Encryption Key (DEK), AAD prefix, and optional file +/// length. Byte-compatible with Java's `StandardKeyMetadata` via Avro +/// serialization. +/// +/// Wire format: `[version byte (0x01)] [Avro binary datum]` +#[derive(Clone, PartialEq, Eq)] +pub struct StandardKeyMetadata { + encryption_key: SensitiveBytes, + aad_prefix: Option<Box<[u8]>>, + file_length: Option<u64>, +} + +impl fmt::Debug for StandardKeyMetadata { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("StandardKeyMetadata") + .field("encryption_key", &self.encryption_key) + .field( + "aad_prefix", + &self + .aad_prefix + .as_ref() + .map(|b| format!("[{} bytes]", b.len())), + ) + .field("file_length", &self.file_length) + .finish() + } +} + +impl StandardKeyMetadata { + /// Creates a new `StandardKeyMetadata`. + pub fn new(encryption_key: &[u8]) -> Self { + Self { + encryption_key: SensitiveBytes::new(encryption_key), + aad_prefix: None, + file_length: None, + } + } + + /// Adds an AAD prefix. + pub fn with_aad_prefix(mut self, aad_prefix: &[u8]) -> Self { + self.aad_prefix = Some(aad_prefix.into()); + self + } + + /// Adds a file length. + pub fn with_file_length(mut self, length: u64) -> Self { + self.file_length = Some(length); + self + } + + /// Returns the plaintext Data Encryption Key. + pub fn encryption_key(&self) -> &SensitiveBytes { + &self.encryption_key + } + + /// Returns the AAD prefix. + pub fn aad_prefix(&self) -> Option<&[u8]> { + self.aad_prefix.as_deref() + } + + /// Returns the optional file length. + pub fn file_length(&self) -> Option<u64> { + self.file_length + } + + /// Encodes to Java-compatible format: `[0x01] [Avro binary datum]` + pub fn encode(&self) -> Result<Box<[u8]>> { + let serde_repr = _serde::StandardKeyMetadataV1::from(self); + + let value = to_value(serde_repr) + .and_then(|v| v.resolve(&_serde::AVRO_SCHEMA_V1)) + .map_err(|e| { + Error::new(ErrorKind::Unexpected, "Failed to encode key metadata").with_source(e) + })?; + + let datum = to_avro_datum(&_serde::AVRO_SCHEMA_V1, value).map_err(|e| { + Error::new(ErrorKind::Unexpected, "Failed to encode key metadata").with_source(e) + })?; + + let mut result = Vec::with_capacity(1 + datum.len()); + result.push(_serde::V1); Review Comment: Agreed, moved all serde logic down into `StandardKeyMetadataV1` -- 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]
