|
| 1 | +// Copyright 2023 SLSA Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package io.github.slsaframework; |
| 16 | + |
| 17 | +import org.apache.maven.artifact.Artifact; |
| 18 | +import org.apache.maven.execution.MavenSession; |
| 19 | +import org.apache.maven.plugin.AbstractMojo; |
| 20 | +import org.apache.maven.plugin.BuildPluginManager; |
| 21 | +import org.apache.maven.plugin.MojoExecutionException; |
| 22 | +import org.apache.maven.plugin.MojoFailureException; |
| 23 | +import org.apache.maven.plugins.annotations.LifecyclePhase; |
| 24 | +import org.apache.maven.plugins.annotations.Component; |
| 25 | +import org.apache.maven.plugins.annotations.Mojo; |
| 26 | +import org.apache.maven.plugins.annotations.Parameter; |
| 27 | +import org.apache.maven.project.MavenProject; |
| 28 | + |
| 29 | +import static org.twdata.maven.mojoexecutor.MojoExecutor.*; |
| 30 | + |
| 31 | +import java.io.File; |
| 32 | +import java.util.Set; |
| 33 | + |
| 34 | +/* |
| 35 | + SlsaVerificationMojo is a Maven plugin that wraps https://github.com/slsa-framework/slsa-verifier. |
| 36 | + At a high level, it does the following: |
| 37 | + 1: Install the slsa-verifier. |
| 38 | + 2: Loop through all dependencies of a pom.xml. Resolve each dependency. |
| 39 | + 3: Check if each dependency also has a provenance file. |
| 40 | + 4: Run the slsa-verifier for the dependency if there is a provenance file. |
| 41 | + 5: Output the results. |
| 42 | +
|
| 43 | + The plugin is meant to be installed and then run from the root of a given project file. |
| 44 | + A pseudo-workflow looks like this: |
| 45 | + 1: git clone --depth=1 https://github.com/slsa-framework/slsa-verifier |
| 46 | + 2: cd slsa-verifier/experimental/maven-plugin |
| 47 | + 3: mvn clean install |
| 48 | + 4: cd /tmp |
| 49 | + 5: git clone your repository |
| 50 | + 6: cd into your repository |
| 51 | + 7: mvn io.github.slsa-framework:slsa-verification-plugin:0.0.1:verify |
| 52 | +*/ |
| 53 | +@Mojo(name = "verify", defaultPhase = LifecyclePhase.VALIDATE) |
| 54 | +public class SlsaVerificationMojo extends AbstractMojo { |
| 55 | + @Parameter(defaultValue = "${project}", required = true, readonly = true) |
| 56 | + private MavenProject project; |
| 57 | + |
| 58 | + /** |
| 59 | + * Custom path of GOHOME, default value is $HOME/go |
| 60 | + **/ |
| 61 | + @Parameter(property = "slsa.verifier.path", required = true) |
| 62 | + private String verifierPath; |
| 63 | + |
| 64 | + @Component |
| 65 | + private MavenSession mavenSession; |
| 66 | + |
| 67 | + @Component |
| 68 | + private BuildPluginManager pluginManager; |
| 69 | + |
| 70 | + |
| 71 | + public void execute() throws MojoExecutionException, MojoFailureException { |
| 72 | + // Verify the slsa of each dependency |
| 73 | + Set<Artifact> dependencyArtifacts = project.getDependencyArtifacts(); |
| 74 | + for (Artifact artifact : dependencyArtifacts ) { |
| 75 | + // Retrieve the dependency jar and its slsa file |
| 76 | + String artifactStr = artifact.getGroupId() + ":" + artifact.getArtifactId() + ":" + artifact.getVersion(); |
| 77 | + try { |
| 78 | + // Retrieve the slsa file of the artifact |
| 79 | + executeMojo( |
| 80 | + plugin( |
| 81 | + groupId("com.googlecode.maven-download-plugin"), |
| 82 | + artifactId("download-maven-plugin"), |
| 83 | + version("1.7.0") |
| 84 | + ), |
| 85 | + goal("artifact"), |
| 86 | + configuration( |
| 87 | + element(name("outputDirectory"), "${project.build.directory}/slsa"), |
| 88 | + element(name("groupId"), artifact.getGroupId()), |
| 89 | + element(name("artifactId"), artifact.getArtifactId()), |
| 90 | + element(name("version"), artifact.getVersion()), |
| 91 | + element(name("type"), "intoto.build.slsa"), |
| 92 | + element(name("classifier"), "jar") |
| 93 | + ), |
| 94 | + executionEnvironment( |
| 95 | + project, |
| 96 | + mavenSession, |
| 97 | + pluginManager |
| 98 | + ) |
| 99 | + ); |
| 100 | + |
| 101 | + // Retrieve the dependency jar if slsa file does exists for this artifact |
| 102 | + executeMojo( |
| 103 | + plugin( |
| 104 | + groupId("org.apache.maven.plugins"), |
| 105 | + artifactId("maven-dependency-plugin"), |
| 106 | + version("3.6.0") |
| 107 | + ), |
| 108 | + goal("copy"), |
| 109 | + configuration( |
| 110 | + element(name("outputDirectory"), "${project.build.directory}/slsa"), |
| 111 | + element(name("artifact"), artifactStr) |
| 112 | + ), |
| 113 | + executionEnvironment( |
| 114 | + project, |
| 115 | + mavenSession, |
| 116 | + pluginManager |
| 117 | + ) |
| 118 | + ); |
| 119 | + } catch(MojoExecutionException e) { |
| 120 | + getLog().info("Skipping slsa verification for " + artifactStr + ": No slsa file found."); |
| 121 | + continue; |
| 122 | + } |
| 123 | + |
| 124 | + // Verify slsa file |
| 125 | + try { |
| 126 | + // Run slsa verification on the artifact and print the result |
| 127 | + // It will never fail the build process |
| 128 | + // This might be prone to command-injections. TODO: Secure against that. |
| 129 | + String arguments = "verify-artifact --provenance-path "; |
| 130 | + arguments += "${project.build.directory}/slsa/" + artifact.getArtifactId() + "-" + artifact.getVersion() + "-jar.intoto.build.slsa "; |
| 131 | + arguments += " --source-uri ./ ${project.build.directory}/slsa/" + artifact.getArtifactId() + "-" + artifact.getVersion() + ".jar"; |
| 132 | + executeMojo( |
| 133 | + plugin( |
| 134 | + groupId("org.codehaus.mojo"), |
| 135 | + artifactId("exec-maven-plugin"), |
| 136 | + version("3.1.0") |
| 137 | + ), |
| 138 | + goal("exec"), |
| 139 | + configuration( |
| 140 | + element(name("executable"), verifierPath), |
| 141 | + element(name("commandlineArgs"), arguments), |
| 142 | + element(name("useMavenLogger"), "true") |
| 143 | + ), |
| 144 | + executionEnvironment( |
| 145 | + project, |
| 146 | + mavenSession, |
| 147 | + pluginManager |
| 148 | + ) |
| 149 | + ); |
| 150 | + } catch(MojoExecutionException e) { |
| 151 | + // TODO: Properly interpret the output based on the verification plugin. |
| 152 | + getLog().info("Skipping slsa verification: Fail to run slsa verifier."); |
| 153 | + return; |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | +} |
0 commit comments