e6bca42662ffee7289f4708e351fcd9aa7d43b9c
[policy/distribution.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2018 Ericsson. All rights reserved.
4  *  Copyright (C) 2019 Nordix Foundation.
5  *  Modifications Copyright (C) 2020-2021 AT&T Inc.
6  * ================================================================================
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  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.distribution.reception.decoding.policy.file;
24
25 import java.io.IOException;
26 import java.nio.file.Path;
27 import java.util.ArrayList;
28 import java.util.Collection;
29 import java.util.Enumeration;
30 import java.util.zip.ZipEntry;
31 import java.util.zip.ZipFile;
32 import org.onap.policy.common.parameters.ParameterService;
33 import org.onap.policy.common.utils.coder.CoderException;
34 import org.onap.policy.common.utils.coder.StandardCoder;
35 import org.onap.policy.common.utils.coder.StandardYamlCoder;
36 import org.onap.policy.distribution.model.Csar;
37 import org.onap.policy.distribution.model.PolicyInput;
38 import org.onap.policy.distribution.reception.decoding.PolicyDecoder;
39 import org.onap.policy.distribution.reception.decoding.PolicyDecodingException;
40 import org.onap.policy.models.tosca.authorative.concepts.ToscaEntity;
41 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
42
43 /**
44  * This class extracts policy files from a CSAR file.
45  *
46  * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com)
47  */
48 public class PolicyDecoderFileInCsarToPolicy implements PolicyDecoder<Csar, ToscaEntity> {
49
50     private PolicyDecoderFileInCsarToPolicyParameterGroup decoderParameters;
51     private StandardCoder coder;
52     private StandardYamlCoder yamlCoder;
53     private static final long MAX_FILE_SIZE = 512L * 1024;
54
55     /**
56      * {@inheritDoc}.
57      */
58     @Override
59     public void configure(final String parameterGroupName) {
60         decoderParameters = ParameterService.get(parameterGroupName);
61         coder = new StandardCoder();
62         yamlCoder = new StandardYamlCoder();
63     }
64
65     /**
66      * {@inheritDoc}.
67      */
68     @Override
69     public boolean canHandle(final PolicyInput policyInput) {
70         return policyInput.getClass().isAssignableFrom(Csar.class);
71     }
72
73     /**
74      * {@inheritDoc}.
75      */
76     @Override
77     public Collection<ToscaEntity> decode(final Csar csar) throws PolicyDecodingException {
78         final Collection<ToscaEntity> policyList = new ArrayList<>();
79
80         try (ZipFile zipFile = new ZipFile(csar.getCsarPath())) {
81             final Enumeration<? extends ZipEntry> entries = zipFile.entries();
82             while (entries.hasMoreElements()) {
83                 //
84                 // Sonar will flag this as a Security Hotspot
85                 // "Expanding archive files is security-sensitive"
86                 // isZipEntryValid ensures the file being read exists in the archive
87                 //
88                 final ZipEntry entry = entries.nextElement(); // NOSONAR
89                 if (isZipEntryValid(entry.getName(), csar.getCsarPath(), entry.getSize())) {
90                     final ToscaServiceTemplate policy =
91                             decodeFile(zipFile, entry);
92                     policyList.add(policy);
93                 }
94             }
95         } catch (final IOException | CoderException exp) {
96             throw new PolicyDecodingException("Failed decoding the policy", exp);
97         }
98
99         return policyList;
100     }
101
102     /**
103      * Method to filter out Policy type and Policy files. In addition,
104      * ensures validation of entries in the Zipfile. Attempts to solve path
105      * injection java security issues.
106      *
107      * @param entry the ZipEntry to check
108      * @param csarPath Absolute path to the csar the ZipEntry is in
109      * @return true if no injection detected, and it is a policy type  or policy file.
110      * @throws PolicyDecodingException if the file size is too large
111      */
112     private boolean isZipEntryValid(String entryName, String csarPath, long entrySize) throws PolicyDecodingException {
113         //
114         // We only care about policy types and policies
115         //
116         if (entryName.contains(decoderParameters.getPolicyTypeFileName())
117                 || entryName.contains(decoderParameters.getPolicyFileName())) {
118             //
119             // Check file size
120             //
121             if (entrySize > MAX_FILE_SIZE) {
122                 throw new PolicyDecodingException("Zip entry for " + entryName + " is too large " + entrySize);
123             }
124             //
125             // Now ensure that there is no path injection
126             //
127             Path path = Path.of(csarPath, entryName).normalize();
128             //
129             // Throw an exception if path is outside the csar
130             //
131             if (! path.startsWith(csarPath)) {
132                 throw new PolicyDecodingException("Potential path injection for zip entry " + entryName);
133             }
134             return true;
135         }
136
137         return false;
138     }
139
140     /**
141      * Method to decode either a json or yaml file into an object.
142      *
143      * @param zipFile the zip file
144      * @param entry the entry to read in the zip file.
145      * @return the decoded ToscaServiceTemplate object.
146      * @throws CoderException IOException if the file decoding fails.
147      */
148     private ToscaServiceTemplate decodeFile(ZipFile zipFile, final ZipEntry entry) throws IOException, CoderException {
149         ToscaServiceTemplate policy = null;
150         if (entry.getName().endsWith(".json")) {
151             policy = coder.decode(zipFile.getInputStream(entry), ToscaServiceTemplate.class);
152         } else if (entry.getName().endsWith(".yaml")) {
153             policy = yamlCoder.decode(zipFile.getInputStream(entry), ToscaServiceTemplate.class);
154         }
155         return policy;
156     }
157 }