From 56fdc4868aaf70e7bf13b82c1acde219138f8a40 Mon Sep 17 00:00:00 2001 From: Pranav Gaikwad Date: Tue, 11 Jun 2024 15:45:27 -0400 Subject: [PATCH] :sparkles: add ability to pass maven coordinate in location Signed-off-by: Pranav Gaikwad --- demo-dep-output.yaml | 9 +++++ demo-output.yaml | 3 +- docs/providers.md | 2 +- .../pkg/java_external_provider/provider.go | 37 +++++++++++++++++++ provider_container_settings.json | 10 +++++ provider_local_external_images.json | 10 +++++ provider_pod_local_settings.json | 10 +++++ rule-example.yaml | 9 +++++ 8 files changed, 88 insertions(+), 2 deletions(-) diff --git a/demo-dep-output.yaml b/demo-dep-output.yaml index 656f10dd..742721ea 100644 --- a/demo-dep-output.yaml +++ b/demo-dep-output.yaml @@ -2473,3 +2473,12 @@ - konveyor.io/dep-source=open-source - konveyor.io/language=java prefix: file:///root/.m2/repository/org/yaml/snakeyaml/1.30 +- fileURI: io/javaoperatorsdk/operator/sample + provider: java + dependencies: + - name: io.javaoperatorsdk.operator.sample + version: 0.0.0 + labels: + - konveyor.io/dep-source=open-source + - konveyor.io/language=java + prefix: file://java-project/src/main/io/javaoperatorsdk/operator/sample diff --git a/demo-output.yaml b/demo-output.yaml index 80c1a0db..28d755f6 100644 --- a/demo-output.yaml +++ b/demo-output.yaml @@ -3,6 +3,7 @@ - Backend=Golang - Infra=Kubernetes - Java + - Java Operator SDK - Language=Golang - License=Apache violations: @@ -907,7 +908,7 @@ errors: error-rule-001: |- unable to get query info: yaml: unmarshal errors: - line 9: cannot unmarshal !!map into string + line 10: cannot unmarshal !!map into string unmatched: - file-002 - lang-ref-002 diff --git a/docs/providers.md b/docs/providers.md index 96e58b5c..3aa3eb8e 100644 --- a/docs/providers.md +++ b/docs/providers.md @@ -92,7 +92,7 @@ Here's an example config for `java` provider that is currently in-tree and does } ``` -The `location` can be a path to the application's source code or to a binary JAR, WAR, or EAR file. +The `location` can be a path to the application's source code or to a binary JAR, WAR, or EAR file. Optionally, coordinates to a maven artifact can be provided as input in the format `mvn://.:::`. The field `` is optional, it specifies a local path where the artifact will be downloaded. If not specified, provider will use a temporary directory to download it. The `java` provider also takes following options in `providerSpecificConfig`: diff --git a/external-providers/java-external-provider/pkg/java_external_provider/provider.go b/external-providers/java-external-provider/pkg/java_external_provider/provider.go index b6bbe975..520e77e9 100644 --- a/external-providers/java-external-provider/pkg/java_external_provider/provider.go +++ b/external-providers/java-external-provider/pkg/java_external_provider/provider.go @@ -31,6 +31,7 @@ const ( WebArchive = ".war" EnterpriseArchive = ".ear" ClassFile = ".class" + MvnURIPrefix = "mvn://" ) // provider specific config keys @@ -220,6 +221,42 @@ func (p *javaProvider) Init(ctx context.Context, log logr.Logger, config provide var returnErr error // each service client should have their own context ctx, cancelFunc := context.WithCancel(ctx) + // location can be a coordinate to a remote mvn artifact + if strings.HasPrefix(config.Location, MvnURIPrefix) { + mvnUri := strings.Replace(config.Location, MvnURIPrefix, "", 1) + // URI format is :::: + // is optional & points to a local path where it will be downloaded + mvnUriParts := strings.Split(mvnUri, ":") + if len(mvnUriParts) < 4 || len(mvnUriParts) > 6 { + cancelFunc() + return nil, fmt.Errorf("invalid maven coordinates in location %s", config.Location) + } + outputDir := "." + if len(mvnUriParts) == 5 { + if stat, err := os.Stat(mvnUriParts[len(mvnUriParts)-1]); err != nil || !stat.IsDir() { + cancelFunc() + return nil, fmt.Errorf("output path does not exist or not a directory") + } + outputDir = mvnUriParts[len(mvnUriParts)-1] + } + mvnOptions := []string{ + "dependency:copy", + fmt.Sprintf("-Dartifact=%s", strings.Join(mvnUriParts[:4], ":")), + fmt.Sprintf("-DoutputDirectory=%s", outputDir), + } + if mavenSettingsFile != "" { + mvnOptions = append(mvnOptions, "-s", mavenSettingsFile) + } + cmd := exec.CommandContext(ctx, "mvn", mvnOptions...) + cmd.Dir = outputDir + if err := cmd.Run(); err != nil { + cancelFunc() + return nil, fmt.Errorf("error downloading java artifact - %s", mvnUri) + } + config.Location = filepath.Join(outputDir, + fmt.Sprintf("%s.%s", strings.Join(mvnUriParts[1:3], "-"), strings.ToLower(mvnUriParts[3]))) + } + extension := strings.ToLower(path.Ext(config.Location)) switch extension { case JavaArchive, WebArchive, EnterpriseArchive: diff --git a/provider_container_settings.json b/provider_container_settings.json index 56c63780..f6d842bf 100644 --- a/provider_container_settings.json +++ b/provider_container_settings.json @@ -111,6 +111,16 @@ "bundles": "/jdtls/java-analyzer-bundle/java-analyzer-bundle.core/target/java-analyzer-bundle.core-1.0.0-SNAPSHOT.jar" }, "analysisMode": "source-only" + }, + { + "location": "mvn://io.javaoperatorsdk:quarkus:1.6.2:jar", + "providerSpecificConfig": { + "lspServerName": "java", + "lspServerPath": "/jdtls/bin/jdtls", + "depOpenSourceLabelsFile": "/usr/local/etc/maven.default.index", + "bundles": "/jdtls/java-analyzer-bundle/java-analyzer-bundle.core/target/java-analyzer-bundle.core-1.0.0-SNAPSHOT.jar" + }, + "analysisMode": "full" } ] }, diff --git a/provider_local_external_images.json b/provider_local_external_images.json index 92a5129c..ad665500 100644 --- a/provider_local_external_images.json +++ b/provider_local_external_images.json @@ -111,6 +111,16 @@ "bundles": "/jdtls/java-analyzer-bundle/java-analyzer-bundle.core/target/java-analyzer-bundle.core-1.0.0-SNAPSHOT.jar" }, "analysisMode": "source-only" + }, + { + "location": "mvn://io.javaoperatorsdk:quarkus:1.6.2:jar", + "providerSpecificConfig": { + "lspServerName": "java", + "lspServerPath": "/jdtls/bin/jdtls", + "depOpenSourceLabelsFile": "/usr/local/etc/maven.default.index", + "bundles": "/jdtls/java-analyzer-bundle/java-analyzer-bundle.core/target/java-analyzer-bundle.core-1.0.0-SNAPSHOT.jar" + }, + "analysisMode": "full" } ] }, diff --git a/provider_pod_local_settings.json b/provider_pod_local_settings.json index 141257da..8e591d09 100644 --- a/provider_pod_local_settings.json +++ b/provider_pod_local_settings.json @@ -111,6 +111,16 @@ "bundles": "/jdtls/java-analyzer-bundle/java-analyzer-bundle.core/target/java-analyzer-bundle.core-1.0.0-SNAPSHOT.jar" }, "analysisMode": "source-only" + }, + { + "location": "mvn://io.javaoperatorsdk:quarkus:1.6.2:jar", + "providerSpecificConfig": { + "lspServerName": "java", + "lspServerPath": "/jdtls/bin/jdtls", + "depOpenSourceLabelsFile": "/usr/local/etc/maven.default.index", + "bundles": "/jdtls/java-analyzer-bundle/java-analyzer-bundle.core/target/java-analyzer-bundle.core-1.0.0-SNAPSHOT.jar" + }, + "analysisMode": "full" } ] }, diff --git a/rule-example.yaml b/rule-example.yaml index 929d10ff..3b07619a 100644 --- a/rule-example.yaml +++ b/rule-example.yaml @@ -300,3 +300,12 @@ when: java.referenced: pattern: com.sun.net.httpserver.HttpExchange +- category: mandatory + description: | + This rule tests the application downloaded from maven artifact + tag: + - Java Operator SDK + ruleID: java-downloaded-maven-artifact + when: + java.referenced: + pattern: io.javaoperatorsdk.operator.Operator \ No newline at end of file