Skip to content

Commit

Permalink
DGS-9732 Omit decrypted DEK when registering DEK in IMPORT mode
Browse files Browse the repository at this point in the history
  • Loading branch information
rayokota committed Jan 11, 2024
1 parent 630e805 commit 0bdb81e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
Expand Up @@ -49,6 +49,7 @@
import io.confluent.kafka.schemaregistry.exceptions.SchemaRegistryRequestForwardingException;
import io.confluent.kafka.schemaregistry.exceptions.UnknownLeaderException;
import io.confluent.kafka.schemaregistry.storage.KafkaSchemaRegistry;
import io.confluent.kafka.schemaregistry.storage.Mode;
import io.confluent.kafka.schemaregistry.storage.SchemaRegistry;
import io.confluent.kafka.schemaregistry.utils.JacksonMapper;
import io.confluent.rest.RestConfigException;
Expand Down Expand Up @@ -568,7 +569,10 @@ public DataEncryptionKey createDek(String kekName, CreateDekRequest request)
// Retrieve key with ts set
key = (DataEncryptionKey) keys.get(keyId);
if (kek.isShared()) {
key = generateRawDek(kek, key);
Mode mode = schemaRegistry.getModeInScope(request.getSubject());
if (mode != Mode.IMPORT) {
key = generateRawDek(kek, key);
}
}
return key;
}
Expand Down
Expand Up @@ -17,6 +17,7 @@
import static io.confluent.dekregistry.storage.DekRegistry.X_FORWARD_HEADER;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;

import com.google.common.collect.ImmutableList;
Expand All @@ -43,6 +44,7 @@
import io.confluent.kafka.schemaregistry.encryption.tink.DekFormat;
import io.confluent.kafka.schemaregistry.rest.SchemaRegistryConfig;
import io.confluent.kafka.schemaregistry.storage.KafkaSchemaRegistry;
import io.confluent.kafka.schemaregistry.storage.Mode;
import io.confluent.kafka.schemaregistry.storage.RuleSetHandler;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
Expand Down Expand Up @@ -109,18 +111,25 @@ public io.confluent.kafka.schemaregistry.storage.RuleSet transform(RuleSet ruleS
public void testBasic() throws Exception {
Map<String, String> headers = new HashMap<>();
headers.put("Content-Type", Versions.SCHEMA_REGISTRY_V1_JSON_WEIGHTED);
testBasic(headers);
testBasic(headers, false);
}

@Test
public void testForwarding() throws Exception {
Map<String, String> headers = new HashMap<>();
headers.put("Content-Type", Versions.SCHEMA_REGISTRY_V1_JSON_WEIGHTED);
headers.put(X_FORWARD_HEADER, "false");
testBasic(headers);
testBasic(headers, true);
}

private void testBasic(Map<String, String> headers) throws Exception {
@Test
public void testBasicImport() throws Exception {
Map<String, String> headers = new HashMap<>();
headers.put("Content-Type", Versions.SCHEMA_REGISTRY_V1_JSON_WEIGHTED);
testBasic(headers, true);
}

private void testBasic(Map<String, String> headers, boolean isImport) throws Exception {
String kekName = "kek1";
String kmsType = "test-kms";
String kmsKeyId = "myid";
Expand All @@ -130,6 +139,10 @@ private void testBasic(Map<String, String> headers) throws Exception {
DekFormat algorithm = DekFormat.AES256_GCM;
Kek kek = new Kek(kekName, kmsType, kmsKeyId, null, null, false, null, null);

if (isImport) {
client.setMode("IMPORT");
}

// Create kek
Kek newKek = client.createKek(headers, kekName, kmsType, kmsKeyId, null, null, false, false);
assertEquals(kek, newKek);
Expand Down Expand Up @@ -264,7 +277,11 @@ private void testBasic(Map<String, String> headers) throws Exception {
// Create dek w/o key material, receive both encrypted and decrypted key material
newDek = client.createDek(headers, kekName, subject2, null, algorithm, null, false);
assertNotNull(newDek.getEncryptedKeyMaterial());
assertNotNull(newDek.getKeyMaterial());
if (isImport) {
assertNull(newDek.getKeyMaterial());
} else {
assertNotNull(newDek.getKeyMaterial());
}
assertNotNull(newDek.getTimestamp());

// Create versioned dek
Expand Down Expand Up @@ -388,7 +405,6 @@ private void testBasic(Map<String, String> headers) throws Exception {
assertEquals(DekRegistryErrors.KEY_NOT_FOUND_ERROR_CODE, e.getErrorCode());
}
}

@Test
public void testUnknownKmsType() throws Exception {
String kekName = "kek1";
Expand Down

0 comments on commit 0bdb81e

Please sign in to comment.