Block Uninstall Policy Feature

kernel-4.4.x
Muhammad Rashad 3 years ago committed by Pahansith Gunathilake
parent 4cfd56efa0
commit 556886daec

@ -24,8 +24,10 @@ import org.wso2.carbon.device.application.mgt.common.exception.SubscriptionManag
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.common.PaginationRequest;
import org.wso2.carbon.device.mgt.common.PaginationResult;
import org.wso2.carbon.device.mgt.common.app.mgt.App;
import java.util.List;
import java.util.Properties;
/**
* This interface manages all the operations related with ApplicationDTO Subscription.
@ -33,10 +35,9 @@ import java.util.List;
public interface SubscriptionManager {
/**
* Performs bulk subscription operation for a given application and a subscriber list.
*
* @param applicationUUID UUID of the application to subscribe/unsubscribe
* @param params list of subscribers. This list can be of either
* {@link org.wso2.carbon.device.mgt.common.DeviceIdentifier} if {@param subType} is equal
* {@link DeviceIdentifier} if {@param subType} is equal
* to DEVICE or
* {@link String} if {@param subType} is USER, ROLE or GROUP
* @param subType subscription type. E.g. <code>DEVICE, USER, ROLE, GROUP</code> {@see {
@ -46,9 +47,10 @@ public interface SubscriptionManager {
* @throws ApplicationManagementException if error occurs when subscribing to the given application
* @link org.wso2.carbon.device.application.mgt.common.SubscriptionType}}
* @link org.wso2.carbon.device.application.mgt.common.SubAction}}
* @param properties
*/
<T> ApplicationInstallResponse performBulkAppOperation(String applicationUUID, List<T> params, String subType,
String action) throws ApplicationManagementException;
String action, Properties properties) throws ApplicationManagementException;
/**
* Create an entry related to the scheduled task in the database.
@ -121,7 +123,7 @@ public interface SubscriptionManager {
* @throws ApplicationManagementException if error occurred while installing given applications into the given
* device
*/
void installAppsForDevice(DeviceIdentifier deviceIdentifier, List<String> releaseUUID)
void installAppsForDevice(DeviceIdentifier deviceIdentifier, List<App> apps)
throws ApplicationManagementException;
/***

@ -121,7 +121,7 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
@Override
public <T> ApplicationInstallResponse performBulkAppOperation(String applicationUUID, List<T> params,
String subType, String action) throws ApplicationManagementException {
String subType, String action, Properties properties) throws ApplicationManagementException {
if (log.isDebugEnabled()) {
log.debug("Install application release which has UUID " + applicationUUID + " to " + params.size()
+ " users.");
@ -134,7 +134,7 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
params);
ApplicationInstallResponse applicationInstallResponse = performActionOnDevices(
applicationSubscriptionInfo.getAppSupportingDeviceTypeName(), applicationSubscriptionInfo.getDevices(),
applicationDTO, subType, applicationSubscriptionInfo.getSubscribers(), action);
applicationDTO, subType, applicationSubscriptionInfo.getSubscribers(), action, properties);
applicationInstallResponse.setErrorDeviceIdentifiers(applicationSubscriptionInfo.getErrorDeviceIdentifiers());
return applicationInstallResponse;
@ -347,7 +347,7 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
}
@Override
public void installAppsForDevice(DeviceIdentifier deviceIdentifier, List<String> releaseUUIDs)
public void installAppsForDevice(DeviceIdentifier deviceIdentifier, List<App> apps)
throws ApplicationManagementException {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
@ -370,7 +370,8 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
List<DeviceIdentifier> appInstallingDevices = new ArrayList<>();
for (String releaseUUID : releaseUUIDs) {
for (App app : apps) {
String releaseUUID = app.getId();
try {
ConnectionManagerUtil.openDBConnection();
ApplicationDTO applicationDTO = this.applicationDAO.getAppWithRelatedRelease(releaseUUID, tenantId);
@ -409,7 +410,7 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
if (!appInstallingDevices.isEmpty()) {
performBulkAppOperation(releaseUUID, appInstallingDevices, SubscriptionType.DEVICE.toString(),
SubAction.INSTALL.toString());
SubAction.INSTALL.toString(), app.getProperties());
}
}
}
@ -621,7 +622,7 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
* data.
*/
private ApplicationInstallResponse performActionOnDevices(String deviceType, List<Device> devices,
ApplicationDTO applicationDTO, String subType, List<String> subscribers, String action)
ApplicationDTO applicationDTO, String subType, List<String> subscribers, String action, Properties properties)
throws ApplicationManagementException {
//Get app subscribing info of each device
@ -667,11 +668,11 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
}
for (Map.Entry<String, List<DeviceIdentifier>> entry : deviceIdentifierMap.entrySet()) {
Activity activity = addAppOperationOnDevices(applicationDTO, new ArrayList<>(entry.getValue()),
entry.getKey(), action);
entry.getKey(), action, properties);
activityList.add(activity);
}
} else {
Activity activity = addAppOperationOnDevices(applicationDTO, deviceIdentifiers, deviceType, action);
Activity activity = addAppOperationOnDevices(applicationDTO, deviceIdentifiers, deviceType, action, properties);
activityList.add(activity);
}
@ -982,13 +983,13 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
* @throws ApplicationManagementException if found an invalid device.
*/
private Activity addAppOperationOnDevices(ApplicationDTO applicationDTO,
List<DeviceIdentifier> deviceIdentifierList, String deviceType, String action)
List<DeviceIdentifier> deviceIdentifierList, String deviceType, String action, Properties properties)
throws ApplicationManagementException {
DeviceManagementProviderService deviceManagementProviderService = HelperUtil
.getDeviceManagementProviderService();
try {
Application application = APIUtil.appDtoToAppResponse(applicationDTO);
Operation operation = generateOperationPayloadByDeviceType(deviceType, application, action);
Operation operation = generateOperationPayloadByDeviceType(deviceType, application, action, properties);
return deviceManagementProviderService.addOperation(deviceType, operation, deviceIdentifierList);
} catch (OperationManagementException e) {
String msg = "Error occurred while adding the application install operation to devices";
@ -1010,7 +1011,7 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
* @throws ApplicationManagementException if unknown application type is found to generate operation payload or
* invalid action is found to generate operation payload.
*/
private Operation generateOperationPayloadByDeviceType(String deviceType, Application application, String action)
private Operation generateOperationPayloadByDeviceType(String deviceType, Application application, String action, Properties properties)
throws ApplicationManagementException {
try {
if (ApplicationType.CUSTOM.toString().equalsIgnoreCase(application.getType())) {
@ -1046,6 +1047,7 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
app.setLocation(application.getApplicationReleases().get(0).getInstallerPath());
app.setIdentifier(application.getPackageName());
app.setName(application.getName());
app.setProperties(properties);
if (SubAction.INSTALL.toString().equalsIgnoreCase(action)) {
return MDMAndroidOperationUtil.createInstallAppOperation(app);
} else {
@ -1067,7 +1069,6 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
app.setType(mobileAppType);
app.setLocation(plistDownloadEndpoint);
app.setIconImage(application.getApplicationReleases().get(0).getIconPath());
Properties properties = new Properties();
properties.put(MDMAppConstants.IOSConstants.IS_PREVENT_BACKUP, true);
properties.put(MDMAppConstants.IOSConstants.IS_REMOVE_APP, true);
properties.put(MDMAppConstants.IOSConstants.I_TUNES_ID, application.getPackageName());

@ -37,6 +37,7 @@ import org.wso2.carbon.device.mgt.core.task.impl.RandomlyAssignedScheduleTask;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
@ -45,6 +46,7 @@ public class ScheduledAppSubscriptionTask extends RandomlyAssignedScheduleTask {
private static final String TASK_NAME = "SCHEDULE_APP_SUBSCRIPTION";
private SubscriptionManager subscriptionManager;
private String payload;
private String subscribers;
private String subscriptionType;
private String application;
@ -57,6 +59,7 @@ public class ScheduledAppSubscriptionTask extends RandomlyAssignedScheduleTask {
@Override
public void setProperties(Map<String, String> map) {
this.subscribers = map.get(Constants.SUBSCRIBERS);
this.payload = map.get(Constants.PAYLOAD);
this.subscriptionType = map.get(Constants.SUB_TYPE);
this.application = map.get(Constants.APP_UUID);
this.action = map.get(Constants.ACTION);
@ -89,8 +92,9 @@ public class ScheduledAppSubscriptionTask extends RandomlyAssignedScheduleTask {
new TypeToken<List<DeviceIdentifier>>() {
}.getType());
try {
Properties properties = new Gson().fromJson(payload, Properties.class);
subscriptionManager.performBulkAppOperation(this.application, deviceIdentifiers,
this.subscriptionType, this.action);
this.subscriptionType, this.action, properties);
subscriptionDTO.setStatus(ExecutionStatus.EXECUTED);
} catch (ApplicationManagementException e) {
log.error(
@ -102,8 +106,9 @@ public class ScheduledAppSubscriptionTask extends RandomlyAssignedScheduleTask {
List<String> subscriberList = Pattern.compile(",").splitAsStream(this.subscribers).collect(
Collectors.toList());
try {
Properties properties = new Gson().fromJson(payload, Properties.class);
subscriptionManager.performBulkAppOperation(this.application, subscriberList,
this.subscriptionType, this.action);
this.subscriptionType, this.action, properties);
subscriptionDTO.setStatus(ExecutionStatus.EXECUTED);
} catch (ApplicationManagementException e) {
log.error(

@ -46,6 +46,7 @@ import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;
import java.util.stream.Collectors;
public class ScheduledAppSubscriptionTaskManager {
@ -73,7 +74,7 @@ public class ScheduledAppSubscriptionTaskManager {
* @throws ApplicationOperationTaskException if error occurred while scheduling the subscription
*/
public void scheduleAppSubscriptionTask(String applicationUUID, List<?> subscribers,
SubscriptionType subscriptionType, SubAction action, long timestamp)
SubscriptionType subscriptionType, SubAction action, long timestamp, Properties properties)
throws ApplicationOperationTaskException {
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date(timestamp * 1000));
@ -106,7 +107,6 @@ public class ScheduledAppSubscriptionTaskManager {
taskProperties.put(Constants.APP_UUID, applicationUUID);
taskProperties.put(Constants.TENANT_DOMAIN, carbonContext.getTenantDomain(true));
taskProperties.put(Constants.SUBSCRIBER, carbonContext.getUsername());
String subscribersString;
if (SubscriptionType.DEVICE.equals(subscriptionType)) {
subscribersString = new Gson().toJson(subscribers);
@ -115,6 +115,10 @@ public class ScheduledAppSubscriptionTaskManager {
subscribersString = subscribers.stream().map(String.class::cast).collect(Collectors.joining(","));
taskProperties.put(Constants.SUBSCRIBERS, subscribersString);
}
if(properties != null) {
String payload = new Gson().toJson(properties);
taskProperties.put(Constants.PAYLOAD, payload);
}
if (log.isDebugEnabled()) {
log.debug("Scheduling a task to " + action.toString() + " application: " + applicationUUID +
" to/from the following " + subscriptionType.toString() + "S [" + subscribersString + "] at: "

@ -58,6 +58,7 @@ public class Constants {
public static final String SUB_TYPE = "SUBSCRIPTION_TYPE";
public static final String ACTION = "ACTION";
public static final String APP_UUID = "APP_UUID";
public static final String APP_PROPERTIES = "APP_PROPERTIES";
public static final String SUBSCRIBER = "SUBSCRIBER";
public static final String TENANT_DOMAIN = "TENANT_DOMAIN";
public static final String TENANT_ID = "__TENANT_ID_PROP__";

@ -130,7 +130,12 @@ public interface SubscriptionManagementAPI {
name = "timestamp",
value = "Timestamp of scheduled install/uninstall operation"
)
@QueryParam("timestamp") long timestamp
@QueryParam("timestamp") long timestamp,
@ApiParam(
name = "block-uninstall",
value = "App removal status of the install operation"
)
@QueryParam("block-uninstall") Boolean isUninstallBlocked
);
@POST
@ -183,7 +188,12 @@ public interface SubscriptionManagementAPI {
name = "timestamp",
value = "Timestamp of scheduled install/uninstall operation"
)
@QueryParam("timestamp") long timestamp
@QueryParam("timestamp") long timestamp,
@ApiParam(
name = "block-uninstall",
value = "App removal status of the install operation"
)
@QueryParam("block-uninstall") Boolean isUninstallBlocked
);
@POST

@ -34,8 +34,6 @@ import org.wso2.carbon.device.application.mgt.common.BasicUserInfoList;
import org.wso2.carbon.device.application.mgt.common.RoleList;
import org.wso2.carbon.device.application.mgt.common.DeviceGroupList;
import org.wso2.carbon.device.application.mgt.store.api.services.impl.util.RequestValidationUtil;
import org.wso2.carbon.device.mgt.common.PaginationRequest;
import org.wso2.carbon.device.mgt.common.PaginationResult;
import org.wso2.carbon.device.application.mgt.core.exception.BadRequestException;
import org.wso2.carbon.device.application.mgt.core.exception.ForbiddenException;
import org.wso2.carbon.device.application.mgt.core.exception.NotFoundException;
@ -44,6 +42,9 @@ import org.wso2.carbon.device.application.mgt.core.util.APIUtil;
import org.wso2.carbon.device.application.mgt.store.api.services.SubscriptionManagementAPI;
import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.common.MDMAppConstants;
import org.wso2.carbon.device.mgt.common.PaginationRequest;
import org.wso2.carbon.device.mgt.common.PaginationResult;
import javax.validation.Valid;
import javax.ws.rs.Path;
@ -55,9 +56,8 @@ import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.core.Response;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Properties;
/**
* Implementation of Subscription Management related APIs.
@ -75,16 +75,22 @@ public class SubscriptionManagementAPIImpl implements SubscriptionManagementAPI{
@PathParam("uuid") String uuid,
@PathParam("action") String action,
@Valid List<DeviceIdentifier> deviceIdentifiers,
@QueryParam("timestamp") long timestamp) {
@QueryParam("timestamp") long timestamp,
@QueryParam("block-uninstall") Boolean isUninstallBlocked
) {
Properties properties = new Properties();
if(isUninstallBlocked != null) {
properties.put(MDMAppConstants.AndroidConstants.IS_BLOCK_UNINSTALL, isUninstallBlocked);
}
try {
if (0 == timestamp) {
SubscriptionManager subscriptionManager = APIUtil.getSubscriptionManager();
ApplicationInstallResponse response = subscriptionManager
.performBulkAppOperation(uuid, deviceIdentifiers, SubscriptionType.DEVICE.toString(), action);
.performBulkAppOperation(uuid, deviceIdentifiers, SubscriptionType.DEVICE.toString(), action, properties);
return Response.status(Response.Status.OK).entity(response).build();
} else {
return scheduleApplicationOperationTask(uuid, deviceIdentifiers, SubscriptionType.DEVICE,
SubAction.valueOf(action.toUpperCase()), timestamp);
SubAction.valueOf(action.toUpperCase()), timestamp, properties);
}
} catch (NotFoundException e) {
String msg = "Couldn't found an application release for UUI: " + uuid;
@ -116,17 +122,23 @@ public class SubscriptionManagementAPIImpl implements SubscriptionManagementAPI{
@PathParam("subType") String subType,
@PathParam("action") String action,
@Valid List<String> subscribers,
@QueryParam("timestamp") long timestamp) {
@QueryParam("timestamp") long timestamp,
@QueryParam("block-uninstall") Boolean isUninstallBlocked
) {
Properties properties = new Properties();
if(isUninstallBlocked != null) {
properties.put(MDMAppConstants.AndroidConstants.IS_BLOCK_UNINSTALL, isUninstallBlocked);
}
try {
if (0 == timestamp) {
SubscriptionManager subscriptionManager = APIUtil.getSubscriptionManager();
ApplicationInstallResponse response = subscriptionManager
.performBulkAppOperation(uuid, subscribers, subType, action);
.performBulkAppOperation(uuid, subscribers, subType, action, properties);
return Response.status(Response.Status.OK).entity(response).build();
} else {
return scheduleApplicationOperationTask(uuid, subscribers,
SubscriptionType.valueOf(subType.toUpperCase()), SubAction.valueOf(action.toUpperCase()),
timestamp);
timestamp, properties);
}
} catch (NotFoundException e) {
String msg = "Couldn't found an application release for UUID: " + uuid + ". Hence, verify the payload";
@ -170,7 +182,7 @@ public class SubscriptionManagementAPIImpl implements SubscriptionManagementAPI{
return Response.status(Response.Status.OK).entity(msg).build();
} else {
return scheduleApplicationOperationTask(uuid, deviceIdentifiers, SubscriptionType.DEVICE,
SubAction.valueOf(SubAction.INSTALL.toString().toUpperCase()), timestamp);
SubAction.valueOf(SubAction.INSTALL.toString().toUpperCase()), timestamp, null);
}
} catch (NotFoundException e) {
String msg = "Couldn't found an application release for UUI: " + uuid + " to perform ent app installation "
@ -216,7 +228,7 @@ public class SubscriptionManagementAPIImpl implements SubscriptionManagementAPI{
} else {
return scheduleApplicationOperationTask(uuid, subscribers,
SubscriptionType.valueOf(subType.toUpperCase()),
SubAction.valueOf(SubAction.INSTALL.toString().toUpperCase()), timestamp);
SubAction.valueOf(SubAction.INSTALL.toString().toUpperCase()), timestamp, null);
}
} catch (NotFoundException e) {
String msg = "Couldn't found an application release for UUID: " + uuid + ". Hence, verify the payload";
@ -255,11 +267,11 @@ public class SubscriptionManagementAPIImpl implements SubscriptionManagementAPI{
* @return {@link Response} of the operation
*/
private Response scheduleApplicationOperationTask(String applicationUUID, List<?> subscribers,
SubscriptionType subType, SubAction subAction, long timestamp) {
SubscriptionType subType, SubAction subAction, long timestamp, Properties payload) {
try {
ScheduledAppSubscriptionTaskManager subscriptionTaskManager = new ScheduledAppSubscriptionTaskManager();
subscriptionTaskManager.scheduleAppSubscriptionTask(applicationUUID, subscribers, subType, subAction,
timestamp);
timestamp, payload);
} catch (ApplicationOperationTaskException e) {
String msg = "Error occurred while scheduling the application install operation";
log.error(msg, e);

@ -136,6 +136,7 @@ import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.ArrayList;
import java.util.Properties;
@Path("/devices")
public class DeviceManagementServiceImpl implements DeviceManagementService {
@ -915,7 +916,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
if (UUID != null) {
ApplicationInstallResponse response = subscriptionManager
.performBulkAppOperation(UUID, deviceIdentifiers, SubscriptionType.DEVICE.toString(),
"uninstall");
"uninstall", new Properties());
return Response.status(Response.Status.OK).entity(response).build();
//if the applications not installed via entgra store
} else {

@ -45,6 +45,7 @@ public class MDMAppConstants {
private AndroidConstants() {
throw new AssertionError();
}
public static final String IS_BLOCK_UNINSTALL = "isBlockUninstall";
public static final String OPCODE_INSTALL_APPLICATION = "INSTALL_APPLICATION";
public static final String OPCODE_UNINSTALL_APPLICATION = "UNINSTALL_APPLICATION";
public static final String UNMANAGED_APP_UNINSTALL= "UNMANAGED_APP_UNINSTALL";

@ -21,6 +21,7 @@ package org.wso2.carbon.device.mgt.common.app.mgt.android;
import com.google.gson.Gson;
import java.io.Serializable;
import java.util.Properties;
/**
* This class represents the Appstore AuthenticationImpl information.
@ -29,6 +30,7 @@ public class AppStoreApplication implements Serializable {
private String type;
private String appIdentifier;
private Properties properties;
public String getType() {
return type;
@ -51,4 +53,11 @@ public class AppStoreApplication implements Serializable {
return gson.toJson(this);
}
public Properties getProperties() {
return properties;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
}

@ -21,6 +21,7 @@ package org.wso2.carbon.device.mgt.common.app.mgt.android;
import com.google.gson.Gson;
import java.io.Serializable;
import java.util.Properties;
/**
* This class represents the Enterprise AuthenticationImpl information.
@ -30,6 +31,7 @@ public class EnterpriseApplication implements Serializable {
private String type;
private String url;
private String appIdentifier;
private Properties properties;
public String getAppIdentifier() {
return appIdentifier;
@ -60,4 +62,11 @@ public class EnterpriseApplication implements Serializable {
return gson.toJson(this);
}
public Properties getProperties() {
return properties;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
}

@ -21,6 +21,7 @@ package org.wso2.carbon.device.mgt.common.app.mgt.android;
import com.google.gson.Gson;
import java.io.Serializable;
import java.util.Properties;
/**
* This class represents the Web AuthenticationImpl information.
@ -30,6 +31,7 @@ public class WebApplication implements Serializable {
private String name;
private String url;
private String type;
private Properties properties;
public String getName() {
return name;
@ -60,4 +62,11 @@ public class WebApplication implements Serializable {
return gson.toJson(this);
}
public Properties getProperties() {
return properties;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
}

@ -52,6 +52,7 @@ public class MDMAndroidOperationUtil {
enterpriseApplication.setType(application.getType().toString());
enterpriseApplication.setUrl(application.getLocation());
enterpriseApplication.setAppIdentifier(application.getIdentifier());
enterpriseApplication.setProperties(application.getProperties());
operation.setPayLoad(enterpriseApplication.toJSON());
break;
case PUBLIC:
@ -59,6 +60,7 @@ public class MDMAndroidOperationUtil {
new AppStoreApplication();
appStoreApplication.setType(application.getType().toString());
appStoreApplication.setAppIdentifier(application.getIdentifier());
appStoreApplication.setProperties(application.getProperties());
operation.setPayLoad(appStoreApplication.toJSON());
break;
case WEBAPP:
@ -67,6 +69,7 @@ public class MDMAndroidOperationUtil {
webApplication.setUrl(application.getLocation());
webApplication.setName(application.getName());
webApplication.setType(application.getType().toString());
webApplication.setProperties(application.getProperties());
operation.setPayLoad(webApplication.toJSON());
break;
default:
@ -93,6 +96,7 @@ public class MDMAndroidOperationUtil {
new EnterpriseApplication();
enterpriseApplication.setType(application.getType().toString());
enterpriseApplication.setAppIdentifier(application.getIdentifier());
enterpriseApplication.setProperties(application.getProperties());
operation.setPayLoad(enterpriseApplication.toJSON());
break;
case PUBLIC:

Loading…
Cancel
Save