Skip to content

feat(sdk): add WithPolicyFrom re-wrap helper#3476

Merged
marythought merged 3 commits into
mainfrom
demo/dspx-2603-tdf-helpers
May 21, 2026
Merged

feat(sdk): add WithPolicyFrom re-wrap helper#3476
marythought merged 3 commits into
mainfrom
demo/dspx-2603-tdf-helpers

Conversation

@marythought
Copy link
Copy Markdown
Contributor

@marythought marythought commented May 14, 2026

Summary

Adds sdk.WithPolicyFrom(r *Reader) TDFOption — a builder that binds the source TDF's policy (its attribute value FQNs) to a new TDF being created via CreateTDF. Targets re-wrap pipelines where the source policy should carry forward to the output without callers having to handle the manifest's base64 + JSON encoding themselves.

Call site is a single line, matching the existing With* option-builder idiom in this package (e.g., WithDataAttributes, WithKasInformation):

if ok, _ := sdk.IsValidTdf(file); !ok {
    // pass through unchanged
    return
}
reader, _ := s.LoadTDF(file)
_ = reader.Init(ctx)
_, _ = s.CreateTDF(out, transformed, sdk.WithPolicyFrom(reader))

Implementation

Thin wrapper over Reader.DataAttributes (which already handles the base64 + JSON decoding) + WithDataAttributes (which already builds the TDF option). The only new logic is the nil Reader check.

Test plan

  • sdk/tdf_rewrap_test.go — unit test for the nil Reader error path
  • go test ./sdk/... -count=1 passes (including TestREADMECodeBlocks)
  • gofumpt clean
  • golangci-lint clean on the changed files
  • Real re-wrap fixture test — follow-up. The helper delegates to two well-tested primitives, so the integration coverage is more useful as part of an end-to-end re-wrap example than as a unit test that constructs a full TDF in-place.

Companion docs

The corresponding tdf.mdx section is drafted on opentdf/docs demo/docs-drift-with-policy-from. That branch was generated by the docs-drift skill mining this function's godoc example verbatim — the doc PR is a demo of that workflow as well as a real doc addition.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • New Features

    • Enable extracting policy attributes from an existing encrypted document and applying them to newly created ones for easier policy reuse.
  • Documentation

    • Added configuration for automated docs discovery and mapping to keep SDK and RPC docs in sync.
  • Tests

    • Added a unit test ensuring an error is returned when attempting to extract policy from a nil source.

Review Change Stack

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 14, 2026

Warning

Rate limit exceeded

@marythought has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 25 minutes and 43 seconds before requesting another review.

You’ve run out of usage credits. Purchase more in the billing tab.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: a3886ef3-01a3-4550-9176-a84c64748a39

📥 Commits

Reviewing files that changed from the base of the PR and between 283cb8e and 0949988.

📒 Files selected for processing (3)
  • .docs-drift.yaml
  • sdk/tdf.go
  • sdk/tdf_rewrap_test.go
📝 Walkthrough

Walkthrough

This PR adds a new WithPolicyFrom TDFOption to the SDK that extracts policy attributes from a source Reader, applies them to a new TDF configuration, and establishes documentation routing for both the new function and related SDK symbols via a docs-drift configuration file.

Changes

Policy Extraction Feature and Documentation

Layer / File(s) Summary
WithPolicyFrom TDFOption Implementation and Test
sdk/tdf.go, sdk/tdf_rewrap_test.go
Exports WithPolicyFrom(r *Reader) TDFOption that extracts attribute FQNs from a source Reader via Reader.DataAttributes(), validates non-nil Reader input, and delegates to WithDataAttributes(). Test verifies nil Reader rejection with appropriate error message.
Documentation Routing Configuration
.docs-drift.yaml
Configures the docs-drift skill to scan SDK Go and proto sources, defines exclusion filters for generated/internal code, establishes glob-pattern-based mapping rules with optional section anchors, and populates mappings for constructors, discovery/validation helpers, TDF mechanics, platform-client initialization options, policy/enum aliases, and per-service RPC routes.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰 A Reader extracts its secrets fine,
Policy attributes, oh so divine,
WithPolicyFrom weaves them anew,
And docs-drift maps them all through,
From SDK source to markdown true! 📚✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 33.33% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'feat(sdk): add WithPolicyFrom re-wrap helper' clearly and concisely describes the main change: adding a new WithPolicyFrom convenience function for SDK re-wrap operations.
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.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch demo/dspx-2603-tdf-helpers

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 and usage tips.

