From bc2890a3966b8c45f71a7919b2b99721529baac4 Mon Sep 17 00:00:00 2001 From: Robert Yokota Date: Wed, 2 Oct 2024 17:06:08 -0700 Subject: [PATCH] Return encrypted value instead of throwing in MetadataEncoderService (#76) (#3316) --- .../encoder/MetadataEncoderService.java | 4 ++- .../encoder/MetadataEncoderServiceTest.java | 26 ++++++++++++++++--- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/core/src/main/java/io/confluent/kafka/schemaregistry/storage/encoder/MetadataEncoderService.java b/core/src/main/java/io/confluent/kafka/schemaregistry/storage/encoder/MetadataEncoderService.java index 1ec887ad102..faa1d89b506 100644 --- a/core/src/main/java/io/confluent/kafka/schemaregistry/storage/encoder/MetadataEncoderService.java +++ b/core/src/main/java/io/confluent/kafka/schemaregistry/storage/encoder/MetadataEncoderService.java @@ -286,7 +286,9 @@ public void decodeMetadata(SchemaValue schema) { byte[] plaintext = aead.decrypt(Base64.getDecoder().decode(value), EMPTY_AAD); return new String(plaintext, StandardCharsets.UTF_8); } catch (GeneralSecurityException e) { - throw new IllegalStateException("Could not encrypt sensitive metadata", e); + log.error("Could not decrypt sensitive metadata for schema {}", schema, e); + // Just return the value as-is if we can't decrypt it + return value; } }); } diff --git a/core/src/test/java/io/confluent/kafka/schemaregistry/storage/encoder/MetadataEncoderServiceTest.java b/core/src/test/java/io/confluent/kafka/schemaregistry/storage/encoder/MetadataEncoderServiceTest.java index 2493c9f8818..e25b9378e7b 100644 --- a/core/src/test/java/io/confluent/kafka/schemaregistry/storage/encoder/MetadataEncoderServiceTest.java +++ b/core/src/test/java/io/confluent/kafka/schemaregistry/storage/encoder/MetadataEncoderServiceTest.java @@ -32,6 +32,8 @@ import java.util.HashMap; import java.util.Map; import java.util.Properties; +import java.util.SortedMap; +import java.util.TreeMap; import org.junit.Test; public class MetadataEncoderServiceTest { @@ -61,10 +63,26 @@ public void testEncoding() throws Exception { assertNotEquals(schema.getMetadata().getProperties().get("sensitive"), "foo"); assertNotNull(schema.getMetadata().getProperties().get(SchemaValue.ENCODED_PROPERTY)); - encoderService.decodeMetadata(schema); - assertEquals(schema.getMetadata().getProperties().get("nonsensitive"), "foo"); + SchemaValue schema2 = new SchemaValue( + "mysubject", null, null, null, null, null, + new io.confluent.kafka.schemaregistry.storage.Metadata( + schema.getMetadata().toMetadataEntity()), null, "true", false); + encoderService.decodeMetadata(schema2); + assertEquals(schema2.getMetadata().getProperties().get("nonsensitive"), "foo"); // the value of "sensitive" is decrypted - assertEquals(schema.getMetadata().getProperties().get("sensitive"), "foo"); - assertNull(schema.getMetadata().getProperties().get(SchemaValue.ENCODED_PROPERTY)); + assertEquals(schema2.getMetadata().getProperties().get("sensitive"), "foo"); + assertNull(schema2.getMetadata().getProperties().get(SchemaValue.ENCODED_PROPERTY)); + + SortedMap badProperties = new TreeMap<>(schema.getMetadata().getProperties()); + badProperties.put("sensitive", "badValue"); + SchemaValue schema3 = new SchemaValue( + "mysubject", null, null, null, null, null, + new io.confluent.kafka.schemaregistry.storage.Metadata( + new Metadata(null, badProperties, Collections.singleton("sensitive"))), null, "true", false); + encoderService.decodeMetadata(schema3); + assertEquals(schema3.getMetadata().getProperties().get("nonsensitive"), "foo"); + // the value of "sensitive" is not decrypted + assertEquals(schema3.getMetadata().getProperties().get("sensitive"), "badValue"); + assertNull(schema3.getMetadata().getProperties().get(SchemaValue.ENCODED_PROPERTY)); } }