Skip to content

Commit

Permalink
Merge branch 'wso2:master' into add-cutsom-fed-auth-mgt-support
Browse files Browse the repository at this point in the history
  • Loading branch information
Thisara-Welmilla authored Nov 8, 2024
2 parents 5a06202 + 4a30de8 commit dd8eed1
Show file tree
Hide file tree
Showing 256 changed files with 1,756 additions and 2,169 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<parent>
<groupId>org.wso2.carbon.identity.framework</groupId>
<artifactId>action-mgt</artifactId>
<version>7.5.115-SNAPSHOT</version>
<version>7.5.121-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<parent>
<groupId>org.wso2.carbon.identity.framework</groupId>
<artifactId>action-mgt</artifactId>
<version>7.5.115-SNAPSHOT</version>
<version>7.5.121-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down Expand Up @@ -105,7 +105,8 @@
org.osgi.framework; version="${osgi.framework.imp.pkg.version.range}",
org.osgi.service.component; version="${osgi.service.component.imp.pkg.version.range}",
org.json.*; version="${json.wso2.version.range}",
org.wso2.carbon.database.utils.jdbc;version="${org.wso2.carbon.database.utils.version.range}",
org.wso2.carbon.database.utils.jdbc; version="${org.wso2.carbon.database.utils.version.range}",
org.wso2.carbon.database.utils.jdbc.exceptions; version="${org.wso2.carbon.database.utils.version.range}",
org.wso2.carbon.identity.core.cache; version="${carbon.identity.package.import.version.range}",
org.wso2.carbon.identity.core.util; version="${carbon.identity.package.import.version.range}",
org.wso2.carbon.identity.secret.mgt.core; version="${carbon.identity.package.import.version.range}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ public enum ErrorMessages {
"Error while decrypting Action Endpoint Authentication properties in the system."),
ERROR_NO_AUTHENTICATION_TYPE("65013",
"Error while retrieving Action Endpoint Authentication configurations",
"Authentication type is not defined for the Action Endpoint.");
"Authentication type is not defined for the Action Endpoint."),
ERROR_WHILE_UPDATING_ACTION_BASIC_INFO("65014", "Error while updating basic Action information",
"Error while updating basic Action information in the system.");

private final String code;
private final String message;
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* 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.management.exception;

