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

Integrate secret management to action-mgt component #5795

Merged
merged 8 commits into from
Jul 17, 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 @@ -37,6 +37,10 @@
<groupId>org.wso2.carbon.identity.framework</groupId>
<artifactId>org.wso2.carbon.identity.core</artifactId>
</dependency>
<dependency>
<groupId>org.wso2.carbon.identity.framework</groupId>
<artifactId>org.wso2.carbon.identity.secret.mgt.core</artifactId>
</dependency>
<!-- Test dependencies -->
<dependency>
<groupId>org.testng</groupId>
Expand Down Expand Up @@ -67,11 +71,15 @@
<Import-Package>
org.apache.commons.lang; version="${commons-lang.wso2.osgi.version.range}",
org.apache.commons.logging; version="${import.package.version.commons.logging}",
org.apache.commons.collections; version="${commons-collections.wso2.osgi.version.range}",
org.osgi.framework; version="${osgi.framework.imp.pkg.version.range}",
org.osgi.service.component; version="${osgi.service.component.imp.pkg.version.range}",
org.wso2.carbon.database.utils.jdbc;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}",
org.wso2.carbon.identity.secret.mgt.core.exception; version="${carbon.identity.package.import.version.range}",
org.wso2.carbon.identity.secret.mgt.core.model; version="${carbon.identity.package.import.version.range}",
org.wso2.carbon.utils; version="${carbon.kernel.package.import.version.range}",
</Import-Package>
</instructions>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

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.AuthType;

