Refactor code

appm_improvement
Rajitha Kumara 11 months ago
parent 2ad8f54a46
commit 957cb748d7

@ -264,8 +264,8 @@ public class ExchangeOnlineCEAEnforcementServiceImpl implements CEAEnforcementSe
* time to avoid unnecessary device bulks. * time to avoid unnecessary device bulks.
* */ * */
List<ActiveSyncDevice> validActiveSyncDevices = isSynced ? DeviceMgtUtil. List<ActiveSyncDevice> validActiveSyncDevices = isSynced ? DeviceMgtUtil.
getEnrolledActiveSyncDevicesSince(lastSynced) : getEnrolledActiveSyncDevices(lastSynced, false) :
DeviceMgtUtil.getEnrolledActiveSyncDevicesPriorTo(new Date()); DeviceMgtUtil.getEnrolledActiveSyncDevices(new Date(), true);
List<ActiveSyncDevice> notValidActiveSyncDevices = new ArrayList<>(); List<ActiveSyncDevice> notValidActiveSyncDevices = new ArrayList<>();
List<ActiveSyncDevice> connectedActiveSyncDevices = isSynced ? List<ActiveSyncDevice> connectedActiveSyncDevices = isSynced ?
@ -286,33 +286,15 @@ public class ExchangeOnlineCEAEnforcementServiceImpl implements CEAEnforcementSe
calendar.add(Calendar.DAY_OF_MONTH, -Constants.MAX_GRACE_PERIOD_IN_DAYS); calendar.add(Calendar.DAY_OF_MONTH, -Constants.MAX_GRACE_PERIOD_IN_DAYS);
List<ActiveSyncDevice> graceExceededNewlyConnectedActiveSyncDevices = List<ActiveSyncDevice> graceExceededNewlyConnectedActiveSyncDevices =
getConnectedActiveSyncDevicesAfter(calendar.getTime(), activeSyncServer); getConnectedActiveSyncDevicesAfter(calendar.getTime(), activeSyncServer);
List<ActiveSyncDevice> managedDevices = DeviceMgtUtil.getEnrolledActiveSyncDevicesSince(calendar.getTime()); List<ActiveSyncDevice> managedDevices = DeviceMgtUtil.getEnrolledActiveSyncDevices(calendar.getTime(), false);
for (ActiveSyncDevice activeSyncDevice : graceExceededNewlyConnectedActiveSyncDevices) { categorizeDevices(validActiveSyncDevices, notValidActiveSyncDevices,
if (!EASMgtUtil.isManageByUEM(activeSyncDevice.getDeviceId()) graceExceededNewlyConnectedActiveSyncDevices, managedDevices, gracePeriod, false);
&& !managedDevices.contains(activeSyncDevice)) {
validActiveSyncDevices.remove(activeSyncDevice);
notValidActiveSyncDevices.add(activeSyncDevice);
} else {
// These devices are managed by UEM, so add to the valid category
notValidActiveSyncDevices.remove(activeSyncDevice);
validActiveSyncDevices.add(activeSyncDevice);
}
}
// Block grace offered existing devices if exists // Block grace offered existing devices if exists
List<ActiveSyncDevice> connectedActiveSyncDevicesBeforeTheCreationOfCEAPolicy = List<ActiveSyncDevice> connectedActiveSyncDevicesBeforeTheCreationOfCEAPolicy =
getConnectedActiveSyncDevicesBefore(created, activeSyncServer); getConnectedActiveSyncDevicesBefore(created, activeSyncServer);
for (ActiveSyncDevice activeSyncDevice: connectedActiveSyncDevicesBeforeTheCreationOfCEAPolicy) { categorizeDevices(validActiveSyncDevices, notValidActiveSyncDevices,
if (!EASMgtUtil.isManageByUEM(activeSyncDevice.getDeviceId()) connectedActiveSyncDevicesBeforeTheCreationOfCEAPolicy, validActiveSyncDevices, gracePeriod, false);
&& !validActiveSyncDevices.contains(activeSyncDevice)) {
validActiveSyncDevices.remove(activeSyncDevice);
notValidActiveSyncDevices.add(activeSyncDevice);
} else {
// These devices are managed by UEM, so add to the valid category
notValidActiveSyncDevices.remove(activeSyncDevice);
validActiveSyncDevices.add(activeSyncDevice);
}
}
} }
if (gracePeriod.getGraceAllowedPolicy().equalsName(GraceAllowedPolicy.NEW_AND_EXISTING.name()) || if (gracePeriod.getGraceAllowedPolicy().equalsName(GraceAllowedPolicy.NEW_AND_EXISTING.name()) ||
@ -320,77 +302,27 @@ public class ExchangeOnlineCEAEnforcementServiceImpl implements CEAEnforcementSe
List<ActiveSyncDevice> newlyConnectedActiveSyncDevices = List<ActiveSyncDevice> newlyConnectedActiveSyncDevices =
getConnectedActiveSyncDevicesAfter(isSynced ? lastSynced : created, activeSyncServer); getConnectedActiveSyncDevicesAfter(isSynced ? lastSynced : created, activeSyncServer);
for (ActiveSyncDevice activeSyncDevice : newlyConnectedActiveSyncDevices) { categorizeDevices(validActiveSyncDevices, notValidActiveSyncDevices,
if (!EASMgtUtil.isManageByUEM(activeSyncDevice.getDeviceId()) newlyConnectedActiveSyncDevices, validActiveSyncDevices, gracePeriod, true);
&& !validActiveSyncDevices.contains(activeSyncDevice)) {
long timeDiff = Math.abs(new Date().getTime() - activeSyncDevice.getFirstSyncTime().getTime());
// Enforce the grace period if the device not exceeds the grace limit
if (TimeUnit.DAYS.convert(timeDiff, TimeUnit.MILLISECONDS) < gracePeriod.getGracePeriod()) {
notValidActiveSyncDevices.remove(activeSyncDevice);
validActiveSyncDevices.add(activeSyncDevice);
} else {
validActiveSyncDevices.remove(activeSyncDevice);
notValidActiveSyncDevices.add(activeSyncDevice);
}
} else {
// These devices are managed by UEM, so add to the valid category
notValidActiveSyncDevices.remove(activeSyncDevice);
validActiveSyncDevices.add(activeSyncDevice);
}
}
Calendar calendar = Calendar.getInstance(); Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DAY_OF_MONTH, -Constants.MAX_GRACE_PERIOD_IN_DAYS); calendar.add(Calendar.DAY_OF_MONTH, -Constants.MAX_GRACE_PERIOD_IN_DAYS);
List<ActiveSyncDevice> graceExceededNewlyConnectedActiveSyncDevices = List<ActiveSyncDevice> graceExceededNewlyConnectedActiveSyncDevices =
getConnectedActiveSyncDevicesAfter(calendar.getTime(), activeSyncServer); getConnectedActiveSyncDevicesAfter(calendar.getTime(), activeSyncServer);
List<ActiveSyncDevice> managedDevices = DeviceMgtUtil.getEnrolledActiveSyncDevicesSince(calendar.getTime()); List<ActiveSyncDevice> managedDevices = DeviceMgtUtil.getEnrolledActiveSyncDevices(calendar.getTime(), false);
for (ActiveSyncDevice activeSyncDevice : graceExceededNewlyConnectedActiveSyncDevices) { categorizeDevices(validActiveSyncDevices, notValidActiveSyncDevices,
if (!EASMgtUtil.isManageByUEM(activeSyncDevice.getDeviceId()) graceExceededNewlyConnectedActiveSyncDevices, managedDevices, gracePeriod, true);
&& !managedDevices.contains(activeSyncDevice)) {
long timeDiff = Math.abs(new Date().getTime() - activeSyncDevice.getFirstSyncTime().getTime());
// Enforce the grace period if the device isn't exceeds the current grace limit
if (TimeUnit.DAYS.convert(timeDiff, TimeUnit.MILLISECONDS) < gracePeriod.getGracePeriod()) {
notValidActiveSyncDevices.remove(activeSyncDevice);
validActiveSyncDevices.add(activeSyncDevice);
} else {
validActiveSyncDevices.remove(activeSyncDevice);
notValidActiveSyncDevices.add(activeSyncDevice);
}
} else {
notValidActiveSyncDevices.remove(activeSyncDevice);
validActiveSyncDevices.add(activeSyncDevice);
}
}
} }
if (gracePeriod.getGraceAllowedPolicy().equalsName(GraceAllowedPolicy.NEW_AND_EXISTING.name()) || if (gracePeriod.getGraceAllowedPolicy().equalsName(GraceAllowedPolicy.NEW_AND_EXISTING.name()) ||
gracePeriod.getGraceAllowedPolicy().equalsName(GraceAllowedPolicy.EXISTING_ONLY.name())) { gracePeriod.getGraceAllowedPolicy().equalsName(GraceAllowedPolicy.EXISTING_ONLY.name())) {
long timeDiffBetweenCEAPolicyCreatedAndNow = Math.abs(new Date().getTime() - ceaPolicy.getCreated().getTime());
List<ActiveSyncDevice> connectedActiveSyncDevicesBeforeTheCreationOfCEAPolicy = List<ActiveSyncDevice> connectedActiveSyncDevicesBeforeTheCreationOfCEAPolicy =
getConnectedActiveSyncDevicesBefore(created, activeSyncServer); getConnectedActiveSyncDevicesBefore(created, activeSyncServer);
for (ActiveSyncDevice activeSyncDevice: connectedActiveSyncDevicesBeforeTheCreationOfCEAPolicy) { categorizeDevices(validActiveSyncDevices, notValidActiveSyncDevices,
if (!EASMgtUtil.isManageByUEM(activeSyncDevice.getDeviceId()) connectedActiveSyncDevicesBeforeTheCreationOfCEAPolicy, validActiveSyncDevices, gracePeriod, true);
&& !validActiveSyncDevices.contains(activeSyncDevice)) {
// Enforce the grace period if the device not exceeds the grace limit
if(TimeUnit.DAYS.convert(timeDiffBetweenCEAPolicyCreatedAndNow, TimeUnit.MILLISECONDS)
< gracePeriod.getGracePeriod()) {
notValidActiveSyncDevices.remove(activeSyncDevice);
validActiveSyncDevices.add(activeSyncDevice);
} else {
validActiveSyncDevices.remove(activeSyncDevice);
notValidActiveSyncDevices.add(activeSyncDevice);
}
} else {
// These devices are managed by UEM, so add to the valid category
notValidActiveSyncDevices.remove(activeSyncDevice);
validActiveSyncDevices.add(activeSyncDevice);
}
}
} }
List<MailboxProfile> mailboxProfiles = generateMailboxPolicies(validActiveSyncDevices, List<MailboxProfile> mailboxProfiles = generateMailboxProfiles(validActiveSyncDevices,
notValidActiveSyncDevices); notValidActiveSyncDevices);
for (MailboxProfile mailboxProfile : mailboxProfiles) { for (MailboxProfile mailboxProfile : mailboxProfiles) {
PowershellCommand powershellCommand = getCommand(Parser.COMMAND_SetCASMailbox.COMMAND, activeSyncServer); PowershellCommand powershellCommand = getCommand(Parser.COMMAND_SetCASMailbox.COMMAND, activeSyncServer);
@ -416,6 +348,61 @@ public class ExchangeOnlineCEAEnforcementServiceImpl implements CEAEnforcementSe
} }
} }
/**
* Categorize active sync devices into valid and not valid
* @param validActiveSyncDevices Valid active sync devices
* @param notValidActiveSyncDevices Not valid active sync devices
* @param deviceList Device list to filter
* @param managedList Already managing devices from UEM
* @param gracePeriod Grace period to consider
* @param allowGrace Whether to allow grace or not
*/
private void categorizeDevices(List<ActiveSyncDevice> validActiveSyncDevices, List<ActiveSyncDevice> notValidActiveSyncDevices,
List<ActiveSyncDevice> deviceList, List<ActiveSyncDevice> managedList, GracePeriod gracePeriod, boolean allowGrace) {
for (ActiveSyncDevice activeSyncDevice : deviceList) {
if (!EASMgtUtil.isManageByUEM(activeSyncDevice.getDeviceId())
&& !managedList.contains(activeSyncDevice)) {
if (allowGrace) {
filterDeviceBasedOnGrace(activeSyncDevice, validActiveSyncDevices, notValidActiveSyncDevices, gracePeriod);
} else {
validActiveSyncDevices.remove(activeSyncDevice);
notValidActiveSyncDevices.add(activeSyncDevice);
}
} else {
// These devices are managed by UEM, so add to the valid category
notValidActiveSyncDevices.remove(activeSyncDevice);
validActiveSyncDevices.add(activeSyncDevice);
}
}
}
/**
* Filter active sync device based on grace period
* @param activeSyncDevice Active sync device
* @param validActiveSyncDevices Valid active sync device list
* @param notValidActiveSyncDevices Not valid active sync device list
* @param gracePeriod Grace period to consider
*/
private void filterDeviceBasedOnGrace(ActiveSyncDevice activeSyncDevice, List<ActiveSyncDevice> validActiveSyncDevices,
List<ActiveSyncDevice> notValidActiveSyncDevices, GracePeriod gracePeriod) {
long timeDiff = Math.abs(new Date().getTime() - activeSyncDevice.getFirstSyncTime().getTime());
// Enforce the grace period if the device not exceeds the grace limit
if (TimeUnit.DAYS.convert(timeDiff, TimeUnit.MILLISECONDS) < gracePeriod.getGracePeriod()) {
notValidActiveSyncDevices.remove(activeSyncDevice);
validActiveSyncDevices.add(activeSyncDevice);
} else {
validActiveSyncDevices.remove(activeSyncDevice);
notValidActiveSyncDevices.add(activeSyncDevice);
}
}
/**
* Generate powershell command {@link PowershellCommand} from command string
* @param command Powershell command string
* @param activeSyncServer {@link ActiveSyncServer}
* @return {@link PowershellCommand}
* @throws GatewayServiceException Throws when error occurred while retrieving access token
*/
private PowershellCommand getCommand(String command, ActiveSyncServer activeSyncServer) private PowershellCommand getCommand(String command, ActiveSyncServer activeSyncServer)
throws GatewayServiceException { throws GatewayServiceException {
String[] urlParts = activeSyncServer.getGatewayUrl().split("/"); String[] urlParts = activeSyncServer.getGatewayUrl().split("/");
@ -426,6 +413,13 @@ public class ExchangeOnlineCEAEnforcementServiceImpl implements CEAEnforcementSe
return commandBuilder.build(); return commandBuilder.build();
} }
/**
* Wrap powershell command to effect all mailboxes in active sync server
* @param command {@link PowershellCommand} command to wrap
* @param activeSyncServer {@link ActiveSyncServer}
* @return {@link PowershellCommand}
* @throws GatewayServiceException Throws when error occurred while retrieving access token
*/
private PowershellCommand toAllMailboxesCommand(PowershellCommand command, private PowershellCommand toAllMailboxesCommand(PowershellCommand command,
ActiveSyncServer activeSyncServer) throws GatewayServiceException { ActiveSyncServer activeSyncServer) throws GatewayServiceException {
PowershellCommand getEXOMailbox = getCommand(Parser.COMMAND_GetEXOMailbox.COMMAND, activeSyncServer); PowershellCommand getEXOMailbox = getCommand(Parser.COMMAND_GetEXOMailbox.COMMAND, activeSyncServer);
@ -438,13 +432,24 @@ public class ExchangeOnlineCEAEnforcementServiceImpl implements CEAEnforcementSe
return getEXOMailbox; return getEXOMailbox;
} }
/**
* Create new powershell request to execute via powershell binaries
* @param command {@link PowershellCommand}
* @return {@link PowershellRequest}
*/
private PowershellRequest getPowershellRequest(PowershellCommand command) { private PowershellRequest getPowershellRequest(PowershellCommand command) {
PowershellRequest powershellRequest = new PowershellRequest(); PowershellRequest powershellRequest = new PowershellRequest();
powershellRequest.setCommand(command); powershellRequest.setCommand(command);
return powershellRequest; return powershellRequest;
} }
private List<MailboxProfile> generateMailboxPolicies(List<ActiveSyncDevice> activeSyncAllowedDevices, /**
* Generate mailbox profiles from active sync block and allowed devices
* @param activeSyncAllowedDevices Active sync allowed device list
* @param activeSyncBlockedDevices Active sync blocked device list
* @return List of {@link MailboxProfile}
*/
private List<MailboxProfile> generateMailboxProfiles(List<ActiveSyncDevice> activeSyncAllowedDevices,
List<ActiveSyncDevice> activeSyncBlockedDevices) { List<ActiveSyncDevice> activeSyncBlockedDevices) {
List<MailboxProfile> mailboxProfiles = new ArrayList<>(); List<MailboxProfile> mailboxProfiles = new ArrayList<>();
MailboxProfile mailboxProfile; MailboxProfile mailboxProfile;
@ -474,6 +479,12 @@ public class ExchangeOnlineCEAEnforcementServiceImpl implements CEAEnforcementSe
return mailboxProfiles; return mailboxProfiles;
} }
/**
* Construct active sync device list from powershell response
* @param powershellResponse Shell response return from powershell binary
* @return List of {@link ActiveSyncDevice}
* @throws CEAEnforcementException Throws when error occurred while generating the device list
*/
private List<ActiveSyncDevice> constructActiveSyncDeviceList(PowershellResponse powershellResponse) private List<ActiveSyncDevice> constructActiveSyncDeviceList(PowershellResponse powershellResponse)
throws CEAEnforcementException { throws CEAEnforcementException {
if (powershellResponse == null) { if (powershellResponse == null) {
@ -514,6 +525,15 @@ public class ExchangeOnlineCEAEnforcementServiceImpl implements CEAEnforcementSe
return activeSyncDevices; return activeSyncDevices;
} }
/**
* Get active sync devices, which are connected with active sync server after a certain timestamp
* @param after Timestamp to retrieve connected devices
* @param activeSyncServer {@link ActiveSyncServer}
* @return List of {@link ActiveSyncDevice}
* @throws GatewayServiceException Throws when error occurred while retrieving access token
* @throws PowershellExecutionException Throws when error occurred while executing the powershell command
* @throws CEAEnforcementException Throws when error occurred while constructing device list
*/
private List<ActiveSyncDevice> getConnectedActiveSyncDevicesAfter(Date after, ActiveSyncServer activeSyncServer) private List<ActiveSyncDevice> getConnectedActiveSyncDevicesAfter(Date after, ActiveSyncServer activeSyncServer)
throws GatewayServiceException, PowershellExecutionException, CEAEnforcementException { throws GatewayServiceException, PowershellExecutionException, CEAEnforcementException {
SimpleDateFormat powershellDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); SimpleDateFormat powershellDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
@ -541,6 +561,15 @@ public class ExchangeOnlineCEAEnforcementServiceImpl implements CEAEnforcementSe
return constructActiveSyncDeviceList(powershellResponse); return constructActiveSyncDeviceList(powershellResponse);
} }
/**
* Get active sync devices, which are connected with active sync server before a certain timestamp
* @param before Timestamp to retrieve connected devices
* @param activeSyncServer {@link ActiveSyncServer}
* @return List of {@link ActiveSyncDevice}
* @throws GatewayServiceException Throws when error occurred while retrieving access token
* @throws PowershellExecutionException Throws when error occurred while executing the powershell command
* @throws CEAEnforcementException Throws when error occurred while constructing device list
*/
private List<ActiveSyncDevice> getConnectedActiveSyncDevicesBefore(Date before, ActiveSyncServer activeSyncServer) private List<ActiveSyncDevice> getConnectedActiveSyncDevicesBefore(Date before, ActiveSyncServer activeSyncServer)
throws GatewayServiceException, PowershellExecutionException, CEAEnforcementException { throws GatewayServiceException, PowershellExecutionException, CEAEnforcementException {
SimpleDateFormat powershellDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); SimpleDateFormat powershellDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
@ -568,6 +597,14 @@ public class ExchangeOnlineCEAEnforcementServiceImpl implements CEAEnforcementSe
return constructActiveSyncDeviceList(powershellResponse); return constructActiveSyncDeviceList(powershellResponse);
} }
/**
* Get all connected active sync devices from active sync server
* @param activeSyncServer {@link ActiveSyncServer}
* @return List of {@link ActiveSyncDevice}
* @throws GatewayServiceException Throws when error occurred while retrieving access token
* @throws PowershellExecutionException Throws when error occurred while executing the powershell command
* @throws CEAEnforcementException Throws when error occurred while constructing device list
*/
private List<ActiveSyncDevice> getAllConnectedActiveSyncDevices(ActiveSyncServer activeSyncServer) private List<ActiveSyncDevice> getAllConnectedActiveSyncDevices(ActiveSyncServer activeSyncServer)
throws GatewayServiceException, PowershellExecutionException, CEAEnforcementException { throws GatewayServiceException, PowershellExecutionException, CEAEnforcementException {
PowershellCommand getEXOMobileDeviceStatistics = getCommand(Parser.COMMAND_GetEXOMobileDeviceStatistics.COMMAND, PowershellCommand getEXOMobileDeviceStatistics = getCommand(Parser.COMMAND_GetEXOMobileDeviceStatistics.COMMAND,

@ -83,6 +83,14 @@ public class ExchangeOnlineGatewayServiceImpl implements GatewayService {
} }
} }
/**
* Retrieve confidential client application if exists, otherwise create and retrieve
* @param clientId Client ID of the Azure AD application
* @param secret Client Secret of the Azure AD application
* @param authority Authority URL of the tenant which Azure AD application belongs
* @return {@link IConfidentialClientApplication}
* @throws MalformedURLException Throws when trying to set malformed authority URL
*/
private IConfidentialClientApplication getOrCreateConfidentialClientApplication(String clientId, String secret, String authority) private IConfidentialClientApplication getOrCreateConfidentialClientApplication(String clientId, String secret, String authority)
throws MalformedURLException { throws MalformedURLException {
IConfidentialClientApplication confidentialClientApplication = confidentialClientApplications.get(clientId); IConfidentialClientApplication confidentialClientApplication = confidentialClientApplications.get(clientId);

@ -41,41 +41,31 @@ import java.util.stream.Collectors;
public class DeviceMgtUtil { public class DeviceMgtUtil {
private static final Log log = LogFactory.getLog(DeviceMgtUtil.class); private static final Log log = LogFactory.getLog(DeviceMgtUtil.class);
public static List<ActiveSyncDevice> getEnrolledActiveSyncDevicesSince(Date since) /**
throws DeviceManagementException, UserStoreException { * Retrieve enrolled devices before or after a certain timestamp
DeviceManagementProviderService deviceManagementProviderService = getDeviceManagementProviderService(); * @param date Timestamp to retrieve devices
if (deviceManagementProviderService == null) { * @param isPriorTo Whether to retrieve prior devices based on the provided timestamp
String msg = "Device management provider service has not initialized"; * @return List of {@link ActiveSyncDevice}
log.error(msg); * @throws DeviceManagementException Throws when error occurred while retrieving devices
throw new IllegalStateException(msg); * @throws UserStoreException Throws when failed to obtain user details belongs to a device
} */
List<Device> devices = deviceManagementProviderService.getEnrolledDevicesSince(since); public static List<ActiveSyncDevice> getEnrolledActiveSyncDevices(Date date, boolean isPriorTo)
if (devices == null) {
return new ArrayList<>();
}
return DeviceMgtUtil.constructActiveSyncDeviceList(devices);
}
public static List<ActiveSyncDevice> getEnrolledActiveSyncDevicesPriorTo(Date priorTo)
throws DeviceManagementException, UserStoreException { throws DeviceManagementException, UserStoreException {
DeviceManagementProviderService deviceManagementProviderService = getDeviceManagementProviderService(); DeviceManagementProviderService deviceManagementProviderService =
EnforcementServiceComponentDataHolder.getInstance().getDeviceManagementProviderService();
if (deviceManagementProviderService == null) { if (deviceManagementProviderService == null) {
String msg = "Device management provider service has not initialized"; String msg = "Device management provider service has not initialized";
log.error(msg); log.error(msg);
throw new IllegalStateException(msg); throw new IllegalStateException(msg);
} }
List<Device> devices = deviceManagementProviderService.getEnrolledDevicesPriorTo(priorTo); List<Device> devices = isPriorTo ? deviceManagementProviderService.getEnrolledDevicesPriorTo(date) :
deviceManagementProviderService.getEnrolledDevicesSince(date);
if (devices == null) { if (devices == null) {
return new ArrayList<>(); return new ArrayList<>();
} }
return DeviceMgtUtil.constructActiveSyncDeviceList(devices); return DeviceMgtUtil.constructActiveSyncDeviceList(devices);
} }
private static DeviceManagementProviderService getDeviceManagementProviderService() {
return EnforcementServiceComponentDataHolder.getInstance().getDeviceManagementProviderService();
}
private static UserStoreManager getUserStoreManager(int tenantId) throws UserStoreException { private static UserStoreManager getUserStoreManager(int tenantId) throws UserStoreException {
RealmService realmService = EnforcementServiceComponentDataHolder.getInstance().getRealmService(); RealmService realmService = EnforcementServiceComponentDataHolder.getInstance().getRealmService();
@ -87,11 +77,12 @@ public class DeviceMgtUtil {
return realmService.getTenantUserRealm(tenantId).getUserStoreManager(); return realmService.getTenantUserRealm(tenantId).getUserStoreManager();
} }
private static String getIdentity(String owner, UserStoreManager userStoreManager) /**
throws UserStoreException { * Generate active sync device list from retrieved device list from device management service
return userStoreManager.getUserClaimValue(owner, Constants.EMAIL_CLAIM_URI, null); * @param devices List of devices retrieved from device management service
} * @return List of {@link ActiveSyncDevice}
* @throws UserStoreException Throws when failed to load user details form user store
*/
private static List<ActiveSyncDevice> constructActiveSyncDeviceList(List<Device> devices) private static List<ActiveSyncDevice> constructActiveSyncDeviceList(List<Device> devices)
throws UserStoreException { throws UserStoreException {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
@ -114,11 +105,19 @@ public class DeviceMgtUtil {
return activeSyncDevices; return activeSyncDevices;
} }
/**
* Map devices which are retrieved from device management service to active sync devices
* @param device Device list retrieved from device management service
* @param userStoreManager {@link UserStoreManager}
* @return {@link ActiveSyncDevice}
* @throws UserStoreException Throws when failed to load user details form user store
*/
public static ActiveSyncDevice mapToActiveSyncDevice(Device device, UserStoreManager userStoreManager) public static ActiveSyncDevice mapToActiveSyncDevice(Device device, UserStoreManager userStoreManager)
throws UserStoreException { throws UserStoreException {
EnrolmentInfo enrolmentInfo = device.getEnrolmentInfo(); EnrolmentInfo enrolmentInfo = device.getEnrolmentInfo();
ActiveSyncDevice activeSyncDevice = new ActiveSyncDevice(); ActiveSyncDevice activeSyncDevice = new ActiveSyncDevice();
activeSyncDevice.setUserPrincipalName(DeviceMgtUtil.getIdentity(enrolmentInfo.getOwner(), userStoreManager)); activeSyncDevice.setUserPrincipalName(userStoreManager.
getUserClaimValue(enrolmentInfo.getOwner(), Constants.EMAIL_CLAIM_URI, null));
if (!Objects.equals(device.getType(), Constants.DEVICE_TYPE_ANDROID)) { if (!Objects.equals(device.getType(), Constants.DEVICE_TYPE_ANDROID)) {
for (Device.Property property : device.getProperties()) { for (Device.Property property : device.getProperties()) {
if (property != null && Objects.equals(property.getName(), Constants.DEVICE_PROPERTY_EAS_ID)) { if (property != null && Objects.equals(property.getName(), Constants.DEVICE_PROPERTY_EAS_ID)) {

@ -25,6 +25,8 @@ import io.entgra.device.mgt.core.cea.mgt.enforce.bean.PowershellRequest;
import io.entgra.device.mgt.core.cea.mgt.enforce.bean.PowershellResponse; import io.entgra.device.mgt.core.cea.mgt.enforce.bean.PowershellResponse;
import io.entgra.device.mgt.core.cea.mgt.enforce.exception.PowershellExecutionException; import io.entgra.device.mgt.core.cea.mgt.enforce.exception.PowershellExecutionException;
import io.entgra.device.mgt.core.cea.mgt.enforce.util.shell.Powershell; import io.entgra.device.mgt.core.cea.mgt.enforce.util.shell.Powershell;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.IOException; import java.io.IOException;
@ -36,6 +38,7 @@ import java.util.Arrays;
import java.util.List; import java.util.List;
public class AbstractPowershell implements Powershell { public class AbstractPowershell implements Powershell {
private static final Log log = LogFactory.getLog(AbstractPowershell.class);
protected static final String SYMBOL_SPLITTER = "&"; protected static final String SYMBOL_SPLITTER = "&";
private static final String PARAMETER_COMMAND = "-Command"; private static final String PARAMETER_COMMAND = "-Command";
private static final String COMMAND_REDIRECT_WARNINGS = "$WarningPreference = 'SilentlyContinue';"; private static final String COMMAND_REDIRECT_WARNINGS = "$WarningPreference = 'SilentlyContinue';";
@ -75,10 +78,12 @@ public class AbstractPowershell implements Powershell {
} catch (IOException e) { } catch (IOException e) {
String msg = "IOException occurred while executing powershell command : " String msg = "IOException occurred while executing powershell command : "
+ powershellRequest.getCommand(); + powershellRequest.getCommand();
log.error(msg, e);
throw new PowershellExecutionException(msg, e); throw new PowershellExecutionException(msg, e);
} catch (InterruptedException e) { } catch (InterruptedException e) {
String msg = "Thread got interrupted while executing powershell command : " String msg = "Thread got interrupted while executing powershell command : "
+ powershellRequest.getCommand(); + powershellRequest.getCommand();
log.error(msg, e);
throw new PowershellExecutionException(msg, e); throw new PowershellExecutionException(msg, e);
} }
} }

@ -297,7 +297,7 @@ public interface CEAManagementAdminService {
) CEAPolicyWrapper ceaPolicyWrapper); ) CEAPolicyWrapper ceaPolicyWrapper);
@GET @GET
@Path("/syncNow") @Path("/sync-now")
@ApiOperation( @ApiOperation(
produces = MediaType.APPLICATION_JSON, produces = MediaType.APPLICATION_JSON,
httpMethod = HttpMethod.GET, httpMethod = HttpMethod.GET,

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2018 - 2023, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. * Copyright (c) 2018 - 2024, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved.
* *
* Entgra (Pvt) Ltd. licenses this file to you under the Apache License, * Entgra (Pvt) Ltd. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except * Version 2.0 (the "License"); you may not use this file except
@ -14,6 +14,7 @@
* KIND, either express or implied. See the License for the * KIND, either express or implied. See the License for the
* specific language governing permissions and limitations * specific language governing permissions and limitations
* under the License. * under the License.
*
*/ */
package io.entgra.device.mgt.core.device.mgt.api.jaxrs.service.impl.admin; package io.entgra.device.mgt.core.device.mgt.api.jaxrs.service.impl.admin;
@ -153,7 +154,7 @@ public class CEAManagementAdminServiceImpl implements CEAManagementAdminService
} }
@GET @GET
@Path("/syncNow") @Path("/sync-now")
@Override @Override
public Response sync() { public Response sync() {
CEAManagementService ceaManagementService = DeviceMgtAPIUtils.getCEAManagementService(); CEAManagementService ceaManagementService = DeviceMgtAPIUtils.getCEAManagementService();
@ -167,6 +168,11 @@ public class CEAManagementAdminServiceImpl implements CEAManagementAdminService
} }
} }
/**
* Construct {@link CEAPolicy} from {@link CEAPolicyWrapper}
* @param ceaPolicyWrapper {@link CEAPolicyWrapper}
* @return {@link CEAPolicy}
*/
private CEAPolicy constructCEAPolicy(CEAPolicyWrapper ceaPolicyWrapper) { private CEAPolicy constructCEAPolicy(CEAPolicyWrapper ceaPolicyWrapper) {
AccessPolicyWrapper accessPolicyWrapper = ceaPolicyWrapper.getConditionalAccessPolicyEntries(); AccessPolicyWrapper accessPolicyWrapper = ceaPolicyWrapper.getConditionalAccessPolicyEntries();
AccessPolicy accessPolicy = new AccessPolicy(); AccessPolicy accessPolicy = new AccessPolicy();
@ -193,20 +199,4 @@ public class CEAManagementAdminServiceImpl implements CEAManagementAdminService
ceaPolicy.setActiveSyncServer(activeSyncServer); ceaPolicy.setActiveSyncServer(activeSyncServer);
return ceaPolicy; return ceaPolicy;
} }
private List<ActiveSyncServerUIConfiguration> constructActiveSyncServerConfigurations
(List<ServerUIConfiguration> serverUIConfigurations) {
List<ActiveSyncServerUIConfiguration> activeSyncServerUIConfigurations = new ArrayList<>();
if (serverUIConfigurations == null) {
return activeSyncServerUIConfigurations;
}
for (ServerUIConfiguration serverUIConfiguration : serverUIConfigurations) {
ActiveSyncServerUIConfiguration activeSyncServerUIConfiguration = new ActiveSyncServerUIConfiguration();
activeSyncServerUIConfiguration.setName(serverUIConfiguration.getName());
activeSyncServerUIConfiguration.setKey(serverUIConfiguration.getKey());
activeSyncServerUIConfiguration.setDescription(serverUIConfiguration.getDescription());
activeSyncServerUIConfigurations.add(activeSyncServerUIConfiguration);
}
return activeSyncServerUIConfigurations;
}
} }

@ -923,6 +923,10 @@ public class RequestValidationUtil {
} }
} }
/**
* Validate conditional access policy
* @param ceaPolicyWrapper {@link CEAPolicyWrapper}
*/
public static void validateCEAPolicy(CEAPolicyWrapper ceaPolicyWrapper) { public static void validateCEAPolicy(CEAPolicyWrapper ceaPolicyWrapper) {
if (ceaPolicyWrapper == null) { if (ceaPolicyWrapper == null) {
String msg = "CEA policy should not be null"; String msg = "CEA policy should not be null";
@ -934,6 +938,10 @@ public class RequestValidationUtil {
validateCEAGracePeriod(ceaPolicyWrapper.getGracePeriodEntries()); validateCEAGracePeriod(ceaPolicyWrapper.getGracePeriodEntries());
} }
/**
* Validate active sync server configurations
* @param activeSyncServer {@link ActiveSyncServer}
*/
public static void validateActiveSyncServer(ActiveSyncServer activeSyncServer) { public static void validateActiveSyncServer(ActiveSyncServer activeSyncServer) {
if (activeSyncServer == null) { if (activeSyncServer == null) {
String msg = "Active sync server should not be null"; String msg = "Active sync server should not be null";
@ -962,6 +970,10 @@ public class RequestValidationUtil {
} }
} }
/**
* Validate conditional access policy configurations
* @param accessPolicyWrapper {@link AccessPolicyWrapper}
*/
public static void validateCEAAccessPolicy(AccessPolicyWrapper accessPolicyWrapper) { public static void validateCEAAccessPolicy(AccessPolicyWrapper accessPolicyWrapper) {
if (accessPolicyWrapper == null) { if (accessPolicyWrapper == null) {
String msg = "Access policy should not be null"; String msg = "Access policy should not be null";
@ -982,6 +994,10 @@ public class RequestValidationUtil {
} }
} }
/**
* Validate grace period configurations
* @param gracePeriodWrapper {@link GracePeriodWrapper}
*/
public static void validateCEAGracePeriod(GracePeriodWrapper gracePeriodWrapper) { public static void validateCEAGracePeriod(GracePeriodWrapper gracePeriodWrapper) {
if (gracePeriodWrapper == null) { if (gracePeriodWrapper == null) {
String msg = "Grace period should not be null"; String msg = "Grace period should not be null";

Loading…
Cancel
Save