-
Notifications
You must be signed in to change notification settings - Fork 25k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[8.3] Setup elasticsearch dependency monitoring with Snyk for product…
…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
Showing
27 changed files
with
939 additions
and
256 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
196 changes: 196 additions & 0 deletions
196
...rg/elasticsearch/gradle/internal/snyk/SnykDependencyMonitoringGradlePluginFuncTest.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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:[email protected]" | ||
} | ||
], | ||
"pkgId": "[email protected]" | ||
}, | ||
{ | ||
"nodeId": "org.apache.lucene:[email protected]", | ||
"deps": [ | ||
{ | ||
"nodeId": "org.apache.lucene:[email protected]" | ||
}, | ||
{ | ||
"nodeId": "org.apache.lucene:[email protected]" | ||
}, | ||
{ | ||
"nodeId": "org.apache.lucene:[email protected]" | ||
} | ||
], | ||
"pkgId": "org.apache.lucene:[email protected]" | ||
}, | ||
{ | ||
"nodeId": "org.apache.lucene:[email protected]", | ||
"deps": [ | ||
{ | ||
"nodeId": "org.apache.lucene:[email protected]" | ||
} | ||
], | ||
"pkgId": "org.apache.lucene:[email protected]" | ||
}, | ||
{ | ||
"nodeId": "org.apache.lucene:[email protected]", | ||
"deps": [ | ||
], | ||
"pkgId": "org.apache.lucene:[email protected]" | ||
}, | ||
{ | ||
"nodeId": "org.apache.lucene:[email protected]", | ||
"deps": [ | ||
{ | ||
"nodeId": "org.apache.lucene:[email protected]" | ||
} | ||
], | ||
"pkgId": "org.apache.lucene:[email protected]" | ||
} | ||
] | ||
}, | ||
"pkgs": [ | ||
{ | ||
"id": "[email protected]", | ||
"info": { | ||
"name": "hello-world", | ||
"version": "1.0-SNAPSHOT" | ||
} | ||
}, | ||
{ | ||
"id": "org.apache.lucene:[email protected]", | ||
"info": { | ||
"name": "org.apache.lucene:lucene-monitor", | ||
"version": "9.2.0" | ||
} | ||
}, | ||
{ | ||
"id": "org.apache.lucene:[email protected]", | ||
"info": { | ||
"name": "org.apache.lucene:lucene-memory", | ||
"version": "9.2.0" | ||
} | ||
}, | ||
{ | ||
"id": "org.apache.lucene:[email protected]", | ||
"info": { | ||
"name": "org.apache.lucene:lucene-core", | ||
"version": "9.2.0" | ||
} | ||
}, | ||
{ | ||
"id": "org.apache.lucene:[email protected]", | ||
"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") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 0 additions & 67 deletions
67
...ols-internal/src/main/java/org/elasticsearch/gradle/internal/DependenciesGraphPlugin.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.