Skip to content

bugfix: auto delete roles relate to cluster#5395

Merged
nobodyiam merged 6 commits intoapolloconfig:masterfrom
spaceluke:bugfix-autodelete-role
Jun 10, 2025
Merged

bugfix: auto delete roles relate to cluster#5395
nobodyiam merged 6 commits intoapolloconfig:masterfrom
spaceluke:bugfix-autodelete-role

Conversation

@spaceluke
Copy link
Member

@spaceluke spaceluke commented May 31, 2025

What's the purpose of this PR

fix bug

Which issue(s) this PR fixes:

Fixes ##5392

Brief changelog

fix sql conditions for quick fix

Follow this checklist to help us incorporate your contribution quickly and easily:

  • Read the Contributing Guide before making this pull request.
  • Write a pull request description that is detailed enough to understand what the pull request does, how, and why.
  • Write necessary unit tests to verify the code.
  • Run mvn clean test to make sure this pull request doesn't break anything.
  • Update the CHANGES log.

Summary by CodeRabbit

  • Documentation

    • Updated release notes to include a bug fix related to the automatic deletion of cluster roles.
  • Bug Fixes

    • Improved handling of cluster-related roles and permissions to ensure they are properly deleted when a cluster is removed.

@dosubot dosubot bot added the size:XS This PR changes 0-9 lines, ignoring generated files. label May 31, 2025
@coderabbitai
Copy link
Contributor

coderabbitai bot commented May 31, 2025

Walkthrough

A release note was added for a bugfix in Apollo 2.5.0 concerning automatic deletion of cluster-related roles. The RoleRepository query findRoleIdsByAppId was extended to include additional cluster role name patterns. New methods were added in RoleRepository and PermissionRepository to query roles and permissions by cluster. The ClusterService was updated to delete related role permissions when deleting a cluster. Corresponding service interface and implementation methods for deleting role permissions by cluster were added.

Changes

File(s) Change Summary
CHANGES.md Added a release note for a bugfix about automatic deletion of cluster-related roles.
apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/repository/RoleRepository.java Extended findRoleIdsByAppId query to include cluster role patterns; added findRoleIdsByCluster method.
apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/repository/PermissionRepository.java Modified findPermissionIdsByAppIdAndNamespace to filter permission types; added findPermissionIdsByAppIdAndEnvAndCluster method.
apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/service/ClusterService.java Injected RolePermissionService; updated deleteCluster to remove cluster-related role permissions.
apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/service/RolePermissionService.java Added method declaration deleteRolePermissionsByCluster.
apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/spi/defaultimpl/DefaultRolePermissionService.java Implemented deleteRolePermissionsByCluster to delete permissions, roles, and mappings scoped by cluster.
apollo-portal/src/test/java/com/ctrip/framework/apollo/portal/spi/defaultImpl/RolePermissionServiceTest.java Added test method verifying deletion of cluster-related roles during deleteRolePermissionsByAppId.
apollo-portal/src/test/resources/sql/permission/RolePermissionServiceTest.deleteRolePermissionsByAppIdWithClusterRoles.sql Added SQL test data inserting cluster-related permissions, roles, and role-permission mappings.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant ClusterService
    participant RolePermissionService
    participant ClusterAPI

    Client->>ClusterService: deleteCluster(env, appId, clusterName)
    ClusterService->>ClusterAPI: delete(env, appId, clusterName)
    ClusterService->>RolePermissionService: deleteRolePermissionsByCluster(appId, env, clusterName, operator)
    RolePermissionService->>RolePermissionService: delete permissions and roles related to cluster
    RolePermissionService-->>ClusterService: deletion complete
    ClusterService-->>Client: cluster deleted with related roles/permissions removed
Loading

Possibly related PRs

Suggested labels

size:M, lgtm

Suggested reviewers

  • spaceluke

Poem

🐇 A cluster hops, roles in line,
Deletion neat, permissions fine.
Queries stretched, new paths to see,
Apollo’s portal runs with glee.
Bugs fixed, roles cleared with care,
A rabbit’s cheer fills the air! 🌿✨


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between a7fff0a and 47bbd8b.

