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

Remove unnecessary dependency to Identity Util to read action execution configs #5864

Merged
merged 4 commits into from
Aug 15, 2024
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 @@ -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;
Expand Down Expand Up @@ -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<String, Object> eventContext, String tenantDomain)
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}

}
Original file line number Diff line number Diff line change
@@ -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> 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));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
<class name="org.wso2.carbon.identity.action.execution.util.AuthMethodsTest"/>
<class name="org.wso2.carbon.identity.action.execution.util.OperationComparatorTest"/>
<class name="org.wso2.carbon.identity.action.execution.util.APIClientTest"/>
<class name="org.wso2.carbon.identity.action.execution.util.ActionExecutorConfigTest"/>
</classes>
</test>
<test name="action-execution-configuration-test">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
Loading