Fix security hotspot on zipinputstream processing
[nonrtric/plt/rappmanager.git] / rapp-manager-models / src / main / java / com / oransc / rappmanager / models / csar / RappCsarConfigurationHandler.java
1 /*-
2  * ============LICENSE_START======================================================================
3  * Copyright (C) 2023 Nordix Foundation. All rights reserved.
4  * ===============================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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  * ============LICENSE_END========================================================================
17  */
18
19 package com.oransc.rappmanager.models.csar;
20
21 import com.oransc.rappmanager.models.rapp.Rapp;
22 import com.oransc.rappmanager.models.rapp.RappResources;
23 import com.oransc.rappmanager.models.rappinstance.RappACMInstance;
24 import com.oransc.rappmanager.models.rappinstance.RappSMEInstance;
25 import java.io.ByteArrayOutputStream;
26 import java.io.File;
27 import java.io.FileInputStream;
28 import java.io.IOException;
29 import java.nio.file.Path;
30 import java.util.List;
31 import java.util.UUID;
32 import java.util.function.Predicate;
33 import java.util.zip.ZipEntry;
34 import java.util.zip.ZipFile;
35 import org.apache.commons.compress.archivers.ArchiveEntry;
36 import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39 import org.springframework.stereotype.Service;
40 import org.springframework.web.multipart.MultipartFile;
41
42 @Service
43 public class RappCsarConfigurationHandler {
44
45     Logger logger = LoggerFactory.getLogger(RappCsarConfigurationHandler.class);
46     private static final String ACM_COMPOSITION_JSON_LOCATION = "Files/Acm/definition/compositions.json";
47     private static final String ACM_DEFINITION_LOCATION = "Files/Acm/definition";
48     private static final String ACM_INSTANCES_LOCATION = "Files/Acm/instances";
49
50     private static final String SME_PROVIDER_FUNCS_LOCATION = "Files/Sme/providers";
51     private static final String SME_SERVICE_APIS_LOCATION = "Files/Sme/serviceapis";
52
53     private static final String SME_INVOKERS_LOCATION = "Files/Sme/invokers";
54
55
56     public boolean isValidRappPackage(MultipartFile multipartFile) {
57         String originalFilename = multipartFile.getOriginalFilename();
58         if (originalFilename != null) {
59             return originalFilename.endsWith(".csar") && isFileExistsInCsar(multipartFile,
60                     ACM_COMPOSITION_JSON_LOCATION);
61         }
62         return false;
63     }
64
65     boolean isFileExistsInCsar(MultipartFile multipartFile, String fileLocation) {
66         try (ZipArchiveInputStream zipArchiveInputStream = new ZipArchiveInputStream(multipartFile.getInputStream())) {
67             ArchiveEntry zipEntry;
68             while ((zipEntry = zipArchiveInputStream.getNextEntry()) != null) {
69                 if (zipEntry.getName().matches(fileLocation)) {
70                     return Boolean.TRUE;
71                 }
72             }
73             return Boolean.FALSE;
74         } catch (IOException e) {
75             logger.error("Unable to find the CSAR file", e);
76             return Boolean.FALSE;
77         }
78     }
79
80     public Path getRappPackageLocation(String csarLocation, String rappId, String fileName) {
81         return Path.of(csarLocation, rappId, fileName);
82     }
83
84     public String getInstantiationPayload(Rapp rapp, RappACMInstance rappACMInstance, UUID compositionId) {
85         return getPayload(rapp, getResourceUri(ACM_INSTANCES_LOCATION, rappACMInstance.getInstance())).replaceAll(
86                 "COMPOSITIONID", String.valueOf(compositionId));
87     }
88
89     String getPayload(Rapp rapp, String location) {
90         logger.info("Getting payload for {} from {}", rapp.getRappId(), location);
91         File csarFile = getCsarFile(rapp);
92         return getFileFromCsar(csarFile, location).toString();
93     }
94
95     File getCsarFile(Rapp rapp) {
96         return new File(
97                 getRappPackageLocation(rapp.getPackageLocation(), rapp.getName(), rapp.getPackageName()).toUri());
98     }
99
100     ByteArrayOutputStream getFileFromCsar(File csarFile, String fileLocation) {
101         ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
102         try (FileInputStream fileInputStream = new FileInputStream(csarFile);
103              ZipArchiveInputStream zipArchiveInputStream = new ZipArchiveInputStream(fileInputStream)) {
104             ArchiveEntry entry;
105             while ((entry = zipArchiveInputStream.getNextEntry()) != null) {
106                 if (!entry.isDirectory() && entry.getName().equals(fileLocation)) {
107                     byte[] buffer = new byte[1024];
108                     int bytesRead;
109                     while ((bytesRead = zipArchiveInputStream.read(buffer)) != -1) {
110                         byteArrayOutputStream.write(buffer, 0, bytesRead);
111                     }
112                 }
113             }
114         } catch (IOException e) {
115             logger.error("Unable to find the CSAR file", e);
116         }
117         return byteArrayOutputStream;
118     }
119
120
121     public String getSmeProviderDomainPayload(Rapp rapp, RappSMEInstance rappSMEInstance) {
122         return getPayload(rapp, getResourceUri(SME_PROVIDER_FUNCS_LOCATION, rappSMEInstance.getProviderFunction()));
123     }
124
125     public String getSmeProviderApiPayload(Rapp rapp, RappSMEInstance rappSMEInstance) {
126         return getPayload(rapp, getResourceUri(SME_SERVICE_APIS_LOCATION, rappSMEInstance.getServiceApis()));
127     }
128
129     public String getSmeInvokerPayload(Rapp rapp, RappSMEInstance rappSMEInstance) {
130         return getPayload(rapp, getResourceUri(SME_INVOKERS_LOCATION, rappSMEInstance.getInvokers()));
131     }
132
133     public String getAcmCompositionPayload(Rapp rapp) {
134         return getPayload(rapp,
135                 getResourceUri(ACM_DEFINITION_LOCATION, rapp.getRappResources().getAcm().getCompositionDefinitions()));
136     }
137
138     String getResourceUri(String resourceLocation, String resource) {
139         return resourceLocation + "/" + resource + ".json";
140     }
141
142     public RappResources getRappResource(Rapp rapp) {
143         RappResources rappResources = new RappResources();
144         try {
145             File csarFile = getCsarFile(rapp);
146             if (csarFile.exists()) {
147                 rappResources.setAcm(RappResources.ACMResources.builder().compositionDefinitions(
148                         getFileListFromCsar(csarFile, ACM_DEFINITION_LOCATION).get(0)).compositionInstances(
149                         getFileListFromCsar(csarFile, ACM_INSTANCES_LOCATION)).build());
150                 rappResources.setSme(RappResources.SMEResources.builder().providerFunctions(
151                                 getFileListFromCsar(csarFile, SME_PROVIDER_FUNCS_LOCATION))
152                                              .serviceApis(getFileListFromCsar(csarFile, SME_SERVICE_APIS_LOCATION))
153                                              .invokers(getFileListFromCsar(csarFile, SME_INVOKERS_LOCATION)).build());
154             }
155         } catch (Exception e) {
156             logger.warn("Error in getting the rapp resources", e);
157         }
158         return rappResources;
159     }
160
161     List<String> getFileListFromCsar(File csarFile, String dirLocation) {
162         try (ZipFile zipFile = new ZipFile(csarFile)) {
163             return zipFile.stream().filter(Predicate.not(ZipEntry::isDirectory)).map(ZipEntry::getName)
164                            .filter(name -> name.startsWith(dirLocation))
165                            .map(name -> name.substring(name.lastIndexOf("/") + 1, name.lastIndexOf("."))).toList();
166         } catch (IOException e) {
167             logger.warn("Error in listing the files from csar", e);
168         }
169         return List.of();
170     }
171 }