Alwaysgaurav1 commented on code in PR #2839:
URL: https://github.com/apache/karaf/pull/2839#discussion_r3996988255


##########
tooling/karaf-maven-plugin/src/main/java/org/apache/karaf/tooling/DockerfileMojo.java:
##########
@@ -31,29 +31,46 @@
 @Mojo(name = "dockerfile", defaultPhase = LifecyclePhase.PACKAGE)
 public class DockerfileMojo extends MojoSupport {
 
+    private static final String DEFAULT_IMAGE = "eclipse-temurin:11-jre";
+    private static final String DEFAULT_COMMAND = "[\"karaf\", \"run\"]";
+
     @Parameter(defaultValue = "${project.build.directory}")
     private File destDir;
 
     @Parameter(defaultValue = "${project.build.directory}/assembly")
     private File assembly;
 
-    @Parameter(defaultValue = "[\"karaf\", \"run\"]")
+    @Parameter(defaultValue = DEFAULT_COMMAND, property = "command")
     private String command;
 
+    @Parameter(defaultValue = DEFAULT_IMAGE, property = "image")
+    private String image;
+
     @Override
     public void execute() throws MojoExecutionException, MojoFailureException {
         getLog().info("Creating Dockerfile");
+
+        String baseImage = (image == null || image.trim().isEmpty()) ? 
DEFAULT_IMAGE : image.trim();
+        String cmd = (command == null || command.trim().isEmpty()) ? 
DEFAULT_COMMAND : command.trim();
+
+        if (baseImage.contains("\n") || baseImage.contains("\r")) {
+            throw new MojoExecutionException("Invalid image: base image cannot 
contain newline characters");
+        }
+        if (cmd.contains("\n") || cmd.contains("\r")) {
+            throw new MojoExecutionException("Invalid command: command cannot 
contain newline characters");
+        }
+
         File dockerFile = new File(destDir, "Dockerfile");
         try {
             StringBuilder buffer = new StringBuilder();
-            buffer.append("FROM eclipse-temurin:11-jre").append("\n");
+            buffer.append("FROM ").append(baseImage).append("\n");
             buffer.append("ENV KARAF_INSTALL_PATH /opt").append("\n");
             buffer.append("ENV KARAF_HOME 
$KARAF_INSTALL_PATH/apache-karaf").append("\n");
             buffer.append("ENV KARAF_EXEC exec").append("\n");
             buffer.append("ENV PATH $PATH:$KARAF_HOME/bin").append("\n");
             buffer.append("COPY ").append(assembly.getName()).append(" 
$KARAF_HOME").append("\n");

Review Comment:
   Hi @jbonofre, thanks for the review!
   
   Regarding the validation: the newline checks were added specifically to 
prevent multi-line Dockerfile directive injection while retaining full 
flexibility for valid single-line Docker options (such as --platform=... on 
FROM and shell-form on CMD).
   
   Since you mentioned you are comfortable merging as-is, that works great for 
me! Thank you for reviewing and guiding this PR.
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to