aboutsummaryrefslogtreecommitdiffstats
path: root/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/csar/SOL004ManifestOnboarding.java
blob: 88e7915188a81ad26115d753e90e435239b818ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/*-
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2019 Nordix Foundation.
 *  Modification Copyright (C) 2021 Nokia.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * SPDX-License-Identifier: Apache-2.0
 * ============LICENSE_END=========================================================
 */
package org.openecomp.sdc.tosca.csar;

import static org.openecomp.sdc.tosca.csar.CSARConstants.ETSI_VERSION_2_6_1;
import static org.openecomp.sdc.tosca.csar.CSARConstants.ETSI_VERSION_2_7_1;
import static org.openecomp.sdc.tosca.csar.CSARConstants.MANIFEST_PNF_METADATA_LIMIT_VERSION_3;
import static org.openecomp.sdc.tosca.csar.CSARConstants.MANIFEST_VNF_METADATA_LIMIT_VERSION_3;
import static org.openecomp.sdc.tosca.csar.ManifestTokenType.COMPATIBLE_SPECIFICATION_VERSIONS;

import com.vdurmont.semver4j.Semver;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map.Entry;
import java.util.Optional;
import org.apache.commons.lang3.StringUtils;
import org.openecomp.sdc.common.errors.Messages;

/**
 * Processes a SOL004 Manifest.
 */
public class SOL004ManifestOnboarding extends AbstractOnboardingManifest {

    private int maxAllowedMetaEntries;

    @Override
    protected void processMetadata() {
        Optional<String> currentLine = getCurrentLine();
        //SOL004 #4.3.2: The manifest file shall start with the package metadata
        if (!currentLine.isPresent() || !isMetadata(currentLine.get())) {
            reportError(Messages.MANIFEST_START_METADATA);
            continueToProcess = false;
            return;
        }
        while (continueToProcess) {
            currentLine = readNextNonEmptyLine();
            if (!currentLine.isPresent()) {
                continueToProcess = validateMetadata();
                return;
            }
            final String metadataLine = currentLine.get();
            final String metadataEntry = readEntryName(metadataLine).orElse(null);
            if (!isMetadataEntry(metadataEntry)) {
                if (metadata.size() < getMaxAllowedManifestMetaEntries()) {
                    reportError(Messages.MANIFEST_METADATA_INVALID_ENTRY1, metadataLine);
                    continueToProcess = false;
                    return;
                }
                continueToProcess = validateMetadata();
                return;
            }
            final String metadataValue = readEntryValue(metadataLine).orElse(null);
            addToMetadata(metadataEntry, metadataValue);
            continueToProcess = isValid();
        }
        readNextNonEmptyLine();
    }

    @Override
    protected void processBody() {
        while (continueToProcess) {
            final ManifestTokenType manifestTokenType = detectLineEntry().orElse(null);
            if (manifestTokenType == null) {
                getCurrentLine().ifPresent(line -> reportInvalidLine());
                break;
            }
            switch (manifestTokenType) {
                case CMS_BEGIN:
                    readCmsSignature();
                    break;
                case NON_MANO_ARTIFACT_SETS:
                    processNonManoArtifactEntry();
                    break;
                case SOURCE:
                    processSource();
                    break;
                default:
                    getCurrentLine().ifPresent(line -> reportInvalidLine());
                    continueToProcess = false;
                    break;
            }
        }
    }

    /**
     * Processes the {@link ManifestTokenType#NON_MANO_ARTIFACT_SETS} entry.
     */
    private void processNonManoArtifactEntry() {
        Optional<String> currentLine = readNextNonEmptyLine();
        while (currentLine.isPresent()) {
            final ManifestTokenType manifestTokenType = detectLineEntry().orElse(null);
            if (manifestTokenType == ManifestTokenType.CMS_BEGIN) {
                return;
            }
            if (manifestTokenType != null) {
                reportError(Messages.MANIFEST_INVALID_NON_MANO_KEY, manifestTokenType.getToken());
                continueToProcess = false;
                return;
            }
            final String nonManoKey = readCurrentEntryName().orElse(null);
            if (nonManoKey == null) {
                reportError(Messages.MANIFEST_INVALID_NON_MANO_KEY, currentLine.get());
                continueToProcess = false;
                return;
            }
            readNextNonEmptyLine();
            final List<String> nonManoSourceList = readNonManoSourceList();
            if (!isValid()) {
                continueToProcess = false;
                return;
            }
            if (nonManoSourceList.isEmpty()) {
                reportError(Messages.MANIFEST_EMPTY_NON_MANO_KEY, nonManoKey);
                continueToProcess = false;
                return;
            }
            if (nonManoSources.get(nonManoKey) == null) {
                nonManoSources.put(nonManoKey, nonManoSourceList);
            } else {
                nonManoSources.get(nonManoKey).addAll(nonManoSourceList);
            }
            currentLine = getCurrentLine();
        }
    }

