Skip to content

Commit

Permalink
Merge pull request #7 from blueoly/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
blueoly authored Jan 21, 2020
2 parents a2f26ad + f1b6764 commit e55d3ad
Show file tree
Hide file tree
Showing 20 changed files with 496 additions and 63 deletions.
20 changes: 18 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,23 @@
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>10</maven.compiler.source>
<maven.compiler.target>10</maven.compiler.target>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>gr.aueb.balab.jadolint.Jadolint</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
<finalName>jadolint</finalName>
</build>
</project>
206 changes: 206 additions & 0 deletions src/main/java/gr/aueb/balab/jadolint/Jadolint.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,216 @@
*/
package gr.aueb.balab.jadolint;

import gr.aueb.balab.jadolint.dependencies.Dependency;
import gr.aueb.balab.jadolint.dependencies.DependencyDetector;
import gr.aueb.balab.jadolint.model.Add;
import gr.aueb.balab.jadolint.model.Cmd;
import gr.aueb.balab.jadolint.model.Copy;
import gr.aueb.balab.jadolint.model.Dockerfile;
import gr.aueb.balab.jadolint.model.Expose;
import gr.aueb.balab.jadolint.model.From;
import gr.aueb.balab.jadolint.model.Line;
import gr.aueb.balab.jadolint.model.Maintainer;
import gr.aueb.balab.jadolint.model.Run;
import gr.aueb.balab.jadolint.model.Workdir;
import gr.aueb.balab.jadolint.rules.AddRules;
import gr.aueb.balab.jadolint.rules.CmdRules;
import gr.aueb.balab.jadolint.rules.CopyRules;
import gr.aueb.balab.jadolint.rules.ExposeRules;
import gr.aueb.balab.jadolint.rules.FromRules;
import gr.aueb.balab.jadolint.rules.MaintainerRules;
import gr.aueb.balab.jadolint.rules.RunRules;
import gr.aueb.balab.jadolint.rules.WorkdirRules;
import gr.aueb.balab.jadolint.violations.Violation;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
*
* @author blue
*/
public class Jadolint {

private Dockerfile doc;

public static void main(String[] args){
try {
if(args.length < 1){
System.out.println("Please provide a Dockerfile");
return;
}

String path = args[0];

LineMerger l = new LineMerger();
Dockerfile doc = new Dockerfile();

doc.setPath(path);

l.mergeLines(doc, new File(path));

List<Violation> violations = new ArrayList<>();

for(Line line : doc.getLines()){
//System.out.println(line.getLine() + " " + line.getLineNumber() + " " + line.getInstruction());
if(line.getInstruction().equals("ADD")){
AddRules addRules = new AddRules(new Add(line.getLine()));
violations = addRules.runAddRules(doc, line.getLineNumber());
}
if(line.getInstruction().equals("CMD")){
CmdRules cmdRules = new CmdRules(new Cmd(line.getLine()));
violations = cmdRules.runCmdRules(doc, line.getLineNumber());
}
if(line.getInstruction().equals("COPY")){
CopyRules copyRules = new CopyRules(new Copy(line.getLine()));
violations = copyRules.runCopyRules(doc, line.getLineNumber());
}
if(line.getInstruction().equals("EXPOSE")){
ExposeRules exposeRules = new ExposeRules(new Expose(line.getLine()));
violations = exposeRules.runExposeRules(doc, line.getLineNumber());
}
if(line.getInstruction().equals("FROM")){
FromRules fromRules = new FromRules(new From(line.getLine()));
violations = fromRules.runFromRules(doc, line.getLineNumber());
}
if(line.getInstruction().equals("MAINTAINER")){
MaintainerRules maintainerRules = new MaintainerRules(new Maintainer(line.getLine()));
violations = maintainerRules.runMaintainerRules(doc, line.getLineNumber());
}
if(line.getInstruction().equals("RUN")){
RunRules runRules = new RunRules(new Run(line.getLine()));
violations = runRules.runRunRules(doc, line.getLineNumber());
}
if(line.getInstruction().equals("WORKDIR")){
WorkdirRules workdirRules = new WorkdirRules(new Workdir(line.getLine()));
violations = workdirRules.runWorkdirRules(doc, line.getLineNumber());
}
if(!violations.isEmpty())
doc.addViolations(violations);
}

getDependencies2(path, doc);

for(Violation v : doc.getViolations())
System.out.println(v.getFileName() + " " + v.getLineNumber() + " " + v.getCode() + " " + v.getMessage());

System.out.println("-----");

for(Dependency d : doc.getDependencies()){
System.out.print(d.getPackageName().trim());
if(d.getPackageVersion() != null)
System.out.println(" " + d.getPackageVersion());
else
System.out.println();
}
} catch (IOException ex) {
Logger.getLogger(Jadolint.class.getName()).log(Level.SEVERE, null, ex);
}

}

public void run(String path){
try {

LineMerger l = new LineMerger();
doc = new Dockerfile();

doc.setPath(path);

l.mergeLines(doc, new File(path));

List<Violation> violations = new ArrayList<>();

for(Line line : doc.getLines()){
//System.out.println(line.getLine() + " " + line.getLineNumber() + " " + line.getInstruction());
if(line.getInstruction().equals("ADD")){
AddRules addRules = new AddRules(new Add(line.getLine()));
violations = addRules.runAddRules(doc, line.getLineNumber());
}
if(line.getInstruction().equals("CMD")){
CmdRules cmdRules = new CmdRules(new Cmd(line.getLine()));
violations = cmdRules.runCmdRules(doc, line.getLineNumber());
}
if(line.getInstruction().equals("COPY")){
CopyRules copyRules = new CopyRules(new Copy(line.getLine()));
violations = copyRules.runCopyRules(doc, line.getLineNumber());
}
if(line.getInstruction().equals("EXPOSE")){
ExposeRules exposeRules = new ExposeRules(new Expose(line.getLine()));
violations = exposeRules.runExposeRules(doc, line.getLineNumber());
}
if(line.getInstruction().equals("FROM")){
FromRules fromRules = new FromRules(new From(line.getLine()));
violations = fromRules.runFromRules(doc, line.getLineNumber());
}
if(line.getInstruction().equals("MAINTAINER")){
MaintainerRules maintainerRules = new MaintainerRules(new Maintainer(line.getLine()));
violations = maintainerRules.runMaintainerRules(doc, line.getLineNumber());
}
if(line.getInstruction().equals("RUN")){
RunRules runRules = new RunRules(new Run(line.getLine()));
violations = runRules.runRunRules(doc, line.getLineNumber());
}
if(line.getInstruction().equals("WORKDIR")){
WorkdirRules workdirRules = new WorkdirRules(new Workdir(line.getLine()));
violations = workdirRules.runWorkdirRules(doc, line.getLineNumber());
}
if(!violations.isEmpty())
doc.addViolations(violations);
}

/*for(Violation v : doc.getViolations())
System.out.println(v.getFileName() + " " + v.getLineNumber() + " " + v.getCode() + " " + v.getMessage());*/

} catch (IOException ex) {
Logger.getLogger(Jadolint.class.getName()).log(Level.SEVERE, null, ex);
}

}

public void getDependencies(String path) {
try {
LineMerger l = new LineMerger();
doc = new Dockerfile();

doc.setPath(path);

l.mergeLines(doc, new File(path));

DependencyDetector detector = new DependencyDetector();

for(Line line : doc.getLines()){
detector.detectDependency(line.getLine());
}

doc.addDependencies(detector.getDependencies());
} catch (IOException ex) {
Logger.getLogger(Jadolint.class.getName()).log(Level.SEVERE, null, ex);
}
}

private static void getDependencies2(String path, Dockerfile doc) {

DependencyDetector detector = new DependencyDetector();

for(Line line : doc.getLines()){
detector.detectDependency(line.getLine());
}

doc.addDependencies(detector.getDependencies());

}

public Dockerfile getDoc() {
return doc;
}

public void setDoc(Dockerfile doc) {
this.doc = doc;
}

}
2 changes: 2 additions & 0 deletions src/main/java/gr/aueb/balab/jadolint/LineMerger.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public void mergeLines(Dockerfile doc, File dockerFile) throws IOException {

while ((line = reader.readLine()) != null) {
lineCounter++;
if(line.trim().startsWith("#"))
continue;
if (hasInstruction(line) && line.endsWith(" \\")) {
//newLine = "";
newLine += line;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,60 +1,68 @@
package gr.aueb.balab.jadolint.dependencies;

import gr.aueb.balab.jadolint.model.From;
import java.util.ArrayList;
import java.util.List;

import gr.aueb.balab.jadolint.model.Run;
import gr.aueb.balab.jadolint.model.RunBlock;
import gr.aueb.balab.jadolint.util.JadolintUtils;

public class DependencyDetector {

private List<Dependency> dependencies = new ArrayList<Dependency>();

public DependencyDetector() {

}

public void detectDependency(String line) {
String[] splitLine = line.split(" ", 2);

String instruction = splitLine[0];
String instruction = JadolintUtils.getInstruction(line);

switch(instruction) {
case "FROM":
checkForFromDependency(splitLine[1]);
checkForFromDependency(line);
break;
case "RUN":
checkForRunDependency(line);
break;
}
}

private void checkForFromDependency(String lineWithoutInstruction) {
String packageName;
String packageVersion = null;
if (lineWithoutInstruction.contains(":")) {
packageName = lineWithoutInstruction.split(":", 2)[0];
packageVersion = lineWithoutInstruction.split(":", 2)[1];
} else {
packageName = lineWithoutInstruction;
}

dependencies.add(new FromDependency(packageName, packageVersion));
private void checkForFromDependency(String line) {
From from = new From(line);

String packageName = from.getImage();

if(from.getTag() != null)
dependencies.add(new FromDependency(packageName, from.getTag()));
else
dependencies.add(new FromDependency(packageName, null));
}

private void checkForRunDependency(String line) {
List<RunBlock> runBlocksInstall = new Run(line).getAptGetInstallBlocks();

for (RunBlock runblock : runBlocksInstall) {
String[] splitLine = runblock.getParams().split(" ");
String packageInfo = splitLine[splitLine.length - 1];

if (packageInfo.contains("=")) {
String name = packageInfo.split("=", 2)[0];
String version = packageInfo.split("=", 2)[1];
dependencies.add(new RunDependency(name, version));
//String packageInfo = splitLine[splitLine.length - 1];

for(String s : splitLine) {

if(s.contains("$")){

}

if(!s.equals("install") && !s.startsWith("-")) {
if (s.contains("=")) {
String name = s.split("=", 2)[0];
String version = s.split("=", 2)[1];
dependencies.add(new RunDependency(name, version));
} else{
dependencies.add(new RunDependency(s, null));
}
}
}
}
}

public List<Dependency> getDependencies() {
return dependencies;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package gr.aueb.balab.jadolint.dependencies;

public class FromDependency extends Dependency {
private String packageName, packageVersion;

public FromDependency(String packageName, String packageVersion) {
super(packageName, packageVersion);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

public class RunDependency extends Dependency {

private String packageName, packageVersion;

public RunDependency(String packageName, String packageVersion) {
super(packageName, packageVersion);
}
Expand Down
12 changes: 8 additions & 4 deletions src/main/java/gr/aueb/balab/jadolint/model/Cmd.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,14 @@ public Cmd(String line){
blockBody = blockBody + " " + s.substring(1, s.length() - 1);
}

String[] splitLine = blockBody.trim().split(" ", 2);

executable = splitLine[0].trim();
params = splitLine[1].trim();
if(blockBody.trim().contains(" ")) {
String[] splitLine = blockBody.trim().split(" ", 2);

executable = splitLine[0].trim();
params = splitLine[1].trim();
} else {
executable = blockBody.trim();
}

execForm = true;

Expand Down
Loading

0 comments on commit e55d3ad

Please sign in to comment.