Skip to content

Introduce base64 encoded scope name as path param for scope client#13855

Open
O-sura wants to merge 1 commit into
wso2:masterfrom
O-sura:special-char-scopes
Open

Introduce base64 encoded scope name as path param for scope client#13855
O-sura wants to merge 1 commit into
wso2:masterfrom
O-sura:special-char-scopes

Conversation

@O-sura

@O-sura O-sura commented Jun 2, 2026

Copy link
Copy Markdown
Contributor

This PR addresses the problem occurring when creating scopes with special characters

Fix for: wso2/api-manager#4659

Comment on lines 1357 to 1359
public void deleteScope(String scopeName) throws APIManagementException {

try {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Log Improvement Suggestion No: 1

Suggested change
public void deleteScope(String scopeName) throws APIManagementException {
try {
@Override
public void deleteScope(String scopeName) throws APIManagementException {
try {
log.info("Deleting scope: " + scopeName);

Comment on lines +1655 to +1661
public static String base64UrlEncode(String value) {
if (value == null || StringUtils.isBlank(value)) {
if (log.isDebugEnabled()) {
log.debug("Scope name is null, empty, or blank");
}
return value;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Log Improvement Suggestion No: 2

Suggested change
public static String base64UrlEncode(String value) {
if (value == null || StringUtils.isBlank(value)) {
if (log.isDebugEnabled()) {
log.debug("Scope name is null, empty, or blank");
}
return value;
}
public static String base64UrlEncode(String value) {
if (value == null || StringUtils.isBlank(value)) {
if (log.isDebugEnabled()) {
log.debug("Scope name is null, empty, or blank");
}
return value;
}
if (log.isDebugEnabled()) {
log.debug("Base64 URL encoding value");

@wso2-engineering wso2-engineering Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

AI Agent Log Improvement Checklist

⚠️ Warning: AI-Generated Review Comments

  • The log-related comments and suggestions in this review were generated by an AI tool to assist with identifying potential improvements. Purpose of reviewing the code for log improvements is to improve the troubleshooting capabilities of our products.
  • Please make sure to manually review and validate all suggestions before applying any changes. Not every code suggestion would make sense or add value to our purpose. Therefore, you have the freedom to decide which of the suggestions are helpful.

✅ Before merging this pull request:

  • Review all AI-generated comments for accuracy and relevance.
  • Complete and verify the table below. We need your feedback to measure the accuracy of these suggestions and the value they add. If you are rejecting a certain code suggestion, please mention the reason briefly in the suggestion for us to capture it.
Comment Accepted (Y/N) Reason
#### Log Improvement Suggestion No: 1
#### Log Improvement Suggestion No: 2

@coderabbitai

coderabbitai Bot commented Jun 2, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 3a9672a8-079d-49d9-8478-6c4ed7650cd3

📥 Commits

Reviewing files that changed from the base of the PR and between 89d86f3 and ba75a43.

📒 Files selected for processing (2)
  • components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/AMDefaultKeyManagerImpl.java
  • components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/kmclient/model/ScopeClient.java
🚧 Files skipped from review as they are similar to previous changes (2)
  • components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/AMDefaultKeyManagerImpl.java
  • components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/kmclient/model/ScopeClient.java

📝 Walkthrough

Walkthrough

Adds URL-safe, no-padding Base64 encoding for scope identifiers in AMDefaultKeyManagerImpl and updates ScopeClient request lines to include encoded=true for scope-by-name operations.

Changes

Scope Identifier Encoding in Key Manager API Calls

Layer / File(s) Summary
Base64 encoding helper and caller updates
components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/AMDefaultKeyManagerImpl.java
A new base64UrlEncode(String value) helper is added and applied at getScopeByName, deleteScope, updateScope, and isScopeExists to send URL-safe Base64 (no padding) encoded scope identifiers to ScopeClient.
ScopeClient endpoint query parameters
components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/kmclient/model/ScopeClient.java
@RequestLine annotations for getScopeByName, updateScope, and deleteScope now include ?encoded=true; isScopeExist appends &encoded=true to its existing query string.

Sequence Diagram

sequenceDiagram
  participant Caller as Scope API Consumer
  participant AMDefaultKeyManagerImpl
  participant ScopeClient
  participant KMAPI as Key Manager API

  Caller->>AMDefaultKeyManagerImpl: getScopeByName(scopeName)
  AMDefaultKeyManagerImpl->>AMDefaultKeyManagerImpl: base64UrlEncode(scopeName)
  AMDefaultKeyManagerImpl->>ScopeClient: getScopeByName(encodedName)
  ScopeClient->>KMAPI: GET /name/{encodedName}?encoded=true
  KMAPI->>ScopeClient: ScopeDTO
  ScopeClient->>AMDefaultKeyManagerImpl: ScopeDTO
  AMDefaultKeyManagerImpl->>Caller: ScopeDTO
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 45.45% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main change: introducing base64 encoded scope names as path parameters for the scope client.
Description check ✅ Passed The description is related to the changeset, explaining that the PR addresses an issue with creating scopes containing special characters.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (1)
components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/AMDefaultKeyManagerImpl.java (1)

1655-1664: 🏗️ Heavy lift

Add regression coverage for reserved-character scope names.

This helper is the new choke point for getScopeByName, deleteScope, updateScope, and isScopeExists, but I do not see any coverage in this cohort proving those calls round-trip names such as read/write, scope:admin, or %scope through the new encoded=true contract.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In
`@components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/AMDefaultKeyManagerImpl.java`
around lines 1655 - 1664, The new base64UrlEncode(String) helper is the choke
point for scope name handling but there are no regression tests ensuring scopes
with reserved characters round-trip via the encoded=true contract; add tests
that exercise getScopeByName, deleteScope, updateScope, and isScopeExists using
base64UrlEncode on names like "read/write", "scope:admin", and "%scope",
asserting that creating a scope with the raw name then
retrieving/updating/deleting it via the encoded=true endpoint (or service call)
succeeds and returns the original raw name; reference base64UrlEncode,
getScopeByName, deleteScope, updateScope, and isScopeExists in tests to ensure
the encoding/decoding path is covered.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In
`@components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/AMDefaultKeyManagerImpl.java`:
- Around line 1359-1361: In AMDefaultKeyManagerImpl.deleteScope, the Response
returned by scopeClient.deleteScope(base64UrlEncode(scopeName)) is not closed,
causing resource leaks; wrap the call in a try-with-resources (try (Response
response = scopeClient.deleteScope(...)) { ... }) so the Response is always
closed, and move existing response.status() checks and response.body() handling
inside that block to ensure the body is consumed or closed in both OK and non-OK
branches; reference the scopeClient.deleteScope(...) call and the
response.status()/response.body() usages when applying the change.

---

Nitpick comments:
In
`@components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/AMDefaultKeyManagerImpl.java`:
- Around line 1655-1664: The new base64UrlEncode(String) helper is the choke
point for scope name handling but there are no regression tests ensuring scopes
with reserved characters round-trip via the encoded=true contract; add tests
that exercise getScopeByName, deleteScope, updateScope, and isScopeExists using
base64UrlEncode on names like "read/write", "scope:admin", and "%scope",
asserting that creating a scope with the raw name then
retrieving/updating/deleting it via the encoded=true endpoint (or service call)
succeeds and returns the original raw name; reference base64UrlEncode,
getScopeByName, deleteScope, updateScope, and isScopeExists in tests to ensure
the encoding/decoding path is covered.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 6e78313c-32cc-46ce-ba2d-af8cdac609a4

📥 Commits

Reviewing files that changed from the base of the PR and between 46b5af0 and 89d86f3.

📒 Files selected for processing (2)
  • components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/AMDefaultKeyManagerImpl.java
  • components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/kmclient/model/ScopeClient.java

@O-sura O-sura force-pushed the special-char-scopes branch from 89d86f3 to ba75a43 Compare June 3, 2026 03:13
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