Skip to content

Commit

Permalink
Merge pull request #5942 from mpmadhavig/app-versioning
Browse files Browse the repository at this point in the history
Add App version field as a first class variable of SP
  • Loading branch information
mpmadhavig authored Oct 6, 2024
2 parents d4ac6f9 + beec3a6 commit 899d471
Show file tree
Hide file tree
Showing 23 changed files with 335 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public class ApplicationBasicInfo implements Serializable {

private int applicationId;
private String applicationName;
private String applicationVersion;
private String description;

private String applicationResourceId;
Expand Down Expand Up @@ -80,6 +81,26 @@ public void setApplicationName(String applicationName) {
this.applicationName = applicationName;
}

/**
* Get application version.
*
* @return Application version.
*/
public String getApplicationVersion() {

return applicationVersion;
}

/**
* Set application version.
*
* @param applicationVersion Application version.
*/
public void setApplicationVersion(String applicationVersion) {

this.applicationVersion = applicationVersion;
}

/**
* Get application description.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public class ServiceProvider implements Serializable {
private static final Log log = LogFactory.getLog(ServiceProvider.class);
private static final String CONSENT_CONFIG_ELEM = "ConsentConfig";

private static final String APPLICATION_VERSION = "ApplicationVersion";
private static final String ACCESS_URL = "AccessUrl";
private static final String IMAGE_URL = "ImageUrl";
private static final String TEMPLATE_ID = "TemplateId";
Expand All @@ -66,6 +67,9 @@ public class ServiceProvider implements Serializable {
@XmlElement(name = "ApplicationName")
private String applicationName;

@XmlElement(name = APPLICATION_VERSION)
private String applicationVersion;

@XmlElement(name = "Description")
private String description;

Expand Down Expand Up @@ -201,6 +205,8 @@ public static ServiceProvider build(OMElement serviceProviderOM) {
log.error("Service provider not loaded from the file. Application Name is null.");
return null;
}
} else if (APPLICATION_VERSION.equals(elementName)) {
serviceProvider.setApplicationVersion(element.getText());
} else if ("Description".equals(elementName)) {
serviceProvider.setDescription(element.getText());
} else if (IMAGE_URL.equals(elementName)) {
Expand Down Expand Up @@ -454,6 +460,22 @@ public void setApplicationName(String applicationName) {
this.applicationName = applicationName;
}

/**
* @return Application version.
*/
public String getApplicationVersion() {

return applicationVersion;
}

/**
* @param applicationVersion Application version.
*/
public void setApplicationVersion(String applicationVersion) {

this.applicationVersion = applicationVersion;
}

/**
* @return
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,46 @@ public static class LogConstants {
public static final String DELETE_APPLICATION = "delete-application";
}

/**
* Group the constants related to application versioning.
*/
public static class ApplicationVersion {

public static final String APP_VERSION_V0 = "v0.0.0";
public static final String APP_VERSION_V1 = "v1.0.0";

// Change the latest version when a new version is introduced.
public static final String LATEST_APP_VERSION = APP_VERSION_V1;
public static final String BASE_APP_VERSION = APP_VERSION_V0;

/**
* Application version enum.
*/
public enum ApplicationVersions {

V0(APP_VERSION_V0),
V1(APP_VERSION_V1);

private final String value;

ApplicationVersions(String value) {
this.value = value;
}

public String getValue() {
return value;
}
}
}

/**
* Grouping of constants related to database SP_APP table.
*/
public static class ApplicationTableColumns {

public static final String ID = "ID";
public static final String APP_NAME = "APP_NAME";
public static final String APP_VERSION = "VERSION";
public static final String DESCRIPTION = "DESCRIPTION";

public static final String USERNAME = "USERNAME";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,9 @@ public ServiceProvider createApplicationWithTemplate(ServiceProvider serviceProv
}
}

// Set default application version.
serviceProvider.setApplicationVersion(ApplicationConstants.ApplicationVersion.LATEST_APP_VERSION);

doPreAddApplicationChecks(serviceProvider, tenantDomain, username);
ApplicationDAO appDAO = ApplicationMgtSystemConfig.getInstance().getApplicationDAO();
serviceProvider.setOwner(getUser(tenantDomain, username).orElseThrow(() ->
Expand Down Expand Up @@ -1506,6 +1509,7 @@ private ImportResponse importApplication(ServiceProvider serviceProvider, String
serviceProvider.setOwner(getUser(tenantDomain, username).orElseThrow(() ->
new IdentityApplicationManagementException("Error resolving service provider owner.")));
serviceProvider.setSpProperties(savedSP.getSpProperties());
serviceProvider.setApplicationVersion(savedSP.getApplicationVersion());

for (ApplicationMgtListener listener : listeners) {
if (listener.isEnable()) {
Expand Down Expand Up @@ -1660,6 +1664,10 @@ public void createApplicationTemplate(SpTemplate spTemplate, String tenantDomain

try {
ServiceProvider serviceProvider = unmarshalSPTemplate(spTemplate.getContent());
// Set default application version.
if (StringUtils.isBlank(serviceProvider.getApplicationVersion())) {
serviceProvider.setApplicationVersion(ApplicationConstants.ApplicationVersion.LATEST_APP_VERSION);
}
validateSPTemplateExists(spTemplate, tenantDomain);
validateUnsupportedTemplateConfigs(serviceProvider);
applicationValidatorManager.validateSPConfigurations(serviceProvider, tenantDomain,
Expand Down Expand Up @@ -1762,6 +1770,10 @@ public void updateApplicationTemplate(String oldTemplateName, SpTemplate spTempl
validateSPTemplateExists(oldTemplateName, spTemplate, tenantDomain);

ServiceProvider serviceProvider = unmarshalSPTemplate(spTemplate.getContent());
// Set default application version.
if (StringUtils.isBlank(serviceProvider.getApplicationVersion())) {
serviceProvider.setApplicationVersion(ApplicationConstants.ApplicationVersion.LATEST_APP_VERSION);
}
validateUnsupportedTemplateConfigs(serviceProvider);

applicationValidatorManager.validateSPConfigurations(serviceProvider, tenantDomain,
Expand Down Expand Up @@ -2543,6 +2555,9 @@ public String createApplication(ServiceProvider application, String tenantDomain
}
}

// Set default application version.
application.setApplicationVersion(ApplicationConstants.ApplicationVersion.LATEST_APP_VERSION);

doPreAddApplicationChecks(application, tenantDomain, username);
ApplicationDAO applicationDAO = ApplicationMgtSystemConfig.getInstance().getApplicationDAO();
String resourceId = doAddApplication(application, tenantDomain, username, applicationDAO::addApplication);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import org.wso2.carbon.identity.application.common.model.ServiceProvider;
import org.wso2.carbon.identity.application.common.model.SpFileStream;
import org.wso2.carbon.identity.application.common.model.User;
import org.wso2.carbon.identity.application.common.util.IdentityApplicationConstants;
import org.wso2.carbon.identity.application.mgt.dao.ApplicationDAO;
import org.wso2.carbon.identity.application.mgt.internal.ApplicationManagementServiceComponentHolder;
import org.wso2.carbon.identity.application.mgt.provider.RegistryBasedApplicationPermissionProvider;
Expand Down Expand Up @@ -1225,4 +1226,38 @@ public static boolean isTrustedAppConsentRequired() {

return Boolean.parseBoolean(IdentityUtil.getProperty(TRUSTED_APP_CONSENT_REQUIRED_PROPERTY));
}

/**
* Get the latest applicable version of the application.
*
* @param serviceProvider Service provider object.
* @return The latest applicable version.
*/
public static String getApplicationUpdatedVersion(ServiceProvider serviceProvider) {

String currentVersion = serviceProvider.getApplicationVersion();
String inboundConfigType = getInboundConfigType(serviceProvider);

// Since there will be new versions onboarded in the future, initialized this as a switch not if.
if (currentVersion.equals(ApplicationConstants.ApplicationVersion.APP_VERSION_V0)) {
if (!inboundConfigType.equals(IdentityApplicationConstants.OAuth2.NAME)) {
currentVersion = ApplicationConstants.ApplicationVersion.APP_VERSION_V1;
}
}
return currentVersion;
}

private static String getInboundConfigType(ServiceProvider serviceProvider) {

String inboundConfigType = StringUtils.EMPTY;
if (serviceProvider.getInboundAuthenticationConfig() != null &&
serviceProvider.getInboundAuthenticationConfig()
.getInboundAuthenticationRequestConfigs() != null &&
serviceProvider.getInboundAuthenticationConfig()
.getInboundAuthenticationRequestConfigs().length != 0) {
inboundConfigType = serviceProvider.getInboundAuthenticationConfig()
.getInboundAuthenticationRequestConfigs()[0].getInboundAuthType();
}
return inboundConfigType;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,7 @@ public int createApplication(ServiceProvider application, String tenantDomain)

Connection connection = IdentityDatabaseUtil.getDBConnection(true);
try {
// Create basic application.
ApplicationCreateResult result = persistBasicApplicationInformation(connection, application, tenantDomain);
IdentityDatabaseUtil.commitTransaction(connection);
return result.getApplicationId();
Expand Down Expand Up @@ -404,6 +405,7 @@ private ApplicationCreateResult persistBasicApplicationInformation(Connection co
String username = UserCoreUtil.removeDomainFromName(qualifiedUsername);
String userStoreDomain = IdentityUtil.extractDomainFromName(qualifiedUsername);
String applicationName = application.getApplicationName();
String applicationVersion = application.getApplicationVersion();
String description = application.getDescription();

if (log.isDebugEnabled()) {
Expand Down Expand Up @@ -438,6 +440,7 @@ private ApplicationCreateResult persistBasicApplicationInformation(Connection co
storeAppPrepStmt.setString(10, resourceId);
storeAppPrepStmt.setString(11, application.getImageUrl());
storeAppPrepStmt.setString(12, templatedAccessUrl);
storeAppPrepStmt.setString(13, applicationVersion);
storeAppPrepStmt.execute();

results = storeAppPrepStmt.getGeneratedKeys();
Expand Down Expand Up @@ -981,6 +984,7 @@ private void updateBasicApplicationData(ServiceProvider serviceProvider, Connect
throws SQLException, UserStoreException, IdentityApplicationManagementException {

int applicationId = serviceProvider.getApplicationID();
String applicationVersion = ApplicationMgtUtil.getApplicationUpdatedVersion(serviceProvider);
String applicationName = serviceProvider.getApplicationName();
String description = serviceProvider.getDescription();
boolean isSaasApp = serviceProvider.isSaasApp();
Expand Down Expand Up @@ -1056,6 +1060,7 @@ private void updateBasicApplicationData(ServiceProvider serviceProvider, Connect
}
statement.setInt(ApplicationTableColumns.TENANT_ID, tenantID);
statement.setInt(ApplicationTableColumns.ID, applicationId);
statement.setString(ApplicationTableColumns.APP_VERSION, applicationVersion);

statement.executeUpdate();
}
Expand Down Expand Up @@ -1837,6 +1842,7 @@ public ServiceProvider getApplication(String applicationName,
localServiceProvider.setApplicationName(applicationName);
localServiceProvider.setDescription("Local Service Provider");
localServiceProvider.setSpProperties(prepareLocalSpProperties());
localServiceProvider.setApplicationVersion(ApplicationConstants.ApplicationVersion.LATEST_APP_VERSION);
applicationId = createServiceProvider(tenantDomain, localServiceProvider);
}
return getApplication(applicationId);
Expand Down Expand Up @@ -2608,6 +2614,7 @@ private ServiceProvider getBasicApplicationData(int appId, Connection connection
serviceProvider.setApplicationID(rs.getInt(ApplicationTableColumns.ID));
serviceProvider.setApplicationResourceId(rs.getString(ApplicationTableColumns.UUID));
serviceProvider.setApplicationName(rs.getString(ApplicationTableColumns.APP_NAME));
serviceProvider.setApplicationVersion(rs.getString(ApplicationTableColumns.APP_VERSION));
serviceProvider.setDescription(rs.getString(ApplicationTableColumns.DESCRIPTION));
serviceProvider.setImageUrl(rs.getString(ApplicationTableColumns.IMAGE_URL));

Expand Down Expand Up @@ -3810,6 +3817,7 @@ public ApplicationBasicInfo[] getAllApplicationBasicInfo()
basicInfo.setApplicationId(appNameResultSet.getInt("ID"));
basicInfo.setApplicationName(appNameResultSet.getString("APP_NAME"));
basicInfo.setDescription(appNameResultSet.getString("DESCRIPTION"));
basicInfo.setApplicationVersion(appNameResultSet.getString(ApplicationTableColumns.APP_VERSION));
appInfo.add(basicInfo);
}

Expand Down Expand Up @@ -4075,6 +4083,7 @@ public ApplicationBasicInfo[] getApplicationBasicInfo(String filter)
basicInfo.setApplicationId(appNameResultSet.getInt("ID"));
basicInfo.setApplicationName(appNameResultSet.getString("APP_NAME"));
basicInfo.setDescription(appNameResultSet.getString("DESCRIPTION"));
basicInfo.setApplicationVersion(appNameResultSet.getString(ApplicationTableColumns.APP_VERSION));
appInfo.add(basicInfo);
}
} catch (SQLException e) {
Expand Down Expand Up @@ -4935,6 +4944,7 @@ public boolean isApplicationExists(String serviceProviderName, String tenantName
}

try (Connection connection = IdentityDatabaseUtil.getDBConnection(false)) {
// Todo: change the query to something basic without getting all info.
try (PreparedStatement checkAppExistence = connection
.prepareStatement(ApplicationMgtDBQueries.LOAD_BASIC_APP_INFO_BY_APP_NAME)) {
checkAppExistence.setString(1, serviceProviderName);
Expand Down Expand Up @@ -6250,6 +6260,7 @@ private ApplicationBasicInfo buildApplicationBasicInfo(ResultSet appNameResultSe
basicInfo.setApplicationName(appNameResultSet.getString(ApplicationTableColumns.APP_NAME));
basicInfo.setDescription(appNameResultSet.getString(ApplicationTableColumns.DESCRIPTION));
basicInfo.setUuid(appNameResultSet.getString(ApplicationTableColumns.UUID));
basicInfo.setApplicationVersion(appNameResultSet.getString(ApplicationTableColumns.APP_VERSION));

basicInfo.setApplicationResourceId(appNameResultSet.getString(ApplicationTableColumns.UUID));
basicInfo.setImageUrl(appNameResultSet.getString(ApplicationTableColumns.IMAGE_URL));
Expand Down Expand Up @@ -6308,6 +6319,7 @@ private ApplicationBasicInfo buildApplicationBasicInfoWithInboundConfig(ResultSe
basicInfo.setApplicationId(appNameResultSet.getInt(ApplicationTableColumns.ID));
basicInfo.setApplicationName(appNameResultSet.getString(ApplicationTableColumns.APP_NAME));
basicInfo.setDescription(appNameResultSet.getString(ApplicationTableColumns.DESCRIPTION));
basicInfo.setApplicationVersion(appNameResultSet.getString(ApplicationTableColumns.APP_VERSION));

basicInfo.setApplicationResourceId(appNameResultSet.getString(ApplicationTableColumns.UUID));
basicInfo.setImageUrl(appNameResultSet.getString(ApplicationTableColumns.IMAGE_URL));
Expand Down
Loading

0 comments on commit 899d471

Please sign in to comment.