From f81293b8b565b2ff5cd4a0273b3626673c70a38a Mon Sep 17 00:00:00 2001 From: Leonid Furman Date: Tue, 1 Dec 2026 16:56:22 -0800 Subject: [PATCH 1/4] Don't fetch public key from the KAS server for the base key scenario. --- .../java/io/opentdf/platform/sdk/Config.java | 12 ++++ .../java/io/opentdf/platform/sdk/Planner.java | 57 +++++++++++++++---- .../io/opentdf/platform/sdk/PlannerTest.java | 38 +++++++++++-- 3 files changed, 93 insertions(+), 14 deletions(-) diff --git a/sdk/src/main/java/io/opentdf/platform/sdk/Config.java b/sdk/src/main/java/io/opentdf/platform/sdk/Config.java index f4dbc4f5a..65f764c0b 100644 --- a/sdk/src/main/java/io/opentdf/platform/sdk/Config.java +++ b/sdk/src/main/java/io/opentdf/platform/sdk/Config.java @@ -54,6 +54,18 @@ public KASInfo clone() { } } + @Override + public boolean equals(Object o) { + if (o == null || getClass() != o.getClass()) return false; + KASInfo kasInfo = (KASInfo) o; + return Objects.equals(URL, kasInfo.URL) && Objects.equals(PublicKey, kasInfo.PublicKey) && Objects.equals(KID, kasInfo.KID) && Objects.equals(Default, kasInfo.Default) && Objects.equals(Algorithm, kasInfo.Algorithm); + } + + @Override + public int hashCode() { + return Objects.hash(URL, PublicKey, KID, Default, Algorithm); + } + @Override public String toString() { var sb = new StringBuilder("KASInfo{"); diff --git a/sdk/src/main/java/io/opentdf/platform/sdk/Planner.java b/sdk/src/main/java/io/opentdf/platform/sdk/Planner.java index 3499cb137..f908bcf0c 100644 --- a/sdk/src/main/java/io/opentdf/platform/sdk/Planner.java +++ b/sdk/src/main/java/io/opentdf/platform/sdk/Planner.java @@ -12,6 +12,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import javax.annotation.Nullable; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; @@ -30,6 +31,8 @@ public class Planner { private final SDK.Services services; private final BiFunction granterFactory; + @Nullable + private SimpleKasKey baseKey; private static final Logger logger = LoggerFactory.getLogger(Planner.class); @@ -65,11 +68,20 @@ Map> getSplits() { } private List getAutoconfigurePlan(SDK.Services services, Config.TDFConfig tdfConfig) { + // Reset base key + // TDF.createTDF(...) builds a new Planner for each TDF, so a stale value can't leak today. + // The reset protects against calling getSplits() twice on the same Planner in the future. + baseKey = null; + + // get list of splits, along with fetching and caching base key if required Autoconfigure.Granter granter = granterFactory.apply(services, tdfConfig); - return granter.getSplits(defaultKases(tdfConfig), Planner::getUUID, () -> Planner.fetchBaseKey(services.wellknown())); + return granter.getSplits(defaultKases(tdfConfig), Planner::getUUID, () -> { + Optional fetched = fetchBaseKey(services.wellknown()); + baseKey = fetched.orElse(null); + return fetched; + }); } - List generatePlanFromProvidedKases(List kases) { if (kases.size() == 1) { var kasInfo = kases.get(0); @@ -174,14 +186,27 @@ Map> resolveKeys(List new ArrayList<>()).add(ki); @@ -202,4 +227,16 @@ static List defaultKases(Config.TDFConfig config) { } return defk.isEmpty() ? allk : defk; } + + /** + * Returns whether a split targets the discovered base key: same KAS URL, and either no KID + * or the base key's KID. Guards against wrapping unrelated splits with the base key. + * @param split the split to check + * @param baseKey the base key from the well-known configuration + * @return true if the split should be wrapped with the base key + */ + private static boolean isBaseKeySplit(Autoconfigure.KeySplitTemplate split, SimpleKasKey baseKey) { + return split.kas.equals(baseKey.getKasUri()) + && (split.kid == null || split.kid.equals(baseKey.getPublicKey().getKid())); + } } diff --git a/sdk/src/test/java/io/opentdf/platform/sdk/PlannerTest.java b/sdk/src/test/java/io/opentdf/platform/sdk/PlannerTest.java index 8d9753df3..712e6f6e3 100644 --- a/sdk/src/test/java/io/opentdf/platform/sdk/PlannerTest.java +++ b/sdk/src/test/java/io/opentdf/platform/sdk/PlannerTest.java @@ -135,10 +135,7 @@ void autoconfigureUsesFetchedBaseKeyForUnmappedAttribute() { Assertions.assertThat(splits).containsOnlyKeys(""); Assertions.assertThat(splits.get("")).containsExactly(resolvedKey); var request = ArgumentCaptor.forClass(Config.KASInfo.class); - Mockito.verify(kas).getPublicKey(request.capture()); - assertThat(request.getValue().URL).isEqualTo("https://example.com/base_key"); - assertThat(request.getValue().KID).isEqualTo("thekid"); - assertThat(request.getValue().Algorithm).isEqualTo("ec:secp256r1"); + Mockito.verify(kas, Mockito.never()).getPublicKey(request.capture()); Mockito.verify(wellknownService).getWellKnownConfigurationBlocking(Mockito.any(), Mockito.anyMap()); } @@ -157,6 +154,39 @@ void autoconfigureFailsWithoutBaseKeyOrDefaultKasForUnmappedAttribute() { Mockito.verify(kas, Mockito.never()).getPublicKey(Mockito.any()); } + @ParameterizedTest + @ValueSource(strings = {"rsa:2048", "rsa:4096", "ec:secp256r1", "ec:secp384r1", "ec:secp521r1"}) + void autoconfigureWrapsWithDiscoveredBaseKeyWithoutCallingKas(String wireAlgorithm) { + var wellknownService = wellknownService(baseKeyConfiguration(structuredBaseKey(wireAlgorithm))); + var kas = Mockito.mock(SDK.KAS.class); + // KAS's default key for the algorithm differs from the base key (GAT-2345: swe-rsa-2048 vs r2) + var kasDefaultKey = new Config.KASInfo(); + kasDefaultKey.URL = "https://example.com/base_key"; + kasDefaultKey.KID = "kas-default-kid"; + kasDefaultKey.Algorithm = wireAlgorithm; + kasDefaultKey.PublicKey = "kas-default-pem"; + Mockito.when(kas.getPublicKey(Mockito.any())).thenReturn(kasDefaultKey); + var services = new FakeServicesBuilder().setWellknownService(wellknownService).setKas(kas).build(); + var config = configWithUnmappedAttribute(); + // A different configured default makes loss of the base key's algorithm observable. + config.wrappingKeyType = KeyType.RSA2048Key; + var planner = new Planner(config, services, Autoconfigure::createGranter); + + var splits = planner.getSplits(); + + Assertions.assertThat(splits).containsOnlyKeys(""); + Assertions.assertThat(splits.get("")).hasSize(1); + var key = splits.get("").get(0); + assertThat(key.URL).isEqualTo("https://example.com/base_key"); + assertThat(key.KID).isEqualTo("thekid"); + assertThat(key.PublicKey).isEqualTo("thepem"); + // the round trip through Algorithm and back must give a string createKeyAccess can parse + assertThat(key.Algorithm).isEqualTo(wireAlgorithm); + assertThat(KeyType.fromString(key.Algorithm).toString()).isEqualTo(wireAlgorithm); + Mockito.verify(kas, Mockito.never()).getPublicKey(Mockito.any()); + Mockito.verify(wellknownService).getWellKnownConfigurationBlocking(Mockito.any(), Mockito.anyMap()); + } + private static Config.TDFConfig configWithUnmappedAttribute() { var attribute = Attribute.newBuilder() .setFqn("https://example.com/attr/classification") From 12bd9c093cf5a922229f61b57c7fcdbdbc7c05f8 Mon Sep 17 00:00:00 2001 From: Leonid Furman Date: Tue, 1 Dec 2026 18:45:38 -0800 Subject: [PATCH 2/4] Removed equals and hashCode methods override from KASInfo class. --- .../main/java/io/opentdf/platform/sdk/Config.java | 12 ------------ .../java/io/opentdf/platform/sdk/PlannerTest.java | 11 ++++++++++- 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/sdk/src/main/java/io/opentdf/platform/sdk/Config.java b/sdk/src/main/java/io/opentdf/platform/sdk/Config.java index 65f764c0b..f4dbc4f5a 100644 --- a/sdk/src/main/java/io/opentdf/platform/sdk/Config.java +++ b/sdk/src/main/java/io/opentdf/platform/sdk/Config.java @@ -54,18 +54,6 @@ public KASInfo clone() { } } - @Override - public boolean equals(Object o) { - if (o == null || getClass() != o.getClass()) return false; - KASInfo kasInfo = (KASInfo) o; - return Objects.equals(URL, kasInfo.URL) && Objects.equals(PublicKey, kasInfo.PublicKey) && Objects.equals(KID, kasInfo.KID) && Objects.equals(Default, kasInfo.Default) && Objects.equals(Algorithm, kasInfo.Algorithm); - } - - @Override - public int hashCode() { - return Objects.hash(URL, PublicKey, KID, Default, Algorithm); - } - @Override public String toString() { var sb = new StringBuilder("KASInfo{"); diff --git a/sdk/src/test/java/io/opentdf/platform/sdk/PlannerTest.java b/sdk/src/test/java/io/opentdf/platform/sdk/PlannerTest.java index 712e6f6e3..53c3b2a52 100644 --- a/sdk/src/test/java/io/opentdf/platform/sdk/PlannerTest.java +++ b/sdk/src/test/java/io/opentdf/platform/sdk/PlannerTest.java @@ -133,7 +133,16 @@ void autoconfigureUsesFetchedBaseKeyForUnmappedAttribute() { var splits = planner.getSplits(); Assertions.assertThat(splits).containsOnlyKeys(""); - Assertions.assertThat(splits.get("")).containsExactly(resolvedKey); + + List kasInfoList = splits.get(""); + Assertions.assertThat(kasInfoList.size()).isEqualTo(1); + + Config.KASInfo kasInfo = kasInfoList.get(0); + Assertions.assertThat(kasInfo.KID.equals(resolvedKey.KID)).isTrue(); + Assertions.assertThat(kasInfo.Algorithm.equals(resolvedKey.Algorithm)).isTrue(); + Assertions.assertThat(kasInfo.PublicKey.equals(resolvedKey.PublicKey)).isTrue(); + Assertions.assertThat(kasInfo.URL.equals(resolvedKey.URL)).isTrue(); + var request = ArgumentCaptor.forClass(Config.KASInfo.class); Mockito.verify(kas, Mockito.never()).getPublicKey(request.capture()); Mockito.verify(wellknownService).getWellKnownConfigurationBlocking(Mockito.any(), Mockito.anyMap()); From 0db94893c74bdcdf6decaf561e626cc8ed90e946 Mon Sep 17 00:00:00 2001 From: Leonid Furman Date: Wed, 2 Dec 2026 07:50:43 -0800 Subject: [PATCH 3/4] Applied SonarCloud suggestions for coding style guidelines. --- .../java/io/opentdf/platform/sdk/PlannerTest.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/sdk/src/test/java/io/opentdf/platform/sdk/PlannerTest.java b/sdk/src/test/java/io/opentdf/platform/sdk/PlannerTest.java index 53c3b2a52..c6f0941fb 100644 --- a/sdk/src/test/java/io/opentdf/platform/sdk/PlannerTest.java +++ b/sdk/src/test/java/io/opentdf/platform/sdk/PlannerTest.java @@ -135,13 +135,13 @@ void autoconfigureUsesFetchedBaseKeyForUnmappedAttribute() { Assertions.assertThat(splits).containsOnlyKeys(""); List kasInfoList = splits.get(""); - Assertions.assertThat(kasInfoList.size()).isEqualTo(1); + Assertions.assertThat(kasInfoList).hasSize(1); Config.KASInfo kasInfo = kasInfoList.get(0); - Assertions.assertThat(kasInfo.KID.equals(resolvedKey.KID)).isTrue(); - Assertions.assertThat(kasInfo.Algorithm.equals(resolvedKey.Algorithm)).isTrue(); - Assertions.assertThat(kasInfo.PublicKey.equals(resolvedKey.PublicKey)).isTrue(); - Assertions.assertThat(kasInfo.URL.equals(resolvedKey.URL)).isTrue(); + Assertions.assertThat(kasInfo.KID).isEqualTo(resolvedKey.KID); + Assertions.assertThat(kasInfo.Algorithm).isEqualTo(resolvedKey.Algorithm); + Assertions.assertThat(kasInfo.PublicKey).isEqualTo(resolvedKey.PublicKey); + Assertions.assertThat(kasInfo.URL).isEqualTo(resolvedKey.URL); var request = ArgumentCaptor.forClass(Config.KASInfo.class); Mockito.verify(kas, Mockito.never()).getPublicKey(request.capture()); @@ -191,7 +191,7 @@ void autoconfigureWrapsWithDiscoveredBaseKeyWithoutCallingKas(String wireAlgorit assertThat(key.PublicKey).isEqualTo("thepem"); // the round trip through Algorithm and back must give a string createKeyAccess can parse assertThat(key.Algorithm).isEqualTo(wireAlgorithm); - assertThat(KeyType.fromString(key.Algorithm).toString()).isEqualTo(wireAlgorithm); + assertThat(KeyType.fromString(key.Algorithm)).hasToString(wireAlgorithm); Mockito.verify(kas, Mockito.never()).getPublicKey(Mockito.any()); Mockito.verify(wellknownService).getWellKnownConfigurationBlocking(Mockito.any(), Mockito.anyMap()); } From 30a64234721f8a2e90bae4b7c1017d81c8ffe853 Mon Sep 17 00:00:00 2001 From: Leonid Furman Date: Wed, 2 Dec 2026 13:53:57 -0800 Subject: [PATCH 4/4] Use the discovered base key's PEM for base-key splits, passing it through a new Plan object instead of caching baseKey. Also added more regression tests. --- .../java/io/opentdf/platform/sdk/Planner.java | 60 ++++---- .../io/opentdf/platform/sdk/PlannerTest.java | 132 +++++++++++++----- 2 files changed, 133 insertions(+), 59 deletions(-) diff --git a/sdk/src/main/java/io/opentdf/platform/sdk/Planner.java b/sdk/src/main/java/io/opentdf/platform/sdk/Planner.java index f908bcf0c..0dc287a1c 100644 --- a/sdk/src/main/java/io/opentdf/platform/sdk/Planner.java +++ b/sdk/src/main/java/io/opentdf/platform/sdk/Planner.java @@ -21,6 +21,7 @@ import java.util.Objects; import java.util.Optional; import java.util.UUID; +import java.util.concurrent.atomic.AtomicReference; import java.util.function.BiFunction; import java.util.stream.Collectors; @@ -30,10 +31,6 @@ public class Planner { private final Config.TDFConfig tdfConfig; private final SDK.Services services; private final BiFunction granterFactory; - - @Nullable - private SimpleKasKey baseKey; - private static final Logger logger = LoggerFactory.getLogger(Planner.class); Planner(Config.TDFConfig config, SDK.Services services, BiFunction granterFactory) { @@ -48,11 +45,14 @@ private static String getUUID() { Map> getSplits() { List splitPlan; + SimpleKasKey baseKey = null; if (tdfConfig.autoconfigure) { if (tdfConfig.splitPlan != null && !tdfConfig.splitPlan.isEmpty()) { throw new IllegalArgumentException("cannot use autoconfigure with a split plan provided in the TDFConfig"); } - splitPlan = getAutoconfigurePlan(services, tdfConfig); + Plan plan = getAutoconfigurePlan(services, tdfConfig); + splitPlan = plan.splits; + baseKey = plan.baseKey; } else if (tdfConfig.splitPlan == null || tdfConfig.splitPlan.isEmpty()) { splitPlan = generatePlanFromProvidedKases(tdfConfig.kasInfoList); } else { @@ -64,22 +64,22 @@ Map> getSplits() { if (splitPlan.isEmpty()) { throw new SDK.KasInfoMissing("no plan was constructed via autoconfigure, explicit split plan or provided kases"); } - return resolveKeys(splitPlan); + return resolveKeys(splitPlan, baseKey); } - private List getAutoconfigurePlan(SDK.Services services, Config.TDFConfig tdfConfig) { - // Reset base key - // TDF.createTDF(...) builds a new Planner for each TDF, so a stale value can't leak today. - // The reset protects against calling getSplits() twice on the same Planner in the future. - baseKey = null; - - // get list of splits, along with fetching and caching base key if required + private Plan getAutoconfigurePlan(SDK.Services services, Config.TDFConfig tdfConfig) { Autoconfigure.Granter granter = granterFactory.apply(services, tdfConfig); - return granter.getSplits(defaultKases(tdfConfig), Planner::getUUID, () -> { + + // The granter calls the supplier only if it needs the base key, so this stays null otherwise. + var fetchedBaseKey = new AtomicReference(); + + var splits = granter.getSplits(defaultKases(tdfConfig), Planner::getUUID, () -> { Optional fetched = fetchBaseKey(services.wellknown()); - baseKey = fetched.orElse(null); + fetchedBaseKey.set(fetched.orElse(null)); return fetched; }); + + return new Plan(splits, fetchedBaseKey.get()); } List generatePlanFromProvidedKases(List kases) { @@ -152,10 +152,10 @@ static Optional fetchBaseKey(WellKnownServiceClientInterface wellk .setKasUri(kasUri) .setPublicKey( SimpleKasPublicKey.newBuilder() - .setKid(kid) - .setAlgorithm(algorithm) - .setPem(pem) - .build()) + .setKid(kid) + .setAlgorithm(algorithm) + .setPem(pem) + .build()) .build()); } @@ -171,7 +171,7 @@ private static boolean isMissing(String value) { return value == null || value.isBlank(); } - Map> resolveKeys(List splitPlan) { + Map> resolveKeys(List splitPlan, @Nullable SimpleKasKey baseKey) { Map> conjunction = new HashMap<>(); var latestKASInfo = new HashMap(); // Seed anything passed in manually @@ -229,14 +229,26 @@ static List defaultKases(Config.TDFConfig config) { } /** - * Returns whether a split targets the discovered base key: same KAS URL, and either no KID - * or the base key's KID. Guards against wrapping unrelated splits with the base key. + * Returns whether a split targets the discovered base key: same KAS URL and the base key's KID. + * Guards against wrapping unrelated splits with the base key. * @param split the split to check * @param baseKey the base key from the well-known configuration * @return true if the split should be wrapped with the base key */ private static boolean isBaseKeySplit(Autoconfigure.KeySplitTemplate split, SimpleKasKey baseKey) { return split.kas.equals(baseKey.getKasUri()) - && (split.kid == null || split.kid.equals(baseKey.getPublicKey().getKid())); + && baseKey.getPublicKey().getKid().equals(split.kid); + } + + private static final class Plan { + final List splits; + + @Nullable + final SimpleKasKey baseKey; + + Plan(List splits, @Nullable SimpleKasKey baseKey) { + this.splits = splits; + this.baseKey = baseKey; + } } -} +} \ No newline at end of file diff --git a/sdk/src/test/java/io/opentdf/platform/sdk/PlannerTest.java b/sdk/src/test/java/io/opentdf/platform/sdk/PlannerTest.java index c6f0941fb..5e616ea40 100644 --- a/sdk/src/test/java/io/opentdf/platform/sdk/PlannerTest.java +++ b/sdk/src/test/java/io/opentdf/platform/sdk/PlannerTest.java @@ -7,6 +7,8 @@ import io.opentdf.platform.policy.Algorithm; import io.opentdf.platform.policy.Attribute; import io.opentdf.platform.policy.AttributeRuleTypeEnum; +import io.opentdf.platform.policy.SimpleKasKey; +import io.opentdf.platform.policy.SimpleKasPublicKey; import io.opentdf.platform.wellknownconfiguration.GetWellKnownConfigurationResponse; import io.opentdf.platform.wellknownconfiguration.WellKnownServiceClientInterface; import org.assertj.core.api.Assertions; @@ -114,40 +116,6 @@ void fetchBaseKeyRejectsUnsupportedAlgorithm(String algorithm) { assertThat(Planner.fetchBaseKey(wellknownService)).isEmpty(); } - @Test - void autoconfigureUsesFetchedBaseKeyForUnmappedAttribute() { - var wellknownService = wellknownService(baseKeyConfiguration(structuredBaseKey("ec:secp256r1"))); - var kas = Mockito.mock(SDK.KAS.class); - var resolvedKey = new Config.KASInfo(); - resolvedKey.URL = "https://example.com/base_key"; - resolvedKey.KID = "thekid"; - resolvedKey.Algorithm = "ec:secp256r1"; - resolvedKey.PublicKey = "thepem"; - Mockito.when(kas.getPublicKey(Mockito.any())).thenReturn(resolvedKey); - var services = new FakeServicesBuilder().setWellknownService(wellknownService).setKas(kas).build(); - var config = configWithUnmappedAttribute(); - // A different configured default makes loss of the base key's algorithm observable. - config.wrappingKeyType = KeyType.RSA2048Key; - var planner = new Planner(config, services, Autoconfigure::createGranter); - - var splits = planner.getSplits(); - - Assertions.assertThat(splits).containsOnlyKeys(""); - - List kasInfoList = splits.get(""); - Assertions.assertThat(kasInfoList).hasSize(1); - - Config.KASInfo kasInfo = kasInfoList.get(0); - Assertions.assertThat(kasInfo.KID).isEqualTo(resolvedKey.KID); - Assertions.assertThat(kasInfo.Algorithm).isEqualTo(resolvedKey.Algorithm); - Assertions.assertThat(kasInfo.PublicKey).isEqualTo(resolvedKey.PublicKey); - Assertions.assertThat(kasInfo.URL).isEqualTo(resolvedKey.URL); - - var request = ArgumentCaptor.forClass(Config.KASInfo.class); - Mockito.verify(kas, Mockito.never()).getPublicKey(request.capture()); - Mockito.verify(wellknownService).getWellKnownConfigurationBlocking(Mockito.any(), Mockito.anyMap()); - } - @Test void autoconfigureFailsWithoutBaseKeyOrDefaultKasForUnmappedAttribute() { var kas = Mockito.mock(SDK.KAS.class); @@ -196,6 +164,100 @@ void autoconfigureWrapsWithDiscoveredBaseKeyWithoutCallingKas(String wireAlgorit Mockito.verify(wellknownService).getWellKnownConfigurationBlocking(Mockito.any(), Mockito.anyMap()); } + @Test + void resolveKeysUsesBaseKeyForMatchingSplit() { + var kas = Mockito.mock(SDK.KAS.class); + var config = new Config.TDFConfig(); + config.wrappingKeyType = KeyType.RSA2048Key; + var planner = plannerWithKas(config, kas); + var plan = List.of(new Autoconfigure.KeySplitTemplate( + "https://example.com/base_key", "", "thekid", KeyType.EC256Key)); + + Map> resolved = planner.resolveKeys(plan, baseKey()); + + Assertions.assertThat(resolved).containsOnlyKeys(""); + Assertions.assertThat(resolved.get("")).hasSize(1); + var kasInfo = resolved.get("").get(0); + Assertions.assertThat(kasInfo.URL).isEqualTo("https://example.com/base_key"); + Assertions.assertThat(kasInfo.KID).isEqualTo("thekid"); + Assertions.assertThat(kasInfo.PublicKey).isEqualTo("thepem"); + Assertions.assertThat(kasInfo.Algorithm).isEqualTo("ec:secp256r1"); + Mockito.verify(kas, Mockito.never()).getPublicKey(Mockito.any()); + } + + @ParameterizedTest + @CsvSource({ + "https://example.com/base_key, mapped-kid", + "https://example.com/base_key, ", + "https://other.example.com, thekid" + }) + void resolveKeysUsesKasForNonBaseKeySplit(String kasUrl, String kid) { + var kas = Mockito.mock(SDK.KAS.class); + Mockito.when(kas.getPublicKey(Mockito.any())).thenAnswer(invocation -> { + Config.KASInfo req = invocation.getArgument(0); + var ret = new Config.KASInfo(); + ret.URL = req.URL; + ret.KID = req.KID == null ? "kas-default-kid" : req.KID; + ret.Algorithm = req.Algorithm; + ret.PublicKey = "kas-pem"; + return ret; + }); + var config = new Config.TDFConfig(); + config.wrappingKeyType = KeyType.RSA2048Key; + var planner = plannerWithKas(config, kas); + var plan = List.of(new Autoconfigure.KeySplitTemplate(kasUrl, "split1", kid, null)); + + Map> resolved = planner.resolveKeys(plan, baseKey()); + + var request = ArgumentCaptor.forClass(Config.KASInfo.class); + Mockito.verify(kas).getPublicKey(request.capture()); + Assertions.assertThat(request.getValue().URL).isEqualTo(kasUrl); + Assertions.assertThat(request.getValue().KID).isEqualTo(kid); + + Assertions.assertThat(resolved).containsOnlyKeys("split1"); + var kasInfo = resolved.get("split1").get(0); + Assertions.assertThat(kasInfo.PublicKey).isEqualTo("kas-pem"); + Assertions.assertThat(kasInfo.PublicKey).isNotEqualTo("thepem"); + } + + @Test + void resolveKeysUsesBaseKeyWhenSeededEntryHasDifferentKid() { + var kas = Mockito.mock(SDK.KAS.class); + var stale = new Config.KASInfo(); + stale.URL = "https://example.com/base_key"; + stale.KID = "old-kid"; + stale.Algorithm = "ec:secp256r1"; + stale.PublicKey = "old-pem"; + var config = new Config.TDFConfig(); + config.kasInfoList.add(stale); + var planner = plannerWithKas(config, kas); + var plan = List.of(new Autoconfigure.KeySplitTemplate( + "https://example.com/base_key", "", "thekid", KeyType.EC256Key)); + + Map> resolved = planner.resolveKeys(plan, baseKey()); + + var kasInfo = resolved.get("").get(0); + Assertions.assertThat(kasInfo.KID).isEqualTo("thekid"); + Assertions.assertThat(kasInfo.PublicKey).isEqualTo("thepem"); + Mockito.verify(kas, Mockito.never()).getPublicKey(Mockito.any()); + } + + private static SimpleKasKey baseKey() { + return SimpleKasKey.newBuilder() + .setKasUri("https://example.com/base_key") + .setPublicKey(SimpleKasPublicKey.newBuilder() + .setKid("thekid") + .setAlgorithm(Algorithm.ALGORITHM_EC_P256) + .setPem("thepem") + .build()) + .build(); + } + + private static Planner plannerWithKas(Config.TDFConfig config, SDK.KAS kas) { + return new Planner(config, new FakeServicesBuilder().setKas(kas).build(), + (ignore1, ignored2) -> { throw new IllegalArgumentException("no granter needed"); }); + } + private static Config.TDFConfig configWithUnmappedAttribute() { var attribute = Attribute.newBuilder() .setFqn("https://example.com/attr/classification") @@ -331,7 +393,7 @@ void testFillingInKeysWithAutoConfigure() { new Autoconfigure.KeySplitTemplate("https://kas2.example.com", "split2", "kid2", null), new Autoconfigure.KeySplitTemplate("https://kas3.example.com", "split2", null, KeyType.EC384Key) ); - Map> filledInPlan = planner.resolveKeys(plan); + Map> filledInPlan = planner.resolveKeys(plan, null); assertThat(filledInPlan.keySet().stream().collect(Collectors.toList())).asList().containsExactlyInAnyOrder("split1", "split2"); assertThat(filledInPlan.get("split1")).asList().hasSize(2); var kasInfo1 = filledInPlan.get("split1").stream().filter(k -> "kid1".equals(k.KID)).findFirst().get();