Skip to content

fix: Missing plan check and ip whitelist parsing#2379

Merged
chronark merged 20 commits intounkeyed:mainfrom
Flo4604:fix/enterprise_ip_whitelisting
Oct 20, 2024
Merged

fix: Missing plan check and ip whitelist parsing#2379
chronark merged 20 commits intounkeyed:mainfrom
Flo4604:fix/enterprise_ip_whitelisting

Conversation

@Flo4604
Copy link
Member

@Flo4604 Flo4604 commented Oct 13, 2024

What does this PR do?

This checks whether the Workspace is allowed to have IP whitelisting.
It also fixes the parsing of the whitelisted IPs as which lead to a 500 error before.

Fixes #2372

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?

Manually send the updated whitelist topic call with an API and workspace ID with the pro or free plan. This should give an error and not go through.

Afterwards, test by doing a curl and overwriting the True-Client-IP to an invalid and then valid IP to see if the IP Whitelist works properly.

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

    • Enhanced input validation for updating IP whitelists, now restricted to enterprise plans.
    • Introduced new request and response schemas for the key verification process, allowing for more detailed interactions.
  • Bug Fixes

    • Improved handling of ipWhitelist property formatting across various tests and services.
  • Documentation

    • Clarified response type formats for better readability.
  • Tests

    • Updated test suites to reflect changes in ipWhitelist handling and added new tests for rate limit overrides.

@changeset-bot
Copy link

changeset-bot bot commented Oct 13, 2024

⚠️ No Changeset found

Latest commit: 6915471

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 Oct 13, 2024

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 Oct 20, 2024 7:22am
engineering ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 20, 2024 7:22am
play ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 20, 2024 7:22am
workflows ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 20, 2024 7:22am
www ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 20, 2024 7:22am

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Oct 13, 2024

📝 Walkthrough
📝 Walkthrough

Walkthrough

The pull request introduces modifications to the KeyService class, specifically enhancing response type structures and refining the handling of IP whitelisting. Changes include a reformatting of response types for clarity, a simplified approach to parsing the ipWhitelist, and improved readability of the ratelimits object declaration. Additionally, the verifyKey route has been updated to reflect schema changes, including the introduction of a new ratelimits field and modifications to the response structure. The updateApiIpWhitelist function has also been modified to include new validation steps based on workspace features.

Changes

File Path Change Summary
apps/api/src/pkg/keys/service.ts Reformatted response types and updated ipWhitelist handling in _verifyKey. Reformatted ratelimits declaration.
apps/api/src/routes/v1_keys_verifyKey.ts Updated request and response schemas, deprecated ratelimit, and added new ratelimits field. Modified response structure.
apps/dashboard/lib/trpc/routers/api/updateIpWhitelist.ts Enhanced input validation for updateApiIpWhitelist, including checks for workspace ID and plan.
apps/dashboard/app/(app)/apis/[apiId]/settings/update-ip-whitelist.tsx Updated logic for enabling IP whitelist feature based on features instead of plan.

Assessment against linked issues

