Skip to content

Commit

Permalink
Restrict access to Legacy Gradle plugins (#99581)
Browse files Browse the repository at this point in the history
This ensures new projects will not use legacy gradle plugins
  • Loading branch information
breskeby committed Sep 18, 2023
1 parent 6c26ca0 commit 9cda6d1
Show file tree
Hide file tree
Showing 9 changed files with 285 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ class TestingConventionsPrecommitPluginFuncTest extends AbstractGradleInternalPl

def "applies conventions on yaml-rest-test tests"() {
given:
buildApiRestrictionsDisabled = true
clazz(dir('src/yamlRestTest/java'), "org.elasticsearch.test.rest.yaml.ESClientYamlSuiteTestCase")
buildFile << """
apply plugin:'elasticsearch.legacy-yaml-rest-test'
Expand Down Expand Up @@ -211,6 +212,7 @@ class TestingConventionsPrecommitPluginFuncTest extends AbstractGradleInternalPl
@Unroll
def "applies conventions on #sourceSetName tests"() {
given:
buildApiRestrictionsDisabled = pluginName.contains('legacy')
clazz(dir("src/${sourceSetName}/java"), "org.elasticsearch.test.ESIntegTestCase")
clazz(dir("src/${sourceSetName}/java"), "org.elasticsearch.test.rest.ESRestTestCase")
buildFile << """
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ class LegacyYamlRestCompatTestPluginFuncTest extends AbstractRestResourcesFuncTe
// 1. TestClustersPlugin not cc compatible due to listener registration
// 2. RestIntegTestTask not cc compatible due to
configurationCacheCompatible = false
buildApiRestrictionsDisabled = true
}

def "yamlRestTestVxCompatTest does nothing when there are no tests"() {
given:
subProject(":distribution:bwc:maintenance") << """
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ import org.gradle.testkit.runner.TaskOutcome
@IgnoreIf({ os.isWindows() })
class LegacyYamlRestTestPluginFuncTest extends AbstractRestResourcesFuncTest {

def setup() {
buildApiRestrictionsDisabled = true
}


def "yamlRestTest does nothing when there are no tests"() {
given:
// RestIntegTestTask not cc compatible due to
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.elasticsearch.gradle.internal.ElasticsearchTestBasePlugin;
import org.elasticsearch.gradle.internal.FixtureStop;
import org.elasticsearch.gradle.internal.InternalTestClustersPlugin;
import org.elasticsearch.gradle.internal.RestrictedBuildApiService;
import org.elasticsearch.gradle.internal.precommit.InternalPrecommitTasks;
import org.elasticsearch.gradle.test.SystemPropertyCommandLineArgumentProvider;
import org.elasticsearch.gradle.testclusters.ElasticsearchCluster;
Expand All @@ -22,12 +23,14 @@
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.plugins.JavaBasePlugin;
import org.gradle.api.provider.Provider;
import org.gradle.api.provider.ProviderFactory;
import org.gradle.api.tasks.Sync;
import org.gradle.api.tasks.bundling.Zip;

import javax.inject.Inject;

import static org.elasticsearch.gradle.internal.RestrictedBuildApiService.BUILD_API_RESTRICTIONS_SYS_PROPERTY;
import static org.elasticsearch.gradle.plugin.BasePluginBuildPlugin.BUNDLE_PLUGIN_TASK_NAME;
import static org.elasticsearch.gradle.plugin.BasePluginBuildPlugin.EXPLODED_BUNDLE_PLUGIN_TASK_NAME;

Expand All @@ -52,6 +55,12 @@ public LegacyRestTestBasePlugin(ProviderFactory providerFactory) {

@Override
public void apply(Project project) {
Provider<RestrictedBuildApiService> serviceProvider = project.getGradle()
.getSharedServices()
.registerIfAbsent("restrictedBuildAPI", RestrictedBuildApiService.class, spec -> {
spec.getParameters().getDisabled().set(Boolean.getBoolean(BUILD_API_RESTRICTIONS_SYS_PROPERTY));
});
serviceProvider.get().failOnUsageRestriction(getClass(), project);
project.getPluginManager().apply(ElasticsearchJavaBasePlugin.class);
project.getPluginManager().apply(ElasticsearchTestBasePlugin.class);
project.getPluginManager().apply(InternalTestClustersPlugin.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,12 @@
import org.elasticsearch.gradle.Version;
import org.elasticsearch.gradle.VersionProperties;
import org.elasticsearch.gradle.internal.ElasticsearchJavaBasePlugin;
import org.elasticsearch.gradle.internal.test.LegacyRestTestBasePlugin;
import org.elasticsearch.gradle.internal.test.rest.CopyRestApiTask;
import org.elasticsearch.gradle.internal.test.rest.CopyRestTestsTask;
import org.elasticsearch.gradle.internal.test.rest.LegacyYamlRestTestPlugin;
import org.elasticsearch.gradle.internal.test.rest.RestResourcesExtension;
import org.elasticsearch.gradle.internal.test.rest.RestResourcesPlugin;
import org.elasticsearch.gradle.testclusters.TestClustersPlugin;
import org.elasticsearch.gradle.test.YamlRestTestPlugin;
import org.elasticsearch.gradle.util.GradleUtils;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
Expand Down Expand Up @@ -77,20 +76,17 @@ public void apply(Project project) {
final Path compatRestResourcesDir = Path.of("restResources").resolve("v" + COMPATIBLE_VERSION);
final Path compatSpecsDir = compatRestResourcesDir.resolve("yamlSpecs");
final Path compatTestsDir = compatRestResourcesDir.resolve("yamlTests");

project.getPluginManager().apply(getBasePlugin());
project.getPluginManager().apply(ElasticsearchJavaBasePlugin.class);
project.getPluginManager().apply(TestClustersPlugin.class);
project.getPluginManager().apply(LegacyRestTestBasePlugin.class);
project.getPluginManager().apply(RestResourcesPlugin.class);
project.getPluginManager().apply(getBasePlugin());

RestResourcesExtension extension = project.getExtensions().getByType(RestResourcesExtension.class);

// create source set
SourceSetContainer sourceSets = project.getExtensions().getByType(SourceSetContainer.class);
SourceSet yamlCompatTestSourceSet = sourceSets.create(SOURCE_SET_NAME);
SourceSet yamlTestSourceSet = sourceSets.getByName(LegacyYamlRestTestPlugin.SOURCE_SET_NAME);
GradleUtils.extendSourceSet(project, LegacyYamlRestTestPlugin.SOURCE_SET_NAME, SOURCE_SET_NAME);
SourceSet yamlTestSourceSet = sourceSets.getByName(YamlRestTestPlugin.YAML_REST_TEST);
GradleUtils.extendSourceSet(project, YamlRestTestPlugin.YAML_REST_TEST, SOURCE_SET_NAME);

// copy compatible rest specs
Configuration bwcMinorConfig = project.getConfigurations().create(BWC_MINOR_CONFIG_NAME);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
package org.elasticsearch.gradle.fixtures

import org.apache.commons.io.FileUtils
import org.elasticsearch.gradle.internal.test.ConfigurationCacheCompatibleAwareGradleRunner
import org.elasticsearch.gradle.internal.test.BuildConfigurationAwareGradleRunner
import org.elasticsearch.gradle.internal.test.InternalAwareGradleRunner
import org.elasticsearch.gradle.internal.test.NormalizeOutputGradleRunner
import org.elasticsearch.gradle.internal.test.TestResultExtension
Expand Down Expand Up @@ -39,7 +39,8 @@ abstract class AbstractGradleFuncTest extends Specification {
File propertiesFile
File projectDir

boolean configurationCacheCompatible = true
protected boolean configurationCacheCompatible = true
protected boolean buildApiRestrictionsDisabled = false

def setup() {
projectDir = testProjectDir.root
Expand Down Expand Up @@ -79,16 +80,17 @@ abstract class AbstractGradleFuncTest extends Specification {

GradleRunner gradleRunner(File projectDir, Object... arguments) {
return new NormalizeOutputGradleRunner(
new ConfigurationCacheCompatibleAwareGradleRunner(
new BuildConfigurationAwareGradleRunner(
new InternalAwareGradleRunner(
GradleRunner.create()
.withDebug(ManagementFactory.getRuntimeMXBean().getInputArguments()
.toString().indexOf("-agentlib:jdwp") > 0
)
.withProjectDir(projectDir)
.withPluginClasspath()
.forwardOutput()
), configurationCacheCompatible),
GradleRunner.create()
.withDebug(ManagementFactory.getRuntimeMXBean().getInputArguments()
.toString().indexOf("-agentlib:jdwp") > 0
)
.withProjectDir(projectDir)
.withPluginClasspath()
.forwardOutput()
), configurationCacheCompatible,
buildApiRestrictionsDisabled)
).withArguments(arguments.collect { it.toString() })
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

package org.elasticsearch.gradle.fixtures

import org.elasticsearch.gradle.internal.test.InternalAwareGradleRunner

class RestrictedBuildServiceAwareGradleRunner {
RestrictedBuildServiceAwareGradleRunner(InternalAwareGradleRunner delegate) {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,19 @@
import java.util.Map;

/**
* A Gradle runner that delegates to another runner, optionally enabling the configuring cache parameter.
* A Gradle runner that delegates to another runner, optionally configuring
* - the configuring cache parameter
* - the internal restricted build api service
*/
public class ConfigurationCacheCompatibleAwareGradleRunner extends GradleRunner {
private GradleRunner delegate;
private boolean ccCompatible;
public class BuildConfigurationAwareGradleRunner extends GradleRunner {
private final GradleRunner delegate;
private final boolean ccCompatible;
private final boolean buildApiRestrictionsDisabled;

public ConfigurationCacheCompatibleAwareGradleRunner(GradleRunner delegate, boolean ccCompatible) {
public BuildConfigurationAwareGradleRunner(GradleRunner delegate, boolean ccCompatible, boolean buildApiRestrictionsDisabled) {
this.delegate = delegate;
this.ccCompatible = ccCompatible;
this.buildApiRestrictionsDisabled = buildApiRestrictionsDisabled;
}

@Override
Expand Down Expand Up @@ -76,11 +80,13 @@ public List<String> getArguments() {

@Override
public GradleRunner withArguments(List<String> arguments) {
List<String> effectiveArgs = arguments;
List<String> effectiveArgs = new ArrayList<>(arguments);
if (ccCompatible) {
effectiveArgs = new ArrayList<>(arguments);
effectiveArgs.add("--configuration-cache");
}
if (buildApiRestrictionsDisabled) {
effectiveArgs.add("-Dorg.elasticsearch.gradle.build-api-restriction.disabled=" + buildApiRestrictionsDisabled);
}
delegate.withArguments(effectiveArgs);
return this;
}
Expand Down

0 comments on commit 9cda6d1

Please sign in to comment.