Skip to content

Add Boolean#parseBoolean to forbidden-apis #129684

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

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ java.nio.channels.SocketChannel#connect(java.net.SocketAddress)
# org.elasticsearch.core.Booleans#parseBoolean(java.lang.String) directly on the string.
@defaultMessage use org.elasticsearch.core.Booleans#parseBoolean(java.lang.String)
java.lang.Boolean#getBoolean(java.lang.String)
java.lang.Boolean#parseBoolean(java.lang.String)
java.lang.Boolean#valueOf(java.lang.String)

org.apache.lucene.util.IOUtils @ use @org.elasticsearch.core.internal.io instead

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

package org.elasticsearch.core.internal.provider;

import org.elasticsearch.core.SuppressForbidden;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
Expand Down Expand Up @@ -465,6 +467,7 @@ private static CodeSource codeSource(URL baseURL, String jarName) throws Malform
return new CodeSource(new URL(baseURL, jarName), (CodeSigner[]) null /*signers*/);
}

@SuppressForbidden(reason = "accept lenient manifest attributes")
private static boolean isMultiRelease(ClassLoader parent, String jarPrefix) throws IOException {
try (InputStream is = parent.getResourceAsStream(jarPrefix + "/META-INF/MANIFEST.MF")) {
if (is != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

package org.elasticsearch.entitlement.tools.publiccallersfinder;

import org.elasticsearch.core.Booleans;
import org.elasticsearch.core.SuppressForbidden;
import org.elasticsearch.entitlement.tools.ExternalAccess;
import org.elasticsearch.entitlement.tools.Utils;
Expand Down Expand Up @@ -191,7 +192,7 @@ private static void parseCsv(Path csvPath, MethodDescriptorConsumer methodConsum

public static void main(String[] args) throws IOException {
var csvFilePath = Path.of(args[0]);
boolean bubbleUpFromPublic = args.length >= 2 && Boolean.parseBoolean(args[1]);
boolean bubbleUpFromPublic = args.length >= 2 && Booleans.parseBoolean(args[1]);
parseCsv(csvFilePath, (method, module, access) -> identifyTopLevelEntryPoints(method, module, access, bubbleUpFromPublic));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,9 @@ static boolean isMacOrLinuxAarch64() {
/** -Dorg.elasticsearch.nativeaccess.enableVectorLibrary=false to disable.*/
static final String ENABLE_JDK_VECTOR_LIBRARY = "org.elasticsearch.nativeaccess.enableVectorLibrary";

@SuppressForbidden(
reason = "TODO Deprecate any lenient usage of Boolean#parseBoolean https://github.com/elastic/elasticsearch/issues/128993"
)
static boolean checkEnableSystemProperty() {
return Optional.ofNullable(System.getProperty(ENABLE_JDK_VECTOR_LIBRARY)).map(Boolean::valueOf).orElse(Boolean.TRUE);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.carrotsearch.randomizedtesting.annotations.Name;
import com.carrotsearch.randomizedtesting.annotations.ParametersFactory;

import org.elasticsearch.core.Booleans;
import org.elasticsearch.test.cluster.ElasticsearchCluster;
import org.elasticsearch.test.cluster.FeatureFlag;
import org.elasticsearch.test.rest.yaml.ClientYamlTestCandidate;
Expand All @@ -26,8 +27,8 @@ private static ElasticsearchCluster makeCluster() {

// On Serverless, we want to disallow scripted metrics aggs per default.
// The following override allows us to still run the scripted metrics agg tests without breaking bwc.
boolean disableAllowListPerDefault = Boolean.parseBoolean(
System.getProperty("tests.disable_scripted_metric_allow_list_per_default")
boolean disableAllowListPerDefault = Booleans.parseBoolean(
System.getProperty("tests.disable_scripted_metric_allow_list_per_default", "false")
);
if (disableAllowListPerDefault) {
return cluster.setting("search.aggs.only_allowed_metric_scripts", "false").build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.elasticsearch.common.settings.SecureString;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.util.concurrent.ThreadContext;
import org.elasticsearch.core.Booleans;
import org.elasticsearch.test.cluster.ElasticsearchCluster;
import org.elasticsearch.test.cluster.local.LocalClusterSpecBuilder;
import org.elasticsearch.test.cluster.local.distribution.DistributionType;
Expand Down Expand Up @@ -51,7 +52,7 @@ private static ElasticsearchCluster createCluster() {
if (initTestSeed().nextBoolean()) {
clusterBuilder.setting("xpack.license.self_generated.type", "trial");
}
boolean setNodes = Boolean.parseBoolean(System.getProperty("yaml.rest.tests.set_num_nodes", "true"));
boolean setNodes = Booleans.parseBoolean(System.getProperty("yaml.rest.tests.set_num_nodes", "true"));
if (setNodes) {
clusterBuilder.nodes(2);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.elasticsearch.common.settings.SecureString;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.util.concurrent.ThreadContext;
import org.elasticsearch.core.Booleans;
import org.elasticsearch.test.cluster.ElasticsearchCluster;
import org.elasticsearch.test.cluster.local.LocalClusterSpecBuilder;
import org.elasticsearch.test.cluster.local.distribution.DistributionType;
Expand Down Expand Up @@ -50,7 +51,7 @@ private static ElasticsearchCluster createCluster() {
.setting("xpack.security.enabled", "true")
.keystore("bootstrap.password", "x-pack-test-password")
.user("x_pack_rest_user", "x-pack-test-password");
boolean setNodes = Boolean.parseBoolean(System.getProperty("yaml.rest.tests.set_num_nodes", "true"));
boolean setNodes = Booleans.parseBoolean(System.getProperty("yaml.rest.tests.set_num_nodes", "true"));
if (setNodes) {
clusterBuilder.nodes(2);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.elasticsearch.common.settings.SecureString;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.util.concurrent.ThreadContext;
import org.elasticsearch.core.Booleans;
import org.elasticsearch.test.cluster.ElasticsearchCluster;
import org.elasticsearch.test.cluster.local.LocalClusterSpecBuilder;
import org.elasticsearch.test.cluster.local.distribution.DistributionType;
Expand Down Expand Up @@ -48,7 +49,7 @@ private static ElasticsearchCluster createCluster() {
.distribution(DistributionType.DEFAULT)
.setting("xpack.security.enabled", "true")
.user("x_pack_rest_user", "x-pack-test-password");
boolean setNodes = Boolean.parseBoolean(System.getProperty("yaml.rest.tests.set_num_nodes", "true"));
boolean setNodes = Booleans.parseBoolean(System.getProperty("yaml.rest.tests.set_num_nodes", "true"));
if (setNodes) {
clusterBuilder.nodes(2);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.util.CollectionUtils;
import org.elasticsearch.common.util.concurrent.ThreadContext;
import org.elasticsearch.core.Booleans;
import org.elasticsearch.core.Nullable;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.test.cluster.ElasticsearchCluster;
Expand Down Expand Up @@ -52,7 +53,7 @@ public class GeoIpReindexedIT extends ParameterizedFullClusterRestartTestCase {

// e.g. use ./gradlew -Dtests.jvm.argline="-Dgeoip_test_with_security=false" ":modules:ingest-geoip:qa:full-cluster-restart:check"
// to set this to false, if you so desire
private static final boolean useSecurity = Boolean.parseBoolean(System.getProperty("geoip_test_with_security", "true"));
private static final boolean useSecurity = Booleans.parseBoolean(System.getProperty("geoip_test_with_security", "true"));

private static final ElasticsearchCluster cluster = ElasticsearchCluster.local()
.distribution(DistributionType.DEFAULT)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.elasticsearch.common.text.SizeLimitingStringWriter;
import org.elasticsearch.common.unit.ByteSizeValue;
import org.elasticsearch.common.unit.MemorySizeValue;
import org.elasticsearch.core.SuppressForbidden;
import org.elasticsearch.script.GeneralScriptException;
import org.elasticsearch.script.Script;
import org.elasticsearch.script.ScriptContext;
Expand Down Expand Up @@ -107,12 +108,19 @@ private static CustomMustacheFactory createMustacheFactory(Map<String, String> o
}

if (options.containsKey(DETECT_MISSING_PARAMS_OPTION)) {
builder.detectMissingParams(Boolean.valueOf(options.get(DETECT_MISSING_PARAMS_OPTION)));
builder.detectMissingParams(getDetectMissingParamsOption(options));
}

return builder.build();
}

@SuppressForbidden(
reason = "TODO Deprecate any lenient usage of Boolean#parseBoolean https://github.com/elastic/elasticsearch/issues/128993"
)
private static boolean getDetectMissingParamsOption(Map<String, String> options) {
return Boolean.valueOf(options.get(DETECT_MISSING_PARAMS_OPTION));
}

@Override
public String getType() {
return NAME;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import org.elasticsearch.SpecialPermission;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.core.SuppressForbidden;
import org.elasticsearch.painless.Compiler.Loader;
import org.elasticsearch.painless.lookup.PainlessLookup;
import org.elasticsearch.painless.lookup.PainlessLookupBuilder;
Expand Down Expand Up @@ -414,7 +415,7 @@ private CompilerSettings buildCompilerSettings(Map<String, String> params) {

value = copy.remove(CompilerSettings.PICKY);
if (value != null) {
compilerSettings.setPicky(Boolean.parseBoolean(value));
compilerSettings.setPicky(parseBoolean(value));
}

value = copy.remove(CompilerSettings.INITIAL_CALL_SITE_DEPTH);
Expand All @@ -439,6 +440,13 @@ private CompilerSettings buildCompilerSettings(Map<String, String> params) {
return compilerSettings;
}

@SuppressForbidden(
reason = "TODO Deprecate any lenient usage of Boolean#parseBoolean https://github.com/elastic/elasticsearch/issues/128993"
)
private static boolean parseBoolean(String value) {
return Boolean.parseBoolean(value);
}

private static ScriptException convertToScriptException(String scriptSource, Throwable t) {
// create a script stack: this is just the script portion
List<String> scriptStack = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.elasticsearch.common.util.PageCacheRecycler;
import org.elasticsearch.core.Assertions;
import org.elasticsearch.core.Booleans;
import org.elasticsearch.core.SuppressForbidden;
import org.elasticsearch.monitor.jvm.JvmInfo;

import java.util.Arrays;
Expand Down Expand Up @@ -55,7 +56,7 @@ public class NettyAllocator {
+ ", factors={es.unsafe.use_netty_default_allocator=true}]";
} else {
final long heapSizeInBytes = JvmInfo.jvmInfo().getMem().getHeapMax().getBytes();
final boolean g1gcEnabled = Boolean.parseBoolean(JvmInfo.jvmInfo().useG1GC());
final boolean g1gcEnabled = useG1GC();
final long g1gcRegionSizeInBytes = JvmInfo.jvmInfo().getG1RegionSize();
final boolean g1gcRegionSizeIsKnown = g1gcRegionSizeInBytes != -1;
ByteSizeValue heapSize = ByteSizeValue.ofBytes(heapSizeInBytes);
Expand Down Expand Up @@ -169,6 +170,13 @@ public int pageSize() {
};
}

@SuppressForbidden(
reason = "TODO Deprecate any lenient usage of Boolean#parseBoolean https://github.com/elastic/elasticsearch/issues/128993"
)
private static boolean useG1GC() {
return Boolean.parseBoolean(JvmInfo.jvmInfo().useG1GC());
}

public static void logAllocatorDescriptionIfNeeded() {
if (descriptionLogged.compareAndSet(false, true)) {
logger.info("creating NettyAllocator with the following configs: " + NettyAllocator.getAllocatorDescription());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.settings.Setting;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.core.Booleans;
import org.elasticsearch.index.IndexSettings;
import org.elasticsearch.index.mapper.MapperService;
import org.elasticsearch.test.XContentTestUtils;
Expand Down Expand Up @@ -330,7 +331,7 @@ protected static List<ClusterBlock> indexBlocks(String indexName) throws Excepti
@SuppressWarnings("unchecked")
protected static void assertIndexSetting(String indexName, Setting<?> setting, Matcher<Boolean> matcher) throws Exception {
var indexSettings = getIndexSettingsAsMap(indexName);
assertThat(Boolean.parseBoolean((String) indexSettings.get(setting.getKey())), matcher);
assertThat(Booleans.parseBoolean((String) indexSettings.get(setting.getKey()), false), matcher);
}

protected static ResponseException expectUpdateIndexSettingsThrows(String indexName, Settings.Builder settings) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.core.Booleans;
import org.elasticsearch.packaging.test.PackagingTestCase;

import java.io.IOException;
Expand Down Expand Up @@ -82,7 +83,7 @@ public static void waitForElasticsearch(Installation installation) throws Except
.lines()
.filter(each -> each.startsWith("xpack.security.enabled"))
.findFirst()
.map(line -> Boolean.parseBoolean(line.split("=")[1]))
.map(line -> Booleans.parseBoolean(line.split("=")[1]))
// security is enabled by default, the only way for it to be disabled is to be explicitly disabled
.orElse(true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ public static ClusterType parse(String value) {
}

protected static final ClusterType CLUSTER_TYPE = ClusterType.parse(System.getProperty("tests.rest.suite"));
protected static final boolean FIRST_MIXED_ROUND = Boolean.parseBoolean(System.getProperty("tests.first_round", "false"));
protected static final Version UPGRADE_FROM_VERSION = Version.fromString(System.getProperty("tests.upgrade_from_version"));

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import org.elasticsearch.client.Request;
import org.elasticsearch.client.ResponseException;
import org.elasticsearch.core.Booleans;
import org.elasticsearch.indices.SystemIndices;
import org.elasticsearch.test.XContentTestUtils;
import org.junit.BeforeClass;
Expand All @@ -33,7 +34,7 @@ public FeatureUpgradeIT(@Name("upgradedNodes") int upgradedNodes) {

@BeforeClass
public static void ensureNotForwardCompatTest() {
assumeFalse("Only supported by bwc tests", Boolean.parseBoolean(System.getProperty("tests.fwc", "false")));
assumeFalse("Only supported by bwc tests", Booleans.parseBoolean(System.getProperty("tests.fwc", "false")));
}

public void testGetFeatureUpgradeStatus() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,7 @@ private static void enrichIndexAbstraction(
if (ia.isSystem()) {
attributes.add(Attribute.SYSTEM);
}
final boolean isFrozen = Boolean.parseBoolean(writeIndex.getSettings().get("index.frozen"));
final boolean isFrozen = writeIndex.getSettings().getAsBoolean("index.frozen", false);
if (isFrozen) {
attributes.add(Attribute.FROZEN);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.common.Strings;
import org.elasticsearch.core.SuppressForbidden;

import java.util.Collections;
import java.util.HashMap;
Expand Down Expand Up @@ -111,7 +112,7 @@ public static LifecycleExecutionState fromCustomMetadata(Map<String, String> cus
}
String isAutoRetryableError = customData.get(IS_AUTO_RETRYABLE_ERROR);
if (isAutoRetryableError != null) {
builder.setIsAutoRetryableError(Boolean.parseBoolean(isAutoRetryableError));
builder.setIsAutoRetryableError(parseIsAutoRetryableError(isAutoRetryableError));
}
String failedStepRetryCount = customData.get(FAILED_STEP_RETRY_COUNT);
if (failedStepRetryCount != null) {
Expand Down Expand Up @@ -204,6 +205,13 @@ public static LifecycleExecutionState fromCustomMetadata(Map<String, String> cus
return builder.build();
}

@SuppressForbidden(
reason = "TODO Deprecate any lenient usage of Boolean#parseBoolean https://github.com/elastic/elasticsearch/issues/128993"
)
private static boolean parseIsAutoRetryableError(String isAutoRetryableError) {
return Boolean.parseBoolean(isAutoRetryableError);
}

/**
* Converts this object to an immutable map representation for use with
* {@link IndexMetadata.Builder#putCustom(String, Map)}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.Constants;
import org.elasticsearch.core.Predicates;
import org.elasticsearch.core.SuppressForbidden;

import java.io.IOException;
import java.net.Inet4Address;
Expand Down Expand Up @@ -44,7 +45,14 @@ private NetworkUtils() {}
* @deprecated transition mechanism only
*/
@Deprecated
static final boolean PREFER_V6 = Boolean.parseBoolean(System.getProperty("java.net.preferIPv6Addresses", "false"));
static final boolean PREFER_V6 = preferIPv6Addresses();

@SuppressForbidden(
reason = "TODO Deprecate any lenient usage of Boolean#parseBoolean https://github.com/elastic/elasticsearch/issues/128993"
)
private static boolean preferIPv6Addresses() {
return Boolean.parseBoolean(System.getProperty("java.net.preferIPv6Addresses", "false"));
}

/**
* True if we can bind to a v6 address. Its silly, but for *binding* we have a need to know
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.core.Assertions;
import org.elasticsearch.core.Booleans;
import org.elasticsearch.core.CheckedFunction;
import org.elasticsearch.core.FixForMultiProject;
import org.elasticsearch.core.IOUtils;
Expand Down Expand Up @@ -690,7 +691,7 @@ public OnDiskStateMetadata loadOnDiskStateMetadataFromUserData(Map<String, Strin
Long.parseLong(userData.get(LAST_ACCEPTED_VERSION_KEY)),
userData.get(NODE_ID_KEY),
userData.get(CLUSTER_UUID_KEY),
userData.get(CLUSTER_UUID_COMMITTED_KEY) != null ? Boolean.parseBoolean(userData.get(CLUSTER_UUID_COMMITTED_KEY)) : null
userData.get(CLUSTER_UUID_COMMITTED_KEY) != null ? Booleans.parseBoolean(userData.get(CLUSTER_UUID_COMMITTED_KEY)) : null
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ public IndexService(
mapperMetrics
);
this.indexFieldData = new IndexFieldDataService(indexSettings, indicesFieldDataCache, circuitBreakerService);
boolean sourceOnly = Boolean.parseBoolean(indexSettings.getSettings().get("index.source_only"));
boolean sourceOnly = indexSettings.getSettings().getAsBoolean("index.source_only", false);
if (indexSettings.getIndexSortConfig().hasIndexSort() && sourceOnly == false) {
// we delay the actual creation of the sort order for this index because the mapping has not been merged yet.
// The sort order is validated right after the merge of the mapping later in the process.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.elasticsearch.common.unit.ByteSizeUnit;
import org.elasticsearch.common.unit.ByteSizeValue;
import org.elasticsearch.common.util.FeatureFlag;
import org.elasticsearch.core.Booleans;
import org.elasticsearch.core.TimeValue;
import org.elasticsearch.index.mapper.IgnoredSourceFieldMapper;
import org.elasticsearch.index.mapper.Mapper;
Expand Down Expand Up @@ -305,7 +306,7 @@ public final class IndexSettings {
static class RefreshIntervalValidator implements Setting.Validator<TimeValue> {

static final String STATELESS_ALLOW_INDEX_REFRESH_INTERVAL_OVERRIDE = "es.stateless.allow.index.refresh_interval.override";
private static final boolean IS_OVERRIDE_ALLOWED = Boolean.parseBoolean(
private static final boolean IS_OVERRIDE_ALLOWED = Booleans.parseBoolean(
System.getProperty(STATELESS_ALLOW_INDEX_REFRESH_INTERVAL_OVERRIDE, "false")
);

Expand Down
Loading