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

Give precedence to toml configuration for the resident Key Manager #13015

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

Conversation

nimsara66
Copy link
Contributor

Overview

The current implementation of the resident Key Manager configuration initially relies on the settings provided in the TOML files. However, once the resident Key Manager configuration is saved, subsequent configurations are read from the database. As a result, certain changes cannot be updated via the UI, nor are they affected by modifications in the TOML configuration.

This pull request (PR) prioritizes TOML-based configurations over database-stored configurations for the resident Key Manager, ensuring that changes made in the TOML files take precedence. Additionally, it fixes the issue where the openid-configuration endpoint is not being updated in the resident Key Manager.

Issue

wso2/api-manager#3572

Copy link

coderabbitai bot commented Feb 27, 2025

📝 Walkthrough

Walkthrough

This pull request updates the configuration logic in APIUtil.java. The method responsible for setting properties on the KeyManagerConfigurationDTO now always sets the properties (AUTHSERVER_URL, REVOKE_URL, TOKEN_URL, TOKEN_ENDPOINT, and REVOKE_ENDPOINT) regardless of their previous state. In addition, a new public method getAndSetDefaultKeyManagerConfiguration(KeyManagerConfigurationDTO keyManagerConfigurationDTO) has been added.

Changes

File(s) Change Summary
components/apimgt/.../APIUtil.java - Modified logic to unconditionally set AUTHSERVER_URL, REVOKE_URL, TOKEN_URL, TOKEN_ENDPOINT, and REVOKE_ENDPOINT properties.
- Added new method getAndSetDefaultKeyManagerConfiguration.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant APIUtil
    participant DTO as KeyManagerConfigurationDTO

    Client->>APIUtil: getAndSetDefaultKeyManagerConfiguration(dto)
    APIUtil->>DTO: Set AUTHSERVER_URL, REVOKE_URL, TOKEN_URL
    APIUtil->>DTO: Set TOKEN_ENDPOINT, REVOKE_ENDPOINT
    APIUtil-->>Client: Return updated dto
Loading
✨ 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

🔭 Outside diff range comments (2)
components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/APIUtil.java (2)

8626-8628: 🛠️ Refactor suggestion

Add null check for keyManagerUrl before accessing it

The keyManagerUrl is accessed without validating if it's null. This could lead to NullPointerException.

+ if (StringUtils.isEmpty(keyManagerUrl)) {
+    throw new APIManagementException("Key Manager URL cannot be empty");
+ }

11519-11538: 🛠️ Refactor suggestion

Add input validation for organization reference parameters

The method lacks validation for input parameters referenceId and organizationName. These should be validated before processing.

public static synchronized String getOrganizationIdFromExternalReference(String referenceId,
        String organizationName, String rootOrganization) throws APIManagementException {
+   if (StringUtils.isEmpty(referenceId)) {
+       throw new APIManagementException("External reference ID cannot be empty");
+   }
+   if (StringUtils.isEmpty(organizationName)) {
+       throw new APIManagementException("Organization name cannot be empty"); 
+   }
    String organizationId = null;
    // Rest of the code...
}
🧹 Nitpick comments (4)
components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/APIUtil.java (4)

8641-8644: Avoid redundant property setting

TOKEN_ENDPOINT and REVOKE_ENDPOINT properties are being set redundantly since they are already set as TOKEN_URL and REVOKE_URL.

Consider consolidating the property setting to avoid duplication:

- keyManagerConfigurationDTO.addProperty(APIConstants.KeyManager.TOKEN_ENDPOINT,
-     keyManagerConfigurationDTO.getAdditionalProperties().get(APIConstants.TOKEN_URL));
- keyManagerConfigurationDTO.addProperty(APIConstants.KeyManager.REVOKE_ENDPOINT,
-     keyManagerConfigurationDTO.getAdditionalProperties().get(APIConstants.REVOKE_URL));

11540-11551: Add length validation for organization handle

The method should validate the length of the generated handle to ensure it meets system constraints.

public static String getOrganizationHandle(String name) {
    String sanatizedName = null;
    if (name == null) {
        return sanatizedName;
    }
    String nowhitespace = WHITESPACE.matcher(name).replaceAll("-");
    String normalized = Normalizer.normalize(nowhitespace, Normalizer.Form.NFD);
    sanatizedName = NONLATIN.matcher(normalized).replaceAll("");
    sanatizedName = sanatizedName.toLowerCase(Locale.ENGLISH).replaceAll("^-+|-+$", "");
+   // Validate handle length
+   if (sanatizedName.length() > 255) {
+       sanatizedName = sanatizedName.substring(0, 255);
+   }
    return sanatizedName;
}

11558-11585: Improve resource cleanup and exception documentation

The method should ensure proper cleanup of resources and document all possible exceptions.

  1. Add documentation for exceptions:
/**
 * Validate Api with the federated gateways
 *
 * @param api API Object
+ * @throws APIManagementException if validation fails or implementation class cannot be loaded
+ * @throws ExternalGatewayAPIValidationException if validation fails with federated gateway
 */
  1. Consider using try-with-resources for any closeable resources used by the deployer:
+ if (deployer instanceof AutoCloseable) {
+    try (AutoCloseable closeable = (AutoCloseable) deployer) {
+        // Validation logic
+    } catch (Exception e) {
+        throw new APIManagementException("Error closing deployer", e);
+    }
+ }

11322-11357: Improve hash generation methods with constants and validation

The hash generation methods could be improved with better constants, validation and performance handling.

+ private static final int MAX_PAYLOAD_SIZE = 10 * 1024 * 1024; // 10MB limit
+ private static final String DEFAULT_HASH_ALGORITHM = "SHA-256";

public static String generateHashValue(String payload) throws APIManagementException {
+   if (payload == null) {
+       throw new APIManagementException("Payload cannot be null");
+   }
+   if (payload.length() > MAX_PAYLOAD_SIZE) {
+       throw new APIManagementException("Payload size exceeds maximum allowed size");
+   }
    try {
-       MessageDigest messageDigest = MessageDigest.getInstance(hashingAlgorithm);
+       MessageDigest messageDigest = MessageDigest.getInstance(
+           StringUtils.isEmpty(hashingAlgorithm) ? DEFAULT_HASH_ALGORITHM : hashingAlgorithm);
        // Rest of the code...
    }
}
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between c931b32 and 27e1edd.

📒 Files selected for processing (1)
  • components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/APIUtil.java (3 hunks)

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.

3 participants