Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Gradle JDK automanagment] GradleWrapperMain patching #341

Merged
merged 73 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
73 commits
Select commit Hold shift + click to select a range
bbc0406
wip working
crogoz Mar 8, 2024
e9a049f
address some comments
crogoz Mar 13, 2024
f39dae0
discover app_home & rm insecure curl
crogoz Mar 14, 2024
681d482
reuse logic
crogoz Mar 14, 2024
36d77f2
move integration tests & refactor
crogoz Mar 15, 2024
072e15b
fix & test
crogoz Mar 15, 2024
4f75484
fix
crogoz Mar 18, 2024
d49fa30
fix certificate
crogoz Mar 19, 2024
48dd039
local test & blocking locks
crogoz Mar 19, 2024
9de3b3a
use java instead to move jdk & set up certs
crogoz Mar 19, 2024
8a7c230
renames & gradle_user_home & configurable certs
crogoz Mar 20, 2024
c1eface
testing on ubuntu
crogoz Mar 20, 2024
3063421
log nonExisting certs
crogoz Mar 20, 2024
5e97259
cleanup
crogoz Mar 20, 2024
b2575df
wip working locally
crogoz Mar 21, 2024
e940d88
fixing tests
crogoz Mar 21, 2024
26c1dd0
only when run locally
crogoz Mar 21, 2024
5d4035b
cleanup
crogoz Mar 21, 2024
b035967
fix checkstyle
crogoz Mar 21, 2024
28942d1
Add generated changelog entries
svc-changelog Mar 21, 2024
72523ef
address comments
crogoz Mar 22, 2024
ab4d87e
fixes
crogoz Mar 22, 2024
f45b357
fix
crogoz Mar 22, 2024
68ebd35
template & fix
crogoz Mar 22, 2024
f37727f
fix ldd
crogoz Mar 22, 2024
e04d97e
test & match CurrentOs
crogoz Mar 25, 2024
a756908
test & match CurrentOs
crogoz Mar 25, 2024
422a69c
wip
crogoz Mar 25, 2024
9df726b
test
crogoz Mar 26, 2024
7ace1a5
fix test names
crogoz Mar 26, 2024
3ed5098
Merge branch 'cr/gradle-jdk-script' into cr/wrapper-patching
crogoz Mar 26, 2024
b32f333
wip
crogoz Mar 27, 2024
227d4d7
wip
crogoz Mar 27, 2024
2ad913f
classpaths ./gradlew test
crogoz Mar 28, 2024
b7aca82
load classpath
crogoz Mar 28, 2024
458d53e
.
crogoz Mar 28, 2024
5483cc3
fix test
crogoz Mar 28, 2024
607293b
split
crogoz Mar 28, 2024
f19cb2d
move integration tests
crogoz Mar 28, 2024
7933d69
tests
crogoz Mar 28, 2024
e580952
address comments
crogoz Apr 2, 2024
d5fbb1f
Merge branch 'develop' into cr/wrapper-patching
crogoz Apr 2, 2024
3a49ce4
Excavator: Upgrade dependencies (#339)
crogoz Apr 2, 2024
3b838d3
Excavator: Upgrades Baseline to the latest version (#340)
svc-excavator-bot Mar 30, 2024
6ee7cb8
Merge branch 'cr/wrapper-patching' of github.com:palantir/gradle-jdks…
crogoz Apr 2, 2024
a8a2b76
test works
crogoz Apr 4, 2024
68f0889
Merge branch 'develop' into cr/patching2
crogoz Apr 4, 2024
027406f
test works
crogoz Apr 5, 2024
1d0e1b4
.
crogoz Apr 8, 2024
7ddb086
fix tests
crogoz Apr 8, 2024
a0ef1cc
split gradle-jdks-commons & 2 step patched jar
crogoz Apr 9, 2024
488122e
all jdks
crogoz Apr 9, 2024
30d290d
fix tests
crogoz Apr 10, 2024
b1512b6
versions
crogoz Apr 10, 2024
8e0ed7b
Merge branch 'develop' into cr/patching2
crogoz Apr 10, 2024
480b7af
tests toolchains
crogoz Apr 11, 2024
ba9ff4b
comment
crogoz Apr 11, 2024
1920ab3
message not present all the time
crogoz Apr 11, 2024
b520f8e
messages based on gradle versions
crogoz Apr 11, 2024
2b249aa
local gradle-baseline bump
crogoz Apr 22, 2024
93f81af
no windows
crogoz Apr 22, 2024
c354fbc
fix
crogoz Apr 23, 2024
d2a25b4
fix
crogoz Apr 23, 2024
aaf2126
jdk extension created
crogoz Apr 23, 2024
27aa008
.
crogoz Apr 24, 2024
0d939b7
bump gradle-jdks
crogoz May 2, 2024
1b0f867
fix test
crogoz May 2, 2024
8717b3d
changelog
crogoz May 3, 2024
838a200
Merge branch 'develop' into cr/patching2
crogoz May 3, 2024
7e2cab0
rename gradle-daemon-jdk-version
crogoz May 7, 2024
d08cb21
address comments
crogoz May 8, 2024
e98404b
move the jar to gradle dir
crogoz May 8, 2024
c8b37a5
Merge branch 'develop' into cr/patching2
crogoz May 17, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions changelog/@unreleased/pr-341.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type: improvement
improvement:
description: '[Gradle JDK automanagment] GradleWrapperMain patching'
links:
- https://github.com/palantir/gradle-jdks/pull/341
1 change: 1 addition & 0 deletions gradle-jdks-distributions/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ apply plugin: 'java'
apply plugin: 'com.palantir.external-publish-jar'

dependencies {
api project(':gradle-jdks-setup-common')
api 'com.fasterxml.jackson.core:jackson-annotations'

testImplementation 'org.junit.jupiter:junit-jupiter'
Expand Down
10 changes: 10 additions & 0 deletions gradle-jdks-setup-common/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
apply plugin: 'java'
apply plugin: 'com.palantir.external-publish-jar'

dependencies {
// Avoid adding large compile dependencies here, as they will also be added in the gradle-jdks-setup-all*.jar using the fatJar task
implementation 'com.fasterxml.jackson.core:jackson-annotations'

testImplementation platform('org.junit:junit-bom:5.9.1')
testImplementation 'org.junit.jupiter:junit-jupiter'
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,16 @@
* limitations under the License.
*/

package com.palantir.gradle.jdks.setup;
package com.palantir.gradle.jdks;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
Expand All @@ -32,9 +34,17 @@
public final class CommandRunner {

public static String run(List<String> commandArguments) {
return run(commandArguments, Optional.empty());
}

public static String run(List<String> commandArguments, Optional<File> directory) {
ExecutorService executorService = Executors.newFixedThreadPool(2);
try {
Process process = new ProcessBuilder().command(commandArguments).start();
ProcessBuilder processBuilder = new ProcessBuilder().command(commandArguments);
if (directory.isPresent()) {
processBuilder.directory(directory.get());
}
Process process = processBuilder.start();
CompletableFuture<String> outputFuture =
CompletableFuture.supplyAsync(() -> readAllInput(process.getInputStream()), executorService);
CompletableFuture<String> errorOutputFuture =
Expand Down Expand Up @@ -64,7 +74,7 @@ public static String run(List<String> commandArguments) {
}
}

static String readAllInput(InputStream inputStream) {
public static String readAllInput(InputStream inputStream) {
try (Stream<String> lines =
new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8)).lines()) {
return lines.collect(Collectors.joining("\n"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,12 @@

package com.palantir.gradle.jdks;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.function.UnaryOperator;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public final class CurrentOs {
public static Os get() {
Expand Down Expand Up @@ -61,8 +56,8 @@ static Os linuxLibcFromLdd(UnaryOperator<List<String>> argTransformer) {
// Extremely frustratingly, musl `ldd` exits with code 1 on --version, and prints to stderr, unlike the more
// reasonable glibc, which exits with code 0 and prints to stdout. So we concat stdout and stderr together,
// check the output for the correct strings, then fail if we can't find it.
String lowercaseOutput = (readAllInput(process.getInputStream()) + "\n"
+ readAllInput(process.getErrorStream()))
String lowercaseOutput = (CommandRunner.readAllInput(process.getInputStream()) + "\n"
+ CommandRunner.readAllInput(process.getErrorStream()))
.toLowerCase(Locale.ROOT);

int secondsToWait = 5;
Expand Down Expand Up @@ -92,11 +87,5 @@ static Os linuxLibcFromLdd(UnaryOperator<List<String>> argTransformer) {
}
}

private static String readAllInput(InputStream inputStream) {
try (Stream<String> lines = new BufferedReader(new InputStreamReader(inputStream)).lines()) {
return lines.collect(Collectors.joining("\n"));
}
}

private CurrentOs() {}
}
17 changes: 16 additions & 1 deletion gradle-jdks-setup/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ testSets {
}

dependencies {
// Avoid adding compile dependencies here, as they will also need to be added in the gradle-jdks-setup*.jar
// Avoid adding large compile dependencies here, as they will also be added in the gradle-jdks-setup-all*.jar using the fatJar task
implementation project(':gradle-jdks-setup-common')

testImplementation 'org.junit.jupiter:junit-jupiter'
testImplementation 'org.assertj:assertj-core'
Expand All @@ -21,10 +22,24 @@ dependencies {
integrationTestImplementation 'org.assertj:assertj-core'
}

tasks.register('fatJar', Jar) {
dependsOn tasks.jar, ':gradle-jdks-setup-common:jar'
manifest {
attributes 'Main-Class': 'com.palantir.gradle.jdks.setup.GradleJdkInstallationSetup'
}
archiveBaseName = 'gradle-jdks-setup-all'
duplicatesStrategy = DuplicatesStrategy.FAIL
from { configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) } }
with jar
}

tasks.withType(JavaCompile) {
options.errorprone.disable 'PreferSafeLoggableExceptions'
}

tasks.integrationTest {
environment("PROJECT_VERSION", project.version)
dependsOn tasks.build
}

tasks.build.dependsOn tasks.fatJar
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How are we going to ship the fat jar to people? I guess we don't have the code in the plugin to do this yet (?), but I imagine we're either going to include the fat jar as a resource or download it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct, we don't have yet the code in the plugin to do this yet.
I think it needs to be distributed alongside "./gradle-jdks.setup.sh" and the "gradle/" files. I was thinking of downloading it (similar how the wrapper.jar gets added to a project). I'll do this in the next PR.

Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.google.common.collect.Iterables;
import com.palantir.gradle.jdks.AmazonCorrettoJdkDistribution;
import com.palantir.gradle.jdks.Arch;
import com.palantir.gradle.jdks.CommandRunner;
import com.palantir.gradle.jdks.CurrentArch;
import com.palantir.gradle.jdks.JdkPath;
import com.palantir.gradle.jdks.JdkRelease;
Expand All @@ -43,7 +44,7 @@ public class GradleJdkInstallationSetupIntegrationTest {
new BigInteger("143266978916655856878034712317230054538369994");
private static final String AMAZON_CERT_ALIAS = "AmazonRootCA1Test";
private static final String NON_EXISTING_CERT_ALIAS = "NonExistingCert";
private static final String SUCCESSFUL_OUTPUT = "Successfully installed JDK distribution, setting JAVA_HOME to";
private static final String SUCCESSFUL_OUTPUT = "Successfully installed JDK distribution in";
private static final String JDK_VERSION = "11.0.21.9.1";
private static final Arch ARCH = CurrentArch.get();
private static final String TEST_HASH = "integration-tests";
Expand Down Expand Up @@ -91,14 +92,15 @@ private Path setupGradleDirectoryStructure(String jdkVersion, Os os) throws IOEx
* │ │ │ │ │ ├── local-path
* │ ├── certs/
* │ │ ├── Palantir3rdGenRootCa.serial-number
* │ ├── gradle-jdk-major-version
* │ ├── gradle-daemon-jdk-version
* │ ├── gradle-jdks-setup.sh
* │ ├── gradle-jdks-setup.jar
* ├── subProjects/...
* ...
*/
String jdkMajorVersion = Iterables.get(Splitter.on('.').split(jdkVersion), 0);
Path gradleDirectory = Files.createDirectories(workingDir.resolve("gradle"));
Path gradleJdkVersion = Files.createFile(gradleDirectory.resolve("gradle-jdk-major-version"));
Path gradleJdkVersion = Files.createFile(gradleDirectory.resolve("gradle-daemon-jdk-version"));
writeFileContent(gradleJdkVersion, jdkMajorVersion.toString());
JdkPath jdkPath = CORRETTO_JDK_DISTRIBUTION.path(
JdkRelease.builder().version(jdkVersion).os(os).arch(ARCH).build());
Expand All @@ -124,9 +126,9 @@ private Path setupGradleDirectoryStructure(String jdkVersion, Os os) throws IOEx
// copy the jar from build/libs to the gradle directory
Files.copy(
Path.of(String.format(
"../gradle-jdks-setup/build/libs/gradle-jdks-setup-%s.jar",
"../gradle-jdks-setup/build/libs/gradle-jdks-setup-all-%s.jar",
System.getenv().get("PROJECT_VERSION"))),
gradleDirectory.resolve("jdks/gradle-jdks-setup.jar"));
gradleDirectory.resolve("gradle-jdks-setup.jar"));

// copy the gradle-jdks-setup.sh to the gradle directory
Files.copy(
Expand Down Expand Up @@ -198,7 +200,7 @@ private static void assertJdkWithNoCertsWasSetUp(String output) {
String expectedDistributionPath =
String.format("/root/.gradle/gradle-jdks/amazon-corretto-%s-%s", JDK_VERSION, TEST_HASH);
assertThat(output)
.contains(SUCCESSFUL_OUTPUT)
.contains(String.format("%s %s", SUCCESSFUL_OUTPUT, expectedDistributionPath))
.contains(String.format("Java home is: %s", expectedDistributionPath))
.containsPattern(String.format("Java path is: java is ([^/]*\\s)*%s", expectedDistributionPath))
.contains(String.format("Java version is: %s", getJavaVersion(JDK_VERSION)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

package com.palantir.gradle.jdks.setup;

import com.palantir.gradle.jdks.CommandRunner;
import com.palantir.gradle.jdks.CurrentOs;
import com.palantir.gradle.jdks.Os;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
Expand All @@ -33,7 +36,6 @@
import java.util.Base64;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -66,13 +68,20 @@ public void maybeImportCertsInJdk(Path jdkInstallationDirectory, Map<String, Str
}

private void importCertInJdk(AliasContentCert aliasContentCert, Path jdkInstallationDirectory) {
String osName = System.getProperty("os.name").toLowerCase(Locale.ROOT);
if (osName.startsWith("mac") || osName.startsWith("linux")) {
unixImportCertInJdk(aliasContentCert, jdkInstallationDirectory);
logger.log(String.format(
"Successfully imported CA certificate %s into the JDK truststore", aliasContentCert.getAlias()));
} else {
logger.logError(String.format("Importing certificates for OS type '%s' is not yet supported", osName));
Os os = CurrentOs.get();
switch (os) {
case MACOS:
case LINUX_GLIBC:
case LINUX_MUSL:
unixImportCertInJdk(aliasContentCert, jdkInstallationDirectory);
logger.log(String.format(
"Successfully imported CA certificate %s into the JDK truststore",
aliasContentCert.getAlias()));
break;
case WINDOWS:
logger.logError(
String.format("Importing certificates for OS type '%s' is not yet supported", os.uiName()));
break;
}
}

Expand Down Expand Up @@ -132,18 +141,21 @@ private static boolean isCertificateInTruststore(Path jdkInstallationDirectory,
}

private Optional<byte[]> systemCertificates() {
String osName = System.getProperty("os.name").toLowerCase(Locale.ROOT);
if (osName.startsWith("mac")) {
return Optional.of(macosSystemCertificates());
} else if (osName.startsWith("linux")) {
return linuxSystemCertificates();
} else {
logger.logError(String.format(
"Not attempting to read Palantir CA from system truststore "
+ "as OS type '%s' does not yet support this",
osName));
return Optional.empty();
Os os = CurrentOs.get();
switch (os) {
case MACOS:
return Optional.of(macosSystemCertificates());
case LINUX_MUSL:
case LINUX_GLIBC:
return linuxSystemCertificates();
case WINDOWS:
logger.logError(String.format(
"Not attempting to read Palantir CA from system truststore "
+ "as OS type '%s' does not yet support this",
os.uiName()));
return Optional.empty();
}
throw new IllegalStateException("Unreachable code; all Os enum values should be handled");
}

private static byte[] macosSystemCertificates() {
Expand Down