Skip to content

Commit aece89d

Browse files
Make boolean conversion strict (#22200)
This PR removes all leniency in the conversion of Strings to booleans: "true" is converted to the boolean value `true`, "false" is converted to the boolean value `false`. Everything else raises an error.
1 parent ee5f8c4 commit aece89d

File tree

169 files changed

+1346
-601
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

169 files changed

+1346
-601
lines changed

buildSrc/src/main/resources/checkstyle_suppressions.xml

-4
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,6 @@
255255
<suppress files="core[/\\]src[/\\]main[/\\]java[/\\]org[/\\]elasticsearch[/\\]cluster[/\\]routing[/\\]allocation[/\\]decider[/\\]AllocationDeciders.java" checks="LineLength" />
256256
<suppress files="core[/\\]src[/\\]main[/\\]java[/\\]org[/\\]elasticsearch[/\\]cluster[/\\]service[/\\]InternalClusterService.java" checks="LineLength" />
257257
<suppress files="core[/\\]src[/\\]main[/\\]java[/\\]org[/\\]elasticsearch[/\\]common[/\\]Base64.java" checks="LineLength" />
258-
<suppress files="core[/\\]src[/\\]main[/\\]java[/\\]org[/\\]elasticsearch[/\\]common[/\\]Booleans.java" checks="LineLength" />
259258
<suppress files="core[/\\]src[/\\]main[/\\]java[/\\]org[/\\]elasticsearch[/\\]common[/\\]Numbers.java" checks="LineLength" />
260259
<suppress files="core[/\\]src[/\\]main[/\\]java[/\\]org[/\\]elasticsearch[/\\]common[/\\]blobstore[/\\]fs[/\\]FsBlobStore.java" checks="LineLength" />
261260
<suppress files="core[/\\]src[/\\]main[/\\]java[/\\]org[/\\]elasticsearch[/\\]common[/\\]blobstore[/\\]url[/\\]URLBlobStore.java" checks="LineLength" />
@@ -562,8 +561,6 @@
562561
<suppress files="core[/\\]src[/\\]test[/\\]java[/\\]org[/\\]elasticsearch[/\\]action[/\\]ingest[/\\]SimulatePipelineResponseTests.java" checks="LineLength" />
563562
<suppress files="core[/\\]src[/\\]test[/\\]java[/\\]org[/\\]elasticsearch[/\\]action[/\\]ingest[/\\]WriteableIngestDocumentTests.java" checks="LineLength" />
564563
<suppress files="core[/\\]src[/\\]test[/\\]java[/\\]org[/\\]elasticsearch[/\\]action[/\\]search[/\\]SearchRequestBuilderTests.java" checks="LineLength" />
565-
<suppress files="core[/\\]src[/\\]test[/\\]java[/\\]org[/\\]elasticsearch[/\\]action[/\\]support[/\\]IndicesOptionsTests.java" checks="LineLength" />
566-
<suppress files="core[/\\]src[/\\]test[/\\]java[/\\]org[/\\]elasticsearch[/\\]action[/\\]support[/\\]IndicesOptionsTests.java" checks="LineLength" />
567564
<suppress files="core[/\\]src[/\\]test[/\\]java[/\\]org[/\\]elasticsearch[/\\]action[/\\]support[/\\]TransportActionFilterChainTests.java" checks="LineLength" />
568565
<suppress files="core[/\\]src[/\\]test[/\\]java[/\\]org[/\\]elasticsearch[/\\]action[/\\]support[/\\]WaitActiveShardCountIT.java" checks="LineLength" />
569566
<suppress files="core[/\\]src[/\\]test[/\\]java[/\\]org[/\\]elasticsearch[/\\]action[/\\]support[/\\]broadcast[/\\]node[/\\]TransportBroadcastByNodeActionTests.java" checks="LineLength" />
@@ -652,7 +649,6 @@
652649
<suppress files="core[/\\]src[/\\]test[/\\]java[/\\]org[/\\]elasticsearch[/\\]cluster[/\\]settings[/\\]ClusterSettingsIT.java" checks="LineLength" />
653650
<suppress files="core[/\\]src[/\\]test[/\\]java[/\\]org[/\\]elasticsearch[/\\]cluster[/\\]shards[/\\]ClusterSearchShardsIT.java" checks="LineLength" />
654651
<suppress files="core[/\\]src[/\\]test[/\\]java[/\\]org[/\\]elasticsearch[/\\]cluster[/\\]structure[/\\]RoutingIteratorTests.java" checks="LineLength" />
655-
<suppress files="core[/\\]src[/\\]test[/\\]java[/\\]org[/\\]elasticsearch[/\\]common[/\\]BooleansTests.java" checks="LineLength" />
656652
<suppress files="core[/\\]src[/\\]test[/\\]java[/\\]org[/\\]elasticsearch[/\\]common[/\\]blobstore[/\\]FsBlobStoreContainerTests.java" checks="LineLength" />
657653
<suppress files="core[/\\]src[/\\]test[/\\]java[/\\]org[/\\]elasticsearch[/\\]common[/\\]blobstore[/\\]FsBlobStoreTests.java" checks="LineLength" />
658654
<suppress files="core[/\\]src[/\\]test[/\\]java[/\\]org[/\\]elasticsearch[/\\]common[/\\]breaker[/\\]MemoryCircuitBreakerTests.java" checks="LineLength" />

core/src/main/java/org/elasticsearch/action/admin/cluster/snapshots/create/CreateSnapshotRequest.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
import static org.elasticsearch.common.settings.Settings.readSettingsFromStream;
4343
import static org.elasticsearch.common.settings.Settings.writeSettingsToStream;
4444
import static org.elasticsearch.common.settings.Settings.Builder.EMPTY_SETTINGS;
45-
import static org.elasticsearch.common.xcontent.support.XContentMapValues.lenientNodeBooleanValue;
45+
import static org.elasticsearch.common.xcontent.support.XContentMapValues.nodeBooleanValue;
4646

4747
/**
4848
* Create snapshot request
@@ -366,14 +366,14 @@ public CreateSnapshotRequest source(Map<String, Object> source) {
366366
throw new IllegalArgumentException("malformed indices section, should be an array of strings");
367367
}
368368
} else if (name.equals("partial")) {
369-
partial(lenientNodeBooleanValue(entry.getValue()));
369+
partial(nodeBooleanValue(entry.getValue(), "partial"));
370370
} else if (name.equals("settings")) {
371371
if (!(entry.getValue() instanceof Map)) {
372372
throw new IllegalArgumentException("malformed settings section, should indices an inner object");
373373
}
374374
settings((Map<String, Object>) entry.getValue());
375375
} else if (name.equals("include_global_state")) {
376-
includeGlobalState = lenientNodeBooleanValue(entry.getValue());
376+
includeGlobalState = nodeBooleanValue(entry.getValue(), "include_global_state");
377377
}
378378
}
379379
indicesOptions(IndicesOptions.fromMap((Map<String, Object>) source, IndicesOptions.lenientExpandOpen()));

core/src/main/java/org/elasticsearch/action/admin/cluster/snapshots/restore/RestoreSnapshotRequest.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
import static org.elasticsearch.common.settings.Settings.readSettingsFromStream;
4141
import static org.elasticsearch.common.settings.Settings.writeSettingsToStream;
4242
import static org.elasticsearch.common.settings.Settings.Builder.EMPTY_SETTINGS;
43-
import static org.elasticsearch.common.xcontent.support.XContentMapValues.lenientNodeBooleanValue;
43+
import static org.elasticsearch.common.xcontent.support.XContentMapValues.nodeBooleanValue;
4444

4545
/**
4646
* Restore snapshot request
@@ -481,16 +481,16 @@ public RestoreSnapshotRequest source(Map<String, Object> source) {
481481
throw new IllegalArgumentException("malformed indices section, should be an array of strings");
482482
}
483483
} else if (name.equals("partial")) {
484-
partial(lenientNodeBooleanValue(entry.getValue()));
484+
partial(nodeBooleanValue(entry.getValue(), "partial"));
485485
} else if (name.equals("settings")) {
486486
if (!(entry.getValue() instanceof Map)) {
487487
throw new IllegalArgumentException("malformed settings section");
488488
}
489489
settings((Map<String, Object>) entry.getValue());
490490
} else if (name.equals("include_global_state")) {
491-
includeGlobalState = lenientNodeBooleanValue(entry.getValue());
491+
includeGlobalState = nodeBooleanValue(entry.getValue(), "include_global_state");
492492
} else if (name.equals("include_aliases")) {
493-
includeAliases = lenientNodeBooleanValue(entry.getValue());
493+
includeAliases = nodeBooleanValue(entry.getValue(), "include_aliases");
494494
} else if (name.equals("rename_pattern")) {
495495
if (entry.getValue() instanceof String) {
496496
renamePattern((String) entry.getValue());

core/src/main/java/org/elasticsearch/action/admin/indices/flush/TransportShardFlushAction.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import org.elasticsearch.action.support.replication.ReplicationResponse;
2424
import org.elasticsearch.action.support.replication.TransportReplicationAction;
2525
import org.elasticsearch.cluster.action.shard.ShardStateAction;
26-
import org.elasticsearch.cluster.block.ClusterBlockLevel;
26+
import org.elasticsearch.cluster.metadata.IndexMetaData;
2727
import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver;
2828
import org.elasticsearch.cluster.service.ClusterService;
2929
import org.elasticsearch.common.inject.Inject;
@@ -65,7 +65,7 @@ protected ReplicaResult shardOperationOnReplica(ShardFlushRequest request, Index
6565
}
6666

6767
@Override
68-
protected boolean shouldExecuteReplication(Settings settings) {
68+
protected boolean shouldExecuteReplication(IndexMetaData indexMetaData) {
6969
return true;
7070
}
7171
}

core/src/main/java/org/elasticsearch/action/admin/indices/refresh/TransportShardRefreshAction.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import org.elasticsearch.action.support.replication.ReplicationResponse;
2525
import org.elasticsearch.action.support.replication.TransportReplicationAction;
2626
import org.elasticsearch.cluster.action.shard.ShardStateAction;
27-
import org.elasticsearch.cluster.block.ClusterBlockLevel;
27+
import org.elasticsearch.cluster.metadata.IndexMetaData;
2828
import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver;
2929
import org.elasticsearch.cluster.service.ClusterService;
3030
import org.elasticsearch.common.inject.Inject;
@@ -68,7 +68,7 @@ protected ReplicaResult shardOperationOnReplica(BasicReplicationRequest request,
6868
}
6969

7070
@Override
71-
protected boolean shouldExecuteReplication(Settings settings) {
71+
protected boolean shouldExecuteReplication(IndexMetaData indexMetaData) {
7272
return true;
7373
}
7474
}

core/src/main/java/org/elasticsearch/action/get/MultiGetRequest.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,8 @@ public static void parseDocuments(XContentParser parser, List<Item> items, @Null
379379
} else if ("_version_type".equals(currentFieldName) || "_versionType".equals(currentFieldName) || "version_type".equals(currentFieldName) || "versionType".equals(currentFieldName)) {
380380
versionType = VersionType.fromString(parser.text());
381381
} else if ("_source".equals(currentFieldName)) {
382-
if (parser.isBooleanValue()) {
382+
// check lenient to avoid interpreting the value as string but parse strict in order to provoke an error early on.
383+
if (parser.isBooleanValueLenient()) {
383384
fetchSourceContext = new FetchSourceContext(parser.booleanValue(), fetchSourceContext.includes(),
384385
fetchSourceContext.excludes());
385386
} else if (token == XContentParser.Token.VALUE_STRING) {

core/src/main/java/org/elasticsearch/action/get/TransportGetAction.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ protected void resolveRequest(ClusterState state, InternalRequest request) {
7171
if (request.request().realtime && // if the realtime flag is set
7272
request.request().preference() == null && // the preference flag is not already set
7373
indexMeta != null && // and we have the index
74-
IndexMetaData.isIndexUsingShadowReplicas(indexMeta.getSettings())) { // and the index uses shadow replicas
74+
indexMeta.isIndexUsingShadowReplicas()) { // and the index uses shadow replicas
7575
// set the preference for the request to use "_primary" automatically
7676
request.request().preference(Preference.PRIMARY.type());
7777
}

core/src/main/java/org/elasticsearch/action/support/AutoCreateIndex.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ private AutoCreate(String value) {
115115
boolean autoCreateIndex;
116116
List<Tuple<String, Boolean>> expressions = new ArrayList<>();
117117
try {
118-
autoCreateIndex = Booleans.parseBooleanExact(value);
118+
autoCreateIndex = Booleans.parseBoolean(value);
119119
} catch (IllegalArgumentException ex) {
120120
try {
121121
String[] patterns = Strings.commaDelimitedListToStringArray(value);

core/src/main/java/org/elasticsearch/action/support/IndicesOptions.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import java.io.IOException;
2727
import java.util.Map;
2828

29-
import static org.elasticsearch.common.xcontent.support.XContentMapValues.lenientNodeBooleanValue;
29+
import static org.elasticsearch.common.xcontent.support.XContentMapValues.nodeBooleanValue;
3030
import static org.elasticsearch.common.xcontent.support.XContentMapValues.nodeStringArrayValue;
3131

3232
/**
@@ -195,8 +195,8 @@ public static IndicesOptions fromParameters(Object wildcardsString, Object ignor
195195

196196
//note that allowAliasesToMultipleIndices is not exposed, always true (only for internal use)
197197
return fromOptions(
198-
lenientNodeBooleanValue(ignoreUnavailableString, defaultSettings.ignoreUnavailable()),
199-
lenientNodeBooleanValue(allowNoIndicesString, defaultSettings.allowNoIndices()),
198+
nodeBooleanValue(ignoreUnavailableString, "ignore_unavailable", defaultSettings.ignoreUnavailable()),
199+
nodeBooleanValue(allowNoIndicesString, "allow_no_indices", defaultSettings.allowNoIndices()),
200200
expandWildcardsOpen,
201201
expandWildcardsClosed,
202202
defaultSettings.allowAliasesToMultipleIndices(),
@@ -279,7 +279,7 @@ public String toString() {
279279
", allow_no_indices=" + allowNoIndices() +
280280
", expand_wildcards_open=" + expandWildcardsOpen() +
281281
", expand_wildcards_closed=" + expandWildcardsClosed() +
282-
", allow_alisases_to_multiple_indices=" + allowAliasesToMultipleIndices() +
282+
", allow_aliases_to_multiple_indices=" + allowAliasesToMultipleIndices() +
283283
", forbid_closed_indices=" + forbidClosedIndices() +
284284
']';
285285
}

core/src/main/java/org/elasticsearch/action/support/replication/TransportReplicationAction.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ public void handleException(TransportException exp) {
315315
} else {
316316
setPhase(replicationTask, "primary");
317317
final IndexMetaData indexMetaData = clusterService.state().getMetaData().index(request.shardId().getIndex());
318-
final boolean executeOnReplicas = (indexMetaData == null) || shouldExecuteReplication(indexMetaData.getSettings());
318+
final boolean executeOnReplicas = (indexMetaData == null) || shouldExecuteReplication(indexMetaData);
319319
final ActionListener<Response> listener = createResponseListener(primaryShardReference);
320320
createReplicatedOperation(request,
321321
ActionListener.wrap(result -> result.respond(listener), listener::onFailure),
@@ -910,8 +910,8 @@ public void onFailure(Exception e) {
910910
* Indicated whether this operation should be replicated to shadow replicas or not. If this method returns true the replication phase
911911
* will be skipped. For example writes such as index and delete don't need to be replicated on shadow replicas but refresh and flush do.
912912
*/
913-
protected boolean shouldExecuteReplication(Settings settings) {
914-
return IndexMetaData.isIndexUsingShadowReplicas(settings) == false;
913+
protected boolean shouldExecuteReplication(IndexMetaData indexMetaData) {
914+
return indexMetaData.isIndexUsingShadowReplicas() == false;
915915
}
916916

917917
class ShardReference implements Releasable {

core/src/main/java/org/elasticsearch/cluster/InternalClusterInfoService.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -377,14 +377,13 @@ static void buildShardLevelInfo(Logger logger, ShardStats[] stats, ImmutableOpen
377377
MetaData meta = state.getMetaData();
378378
for (ShardStats s : stats) {
379379
IndexMetaData indexMeta = meta.index(s.getShardRouting().index());
380-
Settings indexSettings = indexMeta == null ? null : indexMeta.getSettings();
381380
newShardRoutingToDataPath.put(s.getShardRouting(), s.getDataPath());
382381
long size = s.getStats().getStore().sizeInBytes();
383382
String sid = ClusterInfo.shardIdentifierFromRouting(s.getShardRouting());
384383
if (logger.isTraceEnabled()) {
385384
logger.trace("shard: {} size: {}", sid, size);
386385
}
387-
if (indexSettings != null && IndexMetaData.isIndexUsingShadowReplicas(indexSettings)) {
386+
if (indexMeta != null && indexMeta.isIndexUsingShadowReplicas()) {
388387
// Shards on a shared filesystem should be considered of size 0
389388
if (logger.isTraceEnabled()) {
390389
logger.trace("shard: {} is using shadow replicas and will be treated as size 0", sid);

core/src/main/java/org/elasticsearch/cluster/metadata/AutoExpandReplicas.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ final class AutoExpandReplicas {
3333
public static final Setting<AutoExpandReplicas> SETTING = new Setting<>(IndexMetaData.SETTING_AUTO_EXPAND_REPLICAS, "false", (value) -> {
3434
final int min;
3535
final int max;
36-
if (Booleans.parseBoolean(value, true) == false) {
36+
if (Booleans.isFalse(value)) {
3737
return new AutoExpandReplicas(0, 0, false);
3838
}
3939
final int dash = value.indexOf('-');

core/src/main/java/org/elasticsearch/cluster/metadata/IndexMetaData.java

+9-4
Original file line numberDiff line numberDiff line change
@@ -1263,19 +1263,24 @@ public static IndexMetaData fromXContent(XContentParser parser) throws IOExcepti
12631263
* is the returned value from
12641264
* {@link #isIndexUsingShadowReplicas(org.elasticsearch.common.settings.Settings)}.
12651265
*/
1266-
public static boolean isOnSharedFilesystem(Settings settings) {
1266+
public boolean isOnSharedFilesystem(Settings settings) {
12671267
// don't use the setting directly, not to trigger verbose deprecation logging
1268-
return settings.getAsBoolean(SETTING_SHARED_FILESYSTEM, isIndexUsingShadowReplicas(settings));
1268+
return settings.getAsBooleanLenientForPreEs6Indices(
1269+
this.indexCreatedVersion, SETTING_SHARED_FILESYSTEM, isIndexUsingShadowReplicas(settings));
12691270
}
12701271

12711272
/**
12721273
* Returns <code>true</code> iff the given settings indicate that the index associated
12731274
* with these settings uses shadow replicas. Otherwise <code>false</code>. The default
12741275
* setting for this is <code>false</code>.
12751276
*/
1276-
public static boolean isIndexUsingShadowReplicas(Settings settings) {
1277+
public boolean isIndexUsingShadowReplicas() {
1278+
return isIndexUsingShadowReplicas(this.settings);
1279+
}
1280+
1281+
public boolean isIndexUsingShadowReplicas(Settings settings) {
12771282
// don't use the setting directly, not to trigger verbose deprecation logging
1278-
return settings.getAsBoolean(SETTING_SHADOW_REPLICAS, false);
1283+
return settings.getAsBooleanLenientForPreEs6Indices(this.indexCreatedVersion, SETTING_SHADOW_REPLICAS, false);
12791284
}
12801285

12811286
/**

core/src/main/java/org/elasticsearch/cluster/metadata/MappingMetaData.java

+7-6
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
import java.io.IOException;
3535
import java.util.Map;
3636

37-
import static org.elasticsearch.common.xcontent.support.XContentMapValues.lenientNodeBooleanValue;
37+
import static org.elasticsearch.common.xcontent.support.XContentMapValues.nodeBooleanValue;
3838

3939
/**
4040
* Mapping configuration for a type.
@@ -95,10 +95,6 @@ public MappingMetaData(CompressedXContent mapping) throws IOException {
9595
initMappers((Map<String, Object>) mappingMap.get(this.type));
9696
}
9797

98-
public MappingMetaData(Map<String, Object> mapping) throws IOException {
99-
this(mapping.keySet().iterator().next(), mapping);
100-
}
101-
10298
public MappingMetaData(String type, Map<String, Object> mapping) throws IOException {
10399
this.type = type;
104100
XContentBuilder mappingBuilder = XContentFactory.jsonBuilder().map(mapping);
@@ -127,7 +123,12 @@ private void initMappers(Map<String, Object> withoutType) {
127123
String fieldName = entry.getKey();
128124
Object fieldNode = entry.getValue();
129125
if (fieldName.equals("required")) {
130-
required = lenientNodeBooleanValue(fieldNode);
126+
try {
127+
required = nodeBooleanValue(fieldNode);
128+
} catch (IllegalArgumentException ex) {
129+
throw new IllegalArgumentException("Failed to create mapping for type [" + this.type() + "]. " +
130+
"Illegal value in field [_routing.required].", ex);
131+
}
131132
}
132133
}
133134
this.routing = new Routing(required);

core/src/main/java/org/elasticsearch/cluster/metadata/MetaDataCreateIndexService.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ public ClusterState execute(ClusterState currentState) throws Exception {
428428
.put(indexMetaData, false)
429429
.build();
430430

431-
String maybeShadowIndicator = IndexMetaData.isIndexUsingShadowReplicas(indexMetaData.getSettings()) ? "s" : "";
431+
String maybeShadowIndicator = indexMetaData.isIndexUsingShadowReplicas() ? "s" : "";
432432
logger.info("[{}] creating index, cause [{}], templates {}, shards [{}]/[{}{}], mappings {}",
433433
request.index(), request.cause(), templateNames, indexMetaData.getNumberOfShards(),
434434
indexMetaData.getNumberOfReplicas(), maybeShadowIndicator, mappings.keySet());

core/src/main/java/org/elasticsearch/cluster/routing/IndexRoutingTable.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ boolean validate(MetaData metaData) {
140140
}
141141

142142
if (indexMetaData.getCreationVersion().onOrAfter(Version.V_5_0_0_alpha1) &&
143-
IndexMetaData.isIndexUsingShadowReplicas(indexMetaData.getSettings()) == false && // see #20650
143+
indexMetaData.isIndexUsingShadowReplicas() == false && // see #20650
144144
shardRouting.primary() && shardRouting.initializing() && shardRouting.relocating() == false &&
145145
RecoverySource.isInitialRecovery(shardRouting.recoverySource().getType()) == false &&
146146
inSyncAllocationIds.contains(shardRouting.allocationId().getId()) == false)

0 commit comments

Comments
 (0)