Skip to content

fix(sdk): GAT-2345 Inconsistent key is used for TDF wrapping in base key usage scenario - #444

Merged
lfurman merged 4 commits into
mainfrom
fix/GAT-2345-inconsistent-base-key
Dec 3, 2026
Merged

lfurman merged 4 commits into
mainfrom
fix/GAT-2345-inconsistent-base-key

Conversation

@lfurman

@lfurman lfurman commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes a bug where autoconfigure could wrap a TDF with the wrong key when it fell back to the platform's base key:
https://virtru.atlassian.net/jira/browse/GAT-2345

When the attributes have no mapped keys or grants (or no attributes are given), Granter.getSplits falls back to the base key from the well-known configuration. It returns a single split with the base key's KAS URL, KID and algorithm. Planner.resolveKeys then looked up that split's public key with services.kas().getPublicKey(...). On a cache miss, KAS is asked by algorithm only, so it returns its default key for that algorithm, which might not be the base key. For example, KAS returned swe-rsa-2048 while the base key was r2. The TDF was then wrapped with a key that didn't match the KID the planner meant to use.

Changes in Planner:

  • Return the base key with the plan. getAutoconfigurePlan now returns a private Plan object: the splits plus the base key the granter fetched, or null if it never asked for one. The granter calls the base-key supplier only when there are no mapped keys or grants, so the well-known endpoint is still called only when needed. Planner has no mutable state.
  • Use the base key's PEM directly. resolveKeys takes the base key as a @Nullable parameter. When a split needs a public key and matches the base key, its KASInfo is built with Config.KASInfo.fromSimpleKasKey(baseKey) and KAS is not called. This keeps the base key's URL, KID, PEM and algorithm.
  • Add isBaseKeySplit as a defensive check. A split counts as the base key only if its KAS URL equals the base key's KAS URI and its KID equals the base key's KID. Today the granter returns only the base-key split when it uses the base key, but the check makes sure the base key's PEM is never applied to any other split.

Scope

  • Fixed: the base-key fallback path.
  • Mapped keys (planFromAttributes): unchanged. They still resolve through getPublicKey and come back correct because Autoconfigure seeds KASKeyCache with the full key before planning, so the URL + algorithm + KID lookup hits the cache.
  • Grants (planUsingGrants): unchanged. Grants reference keys by KAS URL and algorithm, so fetching from KAS is correct, and the cache saves repeated calls.
  • Provided kasInfoList / explicit split plan: unchanged. No base key is involved, and resolveKeys gets null.

Follow-up: mapped keys rely on the cache having been seeded. A cache miss (TTL expiry, clear(), or a custom SDK.KAS) would fall back to an algorithm-only lookup. Will be tracked in later JIRA tickets / PRs.

Compatibility

  • No public API changes. Plan and isBaseKeySplit are private. The Planner constructor and getSplits() are unchanged. resolveKeys is package-private and gained a @Nullable SimpleKasKey baseKey parameter.
  • Behavior change (the fix): With autoconfigure on and a base key in the well-known config, the base-key split is now wrapped with the real base key, not KAS's default key for the algorithm. The algorithm comes from the base key, not from tdfConfig.wrappingKeyType.
  • One less network call: The base-key split no longer calls KAS's public-key endpoint.
  • No base key configured: fetchBaseKey returns empty, resolveKeys gets null, and behavior is the same as before.

Testing

Updated PlannerTest:

  • Replaced autoconfigureUsesFetchedBaseKeyForUnmappedAttribute, which asserted the old behavior (KAS called for the base-key split), with autoconfigureWrapsWithDiscoveredBaseKeyWithoutCallingKas. It's parameterized over rsa:2048, rsa:4096, ec:secp256r1, ec:secp384r1 and ec:secp521r1:

    • The mocked KAS returns a different default key on the same URL, which reproduces the original bug.
    • wrappingKeyType is RSA2048Key, so losing the base key's algorithm would show up.
    • It asserts the split has the base key's URL, KID, PEM and algorithm, that the algorithm string parses with KeyType.fromString, that KAS is never called, and that the well-known config is read.
  • Added resolveKeys tests with a non-null base key:

    • resolveKeysUsesBaseKeyForMatchingSplit: a matching split resolves to the base key without calling KAS.
    • resolveKeysUsesKasForNonBaseKeySplit: these splits still go through getPublicKey: same KAS with a different KID, same KAS with no KID, and a different KAS with the base key's KID.
    • resolveKeysUsesBaseKeyWhenSeededEntryHasDifferentKid: a kasInfoList entry for the same KAS with a different KID doesn't override the base key.
  • Updated testFillingInKeysWithAutoConfigure to pass null for the new parameter.

    mvn -pl sdk test -Dtest=PlannerTest

