Skip to content

Commit

Permalink
#3156 = migrate to Preconditions API
Browse files Browse the repository at this point in the history
  • Loading branch information
coiouhkc committed Jun 12, 2023
1 parent b0db570 commit f45204b
Showing 1 changed file with 115 additions and 119 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,132 +43,128 @@
@EqualsAndHashCode(callSuper = true)
public class AddMavenWrapperChecksumValidation extends Recipe {

public static final String MVN_WRAPPER_MAVEN_WRAPPER_PROPERTIES = ".mvn/wrapper/maven-wrapper.properties";
public static final String DISTRIBUTION_URL = "distributionUrl";
public static final String WRAPPER_URL = "wrapperUrl";
public static final String DISTRIBUTION_SHA_256_SUM = "distributionSha256Sum";
public static final String WRAPPER_SHA_256_SUM = "wrapperSha256Sum";

@Override
public String getDisplayName() {
return "Add checksum validation to maven wrapper";
}

@Override
public String getDescription() {
return "Add checksum validation to maven wrapper if used.";
}

@Override
protected TreeVisitor<?, ExecutionContext> getSingleSourceApplicableTest() {
return new HasSourcePath<>(MVN_WRAPPER_MAVEN_WRAPPER_PROPERTIES);
}

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return new PropertiesVisitor<ExecutionContext>() {
@Override
public Properties visitFile(Properties.File file, ExecutionContext context) {
Properties mavenWrapperProperties = super.visitFile(file, context);

if (equalIgnoringSeparators(file.getSourcePath(), Paths.get(MVN_WRAPPER_MAVEN_WRAPPER_PROPERTIES))) {
Set<Properties.Entry> distributionUrls =
FindProperties.find(mavenWrapperProperties, DISTRIBUTION_URL, false);
Set<Properties.Entry> distributionSha256Hexs =
FindProperties.find(mavenWrapperProperties, DISTRIBUTION_SHA_256_SUM, false);

if (distributionUrls.size() == 1 && distributionSha256Hexs.isEmpty()) {
Properties.Entry distributionUrl = distributionUrls.stream().findFirst().get();

try {
File distribution = downloadDistributionAndMarkToDelete(distributionUrl.getValue().getText());

String distributionSha256Hex = sha256AsHex(distribution);

mavenWrapperProperties = addPropertyToProperties(mavenWrapperProperties, DISTRIBUTION_SHA_256_SUM, distributionSha256Hex);
} catch (IOException | NoSuchAlgorithmException e) {
// NOP
}
}
public static final String MVN_WRAPPER_MAVEN_WRAPPER_PROPERTIES = ".mvn/wrapper/maven-wrapper.properties";
public static final String DISTRIBUTION_URL = "distributionUrl";
public static final String WRAPPER_URL = "wrapperUrl";
public static final String DISTRIBUTION_SHA_256_SUM = "distributionSha256Sum";
public static final String WRAPPER_SHA_256_SUM = "wrapperSha256Sum";

@Override
public String getDisplayName() {
return "Add checksum validation to maven wrapper";
}

@Override
public String getDescription() {
return "Add checksum validation to maven wrapper if used.";
}

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return Preconditions.check(
new HasSourcePath<>(MVN_WRAPPER_MAVEN_WRAPPER_PROPERTIES),
new PropertiesVisitor<ExecutionContext>() {
@Override
public Properties visitFile(Properties.File file, ExecutionContext context) {
Properties mavenWrapperProperties = super.visitFile(file, context);

Set<Properties.Entry> wrapperUrls =
FindProperties.find(mavenWrapperProperties, WRAPPER_URL, false);
Set<Properties.Entry> wrapperSha256Hexs =
FindProperties.find(mavenWrapperProperties, WRAPPER_SHA_256_SUM, false);
if (equalIgnoringSeparators(file.getSourcePath(), Paths.get(MVN_WRAPPER_MAVEN_WRAPPER_PROPERTIES))) {
Set<Properties.Entry> distributionUrls =
FindProperties.find(mavenWrapperProperties, DISTRIBUTION_URL, false);
Set<Properties.Entry> distributionSha256Hexs =
FindProperties.find(mavenWrapperProperties, DISTRIBUTION_SHA_256_SUM, false);

if (wrapperUrls.size() == 1 && wrapperSha256Hexs.isEmpty()) {
Properties.Entry wrapperUrl = wrapperUrls.stream().findFirst().get();
if (distributionUrls.size() == 1 && distributionSha256Hexs.isEmpty()) {
Properties.Entry distributionUrl = distributionUrls.stream().findFirst().get();

try {
File wrapper = downloadWrapperAndMarkToDelete(wrapperUrl.getValue().getText());
try {
File distribution = downloadDistributionAndMarkToDelete(distributionUrl.getValue().getText());

String wrapperSha256Hex = sha256AsHex(wrapper);
String distributionSha256Hex = sha256AsHex(distribution);

mavenWrapperProperties = addPropertyToProperties(mavenWrapperProperties, WRAPPER_SHA_256_SUM, wrapperSha256Hex);
} catch (IOException | NoSuchAlgorithmException e) {
// NOP
}
}
}
mavenWrapperProperties = addPropertyToProperties(mavenWrapperProperties, DISTRIBUTION_SHA_256_SUM, distributionSha256Hex);
} catch (IOException | NoSuchAlgorithmException e) {
// NOP
}
}


Set<Properties.Entry> wrapperUrls =
FindProperties.find(mavenWrapperProperties, WRAPPER_URL, false);
Set<Properties.Entry> wrapperSha256Hexs =
FindProperties.find(mavenWrapperProperties, WRAPPER_SHA_256_SUM, false);

if (wrapperUrls.size() == 1 && wrapperSha256Hexs.isEmpty()) {
Properties.Entry wrapperUrl = wrapperUrls.stream().findFirst().get();

try {
File wrapper = downloadWrapperAndMarkToDelete(wrapperUrl.getValue().getText());

String wrapperSha256Hex = sha256AsHex(wrapper);

mavenWrapperProperties = addPropertyToProperties(mavenWrapperProperties, WRAPPER_SHA_256_SUM, wrapperSha256Hex);
} catch (IOException | NoSuchAlgorithmException e) {
// NOP
}
}
}

return mavenWrapperProperties;
}
});
}

private File downloadDistributionAndMarkToDelete(String distributionUrl) throws IOException {
File distribution = File.createTempFile("rewrite", "download");
FileUtils.copyURLToFile(new URL(distributionUrl), distribution, 10_000, 10_000);
distribution.deleteOnExit();
return distribution;
}

private File downloadWrapperAndMarkToDelete(String wrapperUrl) throws IOException {
File distribution = File.createTempFile("rewrite", "download");
FileUtils.copyURLToFile(new URL(wrapperUrl), distribution, 10_000, 10_000);
distribution.deleteOnExit();
return distribution;
}

private Properties addPropertyToProperties(Properties mavenWrapperProperties, String name, String value) {
Properties.Value propertyValue = new Properties.Value(Tree.randomId(), "", Markers.EMPTY, value);
Properties.Entry entry = new Properties.Entry(
Tree.randomId(),
"\n",
Markers.EMPTY,
name,
"",
Properties.Entry.Delimiter.EQUALS,
propertyValue
);
List<Properties.Content> contentList = ListUtils.concat(((Properties.File) mavenWrapperProperties).getContent(), entry);
mavenWrapperProperties = ((Properties.File) mavenWrapperProperties).withContent(contentList);
return mavenWrapperProperties;
}
};
}