    /**
     * Processes {@link ManifestTokenType#SOURCE} entries in {@link ManifestTokenType#NON_MANO_ARTIFACT_SETS}.
     *
     * @return A list of sources paths
     */
    protected List<String> readNonManoSourceList() {
        final List<String> nonManoSourceList = new ArrayList<>();
        while (getCurrentLine().isPresent()) {
            final ManifestTokenType manifestTokenType = detectLineEntry().orElse(null);
            if (manifestTokenType != ManifestTokenType.SOURCE) {
                break;
            }
            final String value = readCurrentEntryValue().orElse(null);
            if (!StringUtils.isEmpty(value)) {
                nonManoSourceList.add(value);
            } else {
                reportError(Messages.MANIFEST_EMPTY_NON_MANO_SOURCE);
                break;
            }
            readNextNonEmptyLine();
        }
        return nonManoSourceList;
    }

    /**
     * Reads a manifest CMS signature.
     */
    private void readCmsSignature() {
        if (cmsSignature != null) {
            reportError(Messages.MANIFEST_SIGNATURE_DUPLICATED);
            continueToProcess = false;
            return;
        }
        final StringBuilder cmsSignatureBuilder = new StringBuilder();
        cmsSignatureBuilder.append(currentLine).append("\n");
        Optional<String> currentLine = readNextNonEmptyLine();
        if (!getCurrentLine().isPresent()) {
            return;
        }
        while (currentLine.isPresent()) {
            if (detectLineEntry().orElse(null) == ManifestTokenType.CMS_END) {
                cmsSignatureBuilder.append(currentLine.get());
                break;
            }
            cmsSignatureBuilder.append(currentLine.get()).append("\n");
            currentLine = readNextNonEmptyLine();
        }
        if (currentLine.isPresent()) {
            cmsSignature = cmsSignatureBuilder.toString();
            readNextNonEmptyLine();
        }
        if (getCurrentLine().isPresent()) {
            reportError(Messages.MANIFEST_SIGNATURE_LAST_ENTRY);
            continueToProcess = false;
        }
    }

    /**
     * Detects the current line manifest token.
     *
     * @return the current line manifest token.
     */
    protected Optional<ManifestTokenType> detectLineEntry() {
        final Optional<String> currentLine = getCurrentLine();
        if (currentLine.isPresent()) {
            final String line = currentLine.get();
            final String entry = readEntryName(line).orElse(null);
            if (entry == null) {
                return ManifestTokenType.parse(line);
            } else {
                return ManifestTokenType.parse(entry);
            }
        }
        return Optional.empty();
    }

    /**
     * Validates the manifest metadata content, reporting errors found.
     *
     * @return {@code true} if the metadata content is valid, {@code false} otherwise.
     */
    protected boolean validateMetadata() {
        if (metadata.isEmpty()) {
            reportError(Messages.MANIFEST_NO_METADATA);
            return false;
        }
        String key = metadata.keySet().stream().filter(k -> !COMPATIBLE_SPECIFICATION_VERSIONS.getToken().equals(k)).findFirst().orElse(null);
        final ManifestTokenType firstManifestEntryTokenType = ManifestTokenType.parse(key).orElse(null);
        if (firstManifestEntryTokenType == null) {
            reportError(Messages.MANIFEST_METADATA_INVALID_ENTRY1, key);
            return false;
        }
        for (final Entry<String, String> manifestEntry : metadata.entrySet()) {
            final ManifestTokenType manifestEntryTokenType = ManifestTokenType.parse(manifestEntry.getKey()).orElse(null);
            if (manifestEntryTokenType == null) {
                reportError(Messages.MANIFEST_METADATA_INVALID_ENTRY1, manifestEntry.getKey());
                return false;
            }
            if ((firstManifestEntryTokenType.isMetadataVnfEntry() && !manifestEntryTokenType.isMetadataVnfEntry()) || (
                firstManifestEntryTokenType.isMetadataPnfEntry() && !manifestEntryTokenType.isMetadataPnfEntry())) {
                reportError(Messages.MANIFEST_METADATA_UNEXPECTED_ENTRY_TYPE);
                return false;
            }
        }
        if (metadata.entrySet().size() != getMaxAllowedManifestMetaEntries()) {
            reportError(Messages.MANIFEST_METADATA_DOES_NOT_MATCH_LIMIT, getMaxAllowedManifestMetaEntries());
            return false;
        }
        return true;
    }

    /**
     * Processes a Manifest {@link ManifestTokenType#SOURCE} entry.
     */
    private void processSource() {
        final Optional<String> currentLine = getCurrentLine();
        if (!currentLine.isPresent()) {
            return;
        }
        final ManifestTokenType manifestTokenType = detectLineEntry().orElse(null);
        if (manifestTokenType != ManifestTokenType.SOURCE) {
            return;
        }
        final String sourceLine = currentLine.get();
        final String sourcePath = readEntryValue(sourceLine).orElse(null);
        if (sourcePath == null) {
            reportError(Messages.MANIFEST_EXPECTED_SOURCE_PATH);
            return;
        }
        sources.add(sourcePath);
        readNextNonEmptyLine();
        readAlgorithmEntry(sourcePath);
        readSignatureEntry(sourcePath);
    }