@lfurman
lfurman requested review from a team as code owners December 2, 2026 01:12
@coderabbitai

coderabbitai Bot commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Planner carries a discovered base key from autoconfiguration into split resolution. Matching splits use the discovered key when both the KAS URL and KID match. Other splits continue to use KAS public-key lookup.

Changes: Planner base-key resolution

Layer / File(s) Summary
Capture the discovered base key
sdk/src/main/java/io/opentdf/platform/sdk/Planner.java
Autoconfiguration returns the split templates with the optional base key captured when the granter invokes its supplier. The manual split-plan and provided-KAS paths remain unchanged.
Resolve splits and verify key behavior
sdk/src/main/java/io/opentdf/platform/sdk/Planner.java, sdk/src/test/java/io/opentdf/platform/sdk/PlannerTest.java
A split uses the discovered key when both its KAS URL and KID match. Other splits retain KAS public-key lookup. Tests cover RSA and EC algorithms, matching and nonmatching splits, stale KID replacement, and cases without a base key or default KAS.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Sequence Diagram(s)

sequenceDiagram
  participant Planner
  participant GranterCallback
  participant KASService
  Planner->>GranterCallback: Request autoconfiguration split plan
  GranterCallback-->>Planner: Supply split templates and optional base key
  Planner->>Planner: Match split KAS URL and KID to base key
  alt Both values match
    Planner->>Planner: Build KAS information from base key
  else No base key match
    Planner->>KASService: Request public key for split
    KASService-->>Planner: Return public key
  end
Loading

Possibly related PRs

  • opentdf/java-sdk#429: This PR changes fetchBaseKey and the output contract used by the base-key handling in this change.

Suggested reviewers: dmihalcik-virtru, mkleene, elizabethhealy

Merge Risk: Minimal · up to 30a64

This change makes base-key wrapping use the discovered key instead of an algorithm-only KAS lookup that could return a different key. No actionable merge-blocking risk remains in the reviewed files.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the fix (GAT-2345) and the area (sdk). It also names the symptom: an inconsistent key is used for TDF wrapping in the base key scenario. The raw summary shows the change: base-key spli…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Full details: Title check

Explanation

The title names the fix (GAT-2345) and the area (sdk). It also names the symptom: an inconsistent key is used for TDF wrapping in the base key scenario. The raw summary shows the change: base-key splits use the discovered base key directly when KAS URL and KID match, instead of an algorithm-only KAS lookup. The title therefore matches the main change. It is specific enough for a teammate scanning history. Minor note: the title is long (87 characters), but it is still one clear sentence.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

A rabbit checks the KAS URI,
Then matches KID with careful glee.
The base key serves when both align,
Else KAS lookup fills the line.
RSA and EC keys join the play,
While tests keep watch along the way.

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: bb0191f7-6fd8-42d1-a4de-d3c325fea63e
Commits

Reviewing files that changed from the base of the PR and between 0db9489 and 30a6423.

Files selected for processing: 2

  • sdk/src/main/java/io/opentdf/platform/sdk/Planner.java
  • sdk/src/test/java/io/opentdf/platform/sdk/PlannerTest.java

Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.

@github-actions

github-actions Bot commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

@github-actions

github-actions Bot commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

@mkleene mkleene left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that this goes far enough. The main issue is that there are two ways to reference a key.

  • by reference: grants refer to keys by KAS and KeyType. This means that:
    1. We don't have the key material when we resolve an attribute so we need to retrieve it
    2. We should resolve these keys periodically because they can change
  • by value: for base keys and mapped keys we get the key material in its entirety. It does not make sense to try to resolve the key once it has been retrieved
    1. We have no API to do so: we can retrieve from a particular KAS by algorithm but there may be another key with the same algorithm
    2. A KID should be associated with one cryptographic key. it's not a reference to anything. It's an immutable object so retrieving it again should yield the same object

The problem now is that we retrieve both classes of key. This is incorrect since it's impossible to retrieve a key that is provided by value. Only references should be retrieved.

The current pull fixes the issue with the base key but the same thing can happen with mapped keys. We should at the very least write up an issue describing that problem but I'd prefer to fix it now.

@github-actions

github-actions Bot commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

@lfurman

lfurman commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor Author

