Add validation of manifest for helm packages.
[sdc.git] / openecomp-be / backend / openecomp-sdc-vendor-software-product-manager / src / main / java / org / openecomp / sdc / vendorsoftwareproduct / impl / onboarding / OnboardingPackageProcessor.java
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation
4  *  Copyright (C) 2021 Nokia
5  *  ================================================================================
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  *  SPDX-License-Identifier: Apache-2.0
18  *  ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.vendorsoftwareproduct.impl.onboarding;
22
23 import static org.openecomp.sdc.common.errors.Messages.COULD_NOT_READ_MANIFEST_FILE;
24 import static org.openecomp.sdc.common.errors.Messages.PACKAGE_EMPTY_ERROR;
25 import static org.openecomp.sdc.common.errors.Messages.PACKAGE_INVALID_ERROR;
26 import static org.openecomp.sdc.common.errors.Messages.PACKAGE_INVALID_EXTENSION;
27 import static org.openecomp.sdc.common.errors.Messages.PACKAGE_MISSING_INTERNAL_PACKAGE;
28 import static org.openecomp.sdc.common.errors.Messages.PACKAGE_PROCESS_ERROR;
29 import static org.openecomp.sdc.common.errors.Messages.PACKAGE_PROCESS_INTERNAL_PACKAGE_ERROR;
30 import static org.openecomp.sdc.vendorsoftwareproduct.security.SecurityManager.ALLOWED_CERTIFICATE_EXTENSIONS;
31 import static org.openecomp.sdc.vendorsoftwareproduct.security.SecurityManager.ALLOWED_SIGNATURE_EXTENSIONS;
32
33 import java.io.ByteArrayInputStream;
34 import java.io.File;
35 import java.io.FileInputStream;
36 import java.io.InputStream;
37 import java.nio.ByteBuffer;
38 import java.nio.charset.StandardCharsets;
39 import java.util.ArrayList;
40 import java.util.Collections;
41 import java.util.HashSet;
42 import java.util.List;
43 import java.util.Map;
44 import java.util.Objects;
45 import java.util.Optional;
46 import java.util.Set;
47 import org.apache.commons.collections4.CollectionUtils;
48 import org.apache.commons.collections4.MapUtils;
49 import org.apache.commons.io.FilenameUtils;
50 import org.openecomp.core.utilities.file.FileContentHandler;
51 import org.openecomp.core.utilities.json.JsonUtil;
52 import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum;
53 import org.openecomp.sdc.common.utils.CommonUtil;
54 import org.openecomp.sdc.common.utils.SdcCommon;
55 import org.openecomp.sdc.common.zip.exception.ZipException;
56 import org.openecomp.sdc.datatypes.error.ErrorLevel;
57 import org.openecomp.sdc.datatypes.error.ErrorMessage;
58 import org.openecomp.sdc.heat.datatypes.manifest.FileData;
59 import org.openecomp.sdc.heat.datatypes.manifest.ManifestContent;
60 import org.openecomp.sdc.logging.api.Logger;
61 import org.openecomp.sdc.logging.api.LoggerFactory;
62 import org.openecomp.sdc.vendorsoftwareproduct.impl.onboarding.validation.CnfPackageValidator;
63 import org.openecomp.sdc.vendorsoftwareproduct.types.OnboardPackage;
64 import org.openecomp.sdc.vendorsoftwareproduct.types.OnboardPackageInfo;
65 import org.openecomp.sdc.vendorsoftwareproduct.types.OnboardSignedPackage;
66
67 public class OnboardingPackageProcessor {
68
69     private static final Logger LOGGER = LoggerFactory.getLogger(OnboardingPackageProcessor.class);
70     private static final String CSAR_EXTENSION = "csar";
71     private static final String ZIP_EXTENSION = "zip";
72
73     private final String packageFileName;
74     private final byte[] packageFileContent;
75     private FileContentHandler packageContent;
76     private final Set<ErrorMessage> errorMessages = new HashSet<>();
77     private final OnboardPackageInfo onboardPackageInfo;
78     private final CnfPackageValidator cnfPackageValidator;
79
80     public OnboardingPackageProcessor(final String packageFileName, final byte[] packageFileContent) {
81         this.packageFileName = packageFileName;
82         this.packageFileContent = packageFileContent;
83         this.cnfPackageValidator = new CnfPackageValidator();
84         onboardPackageInfo = processPackage();
85     }
86
87     public Optional<OnboardPackageInfo> getOnboardPackageInfo() {
88         return Optional.ofNullable(onboardPackageInfo);
89     }
90
91     public boolean hasErrors() {
92         return !errorMessages.isEmpty();
93     }
94
95     public boolean hasNoErrors() {
96         return errorMessages.isEmpty();
97     }
98
99     public Set<ErrorMessage> getErrorMessages() {
100         return errorMessages;
101     }
102
103     private OnboardPackageInfo processPackage() {
104         OnboardPackageInfo packageInfo = null;
105         validateFile();
106         if (hasNoErrors()) {
107             final String packageName = FilenameUtils.getBaseName(packageFileName);
108             final String packageExtension = FilenameUtils.getExtension(packageFileName);
109
110             if (hasSignedPackageStructure()) {
111                 packageInfo = processSignedPackage(packageName, packageExtension);
112             } else {
113                 if (packageExtension.equalsIgnoreCase(CSAR_EXTENSION)) {
114                     packageInfo = processCsarPackage(packageName, packageExtension);
115                 } else if (packageExtension.equalsIgnoreCase(ZIP_EXTENSION)) {
116                     packageInfo = processOnapNativeZipPackage(packageName, packageExtension);
117                 }
118             }
119         }
120         return packageInfo;
121     }
122
123     private void validateFile() {
124         if (!hasValidExtension()) {
125             String message = PACKAGE_INVALID_EXTENSION
126                 .formatMessage(packageFileName, String.join(", ", CSAR_EXTENSION, ZIP_EXTENSION));
127             reportError(ErrorLevel.ERROR, message);
128         } else {
129             try {
130                 packageContent = CommonUtil.getZipContent(packageFileContent);
131                 if (isPackageEmpty()) {
132                     String message = PACKAGE_EMPTY_ERROR.formatMessage(packageFileName);
133                     reportError(ErrorLevel.ERROR, message);
134                 }
135             } catch (final ZipException e) {
136                 String message = PACKAGE_PROCESS_ERROR.formatMessage(packageFileName);
137                 reportError(ErrorLevel.ERROR, message);
138                 LOGGER.error(message, e);
139             }
140         }
141     }
142
143     private OnboardPackageInfo processCsarPackage(String packageName, String packageExtension) {
144         OnboardPackage onboardPackage = new OnboardPackage(packageName, packageExtension,
145             ByteBuffer.wrap(packageFileContent), new OnboardingPackageContentHandler(packageContent));
146         return new OnboardPackageInfo(onboardPackage, OnboardingTypesEnum.CSAR);
147     }
148
149     private OnboardPackageInfo processOnapNativeZipPackage(String packageName, String packageExtension) {
150         ManifestContent manifest = getManifest();
151         if (manifest != null) {
152             List<String> errors = validateZipPackage(manifest);
153             if (errors.isEmpty()) {
154                 final OnboardPackage onboardPackage = new OnboardPackage(packageName, packageExtension,
155                     ByteBuffer.wrap(packageFileContent), packageContent);
156                 return new OnboardPackageInfo(onboardPackage, OnboardingTypesEnum.ZIP);
157             } else {
158                 errors.forEach(message -> reportError(ErrorLevel.ERROR, message));
159             }
160         } else {
161             reportError(ErrorLevel.ERROR,
162                 COULD_NOT_READ_MANIFEST_FILE.formatMessage(SdcCommon.MANIFEST_NAME, packageFileName));
163         }
164         return null;
165     }
166
167     List<String> validateZipPackage(ManifestContent manifest) {
168         ManifestAnalyzer analyzer = new ManifestAnalyzer(manifest);
169         List<String> errors = Collections.emptyList();
170         if (analyzer.hasHelmEntries()) {
171             if (shouldValidateHelmPackage(analyzer)) {
172                 errors = cnfPackageValidator.validateHelmPackage(analyzer.getHelmEntries());
173             }
174         }
175         addDummyHeat(manifest);
176         return errors;
177     }
178
179     boolean shouldValidateHelmPackage(ManifestAnalyzer analyzer) {
180         return analyzer.hasHelmEntries() && !analyzer.hasHeatEntries();
181     }
182
183     private ManifestContent getManifest() {
184         ManifestContent manifest = null;
185         try (InputStream zipFileManifest = packageContent.getFileContentAsStream(SdcCommon.MANIFEST_NAME)) {
186             manifest = JsonUtil.json2Object(zipFileManifest, ManifestContent.class);
187
188         } catch (Exception e) {
189             final String message = COULD_NOT_READ_MANIFEST_FILE.formatMessage(SdcCommon.MANIFEST_NAME, packageFileName);
190             LOGGER.error(message, e);
191         }
192         return manifest;
193     }
194
195     private void addDummyHeat(ManifestContent manifestContent) {
196         // temporary fix for adding dummy base
197         List<FileData> newfiledata = new ArrayList<>();
198         try {
199             boolean heatBase = false;
200             for (FileData fileData : manifestContent.getData()) {
201                 if (Objects.nonNull(fileData.getType()) &&
202                     fileData.getType().equals(FileData.Type.HELM) && fileData.getBase()) {
203                     heatBase = true;
204                     fileData.setBase(false);
205                     FileData dummyHeat = new FileData();
206                     dummyHeat.setBase(true);
207                     dummyHeat.setFile("base_template_dummy_ignore.yaml");
208                     dummyHeat.setType(FileData.Type.HEAT);
209                     FileData dummyEnv = new FileData();
210                     dummyEnv.setBase(false);
211                     dummyEnv.setFile("base_template_dummy_ignore.env");
212                     dummyEnv.setType(FileData.Type.HEAT_ENV);
213                     List<FileData> dataEnvList = new ArrayList<>();
214                     dataEnvList.add(dummyEnv);
215                     dummyHeat.setData(dataEnvList);
216                     newfiledata.add(dummyHeat);
217                     String filePath = new File("").getAbsolutePath() + "/resources";
218                     File envFilePath = new File(filePath + "/base_template.env");
219                     File baseFilePath = new File(filePath + "/base_template.yaml");
220                     try (InputStream envStream = new FileInputStream(envFilePath);
221                         InputStream baseStream = new FileInputStream(baseFilePath)) {
222                         packageContent.addFile("base_template_dummy_ignore.env", envStream);
223                         packageContent.addFile("base_template_dummy_ignore.yaml", baseStream);
224                     } catch (Exception e) {
225                         LOGGER.error("Failed creating input stream {}", e);
226                     }
227                 }
228             }
229             if (heatBase) {
230                 manifestContent.getData().addAll(newfiledata);
231                 InputStream manifestContentStream = new ByteArrayInputStream(
232                     (JsonUtil.object2Json(manifestContent)).getBytes(StandardCharsets.UTF_8));
233                 packageContent.remove(SdcCommon.MANIFEST_NAME);
234                 packageContent.addFile(SdcCommon.MANIFEST_NAME, manifestContentStream);
235             }
236         } catch (Exception e) {
237             final String message = PACKAGE_INVALID_ERROR.formatMessage(packageFileName);
238             LOGGER.error(message, e);
239         }
240     }
241
242     private boolean hasValidExtension() {
243         final String packageExtension = FilenameUtils.getExtension(packageFileName);
244         return packageExtension.equalsIgnoreCase(CSAR_EXTENSION) || packageExtension.equalsIgnoreCase(ZIP_EXTENSION);
245     }
246
247     private OnboardPackageInfo processSignedPackage(final String packageName, final String packageExtension) {
248         final String internalPackagePath = findInternalPackagePath().orElse(null);
249         if (internalPackagePath == null) {
250             reportError(ErrorLevel.ERROR, PACKAGE_MISSING_INTERNAL_PACKAGE.getErrorMessage());
251             return null;
252         }
253         final String signatureFilePath = findSignatureFilePath().orElse(null);
254         final String certificateFilePath = findCertificateFilePath().orElse(null);
255         final OnboardSignedPackage onboardSignedPackage =
256             new OnboardSignedPackage(packageName, packageExtension, ByteBuffer.wrap(packageFileContent),
257                 packageContent, signatureFilePath, internalPackagePath, certificateFilePath);
258
259         final String internalPackageName = FilenameUtils.getName(internalPackagePath);
260         final String internalPackageBaseName = FilenameUtils.getBaseName(internalPackagePath);
261         final String internalPackageExtension = FilenameUtils.getExtension(internalPackagePath);
262         final byte[] internalPackageContent = packageContent.getFileContent(internalPackagePath);
263         final OnboardPackage onboardPackage;
264         try {
265             final OnboardingPackageContentHandler fileContentHandler =
266                 new OnboardingPackageContentHandler(CommonUtil.getZipContent(internalPackageContent));
267             onboardPackage = new OnboardPackage(internalPackageBaseName, internalPackageExtension,
268                 internalPackageContent, fileContentHandler);
269         } catch (final ZipException e) {
270             final String message = PACKAGE_PROCESS_INTERNAL_PACKAGE_ERROR.formatMessage(internalPackageName);
271             LOGGER.error(message, e);
272             reportError(ErrorLevel.ERROR, message);
273             return null;
274         }
275
276         return new OnboardPackageInfo(onboardSignedPackage, onboardPackage, OnboardingTypesEnum.SIGNED_CSAR);
277     }
278
279     private void reportError(final ErrorLevel errorLevel, final String message) {
280         errorMessages.add(new ErrorMessage(errorLevel, message));
281     }
282
283     private Optional<String> findInternalPackagePath() {
284         return packageContent.getFileList().stream()
285             .filter(filePath -> {
286                     final String extension = FilenameUtils.getExtension(filePath);
287                     return CSAR_EXTENSION.equalsIgnoreCase(extension) || ZIP_EXTENSION.equalsIgnoreCase(extension);
288                 }
289             )
290             .findFirst();
291     }
292
293     private boolean isPackageEmpty() {
294         return MapUtils.isEmpty(packageContent.getFiles());
295     }
296
297     private boolean hasSignedPackageStructure() {
298         if (MapUtils.isEmpty(packageContent.getFiles()) || !CollectionUtils.isEmpty(
299             packageContent.getFolderList())) {
300             return false;
301         }
302         final int numberOfFiles = packageContent.getFileList().size();
303         if (numberOfFiles == 2) {
304             return hasOneInternalPackageFile(packageContent) &&
305                 hasOneSignatureFile(packageContent);
306         }
307
308         if (numberOfFiles == 3) {
309             return hasOneInternalPackageFile(packageContent) &&
310                 hasOneSignatureFile(packageContent) &&
311                 hasOneCertificateFile(packageContent);
312         }
313
314         return false;
315     }
316
317     private boolean hasOneInternalPackageFile(final FileContentHandler fileContentHandler) {
318         return fileContentHandler.getFileList().parallelStream()
319             .map(FilenameUtils::getExtension)
320             .map(String::toLowerCase)
321             .filter(file -> file.endsWith(CSAR_EXTENSION)).count() == 1;
322     }
323
324     private boolean hasOneSignatureFile(final FileContentHandler fileContentHandler) {
325         return fileContentHandler.getFileList().parallelStream()
326             .map(FilenameUtils::getExtension)
327             .map(String::toLowerCase)
328             .filter(ALLOWED_SIGNATURE_EXTENSIONS::contains).count() == 1;
329     }
330
331     private boolean hasOneCertificateFile(final FileContentHandler fileContentHandler) {
332         return fileContentHandler.getFileList().parallelStream()
333             .map(FilenameUtils::getExtension)
334             .map(String::toLowerCase)
335             .filter(ALLOWED_CERTIFICATE_EXTENSIONS::contains).count() == 1;
336     }
337
338     private Optional<String> findSignatureFilePath() {
339         final Map<String, byte[]> files = packageContent.getFiles();
340         return files.keySet().stream()
341             .filter(
342                 fileName -> ALLOWED_SIGNATURE_EXTENSIONS.contains(FilenameUtils.getExtension(fileName).toLowerCase()))
343             .findFirst();
344     }
345
346     private Optional<String> findCertificateFilePath() {
347         final Map<String, byte[]> files = packageContent.getFiles();
348         return files.keySet().stream()
349             .filter(
350                 fileName -> ALLOWED_CERTIFICATE_EXTENSIONS.contains(FilenameUtils.getExtension(fileName).toLowerCase()))
351             .findFirst();
352     }
353
354 }