Skip to content

Commit

Permalink
Merge pull request #42731 from gayaldassanayake/fix-tool-hash-9.x
Browse files Browse the repository at this point in the history
[2201.9.x] Fix hash verification and hanging issues of bal tool pull
  • Loading branch information
azinneera committed May 14, 2024
2 parents bc31254 + aff96d3 commit 20c311b
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,7 @@ public String[] pullTool(String toolId, String version, Path balaCacheDirPath, S
Optional<String> latestVersion = Optional.empty();
Optional<String> balaUrl = Optional.empty();
Optional<String> platform = Optional.empty();
String digest = "";
if (body.isPresent()) {
Optional<MediaType> contentType = Optional.ofNullable(body.get().contentType());
if (contentType.isPresent() && isApplicationJsonContentType(contentType.get().toString())) {
Expand All @@ -699,6 +700,7 @@ public String[] pullTool(String toolId, String version, Path balaCacheDirPath, S
balaUrl = Optional.ofNullable(jsonContent.get(BALA_URL).getAsString());
platform = Optional.of(jsonContent.get(PLATFORM).getAsString())
.or(() -> Optional.of(ANY_PLATFORM));
digest = Optional.ofNullable(jsonContent.get(DIGEST).getAsString()).orElse("");
}
}

Expand All @@ -724,7 +726,7 @@ public String[] pullTool(String toolId, String version, Path balaCacheDirPath, S
try {
createBalaInHomeRepo(balaDownloadResponse, packagePathInBalaCache, org.get(), pkgName.get(),
isNightlyBuild, null, balaUrl.get(), balaFileName,
enableOutputStream ? outStream : null, logFormatter, "");
enableOutputStream ? outStream : null, logFormatter, digest);
return new String[] { String.valueOf(true), org.get(), pkgName.get(), latestVersion.get() };
} catch (PackageAlreadyExistsException ignore) {
// package already exists. setting org, name and version fields is enough
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
*/
public class Utils {

private static final int BUFFER_SIZE = 1024;
public static final String DEPRECATED_META_FILE_NAME = "deprecated.txt";
public static final boolean SET_BALLERINA_STAGE_CENTRAL = Boolean.parseBoolean(
System.getenv(BALLERINA_STAGE_CENTRAL));
Expand Down Expand Up @@ -348,7 +349,7 @@ private static void writeAndHandleProgress(InputStream inputStream, FileOutputSt
long totalSizeInKB, String fullPkgName, PrintStream outStream, LogFormatter logFormatter,
Path homeRepo) throws IOException {
int count;
byte[] buffer = new byte[1024];
byte[] buffer = new byte[BUFFER_SIZE];
String remoteRepo = getRemoteRepo();
String progressBarTask = fullPkgName + " [" + remoteRepo + " ->" + homeRepo + "] ";
try (ProgressBar progressBar = new ProgressBar(progressBarTask, totalSizeInKB, 1000,
Expand All @@ -365,7 +366,7 @@ private static void writeAndHandleProgress(InputStream inputStream, FileOutputSt
private static void writeAndHandleProgressQuietly(InputStream inputStream, FileOutputStream outputStream)
throws IOException {
int count;
byte[] buffer = new byte[1024];
byte[] buffer = new byte[BUFFER_SIZE];

while ((count = inputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, count);
Expand Down Expand Up @@ -479,7 +480,8 @@ public static String checkHash(String filePath, String algorithm) throws Central

try (InputStream is = new FileInputStream(filePath);
DigestInputStream dis = new DigestInputStream(is, md)) {
while (dis.read() != -1) {
byte[] buffer = new byte[BUFFER_SIZE];
while (dis.read(buffer) != -1) {
}
md = dis.getMessageDigest();
return bytesToHex(md.digest());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -870,14 +870,14 @@ public void testPullTool() throws IOException, CentralClientException {
" \"name\": \"toolbox\",\n" +
" \"version\": \"0.1.0\",\n" +
" \"balaURL\": \"" + toolBalaUrl + "\",\n" +
" \"platform\": \"java17\"\n}");
" \"platform\": \"java17\",\n" +
" \"digest\": \"sha-256=623bae28884bbc9cd61eb684acf7921cf43cb1d19ed0e36766bf6a75b0cdb15b\"\n}");
Response mockResponse = new Response.Builder()
.request(mockRequest)
.protocol(Protocol.HTTP_1_1)
.code(HttpURLConnection.HTTP_OK)
.addHeader(LOCATION, this.balaUrl)
.addHeader(CONTENT_DISPOSITION, balaFileName)
.addHeader(DIGEST, "sha-256=47e043c80d516234b1e6bd93140f126c9d9e79b5c7c0600cc6316d12504c2cf4")
.message("")
.body(mockResponseBody)
.build();
Expand Down

0 comments on commit 20c311b

Please sign in to comment.