I don't think that this goes far enough. The main issue is that there are two ways to reference a key.

  • by reference: grants refer to keys by KAS and KeyType. This means that:

    1. We don't have the key material when we resolve an attribute so we need to retrieve it
    2. We should resolve these keys periodically because they can change
  • by value: for base keys and mapped keys we get the key material in its entirety. It does not make sense to try to resolve the key once it has been retrieved

    1. We have no API to do so: we can retrieve from a particular KAS by algorithm but there may be another key with the same algorithm
    2. A KID should be associated with one cryptographic key. it's not a reference to anything. It's an immutable object so retrieving it again should yield the same object

The problem now is that we retrieve both classes of key. This is incorrect since it's impossible to retrieve a key that is provided by value. Only references should be retrieved.

The current pull fixes the issue with the base key but the same thing can happen with mapped keys. We should at the very least write up an issue describing that problem but I'd prefer to fix it now.

Thanks for catching this. I initilly scoped the fix to the base key because mapped keys resolve correctly today, but only because Autoconfigure puts them into KASKeyCache before planning. I agree that correctness should not depend on that. The cache has a 5-minute TTL, a public clear(), and an unsynchronized shared map, and a custom KAS implementation might not use it at all. Any miss falls back to an algorithm-only PublicKey call which would led to the same issue with the wrong-key.

I'll update this PR to pass keys through the plan by value:

  1. Carry the PEM in KeySplitTemplate / PublicKeyInfo
    ◦ filled from mappedKeys in planFromAttributes
    ◦ filled from the base key in the base-key branch of Granter.getSplits
  2. In resolveKeys, use the carried PEM when it's there, and only call getPublicKey when it isn't (grants, default KAS, kasInfoList without a PEM)
  3. Then, we can remove the baseKey field from the Planner class.

As a follow-up, it would be great to deprecate KASKeyCache / getKeyCache() and remove the code that depends on it. Both are public API, so they'd be removed in a later major version.

@mkleene

mkleene commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

I don't think that this goes far enough. The main issue is that there are two ways to reference a key.

  • by reference: grants refer to keys by KAS and KeyType. This means that:

    1. We don't have the key material when we resolve an attribute so we need to retrieve it
    2. We should resolve these keys periodically because they can change
  • by value: for base keys and mapped keys we get the key material in its entirety. It does not make sense to try to resolve the key once it has been retrieved

    1. We have no API to do so: we can retrieve from a particular KAS by algorithm but there may be another key with the same algorithm
    2. A KID should be associated with one cryptographic key. it's not a reference to anything. It's an immutable object so retrieving it again should yield the same object

The problem now is that we retrieve both classes of key. This is incorrect since it's impossible to retrieve a key that is provided by value. Only references should be retrieved.
The current pull fixes the issue with the base key but the same thing can happen with mapped keys. We should at the very least write up an issue describing that problem but I'd prefer to fix it now.

Thanks for catching this. I initilly scoped the fix to the base key because mapped keys resolve correctly today, but only because Autoconfigure puts them into KASKeyCache before planning. I agree that correctness should not depend on that. The cache has a 5-minute TTL, a public clear(), and an unsynchronized shared map, and a custom KAS implementation might not use it at all. Any miss falls back to an algorithm-only PublicKey call which would led to the same issue with the wrong-key.

I'll update this PR to pass keys through the plan by value:

1. Carry the PEM in KeySplitTemplate / PublicKeyInfo
   ◦ filled from mappedKeys in planFromAttributes
   ◦ filled from the base key in the base-key branch of Granter.getSplits

2. In resolveKeys, use the carried PEM when it's there, and only call getPublicKey when it isn't (grants, default KAS, kasInfoList without a PEM)

3. Then, we can remove the baseKey field from the Planner class.

As a follow-up, it would be gret to deprecate KASKeyCache / getKeyCache() and remove the code that depends on it. Both are public API, so they'd be removed in a later major version.

Ah, that's interesting. I guess if we seed the cache then they do resolve correctly. So maybe we just need to comment that? It's a bit confusing I guess but making this minimal change along with some tests to catch regressions might be enough.

…ough a new Plan object instead of caching baseKey. Also added more regression tests.
@sonarqubecloud

sonarqubecloud Bot commented Dec 2, 2026

Copy link
Copy Markdown

@github-actions

github-actions Bot commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

@mkleene mkleene left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that this fixes the immediate problem and when we remove grants we can come back and get rid of the cache and any retrieval of keys over the network which will simplify things

@lfurman
lfurman merged commit 583e501 into main Dec 3, 2026
24 checks passed
@lfurman
lfurman deleted the fix/GAT-2345-inconsistent-base-key branch December 3, 2026 21:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants