Skip to content

feat: add trpc and filters schema for root keys#3331

Merged
chronark merged 2 commits intomainfrom
root-keys-trpc
Jun 17, 2025
Merged

feat: add trpc and filters schema for root keys#3331
chronark merged 2 commits intomainfrom
root-keys-trpc

Conversation

@ogzhanolguncu
Copy link
Contributor

@ogzhanolguncu ogzhanolguncu commented Jun 16, 2025

What does this PR do?

This PR adds a tRPC endpoint for the new root-keys page and filters schema for the upcoming table view.

The endpoint includes cursor pagination and permission categorization that groups permissions by type (API, Keys, Ratelimit, etc.) with counts. It also detects critical permissions - if a root key has permissions like delete, remove or decrypt, we'll mark it as critical.

In the following PRs we'll introduce the table view which will display UpdatedAt, CreatedAt, Name, ExternalId (creator of the key), PermissionList (high-level summary like APIs(5), Ratelimit(3)) and actions. Users will be able to search and filter using name, identity(externalId), permissionName and start (start of the key).

Fixes # (issue)

If there is not an issue for this, please create one first. This is used to tracking purposes and also helps use understand why this PR exists

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • Chore (refactoring code, technical debt, workflow improvements)
  • Enhancement (small improvements)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How should this be tested?

  • /settings/root-keys-v2 check this page and compare it against /settings/root-keys this one. Currently V2 is not styled, but it will styled in the following iterations.

Checklist

Required

  • Filled out the "How to test" section in this PR
  • Read Contributing Guide
  • Self-reviewed my own code
  • Commented on my code in hard-to-understand areas
  • Ran pnpm build
  • Ran pnpm fmt
  • Checked for warnings, there are none
  • Removed all console.logs
  • Merged the latest changes from main onto my branch with git pull origin main
  • My changes don't cause any responsiveness issues

Appreciated

  • If a UI change was made: Added a screen recording or screenshots to this PR
  • Updated the Unkey Docs if changes were necessary

Summary by CodeRabbit

  • New Features
    • Introduced a new "Root Keys" settings page displaying a paginated list of root keys, including summary information and permission categorization.
    • Added a navigation component for workspace settings with quick navigation and context-sensitive actions.
    • Enabled filtering options for root keys by various fields and operators.
  • Bug Fixes
    • Improved error handling and user feedback when loading root keys fails.

@changeset-bot
Copy link

changeset-bot bot commented Jun 16, 2025

⚠️ No Changeset found

Latest commit: 558349b

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@vercel
Copy link

vercel bot commented Jun 16, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
dashboard ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jun 16, 2025 0:31am
1 Skipped Deployment
Name Status Preview Comments Updated (UTC)
engineering ⬜️ Ignored (Inspect) Visit Preview Jun 16, 2025 0:31am

@github-actions
Copy link
Contributor

github-actions bot commented Jun 16, 2025

Thank you for following the naming conventions for pull request titles! 🙏

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jun 16, 2025

📝 Walkthrough

Walkthrough

This change introduces a new "Root Keys" feature in the dashboard settings. It adds backend TRPC API endpoints for querying root keys with filtering, pagination, and permission categorization. On the frontend, it implements schema definitions, a navigation component, and a page for listing root keys, integrating the new API and UI components.

Changes

File(s) Change Summary
.../settings/root-keys-v2/filters.schema.ts New module defining Zod schemas, TypeScript types, and parsing utilities for filtering root keys.
.../settings/root-keys-v2/navigation.tsx New React navigation component for settings pages with breadcrumbs and context-specific actions.
.../settings/root-keys-v2/page.tsx New React page for displaying root keys, fetching data via TRPC, and rendering UI states.
.../lib/trpc/routers/index.ts Adds a nested settings.rootKeys.query TRPC endpoint to the main router.
.../lib/trpc/routers/settings/root-keys/query.ts New TRPC query procedure for fetching, paginating, and categorizing root keys and their permissions.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant RootKeysPage (UI)
    participant TRPC Client
    participant TRPC Server
    participant Database

    User->>RootKeysPage (UI): Visit Root Keys page
    RootKeysPage (UI)->>TRPC Client: Call settings.rootKeys.query (limit, cursor)
    TRPC Client->>TRPC Server: Forward request
    TRPC Server->>Database: Query root keys (with filters, pagination)
    Database-->>TRPC Server: Return root keys and permissions
    TRPC Server->>TRPC Server: Categorize permissions, build response
    TRPC Server-->>TRPC Client: Return keys, meta, permission summaries
    TRPC Client-->>RootKeysPage (UI): Deliver data
    RootKeysPage (UI)-->>User: Render root keys list and navigation
Loading

Possibly related PRs

Suggested labels

Feature

Suggested reviewers

  • perkinsjr
  • mcstepp
  • MichaelUnkey
✨ Finishing Touches
  • 📝 Generate Docstrings

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
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@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: 8

📜 Review details

Configuration used: CodeRabbit UI
Review profile: ASSERTIVE
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 365c748 and 558349b.

