Skip to content

Commit 67a3a30

Browse files
alan-signalgreyson-signal
authored andcommitted
Run witness checksums in task and only when compiling.
1 parent 898d92b commit 67a3a30

File tree

1 file changed

+58
-37
lines changed

1 file changed

+58
-37
lines changed

buildSrc/src/main/groovy/org/whispersystems/witness/WitnessPlugin.groovy

+58-37
Original file line numberDiff line numberDiff line change
@@ -15,62 +15,83 @@ class WitnessPluginExtension {
1515
class WitnessPlugin implements Plugin<Project> {
1616

1717
static String calculateSha256(file) {
18-
MessageDigest md = MessageDigest.getInstance("SHA-256");
19-
file.eachByte 4096, {bytes, size ->
20-
md.update(bytes, 0, size);
18+
MessageDigest md = MessageDigest.getInstance('SHA-256')
19+
file.eachByte 4096, { bytes, size ->
20+
md.update(bytes, 0, size)
2121
}
22-
return md.digest().collect {String.format "%02x", it}.join();
22+
return md.digest().collect { String.format '%02x', it }.join()
2323
}
2424

2525
void apply(Project project) {
26-
project.extensions.create("dependencyVerification", WitnessPluginExtension)
26+
project.extensions.create('dependencyVerification', WitnessPluginExtension)
27+
2728
project.afterEvaluate {
28-
project.dependencyVerification.verify.each {
29-
assertion ->
30-
List parts = assertion[0].tokenize(':')
31-
String group = parts.get(0)
32-
String name = parts.get(1)
33-
String hash = assertion[1]
34-
35-
def artifacts = allArtifacts(project).findAll {
36-
return it.name.equals(name) && it.moduleVersion.id.group.equals(group)
29+
project.tasks
30+
.findAll { it.name =~ /compile/ }
31+
.each {
32+
it.dependsOn('verifyChecksums')
3733
}
34+
}
35+
36+
project.task('verifyChecksums') {
37+
group = 'Gradle Witness'
38+
description = 'Verify the contents of dependencyVerification block in witness-verifications.gradle file(s) match the checksums of dependencies.'
3839

39-
artifacts.forEach { dependency ->
40-
println "Verifying " + group + ":" + name
40+
doLast {
41+
def allArtifacts = allArtifacts(project)
4142

42-
if (dependency == null) {
43-
throw new InvalidUserDataException("No dependency for integrity assertion found: " + group + ":" + name)
43+
project.dependencyVerification.verify.each {
44+
assertion ->
45+
List parts = assertion[0].tokenize(':')
46+
String group = parts.get(0)
47+
String name = parts.get(1)
48+
String hash = assertion[1]
49+
50+
def artifacts = allArtifacts.findAll {
51+
it.moduleVersion.id.group == group && it.name == name
4452
}
4553

46-
if (!hash.equals(calculateSha256(dependency.file))) {
47-
throw new InvalidUserDataException("Checksum failed for " + assertion)
54+
artifacts.forEach { dependency ->
55+
println "Verifying $group:$name"
56+
57+
if (dependency == null) {
58+
throw new InvalidUserDataException("No dependency for integrity assertion found: $group:$name")
59+
}
60+
61+
if (hash != calculateSha256(dependency.file)) {
62+
throw new InvalidUserDataException("Checksum failed for $assertion")
63+
}
4864
}
49-
}
65+
}
5066
}
5167
}
5268

53-
project.task('calculateChecksums').doLast {
54-
def stringBuilder = new StringBuilder()
69+
project.task('calculateChecksums') {
70+
group = 'Gradle Witness'
71+
description = 'Recalculate checksums of dependencies and update the witness-verifications.gradle file(s).'
5572

56-
stringBuilder.append '// Auto-generated, use ./gradlew calculateChecksums to regenerate\n\n'
57-
stringBuilder.append 'dependencyVerification {\n'
73+
doLast {
74+
def stringBuilder = new StringBuilder()
5875

59-
stringBuilder.append ' verify = [\n'
76+
stringBuilder.append '// Auto-generated, use ./gradlew calculateChecksums to regenerate\n\n'
77+
stringBuilder.append 'dependencyVerification {\n'
6078

61-
allArtifacts(project)
62-
.findAll { dep -> !dep.id.componentIdentifier.displayName.startsWith('project :') }
63-
.collect { dep -> "['$dep.moduleVersion.id.group:$dep.name:$dep.moduleVersion.id.version',\n '${calculateSha256(dep.file)}']" }
64-
.sort()
65-
.unique()
66-
.each {
67-
dep -> stringBuilder.append "\n $dep,\n"
68-
}
79+
stringBuilder.append ' verify = [\n'
6980

70-
stringBuilder.append " ]\n"
71-
stringBuilder.append "}\n"
81+
allArtifacts(project)
82+
.findAll { dep -> !dep.id.componentIdentifier.displayName.startsWith('project :') }
83+
.collect { dep -> "['$dep.moduleVersion.id.group:$dep.name:$dep.moduleVersion.id.version',\n '${calculateSha256(dep.file)}']" }
84+
.sort()
85+
.unique()
86+
.each {
87+
dep -> stringBuilder.append "\n $dep,\n"
88+
}
89+
90+
stringBuilder.append ' ]\n'
91+
stringBuilder.append '}\n'
7292

73-
project.file("witness-verifications.gradle").write(stringBuilder.toString())
93+
project.file('witness-verifications.gradle').write(stringBuilder.toString())
94+
}
7495
}
7596
}
7697

0 commit comments

Comments
 (0)