-
Notifications
You must be signed in to change notification settings - Fork 399
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Snjezana Peco <[email protected]>
- Loading branch information
Showing
13 changed files
with
174 additions
and
3 deletions.
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
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
50 changes: 50 additions & 0 deletions
50
org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/internal/gradle/checksums/HashProvider.java
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,50 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2020 Red Hat Inc. and others. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License 2.0 | ||
* which accompanies this distribution, and is available at | ||
* https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Red Hat Inc. - initial API and implementation | ||
*******************************************************************************/ | ||
package org.eclipse.jdt.ls.internal.gradle.checksums; | ||
|
||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.io.IOException; | ||
import java.security.DigestInputStream; | ||
import java.security.MessageDigest; | ||
import java.security.NoSuchAlgorithmException; | ||
|
||
public class HashProvider { | ||
|
||
private final String algorithm; | ||
|
||
public HashProvider() { | ||
this("SHA-256"); | ||
} | ||
|
||
public HashProvider(String algorithm) { | ||
this.algorithm = algorithm; | ||
} | ||
|
||
public String getChecksum(File file) throws IOException, NoSuchAlgorithmException { | ||
MessageDigest messageDigest = MessageDigest.getInstance(algorithm); | ||
try (DigestInputStream dis = new DigestInputStream(new FileInputStream(file), messageDigest)) { | ||
byte[] bytes = new byte[32768]; | ||
while (dis.read(bytes) != -1) { | ||
; | ||
} | ||
messageDigest = dis.getMessageDigest(); | ||
} | ||
StringBuilder result = new StringBuilder(); | ||
for (byte b : messageDigest.digest()) { | ||
result.append(String.format("%02x", b)); | ||
} | ||
return result.toString(); | ||
} | ||
|
||
} |
97 changes: 97 additions & 0 deletions
97
...clipse.jdt.ls.core/src/org/eclipse/jdt/ls/internal/gradle/checksums/WrapperValidator.java
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,97 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2020 Red Hat Inc. and others. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License 2.0 | ||
* which accompanies this distribution, and is available at | ||
* https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Red Hat Inc. - initial API and implementation | ||
*******************************************************************************/ | ||
package org.eclipse.jdt.ls.internal.gradle.checksums; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.FileInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.InputStreamReader; | ||
import java.net.URL; | ||
import java.net.URLConnection; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.security.NoSuchAlgorithmException; | ||
import java.util.Properties; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin; | ||
|
||
import com.google.common.cache.Cache; | ||
import com.google.common.cache.CacheBuilder; | ||
|
||
public class WrapperValidator { | ||
|
||
private static final String DISTRIBUTION_URL = "distributionUrl"; | ||
private static final String GRADLE_WRAPPER_PROPERTIES = "gradle/wrapper/gradle-wrapper.properties"; | ||
private static final String GRADLE_WRAPPER_JAR = "gradle/wrapper/gradle-wrapper.jar"; | ||
private final boolean DISABLED = Boolean.getBoolean("gradle.wrapper.check.disabled"); | ||
private HashProvider hashProvider; | ||
protected static Cache<String, String> downloadRequestsCache = CacheBuilder.newBuilder().maximumSize(100).expireAfterWrite(1, TimeUnit.HOURS).build(); | ||
|
||
|
||
public WrapperValidator() { | ||
this(new HashProvider()); | ||
} | ||
|
||
public WrapperValidator(HashProvider hashProvider) { | ||
this.hashProvider = hashProvider; | ||
} | ||
|
||
public String checkWrapper(String baseDir) throws IOException, NoSuchAlgorithmException { | ||
if (DISABLED) { | ||
return null; | ||
} | ||
Path wrapperJar = Paths.get(baseDir, GRADLE_WRAPPER_JAR); | ||
if (!wrapperJar.toFile().exists()) { | ||
return wrapperJar.toString() + " doesn't exist."; | ||
} | ||
Path wrapperProperties = Paths.get(baseDir, GRADLE_WRAPPER_PROPERTIES); | ||
if (!wrapperProperties.toFile().exists()) { | ||
return wrapperProperties.toString() + " doesn't exist."; | ||
} | ||
try (InputStream is = new FileInputStream(wrapperProperties.toFile())) { | ||
Properties props = new Properties(); | ||
props.load(is); | ||
String distributionUrl = props.getProperty(DISTRIBUTION_URL); | ||
if (distributionUrl == null) { | ||
return "Invalid the distributionUrl property"; | ||
} | ||
String sha256Url = distributionUrl.replace("-bin.zip","").replace("-all.zip", "") + "-wrapper.jar.sha256"; | ||
String checksum = hashProvider.getChecksum(wrapperJar.toFile()); | ||
String remoteChecksum = downloadRequestsCache.getIfPresent(sha256Url); | ||
if (remoteChecksum == null) { | ||
URL url = new URL(sha256Url); | ||
BufferedReader in = null; | ||
try { | ||
URLConnection urlConnection = url.openConnection(); | ||
in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream())); | ||
remoteChecksum = in.readLine(); | ||
downloadRequestsCache.put(sha256Url, remoteChecksum); | ||
} catch (IOException e) { | ||
JavaLanguageServerPlugin.logException("sha256url='" + sha256Url + "'. You can try to add '-Dgradle.wrapper.check.disabled=true' to 'java.jdt.ls.vmargs'", e); | ||
} finally { | ||
if (in != null) { | ||
in.close(); | ||
} | ||
} | ||
} | ||
if (remoteChecksum != null && remoteChecksum.equals(checksum)) { | ||
return null; | ||
} else { | ||
return "Invalid wrapper checksum: local=" + checksum + ",remote=" + remoteChecksum; | ||
} | ||
} | ||
} | ||
|
||
} |
Binary file modified
BIN
-8 Bytes
(100%)
org.eclipse.jdt.ls.tests/projects/gradle/gradle-11/gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
Binary file modified
BIN
-8 Bytes
(100%)
org.eclipse.jdt.ls.tests/projects/gradle/gradle-12/gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
Binary file modified
BIN
-8 Bytes
(100%)
org.eclipse.jdt.ls.tests/projects/gradle/gradle-13/gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
Binary file modified
BIN
-8 Bytes
(100%)
org.eclipse.jdt.ls.tests/projects/gradle/gradle-14/gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
Binary file modified
BIN
-8 Bytes
(100%)
...eclipse.jdt.ls.tests/projects/gradle/gradle-withoutjava/gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
Binary file modified
BIN
-8 Bytes
(100%)
org.eclipse.jdt.ls.tests/projects/gradle/nested/gradle1/gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
Binary file modified
BIN
-8 Bytes
(100%)
org.eclipse.jdt.ls.tests/projects/gradle/nested/gradle2/gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
Binary file modified
BIN
-8 Bytes
(100%)
org.eclipse.jdt.ls.tests/projects/gradle/simple-gradle/gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
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