From 2040519f981f7b734030877d123f9bc484f02bef Mon Sep 17 00:00:00 2001 From: Georgios Andrianakis Date: Fri, 26 Mar 2021 08:51:19 +0200 Subject: [PATCH] Perform absolute basic Dockerfile validation in Openshift s2i extension --- ...ApplyDockerfileToBuildConfigDecorator.java | 28 +++++++++++++++++-- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/extensions/container-image/container-image-openshift/deployment/src/main/java/io/quarkus/container/image/openshift/deployment/ApplyDockerfileToBuildConfigDecorator.java b/extensions/container-image/container-image-openshift/deployment/src/main/java/io/quarkus/container/image/openshift/deployment/ApplyDockerfileToBuildConfigDecorator.java index 04425d59b02b4..72a9b96b47a6e 100644 --- a/extensions/container-image/container-image-openshift/deployment/src/main/java/io/quarkus/container/image/openshift/deployment/ApplyDockerfileToBuildConfigDecorator.java +++ b/extensions/container-image/container-image-openshift/deployment/src/main/java/io/quarkus/container/image/openshift/deployment/ApplyDockerfileToBuildConfigDecorator.java @@ -1,10 +1,14 @@ package io.quarkus.container.image.openshift.deployment; +import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; +import java.nio.file.Files; import java.nio.file.Path; +import java.util.Optional; +import java.util.stream.Stream; import io.dekorate.kubernetes.decorator.NamedResourceDecorator; import io.fabric8.kubernetes.api.model.ObjectMeta; @@ -17,15 +21,33 @@ public class ApplyDockerfileToBuildConfigDecorator extends NamedResourceDecorato public ApplyDockerfileToBuildConfigDecorator(String name, Path pathToDockerfile) { super(name); - if (!pathToDockerfile.toFile().exists()) { + validate(pathToDockerfile); + this.pathToDockerfile = pathToDockerfile; + } + + private void validate(Path pathToDockerfile) { + File file = pathToDockerfile.toFile(); + if (!file.exists()) { throw new IllegalArgumentException( "Specified Dockerfile: '" + pathToDockerfile.toAbsolutePath().toString() + "' does not exist."); } - if (!pathToDockerfile.toFile().isFile()) { + if (!file.isFile()) { throw new IllegalArgumentException( "Specified Dockerfile: '" + pathToDockerfile.toAbsolutePath().toString() + "' is not a normal file."); } - this.pathToDockerfile = pathToDockerfile; + + try { + Stream lines = Files.lines(pathToDockerfile); + Optional fromLine = lines.filter(l -> !l.startsWith("#")).map(String::trim) + .filter(l -> l.startsWith("FROM")).findFirst(); + if (!fromLine.isPresent()) { + throw new IllegalArgumentException("Specified Dockerfile: '" + pathToDockerfile.toAbsolutePath().toString() + + "' does not contain a FROM directive"); + } + } catch (IOException e) { + throw new IllegalArgumentException( + "Unable to validate specified Dockerfile: '" + pathToDockerfile.toAbsolutePath().toString() + "'"); + } } @Override