Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Perform absolute basic Dockerfile validation in Openshift s2i extension #16036

Merged
merged 1 commit into from Mar 26, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -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