Skip to content

feat(policy): add sort support to listkaskeys#3344

Merged
dsm20 merged 10 commits into
mainfrom
add-sort-support-kaskeys-api
Apr 23, 2026
Merged

feat(policy): add sort support to listkaskeys#3344
dsm20 merged 10 commits into
mainfrom
add-sort-support-kaskeys-api

Conversation

@dsm20
Copy link
Copy Markdown
Contributor

@dsm20 dsm20 commented Apr 21, 2026

Proposed Changes

Resolves DSPX-2694

  • Adds strongly-typed sort support to ListKeys RPC
  • Sortable fields: key_id, created_at, updated_at (ASC/DESC), with backward-compatible fallback to created_at DESC

Changes

Protoservice/policy/kasregistry/key_access_server_registry.proto

  • SortKasKeysType enum (UNSPECIFIED, KEY_ID, CREATED_AT, UPDATED_AT)
  • KasKeysSort message (field + direction)
  • repeated KasKeysSort sort = 11 on ListKeysRequest with max_items = 1 constraint
  • Regenerated protos and docs

SQLservice/policy/db/queries/key_access_server_registry.sql

  • CASE WHEN sort blocks in listKeys query for 3 fields (6 blocks total)
  • Fallback kask.created_at DESC

Goservice/policy/db/utils.go + service/policy/db/key_access_server_registry.go

  • GetKasKeysSortParams(): maps enum to SQL-compatible field/direction strings
  • ListKeys handler wired to call mapper and pass params to sqlc query
  • New constant sortFieldKeyID added

Tests

  • 11 unit tests for the enum mapper helper (nil, empty, unspecified, each field + direction, unspecified direction default)
  • 7 integration tests (key_id ASC/DESC, created_at ASC/DESC, updated_at ASC/DESC, unspecified fallback) using createSortTestKasKeys and createKeyIdSortTestKasKeys suite helpers
  • Protovalidate sort constraint test (Test_ListKeysRequest_Sort)

Notes

  • otdfctl --sort flag deferred to a follow-up, consistent with all prior sort PRs
  • Tie-breaker (kask.id ASC) deferred to a follow-up refactoring ticket

Checklist

  • I have added or updated unit tests
  • I have added or updated integration tests (if appropriate)
  • I have added or updated documentation

Summary by CodeRabbit

  • New Features

    • Added sorting capability for key access server key listings—sort by key ID, creation date, or update date in ascending or descending order. Default ordering is by creation date (descending).
  • Documentation

    • Updated API documentation and schema definitions to reflect new sorting parameters and constraints.
  • Tests

    • Added integration and unit tests for sorting behavior validation.

@dsm20 dsm20 requested review from a team as code owners April 21, 2026 15:09
@dsm20 dsm20 marked this pull request as draft April 21, 2026 15:09
@github-actions github-actions Bot added comp:db DB component comp:policy Policy Configuration ( attributes, subject mappings, resource mappings, kas registry) docs Documentation labels Apr 21, 2026
@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 introduces sorting capabilities to the ListKeys RPC, enabling more flexible data retrieval for key access server registries. The changes include updates to the protocol definitions, database query logic, and application-level mapping, ensuring that sorting is both type-safe and performant. The implementation maintains backward compatibility by defaulting to the existing ordering behavior when no sort parameters are provided.

Highlights

  • API Enhancement: Added strongly-typed sort support to the ListKeys RPC, allowing users to order results by key_id, created_at, or updated_at.
  • Database Updates: Updated the SQL query for listing keys to support dynamic sorting using CASE statements, with a default fallback to created_at DESC.
  • Testing: Implemented comprehensive unit and integration tests to verify sorting logic, enum mapping, and protocol validation constraints.

🧠 New Feature in Public Preview: You can now enable Memory 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.

Ignored Files
  • Ignored by pattern: docs/openapi/**/* (4)
    • docs/openapi/authorization/authorization.openapi.yaml
    • docs/openapi/policy/kasregistry/key_access_server_registry.openapi.yaml
    • docs/openapi/policy/objects.openapi.yaml
    • docs/openapi/policy/subjectmapping/subject_mapping.openapi.yaml
  • Ignored by pattern: protocol/**/* (3)
    • protocol/go/authorization/authorization.pb.go
    • protocol/go/policy/kasregistry/key_access_server_registry.pb.go
    • protocol/go/policy/objects.pb.go
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 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 counter productive. 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.


The keys were scattered, hard to find, With no clear order in the mind. Now sorted by ID or by date, They line up straight, they look just great.

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.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 21, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: dc6100b2-25fd-4b6d-82fc-58a58b8d339e