@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request adds a convenience builder to the SDK that streamlines the process of carrying over TDF policies during re-wrap operations. By abstracting the extraction and application of data attributes, it reduces boilerplate code for developers and ensures consistent policy propagation without manual manifest handling.

Highlights

  • New Helper Function: Introduced WithPolicyFrom(r *Reader) TDFOption to simplify re-wrap pipelines by binding the source TDF's policy to a new TDF.
  • Error Handling: Added a safety check for nil readers within the new helper function to prevent runtime panics.
  • Testing: Added a new unit test in sdk/tdf_rewrap_test.go to verify error handling for nil reader inputs.
New Features

🧠 You can now enable Memory (public preview) to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize the Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counterproductive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.


A helper to bind what we know, To let the new policy flow. With reader in hand, Across all the land, Our TDFs ready to go.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@github-actions github-actions Bot added comp:sdk A software development kit, including library, for client applications and inter-service communicati size/s labels May 14, 2026
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces the WithPolicyFrom function to the SDK, enabling the extraction and application of data attributes from an existing TDF reader to a new TDF configuration. It also includes a unit test to verify error handling for nil readers. Feedback from the reviewer indicates that the documentation incorrectly mandates calling Reader.Init before using this function, which would cause unnecessary network overhead. Additionally, the reviewer provided a suggestion to fix unsafe error handling in the documentation's example code.

Comment thread sdk/tdf.go Outdated
Comment thread sdk/tdf.go Outdated
@github-actions
Copy link
Copy Markdown
Contributor

Benchmark results, click to expand

Benchmark authorization.GetDecisions Results:

Metric Value
Approved Decision Requests 1000
Denied Decision Requests 0
Total Time 172.931801ms

Benchmark authorization.v2.GetMultiResourceDecision Results:

Metric Value
Approved Decision Requests 1000
Denied Decision Requests 0
Total Time 94.476995ms

Benchmark Statistics

Name № Requests Avg Duration Min Duration Max Duration

Bulk Benchmark Results

Metric Value
Total Decrypts 100
Successful Decrypts 100
Failed Decrypts 0
Total Time 408.079134ms
Throughput 245.05 requests/second

TDF3 Benchmark Results:

Metric Value
Total Requests 5000
Successful Requests 5000
Failed Requests 0
Concurrent Requests 50
Total Time 43.534167643s
Average Latency 433.46277ms
Throughput 114.85 requests/second

@github-actions
Copy link
Copy Markdown
Contributor

Benchmark results, click to expand

Benchmark authorization.GetDecisions Results:

Metric Value
Approved Decision Requests 1000
Denied Decision Requests 0
Total Time 182.501204ms

Benchmark authorization.v2.GetMultiResourceDecision Results:

Metric Value
Approved Decision Requests 1000
Denied Decision Requests 0
Total Time 97.085473ms

Benchmark Statistics

Name № Requests Avg Duration Min Duration Max Duration

Bulk Benchmark Results

Metric Value
Total Decrypts 100
Successful Decrypts 100
Failed Decrypts 0
Total Time 421.961009ms
Throughput 236.99 requests/second

TDF3 Benchmark Results:

Metric Value
Total Requests 5000
Successful Requests 5000
Failed Requests 0
Concurrent Requests 50
Total Time 42.554598602s
Average Latency 423.6401ms
Throughput 117.50 requests/second

@marythought marythought marked this pull request as ready for review May 18, 2026 15:23
@marythought marythought requested review from a team as code owners May 18, 2026 15:23
Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

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 `@sdk/tdf.go`:
- Around line 818-820: The doc comment for WithPolicyFrom is misleading by
stating the reader must be initialized via Reader.Init; update the comment to
remove or reword that requirement and instead document that WithPolicyFrom calls
Reader.DataAttributes which reads r.manifest.Policy directly (so Init is not
required). Locate the WithPolicyFrom function and its preceding comment and
replace the Init requirement with a brief note that DataAttributes accesses the
manifest.Policy field and therefore no network/unwrapping via Reader.Init is
necessary.
🪄 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

Run ID: 5fb66014-e03d-4f17-a0d2-427fcf38f853

📥 Commits

Reviewing files that changed from the base of the PR and between 29eff55 and fdd6b11.

📒 Files selected for processing (3)
  • .docs-drift.yaml
  • sdk/tdf.go
  • sdk/tdf_rewrap_test.go

Comment thread sdk/tdf.go Outdated
@github-actions
Copy link
Copy Markdown
Contributor

Benchmark results, click to expand

Benchmark authorization.GetDecisions Results:

Metric Value
Approved Decision Requests 1000
Denied Decision Requests 0
Total Time 183.095356ms

