Fix ec2 discovery when used with IAM profiles.

Follow up for #21039.

We can revert the previous change and do that a bit smarter than it was.

Patch tested successfully manually on ec2 with 2 nodes with a configuration like:

```yml
discovery.type: ec2
network.host: ["_local_", "_site_", "_ec2_"]
cloud.aws.region: us-west-2
```

(cherry picked from commit fbbeded)

Backport of #21048 in master branch
This commit is contained in:
David Pilato 2016-10-20 19:56:06 +02:00
parent ceaae47d38
commit e5d9f393f1
2 changed files with 12 additions and 16 deletions

View File

@ -35,7 +35,6 @@ import com.amazonaws.services.ec2.model.Instance;
import com.amazonaws.services.ec2.model.Reservation;
import org.apache.logging.log4j.message.ParameterizedMessage;
import org.apache.logging.log4j.util.Supplier;
import org.elasticsearch.SpecialPermission;
import org.elasticsearch.Version;
import org.elasticsearch.cloud.aws.AwsEc2Service;
import org.elasticsearch.cloud.aws.AwsEc2Service.DISCOVERY_EC2;
@ -48,8 +47,12 @@ import org.elasticsearch.common.util.SingleObjectCache;
import org.elasticsearch.discovery.zen.UnicastHostsProvider;
import org.elasticsearch.transport.TransportService;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import static java.util.Collections.disjoint;
import static java.util.Collections.emptyMap;
import static java.util.Collections.emptySet;
@ -111,17 +114,7 @@ public class AwsEc2UnicastHostsProvider extends AbstractComponent implements Uni
// NOTE: we don't filter by security group during the describe instances request for two reasons:
// 1. differences in VPCs require different parameters during query (ID vs Name)
// 2. We want to use two different strategies: (all security groups vs. any security groups)
SecurityManager sm = System.getSecurityManager();
if (sm != null) {
// unprivileged code such as scripts do not have SpecialPermission
sm.checkPermission(new SpecialPermission());
}
descInstances = AccessController.doPrivileged(new PrivilegedAction<DescribeInstancesResult>() {
@Override
public DescribeInstancesResult run() {
return client.describeInstances(buildDescribeInstancesRequest());
}
});
descInstances = client.describeInstances(buildDescribeInstancesRequest());
} catch (AmazonClientException e) {
logger.info("Exception while retrieving instance list from AWS API: {}", e.getMessage());
logger.debug("Full exception:", e);

View File

@ -19,6 +19,7 @@
package org.elasticsearch.plugin.discovery.ec2;
import com.amazonaws.util.json.Jackson;
import java.io.BufferedReader;
import java.io.Closeable;
import java.io.IOException;
@ -62,8 +63,6 @@ public class Ec2DiscoveryPlugin extends Plugin implements DiscoveryPlugin, Close
public static final String EC2 = "ec2";
// ClientConfiguration clinit has some classloader problems
// TODO: fix that
static {
SecurityManager sm = System.getSecurityManager();
if (sm != null) {
@ -73,6 +72,10 @@ public class Ec2DiscoveryPlugin extends Plugin implements DiscoveryPlugin, Close
@Override
public Void run() {
try {
// kick jackson to do some static caching of declared members info
Jackson.jsonNodeOf("{}");
// ClientConfiguration clinit has some classloader problems
// TODO: fix that
Class.forName("com.amazonaws.ClientConfiguration");
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);