/**
* Runtime exception class for Action Management.
*/
public class ActionMgtRuntimeException extends RuntimeException {

public ActionMgtRuntimeException(String message, Throwable e) {

super(message, e);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package org.wso2.carbon.identity.action.management.model;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.wso2.carbon.identity.action.management.ActionSecretProcessor;
import org.wso2.carbon.identity.action.management.constant.ActionMgtConstants;
import org.wso2.carbon.identity.action.management.exception.ActionMgtException;
Expand All @@ -27,6 +28,8 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;

/**
* Authentication class which hold supported authentication types and their properties.
Expand Down Expand Up @@ -233,4 +236,62 @@ public Authentication build() {
return new Authentication(this);
}
}

/**
* This builder build endpoint by taking the authentication type and properties as input.
*/
public static class AuthenticationBuilder {

private Type authType;
private Map<String, String> authPropertiesMap;

public AuthenticationBuilder type(Type type) {

this.authType = type;
return this;
}

public AuthenticationBuilder properties(Map<String, String> authPropertiesMap) {

this.authPropertiesMap = authPropertiesMap;
return this;
}

public Authentication build() {

switch (authType) {
case BASIC:
return new Authentication.BasicAuthBuilder(
getProperty(Type.BASIC, authPropertiesMap, Property.USERNAME.getName()),
getProperty(Type.BASIC, authPropertiesMap, Property.PASSWORD.getName())).build();
case BEARER:
return new Authentication.BearerAuthBuilder(
getProperty(Type.BEARER, authPropertiesMap, Property.ACCESS_TOKEN.getName())).build();
case API_KEY:
return new Authentication.APIKeyAuthBuilder(
getProperty(Type.API_KEY, authPropertiesMap, Property.HEADER.getName()),
getProperty(Type.API_KEY, authPropertiesMap, Property.VALUE.getName())).build();
case NONE:
return new Authentication.NoneAuthBuilder().build();
default:
throw new IllegalArgumentException(String.format("An invalid authentication type '%s' is " +
"provided for the authentication configuration of the endpoint.", authType.name()));
}
}

private String getProperty(Authentication.Type authType, Map<String, String> actionEndpointProperties,
String propertyName) {

if (actionEndpointProperties != null && actionEndpointProperties.containsKey(propertyName)) {
String propValue = actionEndpointProperties.get(propertyName);
if (StringUtils.isNotBlank(propValue)) {
return propValue;
}
throw new IllegalArgumentException(String.format("The Property %s cannot be blank.", propertyName));
}

throw new NoSuchElementException(String.format("The property %s must be provided as an authentication " +
"property for the %s authentication type.", propertyName, authType.name()));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.apache.commons.lang.ArrayUtils;
import org.wso2.carbon.identity.action.management.constant.ActionMgtConstants;
import org.wso2.carbon.identity.action.management.exception.ActionMgtClientException;
import org.wso2.carbon.identity.action.management.exception.ActionMgtRuntimeException;
import org.wso2.carbon.identity.action.management.exception.ActionMgtServerException;

/**
Expand Down Expand Up @@ -64,4 +65,15 @@ public static ActionMgtServerException handleServerException(

return new ActionMgtServerException(error.getMessage(), description, error.getCode(), e);
}

/**
* Handle Action Management runtime exceptions.
*
* @param e Throwable.
* @return ActionMgtRuntimeException.
*/
public static ActionMgtRuntimeException handleRuntimeException(String errorMessage, Throwable e) {

return new ActionMgtRuntimeException(errorMessage, e);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,8 @@

package org.wso2.carbon.identity.action.management;

import org.apache.commons.dbcp.BasicDataSource;
import org.mockito.MockedStatic;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
Expand All @@ -33,40 +30,30 @@
import org.wso2.carbon.identity.action.management.model.AuthProperty;
import org.wso2.carbon.identity.action.management.model.Authentication;
import org.wso2.carbon.identity.action.management.model.EndpointConfig;
import org.wso2.carbon.identity.common.testng.WithAxisConfiguration;
import org.wso2.carbon.identity.common.testng.WithCarbonHome;
import org.wso2.carbon.identity.common.testng.WithH2Database;
import org.wso2.carbon.identity.common.testng.WithRealmService;
import org.wso2.carbon.identity.common.testng.WithRegistry;
import org.wso2.carbon.identity.core.internal.IdentityCoreServiceDataHolder;
import org.wso2.carbon.identity.core.util.IdentityDatabaseUtil;
import org.wso2.carbon.identity.secret.mgt.core.SecretManagerImpl;
import org.wso2.carbon.identity.secret.mgt.core.exception.SecretManagementException;
import org.wso2.carbon.identity.secret.mgt.core.model.SecretType;

import java.nio.file.Paths;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.when;

/**
* This class is a test suite for the ActionManagementServiceImpl class.
* It contains unit tests to verify the functionality of the methods
* in the ActionManagementServiceImpl class.
*/
@WithAxisConfiguration
@WithCarbonHome
@WithH2Database(files = {"dbscripts/h2.sql"})
@WithRegistry
@WithRealmService(injectToSingletons = {IdentityCoreServiceDataHolder.class})
public class ActionManagementServiceImplTest {

Expand All @@ -75,41 +62,24 @@ public class ActionManagementServiceImplTest {
private String tenantDomain;
private ActionManagementService serviceImpl;
private Map<String, String> secretProperties;
private static final String DB_NAME = "action_mgt";
private static final String ACCESS_TOKEN = "6e47f1f7-bd29-41e9-b5dc-e9dd70ac22b7";
private static final Map<String, BasicDataSource> dataSourceMap = new HashMap<>();
private static final String PRE_ISSUE_ACCESS_TOKEN = Action.ActionTypes.PRE_ISSUE_ACCESS_TOKEN.getPathParam();

@BeforeClass
public void setUpClass() throws Exception {
public void setUpClass() {

serviceImpl = ActionManagementServiceImpl.getInstance();
tenantDomain = CarbonContext.getThreadLocalCarbonContext().getTenantDomain();
initiateH2Database(getFilePath());
}

@BeforeMethod
public void setUp() throws SecretManagementException {

identityDatabaseUtil = mockStatic(IdentityDatabaseUtil.class);
SecretManagerImpl secretManager = mock(SecretManagerImpl.class);
SecretType secretType = mock(SecretType.class);
ActionMgtServiceComponentHolder.getInstance().setSecretManager(secretManager);
when(secretType.getId()).thenReturn("secretId");
when(secretManager.getSecretType(any())).thenReturn(secretType);
mockDBConnection();
}

@AfterMethod
public void tearDown() {

identityDatabaseUtil.close();
}

@AfterClass
public void wrapUp() throws Exception {

closeH2Database();
}

@Test(priority = 1)
Expand Down Expand Up @@ -396,47 +366,4 @@ private Action buildMockAction(String name,
.endpoint(buildMockEndpointConfig(uri, authentication))
.build();
}

private void mockDBConnection() {

identityDatabaseUtil.when(() -> IdentityDatabaseUtil.getDBConnection(anyBoolean()))
.thenAnswer(invocation -> getConnection());
}

private Connection getConnection() throws Exception {

if (dataSourceMap.get(DB_NAME) != null) {
return dataSourceMap.get(DB_NAME).getConnection();
}
throw new RuntimeException("Invalid datasource.");
}

private void initiateH2Database(String scriptPath) throws Exception {

BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName("org.h2.Driver");
dataSource.setUsername("username");
dataSource.setPassword("password");
dataSource.setUrl("jdbc:h2:mem:test" + DB_NAME);
dataSource.setTestOnBorrow(true);
dataSource.setValidationQuery("select 1");
try (Connection connection = dataSource.getConnection()) {
connection.createStatement().executeUpdate("RUNSCRIPT FROM '" + scriptPath + "'");
}
dataSourceMap.put(DB_NAME, dataSource);
}

private static String getFilePath() {

return Paths.get(System.getProperty("user.dir"), "src", "test", "resources", "dbscripts", "h2.sql")
.toString();
}

private static void closeH2Database() throws SQLException {

BasicDataSource dataSource = dataSourceMap.get(DB_NAME);
if (dataSource != null) {
dataSource.close();
}
}
}
Loading

0 comments on commit dd8eed1

Please sign in to comment.