From 0cb68cace71c96da90ebf35b97fcb73c27ccb426 Mon Sep 17 00:00:00 2001 From: malithie Date: Thu, 15 Aug 2024 19:41:30 +0530 Subject: [PATCH 1/4] Add action executor config. --- .../execution/ActionExecutorServiceImpl.java | 9 +-- .../execution/util/ActionExecutorConfig.java | 64 +++++++++++++++ .../util/ActionExecutorConfigTest.java | 77 +++++++++++++++++++ 3 files changed, 143 insertions(+), 7 deletions(-) create mode 100644 components/action-mgt/org.wso2.carbon.identity.action.execution/src/main/java/org/wso2/carbon/identity/action/execution/util/ActionExecutorConfig.java create mode 100644 components/action-mgt/org.wso2.carbon.identity.action.execution/src/test/java/org/wso2/carbon/identity/action/execution/util/ActionExecutorConfigTest.java diff --git a/components/action-mgt/org.wso2.carbon.identity.action.execution/src/main/java/org/wso2/carbon/identity/action/execution/ActionExecutorServiceImpl.java b/components/action-mgt/org.wso2.carbon.identity.action.execution/src/main/java/org/wso2/carbon/identity/action/execution/ActionExecutorServiceImpl.java index ebbbce78ca62..4e395bbacc55 100644 --- a/components/action-mgt/org.wso2.carbon.identity.action.execution/src/main/java/org/wso2/carbon/identity/action/execution/ActionExecutorServiceImpl.java +++ b/components/action-mgt/org.wso2.carbon.identity.action.execution/src/main/java/org/wso2/carbon/identity/action/execution/ActionExecutorServiceImpl.java @@ -38,13 +38,13 @@ import org.wso2.carbon.identity.action.execution.model.AllowedOperation; import org.wso2.carbon.identity.action.execution.model.PerformableOperation; import org.wso2.carbon.identity.action.execution.util.APIClient; +import org.wso2.carbon.identity.action.execution.util.ActionExecutorConfig; import org.wso2.carbon.identity.action.execution.util.AuthMethods; import org.wso2.carbon.identity.action.execution.util.OperationComparator; import org.wso2.carbon.identity.action.management.exception.ActionMgtException; import org.wso2.carbon.identity.action.management.model.Action; import org.wso2.carbon.identity.action.management.model.AuthProperty; import org.wso2.carbon.identity.action.management.model.AuthType; -import org.wso2.carbon.identity.core.util.IdentityUtil; import java.util.ArrayList; import java.util.List; @@ -79,12 +79,7 @@ public static ActionExecutorServiceImpl getInstance() { @Override public boolean isExecutionEnabled(ActionType actionType) { - switch (actionType) { - case PRE_ISSUE_ACCESS_TOKEN: - return IdentityUtil.isPreIssueAccessTokenActionTypeEnabled(); - default: - return false; - } + return ActionExecutorConfig.getInstance().isExecutionForActionTypeEnabled(actionType); } public ActionExecutionStatus execute(ActionType actionType, Map eventContext, String tenantDomain) diff --git a/components/action-mgt/org.wso2.carbon.identity.action.execution/src/main/java/org/wso2/carbon/identity/action/execution/util/ActionExecutorConfig.java b/components/action-mgt/org.wso2.carbon.identity.action.execution/src/main/java/org/wso2/carbon/identity/action/execution/util/ActionExecutorConfig.java new file mode 100644 index 000000000000..8ce05b9ccbea --- /dev/null +++ b/components/action-mgt/org.wso2.carbon.identity.action.execution/src/main/java/org/wso2/carbon/identity/action/execution/util/ActionExecutorConfig.java @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2024, 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.action.execution.util; + +import org.apache.commons.lang.StringUtils; +import org.wso2.carbon.identity.action.execution.model.ActionType; +import org.wso2.carbon.identity.core.util.IdentityUtil; + +/** + * This class holds the system configurations for the Action Executor Service. + */ +public class ActionExecutorConfig { + + private static final ActionExecutorConfig INSTANCE = new ActionExecutorConfig(); + + private static final String PRE_ISSUE_ACCESS_TOKEN_ACTION_TYPE_ENABLE_PROPERTY = + "Actions.Types.PreIssueAccessToken.Enable"; + + private ActionExecutorConfig() { + + } + + public static ActionExecutorConfig getInstance() { + + return INSTANCE; + } + + public boolean isExecutionForActionTypeEnabled(ActionType actionType) { + + switch (actionType) { + case PRE_ISSUE_ACCESS_TOKEN: + return isActionTypeEnabled(PRE_ISSUE_ACCESS_TOKEN_ACTION_TYPE_ENABLE_PROPERTY); + default: + return false; + } + } + + private boolean isActionTypeEnabled(String actionTypePropertyName) { + + boolean isActionTypeEnabled = false; + String actionTypeEnabledPropertyValue = IdentityUtil.getProperty(actionTypePropertyName); + if (StringUtils.isNotBlank(actionTypeEnabledPropertyValue)) { + return Boolean.parseBoolean(actionTypeEnabledPropertyValue); + } + return isActionTypeEnabled; + } + +} diff --git a/components/action-mgt/org.wso2.carbon.identity.action.execution/src/test/java/org/wso2/carbon/identity/action/execution/util/ActionExecutorConfigTest.java b/components/action-mgt/org.wso2.carbon.identity.action.execution/src/test/java/org/wso2/carbon/identity/action/execution/util/ActionExecutorConfigTest.java new file mode 100644 index 000000000000..445bb7e9184a --- /dev/null +++ b/components/action-mgt/org.wso2.carbon.identity.action.execution/src/test/java/org/wso2/carbon/identity/action/execution/util/ActionExecutorConfigTest.java @@ -0,0 +1,77 @@ +/* + * Copyright (c) 2024, 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.action.execution.util; + +import org.mockito.MockedStatic; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.testng.annotations.AfterMethod; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; +import org.wso2.carbon.identity.action.execution.model.ActionType; +import org.wso2.carbon.identity.core.util.IdentityUtil; + +import static org.testng.Assert.assertFalse; +import static org.testng.Assert.assertTrue; + +public class ActionExecutorConfigTest { + + private ActionExecutorConfig actionExecutorConfig; + + private MockedStatic identityUtil; + + @BeforeMethod + public void setUp() { + + MockitoAnnotations.openMocks(this); + actionExecutorConfig = ActionExecutorConfig.getInstance(); + identityUtil = Mockito.mockStatic(IdentityUtil.class); + } + + @AfterMethod + public void tearDown() { + + identityUtil.close(); + } + + @Test + public void testIsExecutionForActionTypeEnabled_PreIssueAccessToken_Enabled() { + + identityUtil.when(() -> IdentityUtil.getProperty("Actions.Types.PreIssueAccessToken.Enable")) + .thenReturn("true"); + assertTrue(actionExecutorConfig.isExecutionForActionTypeEnabled(ActionType.PRE_ISSUE_ACCESS_TOKEN)); + } + + @Test + public void testIsExecutionForActionTypeEnabled_PreIssueAccessToken_Disabled() { + + identityUtil.when(() -> IdentityUtil.getProperty("Actions.Types.PreIssueAccessToken.Enable")) + .thenReturn("false"); + assertFalse(actionExecutorConfig.isExecutionForActionTypeEnabled(ActionType.PRE_ISSUE_ACCESS_TOKEN)); + } + + @Test + public void testIsExecutionForActionTypeEnabled_PreIssueAccessToken_InvalidValue() { + + identityUtil.when(() -> IdentityUtil.getProperty("Actions.Types.PreIssueAccessToken.Enable")) + .thenReturn("invalid"); + assertFalse(actionExecutorConfig.isExecutionForActionTypeEnabled(ActionType.PRE_ISSUE_ACCESS_TOKEN)); + } + +} From 7db31b78f604d5f04e44c0644d87cf378f5fc593 Mon Sep 17 00:00:00 2001 From: malithie Date: Thu, 15 Aug 2024 19:42:00 +0530 Subject: [PATCH 2/4] Remove dependency to IdentityUtil for configs. --- .../core/util/IdentityCoreConstants.java | 2 -- .../identity/core/util/IdentityUtil.java | 36 ------------------- 2 files changed, 38 deletions(-) diff --git a/components/identity-core/org.wso2.carbon.identity.core/src/main/java/org/wso2/carbon/identity/core/util/IdentityCoreConstants.java b/components/identity-core/org.wso2.carbon.identity.core/src/main/java/org/wso2/carbon/identity/core/util/IdentityCoreConstants.java index 9f5512250fa5..839bf830545d 100644 --- a/components/identity-core/org.wso2.carbon.identity.core/src/main/java/org/wso2/carbon/identity/core/util/IdentityCoreConstants.java +++ b/components/identity-core/org.wso2.carbon.identity.core/src/main/java/org/wso2/carbon/identity/core/util/IdentityCoreConstants.java @@ -109,10 +109,8 @@ public class IdentityCoreConstants { // Actions constants. public static final String MAXIMUM_ACTIONS_PER_TYPE_PROPERTY = "Actions.MaximumActionsPerType"; - public static final String PRE_ISSUE_ACCESS_TOKEN_ACTION_TYPE_ENABLE_PROPERTY = "Actions.Types.PreIssueAccessToken.Enable"; public static final int DEFAULT_MAXIMUM_ACTIONS_PER_TYPE = 1; - public static final boolean DEFAULT_PRE_ISSUE_ACCESS_TOKEN_ACTION_TYPE_ENABLE_VALUE = false; public static class Filter { diff --git a/components/identity-core/org.wso2.carbon.identity.core/src/main/java/org/wso2/carbon/identity/core/util/IdentityUtil.java b/components/identity-core/org.wso2.carbon.identity.core/src/main/java/org/wso2/carbon/identity/core/util/IdentityUtil.java index 8a785047e9ed..87d7f5bc09c6 100644 --- a/components/identity-core/org.wso2.carbon.identity.core/src/main/java/org/wso2/carbon/identity/core/util/IdentityUtil.java +++ b/components/identity-core/org.wso2.carbon.identity.core/src/main/java/org/wso2/carbon/identity/core/util/IdentityUtil.java @@ -1500,42 +1500,6 @@ public static int getMaximumActionsPerActionType() { return maximumActionsPerActionType; } - /** - * Get Pre Issue Access Token Action Type enabled status. - * - * @return Whether the Pre Issue Access Token Action type is enabled or not. - */ - public static boolean isPreIssueAccessTokenActionTypeEnabled() { - - return isActionTypeEnabled(IdentityCoreConstants.PRE_ISSUE_ACCESS_TOKEN_ACTION_TYPE_ENABLE_PROPERTY, - IdentityCoreConstants.DEFAULT_PRE_ISSUE_ACCESS_TOKEN_ACTION_TYPE_ENABLE_VALUE); - } - - /** - * Check whether a given action type is enabled or not. - * - * @param actionTypePropertyName Name of the action type enabled property. - * @param defaultValue Default value of the action type enabled property. - * @return Whether the action type is enabled or not. - */ - private static boolean isActionTypeEnabled(String actionTypePropertyName, boolean defaultValue) { - - boolean isActionTypeEnabled = defaultValue; - String actionTypeEnabledPropertyValue = IdentityUtil.getProperty(actionTypePropertyName); - if (StringUtils.isNotBlank(actionTypeEnabledPropertyValue)) { - if ("true".equalsIgnoreCase(actionTypeEnabledPropertyValue)) { - isActionTypeEnabled = true; - } else if ("false".equalsIgnoreCase(actionTypeEnabledPropertyValue)) { - isActionTypeEnabled = false; - } else { - isActionTypeEnabled = defaultValue; - log.warn("Invalid value for property: " + actionTypePropertyName + - ". Value should be either 'true' or 'false'."); - } - } - return isActionTypeEnabled; - } - /** * Get the Default Items per Page needed to display. * From 6a34cd99d623c84367345b9f15a0d069710804b2 Mon Sep 17 00:00:00 2001 From: malithie Date: Thu, 15 Aug 2024 19:43:09 +0530 Subject: [PATCH 3/4] Add unit test for action executor config. --- .../src/test/resources/testng.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/components/action-mgt/org.wso2.carbon.identity.action.execution/src/test/resources/testng.xml b/components/action-mgt/org.wso2.carbon.identity.action.execution/src/test/resources/testng.xml index 6acc221ecd2a..9550eec84ee7 100644 --- a/components/action-mgt/org.wso2.carbon.identity.action.execution/src/test/resources/testng.xml +++ b/components/action-mgt/org.wso2.carbon.identity.action.execution/src/test/resources/testng.xml @@ -24,6 +24,7 @@ + From bfefdd54bd95762a5d4a4641b93369ac39d9633d Mon Sep 17 00:00:00 2001 From: malithie Date: Thu, 15 Aug 2024 22:03:34 +0530 Subject: [PATCH 4/4] Enable pre issue access token action by default. --- .../org.wso2.carbon.identity.core.server.feature.default.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/features/identity-core/org.wso2.carbon.identity.core.server.feature/resources/org.wso2.carbon.identity.core.server.feature.default.json b/features/identity-core/org.wso2.carbon.identity.core.server.feature/resources/org.wso2.carbon.identity.core.server.feature.default.json index 45aaab91ac19..5a1f5cf49a81 100644 --- a/features/identity-core/org.wso2.carbon.identity.core.server.feature/resources/org.wso2.carbon.identity.core.server.feature.default.json +++ b/features/identity-core/org.wso2.carbon.identity.core.server.feature/resources/org.wso2.carbon.identity.core.server.feature.default.json @@ -1567,7 +1567,7 @@ "on_demand_config.on_initial_use.enable_sms_otp_password_recovery_if_connector_enabled": false, "actions.maximum_actions_per_action_type": 1, - "actions.types.pre_issue_access_token.enable": false, + "actions.types.pre_issue_access_token.enable": true, "oauth.authorize_all_scopes": false }