Adding APK file validation functionalty

To implement apk file validation we are using external library. APK parser: https://github.com/hsiafan/apk-parser
feature/appm-store/pbac
lasantha 7 years ago
parent 460a356bee
commit 6b0e281112

@ -61,20 +61,22 @@ public interface ApplicationStorageManager {
* To upload release artifacts for an Application. * To upload release artifacts for an Application.
* *
* @param applicationRelease Application Release Object. * @param applicationRelease Application Release Object.
* @param appType Application Type.
* @param binaryFile Binary File for the release. * @param binaryFile Binary File for the release.
* @throws ResourceManagementException Resource Management Exception. * @throws ResourceManagementException Resource Management Exception.
*/ */
ApplicationRelease uploadReleaseArtifacts(ApplicationRelease applicationRelease, InputStream binaryFile) ApplicationRelease uploadReleaseArtifact(ApplicationRelease applicationRelease, String appType, InputStream binaryFile)
throws ResourceManagementException; throws ResourceManagementException;
/** /**
* To upload release artifacts for an Application. * To upload release artifacts for an Application.
* *
* @param applicationRelease applicationRelease Application release of a particular application. * @param applicationRelease applicationRelease Application release of a particular application.
* @param appType Type of the application
* @param binaryFile Binary File for the release. * @param binaryFile Binary File for the release.
* @throws ApplicationStorageManagementException Resource Management Exception. * @throws ApplicationStorageManagementException Resource Management Exception.
*/ */
ApplicationRelease updateReleaseArtifacts(ApplicationRelease applicationRelease, InputStream binaryFile) ApplicationRelease updateReleaseArtifacts(ApplicationRelease applicationRelease, String appType, InputStream binaryFile)
throws ApplicationStorageManagementException; throws ApplicationStorageManagementException;
/** /**

@ -76,13 +76,25 @@
org.wso2.carbon.user.api.*, org.wso2.carbon.user.api.*,
org.wso2.carbon.ndatasource.core, org.wso2.carbon.ndatasource.core,
org.wso2.carbon, 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.io,
org.apache.commons.codec.binary;version="${commons-codec.wso2.osgi.version.range}", 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.apache.commons.codec.digest;version="${commons-codec.wso2.osgi.version.range}",
org.wso2.carbon.base, org.wso2.carbon.base,
org.wso2.carbon.device.mgt.core.dto.*;version="${carbon.device.mgt.version}", 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.*
</Import-Package> </Import-Package>
<Embed-Dependency>apk-parser;scope=compile|runtime;inline=false</Embed-Dependency>
<Embed-Transitive>true</Embed-Transitive>
<Embed-StripGroup>true</Embed-StripGroup>
<Export-Package> <Export-Package>
!org.wso2.carbon.device.application.mgt.core.internal.*, !org.wso2.carbon.device.application.mgt.core.internal.*,
org.wso2.carbon.device.application.mgt.core.* org.wso2.carbon.device.application.mgt.core.*
@ -169,6 +181,11 @@
<groupId>org.wso2.carbon</groupId> <groupId>org.wso2.carbon</groupId>
<artifactId>org.wso2.carbon.core</artifactId> <artifactId>org.wso2.carbon.core</artifactId>
</dependency> </dependency>
<dependency>
<groupId>net.dongliu</groupId>
<artifactId>apk-parser</artifactId>
<version>2.5.2</version>
</dependency>
</dependencies> </dependencies>
</project> </project>

@ -19,12 +19,15 @@
package org.wso2.carbon.device.application.mgt.core.impl; 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.codec.digest.DigestUtils;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.device.application.mgt.common.ApplicationRelease; 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.ApplicationStorageManagementException;
import org.wso2.carbon.device.application.mgt.common.exception.ResourceManagementException; import org.wso2.carbon.device.application.mgt.common.exception.ResourceManagementException;
import org.wso2.carbon.device.application.mgt.common.services.ApplicationStorageManager; 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 org.wso2.carbon.device.application.mgt.core.util.StorageManagementUtil;
import java.io.File; import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.List; import java.util.List;
@ -155,45 +159,76 @@ public class ApplicationStorageManagerImpl implements ApplicationStorageManager
} }
@Override @Override
public ApplicationRelease uploadReleaseArtifacts(ApplicationRelease applicationRelease, InputStream binaryFile) public ApplicationRelease uploadReleaseArtifact(ApplicationRelease applicationRelease, String appType, InputStream binaryFile)
throws ResourceManagementException { throws ResourceManagementException {
String artifactDirectoryPath; String artifactDirectoryPath;
String md5OfApp; String md5OfApp;
md5OfApp = getMD5(binaryFile); md5OfApp = getMD5(binaryFile);
//todo validate binary file.
if (md5OfApp != null) { try {
artifactDirectoryPath = storagePath + md5OfApp;
StorageManagementUtil.createArtifactDirectory(artifactDirectoryPath); if (ApplicationType.ANDROID.toString().equals(appType)){
if (log.isDebugEnabled()) { String prefix = "stream2file";
log.debug("Artifact Directory Path for saving the application release related artifacts related with " String suffix = ".apk";
+ "application UUID " + applicationRelease.getUuid() + " is " + artifactDirectoryPath); 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); applicationRelease.setAppHashValue(md5OfApp);
} catch (IOException e) { } else {
throw new ApplicationStorageManagementException( throw new ApplicationStorageManagementException("Error occurred while md5sum value retrieving process: " +
"IO Exception while saving the release artifacts in the server for the application UUID " "application UUID " + applicationRelease.getUuid());
+ applicationRelease.getUuid(), e);
} }
} catch (IOException e) {
} else { throw new ApplicationStorageManagementException(
log.error("Verify application existence and md5sum value retrieving process"); "IO Exception while saving the release artifacts in the server for the application UUID "
+ applicationRelease.getUuid(), e);
} }
return applicationRelease; return applicationRelease;
} }
@Override @Override
public ApplicationRelease updateReleaseArtifacts(ApplicationRelease applicationRelease, InputStream binaryFile) public ApplicationRelease updateReleaseArtifacts(ApplicationRelease applicationRelease, String appType,
throws ApplicationStorageManagementException { InputStream binaryFile) throws ApplicationStorageManagementException {
if (binaryFile != null) { if (binaryFile != null) {
try { try {
deleteApplicationReleaseArtifacts(applicationRelease.getAppStoredLoc()); deleteApplicationReleaseArtifacts(applicationRelease.getAppStoredLoc());
applicationRelease = uploadReleaseArtifacts(applicationRelease, binaryFile); applicationRelease = uploadReleaseArtifact(applicationRelease, appType, binaryFile);
} catch (ApplicationStorageManagementException e) { } catch (ApplicationStorageManagementException e) {
throw new ApplicationStorageManagementException("Application Artifact doesn't contains in the System", e); throw new ApplicationStorageManagementException("Application Artifact doesn't contains in the System", e);
} catch (ResourceManagementException e) { } catch (ResourceManagementException e) {

Loading…
Cancel
Save