From 6b0e2811122330ce39658e3478b18790e163d0c6 Mon Sep 17 00:00:00 2001 From: lasantha Date: Sun, 18 Feb 2018 08:23:15 +0530 Subject: [PATCH] Adding APK file validation functionalty To implement apk file validation we are using external library. APK parser: https://github.com/hsiafan/apk-parser --- .../services/ApplicationStorageManager.java | 6 +- .../pom.xml | 19 ++++- .../impl/ApplicationStorageManagerImpl.java | 77 ++++++++++++++----- 3 files changed, 78 insertions(+), 24 deletions(-) diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/ApplicationStorageManager.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/ApplicationStorageManager.java index 33b76ce5dc..61daf1a96d 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/ApplicationStorageManager.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/ApplicationStorageManager.java @@ -61,20 +61,22 @@ public interface ApplicationStorageManager { * To upload release artifacts for an Application. * * @param applicationRelease Application Release Object. + * @param appType Application Type. * @param binaryFile Binary File for the release. * @throws ResourceManagementException Resource Management Exception. */ - ApplicationRelease uploadReleaseArtifacts(ApplicationRelease applicationRelease, InputStream binaryFile) + ApplicationRelease uploadReleaseArtifact(ApplicationRelease applicationRelease, String appType, InputStream binaryFile) throws ResourceManagementException; /** * To upload release artifacts for an Application. * * @param applicationRelease applicationRelease Application release of a particular application. + * @param appType Type of the application * @param binaryFile Binary File for the release. * @throws ApplicationStorageManagementException Resource Management Exception. */ - ApplicationRelease updateReleaseArtifacts(ApplicationRelease applicationRelease, InputStream binaryFile) + ApplicationRelease updateReleaseArtifacts(ApplicationRelease applicationRelease, String appType, InputStream binaryFile) throws ApplicationStorageManagementException; /** diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/pom.xml b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/pom.xml index 57b8686ef8..6dec736627 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/pom.xml +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/pom.xml @@ -76,13 +76,25 @@ org.wso2.carbon.user.api.*, org.wso2.carbon.ndatasource.core, org.wso2.carbon, + javax.annotation, + org.bouncycastle.cert, + org.bouncycastle.cert.jcajce, + org.bouncycastle.cms, + org.bouncycastle.jce.provider, + org.bouncycastle.util, + org.xml.sax, + org.xml.sax.helpers, org.apache.commons.io, org.apache.commons.codec.binary;version="${commons-codec.wso2.osgi.version.range}", org.apache.commons.codec.digest;version="${commons-codec.wso2.osgi.version.range}", org.wso2.carbon.base, org.wso2.carbon.device.mgt.core.dto.*;version="${carbon.device.mgt.version}", - org.wso2.carbon.device.mgt.core.dao.*;version="${carbon.device.mgt.version}" + org.wso2.carbon.device.mgt.core.dao.*;version="${carbon.device.mgt.version}", + net.dongliu.* + apk-parser;scope=compile|runtime;inline=false + true + true !org.wso2.carbon.device.application.mgt.core.internal.*, org.wso2.carbon.device.application.mgt.core.* @@ -169,6 +181,11 @@ org.wso2.carbon org.wso2.carbon.core + + net.dongliu + apk-parser + 2.5.2 + diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationStorageManagerImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationStorageManagerImpl.java index 91d9b3566a..ca9bf967eb 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationStorageManagerImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationStorageManagerImpl.java @@ -19,12 +19,15 @@ package org.wso2.carbon.device.application.mgt.core.impl; +import net.dongliu.apk.parser.ApkFile; +import net.dongliu.apk.parser.bean.ApkMeta; import org.apache.commons.codec.digest.DigestUtils; import org.apache.commons.io.IOUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.device.application.mgt.common.ApplicationRelease; +import org.wso2.carbon.device.application.mgt.common.ApplicationType; import org.wso2.carbon.device.application.mgt.common.exception.ApplicationStorageManagementException; import org.wso2.carbon.device.application.mgt.common.exception.ResourceManagementException; import org.wso2.carbon.device.application.mgt.common.services.ApplicationStorageManager; @@ -33,6 +36,7 @@ import org.wso2.carbon.device.application.mgt.core.util.Constants; import org.wso2.carbon.device.application.mgt.core.util.StorageManagementUtil; import java.io.File; +import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.List; @@ -155,45 +159,76 @@ public class ApplicationStorageManagerImpl implements ApplicationStorageManager } @Override - public ApplicationRelease uploadReleaseArtifacts(ApplicationRelease applicationRelease, InputStream binaryFile) + public ApplicationRelease uploadReleaseArtifact(ApplicationRelease applicationRelease, String appType, InputStream binaryFile) throws ResourceManagementException { String artifactDirectoryPath; String md5OfApp; md5OfApp = getMD5(binaryFile); - //todo validate binary file. - if (md5OfApp != null) { - artifactDirectoryPath = storagePath + md5OfApp; - StorageManagementUtil.createArtifactDirectory(artifactDirectoryPath); - if (log.isDebugEnabled()) { - log.debug("Artifact Directory Path for saving the application release related artifacts related with " - + "application UUID " + applicationRelease.getUuid() + " is " + artifactDirectoryPath); + try { + + if (ApplicationType.ANDROID.toString().equals(appType)){ + String prefix = "stream2file"; + String suffix = ".apk"; + Boolean isTempDelete; + + File tempFile = File.createTempFile(prefix, suffix); + FileOutputStream out = new FileOutputStream(tempFile); + IOUtils.copy(binaryFile, out); + ApkFile apkFile = new ApkFile(tempFile); + ApkMeta apkMeta = apkFile.getApkMeta(); + applicationRelease.setVersion(apkMeta.getVersionName()); + isTempDelete = tempFile.delete(); + if (!isTempDelete) { + log.error("Temporary created APK file deletion failed"); + } + }else if (ApplicationType.iOS.toString().equals(appType)){ + //todo iOS ipa validate + }else if (ApplicationType.WEB_CLIP.toString().equals(appType)){ + //todo Web Clip validate + }else{ + throw new ApplicationStorageManagementException("Application Type doesn't match with supporting " + + "application types " + applicationRelease.getUuid()); } - try { - saveFile(binaryFile, artifactDirectoryPath + Constants.RELEASE_ARTIFACT); - applicationRelease.setAppStoredLoc(artifactDirectoryPath); + + + + + if (md5OfApp != null) { + artifactDirectoryPath = storagePath + md5OfApp; + StorageManagementUtil.createArtifactDirectory(artifactDirectoryPath); + if (log.isDebugEnabled()) { + log.debug("Artifact Directory Path for saving the application release related artifacts related with " + + "application UUID " + applicationRelease.getUuid() + " is " + artifactDirectoryPath); + } + + String artifactPath = artifactDirectoryPath + Constants.RELEASE_ARTIFACT; + saveFile(binaryFile, artifactPath); + applicationRelease.setAppStoredLoc(artifactPath); applicationRelease.setAppHashValue(md5OfApp); - } catch (IOException e) { - throw new ApplicationStorageManagementException( - "IO Exception while saving the release artifacts in the server for the application UUID " - + applicationRelease.getUuid(), e); + } else { + throw new ApplicationStorageManagementException("Error occurred while md5sum value retrieving process: " + + "application UUID " + applicationRelease.getUuid()); } - - } else { - log.error("Verify application existence and md5sum value retrieving process"); + } catch (IOException e) { + throw new ApplicationStorageManagementException( + "IO Exception while saving the release artifacts in the server for the application UUID " + + applicationRelease.getUuid(), e); } + + return applicationRelease; } @Override - public ApplicationRelease updateReleaseArtifacts(ApplicationRelease applicationRelease, InputStream binaryFile) - throws ApplicationStorageManagementException { + public ApplicationRelease updateReleaseArtifacts(ApplicationRelease applicationRelease, String appType, + InputStream binaryFile) throws ApplicationStorageManagementException { if (binaryFile != null) { try { deleteApplicationReleaseArtifacts(applicationRelease.getAppStoredLoc()); - applicationRelease = uploadReleaseArtifacts(applicationRelease, binaryFile); + applicationRelease = uploadReleaseArtifact(applicationRelease, appType, binaryFile); } catch (ApplicationStorageManagementException e) { throw new ApplicationStorageManagementException("Application Artifact doesn't contains in the System", e); } catch (ResourceManagementException e) {