📥 Commits

Reviewing files that changed from the base of the PR and between 32e6de3 and fac5b48.

📒 Files selected for processing (3)
  • docs/grpc/index.html
  • docs/openapi/authorization/authorization.openapi.yaml
  • docs/openapi/policy/kasregistry/key_access_server_registry.openapi.yaml

📝 Walkthrough

Walkthrough

This PR introduces sorting capability to the KAS registry's ListKeys API by adding a new sort parameter type with field and direction specification in proto, mapping sort parameters to SQL through a helper function, and updating the database query to use dynamic ORDER BY clauses based on the provided sort criteria.

Changes

Cohort / File(s) Summary
Proto Definitions
service/policy/kasregistry/key_access_server_registry.proto, service/policy/kasregistry/key_access_server_registry_test.go
Added SortKasKeysType enum (UNSPECIFIED, KEY_ID, CREATED_AT, UPDATED_AT) and KasKeysSort message. Extended ListKeysRequest with optional sort field (max 1 item). Added validation test for max-item constraint.
Database Layer
service/policy/db/key_access_server_registry.go, service/policy/db/key_access_server_registry.sql.go, service/policy/db/queries/key_access_server_registry.sql
Wired sort parameters through ListKeys handler into query execution. Updated listKeysParams struct with SortField and SortDirection. Replaced fixed ORDER BY kask.created_at DESC with dynamic CASE-driven sorting on key_id, created_at, updated_at.
Utilities
service/policy/db/utils.go, service/policy/db/utils_test.go
Added GetKasKeysSortParams helper to map KasKeysSort enum values to SQL column names and directions. Included comprehensive unit tests covering nil/empty inputs, UNSPECIFIED fields, and all sort field/direction combinations.
Integration Tests
service/integration/kas_registry_key_test.go
Added helper functions and test cases for sorting by key_id, created_at, and updated_at in both directions. Includes tests for default ordering behavior when sort is omitted.
Documentation
docs/grpc/index.html, docs/openapi/policy/kasregistry/key_access_server_registry.openapi.yaml, docs/openapi/authorization/authorization.openapi.yaml
Updated OpenAPI and gRPC documentation with new sort types and field descriptions. Clarified date boundary wording for CreatedAt metadata.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~30 minutes

Possibly related PRs

Suggested reviewers

  • c-r33d
  • alkalescent
  • marythought

Poem

🐰 ✨ Sort the keys with care and grace,
By ID, by date—find the perfect place!
Order ascending, descending too,
New SQL CASE makes sorting dreams come true! 🔑

🚥 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 'feat(policy): add sort support to listkaskeys' accurately summarizes the main change: adding sort functionality to the ListKeys RPC, which is the primary objective of this PR.
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.

✏️ 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 add-sort-support-kaskeys-api

Warning

Review ran into problems

🔥 Problems

Timed out fetching pipeline failures after 30000ms


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.

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 sorting capabilities to the ListKeys endpoint in the KAS registry. It adds a new sort field to the ListKeysRequest protobuf message, allowing users to sort by key ID, creation time, or update time in either ascending or descending order. The implementation includes updates to the database layer using dynamic SQL sorting, utility functions for parameter mapping, and comprehensive integration and unit tests. Feedback was provided regarding the SQL ORDER BY clause, specifically suggesting an optimization to avoid redundant sorting criteria when a specific order is explicitly requested.

Comment thread service/policy/db/queries/key_access_server_registry.sql
@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.78565ms

Benchmark authorization.v2.GetMultiResourceDecision Results:

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

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 425.488982ms
Throughput 235.02 requests/second

TDF3 Benchmark Results:

Metric Value
Total Requests 5000
Successful Requests 5000
Failed Requests 0
Concurrent Requests 50
Total Time 45.751038866s
Average Latency 456.218247ms
Throughput 109.29 requests/second

dsm20 added 6 commits April 21, 2026 11:16
also regen SQL and docs
maps sort fields. also included unit test func
same as other listAPIs, tests no sort, 1 item sort, and 2 items (invalid)
two helpers: creation of timestamped keys and named keys to test time-based and name-based sorting respectively. covers key ID, created at, updated at (all ASC/DESC), and default fallback
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 the current code and only fix it if needed.

Inline comments:
In `@service/policy/kasregistry/key_access_server_registry.proto`:
- Around line 80-83: KasKeysSort's enum fields (KasKeysSort.field and
KasKeysSort.direction) lack defined_only validation, allowing numeric
out-of-range values to pass; update the proto message KasKeysSort to annotate
both SortKasKeysType field and policy.SortDirection direction with
[(validate.rules).enum.defined_only = true]; also extend the sort validation
unit test that targets KasKeysSort to include cases with invalid numeric enum
values to assert the validator rejects them.
🪄 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: 16631ee5-459e-4796-b439-8606ab18791a

