From 3961de3e569b88fe79b9e2df5acce77f7ee2277a Mon Sep 17 00:00:00 2001 From: Pedro Andujar Date: Tue, 15 Sep 2015 14:25:56 +0100 Subject: [PATCH 1/6] Enable SSL for blob storage In order to increase Security. The Microsoft Azure storage services support both HTTP and HTTPS; however, using HTTPS is highly recommended. --- .../cloud/azure/storage/AzureStorageServiceImpl.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/cloud-azure/src/main/java/org/elasticsearch/cloud/azure/storage/AzureStorageServiceImpl.java b/plugins/cloud-azure/src/main/java/org/elasticsearch/cloud/azure/storage/AzureStorageServiceImpl.java index 86e6ba93f11..f38b23250cd 100644 --- a/plugins/cloud-azure/src/main/java/org/elasticsearch/cloud/azure/storage/AzureStorageServiceImpl.java +++ b/plugins/cloud-azure/src/main/java/org/elasticsearch/cloud/azure/storage/AzureStorageServiceImpl.java @@ -57,14 +57,14 @@ public class AzureStorageServiceImpl extends AbstractLifecycleComponent= '0' && c <= '9' ? c - '0' : -1; + } + + public static int[] toArray(Collection ints) { + Objects.requireNonNull(ints); + return ints.stream().mapToInt(s -> s).toArray(); + } + + public static int checkedCast(long value) { + int cast = (int)value; + if ((long)cast != value) { + throw new IllegalArgumentException(Long.toString(value)); + } + return cast; + } +} diff --git a/core/src/main/java/org/elasticsearch/env/NodeEnvironment.java b/core/src/main/java/org/elasticsearch/env/NodeEnvironment.java index 06562f82db5..b17052c996b 100644 --- a/core/src/main/java/org/elasticsearch/env/NodeEnvironment.java +++ b/core/src/main/java/org/elasticsearch/env/NodeEnvironment.java @@ -20,15 +20,9 @@ package org.elasticsearch.env; import com.google.common.collect.ImmutableSet; -import com.google.common.primitives.Ints; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.SegmentInfos; -import org.apache.lucene.store.Directory; -import org.apache.lucene.store.FSDirectory; -import org.apache.lucene.store.Lock; -import org.apache.lucene.store.LockObtainFailedException; -import org.apache.lucene.store.NativeFSLockFactory; -import org.apache.lucene.store.SimpleFSDirectory; +import org.apache.lucene.store.*; import org.apache.lucene.util.IOUtils; import org.elasticsearch.ElasticsearchException; import org.elasticsearch.cluster.metadata.IndexMetaData; @@ -39,6 +33,7 @@ import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.io.FileSystemUtils; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.TimeValue; +import org.elasticsearch.common.util.primitives.Integers; import org.elasticsearch.index.Index; import org.elasticsearch.index.settings.IndexSettings; import org.elasticsearch.index.shard.ShardId; @@ -48,21 +43,8 @@ import org.elasticsearch.monitor.fs.FsProbe; import java.io.Closeable; import java.io.IOException; -import java.nio.file.AtomicMoveNotSupportedException; -import java.nio.file.DirectoryStream; -import java.nio.file.FileStore; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.StandardCopyOption; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Locale; -import java.util.Map; -import java.util.Set; +import java.nio.file.*; +import java.util.*; import java.util.concurrent.Semaphore; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; @@ -712,7 +694,7 @@ public class NodeEnvironment extends AbstractComponent implements Closeable { String currentIndex = indexPath.getFileName().toString(); for (Path shardPath : stream) { if (Files.isDirectory(shardPath)) { - Integer shardId = Ints.tryParse(shardPath.getFileName().toString()); + Integer shardId = Integers.tryParse(shardPath.getFileName().toString()); if (shardId != null) { ShardId id = new ShardId(currentIndex, shardId); shardIds.add(id); diff --git a/core/src/test/java/org/elasticsearch/common/util/primitives/IntegerTests.java b/core/src/test/java/org/elasticsearch/common/util/primitives/IntegerTests.java new file mode 100644 index 00000000000..9a7f18ac7ab --- /dev/null +++ b/core/src/test/java/org/elasticsearch/common/util/primitives/IntegerTests.java @@ -0,0 +1,53 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.elasticsearch.common.util.primitives; + +import org.elasticsearch.test.ESTestCase; + +public class IntegerTests extends ESTestCase { + public void testTryParse() { + assertTryParse(0, "0"); + assertTryParse(0, "-0"); + assertTryParse(1, "1"); + assertTryParse(-1, "-1"); + assertTryParse(12345, "12345"); + assertTryParse(-12345, "-12345"); + for (int i = 0; i < 1 << 20; i++) { + int value = randomInt(); + assertTryParse(value, Integer.toString(value)); + } + assertTryParse(Integer.MAX_VALUE, Integer.toString(Integer.MAX_VALUE)); + assertTryParse(Integer.MIN_VALUE, Integer.toString(Integer.MIN_VALUE)); + assertNull(Integers.tryParse(null)); + assertNull(Integers.tryParse("")); + assertNull(Integers.tryParse("-")); + assertNull(Integers.tryParse("9999999999999999")); + assertNull(Integers.tryParse(Long.toString(((long) Integer.MAX_VALUE) + 1))); + assertNull(Integers.tryParse(Long.toString(((long) Integer.MAX_VALUE) * 10))); + assertNull(Integers.tryParse(Long.toString(((long) Integer.MIN_VALUE) - 1))); + assertNull(Integers.tryParse(Long.toString(((long) Integer.MIN_VALUE) * 10))); + assertNull(Integers.tryParse(Long.toString(Long.MAX_VALUE))); + assertNull(Integers.tryParse(Long.toString(Long.MIN_VALUE))); + } + + private static void assertTryParse(Integer expected, String value) { + assertEquals(expected, Integers.tryParse(value)); + } +} \ No newline at end of file diff --git a/core/src/test/java/org/elasticsearch/test/discovery/ClusterDiscoveryConfiguration.java b/core/src/test/java/org/elasticsearch/test/discovery/ClusterDiscoveryConfiguration.java index bcd9384a88b..ad46047855b 100644 --- a/core/src/test/java/org/elasticsearch/test/discovery/ClusterDiscoveryConfiguration.java +++ b/core/src/test/java/org/elasticsearch/test/discovery/ClusterDiscoveryConfiguration.java @@ -19,11 +19,11 @@ package org.elasticsearch.test.discovery; import com.carrotsearch.randomizedtesting.RandomizedTest; -import com.google.common.primitives.Ints; import org.elasticsearch.ElasticsearchException; import org.elasticsearch.common.SuppressForbidden; import org.elasticsearch.common.network.NetworkUtils; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.common.util.primitives.Integers; import org.elasticsearch.test.InternalTestCluster; import org.elasticsearch.test.NodeConfigurationSource; @@ -82,7 +82,7 @@ public class ClusterDiscoveryConfiguration extends NodeConfigurationSource { while (ordinals.size() != numOfUnicastHosts) { ordinals.add(RandomizedTest.randomInt(numOfNodes - 1)); } - unicastHostOrdinals = Ints.toArray(ordinals); + unicastHostOrdinals = Integers.toArray(ordinals); } this.unicastHostPorts = unicastHostPorts(numOfNodes); assert unicastHostOrdinals.length <= unicastHostPorts.length; diff --git a/dev-tools/src/main/resources/forbidden/all-signatures.txt b/dev-tools/src/main/resources/forbidden/all-signatures.txt index 613ed07112d..8b976b55baa 100644 --- a/dev-tools/src/main/resources/forbidden/all-signatures.txt +++ b/dev-tools/src/main/resources/forbidden/all-signatures.txt @@ -125,6 +125,7 @@ com.google.common.collect.ArrayListMultimap com.google.common.collect.HashMultimap com.google.common.collect.FluentIterable com.google.common.io.Files +com.google.common.primitives.Ints @defaultMessage Do not violate java's access system java.lang.reflect.AccessibleObject#setAccessible(boolean) From f0e20cf594b600c5275df4c3d44ac0957e418605 Mon Sep 17 00:00:00 2001 From: Jason Tedor Date: Thu, 17 Sep 2015 23:07:39 -0400 Subject: [PATCH 4/6] Remove need for lenient tryParse method --- .../common/util/MultiDataPathUpgrader.java | 12 ++-- .../common/util/primitives/Integers.java | 56 +------------------ .../elasticsearch/env/NodeEnvironment.java | 12 ++-- .../common/util/primitives/IntegerTests.java | 53 ------------------ 4 files changed, 12 insertions(+), 121 deletions(-) delete mode 100644 core/src/test/java/org/elasticsearch/common/util/primitives/IntegerTests.java diff --git a/core/src/main/java/org/elasticsearch/common/util/MultiDataPathUpgrader.java b/core/src/main/java/org/elasticsearch/common/util/MultiDataPathUpgrader.java index 50b1e56e074..2cbc8cbdf99 100644 --- a/core/src/main/java/org/elasticsearch/common/util/MultiDataPathUpgrader.java +++ b/core/src/main/java/org/elasticsearch/common/util/MultiDataPathUpgrader.java @@ -31,7 +31,6 @@ import org.elasticsearch.common.io.stream.BytesStreamOutput; import org.elasticsearch.common.logging.ESLogger; import org.elasticsearch.common.logging.Loggers; import org.elasticsearch.common.unit.ByteSizeValue; -import org.elasticsearch.common.util.primitives.Integers; import org.elasticsearch.env.NodeEnvironment; import org.elasticsearch.env.ShardLock; import org.elasticsearch.gateway.MetaDataStateFormat; @@ -359,12 +358,11 @@ public class MultiDataPathUpgrader { try (DirectoryStream stream = Files.newDirectoryStream(indexPath)) { String currentIndex = indexPath.getFileName().toString(); for (Path shardPath : stream) { - if (Files.isDirectory(shardPath)) { - Integer shardId = Integers.tryParse(shardPath.getFileName().toString()); - if (shardId != null) { - ShardId id = new ShardId(currentIndex, shardId); - shardIds.add(id); - } + String fileName = shardPath.getFileName().toString(); + if (Files.isDirectory(shardPath) && fileName.chars().allMatch(Character::isDigit)) { + int shardId = Integer.parseInt(fileName); + ShardId id = new ShardId(currentIndex, shardId); + shardIds.add(id); } } } diff --git a/core/src/main/java/org/elasticsearch/common/util/primitives/Integers.java b/core/src/main/java/org/elasticsearch/common/util/primitives/Integers.java index db315f23cf5..03f4d06b634 100644 --- a/core/src/main/java/org/elasticsearch/common/util/primitives/Integers.java +++ b/core/src/main/java/org/elasticsearch/common/util/primitives/Integers.java @@ -19,62 +19,10 @@ package org.elasticsearch.common.util.primitives; -import org.elasticsearch.common.Strings; - -import java.util.*; +import java.util.Collection; +import java.util.Objects; public class Integers { - /** - * Tries to parse the given String to an int - * - * @param value the String to try to parse to an int - * @return the parsed value as an int or null if the String can not be parsed to an int - */ - public static Integer tryParse(String value) { - if (Strings.isNullOrEmpty(value)) { - return null; - } else { - boolean negative = value.charAt(0) == '-'; - int index = negative ? 1 : 0; - if (index == value.length()) { - return null; - } else { - int digit = digit(value.charAt(index++)); - if (digit != -1) { - // so we can accumulate to Integer.MIN_VALUE - int accumulator = -digit; - for (int cap = Integer.MIN_VALUE / 10; index < value.length(); accumulator -= digit) { - digit = digit(value.charAt(index++)); - if (digit == -1 || accumulator < cap) { - // non-digit or will overflow - return null; - } - accumulator *= 10; - if (accumulator < Integer.MIN_VALUE + digit) { - // will overflow - return null; - } - } - if (negative) { - return Integer.valueOf(accumulator); - } else if (accumulator == Integer.MIN_VALUE) { - // overflow - return null; - } else { - return Integer.valueOf(-accumulator); - } - } else { - // non-digit encountered - return null; - } - } - } - } - - private static int digit(char c) { - return c >= '0' && c <= '9' ? c - '0' : -1; - } - public static int[] toArray(Collection ints) { Objects.requireNonNull(ints); return ints.stream().mapToInt(s -> s).toArray(); diff --git a/core/src/main/java/org/elasticsearch/env/NodeEnvironment.java b/core/src/main/java/org/elasticsearch/env/NodeEnvironment.java index b17052c996b..7fe71d8535c 100644 --- a/core/src/main/java/org/elasticsearch/env/NodeEnvironment.java +++ b/core/src/main/java/org/elasticsearch/env/NodeEnvironment.java @@ -33,7 +33,6 @@ import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.io.FileSystemUtils; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.TimeValue; -import org.elasticsearch.common.util.primitives.Integers; import org.elasticsearch.index.Index; import org.elasticsearch.index.settings.IndexSettings; import org.elasticsearch.index.shard.ShardId; @@ -693,12 +692,11 @@ public class NodeEnvironment extends AbstractComponent implements Closeable { try (DirectoryStream stream = Files.newDirectoryStream(indexPath)) { String currentIndex = indexPath.getFileName().toString(); for (Path shardPath : stream) { - if (Files.isDirectory(shardPath)) { - Integer shardId = Integers.tryParse(shardPath.getFileName().toString()); - if (shardId != null) { - ShardId id = new ShardId(currentIndex, shardId); - shardIds.add(id); - } + String fileName = shardPath.getFileName().toString(); + if (Files.isDirectory(shardPath) && fileName.chars().allMatch(Character::isDigit)) { + int shardId = Integer.parseInt(fileName); + ShardId id = new ShardId(currentIndex, shardId); + shardIds.add(id); } } } diff --git a/core/src/test/java/org/elasticsearch/common/util/primitives/IntegerTests.java b/core/src/test/java/org/elasticsearch/common/util/primitives/IntegerTests.java deleted file mode 100644 index 9a7f18ac7ab..00000000000 --- a/core/src/test/java/org/elasticsearch/common/util/primitives/IntegerTests.java +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Licensed to Elasticsearch under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.elasticsearch.common.util.primitives; - -import org.elasticsearch.test.ESTestCase; - -public class IntegerTests extends ESTestCase { - public void testTryParse() { - assertTryParse(0, "0"); - assertTryParse(0, "-0"); - assertTryParse(1, "1"); - assertTryParse(-1, "-1"); - assertTryParse(12345, "12345"); - assertTryParse(-12345, "-12345"); - for (int i = 0; i < 1 << 20; i++) { - int value = randomInt(); - assertTryParse(value, Integer.toString(value)); - } - assertTryParse(Integer.MAX_VALUE, Integer.toString(Integer.MAX_VALUE)); - assertTryParse(Integer.MIN_VALUE, Integer.toString(Integer.MIN_VALUE)); - assertNull(Integers.tryParse(null)); - assertNull(Integers.tryParse("")); - assertNull(Integers.tryParse("-")); - assertNull(Integers.tryParse("9999999999999999")); - assertNull(Integers.tryParse(Long.toString(((long) Integer.MAX_VALUE) + 1))); - assertNull(Integers.tryParse(Long.toString(((long) Integer.MAX_VALUE) * 10))); - assertNull(Integers.tryParse(Long.toString(((long) Integer.MIN_VALUE) - 1))); - assertNull(Integers.tryParse(Long.toString(((long) Integer.MIN_VALUE) * 10))); - assertNull(Integers.tryParse(Long.toString(Long.MAX_VALUE))); - assertNull(Integers.tryParse(Long.toString(Long.MIN_VALUE))); - } - - private static void assertTryParse(Integer expected, String value) { - assertEquals(expected, Integers.tryParse(value)); - } -} \ No newline at end of file From 702cf7be72409c5bbc8479329856451701240aae Mon Sep 17 00:00:00 2001 From: Jason Tedor Date: Thu, 17 Sep 2015 23:36:28 -0400 Subject: [PATCH 5/6] Replace Integers.checkedCast with built-in method --- .../java/org/elasticsearch/common/util/BloomFilter.java | 3 +-- .../elasticsearch/common/util/primitives/Integers.java | 8 -------- 2 files changed, 1 insertion(+), 10 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/common/util/BloomFilter.java b/core/src/main/java/org/elasticsearch/common/util/BloomFilter.java index 561fff5ab7e..360bd39e800 100644 --- a/core/src/main/java/org/elasticsearch/common/util/BloomFilter.java +++ b/core/src/main/java/org/elasticsearch/common/util/BloomFilter.java @@ -29,7 +29,6 @@ import org.elasticsearch.common.hash.MurmurHash3; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.unit.SizeValue; -import org.elasticsearch.common.util.primitives.Integers; import java.io.IOException; import java.util.Arrays; @@ -325,7 +324,7 @@ public class BloomFilter { private static int size(long bits) { long quotient = bits / 64; long remainder = bits - quotient * 64; - return Integers.checkedCast(remainder == 0 ? quotient : 1 + quotient); + return Math.toIntExact(remainder == 0 ? quotient : 1 + quotient); } // Used by serialization diff --git a/core/src/main/java/org/elasticsearch/common/util/primitives/Integers.java b/core/src/main/java/org/elasticsearch/common/util/primitives/Integers.java index 03f4d06b634..1c49b3f3fa3 100644 --- a/core/src/main/java/org/elasticsearch/common/util/primitives/Integers.java +++ b/core/src/main/java/org/elasticsearch/common/util/primitives/Integers.java @@ -27,12 +27,4 @@ public class Integers { Objects.requireNonNull(ints); return ints.stream().mapToInt(s -> s).toArray(); } - - public static int checkedCast(long value) { - int cast = (int)value; - if ((long)cast != value) { - throw new IllegalArgumentException(Long.toString(value)); - } - return cast; - } } From dea7989a0fda5c4e475788d250651bc8f72009f0 Mon Sep 17 00:00:00 2001 From: Jason Tedor Date: Fri, 18 Sep 2015 00:07:55 -0400 Subject: [PATCH 6/6] Move method to CollectionUtils to reduce number of source files --- .../common/util/CollectionUtils.java | 5 ++++ .../common/util/primitives/Integers.java | 30 ------------------- .../ClusterDiscoveryConfiguration.java | 4 +-- 3 files changed, 7 insertions(+), 32 deletions(-) delete mode 100644 core/src/main/java/org/elasticsearch/common/util/primitives/Integers.java diff --git a/core/src/main/java/org/elasticsearch/common/util/CollectionUtils.java b/core/src/main/java/org/elasticsearch/common/util/CollectionUtils.java index ec37fb21de1..e4a074b3961 100644 --- a/core/src/main/java/org/elasticsearch/common/util/CollectionUtils.java +++ b/core/src/main/java/org/elasticsearch/common/util/CollectionUtils.java @@ -277,6 +277,11 @@ public enum CollectionUtils { }.sort(0, array.size()); } + public static int[] toArray(Collection ints) { + Objects.requireNonNull(ints); + return ints.stream().mapToInt(s -> s).toArray(); + } + private static class RotatedList extends AbstractList implements RandomAccess { private final List in; diff --git a/core/src/main/java/org/elasticsearch/common/util/primitives/Integers.java b/core/src/main/java/org/elasticsearch/common/util/primitives/Integers.java deleted file mode 100644 index 1c49b3f3fa3..00000000000 --- a/core/src/main/java/org/elasticsearch/common/util/primitives/Integers.java +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Licensed to Elasticsearch under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.elasticsearch.common.util.primitives; - -import java.util.Collection; -import java.util.Objects; - -public class Integers { - public static int[] toArray(Collection ints) { - Objects.requireNonNull(ints); - return ints.stream().mapToInt(s -> s).toArray(); - } -} diff --git a/core/src/test/java/org/elasticsearch/test/discovery/ClusterDiscoveryConfiguration.java b/core/src/test/java/org/elasticsearch/test/discovery/ClusterDiscoveryConfiguration.java index ad46047855b..e549c185616 100644 --- a/core/src/test/java/org/elasticsearch/test/discovery/ClusterDiscoveryConfiguration.java +++ b/core/src/test/java/org/elasticsearch/test/discovery/ClusterDiscoveryConfiguration.java @@ -23,7 +23,7 @@ import org.elasticsearch.ElasticsearchException; import org.elasticsearch.common.SuppressForbidden; import org.elasticsearch.common.network.NetworkUtils; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.common.util.primitives.Integers; +import org.elasticsearch.common.util.CollectionUtils; import org.elasticsearch.test.InternalTestCluster; import org.elasticsearch.test.NodeConfigurationSource; @@ -82,7 +82,7 @@ public class ClusterDiscoveryConfiguration extends NodeConfigurationSource { while (ordinals.size() != numOfUnicastHosts) { ordinals.add(RandomizedTest.randomInt(numOfNodes - 1)); } - unicastHostOrdinals = Integers.toArray(ordinals); + unicastHostOrdinals = CollectionUtils.toArray(ordinals); } this.unicastHostPorts = unicastHostPorts(numOfNodes); assert unicastHostOrdinals.length <= unicastHostPorts.length;