Skip to content

Commit

Permalink
Check gradle-wrapper.jar
Browse files Browse the repository at this point in the history
Signed-off-by: Snjezana Peco <[email protected]>
  • Loading branch information
snjeza committed May 5, 2020
1 parent f5d4616 commit 8df1aed
Show file tree
Hide file tree
Showing 13 changed files with 174 additions and 3 deletions.
5 changes: 3 additions & 2 deletions org.eclipse.jdt.ls.core/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ Export-Package: org.eclipse.jdt.ls.core.internal;x-friends:="org.eclipse.jdt.ls.
org.eclipse.jdt.ls.core.internal.commands;x-friends:="org.eclipse.jdt.ls.tests",
org.eclipse.jdt.ls.core.internal.contentassist;x-friends:="org.eclipse.jdt.ls.tests",
org.eclipse.jdt.ls.core.internal.corext.codemanipulation;x-friends:="org.eclipse.jdt.ls.tests",
org.eclipse.jdt.ls.core.internal.corext.template.java;x-friends:="org.eclipse.jdt.ls.tests",
org.eclipse.jdt.ls.core.internal.corext.dom;x-internal:=true,
org.eclipse.jdt.ls.core.internal.corext.refactoring;x-internal:=true,
org.eclipse.jdt.ls.core.internal.corext.refactoring.changes;x-internal:=true,
Expand All @@ -46,6 +45,7 @@ Export-Package: org.eclipse.jdt.ls.core.internal;x-friends:="org.eclipse.jdt.ls.
org.eclipse.jdt.ls.core.internal.corext.refactoring.reorg;x-internal:=true,
org.eclipse.jdt.ls.core.internal.corext.refactoring.tagging;x-internal:=true,
org.eclipse.jdt.ls.core.internal.corext.refactoring.util;x-internal:=true,
org.eclipse.jdt.ls.core.internal.corext.template.java;x-friends:="org.eclipse.jdt.ls.tests",
org.eclipse.jdt.ls.core.internal.corext.util;x-internal:=true,
org.eclipse.jdt.ls.core.internal.corrections;x-internal:=true,
org.eclipse.jdt.ls.core.internal.corrections.proposals;x-internal:=true,
Expand All @@ -56,9 +56,10 @@ Export-Package: org.eclipse.jdt.ls.core.internal;x-friends:="org.eclipse.jdt.ls.
org.eclipse.jdt.ls.core.internal.lsp;x-friends:="org.eclipse.jdt.ls.tests",
org.eclipse.jdt.ls.core.internal.managers;x-friends:="org.eclipse.jdt.ls.tests,org.eclipse.jdt.ls.tests.syntaxserver",
org.eclipse.jdt.ls.core.internal.preferences;x-friends:="org.eclipse.jdt.ls.tests,org.eclipse.jdt.ls.tests.syntaxserver",
org.eclipse.jdt.ls.core.internal.semantictokens;x-friends:="org.eclipse.jdt.ls.tests",
org.eclipse.jdt.ls.core.internal.syntaxserver;x-friends:="org.eclipse.jdt.ls.tests.syntaxserver",
org.eclipse.jdt.ls.core.internal.text.correction;x-friends:="org.eclipse.jdt.ls.tests",
org.eclipse.jdt.ls.core.internal.semantictokens;x-friends:="org.eclipse.jdt.ls.tests"
org.eclipse.jdt.ls.internal.gradle.checksums;x-friends:="org.eclipse.jdt.ls.tests"
Bundle-ClassPath: lib/jsoup-1.9.2.jar,
lib/remark-1.2.0.jar,
.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
Expand All @@ -39,6 +41,7 @@
import org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin;
import org.eclipse.jdt.ls.core.internal.ProjectUtils;
import org.eclipse.jdt.ls.core.internal.preferences.PreferenceManager;
import org.eclipse.jdt.ls.internal.gradle.checksums.WrapperValidator;

/**
* @author Fred Bricon
Expand Down Expand Up @@ -105,7 +108,18 @@ private void importDir(Path rootFolder, IProgressMonitor monitor) {

public static GradleDistribution getGradleDistribution(Path rootFolder) {
if (JavaLanguageServerPlugin.getPreferencesManager() != null && JavaLanguageServerPlugin.getPreferencesManager().getPreferences().isGradleWrapperEnabled() && Files.exists(rootFolder.resolve("gradlew"))) {
return GradleDistribution.fromBuild();
WrapperValidator validator = new WrapperValidator();
try {
String result = validator.checkWrapper(rootFolder.toFile().getAbsolutePath());
if (result == null) {
WrapperGradleDistribution gradleDistribution = GradleDistribution.fromBuild();
return gradleDistribution;
} else {
JavaLanguageServerPlugin.logInfo(result);
}
} catch (NoSuchAlgorithmException | IOException e) {
JavaLanguageServerPlugin.logException(e.getMessage(), e);
}
}
if (JavaLanguageServerPlugin.getPreferencesManager() != null && JavaLanguageServerPlugin.getPreferencesManager().getPreferences().getGradleVersion() != null) {
List<GradleVersion> versions = CorePlugin.publishedGradleVersions().getVersions();
Expand Down
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();
}

}
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 not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import org.eclipse.jdt.ls.core.internal.WorkspaceHelper;
import org.eclipse.jdt.ls.core.internal.managers.ProjectsManager.CHANGE_TYPE;
import org.eclipse.jdt.ls.core.internal.preferences.Preferences.FeatureStatus;
import org.eclipse.jdt.ls.internal.gradle.checksums.WrapperValidator;
import org.junit.After;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand Down Expand Up @@ -158,6 +159,14 @@ public void testDisableGradleWrapper() throws Exception {
}
}

@Test
public void testGradleWrapper() throws Exception {
File file = new File(getSourceProjectDirectory(), "gradle/simple-gradle");
assertTrue(file.isDirectory());
String result = new WrapperValidator().checkWrapper(file.getAbsolutePath());
assertNull(result);
}

@Test
public void testGradleUserHome() throws Exception {
String gradleUserHomePreference = JavaLanguageServerPlugin.getPreferencesManager().getPreferences().getGradleUserHome();
Expand Down

0 comments on commit 8df1aed

Please sign in to comment.