diff options
author | Bogumil Zebek <bogumil.zebek@nokia.com> | 2019-04-17 07:56:27 +0200 |
---|---|---|
committer | Zebek Bogumil <bogumil.zebek@nokia.com> | 2019-04-18 10:29:04 +0200 |
commit | 089d8c3fb0a277351a55371dff8c2b27bd3f4ed5 (patch) | |
tree | 4b5be25b0a4bd80ac6c413e481413fc2c55ea210 /csarvalidation/src/main/java/org/onap/cvc/csar/FileArchive.java | |
parent | 96fcbb323829e81abf746efc5f62763f277786b6 (diff) |
Security TC op2
Change-Id: I247c1223b5731c8dbea1480ca88db1cff78cb633
Issue-ID: VNFSDK-342
Signed-off-by: Zebek Bogumil <bogumil.zebek@nokia.com>
Diffstat (limited to 'csarvalidation/src/main/java/org/onap/cvc/csar/FileArchive.java')
-rw-r--r-- | csarvalidation/src/main/java/org/onap/cvc/csar/FileArchive.java | 215 |
1 files changed, 215 insertions, 0 deletions
diff --git a/csarvalidation/src/main/java/org/onap/cvc/csar/FileArchive.java b/csarvalidation/src/main/java/org/onap/cvc/csar/FileArchive.java new file mode 100644 index 0000000..86904b1 --- /dev/null +++ b/csarvalidation/src/main/java/org/onap/cvc/csar/FileArchive.java @@ -0,0 +1,215 @@ +/* + * Copyright 2019 Nokia + * <p> + * 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 + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * 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. + * + */ + +package org.onap.cvc.csar; + + +import java.io.BufferedInputStream; +import java.io.BufferedOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Optional; +import java.util.stream.Stream; +import java.util.zip.ZipEntry; +import java.util.zip.ZipInputStream; + + + +public class FileArchive { + + private static final String ZIP_POSTFIX = "zip"; + private static final String CSAR_POSTFIX = ".csar"; + private static final String CERT_POSTFIX = ".cert"; + private static final String CMS_POSTFIX = ".cms"; + private final String tempDir; + + FileArchive(String tempDir){ + this.tempDir = tempDir; + } + + Workspace unpack(String pathToFile) throws IOException { + File fileArchive = new File(pathToFile); + if (!fileArchive.exists()) { + throw new IllegalArgumentException(String.format("%s does not exist", fileArchive.getName())); + } + + String path = String.format("%s%s%d", tempDir, File.separator, System.currentTimeMillis()); + Optional<Path> workspaceFolderPath = createWorkspaceFolder(path); + if(workspaceFolderPath.isPresent()) { + final Path destination = workspaceFolderPath.get(); + unzip(fileArchive, destination); + + if (pathToFile.endsWith(ZIP_POSTFIX)) { + return createZipWorkspace(path, destination); + }else { + return Workspace.forCsar(destination); + } + } + + return Workspace.empty(); + } + + private Workspace createZipWorkspace(String path, Path workspaceFolderPath) throws IOException { + + Optional<Path> pathToCsarFile = findFile(workspaceFolderPath, CSAR_POSTFIX); + Optional<Path> pathToCertFile = findFile(workspaceFolderPath, CERT_POSTFIX); + Optional<Path> pathToCmsFile = findFile(workspaceFolderPath, CMS_POSTFIX); + + Path workspaceCsarPath = new File(String.format("%s%scsar", path, File.separator)).toPath(); + if (pathToCsarFile.isPresent()) { + final Path csarFilePath = pathToCsarFile.get(); + unzip(csarFilePath.toFile(), workspaceCsarPath); + + return Workspace.forZip( + workspaceFolderPath, + workspaceCsarPath, + pathToCertFile.orElse(null), + pathToCmsFile.orElse(null), + csarFilePath + ); + } + + + return Workspace.forZip(workspaceFolderPath); + } + + private Optional<Path> findFile(Path workspaceFolderPath, String filePostfix) throws IOException { + try(Stream<Path> files = Files.find( + workspaceFolderPath, + 1, + (p, b)->p.getFileName().toString().endsWith(filePostfix))){ + return files.findFirst(); + } + } + + private Optional<Path> createWorkspaceFolder(String path) { + Path destination = Paths.get(path); + if(destination.toFile().mkdirs()){ + return Optional.of(destination); + } + + return Optional.empty(); + } + + private void unzip(File file, Path destination) throws IOException { + + try (ZipInputStream zipInputStream = new ZipInputStream(new BufferedInputStream(new FileInputStream(file)))){ + + ZipEntry entry; + while ((entry = zipInputStream.getNextEntry()) != null) { + + File filePath = new File(destination + File.separator + entry.getName()); + + if(entry.isDirectory()){ + filePath.mkdirs(); + } else { + extract(zipInputStream, filePath); + } + } + } + } + + private void extract(ZipInputStream csar, File filePath) throws IOException { + byte[] buffer = new byte[2048]; + try (FileOutputStream fos = new FileOutputStream(filePath); + BufferedOutputStream bos = new BufferedOutputStream(fos, buffer.length)) { + + int len; + while ((len = csar.read(buffer)) > 0) { + bos.write(buffer, 0, len); + } + } + } + + public static class Workspace{ + private boolean isZip; + private Path rootFolder; + private Path pathToCsarFolder; + private Path certFile; + private Path cmsFile; + private Path csarFile; + + private Workspace(boolean isZip, Path rootFolder, + Path pathToCsarFolder, + Path certFile, Path cmsFile, + Path csarFile) { + this.isZip = isZip; + this.rootFolder = rootFolder; + this.pathToCsarFolder = pathToCsarFolder; + this.certFile = certFile; + this.cmsFile = cmsFile; + this.csarFile = csarFile; + } + + private Workspace() { + } + + private Workspace(boolean isZip, Path rootFolder){ + this.isZip = isZip; + this.rootFolder = rootFolder; + this.pathToCsarFolder = rootFolder; + } + + static Workspace empty(){ + return new Workspace(); + } + + static Workspace forCsar(Path workspaceFolder) { + return new Workspace(false, workspaceFolder); + } + + static Workspace forZip(Path rootFolder) { + return new Workspace(true, rootFolder); + } + + + static Workspace forZip(Path rootFolder, Path pathToCsarWorkspace, + Path certFile, Path cmsFile, Path csarFile) { + return new Workspace(true, rootFolder, pathToCsarWorkspace, certFile, cmsFile, csarFile); + } + + public boolean isZip() { + return isZip; + } + + public Optional<Path> getPathToCsarFolder() { + return Optional.ofNullable(pathToCsarFolder); + } + + public Optional<Path> getPathToCertFile() { + return Optional.ofNullable(certFile); + } + + public Optional<Path> getRootFolder() { + return Optional.ofNullable(rootFolder); + } + + public Optional<Path> getPathToCmsFile() { + return Optional.ofNullable(cmsFile); + } + + public Optional<Path> getPathToCsarFile() { + return Optional.ofNullable(csarFile); + } + } + +} |