From 433947b5ab5e28fc29aee447de934de89a707419 Mon Sep 17 00:00:00 2001 From: "andre.schmid" Date: Thu, 19 Sep 2019 16:14:01 +0100 Subject: Centralize onboarding package validation Change-Id: I3cc58cf15f62008e83cfc7ddb095d07ab216b82a Issue-ID: SDC-2583 Signed-off-by: andre.schmid --- .../org/openecomp/sdc/common/errors/Messages.java | 8 +++++- .../sdc/common/exception/ZipException.java | 33 ++++++++++++++++++++++ .../org/openecomp/sdc/common/utils/CommonUtil.java | 32 +++++++++++++++++++++ 3 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 openecomp-be/lib/openecomp-common-lib/src/main/java/org/openecomp/sdc/common/exception/ZipException.java (limited to 'openecomp-be/lib/openecomp-common-lib/src') diff --git a/openecomp-be/lib/openecomp-common-lib/src/main/java/org/openecomp/sdc/common/errors/Messages.java b/openecomp-be/lib/openecomp-common-lib/src/main/java/org/openecomp/sdc/common/errors/Messages.java index 7163dbecfd..99de164fb3 100644 --- a/openecomp-be/lib/openecomp-common-lib/src/main/java/org/openecomp/sdc/common/errors/Messages.java +++ b/openecomp-be/lib/openecomp-common-lib/src/main/java/org/openecomp/sdc/common/errors/Messages.java @@ -22,6 +22,12 @@ public enum Messages { VERSION_UPGRADE("Item %s is of old version. A check out was made in order to get new " + "functionalities"), + PACKAGE_PROCESS_ERROR("Could not process package '%s'"), + PACKAGE_INVALID_EXTENSION("Invalid package '%s' extension. Expecting %s."), + PACKAGE_EMPTY_ERROR("The given package is empty '%s'"), + PACKAGE_PROCESS_INTERNAL_PACKAGE_ERROR("Could not process internal package '%s'"), + PACKAGE_INVALID_ERROR("Invalid package content '%s'"), + PACKAGE_MISSING_INTERNAL_PACKAGE("Missing expected internal package"), INVALID_ZIP_FILE("Invalid zip file"), INVALID_CSAR_FILE("Invalid csar file"), CSAR_FILE_NOT_FOUND("Each CSAR file must contain %s file."), @@ -207,7 +213,7 @@ public enum Messages { /* Notifications */ FAILED_TO_MARK_NOTIFICATION_AS_READ("Failed to mark notifications as read"), FAILED_TO_UPDATE_LAST_SEEN_NOTIFICATION("Failed to update last seen notification for user %s"), - FAILED_TO_VERIFY_SIGNATURE("Cannot verify signature of signed archive!"); + FAILED_TO_VERIFY_SIGNATURE("Could not verify signature of signed package."); private String errorMessage; diff --git a/openecomp-be/lib/openecomp-common-lib/src/main/java/org/openecomp/sdc/common/exception/ZipException.java b/openecomp-be/lib/openecomp-common-lib/src/main/java/org/openecomp/sdc/common/exception/ZipException.java new file mode 100644 index 0000000000..d26fae62e4 --- /dev/null +++ b/openecomp-be/lib/openecomp-common-lib/src/main/java/org/openecomp/sdc/common/exception/ZipException.java @@ -0,0 +1,33 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.openecomp.sdc.common.exception; + +import java.io.IOException; + +public class ZipException extends IOException { + + public ZipException(String s) { + super(s); + } + + public ZipException(String s, Throwable throwable) { + super(s, throwable); + } +} diff --git a/openecomp-be/lib/openecomp-common-lib/src/main/java/org/openecomp/sdc/common/utils/CommonUtil.java b/openecomp-be/lib/openecomp-common-lib/src/main/java/org/openecomp/sdc/common/utils/CommonUtil.java index 8610ecb74b..f286dc1e76 100644 --- a/openecomp-be/lib/openecomp-common-lib/src/main/java/org/openecomp/sdc/common/utils/CommonUtil.java +++ b/openecomp-be/lib/openecomp-common-lib/src/main/java/org/openecomp/sdc/common/utils/CommonUtil.java @@ -20,6 +20,7 @@ package org.openecomp.sdc.common.utils; import com.google.common.collect.Multimap; +import java.io.ByteArrayInputStream; import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; @@ -29,11 +30,14 @@ import java.util.Map; import java.util.Objects; import java.util.Optional; import java.util.Set; +import java.util.zip.ZipEntry; +import java.util.zip.ZipInputStream; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.io.FilenameUtils; import org.apache.commons.lang3.tuple.ImmutablePair; import org.apache.commons.lang3.tuple.Pair; import org.openecomp.core.utilities.file.FileContentHandler; +import org.openecomp.core.utilities.file.FileUtils; import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum; import org.openecomp.sdc.common.errors.CoreException; import org.openecomp.sdc.common.errors.ErrorCategory; @@ -71,6 +75,14 @@ public class CommonUtil { return pair.getLeft(); } + /** + * Extracts the zip in memory and build a pair of {@link FileContentHandler} and the zip folder list. The {@link + * FileContentHandler} will only contain the files, not the folders. + * + * @param uploadFileData the zip file to extract + * @return a pair of {@link FileContentHandler} only with the zip files and a list of the zip folders. + * @throws ZipException when there was a problem during the zip reading + */ public static Pair> getFileContentMapFromOrchestrationCandidateZip( byte[] uploadFileData) throws ZipException { final Map zipFileMap = ZipUtils.readZip(uploadFileData, true); @@ -88,6 +100,26 @@ public class CommonUtil { return new ImmutablePair<>(mapFileContent, folderList); } + /** + * Extracts the zip in memory and build the {@link FileContentHandler}. + * + * @param zipFile the zip file to extract + * @return The {@link FileContentHandler} based on the zip content + * @throws ZipException when there was a problem during the zip reading + */ + public static FileContentHandler getZipContent(final byte[] zipFile) throws ZipException { + final Map zipFileMap = ZipUtils.readZip(zipFile, true); + final FileContentHandler fileContentHandler = new FileContentHandler(); + zipFileMap.forEach((key, value) -> { + if (value == null) { + fileContentHandler.addFolder(key); + } else { + fileContentHandler.addFile(key, value); + } + }); + return fileContentHandler; + } + private static void validateNoFolders(List folderList) { if (CollectionUtils.isNotEmpty(folderList)) { throw new CoreException((new ErrorCode.ErrorCodeBuilder()) -- cgit 1.2.3-korg