Skip to content
This repository has been archived by the owner on Oct 12, 2023. It is now read-only.

Commit

Permalink
Package type validation while uploading the package(s).
Browse files Browse the repository at this point in the history
  • Loading branch information
sushilraje authored Dec 20, 2018
1 parent 22a4e8c commit 24b3f04
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 8 deletions.
18 changes: 12 additions & 6 deletions asa-manager/scripts/docker/run.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,21 @@ IF %ERRORLEVEL% NEQ 0 GOTO FAIL
:: Start the application
echo Starting ASA manager ...
docker run -it -p 9024:9024 ^
-e PCS_TELEMETRY_WEBSERVICE_URL ^
-e PCS_CONFIG_WEBSERVICE_URL ^
-e PCS_IOTHUBMANAGER_WEBSERVICE_URL ^
-e PCS_TELEMETRY_DOCUMENTDB_CONNSTRING ^
-e PCS_EVENTHUB_CONNSTRING ^
-e PCS_EVENTHUB_NAME ^
-e PCS_TELEMETRY_WEBSERVICE_URL=http://host.docker.internal:9004/v1 ^
-e PCS_CONFIG_WEBSERVICE_URL=http://host.docker.internal:9005/v1 ^
-e PCS_IOTHUBMANAGER_WEBSERVICE_URL=http://host.docker.internal:9002/v1 ^
-e PCS_ASA_DATA_AZUREBLOB_ACCOUNT ^
-e PCS_ASA_DATA_AZUREBLOB_KEY ^
-e PCS_ASA_DATA_AZUREBLOB_ENDPOINT_SUFFIX ^
-e PCS_EVENTHUB_CONNSTRING ^
-e PCS_EVENTHUB_NAME ^
-e PCS_AUTH_REQUIRED ^
-e PCS_CORS_WHITELIST ^
-e PCS_AUTH_ISSUER ^
-e PCS_AUTH_AUDIENCE ^
-e PCS_TWIN_READ_WRITE_ENABLED ^
-e PCS_TELEMETRY_DOCUMENTDB_CONNSTRING ^
-e PCS_TELEMETRY_STORAGE_TYPE ^
%DOCKER_IMAGE%:testing

:: - - - - - - - - - - - - - -
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.microsoft.azure.iotsolutions.uiconfig.webservice.auth.Authorize;
import com.microsoft.azure.iotsolutions.uiconfig.services.models.PackageType;
import com.microsoft.azure.iotsolutions.uiconfig.webservice.v1.exceptions.BadRequestException;
import com.microsoft.azure.iotsolutions.uiconfig.webservice.v1.helpers.PackagesHelper;
import com.microsoft.azure.iotsolutions.uiconfig.webservice.v1.models.PackageApiModel;
import com.microsoft.azure.iotsolutions.uiconfig.webservice.v1.models.ConfigTypeListApiModel;
import com.microsoft.azure.iotsolutions.uiconfig.webservice.v1.models.PackageListApiModel;
Expand Down Expand Up @@ -112,6 +113,11 @@ public CompletionStage<Result> createAsync() throws
final String packageType = data.get(PACKAGE_TYPE_PARAM)[0];
String configType = data.get(PACKAGE_CONFIG_TYPE_PARAM)[0];

if (!(PackagesHelper.verifyPackageType(content, packageType))) {
throw new BadRequestException(String.format("Package uploaded is invalid. Package contents" +
" do not match with the given package type %s.", packageType.toString()));
}

if (packageType.equals(PackageType.edgeManifest.toString()) &&
!(StringUtils.isBlank(configType))) {
throw new BadRequestException("Package of type EdgeManifest cannot have parameter " +
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.microsoft.azure.iotsolutions.uiconfig.webservice.v1.helpers;

import com.microsoft.azure.iotsolutions.uiconfig.services.exceptions.InvalidConfigurationException;
import com.microsoft.azure.iotsolutions.uiconfig.services.models.PackageType;
import com.microsoft.azure.sdk.iot.service.Configuration;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import play.libs.Json;

import static play.libs.Json.fromJson;

public class PackagesHelper {

/**
* This function is used to verify if the package type and package contents are
* compatible. for eg:- if package type is DeviceConfiguration it should contain
* "devicesContent" object.
*/
public static boolean verifyPackageType(String packageContent, String packageType) {
if (packageType.equals(PackageType.edgeManifest.toString()) && isEdgePackage(packageContent)) {
return true;
} else if (packageType.equals(PackageType.deviceConfiguration.toString())
&& !(isEdgePackage(packageContent))) {
return true;
}

return false;
}

public static boolean isEdgePackage(String packageContent) {
final Configuration pckgContent = fromJson(Json.parse(packageContent), Configuration.class);

if (MapUtils.isNotEmpty(pckgContent.getContent().getModulesContent()) &&
MapUtils.isEmpty(pckgContent.getContent().getDeviceContent())) {
return true;
}

return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -510,4 +510,8 @@ private CompletionStage<Boolean> doesDeviceHaveConnectedModules(String deviceId)
}
);
}

public void finalize(){
this.registry.close();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,9 @@ public static Boolean isEdgeDeployment(Configuration deployment) throws
/* This is for the backward compatibility, as some of the old
* deployments may not have the required label.
*/
if (deployment.getContent().getModulesContent() != null) {
if (MapUtils.isNotEmpty(deployment.getContent().getModulesContent())) {
return true;
} else if (deployment.getContent().getDeviceContent() != null) {
} else if (MapUtils.isNotEmpty(deployment.getContent().getDeviceContent())) {
return false;
} else {
throw new InvalidConfigurationException("Deployment package type should not be empty.");
Expand Down

0 comments on commit 24b3f04

Please sign in to comment.