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;
25 import java.util.Optional;
26 import org.openecomp.core.utilities.file.FileContentHandler;
27 import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum;
28 import org.openecomp.sdc.common.errors.CoreException;
29 import org.openecomp.sdc.common.errors.Messages;
30 import org.openecomp.sdc.common.utils.SdcCommon;
31 import org.openecomp.sdc.datatypes.error.ErrorLevel;
32 import org.openecomp.sdc.datatypes.error.ErrorMessage;
33 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.OrchestrationTemplateCandidateData;
34 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
35 import org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validation.CsarSecurityValidator;
36 import org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validation.Validator;
37 import org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validation.ValidatorFactory;
38 import org.openecomp.sdc.vendorsoftwareproduct.security.SecurityManagerException;
39 import org.openecomp.sdc.vendorsoftwareproduct.services.filedatastructuremodule.CandidateService;
40 import org.openecomp.sdc.vendorsoftwareproduct.types.OnboardPackage;
41 import org.openecomp.sdc.vendorsoftwareproduct.types.OnboardPackageInfo;
42 import org.openecomp.sdc.vendorsoftwareproduct.types.OnboardSignedPackage;
43 import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileResponse;
45 public class OrchestrationTemplateCSARHandler extends BaseOrchestrationTemplateHandler {
48 public UploadFileResponse validate(final OnboardPackageInfo onboardPackageInfo) {
49 final UploadFileResponse uploadFileResponse = new UploadFileResponse();
50 if (onboardPackageInfo.getPackageType() == OnboardingTypesEnum.SIGNED_CSAR) {
51 final OnboardSignedPackage originalOnboardPackage = (OnboardSignedPackage) onboardPackageInfo.getOriginalOnboardPackage();
52 validatePackageSecurity(originalOnboardPackage).ifPresent(packageSignatureResponse -> {
53 if (packageSignatureResponse.hasErrors()) {
54 uploadFileResponse.addStructureErrors(packageSignatureResponse.getErrors());
57 if (uploadFileResponse.hasErrors()) {
58 return uploadFileResponse;
61 final OnboardPackage onboardPackage = onboardPackageInfo.getOnboardPackage();
62 final FileContentHandler fileContentHandler = onboardPackage.getFileContentHandler();
64 final Validator validator = ValidatorFactory.getValidator(fileContentHandler);
65 uploadFileResponse.addStructureErrors(validator.validateContent(fileContentHandler));
66 } catch (IOException exception) {
67 logger.error(exception.getMessage(), exception);
69 .addStructureError(SdcCommon.UPLOAD_FILE, new ErrorMessage(ErrorLevel.ERROR, Messages.INVALID_CSAR_FILE.getErrorMessage()));
70 } catch (CoreException coreException) {
71 logger.error(coreException.getMessage(), coreException);
72 uploadFileResponse.addStructureError(SdcCommon.UPLOAD_FILE, new ErrorMessage(ErrorLevel.ERROR, coreException.getMessage()));
74 return uploadFileResponse;
77 private Optional<UploadFileResponse> validatePackageSecurity(final OnboardSignedPackage originalOnboardPackage) {
78 final UploadFileResponse uploadFileResponseDto = new UploadFileResponse();
80 final CsarSecurityValidator csarSecurityValidator = new CsarSecurityValidator();
81 if (!csarSecurityValidator.verifyPackageSignature(originalOnboardPackage)) {
82 final ErrorMessage errorMessage = new ErrorMessage(ErrorLevel.ERROR, Messages.FAILED_TO_VERIFY_SIGNATURE.getErrorMessage());
83 logger.error(errorMessage.getMessage());
84 uploadFileResponseDto.addStructureError(SdcCommon.UPLOAD_FILE, errorMessage);
85 return Optional.of(uploadFileResponseDto);
87 } catch (final SecurityManagerException e) {
88 final ErrorMessage errorMessage = new ErrorMessage(ErrorLevel.ERROR, e.getMessage());
89 logger.error("Could not validate package signature {}", originalOnboardPackage.getFilename(), e);
90 uploadFileResponseDto.addStructureError(SdcCommon.UPLOAD_FILE, errorMessage);
91 return Optional.of(uploadFileResponseDto);
93 return Optional.empty();
97 protected UploadFileResponse updateCandidateData(final VspDetails vspDetails, final OnboardPackageInfo onboardPackageInfo,
98 final CandidateService candidateService) {
99 final UploadFileResponse uploadFileResponse = new UploadFileResponse();
100 final OnboardPackage csarPackage = onboardPackageInfo.getOnboardPackage();
101 final OnboardPackage originalOnboardPackage = onboardPackageInfo.getOriginalOnboardPackage();
103 final var candidateData = new OrchestrationTemplateCandidateData(csarPackage.getFileContent(), csarPackage.getFileExtension(),
104 csarPackage.getFilename(), originalOnboardPackage.getFilename(), originalOnboardPackage.getFileExtension(),
105 originalOnboardPackage.getFileContent(), onboardPackageInfo.getArtifactInfo());
106 candidateService.updateCandidateUploadData(vspDetails.getId(), vspDetails.getVersion(), candidateData);
107 } catch (final Exception exception) {
108 logger.error(getErrorWithParameters(Messages.FILE_LOAD_CONTENT_ERROR.getErrorMessage(), getHandlerType().toString()), exception);
109 uploadFileResponse.addStructureError(SdcCommon.UPLOAD_FILE, new ErrorMessage(ErrorLevel.ERROR, exception.getMessage()));
111 return uploadFileResponse;
115 protected OnboardingTypesEnum getHandlerType() {
116 return OnboardingTypesEnum.CSAR;