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

[Draft] XDS Changes for Remote Fetch for CP/DP Seperation #66

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
17 changes: 16 additions & 1 deletion components/org.wso2.carbon.identity.remotefetch.core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,18 @@
<artifactId>org.wso2.carbon.identity.remotefetch.common</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.wso2.carbon.identity.framework</groupId>
<artifactId>org.wso2.carbon.identity.xds.client.mgt</artifactId>
</dependency>
<dependency>
<groupId>org.wso2.carbon.identity.framework</groupId>
<artifactId>org.wso2.carbon.identity.xds.common</artifactId>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</dependency>
</dependencies>

<packaging>bundle</packaging>
Expand Down Expand Up @@ -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}",

</Import-Package>
<Export-Package>
!org.wso2.carbon.identity.remotefetch.core.internal,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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);
}
}

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -38,6 +39,7 @@ public class RemoteFetchServiceComponentHolder {
private RealmService realmService;
private DataSource dataSource;
private RemoteFetchCoreConfiguration fetchCoreConfiguration;
private XDSClientService xdsClientService;

public static RemoteFetchServiceComponentHolder getInstance() {

Expand Down Expand Up @@ -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;
}

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

}
19 changes: 18 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,21 @@
<version>${h2database.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.wso2.carbon.identity.framework</groupId>
<artifactId>org.wso2.carbon.identity.xds.client.mgt</artifactId>
<version>${identity.framework.version}</version>
</dependency>
<dependency>
<groupId>org.wso2.carbon.identity.framework</groupId>
<artifactId>org.wso2.carbon.identity.xds.common</artifactId>
<version>${identity.framework.version}</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>${com.google.code.gson.version}</version>
</dependency>
</dependencies>
</dependencyManagement>

Expand Down Expand Up @@ -350,8 +365,10 @@
<carbon.user.api.imp.pkg.version.range>[1.0.1, 2.0.0)</carbon.user.api.imp.pkg.version.range>

<!--Carbon identity version-->
<identity.framework.version>5.14.73</identity.framework.version>
<identity.framework.version>5.25.163</identity.framework.version>
<carbon.identity.package.import.version.range>[5.0.0, 7.0.0)</carbon.identity.package.import.version.range>
<com.google.code.gson.version>2.9.0</com.google.code.gson.version>

</properties>

</project>