diff --git a/components/org.wso2.carbon.identity.remotefetch.core/pom.xml b/components/org.wso2.carbon.identity.remotefetch.core/pom.xml index e354685..e67a88b 100644 --- a/components/org.wso2.carbon.identity.remotefetch.core/pom.xml +++ b/components/org.wso2.carbon.identity.remotefetch.core/pom.xml @@ -113,6 +113,18 @@ org.wso2.carbon.identity.remotefetch.common ${project.version} + + org.wso2.carbon.identity.framework + org.wso2.carbon.identity.xds.client.mgt + + + org.wso2.carbon.identity.framework + org.wso2.carbon.identity.xds.common + + + com.google.code.gson + gson + bundle @@ -165,7 +177,10 @@ org.wso2.carbon.user.core.*; version="${carbon.kernel.package.import.version.range}", org.wso2.carbon.user.api; version="${carbon.user.api.imp.pkg.version.range}", - org.wso2.carbon.identity.remotefetch.common.* + org.wso2.carbon.identity.remotefetch.common.*, + org.wso2.carbon.identity.xds.client.mgt.*; version="${carbon.identity.package.import.version.range}", + org.wso2.carbon.identity.xds.common.*; version="${carbon.identity.package.import.version.range}", + !org.wso2.carbon.identity.remotefetch.core.internal, diff --git a/components/org.wso2.carbon.identity.remotefetch.core/src/main/java/org/wso2/carbon/identity/remotefetch/core/RemoteFetchConfigurationServiceImpl.java b/components/org.wso2.carbon.identity.remotefetch.core/src/main/java/org/wso2/carbon/identity/remotefetch/core/RemoteFetchConfigurationServiceImpl.java index 22abf65..1bd5515 100644 --- a/components/org.wso2.carbon.identity.remotefetch.core/src/main/java/org/wso2/carbon/identity/remotefetch/core/RemoteFetchConfigurationServiceImpl.java +++ b/components/org.wso2.carbon.identity.remotefetch.core/src/main/java/org/wso2/carbon/identity/remotefetch/core/RemoteFetchConfigurationServiceImpl.java @@ -18,10 +18,14 @@ package org.wso2.carbon.identity.remotefetch.core; +import com.google.gson.Gson; +import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.context.CarbonContext; +import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.identity.core.util.IdentityTenantUtil; +import org.wso2.carbon.identity.core.util.IdentityUtil; import org.wso2.carbon.identity.remotefetch.common.BasicRemoteFetchConfiguration; import org.wso2.carbon.identity.remotefetch.common.DeploymentRevision; import org.wso2.carbon.identity.remotefetch.common.RemoteFetchConfiguration; @@ -35,8 +39,11 @@ import org.wso2.carbon.identity.remotefetch.core.executers.RemoteFetchTaskExecutor; import org.wso2.carbon.identity.remotefetch.core.impl.handlers.action.WebHookHandler; import org.wso2.carbon.identity.remotefetch.core.internal.RemoteFetchServiceComponentHolder; +import org.wso2.carbon.identity.remotefetch.core.model.RemoteFetchXDSWrapper; import org.wso2.carbon.identity.remotefetch.core.util.RemoteFetchConfigurationUtils; import org.wso2.carbon.identity.remotefetch.core.util.RemoteFetchConfigurationValidator; +import org.wso2.carbon.identity.xds.common.constant.XDSConstants; +import org.wso2.carbon.identity.xds.common.constant.XDSOperationType; import java.util.List; import java.util.OptionalInt; @@ -81,13 +88,22 @@ public ValidationReport addRemoteFetchConfiguration(RemoteFetchConfiguration fet ValidationReport validationReport = validator.validate(); if (validationReport.getValidationStatus() == ValidationReport.ValidationStatus.PASSED) { - String remoteConfigurationId = RemoteFetchConfigurationUtils.generateUniqueID(); + String remoteConfigurationId = StringUtils.isNotBlank(fetchConfiguration.getRemoteFetchConfigurationId()) + ? fetchConfiguration.getRemoteFetchConfigurationId() + : RemoteFetchConfigurationUtils.generateUniqueID(); if (log.isDebugEnabled()) { log.debug("Remote Configuration ID is generated: " + remoteConfigurationId); } fetchConfiguration.setRemoteFetchConfigurationId(remoteConfigurationId); this.fetchConfigurationDAO.createRemoteFetchConfiguration(fetchConfiguration); validationReport.setId(remoteConfigurationId); + if (isControlPlane()) { + RemoteFetchXDSWrapper emailTemplateXDSWrapper = new RemoteFetchXDSWrapper.RemoteFetchXDSWrapperBuilder() + .setRemoteFetchConfiguration(fetchConfiguration) + .build(); + publishData(emailTemplateXDSWrapper, XDSConstants.EventType.REMOTE_FETCH, + RemoteFetchXDSOperationType.ADD_REMOTE_FETCH_CONFIGURATION); + } } else { throw RemoteFetchConfigurationUtils.handleClientException(RemoteFetchConstants.ErrorMessage. ERROR_CODE_RF_CONFIG_ADD_REQUEST_INVALID, validationReport.getMessages()); @@ -117,6 +133,13 @@ public ValidationReport updateRemoteFetchConfiguration(RemoteFetchConfiguration if (validationReport.getValidationStatus() == ValidationReport.ValidationStatus.PASSED) { this.fetchConfigurationDAO.updateRemoteFetchConfiguration(fetchConfiguration); validationReport.setId(fetchConfiguration.getRemoteFetchConfigurationId()); + if (isControlPlane()) { + RemoteFetchXDSWrapper emailTemplateXDSWrapper = new RemoteFetchXDSWrapper.RemoteFetchXDSWrapperBuilder() + .setRemoteFetchConfiguration(fetchConfiguration) + .build(); + publishData(emailTemplateXDSWrapper, XDSConstants.EventType.REMOTE_FETCH, + RemoteFetchXDSOperationType.UPDATE_REMOTE_FETCH_CONFIGURATION); + } } else { throw RemoteFetchConfigurationUtils.handleClientException(RemoteFetchConstants.ErrorMessage. ERROR_CODE_RF_CONFIG_UPDATE_REQUEST_INVALID, validationReport.getMessages()); @@ -188,6 +211,13 @@ public void deleteRemoteFetchConfiguration(String fetchConfigurationId) int tenantId = IdentityTenantUtil.getTenantId(CarbonContext.getThreadLocalCarbonContext().getTenantDomain()); this.fetchConfigurationDAO.deleteRemoteFetchConfiguration(fetchConfigurationId, tenantId); + if (isControlPlane()) { + RemoteFetchXDSWrapper emailTemplateXDSWrapper = new RemoteFetchXDSWrapper.RemoteFetchXDSWrapperBuilder() + .setFetchConfigurationId(fetchConfigurationId) + .build(); + publishData(emailTemplateXDSWrapper, XDSConstants.EventType.REMOTE_FETCH, + RemoteFetchXDSOperationType.DELETE_REMOTE_FETCH_CONFIGURATION); + } } /** @@ -288,4 +318,25 @@ private int validateOffset(OptionalInt optionalOffset) throws RemoteFetchCoreExc } return offset; } + + private String buildJson(RemoteFetchXDSWrapper remoteFetchXDSWrapper) { + + Gson gson = new Gson(); + return gson.toJson(remoteFetchXDSWrapper); + } + + private boolean isControlPlane() { + + return Boolean.parseBoolean(IdentityUtil.getProperty("Server.ControlPlane")); + } + + private void publishData(RemoteFetchXDSWrapper remoteFetchXDSWrapper, XDSConstants.EventType eventType, + XDSOperationType xdsOperationType) { + + String json = buildJson(remoteFetchXDSWrapper); + String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain(); + String username = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); + RemoteFetchServiceComponentHolder.getInstance().getXdsClientService() + .publishData(tenantDomain, username, json, eventType, xdsOperationType); + } } diff --git a/components/org.wso2.carbon.identity.remotefetch.core/src/main/java/org/wso2/carbon/identity/remotefetch/core/RemoteFetchXDSOperationType.java b/components/org.wso2.carbon.identity.remotefetch.core/src/main/java/org/wso2/carbon/identity/remotefetch/core/RemoteFetchXDSOperationType.java new file mode 100644 index 0000000..1dea228 --- /dev/null +++ b/components/org.wso2.carbon.identity.remotefetch.core/src/main/java/org/wso2/carbon/identity/remotefetch/core/RemoteFetchXDSOperationType.java @@ -0,0 +1,14 @@ +package org.wso2.carbon.identity.remotefetch.core; + +import org.wso2.carbon.identity.xds.common.constant.XDSOperationType; + +/** + * Enum for RemoteFetch XDS operations. + */ +public enum RemoteFetchXDSOperationType implements XDSOperationType { + + ADD_REMOTE_FETCH_CONFIGURATION, + UPDATE_REMOTE_FETCH_CONFIGURATION, + DELETE_REMOTE_FETCH_CONFIGURATION + +} diff --git a/components/org.wso2.carbon.identity.remotefetch.core/src/main/java/org/wso2/carbon/identity/remotefetch/core/internal/RemoteFetchServiceComponent.java b/components/org.wso2.carbon.identity.remotefetch.core/src/main/java/org/wso2/carbon/identity/remotefetch/core/internal/RemoteFetchServiceComponent.java index fcc3e93..d90f7e8 100644 --- a/components/org.wso2.carbon.identity.remotefetch.core/src/main/java/org/wso2/carbon/identity/remotefetch/core/internal/RemoteFetchServiceComponent.java +++ b/components/org.wso2.carbon.identity.remotefetch.core/src/main/java/org/wso2/carbon/identity/remotefetch/core/internal/RemoteFetchServiceComponent.java @@ -43,6 +43,7 @@ import org.wso2.carbon.identity.remotefetch.core.impl.handlers.action.webhook.WebHookActionListenerComponent; import org.wso2.carbon.identity.remotefetch.core.impl.handlers.repository.GitRepositoryManagerComponent; import org.wso2.carbon.identity.remotefetch.core.util.RemoteFetchConfigurationUtils; +import org.wso2.carbon.identity.xds.client.mgt.XDSClientService; import org.wso2.carbon.user.core.service.RealmService; import javax.sql.DataSource; @@ -174,4 +175,20 @@ private RemoteFetchCoreConfiguration parseRemoteFetchCoreConfiguration() { return new RemoteFetchCoreConfiguration(null, false); } } + + @Reference( + name = "xds.client.service", + service = org.wso2.carbon.identity.xds.client.mgt.XDSClientService.class, + cardinality = ReferenceCardinality.MANDATORY, + policy = ReferencePolicy.DYNAMIC, + unbind = "unsetXDSClientService") + protected void setXDSClientService(XDSClientService xdsClientService) { + + RemoteFetchServiceComponentHolder.getInstance().setXdsClientService(xdsClientService); + } + + protected void unsetXDSClientService(XDSClientService xdsClientService) { + + RemoteFetchServiceComponentHolder.getInstance().setXdsClientService(null); + } } diff --git a/components/org.wso2.carbon.identity.remotefetch.core/src/main/java/org/wso2/carbon/identity/remotefetch/core/internal/RemoteFetchServiceComponentHolder.java b/components/org.wso2.carbon.identity.remotefetch.core/src/main/java/org/wso2/carbon/identity/remotefetch/core/internal/RemoteFetchServiceComponentHolder.java index 9f9d490..a9066c9 100644 --- a/components/org.wso2.carbon.identity.remotefetch.core/src/main/java/org/wso2/carbon/identity/remotefetch/core/internal/RemoteFetchServiceComponentHolder.java +++ b/components/org.wso2.carbon.identity.remotefetch.core/src/main/java/org/wso2/carbon/identity/remotefetch/core/internal/RemoteFetchServiceComponentHolder.java @@ -22,6 +22,7 @@ import org.wso2.carbon.identity.remotefetch.common.RemoteFetchComponentRegistry; import org.wso2.carbon.identity.remotefetch.common.RemoteFetchConfigurationService; import org.wso2.carbon.identity.remotefetch.common.RemoteFetchCoreConfiguration; +import org.wso2.carbon.identity.xds.client.mgt.XDSClientService; import org.wso2.carbon.user.core.service.RealmService; import javax.sql.DataSource; @@ -38,6 +39,7 @@ public class RemoteFetchServiceComponentHolder { private RealmService realmService; private DataSource dataSource; private RemoteFetchCoreConfiguration fetchCoreConfiguration; + private XDSClientService xdsClientService; public static RemoteFetchServiceComponentHolder getInstance() { @@ -104,4 +106,14 @@ public void setFetchCoreConfiguration(RemoteFetchCoreConfiguration fetchCoreConf this.fetchCoreConfiguration = fetchCoreConfiguration; } + public XDSClientService getXdsClientService() { + + return xdsClientService; + } + + public void setXdsClientService(XDSClientService xdsClientService) { + + this.xdsClientService = xdsClientService; + } + } diff --git a/components/org.wso2.carbon.identity.remotefetch.core/src/main/java/org/wso2/carbon/identity/remotefetch/core/model/RemoteFetchXDSWrapper.java b/components/org.wso2.carbon.identity.remotefetch.core/src/main/java/org/wso2/carbon/identity/remotefetch/core/model/RemoteFetchXDSWrapper.java new file mode 100644 index 0000000..454be5e --- /dev/null +++ b/components/org.wso2.carbon.identity.remotefetch.core/src/main/java/org/wso2/carbon/identity/remotefetch/core/model/RemoteFetchXDSWrapper.java @@ -0,0 +1,56 @@ +package org.wso2.carbon.identity.remotefetch.core.model; + +import org.wso2.carbon.identity.remotefetch.common.RemoteFetchConfiguration; +import org.wso2.carbon.identity.xds.common.constant.XDSWrapper; + +/** + * Wrapper class for RemoteFetchConfiguration. + */ +public class RemoteFetchXDSWrapper implements XDSWrapper { + + private RemoteFetchConfiguration remoteFetchConfiguration; + private String fetchConfigurationId; + private String timestamp; + + public RemoteFetchXDSWrapper(RemoteFetchXDSWrapperBuilder builder) { + this.remoteFetchConfiguration = builder.remoteFetchConfiguration; + this.fetchConfigurationId = builder.fetchConfigurationId; + this.timestamp = builder.timestamp; + } + + public RemoteFetchConfiguration getRemoteFetchConfiguration() { + return remoteFetchConfiguration; + } + + public String getFetchConfigurationId() { + return fetchConfigurationId; + } + + /** + * Builder class for RemoteFetchXDSWrapper. + */ + public static class RemoteFetchXDSWrapperBuilder { + + private RemoteFetchConfiguration remoteFetchConfiguration; + private String fetchConfigurationId; + private String timestamp; + + public RemoteFetchXDSWrapperBuilder setRemoteFetchConfiguration( + RemoteFetchConfiguration remoteFetchConfiguration) { + this.remoteFetchConfiguration = remoteFetchConfiguration; + return this; + } + + public RemoteFetchXDSWrapperBuilder setFetchConfigurationId(String fetchConfigurationId) { + this.fetchConfigurationId = fetchConfigurationId; + return this; + } + + public RemoteFetchXDSWrapper build() { + + this.timestamp = String.valueOf(System.currentTimeMillis()); + return new RemoteFetchXDSWrapper(this); + } + } + +} diff --git a/pom.xml b/pom.xml index 9cd00be..45ad9d1 100644 --- a/pom.xml +++ b/pom.xml @@ -198,6 +198,21 @@ ${h2database.version} test + + org.wso2.carbon.identity.framework + org.wso2.carbon.identity.xds.client.mgt + ${identity.framework.version} + + + org.wso2.carbon.identity.framework + org.wso2.carbon.identity.xds.common + ${identity.framework.version} + + + com.google.code.gson + gson + ${com.google.code.gson.version} + @@ -350,8 +365,10 @@ [1.0.1, 2.0.0) - 5.14.73 + 5.25.163 [5.0.0, 7.0.0) + 2.9.0 +