    /**
     * Processes entries  {@link ManifestTokenType#ALGORITHM} and {@link ManifestTokenType#HASH} of a {@link ManifestTokenType#SOURCE} entry.
     *
     * @param sourcePath the source path related to the algorithm entry.
     */
    private void readAlgorithmEntry(final String sourcePath) {
        Optional<String> currentLine = getCurrentLine();
        if (!currentLine.isPresent()) {
            return;
        }
        final ManifestTokenType manifestTokenType = detectLineEntry().orElse(null);
        if (manifestTokenType == ManifestTokenType.HASH) {
            reportError(Messages.MANIFEST_EXPECTED_ALGORITHM_BEFORE_HASH);
            continueToProcess = false;
            return;
        }
        if (manifestTokenType != ManifestTokenType.ALGORITHM) {
            return;
        }
        final String algorithmLine = currentLine.get();
        final String algorithmType = readEntryValue(algorithmLine).orElse(null);
        if (algorithmType == null) {
            reportError(Messages.MANIFEST_EXPECTED_ALGORITHM_VALUE);
            continueToProcess = false;
            return;
        }
        currentLine = readNextNonEmptyLine();
        if (!currentLine.isPresent() || detectLineEntry().orElse(null) != ManifestTokenType.HASH) {
            reportError(Messages.MANIFEST_EXPECTED_HASH_ENTRY);
            continueToProcess = false;
            return;
        }
        final String hashLine = currentLine.get();
        final String hash = readEntryValue(hashLine).orElse(null);
        if (hash == null) {
            reportError(Messages.MANIFEST_EXPECTED_HASH_VALUE);
            continueToProcess = false;
            return;
        }
        sourceAndChecksumMap.put(sourcePath, new AlgorithmDigest(algorithmType, hash));
        readNextNonEmptyLine();
    }

    /**
     * Processes entries  {@link ManifestTokenType#SIGNATURE} and {@link ManifestTokenType#CERTIFICATE} of a {@link ManifestTokenType#SOURCE} entry.
     *
     * @param sourcePath the source path related to the algorithm entry.
     */
    private void readSignatureEntry(final String sourcePath) {
        Optional<String> currentLine = getCurrentLine();
        if (!currentLine.isPresent()) {
            return;
        }
        final ManifestTokenType manifestTokenType = detectLineEntry().orElse(null);
        if (manifestTokenType == ManifestTokenType.CERTIFICATE) {
            reportError(Messages.MANIFEST_EXPECTED_SIGNATURE_BEFORE_CERTIFICATE);
            continueToProcess = false;
            return;
        }
        if (manifestTokenType != ManifestTokenType.SIGNATURE) {
            return;
        }
        final String signatureLine = currentLine.get();
        final String signatureFile = readEntryValue(signatureLine).orElse(null);
        if (signatureFile == null) {
            reportError(Messages.MANIFEST_EXPECTED_SIGNATURE_VALUE);
            continueToProcess = false;
            return;
        }
        currentLine = readNextNonEmptyLine();
        if (!currentLine.isPresent() || detectLineEntry().orElse(null) != ManifestTokenType.CERTIFICATE) {
            sourceAndSignatureMap.put(sourcePath, new SignatureData(signatureFile, null));
            return;
        }
        final String certLine = currentLine.get();
        final String certFile = readEntryValue(certLine).orElse(null);
        if (certFile == null) {
            reportError(Messages.MANIFEST_EXPECTED_CERTIFICATE_VALUE);
            continueToProcess = false;
            return;
        }
        sourceAndSignatureMap.put(sourcePath, new SignatureData(signatureFile, certFile));
        readNextNonEmptyLine();
    }

    protected int getMaxAllowedManifestMetaEntries() {
        if (maxAllowedMetaEntries == 0) {
            boolean isVersion3 =
                metadata.containsKey(COMPATIBLE_SPECIFICATION_VERSIONS.getToken()) && !getHighestCompatibleVersion().isLowerThan(ETSI_VERSION_2_7_1);
            //Both PNF and VNF share attribute COMPATIBLE_SPECIFICATION_VERSIONS
            if (isVersion3) {
                maxAllowedMetaEntries = metadata.keySet().stream().anyMatch(
                    k -> !COMPATIBLE_SPECIFICATION_VERSIONS.getToken().equals(k) && isMetadataEntry(k) && ManifestTokenType.parse(k).get()
                        .isMetadataPnfEntry()) ? MANIFEST_PNF_METADATA_LIMIT_VERSION_3 : MANIFEST_VNF_METADATA_LIMIT_VERSION_3;
            } else {
                maxAllowedMetaEntries = MAX_ALLOWED_MANIFEST_META_ENTRIES;
            }
        }
        return maxAllowedMetaEntries;
    }

    private Semver getHighestCompatibleVersion() {
        return Arrays.asList(metadata.get(COMPATIBLE_SPECIFICATION_VERSIONS.getToken()).split(",")).stream().map(Semver::new)
            .max((v1, v2) -> v1.compareTo(v2)).orElse(new Semver(ETSI_VERSION_2_6_1));
    }
}