Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[OTE-626]: Add support for whitelisted addresses #1966

Merged
merged 1 commit into from
Jul 25, 2024

Conversation

Christopher-Li
Copy link
Contributor

@Christopher-Li Christopher-Li commented Jul 25, 2024

Changelist

[Describe or list the changes made in this PR]

Test Plan

[Describe how this PR was tested (if applicable)]

Author/Reviewer Checklist

  • If this PR has changes that result in a different app state given the same prior state and transaction list, manually add the state-breaking label.
  • If the PR has breaking postgres changes to the indexer add the indexer-postgres-breaking label.
  • If this PR isn't state-breaking but has changes that modify behavior in PrepareProposal or ProcessProposal, manually add the label proposal-breaking.
  • If this PR is one of many that implement a specific feature, manually label them all feature:[feature-name].
  • If you wish to for mergify-bot to automatically create a PR to backport your change to a release branch, manually add the label backport/[branch-name].
  • Manually add any of the following labels: refactor, chore, bug.

Summary by CodeRabbit

  • New Features

    • Introduced a new configuration parameter for whitelisted addresses to enhance compliance checks.
    • Added a function to validate if an address is whitelisted, impacting geoblocking logic.
    • Enhanced compliance logic to allow whitelisted addresses to bypass certain restrictions.
  • Bug Fixes

    • Improved test coverage for compliance status handling related to address whitelisting and geographical restrictions.
  • Tests

    • Added new test cases to verify the functionality of the whitelisting feature across various scenarios.

Copy link

linear bot commented Jul 25, 2024

Copy link
Contributor

coderabbitai bot commented Jul 25, 2024

Walkthrough

The changes introduce a comprehensive enhancement to the compliance functionality by incorporating a new address whitelisting feature. A new function, isWhitelistedAddress, checks if addresses are permitted, impacting compliance checks across various components. Test cases are added to ensure proper functionality, and the main compliance controller is optimized to respond quicker for whitelisted addresses. This overall improvement streamlines compliance handling and validates the integration of geoblocking with address whitelisting.

Changes

File(s) Change Summary
indexer/packages/compliance/__tests__/geoblocking/restrict-countries.test.ts New test suite for isWhitelistedAddress, verifying address whitelisting logic.
indexer/packages/compliance/src/config.ts Added WHITELISTED_ADDRESSES parameter to complianceConfigSchema, defining a list of approved addresses.
indexer/packages/compliance/src/geoblocking/restrict-countries.ts Introduced isWhitelistedAddress function to validate addresses against the whitelist.
indexer/services/comlink/__tests__/controllers/api/v4/compliance-v2-controller.test.ts Enhanced ComplianceV2Controller tests for compliance status based on whitelisting.
indexer/services/comlink/__tests__/lib/compliance-and-geo-check.test.ts Added checks for whitelisted addresses in compliance checks, modifying response logic.
indexer/services/comlink/src/controllers/api/v4/compliance-v2-controller.ts Integrated isWhitelistedAddress to improve compliance responses for whitelisted addresses.
indexer/services/comlink/src/lib/compliance-and-geo-check.ts Added logic to bypass compliance checks for whitelisted addresses, optimizing control flow.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant ComplianceController
    participant ComplianceCheck

    User->>ComplianceController: Request with address
    ComplianceController->>ComplianceCheck: Check if address is whitelisted
    alt Address is whitelisted
        ComplianceCheck-->>ComplianceController: Return compliant status
        ComplianceController-->>User: Respond with compliance status
    else Address is not whitelisted
        ComplianceCheck-->>ComplianceController: Continue with checks
        ComplianceController-->>User: Respond with compliance status
    end
Loading

Poem

🐰 In the garden where compliance blooms,
Whitelisted addresses chase away glooms.
Hopping through checks, swift as a breeze,
With a twinkle of logic, we handle with ease!
Now every request can dance and play,
For our little rabbit leads the way! ✨


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>.
    • 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 generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @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 as 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.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 4199c3e and 9e56be0.

Files selected for processing (7)
  • indexer/packages/compliance/tests/geoblocking/restrict-countries.test.ts (2 hunks)
  • indexer/packages/compliance/src/config.ts (1 hunks)
  • indexer/packages/compliance/src/geoblocking/restrict-countries.ts (1 hunks)
  • indexer/services/comlink/tests/controllers/api/v4/compliance-v2-controller.test.ts (7 hunks)
  • indexer/services/comlink/tests/lib/compliance-and-geo-check.test.ts (5 hunks)
  • indexer/services/comlink/src/controllers/api/v4/compliance-v2-controller.ts (3 hunks)
  • indexer/services/comlink/src/lib/compliance-and-geo-check.ts (2 hunks)
Additional comments not posted (11)
indexer/packages/compliance/src/config.ts (1)

25-28: LGTM!

The addition of the WHITELISTED_ADDRESSES configuration parameter is straightforward and well-integrated.

