Skip to content

Commit

Permalink
[8.3] Setup elasticsearch dependency monitoring with Snyk for product…
Browse files Browse the repository at this point in the history
…ion code (#88036) (#88566)

* Setup elasticsearch dependency monitoring with Snyk for production code (#88036)

This adds the generation and upload logic of Gradle dependency graphs to snyk

We directly implemented a rest api based snyk plugin as:

the existing snyk gradle plugin delegates to the snyk command line tool the command line tool
uses custom gradle logic by injecting a init file that is

a) using deprecated build logic which we definitely want to avoid
b) uses gradle api we avoid like eager task creation.

Shipping this as a internal gradle plugin gives us the most flexibility as we only want to monitor
production code for now we apply this plugin as part of the elasticsearch.build plugin,
that usage has been for now the de-facto indicator if a project is considered a "production" project
that ends up in our distribution or public maven repositories. This isnt yet ideal and we will revisit
the distinction between production and non production code / projects in a separate effort.

As part of this effort we added the elasticsearch.build plugin to more projects that actually end up
in the distribution. To unblock us on this we for now disabled a few check tasks that started failing by applying elasticsearch.build.

Addresses  #87620
# Conflicts:
#	test/framework/build.gradle

* Fix license report when backporting
  • Loading branch information
breskeby committed Jul 15, 2022
1 parent 481fe9a commit dedb849
Show file tree
Hide file tree
Showing 27 changed files with 939 additions and 256 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,6 @@ private static String readFirstLine(final Path path) throws IOException {

/** Find the reponame. */
public String urlFromOrigin() {
String oritin = getOrigin();
if (origin == null) {
return null; // best effort, the url doesnt really matter, it is just required by maven central
}
Expand Down
1 change: 1 addition & 0 deletions build-tools-internal/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@ dependencies {
exclude module: "groovy"
}
testImplementation buildLibs.spock.junit4
testImplementation buildLibs.json.assert
integTestImplementation buildLibs.xmlunit.core
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

package org.elasticsearch.gradle.internal.snyk

import org.elasticsearch.gradle.fixtures.AbstractGradleInternalPluginFuncTest
import org.gradle.api.Plugin
import org.gradle.testkit.runner.TaskOutcome
import org.skyscreamer.jsonassert.JSONAssert

import static java.net.HttpURLConnection.HTTP_CREATED
import static java.net.HttpURLConnection.HTTP_INTERNAL_ERROR
import static org.elasticsearch.gradle.fixtures.WiremockFixture.PUT
import static org.elasticsearch.gradle.fixtures.WiremockFixture.withWireMock
import static org.elasticsearch.gradle.internal.snyk.UploadSnykDependenciesGraph.GRADLE_GRAPH_ENDPOINT

class SnykDependencyMonitoringGradlePluginFuncTest extends AbstractGradleInternalPluginFuncTest {

Class<? extends Plugin> pluginClassUnderTest = SnykDependencyMonitoringGradlePlugin.class

def setup() {
configurationCacheCompatible = false // configuration is not cc compliant
}

def "can calculate snyk dependency graph"() {
given:
buildFile << """
apply plugin:'java'
version = "1.0-SNAPSHOT"
repositories {
mavenCentral()
}
dependencies {
implementation 'org.apache.lucene:lucene-monitor:9.2.0'
}
"""
when:
def build = gradleRunner("generateSnykDependencyGraph").build()
then:
build.task(":generateSnykDependencyGraph").outcome == TaskOutcome.SUCCESS
JSONAssert.assertEquals(file( "build/snyk/dependencies.json").text, """{
"meta": {
"method": "custom gradle",
"id": "gradle",
"node": "v16.15.1",
"name": "gradle",
"plugin": "extern:gradle",
"pluginRuntime": "unknown",
"monitorGraph": true
},
"depGraphJSON": {
"pkgManager": {
"version": "7.4.2",
"name": "gradle"
},
"schemaVersion": "1.2.0",
"graph": {
"rootNodeId": "root-node",
"nodes": [
{
"nodeId": "root-node",
"deps": [
{
"nodeId": "org.apache.lucene:lucene-monitor@9.2.0"
}
],
"pkgId": "hello-world@1.0-SNAPSHOT"
},
{
"nodeId": "org.apache.lucene:lucene-monitor@9.2.0",
"deps": [
{
"nodeId": "org.apache.lucene:lucene-memory@9.2.0"
},
{
"nodeId": "org.apache.lucene:lucene-analysis-common@9.2.0"
},
{
"nodeId": "org.apache.lucene:lucene-core@9.2.0"
}
],
"pkgId": "org.apache.lucene:lucene-monitor@9.2.0"
},
{
"nodeId": "org.apache.lucene:lucene-memory@9.2.0",
"deps": [
{
"nodeId": "org.apache.lucene:lucene-core@9.2.0"
}
],
"pkgId": "org.apache.lucene:lucene-memory@9.2.0"
},
{
"nodeId": "org.apache.lucene:lucene-core@9.2.0",
"deps": [
],
"pkgId": "org.apache.lucene:lucene-core@9.2.0"
},
{
"nodeId": "org.apache.lucene:lucene-analysis-common@9.2.0",
"deps": [
{
"nodeId": "org.apache.lucene:lucene-core@9.2.0"
}
],
"pkgId": "org.apache.lucene:lucene-analysis-common@9.2.0"
}
]
},
"pkgs": [
{
"id": "hello-world@1.0-SNAPSHOT",
"info": {
"name": "hello-world",
"version": "1.0-SNAPSHOT"
}
},
{
"id": "org.apache.lucene:lucene-monitor@9.2.0",
"info": {
"name": "org.apache.lucene:lucene-monitor",
"version": "9.2.0"
}
},
{
"id": "org.apache.lucene:lucene-memory@9.2.0",
"info": {
"name": "org.apache.lucene:lucene-memory",
"version": "9.2.0"
}
},
{
"id": "org.apache.lucene:lucene-core@9.2.0",
"info": {
"name": "org.apache.lucene:lucene-core",
"version": "9.2.0"
}
},
{
"id": "org.apache.lucene:lucene-analysis-common@9.2.0",
"info": {
"name": "org.apache.lucene:lucene-analysis-common",
"version": "9.2.0"
}
}
]
},
"target": {
"remoteUrl": "http://github.com/elastic/elasticsearch.git",
"branch": "unknown"
}
}""", true)
}

def "upload fails with reasonable error message"() {
given:
buildFile << """
apply plugin:'java'
"""
when:
def result = withWireMock(PUT, "/api/v1/monitor/gradle/graph", "OK", HTTP_CREATED) { server ->
buildFile << """
tasks.named('uploadSnykDependencyGraph').configure {
getUrl().set('${server.baseUrl()}/api/v1/monitor/gradle/graph')
getToken().set("myToken")
}
"""
gradleRunner("uploadSnykDependencyGraph", '-i', '--stacktrace').build()
}
then:
result.task(":uploadSnykDependencyGraph").outcome == TaskOutcome.SUCCESS
result.output.contains("Snyk API call response status: 201")

when:
result = withWireMock(PUT, GRADLE_GRAPH_ENDPOINT, "Internal Error", HTTP_INTERNAL_ERROR) { server ->
buildFile << """
tasks.named('uploadSnykDependencyGraph').configure {
getUrl().set('${server.baseUrl()}/api/v1/monitor/gradle/graph')
}
"""
gradleRunner("uploadSnykDependencyGraph", '-i').buildAndFail()
}

then:
result.task(":uploadSnykDependencyGraph").outcome == TaskOutcome.FAILED
result.output.contains("Uploading Snyk Graph failed with http code 500: Internal Error")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import org.elasticsearch.gradle.internal.info.GlobalBuildInfoPlugin;
import org.elasticsearch.gradle.internal.precommit.InternalPrecommitTasks;
import org.elasticsearch.gradle.internal.snyk.SnykDependencyMonitoringGradlePlugin;
import org.gradle.api.InvalidUserDataException;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
Expand Down Expand Up @@ -59,7 +60,7 @@ public void apply(final Project project) {
project.getPluginManager().apply("elasticsearch.publish");
project.getPluginManager().apply(ElasticsearchJavadocPlugin.class);
project.getPluginManager().apply(DependenciesInfoPlugin.class);
project.getPluginManager().apply(DependenciesGraphPlugin.class);
project.getPluginManager().apply(SnykDependencyMonitoringGradlePlugin.class);
InternalPrecommitTasks.create(project, true);
configureLicenseAndNotice(project);
}
Expand Down

This file was deleted.

0 comments on commit dedb849

Please sign in to comment.