Skip to content

Commit 7cf17bd

Browse files
authored
Adds Skaffold digest downloader. (#6)
1 parent 6d304ed commit 7cf17bd

2 files changed

Lines changed: 36 additions & 2 deletions

File tree

skaffold-plugins-core/src/integration-test/java/com/google/cloud/tools/skaffold/downloader/SkaffoldDownloaderIntegrationTest.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,12 @@
1717
package com.google.cloud.tools.skaffold.downloader;
1818

1919
import java.io.IOException;
20+
import java.nio.charset.StandardCharsets;
21+
import java.nio.file.Files;
2022
import java.nio.file.Path;
23+
import java.security.MessageDigest;
24+
import java.security.NoSuchAlgorithmException;
25+
import javax.xml.bind.DatatypeConverter;
2126
import org.junit.Assert;
2227
import org.junit.Rule;
2328
import org.junit.Test;
@@ -29,10 +34,25 @@ public class SkaffoldDownloaderIntegrationTest {
2934
@Rule public TemporaryFolder temporaryFolder = new TemporaryFolder();
3035

3136
@Test
32-
public void testDownloadLatest() throws IOException, InterruptedException {
37+
public void testDownloadLatest()
38+
throws IOException, InterruptedException, NoSuchAlgorithmException {
3339
Path temporarySkaffoldExecutable = temporaryFolder.newFile().toPath();
3440
SkaffoldDownloader.downloadLatest(temporarySkaffoldExecutable);
3541
Process skaffoldProcess = new ProcessBuilder(temporarySkaffoldExecutable.toString()).start();
3642
Assert.assertEquals(0, skaffoldProcess.waitFor());
43+
44+
// Downloads and checks that the digest matches.
45+
Path temporarySkaffoldExecutableDigest = temporaryFolder.newFile().toPath();
46+
SkaffoldDownloader.downloadLatestDigest(temporarySkaffoldExecutableDigest);
47+
48+
MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");
49+
byte[] expectedDigest = messageDigest.digest(Files.readAllBytes(temporarySkaffoldExecutable));
50+
51+
String receivedDigestHex =
52+
new String(Files.readAllBytes(temporarySkaffoldExecutableDigest), StandardCharsets.UTF_8)
53+
.substring(0, 64);
54+
byte[] receivedDigest = DatatypeConverter.parseHexBinary(receivedDigestHex);
55+
56+
Assert.assertArrayEquals(expectedDigest, receivedDigest);
3757
}
3858
}

skaffold-plugins-core/src/main/java/com/google/cloud/tools/skaffold/downloader/SkaffoldDownloader.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,22 @@ public class SkaffoldDownloader {
4646
* @throws MalformedURLException if the URL to download from is malformed
4747
*/
4848
public static void downloadLatest(Path destination) throws IOException {
49+
download(getLatestUrl(""), destination);
50+
}
51+
52+
/**
53+
* Downloads the latest {@code skaffold} release digest.
54+
*
55+
* @throws IOException if an I/O exception occurs during download
56+
* @throws MalformedURLException if the URL to download from is malformed
57+
*/
58+
public static void downloadLatestDigest(Path destination) throws IOException {
59+
download(getLatestUrl(".sha256"), destination);
60+
}
61+
62+
private static URL getLatestUrl(String suffix) throws MalformedURLException {
4963
// Skaffold publishes the latest release with version "latest".
50-
download(new URL(getUrl("latest", OperatingSystem.resolve())), destination);
64+
return new URL(getUrl("latest", OperatingSystem.resolve()) + suffix);
5165
}
5266

5367
/**

0 commit comments

Comments
 (0)