Skip to content

Fix Sandbox API Key requests returning Production Response for API Products #13153

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Dakshithas
Copy link
Contributor

Issue

When an API Product is secured using API Key or Basic Auth (without OAuth2), requests made with a sandbox API key were incorrectly routed to the production endpoint.

Root Cause

Certain velocity context properties, specifically apiIsApiKeyProtected and apiIsBasicAuthProtected, were not set during the API Product deployment process. These properties are essential for rendering the Synapse configuration with correct routing logic.

As a result, API Products using only API Key or Basic Auth lacked the conditional filters in their Synapse configs, causing all requests—regardless of the key type—to route to production.

Solution

Added the missing velocity context properties (apiIsApiKeyProtected, apiIsBasicAuthProtected) to ensure the Synapse configuration includes the correct conditional routing logic based on the security scheme.

Impact

  • Fixes incorrect endpoint routing for API Products using API Key or Basic Auth.
  • Enables proper sandbox testing with sandbox API keys.

Files Modified

  • APIConfigContext.java (publisher v1 common)

Testing

  • Verified sandbox API key requests are now routed to sandbox endpoints.
  • Confirmed production API key requests still function as expected.

Related Issue

Copy link

coderabbitai bot commented Jun 20, 2025

📝 Walkthrough

Walkthrough

The update enhances the velocity context setup for API products by adding checks for API key and basic authentication security types. Boolean flags are now set to indicate whether these security options are enabled, aligning the logic with what is already present for regular APIs. No public API signatures are modified.

Changes

File(s) Change Summary
.../APIConfigContext.java Added checks and boolean flags to velocity context for API key and basic authentication on API products.

Suggested reviewers

  • pubudu538
  • chamilaadhi
  • npamudika
  • Arshardh
  • AnuGayan
  • senthuran16
  • RakhithaRR

📜 Recent review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 9f3c064 and 6440dc8.

📒 Files selected for processing (1)
  • components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/common/template/APIConfigContext.java (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: build-product (3, group3)
  • GitHub Check: build-product (1, group1)
  • GitHub Check: run-benchmark-test
🔇 Additional comments (1)
components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/common/template/APIConfigContext.java (1)

141-152: LGTM! Excellent fix for the API Product routing issue.

The added security context properties perfectly mirror the existing logic for regular APIs (lines 91-101) and directly address the root cause described in the PR objectives. This ensures API Products secured with API Key or Basic Auth will now have the necessary velocity context properties (apiIsApiKeyProtected and apiIsBasicAuthProtected) for proper Synapse configuration generation, enabling correct sandbox endpoint routing.

The implementation is consistent, using the same constants and boolean logic pattern as the existing code.

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

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

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

Fixes incorrect routing of sandbox API key and Basic Auth requests for API Products by adding missing velocity context properties.

  • Adds apiIsApiKeyProtected and apiIsBasicAuthProtected flags to the Velocity context
  • Ensures conditional filters in Synapse configs include API Key and Basic Auth security schemes
Comments suppressed due to low confidence (1)

components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/common/template/APIConfigContext.java:142

  • Consider adding unit tests for setApiProductVelocityContext to verify that apiIsApiKeyProtected and apiIsBasicAuthProtected are correctly set in the VelocityContext when the API product includes these security schemes.
        if (apiSecurity != null && apiSecurity.contains(APIConstants.API_SECURITY_API_KEY)) {

@@ -138,6 +138,18 @@ private void setApiProductVelocityContext(APIProduct apiProduct, VelocityContext
} else {
context.put("apiIsOauthProtected", Boolean.FALSE);
}
// if API is secured with api_Key
Copy link
Preview

Copilot AI Jun 20, 2025

Choose a reason for hiding this comment

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

[nitpick] You can simplify these blocks by directly putting the boolean expression into the context. For example: context.put("apiIsApiKeyProtected", apiSecurity != null && apiSecurity.contains(APIConstants.API_SECURITY_API_KEY)); This reduces verbosity and improves readability.

Copilot uses AI. Check for mistakes.

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.

1 participant