Skip to content

fix(sdk,policy): skip grant table entry when mapped key has empty PEM/KID - #3827

Open
timdumm wants to merge 1 commit into
opentdf:mainfrom
timdumm:fix/rsa4096-partner-key-grant-mapping
Open

fix(sdk,policy): skip grant table entry when mapped key has empty PEM/KID#3827
timdumm wants to merge 1 commit into
opentdf:mainfrom
timdumm:fix/rsa4096-partner-key-grant-mapping

Conversation

@timdumm

@timdumm timdumm commented Aug 3, 2026

Copy link
Copy Markdown

Summary

  • SDK fix (granter.go): When addMappedKey() rejects a SimpleKasKey due to missing PEM or KID, skip adding the KAS URI to the grant table instead of adding it unconditionally. This prevents prepareManifest() from falling back to dialing unreachable partner KAS URIs.
  • Platform fix (grant_mappings.go): Validate that PEM and KID are non-empty before including keys in SimpleKasKey responses. Skip keys with missing material instead of aborting all grant mapping.
  • Log level upgraded from Debug to Warn for missing key material — this is a configuration issue operators need to see.

Context

When encrypting with RSA 4096 partner KAS keys, the clib SDK falls back to fetching the public key from the partner KAS URI (e.g. https://partner.example.com), which is a logical identifier not a reachable endpoint. This causes DNS failures and dropped email attachments in the Exchange MTA.

Root cause: addAllGrants() unconditionally added the KAS URI to the grant table even after addMappedKey() rejected the key. Later, prepareManifest() saw the URI with no cached key and tried to dial it.

Fixes: PEP-5140, PEP-4333

Test plan

  • go test ./sdk/... -run "TestReasoner|TestConfiguration|TestAttribute" — PASS
  • go test ./service/policy/db/... -run TestMapKasKeysToGrants — PASS (updated test expectation for nil public key: skip instead of error)

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Invalid or incomplete public-key data is now skipped instead of preventing other valid grants from being processed.
    • Clear warnings are logged when a KAS grant cannot be added, improving visibility into skipped grants.

@timdumm
timdumm requested review from a team as code owners August 3, 2026 19:57
@github-actions github-actions Bot added comp:db DB component comp:policy Policy Configuration ( attributes, subject mappings, resource mappings, kas registry) comp:sdk A software development kit, including library, for client applications and inter-service communicati size/s labels Aug 3, 2026
@coderabbitai

coderabbitai Bot commented Aug 3, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

Mapped KAS keys with incomplete public-key data or grant creation failures are now logged and skipped. Valid keys continue through grant mapping. The nil public-key test now expects successful mapping without an error.

Changes

Mapped KAS grant handling

Layer / File(s) Summary
Skip invalid mapped grants
service/policy/db/grant_mappings.go, sdk/granter.go, service/policy/db/grant_mappings_test.go
Missing key information and mapped-key grant failures now generate warnings and skip the affected KAS grant. Grant state is updated only after successful addition. The nil public-key test now expects no error.

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

Possibly related PRs

Suggested reviewers: elizabethhealy

Poem

A rabbit checks each grant in line,
Broken keys skip by design.
Warnings hop through logs so bright,
Valid grants proceed just right.
Nil keys no longer stop the flight.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly describes the primary change: skipping grant table entries when mapped keys have empty PEM or KID values.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
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.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
service/policy/db/grant_mappings_test.go (1)

137-143: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Add regression cases for each missing-material branch.

This test covers only PublicKey == nil. Add separate cases for empty PEM and empty KID. Add a mixed case with one invalid key and one valid key to verify that invalid keys are skipped and valid keys still create grants.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@service/policy/db/grant_mappings_test.go` around lines 137 - 143, Add
regression cases alongside the existing “key with nil public key” case for an
empty PEM and an empty KID. Also add a mixed-key case containing one invalid key
and one valid key, asserting the invalid key is skipped while the valid key
still produces its grant and no error is returned.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@sdk/granter.go`:
- Around line 340-345: In the mapped-key handling flow around addMappedKey, move
the r.typ and result = mappedFound assignments to after addMappedKey returns
successfully. Ensure failed additions continue without marking mappedFound,
allowing the subsequent ag.GetGrants() fallback to run when no mapped key was
added.

---

Outside diff comments:
In `@service/policy/db/grant_mappings_test.go`:
- Around line 137-143: Add regression cases alongside the existing “key with nil
public key” case for an empty PEM and an empty KID. Also add a mixed-key case
containing one invalid key and one valid key, asserting the invalid key is
skipped while the valid key still produces its grant and no error is returned.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: fdbea4be-793c-4fb3-8a8d-17c1969b5d30

📥 Commits

Reviewing files that changed from the base of the PR and between 2619a40 and e680ed5.

📒 Files selected for processing (3)
  • sdk/granter.go
  • service/policy/db/grant_mappings.go
  • service/policy/db/grant_mappings_test.go

Comment thread sdk/granter.go
…/KID

When addMappedKey() rejects a SimpleKasKey due to missing PEM or KID,
the KAS URI was still unconditionally added to the grant table. This
caused prepareManifest() to attempt fetching the public key from the
KAS URI, which for partner keys is a logical identifier (e.g.
https://partner.example.com) not a reachable endpoint — resulting in
DNS failures and dropped attachments.

SDK fix (granter.go):
- Skip grant table addition when addMappedKey() fails
- Upgrade log from Debug to Warn for visibility

Platform fix (grant_mappings.go):
- Validate PEM and KID are non-empty before including in SimpleKasKey
- Skip keys with missing material instead of aborting all grant mapping

Fixes: PEP-5140, PEP-4333

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@timdumm
timdumm force-pushed the fix/rsa4096-partner-key-grant-mapping branch from e680ed5 to e49dc1c Compare August 4, 2026 21:32
@coderabbitai

coderabbitai Bot commented Aug 4, 2026

Copy link
Copy Markdown

Note

GitHub couldn't provide a complete incremental comparison for this pull request, so CodeRabbit is performing a full review instead. This review may take a little longer.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@service/policy/db/grant_mappings_test.go`:
- Line 143: Add table-driven cases in the relevant grant-mapping test alongside
the existing PublicKey == nil case for empty PEM and empty KID inputs. Assert
wantErr is false and that no grant is generated for each case, reusing the
existing test fixtures and assertion pattern.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 80e7852e-4254-4c3d-af7a-2c9e604da025

📥 Commits

Reviewing files that changed from the base of the PR and between a85bc64 and e49dc1c.

📒 Files selected for processing (3)
  • sdk/granter.go
  • service/policy/db/grant_mappings.go
  • service/policy/db/grant_mappings_test.go

expectedGrants: []*policy.KeyAccessServer{},
wantErr: true,
errContains: "kas key info is nil for a key with kas uri http://kas1.example.com",
wantErr: false,

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Add coverage for empty PEM and empty KID.

This case covers only PublicKey == nil. Add one table case for an empty PEM and one for an empty KID. Assert no error and no generated grant in both cases.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@service/policy/db/grant_mappings_test.go` at line 143, Add table-driven cases
in the relevant grant-mapping test alongside the existing PublicKey == nil case
for empty PEM and empty KID inputs. Assert wantErr is false and that no grant is
generated for each case, reusing the existing test fixtures and assertion
pattern.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp:db DB component comp:policy Policy Configuration ( attributes, subject mappings, resource mappings, kas registry) comp:sdk A software development kit, including library, for client applications and inter-service communicati size/s

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants