From 2fb16ce4a3b41045392d926fcf9d33906108c3cf Mon Sep 17 00:00:00 2001 From: ralph isenmann Date: Wed, 10 Feb 2021 14:36:37 +0100 Subject: [docker] Check recieved content. Add a simple check to prevent any content as acceptable --- .../org/openslx/util/vm/DockerMetaDataDummy.java | 64 ++++++++++++++++------ src/main/java/org/openslx/util/vm/VmMetaData.java | 21 +++---- 2 files changed, 55 insertions(+), 30 deletions(-) diff --git a/src/main/java/org/openslx/util/vm/DockerMetaDataDummy.java b/src/main/java/org/openslx/util/vm/DockerMetaDataDummy.java index 3ee964f..eea4d8b 100644 --- a/src/main/java/org/openslx/util/vm/DockerMetaDataDummy.java +++ b/src/main/java/org/openslx/util/vm/DockerMetaDataDummy.java @@ -4,42 +4,72 @@ import org.apache.log4j.Logger; import org.openslx.bwlp.thrift.iface.Virtualizer; import org.openslx.thrifthelper.TConst; -import java.io.*; +import java.io.BufferedInputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; import java.util.List; public class DockerMetaDataDummy extends VmMetaData { - // TODO Define DOCKER CONSTANT - private static final Logger LOGGER = Logger.getLogger( DockerMetaDataDummy.class); + private static final Logger LOGGER = Logger.getLogger(DockerMetaDataDummy.class); - private final Virtualizer virtualizer = new Virtualizer( TConst.VIRT_DOCKER, "Docker" ); + private final Virtualizer virtualizer = new Virtualizer(TConst.VIRT_DOCKER, "Docker"); - /* this field is in vm context the machine description - e.g. vmware = vmx. - This field will be stored in table imageversion.virtualizerconfig - */ - private byte[] dockerfile; + /** + * containerDefinition is a serialized tar.gz archive and represents a + * ContainerDefinition. This archive contains a serialized Container Recipe (e.g. Dockerfile) + * and a ContainerMeta witch is serialized as a json file. + *

+ * See ContainerDefintion in tutor-module (bwsuite). + *

+ * This field is in vm context the machine description e.g. vmware = vmx. + * This field will be stored in table imageversion.virtualizerconfig + */ + private byte[] containerDefinition; - public DockerMetaDataDummy(List osList, File file) { + public DockerMetaDataDummy(List osList, File file) throws UnsupportedVirtualizerFormatException { super(osList); try { - BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file)); - dockerfile = new byte[(int) file.length()]; - bis.read(dockerfile); + BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file)); + containerDefinition = new byte[(int) file.length()]; + bis.read(containerDefinition); + + checkIsTarGz(); } catch (IOException e) { - LOGGER.error("Couldn't read dockerfile",e); + LOGGER.error("Couldn't read dockerfile", e); } } - public DockerMetaDataDummy(List osList, byte[] vmContent, int length) { + public DockerMetaDataDummy(List osList, byte[] vmContent, int length) + throws UnsupportedVirtualizerFormatException { super(osList); - dockerfile = vmContent; + containerDefinition = vmContent; + + checkIsTarGz(); + } + + /* + TODO This is just a simple check to prevent the workflow from considering any content as acceptable. + */ + /** + * Checks if the first two bytes of the content identifies a tar.gz archive. + * The first byte is 31 == 0x1f, the second byte has to be -117 == 0x8b. + * + * @throws UnsupportedVirtualizerFormatException + */ + private void checkIsTarGz() throws UnsupportedVirtualizerFormatException { + if (!((31 == containerDefinition[0]) && (-117 == containerDefinition[1]))) { + LOGGER.warn("Not Supported Content."); + throw new UnsupportedVirtualizerFormatException( + "DockerMetaDataDummy: Not tar.gz encoded content!"); + } } @Override public byte[] getFilteredDefinitionArray() { - return dockerfile; + return containerDefinition; } @Override public void applySettingsForLocalEdit() { diff --git a/src/main/java/org/openslx/util/vm/VmMetaData.java b/src/main/java/org/openslx/util/vm/VmMetaData.java index c836697..dca9e27 100644 --- a/src/main/java/org/openslx/util/vm/VmMetaData.java +++ b/src/main/java/org/openslx/util/vm/VmMetaData.java @@ -105,16 +105,16 @@ public abstract class VmMetaData this.displayName = dName; } } - + public static enum UsbSpeed { NONE( "None" ), USB1_1( "USB 1.1" ), USB2_0( "USB 2.0" ), USB3_0( "USB 3.0" ); - + public final String displayName; - + private UsbSpeed( String dName ) { this.displayName = dName; @@ -254,7 +254,7 @@ public abstract class VmMetaData /** * Called from subclass to set the OS. If the OS cannot be determined from the * given parameters, it will not be set. - * + * * @param virtId * virtualizer, eg "vmware" for VMware * @param virtOsId @@ -292,7 +292,7 @@ public abstract class VmMetaData * * @param osList List of supported operating systems * @param file VM's machine description file to get the metadata instance from - * @return VmMetaData object representing the relevant parts of the given machine description + * @return VmMetaData object representing the relevant parts of the given machine description */ public static VmMetaData getInstance( List osList, File file ) throws IOException @@ -313,11 +313,9 @@ public abstract class VmMetaData LOGGER.info( "Not a QEmu file", e ); } try { - // TODO This will work for each file because simple read as byte array - // TODO No checks if file is a dockerfile --- THIS SHOOULD NOT BE IN PRODUCTION return new DockerMetaDataDummy(osList, file); } catch ( Exception e ) { - LOGGER.info( "Not a docker file", e ); + LOGGER.info( "Not a tar.gz file, for docker container", e ); } LOGGER.error( "Could not detect any known virtualizer format" ); return null; @@ -346,12 +344,9 @@ public abstract class VmMetaData exceptions.put( "Not a VirtualBox file", e ); } try { - // TODO This should work in each case, because no checks if vmContent is dockerfile - // TODO --- THIS SHOULD NOT BE IN PRODUCTION - LOGGER.info("Creating DockerMetaDataDummy from vmContent"); return new DockerMetaDataDummy(osList, vmContent, length); - } catch (Exception e) { - e.printStackTrace(); + } catch (UnsupportedVirtualizerFormatException e) { + exceptions.put( "Not tar.gz file for DockerMetaDataDummy ", e); } // TODO QEmu -- hack above expects qcow2 file, so we can't do anything here yet LOGGER.error( "Could not detect any known virtualizer format" ); -- cgit v1.2.3-55-g7522