diff --git a/hive-metastore/src/main/java/org/apache/iceberg/hive/HiveTableOperations.java b/hive-metastore/src/main/java/org/apache/iceberg/hive/HiveTableOperations.java index a4338ec07a5f..4d382f8d388e 100644 --- a/hive-metastore/src/main/java/org/apache/iceberg/hive/HiveTableOperations.java +++ b/hive-metastore/src/main/java/org/apache/iceberg/hive/HiveTableOperations.java @@ -321,7 +321,14 @@ protected void doCommit(TableMetadata base, TableMetadata metadata) { } if (removedProps.contains(TableProperties.ENCRYPTION_TABLE_KEY)) { - throw new RuntimeException("Cannot remove key in encrypted table"); + throw new IllegalArgumentException("Cannot remove key in encrypted table"); + } + + if (base != null + && !Objects.equals( + base.properties().get(TableProperties.ENCRYPTION_TABLE_KEY), + metadata.properties().get(TableProperties.ENCRYPTION_TABLE_KEY))) { + throw new IllegalArgumentException("Cannot modify key in encrypted table"); } HMSTablePropertyHelper.updateHmsTableForIcebergTable( diff --git a/spark/v4.0/spark/src/test/java/org/apache/iceberg/spark/sql/TestTableEncryption.java b/spark/v4.0/spark/src/test/java/org/apache/iceberg/spark/sql/TestTableEncryption.java index c71bd28706c9..8f0552a37877 100644 --- a/spark/v4.0/spark/src/test/java/org/apache/iceberg/spark/sql/TestTableEncryption.java +++ b/spark/v4.0/spark/src/test/java/org/apache/iceberg/spark/sql/TestTableEncryption.java @@ -169,6 +169,13 @@ public void testKeyDelete() { .hasMessageContaining("Cannot remove key in encrypted table"); } + @TestTemplate + public void testKeyAlter() { + assertThatThrownBy( + () -> sql("ALTER TABLE %s SET TBLPROPERTIES ('encryption.key-id'='abcd')", tableName)) + .hasMessageContaining("Cannot modify key in encrypted table"); + } + @TestTemplate public void testDirectDataFileRead() { List dataFileTable =