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

Key Manager Visibility, Gateway Visibility Bug Fixes and Other Fixes #13021

Merged
merged 2 commits into from
Feb 28, 2025

Conversation

Avishka-Shamendra
Copy link
Contributor

@Avishka-Shamendra Avishka-Shamendra commented Feb 28, 2025

Purpose

Fixes wso2/api-manager#3730
Fixes wso2/api-manager#3727
Close unclosed resource streams

This pull request includes various improvements and fixes across multiple files. The most important changes include adding permissions to the Environment class, ensuring case insensitivity in the hasIntersection method, and updating the swaggerYamlGet method to use InputStream for reading YAML files.

Enhancements to Environment class:

  • Added a new field permissions to the Environment class and updated the constructor to initialize it. (components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/Environment.java)

Case insensitivity in hasIntersection method:

  • Updated the hasIntersection method to convert elements to lowercase before adding to the set and checking for intersections. (components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIConsumerImpl.java)
  • Applied the same case insensitivity update to the hasIntersection method in APIUtil class. (components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/APIUtil.java)

Improvements to swaggerYamlGet method:

  • Updated swaggerYamlGet method to use InputStream for reading YAML files in SwaggerYamlApi classes across various modules:
    • components/apimgt/org.wso2.carbon.apimgt.governance.rest.api/src/main/java/org/wso2/carbon/apimgt/governance/rest/api/SwaggerYamlApi.java
    • components/apimgt/org.wso2.carbon.apimgt.rest.api.admin.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/admin/v1/SwaggerYamlApi.java
    • components/apimgt/org.wso2.carbon.apimgt.rest.api.gateway/src/main/java/org/wso2/carbon/apimgt/rest/api/gateway/SwaggerYamlApi.java
    • components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/SwaggerYamlApi.java
    • components/apimgt/org.wso2.carbon.apimgt.rest.api.store.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/store/v1/SwaggerYamlApi.java

Copy link

coderabbitai bot commented Feb 28, 2025

📝 Walkthrough

Walkthrough

This pull request introduces several changes across multiple classes. It adds a new assignment in the copy constructor of the Environment class to ensure that the permissions field is copied from the source instance. The hasIntersection methods in both the APIConsumerImpl and APIUtil classes are updated to perform case-insensitive comparisons by converting array elements to lowercase. Additionally, modifications are made to the SwaggerYamlApi classes to enhance resource management when retrieving OpenAPI definitions, transitioning to a try-with-resources approach and updating character encoding handling.

Changes

File Change Summary
components/apimgt/.../Environment.java Added a new private field permissions of type GatewayVisibilityPermissionConfigurationDTO and copied its value in the copy constructor.
components/apimgt/.../APIConsumerImpl.java & APIUtil.java Modified the hasIntersection methods to convert array elements to lowercase for case-insensitive comparison; added import java.util.Locale; in APIConsumerImpl.
components/apimgt/.../SwaggerYamlApi.java (multiple) Updated the OpenAPI definition retrieval to use try-with-resources for better resource management and changed character encoding to StandardCharsets.UTF_8.

Suggested reviewers

  • tgtshanika
  • chamilaadhi
  • tharindu1st
  • dushaniw
  • Arshardh
  • AnuGayan

📜 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 83c56b5 and ebb7e76.