indexer/packages/compliance/__tests__/geoblocking/restrict-countries.test.ts (1)

52-64: LGTM!

The tests for the isWhitelistedAddress function are comprehensive and ensure proper functionality.

indexer/services/comlink/src/lib/compliance-and-geo-check.ts (1)

44-46: LGTM! Verify the reliability of isWhitelistedAddress.

The code changes are approved. The new check optimizes the control flow by bypassing compliance checks for whitelisted addresses.

However, ensure that the isWhitelistedAddress function is reliable and does not introduce security vulnerabilities.

indexer/services/comlink/__tests__/lib/compliance-and-geo-check.test.ts (1)

241-256: LGTM! The test case is comprehensive.

The new test case correctly verifies that if an address is whitelisted, the application does not return a 403 status even if the request comes from a restricted country.

indexer/services/comlink/src/controllers/api/v4/compliance-v2-controller.ts (2)

148-152: LGTM! Verify the reliability of isWhitelistedAddress.

The code changes are approved. The new check optimizes the control flow by returning a compliant status for whitelisted addresses.

However, ensure that the isWhitelistedAddress function is reliable and does not introduce security vulnerabilities.

Verification successful

The isWhitelistedAddress function is reliable.

The function isWhitelistedAddress is implemented in indexer/packages/compliance/src/geoblocking/restrict-countries.ts and checks if an address is in a comma-separated list from the configuration. The WHITELISTED_ADDRESSES configuration is securely managed and tested, ensuring the function's reliability.

  • indexer/packages/compliance/src/geoblocking/restrict-countries.ts
  • indexer/packages/compliance/src/config.ts
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the implementation of `isWhitelistedAddress` function.

# Test: Search for the function implementation. Expect: The function should be implemented correctly.
rg --type python -A 5 $'def isWhitelistedAddress'

Length of output: 81


Script:

#!/bin/bash
# Description: Verify the implementation of `isWhitelistedAddress` function.

# Test: Search for the function implementation across all file types.
rg 'function isWhitelistedAddress' -A 10

Length of output: 375


Script:

#!/bin/bash
# Description: Verify the source and handling of `config.WHITELISTED_ADDRESSES`.

# Test: Search for the definition and handling of `config.WHITELISTED_ADDRESSES`.
rg 'WHITELISTED_ADDRESSES' -A 5

Length of output: 2462


260-265: LGTM! Verify the reliability of isWhitelistedAddress.

The code changes are approved. The new check optimizes the control flow by returning a compliant status for whitelisted addresses.

However, ensure that the isWhitelistedAddress function is reliable and does not introduce security vulnerabilities.

indexer/services/comlink/__tests__/controllers/api/v4/compliance-v2-controller.test.ts (5)

22-22: Import statement looks good.

The import of isWhitelistedAddress is correct and aligns with the new functionality being tested.


63-67: Spy instance setup for isWhitelistedAddress looks good.

The isWhitelistedAddressSpy is correctly set up and mocked for use in the GET request tests.


164-187: New test case for whitelisted address in GET request looks good.

The test case correctly verifies that a restricted address with an existing CLOSE_ONLY compliance status returns COMPLIANT when whitelisted.


Line range hint 330-344: Spy instance setup for isWhitelistedAddress in POST request tests looks good.

The isWhitelistedAddressSpy is correctly set up and mocked for use in the POST request tests.


426-448: New test case for whitelisted address in POST request looks good.

The test case correctly verifies that a restricted address from a restricted country returns COMPLIANT when whitelisted.

Comment on lines +35 to +37
export function isWhitelistedAddress(address: string): boolean {
return config.WHITELISTED_ADDRESSES.split(',').includes(address);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Optimize the whitelisted address check.

Consider caching the split whitelist addresses to avoid splitting the string each time the function is called.

export function isWhitelistedAddress(address: string): boolean {
-  return config.WHITELISTED_ADDRESSES.split(',').includes(address);
+  const whitelistedAddresses = config.WHITELISTED_ADDRESSES.split(',');
+  return whitelistedAddresses.includes(address);
}
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
export function isWhitelistedAddress(address: string): boolean {
return config.WHITELISTED_ADDRESSES.split(',').includes(address);
}
export function isWhitelistedAddress(address: string): boolean {
const whitelistedAddresses = config.WHITELISTED_ADDRESSES.split(',');
return whitelistedAddresses.includes(address);
}

@Christopher-Li Christopher-Li merged commit f162a11 into main Jul 25, 2024
11 checks passed
@Christopher-Li Christopher-Li deleted the cl_whitelist_addresses branch July 25, 2024 20:17
@Christopher-Li
Copy link
Contributor Author

@Mergifyio backport release/indexer/v5.x

Copy link
Contributor

mergify bot commented Jul 26, 2024

backport release/indexer/v5.x

✅ Backports have been created

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

Successfully merging this pull request may close these issues.

2 participants