|
38 | 38 | import java.security.cert.CertPath;
|
39 | 39 | import java.util.Base64;
|
40 | 40 | import java.util.concurrent.Callable;
|
| 41 | +import org.apache.commons.codec.binary.Hex; |
41 | 42 | import picocli.CommandLine.ArgGroup;
|
42 | 43 | import picocli.CommandLine.Command;
|
43 | 44 | import picocli.CommandLine.Option;
|
|
48 | 49 | aliases = {"verify-bundle"},
|
49 | 50 | description = "verify an artifact")
|
50 | 51 | public class Verify implements Callable<Integer> {
|
51 |
| - @Parameters(arity = "1", paramLabel = "<artifact>", description = "artifact to verify") |
52 |
| - Path artifact; |
| 52 | + |
| 53 | + private static final String SHA256_PREFIX = "sha256:"; |
| 54 | + |
| 55 | + @Parameters( |
| 56 | + arity = "1", |
| 57 | + paramLabel = "<artifact>", |
| 58 | + description = "an artifact path or artifact hash (sha256:abc...) to verify") |
| 59 | + String artifact; |
53 | 60 |
|
54 | 61 | @ArgGroup(multiplicity = "1", exclusive = true)
|
55 | 62 | SignatureFiles signatureFiles;
|
@@ -107,7 +114,10 @@ static class Policy {
|
107 | 114 |
|
108 | 115 | @Override
|
109 | 116 | public Integer call() throws Exception {
|
110 |
| - byte[] digest = asByteSource(artifact.toFile()).hash(Hashing.sha256()).asBytes(); |
| 117 | + byte[] digest = |
| 118 | + artifact.startsWith(SHA256_PREFIX) |
| 119 | + ? Hex.decodeHex(artifact.substring(SHA256_PREFIX.length())) |
| 120 | + : asByteSource(Path.of(artifact).toFile()).hash(Hashing.sha256()).asBytes(); |
111 | 121 |
|
112 | 122 | Bundle bundle;
|
113 | 123 | if (signatureFiles.sigAndCert != null) {
|
@@ -178,7 +188,11 @@ public Integer call() throws Exception {
|
178 | 188 | } else {
|
179 | 189 | throw new IllegalStateException("Unable to initialize verifier");
|
180 | 190 | }
|
181 |
| - verifier.verify(artifact, bundle, verificationOptions); |
| 191 | + if (artifact.startsWith(SHA256_PREFIX)) { |
| 192 | + verifier.verify(digest, bundle, verificationOptions); |
| 193 | + } else { |
| 194 | + verifier.verify(Path.of(artifact), bundle, verificationOptions); |
| 195 | + } |
182 | 196 | return 0;
|
183 | 197 | }
|
184 | 198 | }
|
0 commit comments