📥 Commits

Reviewing files that changed from the base of the PR and between 5177bec and 32e6de3.

⛔ Files ignored due to path filters (3)
  • protocol/go/authorization/authorization.pb.go is excluded by !**/*.pb.go
  • protocol/go/policy/kasregistry/key_access_server_registry.pb.go is excluded by !**/*.pb.go
  • protocol/go/policy/objects.pb.go is excluded by !**/*.pb.go
📒 Files selected for processing (13)
  • docs/grpc/index.html
  • docs/openapi/authorization/authorization.openapi.yaml
  • docs/openapi/policy/kasregistry/key_access_server_registry.openapi.yaml
  • docs/openapi/policy/objects.openapi.yaml
  • docs/openapi/policy/subjectmapping/subject_mapping.openapi.yaml
  • service/integration/kas_registry_key_test.go
  • service/policy/db/key_access_server_registry.go
  • service/policy/db/key_access_server_registry.sql.go
  • service/policy/db/queries/key_access_server_registry.sql
  • service/policy/db/utils.go
  • service/policy/db/utils_test.go
  • service/policy/kasregistry/key_access_server_registry.proto
  • service/policy/kasregistry/key_access_server_registry_test.go
💤 Files with no reviewable changes (3)
  • docs/openapi/policy/subjectmapping/subject_mapping.openapi.yaml
  • docs/openapi/authorization/authorization.openapi.yaml
  • docs/openapi/policy/objects.openapi.yaml

Comment thread service/policy/kasregistry/key_access_server_registry.proto
@dsm20 dsm20 force-pushed the add-sort-support-kaskeys-api branch from 32e6de3 to 222b85f Compare April 21, 2026 15:17
@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 142.016119ms

Benchmark authorization.v2.GetMultiResourceDecision Results:

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

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 333.838608ms
Throughput 299.55 requests/second

TDF3 Benchmark Results:

Metric Value
Total Requests 5000
Successful Requests 5000
Failed Requests 0
Concurrent Requests 50
Total Time 33.490672343s
Average Latency 333.728399ms
Throughput 149.30 requests/second

had buf 1.68.1 generating docs, wasn't matching CI version. regenerated with correct version
@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 185.521125ms

Benchmark authorization.v2.GetMultiResourceDecision Results:

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

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 400.307622ms
Throughput 249.81 requests/second

TDF3 Benchmark Results:

Metric Value
Total Requests 5000
Successful Requests 5000
Failed Requests 0
Concurrent Requests 50
Total Time 43.023599285s
Average Latency 428.017542ms
Throughput 116.22 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 200.916914ms

Benchmark authorization.v2.GetMultiResourceDecision Results:

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

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 427.085843ms
Throughput 234.14 requests/second

TDF3 Benchmark Results:

Metric Value
Total Requests 5000
Successful Requests 5000
Failed Requests 0
Concurrent Requests 50
Total Time 45.437555159s
Average Latency 452.331922ms
Throughput 110.04 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 210.776811ms

Benchmark authorization.v2.GetMultiResourceDecision Results:

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

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 405.007007ms
Throughput 246.91 requests/second

TDF3 Benchmark Results:

Metric Value
Total Requests 5000
Successful Requests 5000
Failed Requests 0
Concurrent Requests 50
Total Time 43.967979834s
Average Latency 438.502837ms
Throughput 113.72 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 143.888711ms

Benchmark authorization.v2.GetMultiResourceDecision Results:

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

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 392.507037ms
Throughput 254.77 requests/second

TDF3 Benchmark Results:

Metric Value
Total Requests 5000
Successful Requests 5000
Failed Requests 0
Concurrent Requests 50
Total Time 41.669959228s
Average Latency 415.179042ms
Throughput 119.99 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.

@dsm20 dsm20 marked this pull request as ready for review April 21, 2026 19:55
Copy link
Copy Markdown
Member

@elizabethhealy elizabethhealy left a comment

Choose a reason for hiding this comment

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

lgtm!

@dsm20 dsm20 added this pull request to the merge queue Apr 23, 2026
Merged via the queue into main with commit de1fe92 Apr 23, 2026
40 checks passed
@dsm20 dsm20 deleted the add-sort-support-kaskeys-api branch April 23, 2026 15:06
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) docs Documentation size/m

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants