-
-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Api/Implementation compatible. - Regex configuration name.
- Loading branch information
1 parent
77e3cc4
commit 132c81b
Showing
4 changed files
with
94 additions
and
1 deletion.
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
92 changes: 92 additions & 0 deletions
92
buildSrc/src/main/groovy/org/whispersystems/witness/WitnessPlugin.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,92 @@ | ||
package org.whispersystems.witness | ||
|
||
import org.gradle.api.InvalidUserDataException | ||
import org.gradle.api.Plugin | ||
import org.gradle.api.Project | ||
import org.gradle.api.artifacts.Configuration | ||
import org.gradle.api.artifacts.ResolvedArtifact | ||
|
||
import java.security.MessageDigest | ||
|
||
class WitnessPluginExtension { | ||
List verify | ||
String configuration | ||
} | ||
|
||
class WitnessPlugin implements Plugin<Project> { | ||
|
||
static String calculateSha256(file) { | ||
MessageDigest md = MessageDigest.getInstance("SHA-256"); | ||
file.eachByte 4096, {bytes, size -> | ||
md.update(bytes, 0, size); | ||
} | ||
return md.digest().collect {String.format "%02x", it}.join(); | ||
} | ||
|
||
void apply(Project project) { | ||
project.extensions.create("dependencyVerification", WitnessPluginExtension) | ||
project.afterEvaluate { | ||
project.dependencyVerification.verify.each { | ||
assertion -> | ||
List parts = assertion.tokenize(":") | ||
String group = parts.get(0) | ||
String name = parts.get(1) | ||
String hash = parts.get(2) | ||
|
||
def artifacts = allArtifacts(project).findAll { | ||
return it.name.equals(name) && it.moduleVersion.id.group.equals(group) | ||
} | ||
|
||
if (artifacts.size() > 1) { | ||
throw new InvalidUserDataException("Multiple artifacts found for $group:$name, ${artifacts.size()} found") | ||
} | ||
|
||
ResolvedArtifact dependency = artifacts.find() | ||
|
||
println "Verifying " + group + ":" + name | ||
|
||
if (dependency == null) { | ||
throw new InvalidUserDataException("No dependency for integrity assertion found: " + group + ":" + name) | ||
} | ||
|
||
if (!hash.equals(calculateSha256(dependency.file))) { | ||
throw new InvalidUserDataException("Checksum failed for " + assertion) | ||
} | ||
} | ||
} | ||
|
||
project.task('calculateChecksums').doLast { | ||
println "dependencyVerification {" | ||
|
||
def configurationName = project.dependencyVerification.configuration | ||
if (configurationName != null) { | ||
println " configuration = '$configurationName'" | ||
} | ||
|
||
println " verify = [" | ||
|
||
allArtifacts(project).each { | ||
dep -> | ||
println " '" + dep.moduleVersion.id.group+ ":" + dep.name + ":" + calculateSha256(dep.file) + "'," | ||
} | ||
|
||
println " ]" | ||
println "}" | ||
} | ||
} | ||
|
||
private static Set<ResolvedArtifact> allArtifacts(Project project) { | ||
def configurationName = project.dependencyVerification.configuration | ||
project.configurations | ||
.findAll { config -> config.name =~ configurationName } | ||
.collectMany { tryGetArtifacts(it) } | ||
} | ||
|
||
private static Set<ResolvedArtifact> tryGetArtifacts(Configuration configuration) { | ||
try { | ||
configuration.resolvedConfiguration.resolvedArtifacts | ||
} catch (Exception ignored) { | ||
[] as Set<ResolvedArtifact> | ||
} | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
buildSrc/src/main/resources/META-INF/gradle-plugins/witness.properties
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 @@ | ||
implementation-class=org.whispersystems.witness.WitnessPlugin |
Binary file not shown.