📒 Files selected for processing (5)
  • apps/dashboard/app/(app)/settings/root-keys-v2/filters.schema.ts (1 hunks)
  • apps/dashboard/app/(app)/settings/root-keys-v2/navigation.tsx (1 hunks)
  • apps/dashboard/app/(app)/settings/root-keys-v2/page.tsx (1 hunks)
  • apps/dashboard/lib/trpc/routers/index.ts (2 hunks)
  • apps/dashboard/lib/trpc/routers/settings/root-keys/query.ts (1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (2)
apps/dashboard/lib/trpc/routers/index.ts (2)
apps/dashboard/lib/trpc/trpc.ts (1)
  • t (8-8)
apps/dashboard/lib/trpc/routers/settings/root-keys/query.ts (1)
  • queryRootKeys (41-139)
apps/dashboard/app/(app)/settings/root-keys-v2/filters.schema.ts (2)
apps/dashboard/components/logs/validation/filter.types.ts (2)
  • StringConfig (33-38)
  • FilterValue (49-62)
apps/dashboard/components/logs/validation/utils/nuqs-parsers.ts (1)
  • parseAsFilterValueArray (29-57)
⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: Test Packages / Test ./internal/clickhouse
  • GitHub Check: autofix
  • GitHub Check: Analyze (javascript-typescript)
🔇 Additional comments (1)
apps/dashboard/lib/trpc/routers/index.ts (1)

88-98: Router hierarchy looks good – make sure we don’t already expose a settings router elsewhere.

The new import and nested settings.rootKeys.query route integrate cleanly with the existing structure.
Just double-check that no other settings: namespace already exists in a different router file to avoid name collision in the generated TRPC types.

Also applies to: 125-129

Comment on lines +24 to +26
total: z.number(),
categories: z.record(z.number()), // { "API": 4, "Keys": 6, "Ratelimit": 2 }
hasCriticalPerm: z.boolean(), // delete, decrypt permissions
Copy link
Contributor

@coderabbitai coderabbitai bot Jun 16, 2025

Choose a reason for hiding this comment

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

🧹 Nitpick (assertive)

Schema comment inconsistent with implementation.

Comment lists "Keys" category but categorizePermissions never returns that key.
Either add the category or update the comment to avoid confusion.

🤖 Prompt for AI Agents
In apps/dashboard/lib/trpc/routers/settings/root-keys/query.ts around lines 24
to 26, the comment for the categories schema mentions a "Keys" category that is
not returned by the categorizePermissions function. To fix this, either update
the categorizePermissions function to include the "Keys" category if it is
intended to be part of the data, or remove "Keys" from the comment to accurately
reflect the actual categories returned, ensuring the comment matches the
implementation.

Copy link
Collaborator

Choose a reason for hiding this comment

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

good point, didn't we get rid of the category display @ogzhanolguncu ?
pls ignore if this is already addressed in the UI PR

Copy link
Contributor

Choose a reason for hiding this comment

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

Seems like the humans are having a chat. I'll hop back into my burrow for now. If you need me again, just tag @coderabbitai in a new comment, and I'll come hopping out!

@chronark chronark self-assigned this Jun 17, 2025
import { Button, CopyButton } from "@unkey/ui";
import Link from "next/link";

const settingsNavbar = [
Copy link
Collaborator

Choose a reason for hiding this comment

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

not a blocker here, but we should definitely put these kind of link lists into a constant to reuse in other pages, wdyt?

let hasCriticalPerm = false;

for (const permission of permissions) {
if (!permission?.name || typeof permission.name !== "string") {
Copy link
Collaborator

Choose a reason for hiding this comment

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

use the slug instead of name please

}

// Extract category from permission name (e.g., "api.*.create_key" -> "api")
const parts = permission.name.split(".");
Copy link
Collaborator

Choose a reason for hiding this comment

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

slug

const [identifier] = parts;
let category: string;

switch (identifier) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

probably no longer necessary and already addressed in the next PR

Copy link
Collaborator

@chronark chronark left a comment

Choose a reason for hiding this comment

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

only change required is using the slug instead of name on permissions

though we can merge this if you fix it in the other PR, your call

Copy link
Contributor Author

Yeah most of the things are fixed in the following prs

Copy link
Collaborator

ok, should I let this one through as is?

Copy link
Contributor Author

Yes

Copy link
Contributor Author

Unless you see something you don’t like in the db queries or something

Copy link
Collaborator

well I did not like you using permission.name, cause it needs to be permission.slug lol
but it's your call whether you want to fix it here or there

Copy link
Contributor Author

We can change it in the final PR 🫡

@chronark chronark disabled auto-merge June 17, 2025 16:41
@chronark chronark merged commit 70a92a5 into main Jun 17, 2025
29 of 31 checks passed
@chronark chronark deleted the root-keys-trpc branch June 17, 2025 16:41
github-merge-queue bot pushed a commit that referenced this pull request Jun 25, 2025
* feat: add trpc and filters schema for root keys

* refactor: get rid of extra round trip and simplify query

* feat: add root keys table
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.

2 participants