Skip to content

Commit 4218bbc

Browse files
committed
add "check" functionality
1 parent 8947ca5 commit 4218bbc

File tree

7 files changed

+197
-12
lines changed

7 files changed

+197
-12
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
### Gradle template
1212
.gradle
1313
build/
14+
out/
1415

1516
# Ignore Gradle GUI config
1617
gradle-app.setting

build.gradle

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88

99

1010
group 'de.eacg'
11-
version '0.1.4'
11+
version '0.2.0'
1212

1313
buildscript {
1414
dependencies {
1515
classpath 'io.codearte.gradle.nexus:gradle-nexus-staging-plugin:0.5.3'
1616
classpath "com.gradle.publish:plugin-publish-plugin:0.9.4"
17-
// classpath "de.eacg:ecs-gradle-plugin:0.1.3" // uncomment to scan this compoment
17+
// classpath "de.eacg:ecs-gradle-plugin:0.2.0" // uncomment to scan this compoment
1818
}
1919
repositories {
2020
mavenLocal()
@@ -30,7 +30,7 @@ apply plugin: 'maven'
3030
apply plugin: 'signing'
3131
apply plugin: 'io.codearte.nexus-staging'
3232
apply plugin: 'com.gradle.plugin-publish'
33-
// apply plugin: 'de.eacg.ecsPlugin' // uncomment to scan this compoment
33+
//apply plugin: 'de.eacg.ecsPlugin' // uncomment to scan this compoment
3434

3535

3636
sourceCompatibility = JavaVersion.VERSION_1_7
@@ -52,15 +52,15 @@ nexusStaging {
5252

5353
/*
5454
ecsPlugin { // uncomment to scan this compoment
55-
credentials = '~/.ecsrc.json'
56-
verbose = true
55+
credentials = '../ecs-test-settings.json'
56+
verbose = false
5757
}
5858
*/
5959

6060
dependencies {
6161
compile gradleApi()
6262
compile localGroovy()
63-
compile 'de.eacg:ecs-java-client:0.2.1'
63+
compile 'de.eacg:ecs-java-client:0.2.2'
6464
testCompile 'junit:junit:4.12'
6565
}
6666

@@ -83,8 +83,9 @@ artifacts {
8383
}
8484

8585
signing {
86-
required { gradle.taskGraph.hasTask("uploadArchives") }
8786
sign configurations.archives
87+
useGpgCmd()
88+
required { gradle.taskGraph.hasTask("uploadArchives") }
8889
}
8990

9091
archivesBaseName = 'ecs-gradle-plugin'
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
#Fri Apr 15 16:31:47 CEST 2016
1+
#Sun Oct 28 19:23:29 CET 2018
22
distributionBase=GRADLE_USER_HOME
33
distributionPath=wrapper/dists
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
6-
distributionUrl=https\://services.gradle.org/distributions/gradle-2.12-bin.zip
6+
distributionUrl=https\://services.gradle.org/distributions/gradle-2.12-all.zip
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
package de.eacg.ecs.gradle.plugin
2+
3+
import de.eacg.ecs.client.CheckResults
4+
import de.eacg.ecs.client.JsonProperties
5+
import de.eacg.ecs.client.RestClient
6+
import de.eacg.ecs.client.Scan
7+
import org.gradle.api.GradleException
8+
import org.gradle.api.artifacts.Configuration
9+
import org.gradle.api.artifacts.result.ResolutionResult
10+
import org.gradle.api.artifacts.result.ResolvedComponentResult
11+
import org.gradle.api.tasks.StopActionException
12+
import org.gradle.api.tasks.TaskAction
13+
14+
class CheckTask extends ScanTask{
15+
16+
@TaskAction
17+
def scan() {
18+
def scanExt = project.ecsPlugin
19+
def projProps = new ProjectProperties()
20+
21+
def userAgent = "${projProps.getName()}/${projProps.getVersion()}"
22+
this.verbose = scanExt.verbose
23+
this.prefixString = 'ecsCheck =>'
24+
25+
JsonProperties apiClientConfig = readAndCheckCredentials(scanExt);
26+
27+
if (scanExt.skip) {
28+
println "${prefixString} Skipping execution"
29+
return
30+
}
31+
32+
33+
/**
34+
* TODO: allow more than one configurations????
35+
*/
36+
37+
Configuration configuration = getConfiguration(scanExt);
38+
39+
ResolutionResult result = configuration.getIncoming().getResolutionResult()
40+
ResolvedComponentResult root = result.getRoot();
41+
42+
Configuration pomsConfig = project.configurations.detachedConfiguration()
43+
44+
setupPomConfig(root, pomsConfig)
45+
populateMetaInfCache(pomsConfig)
46+
47+
def ecsRootDependency = mapDependencies(root)
48+
if (verbose) {
49+
printDependencies(ecsRootDependency, 0)
50+
}
51+
52+
CheckResults results = null
53+
54+
RestClient restClient = new RestClient(apiClientConfig, userAgent);
55+
Scan scan = new Scan(scanExt.projectName, scanExt.moduleName, scanExt.moduleId, ecsRootDependency);
56+
57+
try {
58+
results = restClient.checkScan(scan);
59+
} catch (RestClient.RestClientException e) {
60+
println "WARNING: ${e.getMessage()}"
61+
}
62+
63+
if (restClient.getResponseStatus() == 200 && results != null ) {
64+
evaluateResults(results);
65+
}
66+
}
67+
68+
private void evaluateResults(CheckResults results) throws GradleException {
69+
def scanExt = project.ecsPlugin
70+
71+
for (CheckResults.Warning w : results.getWarnings()) {
72+
String cStr = w.getComponent()
73+
String vStr = w.getVersion()
74+
75+
String msg = String.format("Component \"%s %s\"", cStr != null ? cStr : "", vStr != null ? vStr : "")
76+
77+
if (w.isComponentNotFound()) {
78+
println "WARNING: ${msg} not found"
79+
}
80+
81+
if (w.isVersionNotFound()) {
82+
println "WARNING: ${msg} version not found"
83+
}
84+
85+
if (w.isLicenseNotFound()) {
86+
println "WARNING: ${msg} license not found"
87+
}
88+
}
89+
90+
if (!scanExt.allowBreakBuild) {
91+
return
92+
}
93+
94+
if (scanExt.breakOnLegalIssues) {
95+
int violations = 0
96+
int warnings = 0
97+
98+
for (CheckResults.Result result : results.getData()) {
99+
String msg = result.getComponent().getName() + " " + result.getComponent().getVersion()
100+
List<CheckResults.Violation> legalViolations
101+
if (scanExt.assumeComponentsModified) {
102+
legalViolations = result.getChanged().getViolations()
103+
} else {
104+
legalViolations = result.getNot_changed().getViolations()
105+
}
106+
107+
for (CheckResults.Violation v : legalViolations) {
108+
if (v.isViolation()) {
109+
println "${msg} : ${v.getMessage()}"
110+
violations++
111+
} else if (v.isWarning()) {
112+
println "${msg} : ${v.getMessage()}"
113+
warnings++
114+
}
115+
}
116+
}
117+
118+
if (scanExt.breakOnViolationsAndWarnings && (warnings > 0 || violations > 0)) {
119+
throw new GradleException("Found legal violations")
120+
}
121+
122+
if (scanExt.breakOnViolationsOnly && (violations > 0)) {
123+
throw new GradleException("Found legal violations")
124+
}
125+
126+
}
127+
128+
if (scanExt.breakOnVulnerabilities) {
129+
int violations = 0
130+
int warnings = 0
131+
132+
for (CheckResults.Result result : results.getData()) {
133+
String componentStr = result.getComponent().getName() + " " + result.getComponent().getVersion()
134+
for (CheckResults.Vulnerabilities v : result.getVulnerabilities()) {
135+
String msg = componentStr + ": [" + v.getName() + "] " + v.getDescription()
136+
if (v.isViolation()) {
137+
println msg
138+
violations++
139+
} else if (v.isWarning()) {
140+
println msg
141+
warnings++
142+
}
143+
}
144+
}
145+
146+
if (scanExt.breakOnViolationsAndWarnings && (warnings > 0 || violations > 0)) {
147+
throw new StopActionException("Found vulnerabilities")
148+
}
149+
150+
if (scanExt.breakOnViolationsOnly && (violations > 0)) {
151+
throw new StopActionException("Found vulnerabilities")
152+
}
153+
154+
}
155+
}
156+
}

src/main/groovy/de/eacg/ecs/gradle/plugin/EcsPlugin.groovy

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,16 @@ import org.gradle.api.Project
1414
class EcsPlugin implements Plugin<Project> {
1515
void apply(Project project) {
1616
project.extensions.create('ecsPlugin', EcsPluginExtension)
17+
1718
project.ecsPlugin.projectName = project.name
1819
project.ecsPlugin.moduleName = project.name
1920
project.ecsPlugin.moduleId = project.group + ':' + project.name
21+
2022
project.task('dependency-scan', type: ScanTask)
2123
project.task('ecsScan', type: ScanTask) // alias
24+
25+
project.task('dependency-check', type: CheckTask)
26+
project.task('ecsCheck', type: CheckTask) // alias
2227
}
2328
}
2429

src/main/groovy/de/eacg/ecs/gradle/plugin/EcsPluginExtension.groovy

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,23 @@ class EcsPluginExtension {
2929
Boolean skipTransfer = false
3030
Boolean verbose = false
3131

32+
String proxyUrl
33+
String proxyPort
34+
35+
String proxyUser
36+
String proxyPass
37+
38+
39+
Boolean allowBreakBuild;
40+
Boolean breakOnLegalIssues;
41+
42+
Boolean breakOnVulnerabilities;
43+
Boolean breakOnViolationsOnly;
44+
45+
Boolean breakOnViolationsAndWarnings;
46+
Boolean assumeComponentsModified;
47+
48+
3249
void configuration(String... confs) {
3350
for (String s in confs) {
3451
configurations << s

src/main/groovy/de/eacg/ecs/gradle/plugin/ScanTask.groovy

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ class ScanTask extends DefaultTask {
147147
}
148148
}
149149

150-
private Configuration getConfiguration(def scanExt) {
150+
protected Configuration getConfiguration(def scanExt) {
151151
def firstConfig = scanExt.configurations.first()
152152
println "${prefixString} Scanning with '${firstConfig}' configuration"
153153
try {
@@ -158,7 +158,7 @@ class ScanTask extends DefaultTask {
158158
}
159159
}
160160

161-
private void populateMetaInfCache(Configuration pomsConfig) {
161+
protected void populateMetaInfCache(Configuration pomsConfig) {
162162
try {
163163
def resolvedPomsConfig = pomsConfig.resolvedConfiguration.lenientConfiguration
164164
resolvedPomsConfig.getArtifacts(new Spec<org.gradle.api.artifacts.Dependency>(){
@@ -181,7 +181,7 @@ class ScanTask extends DefaultTask {
181181
}
182182
}
183183

184-
private JsonProperties readAndCheckCredentials(def scanExt) {
184+
protected JsonProperties readAndCheckCredentials(def scanExt) {
185185
JsonProperties properties
186186

187187
try {
@@ -195,6 +195,11 @@ class ScanTask extends DefaultTask {
195195
properties.setBaseUrl(scanExt.baseUrl)
196196
properties.setApiPath(scanExt.apiPath)
197197

198+
properties.setProxyUrl(scanExt.proxyUrl);
199+
properties.setProxyPort(scanExt.proxyPort);
200+
properties.setProxyUser(scanExt.proxyUser);
201+
properties.setProxyPass(scanExt.proxyPass);
202+
198203
def missingKeys = properties.validate()
199204
if(missingKeys.isEmpty() == false) {
200205
String err = "The mandatory parameter(s) '${missingKeys}' for plugin 'ecs-gradle-plugin' is/are missing or invalid"

0 commit comments

Comments
 (0)