Fix cascade deletion of client roles in keycloak_generic_role_mapper #1357
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Overview
This PR fixes a critical bug in the
DeleteRoleScopeMappingfunction where deleting a single client role from a scope would inadvertently delete all client roles from that scope due to an empty request body being sent to the Keycloak API.Description
When using
keycloak_generic_role_mapperto manage client roles in scopes, deleting one mapper resource would cause all other client role mappers in the same scope to be removed from Keycloak, even though Terraform only showed one resource being deleted.Root Cause
The issue was in the
DeleteRoleScopeMappingfunction inrole_scope_mapping.go. For client roles (role.ClientRole = true), the DELETE request was sent with an empty body (nil), while the Keycloak API expects the specific roles to be deleted in the request body as an array.This contrasted with realm roles, which correctly included the role data in the request body.
Solution
This PR standardizes the deletion behavior by ensuring both client roles and realm roles include the role data in the DELETE request body:
Changes Made
DeleteRoleScopeMappingfunction inrole_scope_mapping.goTesting
Impact
Before
terraform applycycles to achieve intended resultAfter
terraform applyachieves intended resultBackward Compatibility
This change is fully backward compatible:
Related Issues
Test Scenarios
Scenario 1: Single Role Deletion
Scenario 2: Multiple Client Scopes
Verification Steps
keycloak_generic_role_mapperresources for client rolesterraform plan- should show only one deletionterraform apply- should remove only the intended roleFiles Changed
keycloak/role_scope_mapping.go- Fixed DeleteRoleScopeMapping functionBreaking Changes
None. This is a bug fix that corrects unintended behavior.
Additional Notes
This fix brings consistency between client role and realm role deletion behavior, making the provider more predictable and reliable for managing Keycloak role scope mappings.