2 * Copyright (c) 2018 AT&T Intellectual Property.
4 * Modifications Copyright (c) 2018 Verizon Property.
5 * Modifications Copyright (c) 2019 Nordix Foundation.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
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.
15 * See the License for the specific language governing permissions and
17 * limitations under the License.
20 package org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration;
22 import static org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder.getErrorWithParameters;
24 import java.io.IOException;
26 import java.util.Optional;
27 import org.apache.commons.collections4.CollectionUtils;
28 import org.openecomp.core.utilities.file.FileContentHandler;
29 import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum;
30 import org.openecomp.sdc.be.csar.storage.ArtifactInfo;
31 import org.openecomp.sdc.common.errors.CoreException;
32 import org.openecomp.sdc.common.errors.Messages;
33 import org.openecomp.sdc.common.utils.SdcCommon;
34 import org.openecomp.sdc.datatypes.error.ErrorLevel;
35 import org.openecomp.sdc.datatypes.error.ErrorMessage;
36 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.OrchestrationTemplateCandidateData;
37 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
38 import org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validation.CsarSecurityValidator;
39 import org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validation.ValidationResult;
40 import org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validation.Validator;
41 import org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validation.ValidatorFactory;
42 import org.openecomp.sdc.vendorsoftwareproduct.security.SecurityManagerException;
43 import org.openecomp.sdc.vendorsoftwareproduct.services.filedatastructuremodule.CandidateService;
44 import org.openecomp.sdc.vendorsoftwareproduct.types.OnboardPackage;
45 import org.openecomp.sdc.vendorsoftwareproduct.types.OnboardPackageInfo;
46 import org.openecomp.sdc.vendorsoftwareproduct.types.OnboardSignedPackage;
47 import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileResponse;
49 public class OrchestrationTemplateCSARHandler extends BaseOrchestrationTemplateHandler {
52 public UploadFileResponse validate(final OnboardPackageInfo onboardPackageInfo) {
53 final UploadFileResponse uploadFileResponse = new UploadFileResponse();
54 if (onboardPackageInfo.getPackageType() == OnboardingTypesEnum.SIGNED_CSAR) {
55 final OnboardSignedPackage originalOnboardPackage = (OnboardSignedPackage) onboardPackageInfo.getOriginalOnboardPackage();
56 final ArtifactInfo artifactInfo = onboardPackageInfo.getArtifactInfo();
57 validatePackageSecurity(originalOnboardPackage, artifactInfo).ifPresent(packageSignatureResponse -> {
58 if (packageSignatureResponse.hasErrors()) {
59 uploadFileResponse.addStructureErrors(packageSignatureResponse.getErrors());
62 if (uploadFileResponse.hasErrors()) {
63 return uploadFileResponse;
66 final OnboardPackage onboardPackage = onboardPackageInfo.getOnboardPackage();
67 final FileContentHandler fileContentHandler = onboardPackage.getFileContentHandler();
69 final Validator validator = ValidatorFactory.getValidator(fileContentHandler);
70 final ValidationResult validationResult = validator.validate(fileContentHandler);
71 if (CollectionUtils.isNotEmpty(validationResult.getErrors())) {
72 uploadFileResponse.addStructureErrors(Map.of(SdcCommon.UPLOAD_FILE, validationResult.getErrors()));
74 } catch (IOException exception) {
75 logger.error(exception.getMessage(), exception);
77 .addStructureError(SdcCommon.UPLOAD_FILE, new ErrorMessage(ErrorLevel.ERROR, Messages.INVALID_CSAR_FILE.getErrorMessage()));
78 } catch (CoreException coreException) {
79 logger.error(coreException.getMessage(), coreException);
80 uploadFileResponse.addStructureError(SdcCommon.UPLOAD_FILE, new ErrorMessage(ErrorLevel.ERROR, coreException.getMessage()));
82 return uploadFileResponse;
85 private Optional<UploadFileResponse> validatePackageSecurity(final OnboardSignedPackage signedPackage, final ArtifactInfo artifactInfo) {
86 final UploadFileResponse uploadFileResponseDto = new UploadFileResponse();
88 final CsarSecurityValidator csarSecurityValidator = new CsarSecurityValidator();
89 if (!csarSecurityValidator.verifyPackageSignature(signedPackage, artifactInfo)) {
90 final ErrorMessage errorMessage = new ErrorMessage(ErrorLevel.ERROR, Messages.FAILED_TO_VERIFY_SIGNATURE.getErrorMessage());
91 logger.error(errorMessage.getMessage());
92 uploadFileResponseDto.addStructureError(SdcCommon.UPLOAD_FILE, errorMessage);
93 return Optional.of(uploadFileResponseDto);
95 } catch (final SecurityManagerException e) {
96 final ErrorMessage errorMessage = new ErrorMessage(ErrorLevel.ERROR, e.getMessage());
97 logger.error("Could not validate package signature {}", signedPackage.getFilename(), e);
98 uploadFileResponseDto.addStructureError(SdcCommon.UPLOAD_FILE, errorMessage);
99 return Optional.of(uploadFileResponseDto);
101 return Optional.empty();
105 protected UploadFileResponse updateCandidateData(final VspDetails vspDetails, final OnboardPackageInfo onboardPackageInfo,
106 final CandidateService candidateService) {
107 final UploadFileResponse uploadFileResponse = new UploadFileResponse();
108 final OnboardPackage csarPackage = onboardPackageInfo.getOnboardPackage();
109 final OnboardPackage originalOnboardPackage = onboardPackageInfo.getOriginalOnboardPackage();
111 final var candidateData = new OrchestrationTemplateCandidateData(csarPackage.getFileContent(), csarPackage.getFileExtension(),
112 csarPackage.getFilename(), originalOnboardPackage.getFilename(), originalOnboardPackage.getFileExtension(),
113 originalOnboardPackage.getFileContent(), onboardPackageInfo.getArtifactInfo());
114 candidateService.updateCandidateUploadData(vspDetails.getId(), vspDetails.getVersion(), candidateData);
115 } catch (final Exception exception) {
116 logger.error(getErrorWithParameters(Messages.FILE_LOAD_CONTENT_ERROR.getErrorMessage(), getHandlerType().toString()), exception);
117 uploadFileResponse.addStructureError(SdcCommon.UPLOAD_FILE, new ErrorMessage(ErrorLevel.ERROR, exception.getMessage()));
119 return uploadFileResponse;
123 protected OnboardingTypesEnum getHandlerType() {
124 return OnboardingTypesEnum.CSAR;