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

Clear sharing policy store based on related resource deletion #433

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@
<groupId>org.wso2.carbon.identity.framework</groupId>
<artifactId>org.wso2.carbon.identity.central.log.mgt</artifactId>
</dependency>
<dependency>
<groupId>org.wso2.carbon.identity.organization.management</groupId>
<artifactId>org.wso2.carbon.identity.organization.resource.sharing.policy.management</artifactId>
</dependency>
<!--Test Dependencies-->
<dependency>
<groupId>org.testng</groupId>
Expand Down Expand Up @@ -155,6 +159,12 @@
org.wso2.carbon.identity.core.util;version="${carbon.identity.package.import.version.range}",
org.wso2.carbon.context;version="${carbon.kernel.package.import.version.range}",
org.wso2.carbon;version="${carbon.kernel.package.import.version.range}",
org.wso2.carbon.identity.organization.resource.sharing.policy.management;
version="${org.wso2.identity.organization.mgt.imp.pkg.version.range}",
org.wso2.carbon.identity.organization.resource.sharing.policy.management.constant;
version="${org.wso2.identity.organization.mgt.imp.pkg.version.range}",
org.wso2.carbon.identity.organization.resource.sharing.policy.management.exception;
version="${org.wso2.identity.organization.mgt.imp.pkg.version.range}",
</Import-Package>
</instructions>
</configuration>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Copyright (c) 2025, WSO2 LLC. (http://www.wso2.com).
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.wso2.carbon.identity.organization.management.handler;

import org.wso2.carbon.identity.event.IdentityEventException;
import org.wso2.carbon.identity.event.event.Event;
import org.wso2.carbon.identity.event.handler.AbstractEventHandler;
import org.wso2.carbon.identity.organization.management.handler.internal.OrganizationManagementHandlerDataHolder;
import org.wso2.carbon.identity.organization.resource.sharing.policy.management.ResourceSharingPolicyHandlerService;
import org.wso2.carbon.identity.organization.resource.sharing.policy.management.constant.ResourceType;
import org.wso2.carbon.identity.organization.resource.sharing.policy.management.constant.SharedAttributeType;
import org.wso2.carbon.identity.organization.resource.sharing.policy.management.exception.ResourceSharingPolicyMgtException;

import java.util.Map;

import static org.wso2.carbon.identity.event.IdentityEventConstants.Event.POST_DELETE_ROLE_V2_EVENT;
import static org.wso2.carbon.identity.event.IdentityEventConstants.Event.POST_DELETE_USER_WITH_ID;
import static org.wso2.carbon.identity.event.IdentityEventConstants.EventProperty.ROLE_ID;
import static org.wso2.carbon.identity.event.IdentityEventConstants.EventProperty.USER_ID;

/**
* Event handler to clean up the stored sharing policies when associated resources are deleted.
*/
public class SharingPolicyCleanUpHandler extends AbstractEventHandler {

@Override
public void handleEvent(Event event) throws IdentityEventException {

String eventName = event.getEventName();
Map<String, Object> eventProperties = event.getEventProperties();
switch (eventName) {
case POST_DELETE_ROLE_V2_EVENT:
String deletedRoleId = (String) eventProperties.get(ROLE_ID);
deleteSharedResourceAttributesByRoleId(deletedRoleId);
break;
case POST_DELETE_USER_WITH_ID:
String deletedUserId = (String) eventProperties.get(USER_ID);
deleteResourceSharingPoliciesOfUser(deletedUserId);
break;
default:
break;
}
}

private void deleteResourceSharingPoliciesOfUser(String deletedUserId) throws IdentityEventException {

try {
getResourceSharingPolicyHandlerService().deleteResourceSharingPolicyByResourceTypeAndId(
ResourceType.USER, deletedUserId);
} catch (ResourceSharingPolicyMgtException e) {
throw new IdentityEventException(e.getErrorCode(), e.getMessage(), e);
}
}

private void deleteSharedResourceAttributesByRoleId(String deletedRoleId) throws IdentityEventException {

try {
getResourceSharingPolicyHandlerService().deleteSharedResourceAttributeByAttributeTypeAndId(
SharedAttributeType.ROLE, deletedRoleId);
} catch (ResourceSharingPolicyMgtException e) {
throw new IdentityEventException(e.getErrorCode(), e.getMessage(), e);
}
}

private static ResourceSharingPolicyHandlerService getResourceSharingPolicyHandlerService() {

return OrganizationManagementHandlerDataHolder.getInstance().getResourceSharingPolicyHandlerService();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.wso2.carbon.identity.governance.IdentityGovernanceService;
import org.wso2.carbon.identity.organization.management.application.OrgApplicationManager;
import org.wso2.carbon.identity.organization.management.service.OrganizationManager;
import org.wso2.carbon.identity.organization.resource.sharing.policy.management.ResourceSharingPolicyHandlerService;
import org.wso2.carbon.identity.role.v2.mgt.core.RoleManagementService;
import org.wso2.carbon.user.core.service.RealmService;

Expand All @@ -41,6 +42,7 @@ public class OrganizationManagementHandlerDataHolder {
private OrgApplicationManager orgApplicationManager;
private ApplicationManagementService applicationManagementService;
private RealmService realmService;
private ResourceSharingPolicyHandlerService resourceSharingPolicyHandlerService;

public static OrganizationManagementHandlerDataHolder getInstance() {

Expand Down Expand Up @@ -187,5 +189,26 @@ public void setRealmService(RealmService realmService) {

this.realmService = realmService;
}

/**
* Get {@link ResourceSharingPolicyHandlerService}.
*
* @return ResourceSharingPolicyHandlerService {@link ResourceSharingPolicyHandlerService}.
*/
public ResourceSharingPolicyHandlerService getResourceSharingPolicyHandlerService() {

return resourceSharingPolicyHandlerService;
}

/**
* Set {@link ResourceSharingPolicyHandlerService}.
*
* @param resourceSharingPolicyHandlerService Instance of {@link ResourceSharingPolicyHandlerService}.
*/
public void setResourceSharingPolicyHandlerService(
ResourceSharingPolicyHandlerService resourceSharingPolicyHandlerService) {

this.resourceSharingPolicyHandlerService = resourceSharingPolicyHandlerService;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@
import org.wso2.carbon.identity.organization.management.application.OrgApplicationManager;
import org.wso2.carbon.identity.organization.management.handler.GovernanceConfigUpdateHandler;
import org.wso2.carbon.identity.organization.management.handler.SharedRoleMgtHandler;
import org.wso2.carbon.identity.organization.management.handler.SharingPolicyCleanUpHandler;
import org.wso2.carbon.identity.organization.management.handler.listener.SharedRoleMgtListener;
import org.wso2.carbon.identity.organization.management.service.OrganizationManager;
import org.wso2.carbon.identity.organization.resource.sharing.policy.management.ResourceSharingPolicyHandlerService;
import org.wso2.carbon.identity.role.v2.mgt.core.RoleManagementService;
import org.wso2.carbon.user.core.service.RealmService;

Expand Down Expand Up @@ -64,6 +66,7 @@ protected void activate(ComponentContext componentContext) {
bundleContext.registerService(AbstractEventHandler.class, new GovernanceConfigUpdateHandler(), null);
bundleContext.registerService(AbstractEventHandler.class, new SharedRoleMgtHandler(), null);
bundleContext.registerService(ApplicationMgtListener.class.getName(), new SharedRoleMgtListener(), null);
bundleContext.registerService(AbstractEventHandler.class, new SharingPolicyCleanUpHandler(), null);
LOG.debug("Organization management handler component activated successfully.");
} catch (Throwable e) {
LOG.error("Error while activating organization management handler module.", e);
Expand Down Expand Up @@ -191,4 +194,23 @@ protected void unsetRealmService(RealmService realmService) {
OrganizationManagementHandlerDataHolder.getInstance().setRealmService(null);
LOG.debug("Realm service unset in OrganizationManagementHandlerService bundle.");
}

@Reference(
name = "ResourceSharingPolicyHandlerService",
service = ResourceSharingPolicyHandlerService.class,
cardinality = ReferenceCardinality.MANDATORY,
policy = ReferencePolicy.DYNAMIC,
unbind = "unsetResourceSharingPolicyHandlerService")
protected void setResourceSharingPolicyHandlerService(
ResourceSharingPolicyHandlerService resourceSharingPolicyHandlerService) {

OrganizationManagementHandlerDataHolder.getInstance()
.setResourceSharingPolicyHandlerService(resourceSharingPolicyHandlerService);
}

protected void unsetResourceSharingPolicyHandlerService(
ResourceSharingPolicyHandlerService resourceSharingPolicyHandlerService) {

OrganizationManagementHandlerDataHolder.getInstance().setResourceSharingPolicyHandlerService(null);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2023, WSO2 LLC. (https://www.wso2.com).
* Copyright (c) 2023-2025, WSO2 LLC. (https://www.wso2.com).
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
Expand All @@ -16,7 +16,7 @@
* under the License.
*/

package org.wso2.carbon.identity.organization.management.handler.tests;
package org.wso2.carbon.identity.organization.management.handler;

import org.mockito.InjectMocks;
import org.mockito.Mock;
Expand All @@ -30,7 +30,6 @@
import org.wso2.carbon.identity.governance.IdentityGovernanceException;
import org.wso2.carbon.identity.governance.IdentityGovernanceService;
import org.wso2.carbon.identity.organization.management.ext.Constants;
import org.wso2.carbon.identity.organization.management.handler.GovernanceConfigUpdateHandler;
import org.wso2.carbon.identity.organization.management.handler.internal.OrganizationManagementHandlerDataHolder;
import org.wso2.carbon.identity.organization.management.service.OrganizationManager;
import org.wso2.carbon.identity.organization.management.service.exception.OrganizationManagementException;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2024, WSO2 LLC. (http://www.wso2.com).
* Copyright (c) 2024-2025, WSO2 LLC. (http://www.wso2.com).
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
Expand All @@ -16,7 +16,7 @@
* under the License.
*/

package org.wso2.carbon.identity.organization.management.handler.tests;
package org.wso2.carbon.identity.organization.management.handler;
AnuradhaSK marked this conversation as resolved.
Show resolved Hide resolved

import org.mockito.MockedStatic;
import org.testng.annotations.BeforeClass;
Expand All @@ -31,7 +31,6 @@
import org.wso2.carbon.identity.event.IdentityEventException;
import org.wso2.carbon.identity.event.event.Event;
import org.wso2.carbon.identity.organization.management.application.constant.OrgApplicationMgtConstants;
import org.wso2.carbon.identity.organization.management.handler.SharedRoleMgtHandler;
import org.wso2.carbon.identity.organization.management.handler.internal.OrganizationManagementHandlerDataHolder;
import org.wso2.carbon.identity.organization.management.service.OrganizationManager;
import org.wso2.carbon.identity.role.v2.mgt.core.RoleManagementService;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* Copyright (c) 2025, WSO2 LLC. (http://www.wso2.com).
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.wso2.carbon.identity.organization.management.handler;

import org.mockito.Mock;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import org.wso2.carbon.identity.event.event.Event;
import org.wso2.carbon.identity.organization.management.handler.internal.OrganizationManagementHandlerDataHolder;
import org.wso2.carbon.identity.organization.resource.sharing.policy.management.ResourceSharingPolicyHandlerService;
import org.wso2.carbon.identity.organization.resource.sharing.policy.management.constant.ResourceType;

import java.util.HashMap;
import java.util.Map;

import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.MockitoAnnotations.openMocks;
import static org.wso2.carbon.identity.event.IdentityEventConstants.Event.POST_DELETE_ROLE_V2_EVENT;
import static org.wso2.carbon.identity.event.IdentityEventConstants.Event.POST_DELETE_USER_WITH_ID;
import static org.wso2.carbon.identity.event.IdentityEventConstants.EventProperty.ROLE_ID;
import static org.wso2.carbon.identity.event.IdentityEventConstants.EventProperty.USER_ID;
import static org.wso2.carbon.identity.organization.resource.sharing.policy.management.constant.SharedAttributeType.ROLE;

/**
* Unit tests for SharingPolicyCleanUpHandler.
*/
public class SharingPolicyCleanUpHandlerTest {

@Mock
ResourceSharingPolicyHandlerService resourceSharingPolicyHandlerService;

@BeforeMethod
public void setUp() {

openMocks(this);
OrganizationManagementHandlerDataHolder.getInstance()
.setResourceSharingPolicyHandlerService(resourceSharingPolicyHandlerService);
}

@AfterMethod
public void tearDown() {

reset(resourceSharingPolicyHandlerService);
}

@Test
public void testPosUserDeleteHandleMethod() throws Exception {

// Trigger the event.
Map<String, Object> eventProperties = new HashMap<>();
eventProperties.put(USER_ID, "user-id");
Event event = new Event(POST_DELETE_USER_WITH_ID, eventProperties);

SharingPolicyCleanUpHandler sharingPolicyCleanUpHandler = new SharingPolicyCleanUpHandler();
sharingPolicyCleanUpHandler.handleEvent(event);
verify(resourceSharingPolicyHandlerService,
times(1)).deleteResourceSharingPolicyByResourceTypeAndId(
ResourceType.USER, "user-id");
}

@Test
public void testPostRoleDeletionHandlerMethod() throws Exception {

// Trigger the event.
Map<String, Object> eventProperties = new HashMap<>();
eventProperties.put(ROLE_ID, "role-id");
Event event = new Event(POST_DELETE_ROLE_V2_EVENT, eventProperties);

SharingPolicyCleanUpHandler sharingPolicyCleanUpHandler = new SharingPolicyCleanUpHandler();
sharingPolicyCleanUpHandler.handleEvent(event);
verify(resourceSharingPolicyHandlerService, times(1)).
deleteSharedResourceAttributeByAttributeTypeAndId(ROLE, "role-id");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<!--
~ Copyright (c) 2025, WSO2 LLC. (http://www.wso2.com).
~
~ WSO2 LLC. licenses this file to you under the Apache License,
~ Version 2.0 (the "License"); you may not use this file except
~ in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing,
~ software distributed under the License is distributed on an
~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
~ KIND, either express or implied. See the License for the
~ specific language governing permissions and limitations
~ under the License.
-->

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >

<suite name="org.wso2.carbon.identity.organization.management.handler.suite">
<test name="org.wso2.carbon.identity.organization.management.handler.tests" preserve-order="true" parallel="false">
<classes>
<class name="org.wso2.carbon.identity.organization.management.handler.listener.SharedRoleMgtListenerTest"/>
<class name="org.wso2.carbon.identity.organization.management.handler.GovernanceConfigUpdateHandlerTest"/>
<class name="org.wso2.carbon.identity.organization.management.handler.SharedRoleMgtHandlerTest"/>
<class name="org.wso2.carbon.identity.organization.management.handler.SharingPolicyCleanUpHandlerTest"/>
</classes>
</test>
</suite>
Loading