Objective Addressed Explanation
Fix IP Whitelist parsing to avoid 500 errors (#2372)

Possibly related PRs

Suggested labels

Feature, :joystick: 150 points, hacktoberfest

Suggested reviewers

  • mcstepp
  • chronark
  • perkinsjr

📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between fb17524 and 6915471.

📒 Files selected for processing (1)
  • apps/api/src/pkg/keys/service.ts (4 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • apps/api/src/pkg/keys/service.ts

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ 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.
    • Generate unit testing code for this file.
    • 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 generate unit testing code for this file.
    • @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 generate unit testing code.
    • @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.

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 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.

@vercel
Copy link

vercel bot commented Oct 13, 2024

@Flo4604 is attempting to deploy a commit to the Unkey Team on Vercel.

A member of the Team first needs to authorize it.

@github-actions
Copy link
Contributor

github-actions bot commented Oct 13, 2024

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

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: 2

🧹 Outside diff range and nitpick comments (3)
apps/api/src/routes/v1_apis_getApi.happy.test.ts (1)

34-34: Approved: IP whitelist format change aligns with PR objectives.

The change from JSON.stringify(["127.0.0.1"]) to ["127.0.0.1"].join(",") addresses the IP whitelist parsing issue mentioned in the PR objectives and linked issue #2372. This should prevent the 500 error caused by attempting to JSON parse a CSV-formatted list.

Consider adding the following test cases to improve coverage:

  1. A test with multiple IP addresses in the whitelist to ensure correct handling of multiple entries.
  2. A test that explicitly verifies the format of the stored IP whitelist in the database.

Example for multiple IP addresses:

const multipleIps = ["127.0.0.1", "192.168.0.1", "10.0.0.1"];
const api = {
  // ... other properties ...
  ipWhitelist: multipleIps.join(","),
};
// ... rest of the test ...
// Add an assertion to check if the stored ipWhitelist matches the expected format
expect(storedApi.ipWhitelist).toBe(multipleIps.join(","));
apps/api/src/routes/legacy_keys_verifyKey.test.ts (1)

180-180: LGTM: IP whitelist format consistently updated.

The change to use a comma-separated string for the ipWhitelist property is consistent with the previous change and aligns with the PR objectives.

For improved code consistency and readability, consider extracting the IP whitelist into a constant at the top of the file:

const TEST_IP_WHITELIST = ["100.100.100.100"].join(",");

Then use this constant in both test cases:

ipWhitelist: TEST_IP_WHITELIST,

This would make it easier to maintain and update the test IP whitelist across all test cases.

apps/api/src/routes/v1_keys_verifyKey.test.ts (1)

Line range hint 352-385: LGTM! Consider uncommenting and fixing the remaining tokens expectation.

The new test suite for rate limit override looks good. It correctly tests the deduction of tokens when a specific cost is applied to a request. However, there's a commented-out expectation for the remaining tokens:

// expect(res.body.ratelimit?.remaining).toEqual(6);

Consider uncommenting this line and ensuring it's correct. If the initial limit is 10 and the cost is 4, the remaining should indeed be 6.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 7972170 and 8dc2d19.

📒 Files selected for processing (5)
  • apps/api/src/pkg/keys/service.ts (5 hunks)
  • apps/api/src/routes/legacy_keys_verifyKey.test.ts (2 hunks)
  • apps/api/src/routes/v1_apis_getApi.happy.test.ts (1 hunks)
  • apps/api/src/routes/v1_keys_verifyKey.test.ts (3 hunks)
  • apps/dashboard/lib/trpc/routers/api/updateIpWhitelist.ts (2 hunks)
🧰 Additional context used
🔇 Additional comments (10)
apps/api/src/routes/legacy_keys_verifyKey.test.ts (2)

Line range hint 1-214: Summary: IP whitelist parsing issue addressed successfully.

The changes in this file successfully address the IP whitelist parsing issue mentioned in the PR objectives and linked issue #2372. The ipWhitelist property is now consistently set as a comma-separated string, which should resolve the 500 error caused by incorrect parsing.

The test cases cover various scenarios, including:

  1. Valid key verification
  2. Bad requests
  3. Temporary key handling
  4. IP whitelist validation (both valid and invalid IPs)

These comprehensive tests ensure that the key verification process behaves correctly under different conditions, particularly with the updated IP whitelist format.

To further improve the test suite, consider adding the following test cases:

  1. Multiple IP addresses in the whitelist
  2. IPv6 addresses in the whitelist
  3. Edge cases such as empty whitelist or whitelist with invalid IP formats

Would you like me to provide examples of these additional test cases?


132-132: LGTM: IP whitelist format updated correctly.

The change from a JSON string to a comma-separated string for the ipWhitelist property aligns with the PR objectives and addresses the issue described in #2372. This should resolve the 500 error caused by incorrect parsing.

To ensure consistency across the codebase, let's verify that this change is applied uniformly:

apps/api/src/pkg/keys/service.ts (5)

60-64: LGTM: Improved readability of InvalidResponse type

The reformatting of the identity field in the InvalidResponse type enhances code readability without changing the underlying logic. This change aligns with TypeScript best practices for type definitions.


80-84: LGTM: Consistent improvement in ValidResponse type

The reformatting of the identity field in the ValidResponse type mirrors the changes made to the InvalidResponse type. This consistency improves overall code readability and maintains a uniform structure across related type definitions.


296-298: LGTM: Enhanced readability of ratelimits object declaration

The reformatting of the ratelimits object declaration improves code readability while maintaining the original functionality. This change is consistent with the earlier modifications to type definitions, contributing to a more uniform and readable codebase.


410-410: LGTM: Fixed IP whitelist parsing and improved readability

The changes address the issue mentioned in PR #2372 regarding the 500 error caused by incorrect parsing of IP whitelist data. The new implementation correctly parses the CSV string into an array of IP addresses using split(',').map((s) => s.trim()). This fix resolves the parsing error and ensures proper functionality of the IP whitelist feature.

Additionally, the added empty line improves code readability by separating logical blocks.

To ensure the fix works as expected, we can run the following test:

Also applies to: 422-422

✅ Verification successful

Verified: IP whitelist parsing implemented correctly and readability improved

The IP whitelist parsing has been successfully verified, addressing the issue described in PR #2372. The implementation correctly parses the CSV string into an array of IP addresses using split(',').map((s) => s.trim()), ensuring proper functionality of the IP whitelist feature.

Additionally, the added empty line enhances code readability by separating logical blocks.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Test IP whitelist parsing
echo "Testing IP whitelist parsing..."
ast-grep --lang typescript --pattern 'const ipWhitelist = $_.split(",").map((s) => s.trim());'
if [ $? -eq 0 ]; then
    echo "IP whitelist parsing implemented correctly."
else
    echo "IP whitelist parsing not found or implemented incorrectly."
fi

Length of output: 401


Line range hint 1-624: Overall improvements implemented, but clarification needed on missing plan check

The changes in this PR successfully address the IP whitelist parsing issue mentioned in #2372, which should resolve the 500 error. The code readability has also been improved through consistent reformatting of type definitions and object declarations.

However, the PR title mentions a "Missing plan check," but there doesn't seem to be any explicit changes related to this in the reviewed code. Could you please clarify if the plan check has been implemented elsewhere or if it's still pending?

To confirm if any plan-related changes were made, we can run the following search:

apps/api/src/routes/v1_keys_verifyKey.test.ts (2)

521-521: LGTM! IP whitelist format updated correctly.

The change from a JSON string to a comma-separated string for the ipWhitelist is correct and aligns with the PR objectives. This update should resolve the 500 error mentioned in issue #2372 by ensuring proper parsing of the IP whitelist.


569-569: LGTM! IP whitelist format consistently updated.

The change to use a comma-separated string for the ipWhitelist is consistent with the update in the "with valid ip" test case. This ensures that both test cases use the same format for the IP whitelist, maintaining consistency across the test suite.

apps/dashboard/lib/trpc/routers/api/updateIpWhitelist.ts (1)

84-84: No action needed

The addition of a blank line improves code readability and follows standard formatting conventions.

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
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.

one issue, rest looks good

@chronark chronark assigned chronark and unassigned perkinsjr Oct 16, 2024
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: 0

🧹 Outside diff range and nitpick comments (3)
apps/dashboard/app/(app)/apis/[apiId]/settings/update-ip-whitelist.tsx (3)

33-33: Approved: Good shift to feature-based access control.

The change from plan to features in the workspace prop type is a good move towards more granular feature-based access control. This aligns well with the PR objectives and allows for more flexible management of the IP whitelist feature.

Consider using a more specific type for features if possible, such as { ipWhitelist: boolean }, to make the expected structure clearer and improve type safety.


Line range hint 82-121: Approved: Good implementation of conditional rendering based on feature flag.

The updated rendering logic correctly uses the isEnabled variable to conditionally render the IP whitelist input or an upgrade alert. This change effectively implements the feature-based access control.

Consider updating the alert message to reflect the new feature-based approach rather than mentioning the enterprise plan specifically. For example:

<AlertDescription>
  IP whitelists are not enabled for your workspace. Please contact support to enable this feature.
</AlertDescription>

Line range hint 1-134: Summary: Successful implementation of feature-based IP whitelist control.

The changes in this file effectively implement the feature-based control for IP whitelisting, addressing the issues mentioned in the PR objectives. The shift from plan-based to feature-based access control provides more flexibility and aligns well with the intended improvements.

Key points:

  1. The workspace prop now uses features instead of plan.
  2. A new isEnabled variable clearly determines feature availability.
  3. Conditional rendering based on the feature flag is correctly implemented.

These changes should resolve the issues related to IP whitelisting and improve the overall functionality of the component. The code is clean, well-structured, and follows React best practices.

As you continue to develop this feature, consider the following architectural advice:

  1. Implement a centralized feature flag management system if not already in place.
  2. Ensure that the backend API is also updated to respect these feature flags consistently.
  3. Consider adding analytics or logging to track usage of the IP whitelist feature across different workspaces.
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 178c2f0 and fb17524.

📒 Files selected for processing (5)
  • apps/api/src/pkg/keys/service.ts (5 hunks)
  • apps/api/src/routes/v1_keys_verifyKey.ts (1 hunks)
  • apps/dashboard/app/(app)/apis/[apiId]/settings/page.tsx (1 hunks)
  • apps/dashboard/app/(app)/apis/[apiId]/settings/update-ip-whitelist.tsx (3 hunks)
  • apps/dashboard/lib/trpc/routers/api/updateIpWhitelist.ts (2 hunks)
✅ Files skipped from review due to trivial changes (1)
  • apps/dashboard/app/(app)/apis/[apiId]/settings/page.tsx
🚧 Files skipped from review as they are similar to previous changes (2)
  • apps/api/src/pkg/keys/service.ts
  • apps/dashboard/lib/trpc/routers/api/updateIpWhitelist.ts
🧰 Additional context used
🔇 Additional comments (2)
apps/dashboard/app/(app)/apis/[apiId]/settings/update-ip-whitelist.tsx (1)

45-45: LGTM: Clear implementation of feature flag.

The introduction of the isEnabled variable using workspace.features.ipWhitelist is a clear and effective implementation of the feature flag. This change directly addresses the PR objectives and simplifies the logic for enabling the IP whitelist feature.

apps/api/src/routes/v1_keys_verifyKey.ts (1)

347-350: LGTM!

The identity object is correctly constructed, defaulting meta to an empty object if undefined. This ensures that the response consistently includes the meta field as an object, which aligns with the response schema.

@Flo4604
Copy link
Member Author

Flo4604 commented Oct 18, 2024

I will fix the merge conflict.

chronark and others added 10 commits October 19, 2024 10:21
… Tree Format (unkeyed#2238)

* fix:changed the permission view

* fixed issue comments

* [autofix.ci] apply automated fixes

* removed font

---------

Co-authored-by: Andreas Thomas <dev@chronark.com>
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
unkeyed#2273)

* fix: show shallow permissions followed by nested in alphabetical order

* fix: correct the sorting of nested permissions top level keys

* [autofix.ci] apply automated fixes

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Co-authored-by: Andreas Thomas <dev@chronark.com>
@oss-gg
Copy link

oss-gg bot commented Oct 20, 2024

Awarding Flo4604: 300 points 🕹️ Well done! Check out your new contribution on oss.gg/Flo4604

@Flo4604 Flo4604 deleted the fix/enterprise_ip_whitelisting branch October 20, 2024 07:39
@coderabbitai coderabbitai bot mentioned this pull request Oct 21, 2024
18 tasks
@coderabbitai coderabbitai bot mentioned this pull request Dec 5, 2024
16 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

IP Whitelist leading to 500 error in API

7 participants