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.
21 package org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration;
23 import static org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder.getErrorWithParameters;
25 import java.io.IOException;
26 import java.util.Optional;
27 import org.openecomp.core.utilities.file.FileContentHandler;
28 import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum;
29 import org.openecomp.sdc.common.errors.CoreException;
30 import org.openecomp.sdc.common.errors.Messages;
31 import org.openecomp.sdc.common.utils.SdcCommon;
32 import org.openecomp.sdc.datatypes.error.ErrorLevel;
33 import org.openecomp.sdc.datatypes.error.ErrorMessage;
34 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.OrchestrationTemplateCandidateData;
35 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
36 import org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validation.CsarSecurityValidator;
37 import org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validation.Validator;
38 import org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validation.ValidatorFactory;
39 import org.openecomp.sdc.vendorsoftwareproduct.security.SecurityManagerException;
40 import org.openecomp.sdc.vendorsoftwareproduct.services.filedatastructuremodule.CandidateService;
41 import org.openecomp.sdc.vendorsoftwareproduct.types.OnboardPackage;
42 import org.openecomp.sdc.vendorsoftwareproduct.types.OnboardPackageInfo;
43 import org.openecomp.sdc.vendorsoftwareproduct.types.OnboardSignedPackage;
44 import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileResponse;
46 public class OrchestrationTemplateCSARHandler extends BaseOrchestrationTemplateHandler
47 implements OrchestrationTemplateFileHandler {
50 public UploadFileResponse validate(final OnboardPackageInfo onboardPackageInfo) {
51 final UploadFileResponse uploadFileResponse = new UploadFileResponse();
52 if (onboardPackageInfo.getPackageType() == OnboardingTypesEnum.SIGNED_CSAR) {
53 final OnboardSignedPackage originalOnboardPackage =
54 (OnboardSignedPackage) onboardPackageInfo.getOriginalOnboardPackage();
55 validatePackageSecurity(originalOnboardPackage).ifPresent(packageSignatureResponse -> {
56 if (packageSignatureResponse.hasErrors()) {
57 uploadFileResponse.addStructureErrors(packageSignatureResponse.getErrors());
61 if (uploadFileResponse.hasErrors()) {
62 return uploadFileResponse;
65 final OnboardPackage onboardPackage = onboardPackageInfo.getOnboardPackage();
66 final FileContentHandler fileContentHandler = onboardPackage.getFileContentHandler();
69 final Validator validator = ValidatorFactory.getValidator(fileContentHandler);
70 uploadFileResponse.addStructureErrors(validator.validateContent(fileContentHandler));
71 } catch (IOException exception) {
72 logger.error(exception.getMessage(), exception);
73 uploadFileResponse.addStructureError(
74 SdcCommon.UPLOAD_FILE,
75 new ErrorMessage(ErrorLevel.ERROR, Messages.INVALID_CSAR_FILE.getErrorMessage()));
76 } catch (CoreException coreException) {
77 logger.error(coreException.getMessage(), coreException);
78 uploadFileResponse.addStructureError(
79 SdcCommon.UPLOAD_FILE, new ErrorMessage(ErrorLevel.ERROR, coreException.getMessage()));
82 return uploadFileResponse;
85 private Optional<UploadFileResponse> validatePackageSecurity(final OnboardSignedPackage originalOnboardPackage) {
86 final UploadFileResponse uploadFileResponseDto = new UploadFileResponse();
88 final CsarSecurityValidator csarSecurityValidator = new CsarSecurityValidator();
89 if (!csarSecurityValidator.verifyPackageSignature(originalOnboardPackage)) {
90 final ErrorMessage errorMessage = new ErrorMessage(ErrorLevel.ERROR,
91 Messages.FAILED_TO_VERIFY_SIGNATURE.getErrorMessage());
92 logger.error(errorMessage.getMessage());
93 uploadFileResponseDto.addStructureError(SdcCommon.UPLOAD_FILE, errorMessage);
94 return Optional.of(uploadFileResponseDto);
96 } catch (final SecurityManagerException e) {
97 final ErrorMessage errorMessage = new ErrorMessage(ErrorLevel.ERROR, e.getMessage());
98 logger.error("Could not validate package signature {}", originalOnboardPackage.getFilename(), e);
99 uploadFileResponseDto.addStructureError(SdcCommon.UPLOAD_FILE, errorMessage);
100 return Optional.of(uploadFileResponseDto);
102 return Optional.empty();
106 protected UploadFileResponse updateCandidateData(final VspDetails vspDetails,
107 final OnboardPackageInfo onboardPackageInfo,
108 final CandidateService candidateService) {
109 final UploadFileResponse uploadFileResponse = new UploadFileResponse();
110 final OnboardPackage csarPackage = onboardPackageInfo.getOnboardPackage();
111 final OnboardPackage originalOnboardPackage = onboardPackageInfo.getOriginalOnboardPackage();
113 candidateService.updateCandidateUploadData(vspDetails.getId(), vspDetails.getVersion(),
114 new OrchestrationTemplateCandidateData(csarPackage.getFileContent(),
115 "", csarPackage.getFileExtension(),
116 csarPackage.getFilename(), originalOnboardPackage.getFilename(),
117 originalOnboardPackage.getFileExtension(),
118 originalOnboardPackage.getFileContent()));
119 } catch (final Exception exception) {
120 logger.error(getErrorWithParameters(Messages.FILE_CONTENT_MAP.getErrorMessage(),
121 getHandlerType().toString()), exception);
122 uploadFileResponse.addStructureError(SdcCommon.UPLOAD_FILE,
123 new ErrorMessage(ErrorLevel.ERROR, exception.getMessage()));
125 return uploadFileResponse;
129 protected OnboardingTypesEnum getHandlerType() {
130 return OnboardingTypesEnum.CSAR;