Benchmark authorization.v2.GetMultiResourceDecision Results:

Metric Value
Approved Decision Requests 1000
Denied Decision Requests 0
Total Time 88.652159ms

Benchmark Statistics

Name № Requests Avg Duration Min Duration Max Duration

Bulk Benchmark Results

Metric Value
Total Decrypts 100
Successful Decrypts 100
Failed Decrypts 0
Total Time 443.149848ms
Throughput 225.66 requests/second

TDF3 Benchmark Results:

Metric Value
Total Requests 5000
Successful Requests 5000
Failed Requests 0
Concurrent Requests 50
Total Time 43.871410849s
Average Latency 437.508707ms
Throughput 113.97 requests/second

marythought and others added 3 commits May 19, 2026 09:21
Adds a TDFOption builder that binds the source TDF's policy — its
attribute value FQNs — to a new TDF being created via CreateTDF.
Targets re-wrap pipelines where the source policy should carry forward
without callers handling the manifest's base64+JSON encoding themselves.

Call site is a single line, matching the existing With* option-builder
idiom in this package:

  if ok, _ := sdk.IsValidTdf(file); !ok {
      // pass through unchanged
  }
  reader, _ := s.LoadTDF(file)
  _ = reader.Init(ctx)
  _, _ = s.CreateTDF(out, transformed, sdk.WithPolicyFrom(reader))

Includes a unit test for the nil-Reader error path. Real re-wrap
coverage will come with a TDF fixture test in a follow-up; the helper
delegates to Reader.DataAttributes + WithDataAttributes, both of which
are already well-tested.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Signed-off-by: Mary Dickson <mary.dickson@virtru.com>
Adds a top-level .docs-drift.yaml that documents the contract used by
the docs-drift skill when scanning this repo for SDK changes that
should be reflected in opentdf/docs.

Most of the file mirrors the skill's built-in OpenTDF defaults — those
are committed here so the conventions are discoverable in this repo
rather than hidden in the skill source.

The `mappings:` section adds routing rules the skill can't infer from
file layout alone — e.g., that EntityIdentifier constructor helpers
(ForToken, ForEmail, ForRegisteredResource, etc.) belong in
authorization.mdx#entityidentifier alongside the existing entries, not
in net-new per-symbol files. Without these mappings the skill falls
back to a name-only sniff which produces good-but-imperfect placement.

First-match-wins, so the more-specific patterns are listed first
(e.g., WithPolicyFrom → tdf.mdx wins over the catch-all
With* → platform-client.mdx#initializing-the-sdk-client).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Signed-off-by: Mary Dickson <mary.dickson@virtru.com>
DataAttributes() reads from the manifest, which LoadTDF has already
populated. The previous comment pushed callers into an unnecessary KAS
rewrap. Also tightens the example to handle the LoadTDF error.

Signed-off-by: Mary Dickson <mary.dickson@virtru.com>
@marythought marythought force-pushed the demo/dspx-2603-tdf-helpers branch from 283cb8e to 0949988 Compare May 19, 2026 16:22
@github-actions
Copy link
Copy Markdown
Contributor

Benchmark results, click to expand

Benchmark authorization.GetDecisions Results:

Metric Value
Approved Decision Requests 1000
Denied Decision Requests 0
Total Time 193.764655ms

Benchmark authorization.v2.GetMultiResourceDecision Results:

Metric Value
Approved Decision Requests 1000
Denied Decision Requests 0
Total Time 105.116437ms

Benchmark Statistics

Name № Requests Avg Duration Min Duration Max Duration

Bulk Benchmark Results

Metric Value
Total Decrypts 100
Successful Decrypts 100
Failed Decrypts 0
Total Time 419.552505ms
Throughput 238.35 requests/second

TDF3 Benchmark Results:

Metric Value
Total Requests 5000
Successful Requests 5000
Failed Requests 0
Concurrent Requests 50
Total Time 46.210651398s
Average Latency 459.707763ms
Throughput 108.20 requests/second

@github-actions
Copy link
Copy Markdown
Contributor

⚠️ Govulncheck found vulnerabilities ⚠️

The following modules have known vulnerabilities:

  • examples
  • otdfctl
  • sdk
  • service
  • lib/fixtures
  • tests-bdd

See the workflow run for details.

@marythought marythought added this pull request to the merge queue May 21, 2026
Merged via the queue into main with commit baa1403 May 21, 2026
39 checks passed
@marythought marythought deleted the demo/dspx-2603-tdf-helpers branch May 21, 2026 15:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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