Skip to content

Commit

Permalink
Merge pull request #16036 from geoand/#QUARKUS-911
Browse files Browse the repository at this point in the history
Perform absolute basic Dockerfile validation in Openshift s2i extension
  • Loading branch information
geoand authored Mar 26, 2021
2 parents e6cd8a8 + 2040519 commit 5207b18
Showing 1 changed file with 25 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<String> lines = Files.lines(pathToDockerfile);
Optional<String> 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
Expand Down

0 comments on commit 5207b18

Please sign in to comment.