import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -113,4 +114,17 @@ Action updateAction(String actionType, String actionId, Action action, String te
* @throws ActionMgtException If an error occurs while retrieving the Action of a given Action ID.
*/
Action getActionByActionId(String actionId, String tenantDomain) throws ActionMgtException;

/**
* Update the authentication of the action endpoint.
*
* @param actionType Action Type.
* @param actionId Action ID.
* @param authentication Authentication Information to be updated.
* @param tenantDomain Tenant domain.
* @return Action response after update.
* @throws ActionMgtException If an error occurs while updating action endpoint authentication information.
*/
Action updateActionEndpointAuthentication(String actionType, String actionId, AuthType authentication,
String tenantDomain) throws ActionMgtException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

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

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.identity.action.management.constant.ActionMgtConstants;
Expand All @@ -26,6 +27,8 @@
import org.wso2.carbon.identity.action.management.exception.ActionMgtClientException;
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.AuthType;
import org.wso2.carbon.identity.action.management.model.EndpointConfig;
import org.wso2.carbon.identity.action.management.util.ActionManagementUtil;
import org.wso2.carbon.identity.core.util.IdentityTenantUtil;
import org.wso2.carbon.identity.core.util.IdentityUtil;
Expand All @@ -44,6 +47,7 @@ public class ActionManagementServiceImpl implements ActionManagementService {
private static final ActionManagementService INSTANCE = new ActionManagementServiceImpl();
private static final CacheBackedActionMgtDAO CACHE_BACKED_DAO =
new CacheBackedActionMgtDAO(new ActionManagementDAOImpl());
private static final ActionSecretProcessor ACTION_SECRET_PROCESSOR = new ActionSecretProcessor();

private ActionManagementServiceImpl() {
}
Expand All @@ -57,7 +61,7 @@ public static ActionManagementService getInstance() {
public Action addAction(String actionType, Action action, String tenantDomain) throws ActionMgtException {

if (LOG.isDebugEnabled()) {
LOG.debug("Adding Action for Action Type: " + actionType);
LOG.debug(String.format("Adding Action for Action Type: %s.", actionType));
}
String resolvedActionType = getActionTypeFromPath(actionType);
// Check whether the maximum allowed actions per type is reached.
Expand All @@ -71,7 +75,7 @@ public Action addAction(String actionType, Action action, String tenantDomain) t
public List<Action> getActionsByActionType(String actionType, String tenantDomain) throws ActionMgtException {

if (LOG.isDebugEnabled()) {
LOG.debug("Retrieving Actions for Action Type: " + actionType);
LOG.debug(String.format("Retrieving Actions for Action Type: %s.", actionType));
}
return CACHE_BACKED_DAO.getActionsByActionType(getActionTypeFromPath(actionType),
IdentityTenantUtil.getTenantId(tenantDomain));
Expand All @@ -82,9 +86,10 @@ public Action updateAction(String actionType, String actionId, Action action, St
throws ActionMgtException {

if (LOG.isDebugEnabled()) {
LOG.debug("Updating Action for Action Type: " + actionType + " and Action Id: " + actionId);
LOG.debug(String.format("Updating Action for Action Type: %s and Action ID: %s.", actionType, actionId));
}
checkIfActionExists(actionId, tenantDomain);
Action existingAction = checkIfActionExists(actionId, tenantDomain);
action = mergeActionWithExisting(action, existingAction);
return CACHE_BACKED_DAO.updateAction(getActionTypeFromPath(actionType), actionId, action,
IdentityTenantUtil.getTenantId(tenantDomain));
}
Expand All @@ -93,18 +98,18 @@ public Action updateAction(String actionType, String actionId, Action action, St
public void deleteAction(String actionType, String actionId, String tenantDomain) throws ActionMgtException {

if (LOG.isDebugEnabled()) {
LOG.debug("Deleting Action for Action Type: " + actionType + " and Action Id: " + actionId);
LOG.debug(String.format("Deleting Action for Action Type: %s and Action ID: %s", actionType, actionId));
}
checkIfActionExists(actionId, tenantDomain);
CACHE_BACKED_DAO.deleteAction(getActionTypeFromPath(actionType), actionId,
Action action = checkIfActionExists(actionId, tenantDomain);
CACHE_BACKED_DAO.deleteAction(getActionTypeFromPath(actionType), actionId, action,
IdentityTenantUtil.getTenantId(tenantDomain));
}

@Override
public Action activateAction(String actionType, String actionId, String tenantDomain) throws ActionMgtException {

if (LOG.isDebugEnabled()) {
LOG.debug("Activating Action for Action Type: " + actionType + " and Action Id: " + actionId);
LOG.debug(String.format("Activating Action for Action Type: %s and Action ID: %s.", actionType, actionId));
}
checkIfActionExists(actionId, tenantDomain);
return CACHE_BACKED_DAO.activateAction(getActionTypeFromPath(actionType), actionId,
Expand All @@ -115,7 +120,8 @@ public Action activateAction(String actionType, String actionId, String tenantDo
public Action deactivateAction(String actionType, String actionId, String tenantDomain) throws ActionMgtException {

if (LOG.isDebugEnabled()) {
LOG.debug("Deactivating Action for Action Type: " + actionType + " and Action Id: " + actionId);
LOG.debug(String.format("Deactivating Action for Action Type: %s and Action ID: %s.", actionType,
actionId));
}
checkIfActionExists(actionId, tenantDomain);
return CACHE_BACKED_DAO.deactivateAction(getActionTypeFromPath(actionType), actionId,
Expand All @@ -135,11 +141,25 @@ public Map<String, Integer> getActionsCountPerType(String tenantDomain) throws A
public Action getActionByActionId(String actionId, String tenantDomain) throws ActionMgtException {

if (LOG.isDebugEnabled()) {
LOG.debug("Retrieving Action of Action Id: " + actionId);
LOG.debug(String.format("Retrieving Action of Action ID: %s", actionId));
}
return CACHE_BACKED_DAO.getActionByActionId(actionId, IdentityTenantUtil.getTenantId(tenantDomain));
}

@Override
public Action updateActionEndpointAuthentication(String actionType, String actionId, AuthType authentication,
String tenantDomain) throws ActionMgtException {
ashanthamara marked this conversation as resolved.
Show resolved Hide resolved

Action existingAction = checkIfActionExists(actionId, tenantDomain);
if (existingAction.getEndpoint().getAuthentication().getType().equals(authentication.getType())) {
// Only need to update the properties since the authType is same.
return updateEndpointAuthenticationProperties(actionType, actionId, authentication, tenantDomain);
} else {
// Need to update the authentication type and properties.
return updateEndpoint(actionType, actionId, existingAction, authentication, tenantDomain);
}
}
ashanthamara marked this conversation as resolved.
Show resolved Hide resolved

/**
* Get Action Type from path.
*
Expand Down Expand Up @@ -181,11 +201,82 @@ private void validateMaxActionsPerType(String actionType, String tenantDomain) t
* @param tenantDomain Tenant Domain.
* @throws ActionMgtException If the action does not exist.
*/
private void checkIfActionExists(String actionId, String tenantDomain) throws ActionMgtException {
private Action checkIfActionExists(String actionId, String tenantDomain) throws ActionMgtException {

if (CACHE_BACKED_DAO.getActionByActionId(actionId, IdentityTenantUtil.getTenantId(tenantDomain)) == null) {
Action action = CACHE_BACKED_DAO.getActionByActionId(actionId, IdentityTenantUtil.getTenantId(tenantDomain));
if (action == null) {
throw ActionManagementUtil.handleClientException(
ActionMgtConstants.ErrorMessages.ERROR_NO_ACTION_CONFIGURED_ON_GIVEN_ID);
}
return action;
}

/**
* Merge the updating action with the existing action.
*
* @param updatingAction Action object with updating information.
* @param existingAction Action object with existing information.
* @return Action object with merged information.
*/
private Action mergeActionWithExisting(Action updatingAction, Action existingAction) {

return new Action.ActionRequestBuilder()
.name(StringUtils.isEmpty(updatingAction.getName()) ? existingAction.getName() :
updatingAction.getName())
.description(StringUtils.isEmpty(updatingAction.getDescription()) ? existingAction.getDescription() :
updatingAction.getDescription())
.endpoint(new EndpointConfig.EndpointConfigBuilder()
.uri(StringUtils.isEmpty(updatingAction.getEndpoint().getUri()) ?
existingAction.getEndpoint().getUri() : updatingAction.getEndpoint().getUri())
.build())
.build();
}

/**
* Update the authentication type and properties of the action endpoint.
*
* @param actionType Action Type.
* @param actionId Action Id.
* @param existingAction Existing Action Information.
* @param authentication Authentication Information to be updated.
* @param tenantDomain Tenant Domain.
* @return Action response after update.
* @throws ActionMgtException If an error occurs while updating action endpoint authentication.
*/
private Action updateEndpoint(String actionType, String actionId, Action existingAction,
AuthType authentication, String tenantDomain)
throws ActionMgtException {

if (LOG.isDebugEnabled()) {
LOG.debug(String.format("Updating endpoint authentication of Action Type: %s " +
"and Action ID: %s to AuthType: %s", actionType, actionId, authentication.getType().name()));
}
EndpointConfig endpoint = new EndpointConfig.EndpointConfigBuilder()
.uri(existingAction.getEndpoint().getUri())
.authentication(authentication).build();
return CACHE_BACKED_DAO.updateActionEndpoint(getActionTypeFromPath(actionType), actionId, endpoint,
existingAction.getEndpoint().getAuthentication(), IdentityTenantUtil.getTenantId(tenantDomain));
}

/**
* Update the authentication properties of the action endpoint.
*
* @param actionType Action Type.
* @param actionId Action Id.
* @param authentication Authentication Information to be updated.
* @param tenantDomain Tenant domain.
* @return Action response after update.
* @throws ActionMgtException If an error occurs while updating action endpoint authentication properties.
*/
private Action updateEndpointAuthenticationProperties(String actionType, String actionId, AuthType authentication,
String tenantDomain) throws ActionMgtException {

if (LOG.isDebugEnabled()) {
LOG.debug(String.format("Updating endpoint authentication properties of Action Type: %s " +
"Action ID: %s and AuthType: %s", actionType, actionId, authentication.getType().name()));
}
return CACHE_BACKED_DAO.updateActionEndpointAuthProperties(actionId, authentication,
IdentityTenantUtil.getTenantId(tenantDomain));

}
}
Loading
Loading