📒 Files selected for processing (1)
  • CHANGES.md (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • CHANGES.md
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: build (8)

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.

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.

@spaceluke spaceluke requested review from Copilot and nobodyiam June 1, 2025 12:00
Copy link
Contributor

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

This PR fixes a bug related to the automatic deletion of roles associated with clusters by updating the SQL conditions in the RoleRepository.

  • Update the JPQL query in RoleRepository to include patterns for cluster-related roles.
  • Update CHANGES.md to log the bugfix.

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/repository/RoleRepository.java Updated SQL query conditions to include new patterns for cluster roles.
CHANGES.md Added a bugfix entry for auto deletion of cluster-related roles.
Comments suppressed due to low confidence (2)

apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/repository/RoleRepository.java:39

  • [nitpick] Verify the naming consistency: the new cluster-specific role pattern uses 'ModifyNamespacesInCluster' while existing patterns use the singular 'ModifyNamespace'. If intentional, consider documenting the naming convention.
+      + "OR r.roleName like CONCAT('ModifyNamespacesInCluster+', ?1, '+%')"

apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/repository/RoleRepository.java:40

  • [nitpick] Verify the naming consistency: the new cluster-specific role pattern uses 'ReleaseNamespacesInCluster' while other related patterns use the singular form. If this is intentional, consider clarifying the convention in a comment.
+      + "OR r.roleName like CONCAT('ReleaseNamespacesInCluster+', ?1, '+%')"

@spaceluke spaceluke marked this pull request as draft June 3, 2025 03:26
@spaceluke spaceluke marked this pull request as ready for review June 5, 2025 15:26
@dosubot dosubot bot removed the size:XS This PR changes 0-9 lines, ignoring generated files. label Jun 5, 2025
@spaceluke spaceluke requested a review from nobodyiam June 5, 2025 15:26
@dosubot dosubot bot added the size:M This PR changes 30-99 lines, ignoring generated files. label Jun 5, 2025
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: 0

🧹 Nitpick comments (1)
apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/service/ClusterService.java (1)

26-26: Remove unused import.

The javax.transaction.Transactional import is not used in this file.

-import javax.transaction.Transactional;
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 593e7eb and b05d0ab.

📒 Files selected for processing (5)
  • apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/repository/PermissionRepository.java (1 hunks)
  • apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/repository/RoleRepository.java (2 hunks)
  • apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/service/ClusterService.java (3 hunks)
  • apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/service/RolePermissionService.java (1 hunks)
  • apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/spi/defaultimpl/DefaultRolePermissionService.java (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/repository/RoleRepository.java
🔇 Additional comments (5)
apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/service/RolePermissionService.java (1)

91-94: LGTM! Method declaration follows established patterns.

The new method declaration is consistent with existing deletion methods and properly documents the cluster-scoped deletion functionality.

apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/repository/PermissionRepository.java (1)

54-56:

Details

✅ Verification successful

Verify the targetId format consistency across the codebase.

The query logic appears correct, but please ensure the targetId format (appId + '+' + env + '+' + clusterName) is consistent with how cluster-scoped permissions are created elsewhere in the codebase.


🏁 Script executed:

#!/bin/bash
# Description: Verify how cluster-scoped permission targetIds are constructed in the codebase
# Expected: Find patterns showing targetId construction for cluster permissions

# Search for targetId construction patterns involving cluster names
rg -A 3 -B 3 "targetId.*\+.*cluster|cluster.*\+.*targetId" --type java

# Search for permission creation with cluster context
ast-grep --pattern 'new Permission($$$)' | grep -A 10 -B 10 cluster

# Search for ModifyNamespacesInCluster and ReleaseNamespacesInCluster usage
rg "ModifyNamespacesInCluster|ReleaseNamespacesInCluster" --type java -A 2 -B 2

Length of output: 19582


🏁 Script executed:

#!/bin/bash
# Inspect createClusterRole method in DefaultRoleInitializationService
rg -A10 -B2 "createClusterRole" --type java apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/spi/defaultimpl/DefaultRoleInitializationService.java

Length of output: 1938


🏁 Script executed:

#!/bin/bash
# Show buildClusterTargetId method and STRING_JOINER definition in RoleUtils
rg -n "buildClusterTargetId" apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/util/RoleUtils.java
rg -A5 -B5 "buildClusterTargetId" apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/util/RoleUtils.java
rg -A5 -B5 "STRING_JOINER" apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/util/RoleUtils.java

Length of output: 4516


🏁 Script executed:

#!/bin/bash
# Locate ConfigConsts to verify CLUSTER_NAMESPACE_SEPARATOR value
rg -n "class ConfigConsts" -n .
rg -A2 -B2 "CLUSTER_NAMESPACE_SEPARATOR" --no-heading

Length of output: 21172


targetId format is consistent with buildClusterTargetId

The SQL in PermissionRepository

WHERE p.targetId = CONCAT(?1, '+', ?2, '+', ?3)…

matches exactly what RoleUtils.buildClusterTargetId(appId, env, clusterName) produces (it joins on ConfigConsts.CLUSTER_NAMESPACE_SEPARATOR, which is '+'). No further changes needed.

apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/service/ClusterService.java (2)

37-37: LGTM! Proper dependency injection.

The RolePermissionService dependency is correctly injected through constructor injection.

Also applies to: 40-41, 45-45


68-70: LGTM! Proper cleanup integration.

The role permission cleanup is correctly called after the cluster deletion, ensuring data consistency.

apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/spi/defaultimpl/DefaultRolePermissionService.java (1)

352-377:

Details

✅ Verification successful

LGTM! Implementation follows established patterns correctly.

The implementation is consistent with existing deletion methods and properly handles the deletion order to maintain referential integrity. The transactional boundary and error handling are appropriate.

Please verify that the roleRepository.findRoleIdsByCluster method exists, as it's called but not shown in the provided files:


🏁 Script executed:

#!/bin/bash
# Description: Verify the findRoleIdsByCluster method exists in RoleRepository
# Expected: Find the method declaration and implementation

# Search for the method in RoleRepository
ast-grep --pattern 'findRoleIdsByCluster($_,$_,$_)'

# Search for any references to this method
rg "findRoleIdsByCluster" --type java -A 2 -B 2

Length of output: 1492


Confirmed: findRoleIdsByCluster Exists – No Further Action Needed

I’ve verified that RoleRepository declares the required method:

  • In apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/repository/RoleRepository.java:
    List<Long> findRoleIdsByCluster(String appId, String env, String clusterName);

Everything aligns with the deletion logic in DefaultRolePermissionService. No changes are required.


@Transactional
@Override
public void deleteRolePermissionsByCluster(String appId, String env, String clusterName, String operator) {
Copy link
Member

Choose a reason for hiding this comment

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

We could move the Codex-generated test to this PR.

@dosubot dosubot bot added size:L This PR changes 100-499 lines, ignoring generated files. and removed size:M This PR changes 30-99 lines, ignoring generated files. labels Jun 8, 2025
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
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between b05d0ab and a7fff0a.

📒 Files selected for processing (6)
  • CHANGES.md (1 hunks)
  • apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/repository/PermissionRepository.java (1 hunks)
  • apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/repository/RoleRepository.java (2 hunks)
  • apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/spi/defaultimpl/DefaultRolePermissionService.java (1 hunks)
  • apollo-portal/src/test/java/com/ctrip/framework/apollo/portal/spi/defaultImpl/RolePermissionServiceTest.java (3 hunks)
  • apollo-portal/src/test/resources/sql/permission/RolePermissionServiceTest.deleteRolePermissionsByAppIdWithClusterRoles.sql (1 hunks)
✅ Files skipped from review due to trivial changes (1)
  • apollo-portal/src/test/resources/sql/permission/RolePermissionServiceTest.deleteRolePermissionsByAppIdWithClusterRoles.sql
🚧 Files skipped from review as they are similar to previous changes (4)
  • apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/repository/RoleRepository.java
  • apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/repository/PermissionRepository.java
  • apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/spi/defaultimpl/DefaultRolePermissionService.java
  • CHANGES.md
🧰 Additional context used
🧬 Code Graph Analysis (1)
apollo-portal/src/test/java/com/ctrip/framework/apollo/portal/spi/defaultImpl/RolePermissionServiceTest.java (1)
apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/util/RoleUtils.java (1)
  • RoleUtils (25-124)
🔇 Additional comments (1)
apollo-portal/src/test/java/com/ctrip/framework/apollo/portal/spi/defaultImpl/RolePermissionServiceTest.java (1)

325-340: LGTM! Well-structured test for cluster role deletion.

The test correctly verifies that cluster-related roles are properly deleted when calling deleteRolePermissionsByAppId. The test logic is sound:

  1. Sets up test data via SQL script
  2. Calls the deletion method with a specific app ID
  3. Uses RoleUtils to construct expected role names (consistent with the utility class design)
  4. Verifies that the cluster roles no longer exist after deletion

This test provides good coverage for the cluster role deletion functionality mentioned in the PR objectives.

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertNull;
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Fix inconsistent testing framework imports.

The test class uses JUnit 4 throughout (imports on lines 19-21, @Test annotations), but line 22 imports assertNull from JUnit 5. This creates inconsistency and potential conflicts.

Use the JUnit 4 equivalent instead:

-import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.Assert.assertNull;
🤖 Prompt for AI Agents
In
apollo-portal/src/test/java/com/ctrip/framework/apollo/portal/spi/defaultImpl/RolePermissionServiceTest.java
at line 22, replace the import of assertNull from JUnit 5 with the JUnit 4
equivalent to maintain consistency with the rest of the test class that uses
JUnit 4. Change the import statement to use org.junit.Assert.assertNull instead
of org.junit.jupiter.api.Assertions.assertNull.

@spaceluke spaceluke requested a review from nobodyiam June 8, 2025 15:14
Copy link
Member

@nobodyiam nobodyiam left a comment

Choose a reason for hiding this comment

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

LGTM

@dosubot dosubot bot added the lgtm This PR has been approved by a maintainer label Jun 10, 2025
@nobodyiam nobodyiam merged commit f4dc94e into apolloconfig:master Jun 10, 2025
8 checks passed
@github-actions github-actions bot locked and limited conversation to collaborators Jun 10, 2025
@nobodyiam nobodyiam added this to the 2.5.0 milestone Feb 19, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

lgtm This PR has been approved by a maintainer size:L This PR changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants