From 24b3f04e5d2b00af44d555a15df8d76c1098bee3 Mon Sep 17 00:00:00 2001 From: sushilraje <39531904+sushilraje@users.noreply.github.com> Date: Thu, 20 Dec 2018 13:59:53 -0800 Subject: [PATCH] Package type validation while uploading the package(s). --- asa-manager/scripts/docker/run.cmd | 18 ++++++--- .../v1/controllers/PackagesController.java | 6 +++ .../webservice/v1/helpers/PackagesHelper.java | 40 +++++++++++++++++++ .../iothubmanager/services/Devices.java | 4 ++ .../external/ConfigurationsHelper.java | 4 +- 5 files changed, 64 insertions(+), 8 deletions(-) create mode 100644 config/app/com/microsoft/azure/iotsolutions/uiconfig/webservice/v1/helpers/PackagesHelper.java diff --git a/asa-manager/scripts/docker/run.cmd b/asa-manager/scripts/docker/run.cmd index 6d2a65b8..4815cb93 100644 --- a/asa-manager/scripts/docker/run.cmd +++ b/asa-manager/scripts/docker/run.cmd @@ -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 :: - - - - - - - - - - - - - - diff --git a/config/app/com/microsoft/azure/iotsolutions/uiconfig/webservice/v1/controllers/PackagesController.java b/config/app/com/microsoft/azure/iotsolutions/uiconfig/webservice/v1/controllers/PackagesController.java index 57fb48f0..692d78b5 100644 --- a/config/app/com/microsoft/azure/iotsolutions/uiconfig/webservice/v1/controllers/PackagesController.java +++ b/config/app/com/microsoft/azure/iotsolutions/uiconfig/webservice/v1/controllers/PackagesController.java @@ -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; @@ -112,6 +113,11 @@ public CompletionStage 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 " + diff --git a/config/app/com/microsoft/azure/iotsolutions/uiconfig/webservice/v1/helpers/PackagesHelper.java b/config/app/com/microsoft/azure/iotsolutions/uiconfig/webservice/v1/helpers/PackagesHelper.java new file mode 100644 index 00000000..a44fd277 --- /dev/null +++ b/config/app/com/microsoft/azure/iotsolutions/uiconfig/webservice/v1/helpers/PackagesHelper.java @@ -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; + } +} diff --git a/iothub-manager/app/com/microsoft/azure/iotsolutions/iothubmanager/services/Devices.java b/iothub-manager/app/com/microsoft/azure/iotsolutions/iothubmanager/services/Devices.java index 4ca8556a..930d8e0b 100644 --- a/iothub-manager/app/com/microsoft/azure/iotsolutions/iothubmanager/services/Devices.java +++ b/iothub-manager/app/com/microsoft/azure/iotsolutions/iothubmanager/services/Devices.java @@ -510,4 +510,8 @@ private CompletionStage doesDeviceHaveConnectedModules(String deviceId) } ); } + + public void finalize(){ + this.registry.close(); + } } diff --git a/iothub-manager/app/com/microsoft/azure/iotsolutions/iothubmanager/services/external/ConfigurationsHelper.java b/iothub-manager/app/com/microsoft/azure/iotsolutions/iothubmanager/services/external/ConfigurationsHelper.java index e537d5e4..b59a17f4 100644 --- a/iothub-manager/app/com/microsoft/azure/iotsolutions/iothubmanager/services/external/ConfigurationsHelper.java +++ b/iothub-manager/app/com/microsoft/azure/iotsolutions/iothubmanager/services/external/ConfigurationsHelper.java @@ -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.");