private File downloadDistributionAndMarkToDelete(String distributionUrl) throws IOException {
File distribution = File.createTempFile("rewrite", "download");
FileUtils.copyURLToFile(new URL(distributionUrl), distribution, 10_000, 10_000);
distribution.deleteOnExit();
return distribution;
}

private File downloadWrapperAndMarkToDelete(String wrapperUrl) throws IOException {
File distribution = File.createTempFile("rewrite", "download");
FileUtils.copyURLToFile(new URL(wrapperUrl), distribution, 10_000, 10_000);
distribution.deleteOnExit();
return distribution;
}

private Properties addPropertyToProperties(Properties mavenWrapperProperties, String name, String value) {
Properties.Value propertyValue = new Properties.Value(Tree.randomId(), "", Markers.EMPTY, value);
Properties.Entry entry = new Properties.Entry(
Tree.randomId(),
"\n",
Markers.EMPTY,
name,
"",
Properties.Entry.Delimiter.EQUALS,
propertyValue
);
List<Properties.Content> contentList = ListUtils.concat(((Properties.File) mavenWrapperProperties).getContent(), entry);
mavenWrapperProperties = ((Properties.File) mavenWrapperProperties).withContent(contentList);
return mavenWrapperProperties;
}

/**
* Compute SHA256 of file.
*
* @param file file
* @return SHA256 as hex
*
* @see <a href="https://www.baeldung.com/sha-256-hashing-java">source</a>
*/
private String sha256AsHex(File file) throws NoSuchAlgorithmException, IOException {

MessageDigest digest = MessageDigest.getInstance("SHA-256");
byte[] hash = digest.digest(
FileUtils.readFileToByteArray(file));

StringBuilder hexString = new StringBuilder(2 * hash.length);
for (byte b : hash) {
String hex = Integer.toHexString(0xff & b);
if (hex.length() == 1) {
hexString.append('0');
}
hexString.append(hex);
}
return hexString.toString();
}

/**
* Compute SHA256 of file.
*
* @param file file
* @return SHA256 as hex
* @see <a href="https://www.baeldung.com/sha-256-hashing-java">source</a>
*/
private String sha256AsHex(File file) throws NoSuchAlgorithmException, IOException {

MessageDigest digest = MessageDigest.getInstance("SHA-256");
byte[] hash = digest.digest(
FileUtils.readFileToByteArray(file));

StringBuilder hexString = new StringBuilder(2 * hash.length);
for (byte b : hash) {
String hex = Integer.toHexString(0xff & b);
if (hex.length() == 1) {
hexString.append('0');
}
hexString.append(hex);
}
return hexString.toString();
}
}

0 comments on commit f45204b

Please sign in to comment.