📒 Files selected for processing (5)
  • components/apimgt/org.wso2.carbon.apimgt.governance.rest.api/src/main/java/org/wso2/carbon/apimgt/governance/rest/api/SwaggerYamlApi.java (2 hunks)
  • components/apimgt/org.wso2.carbon.apimgt.rest.api.admin.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/admin/v1/SwaggerYamlApi.java (2 hunks)
  • components/apimgt/org.wso2.carbon.apimgt.rest.api.gateway/src/main/java/org/wso2/carbon/apimgt/rest/api/gateway/SwaggerYamlApi.java (2 hunks)
  • components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/SwaggerYamlApi.java (2 hunks)
  • components/apimgt/org.wso2.carbon.apimgt.rest.api.store.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/store/v1/SwaggerYamlApi.java (2 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (6)
  • GitHub Check: build-product (4, group4)
  • GitHub Check: build-product (3, group3)
  • GitHub Check: build-product (2, group2)
  • GitHub Check: build-product (1, group1)
  • GitHub Check: build-carbon
  • GitHub Check: run-benchmark-test
🔇 Additional comments (11)
components/apimgt/org.wso2.carbon.apimgt.governance.rest.api/src/main/java/org/wso2/carbon/apimgt/governance/rest/api/SwaggerYamlApi.java (2)

31-31: Added appropriate import for new try-with-resources implementation.

The import for java.io.InputStream is correctly added to support the improved resource management implementation.


78-82: Great improvement in resource management.

The code now uses a try-with-resources pattern which ensures that the input stream is properly closed after use, even if an exception occurs. This prevents potential resource leaks and follows Java best practices for resource handling.

components/apimgt/org.wso2.carbon.apimgt.rest.api.admin.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/admin/v1/SwaggerYamlApi.java (2)

33-34: Added appropriate imports for enhanced implementation.

The imports for java.io.InputStream and java.nio.charset.StandardCharsets are correctly added to support the improved resource management and character encoding handling.


67-71: Good improvements in resource management and encoding practices.

The implementation now:

  1. Uses try-with-resources to ensure proper stream closure
  2. Uses getClassLoader().getResourceAsStream() for more reliable resource loading
  3. Uses StandardCharsets.UTF_8 instead of string literal "UTF-8" for improved type safety

These changes follow Java best practices and help prevent resource leaks.

components/apimgt/org.wso2.carbon.apimgt.rest.api.store.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/store/v1/SwaggerYamlApi.java (2)

31-32: Added appropriate imports for enhanced implementation.

The imports for java.io.InputStream and java.nio.charset.StandardCharsets are correctly added to support the improved resource management and character encoding handling.


72-76: Good enhancements to resource management and encoding practices.

The implementation now:

  1. Uses try-with-resources to ensure proper stream closure
  2. Uses getClassLoader().getResourceAsStream() for more reliable resource loading
  3. Uses StandardCharsets.UTF_8 instead of string literal "UTF-8" for improved type safety

These changes align with Java best practices and prevent potential resource leaks.

components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/SwaggerYamlApi.java (2)

31-32: Added appropriate imports for enhanced implementation.

The imports for java.io.InputStream and java.nio.charset.StandardCharsets are correctly added to support the improved resource management and character encoding handling.


71-75: Good improvements in resource management and encoding practices.

The implementation now:

  1. Uses try-with-resources to ensure proper stream closure
  2. Uses getClassLoader().getResourceAsStream() for more reliable resource loading
  3. Uses StandardCharsets.UTF_8 instead of string literal "UTF-8" for improved type safety

These changes follow Java best practices and help prevent resource leaks.

components/apimgt/org.wso2.carbon.apimgt.rest.api.gateway/src/main/java/org/wso2/carbon/apimgt/rest/api/gateway/SwaggerYamlApi.java (3)

25-25: Import change reflects update from OAS2 to OAS3 parser

The import change from OAS2Parser to OAS3Parser indicates an upgrade from OpenAPI Specification 2.0 to 3.0, which is a significant enhancement in API definition capabilities.


29-30: Appropriate imports added for enhanced implementation

The addition of imports for InputStream and StandardCharsets supports the improved implementation in the swaggerYamlGet method.


68-72: Improved resource management and updated OpenAPI parsing

The implementation has been enhanced in several ways:

  1. Uses try-with-resources for proper stream closure
  2. Uses class loader to load resources more reliably
  3. Employs StandardCharsets.UTF_8 instead of string literals
  4. Updates from OAS2Parser to OAS3Parser with corresponding method change

These changes improve resource handling and modernize the API definition parsing.

✨ Finishing Touches
  • 📝 Generate Docstrings

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 generate docstrings to generate docstrings for 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

@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

🧹 Nitpick comments (4)
components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/APIUtil.java (4)

329-332: Consider adding more detailed class-level documentation

While the class has a basic Javadoc comment, it would be helpful to add more details about:

  • The key responsibilities and purpose of this utility class
  • Any important usage patterns or considerations
  • Links to related classes/documentation

6814-6877: Consider breaking down this long method into smaller focused methods

The marketplaceAssistantDeleteService method is quite long and handles multiple responsibilities including:

  • URL construction
  • HTTP client setup
  • Request execution
  • Response handling
  • Error handling

Consider extracting some of these into separate helper methods for better maintainability and testability.


11196-11202: Enhance error messages with more context

The error messages in the catch blocks could be more descriptive by including:

  • The specific operation that failed
  • The relevant parameters/context
  • A more user-friendly message format

For example:

- throw new APIManagementException("Error encountered while connecting to service", e);
+ throw new APIManagementException("Failed to connect to AI service endpoint: " + endpoint + 
    ". Please check the endpoint configuration and network connectivity.", e);

11004-11015: Consider additional input validation for security

The generateCodeVerifier method generates security-sensitive PKCE code verifiers. Consider adding:

  • Validation of the generated value length
  • Additional entropy checks
  • Proper cleanup of sensitive data

Example:

public static String generateCodeVerifier() {
    SecureRandom secureRandom = new SecureRandom();
    byte[] codeVerifier = new byte[32];
    try {
        secureRandom.nextBytes(codeVerifier);
+       // Validate minimum entropy
+       if (!hasMinimumEntropy(codeVerifier)) {
+           throw new SecurityException("Generated code verifier does not meet minimum entropy requirements");
+       }
        return java.util.Base64.getUrlEncoder().withoutPadding().encodeToString(codeVerifier);
    } finally {
+       // Clean up sensitive data
+       Arrays.fill(codeVerifier, (byte) 0);
    }
}
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between c9e0856 and 83c56b5.

📒 Files selected for processing (3)
  • components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/Environment.java (1 hunks)
  • components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIConsumerImpl.java (2 hunks)
  • components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/APIUtil.java (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (4)
  • GitHub Check: build-product (2, group2)
  • GitHub Check: build-product (1, group1)
  • GitHub Check: build-carbon
  • GitHub Check: run-benchmark-test
🔇 Additional comments (4)
components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/Environment.java (1)

86-86: Properly initialize permissions field in the copy constructor

Good addition to ensure that the permissions field is properly copied from the source Environment instance. This fixes a bug where the permissions configuration would be reset to default when cloning an Environment object.

components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIConsumerImpl.java (2)

165-165: New Locale import added for case-insensitive comparison.

This import is required for the enhanced hasIntersection method that now uses Locale.ENGLISH for consistent case-insensitive string comparisons.


4780-4794: Improved case-insensitive string comparison in hasIntersection method.

The method has been enhanced to perform case-insensitive comparisons by converting strings to lowercase using Locale.ENGLISH before comparison. This is a good practice as it ensures consistent behavior across different system locales when checking for role intersections.

Using Locale.ENGLISH explicitly instead of the default locale prevents potential inconsistencies across different deployment environments, especially in internationalized applications.

components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/APIUtil.java (1)

6372-6387: LGTM! Case-insensitive string comparison implementation looks good.

The implementation correctly handles case-insensitive comparison by converting elements to lowercase using Locale.ENGLISH before checking intersection. The early return optimization and use of HashSet for O(1) lookups makes it efficient.

tharindu1st
tharindu1st previously approved these changes Feb 28, 2025
@Avishka-Shamendra Avishka-Shamendra changed the title Key Manager Visibility and Gateway Visibility Bug Fixes Key Manager Visibility, Gateway Visibility Bug Fixes and Other Fixes Feb 28, 2025
@tharindu1st tharindu1st merged commit 18be033 into wso2:master Feb 28, 2025
7 of 8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants