From 72e3b0f0b37b81d6ab910fd97efc7dd1673583e7 Mon Sep 17 00:00:00 2001 From: Jonathan Christison Date: Sat, 1 Jul 2023 02:56:23 +0100 Subject: [PATCH] Start adding picocli as way of verifying CLI args --- pom.xml | 9 ++-- .../com/redhat/pctsec/model/BrewBuild.java | 7 +++ .../com/redhat/pctsec/model/BuildType.java | 1 + .../java/com/redhat/pctsec/model/Git.java | 13 +++++ .../com/redhat/pctsec/model/PNCBuild.java | 11 +++++ .../com/redhat/pctsec/model/ScanRequest.java | 49 ++++++++++--------- .../com/redhat/pctsec/model/ScanRequests.java | 28 ++++++++--- .../redhat/pctsec/model/api/request/git.java | 6 ++- .../redhat/pctsec/model/osh/paramMapper.java | 27 ++++++++++ .../pctsec/rest/v1alpha1/ScanResource.java | 8 +-- .../pctsec/model/osh/paramMapperTest.java | 21 ++++++++ 11 files changed, 138 insertions(+), 42 deletions(-) create mode 100644 src/main/java/com/redhat/pctsec/model/Git.java create mode 100644 src/test/java/com/redhat/pctsec/model/osh/paramMapperTest.java diff --git a/pom.xml b/pom.xml index 7f0e9a2..0d831fd 100644 --- a/pom.xml +++ b/pom.xml @@ -74,6 +74,10 @@ io.quarkus quarkus-smallrye-openapi + + io.quarkus + quarkus-picocli + io.quarkus quarkus-junit5 @@ -84,11 +88,6 @@ rest-assured test - - info.picocli - picocli - 4.7.3 - diff --git a/src/main/java/com/redhat/pctsec/model/BrewBuild.java b/src/main/java/com/redhat/pctsec/model/BrewBuild.java index fafbe33..a8c6ec0 100644 --- a/src/main/java/com/redhat/pctsec/model/BrewBuild.java +++ b/src/main/java/com/redhat/pctsec/model/BrewBuild.java @@ -18,4 +18,11 @@ public class BrewBuild extends BuildType { public URL URL() { return null; } + + @Override + public String revision() { + return null; + } + + } diff --git a/src/main/java/com/redhat/pctsec/model/BuildType.java b/src/main/java/com/redhat/pctsec/model/BuildType.java index 64d04f1..25d738d 100644 --- a/src/main/java/com/redhat/pctsec/model/BuildType.java +++ b/src/main/java/com/redhat/pctsec/model/BuildType.java @@ -25,4 +25,5 @@ abstract public class BuildType { abstract public URI SCMURL(void); abstract public URL URL(void); + abstract public String revision(void); } diff --git a/src/main/java/com/redhat/pctsec/model/Git.java b/src/main/java/com/redhat/pctsec/model/Git.java new file mode 100644 index 0000000..15eaddd --- /dev/null +++ b/src/main/java/com/redhat/pctsec/model/Git.java @@ -0,0 +1,13 @@ +package com.redhat.pctsec.model; + +import java.net.URI; + +public class Git { + private URI repo; + private String ref; + + public Git(String repo, String ref) { + this.repo = URI.create(repo); + this.ref = ref; + } +} diff --git a/src/main/java/com/redhat/pctsec/model/PNCBuild.java b/src/main/java/com/redhat/pctsec/model/PNCBuild.java index 42eebfd..9cc9c6c 100644 --- a/src/main/java/com/redhat/pctsec/model/PNCBuild.java +++ b/src/main/java/com/redhat/pctsec/model/PNCBuild.java @@ -17,4 +17,15 @@ public class PNCBuild extends BuildType{ public URL URL() { return null; } + + @Override + public String revision() { + return null; + } + + public static boolean isValidRef(String ref){ + //New type PNC Ref + if(ref.length()!=14) + return false; + } } diff --git a/src/main/java/com/redhat/pctsec/model/ScanRequest.java b/src/main/java/com/redhat/pctsec/model/ScanRequest.java index bf67e66..1a51340 100644 --- a/src/main/java/com/redhat/pctsec/model/ScanRequest.java +++ b/src/main/java/com/redhat/pctsec/model/ScanRequest.java @@ -1,52 +1,57 @@ package com.redhat.pctsec.model; +import com.redhat.pctsec.model.api.request.git; import com.redhat.pctsec.tekton.brewTaskRun; import com.redhat.pctsec.tekton.scmUrlPipelineRun; import io.vertx.mutiny.core.eventbus.EventBus; import jakarta.enterprise.context.ApplicationScoped; import jakarta.inject.Inject; import jakarta.inject.Singleton; +import jakarta.persistence.Entity; import java.util.HashMap; + +enum RequestType{BREW, PNC, GIT} @ApplicationScoped -abstract public class ScanRequest { +@Entity +public class ScanRequest { @Inject EventBus bus; - private HashMap metadata; - private HashMap oshScanOptions; + private String metadata; + private String oshScanOptions; -<<<<<<< HEAD - //@Inject - brewTaskRun btr; + RequestType type; + BrewBuild brewBuild; + PNCBuild pncBuild; + Git git; - //@Inject - scmUrlPipelineRun plr; + public ScanRequest(BrewBuild brewBuild) + { + this.type = RequestType.BREW; + this.brewBuild = brewBuild; + } + public ScanRequest(PNCBuild pncBuild) + { + this.type = RequestType.PNC; + this.pncBuild = pncBuild; + } - public ScanRequest(String brewBuildId) + public ScanRequest(Git git) { - this.brewBuild = brewBuildId; + this.type = RequestType.GIT; + this.git = git; } public ScanRequest(String repo, String ref) { - this.scmurl = new HashMap<>(); - this.scmurl.put("repo", repo); - this.scmurl.put("ref", ref); + this.git = new Git(repo, ref); } public void executeScan(){ - if(this.brewBuild != null && !this.brewBuild.trim().isEmpty()){ - btr = new brewTaskRun(); - btr.invokeScanTask(this.brewBuild); - } - else if (this.scmurl != null && !this.scmurl.isEmpty()) - { - plr = new scmUrlPipelineRun(); - plr.invokeOshScmScanPipeline(this.scmurl.get("repo"), this.scmurl.get("ref")); - } + //Drop self on event bus for tekton handler } } diff --git a/src/main/java/com/redhat/pctsec/model/ScanRequests.java b/src/main/java/com/redhat/pctsec/model/ScanRequests.java index e4dc2f6..359c6ad 100644 --- a/src/main/java/com/redhat/pctsec/model/ScanRequests.java +++ b/src/main/java/com/redhat/pctsec/model/ScanRequests.java @@ -1,6 +1,7 @@ package com.redhat.pctsec.model; import com.redhat.pctsec.model.api.request.pssaas; +import com.redhat.pctsec.model.api.request.scanChain; import jakarta.enterprise.context.ApplicationScoped; import java.util.HashMap; @@ -24,6 +25,9 @@ public class ScanRequests { @Column(name="scan_properties") private String scanProperties; + @Column(name="scan_metadata") + private String scanMetadata; + public ScanRequests(){ //Default to the Snyk scan @@ -31,26 +35,38 @@ public class ScanRequests { } public ScanRequests(pssaas pssaas){ + this(); //Iterate scan payload and create scans } - public ScanRequests(String repo, String rev){ - //shortcut for single scans - scanRequests.add(new ScanRequest(repo, rev)); + public ScanRequests(scanChain scanchain){ + this(); } + //public ScanRequests(String repo, String rev){ + // //shortcut for single scans + // scanRequests.add(new ScanRequest(repo, rev)); + //} + + /* public ScanRequests(String brewBuildId){ - scanRequests.add(new ScanRequest(brewBuildId)); + scanRequests.add(new ScanRequest(new B)); } + */ public void addBrewBuild(String brewBuildId) { - scanRequests.add(new ScanRequest(brewBuildId)); + scanRequests.add(new ScanRequest(new BrewBuild(brewBuildId))); } public void addGit(String repo, String rev) { - scanRequests.add(new ScanRequest(repo, rev)); + scanRequests.add(new ScanRequest(new Git(repo, rev))); + } + + public void addPNCBuild(String pncBuildId) + { + scanRequests.add(new ScanRequest(new PNCBuild(pncBuildId))); } //Create tekton pipeline/taskrun diff --git a/src/main/java/com/redhat/pctsec/model/api/request/git.java b/src/main/java/com/redhat/pctsec/model/api/request/git.java index 203327a..a9c4beb 100644 --- a/src/main/java/com/redhat/pctsec/model/api/request/git.java +++ b/src/main/java/com/redhat/pctsec/model/api/request/git.java @@ -5,6 +5,8 @@ import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonPropertyOrder; import jakarta.validation.constraints.NotNull; +import java.net.URI; + @JsonInclude(JsonInclude.Include.NON_NULL) @JsonPropertyOrder({ "type", @@ -18,9 +20,9 @@ public class git { @NotNull @JsonProperty("repo") - public String repo; + public URI repo; @NotNull - @JsonProperty("repo") + @JsonProperty("ref") public String ref; } \ No newline at end of file diff --git a/src/main/java/com/redhat/pctsec/model/osh/paramMapper.java b/src/main/java/com/redhat/pctsec/model/osh/paramMapper.java index 4cd1c24..83968eb 100644 --- a/src/main/java/com/redhat/pctsec/model/osh/paramMapper.java +++ b/src/main/java/com/redhat/pctsec/model/osh/paramMapper.java @@ -1,7 +1,34 @@ package com.redhat.pctsec.model.osh; +import picocli.CommandLine; import picocli.CommandLine.Option; import picocli.CommandLine.Parameters; public class paramMapper { + @Option(names = {"-p", "--profile"}, description = "list of analyzers to use (see command 'list-\n" + + " analyzers'); use comma as a separator: e.g. \"\n" + + " --analyzer=gcc,clang,cppcheck\"") + private String profile; + + @Option(names = {"-a", "--analyzer"}, description = "list of analyzers to use (see command 'list-\n" + + " analyzers'); use comma as a separator: e.g. \"\n" + + " --analyzer=gcc,clang,cppcheck\"") + private String analyzers; + + @Option(names = {"--tarball-build-script"}, description = "With this option osh-cli accepts path to\n" + + " tarball specified via first argument and then\n" + + " the tarball will be scanned. This option sets\n" + + " command which should build the package,\n" + + " usually this should be just \"make\", in case\n" + + " of packages which doesn't need to be built,\n" + + " just pass \"true\".\n") + private String tarballBuildScript; + + @Option(names = {"--brew-build"}, description = "use a brew build (specified by NVR) instead\n" + + " of a local file") + private String brewBuild; + + public paramMapper(String params){ + new CommandLine(this).parseArgs("params"); + } } diff --git a/src/main/java/com/redhat/pctsec/rest/v1alpha1/ScanResource.java b/src/main/java/com/redhat/pctsec/rest/v1alpha1/ScanResource.java index 46b292d..09a4a53 100644 --- a/src/main/java/com/redhat/pctsec/rest/v1alpha1/ScanResource.java +++ b/src/main/java/com/redhat/pctsec/rest/v1alpha1/ScanResource.java @@ -14,7 +14,6 @@ import java.util.HashMap; @Path("/api/v1a/Scan") public class ScanResource { - HashMap scanCollection = new HashMap<>(); @POST @Path("PSSaaS") @Consumes({ "application/json" }) @@ -31,13 +30,12 @@ public class ScanResource { @GET @Path("{id}") public Scan scanRequest(String id){ - return scanCollection.get(id.toString()); + return null; } @GET @Path("{id}/run") public String scanRequestExe(String id){ - scanCollection.get(id.toString()).execute(); return "We'd normally have a json payload here, with pipeline UID"; } @@ -45,8 +43,6 @@ public class ScanResource { @Path("single/git") public String singleGit(@RestQuery String repo, @RestQuery String ref) { - ScanRequests sc = new ScanRequests(repo, ref); - scanCollection.put("1",sc); return "restult"; } @@ -54,8 +50,6 @@ public class ScanResource { @Path("single/brew") public String singleGit(@RestQuery String brewId) { - ScanRequests sc = new ScanRequests(brewId); - scanCollection.put("2",sc); return "result"; } } diff --git a/src/test/java/com/redhat/pctsec/model/osh/paramMapperTest.java b/src/test/java/com/redhat/pctsec/model/osh/paramMapperTest.java new file mode 100644 index 0000000..e1616b8 --- /dev/null +++ b/src/test/java/com/redhat/pctsec/model/osh/paramMapperTest.java @@ -0,0 +1,21 @@ +package com.redhat.pctsec.model.osh; + +import io.quarkus.test.junit.QuarkusTest; +import jakarta.inject.Inject; +import org.junit.jupiter.api.Test; + +import static io.restassured.RestAssured.given; +import static org.hamcrest.CoreMatchers.is; + +@QuarkusTest +public class paramMapperTest { + + @Inject + paramMapper pm; + + @Test + public void testSnykScan() { + pm = new paramMapper("-p snyk-only-unstable --tarball-build-script=\":\""); + } + +} \ No newline at end of file