Make discovery-azure work again

The discovery-plugin has been broken since 2.x because the code was not compliant with the security manager and because plugins have been refactored.

closes #18637, #15630
This commit is contained in:
Tanguy Leroux 2016-06-24 16:51:49 +02:00
parent 18d45b2d5c
commit c557663b90
2 changed files with 31 additions and 19 deletions

View File

@ -56,9 +56,11 @@ discovery:
.Binding the network host .Binding the network host
============================================== ==============================================
WARNING: The keystore file must be placed in a directory accessible by elasticsearch like the `config` directory.
It's important to define `network.host` as by default it's bound to `localhost`. It's important to define `network.host` as by default it's bound to `localhost`.
You can use {ref}/modules-network.html[core network host settings]. For example `_non_loopback_` or `_en0_`. You can use {ref}/modules-network.html[core network host settings]. For example `_en0_`.
============================================== ==============================================

View File

@ -20,19 +20,21 @@
package org.elasticsearch.cloud.azure.management; package org.elasticsearch.cloud.azure.management;
import com.microsoft.windowsazure.Configuration; import com.microsoft.windowsazure.Configuration;
import com.microsoft.windowsazure.core.Builder;
import com.microsoft.windowsazure.core.DefaultBuilder;
import com.microsoft.windowsazure.core.utils.KeyStoreType; import com.microsoft.windowsazure.core.utils.KeyStoreType;
import com.microsoft.windowsazure.management.compute.ComputeManagementClient; import com.microsoft.windowsazure.management.compute.ComputeManagementClient;
import com.microsoft.windowsazure.management.compute.ComputeManagementService; import com.microsoft.windowsazure.management.compute.ComputeManagementService;
import com.microsoft.windowsazure.management.compute.models.HostedServiceGetDetailedResponse; import com.microsoft.windowsazure.management.compute.models.HostedServiceGetDetailedResponse;
import com.microsoft.windowsazure.management.configuration.ManagementConfiguration; import com.microsoft.windowsazure.management.configuration.ManagementConfiguration;
import org.elasticsearch.ElasticsearchException; import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.cloud.azure.AzureServiceDisableException;
import org.elasticsearch.cloud.azure.AzureServiceRemoteException; import org.elasticsearch.cloud.azure.AzureServiceRemoteException;
import org.elasticsearch.common.component.AbstractLifecycleComponent; import org.elasticsearch.common.component.AbstractLifecycleComponent;
import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import java.io.IOException; import java.io.IOException;
import java.util.ServiceLoader;
/** /**
* *
@ -40,7 +42,7 @@ import java.io.IOException;
public class AzureComputeServiceImpl extends AbstractLifecycleComponent<AzureComputeServiceImpl> public class AzureComputeServiceImpl extends AbstractLifecycleComponent<AzureComputeServiceImpl>
implements AzureComputeService { implements AzureComputeService {
private final ComputeManagementClient computeManagementClient; private final ComputeManagementClient client;
private final String serviceName; private final String serviceName;
@Inject @Inject
@ -54,28 +56,36 @@ public class AzureComputeServiceImpl extends AbstractLifecycleComponent<AzureCom
KeyStoreType keystoreType = Management.KEYSTORE_TYPE_SETTING.get(settings); KeyStoreType keystoreType = Management.KEYSTORE_TYPE_SETTING.get(settings);
logger.trace("creating new Azure client for [{}], [{}]", subscriptionId, serviceName); logger.trace("creating new Azure client for [{}], [{}]", subscriptionId, serviceName);
ComputeManagementClient result;
try { try {
// Check that we have all needed properties // Azure SDK configuration uses DefaultBuilder which uses java.util.ServiceLoader to load the
Configuration configuration = ManagementConfiguration.configure(Management.ENDPOINT_SETTING.get(settings), // various Azure services. By default, this will use the current thread's context classloader
subscriptionId, keystorePath, keystorePassword, keystoreType); // to load services. Since the current thread refers to the main application classloader it
result = ComputeManagementService.create(configuration); // won't find any Azure service implementation.
// Here we basically create a new DefaultBuilder that uses the current class classloader to load services.
DefaultBuilder builder = new DefaultBuilder();
for (Builder.Exports exports : ServiceLoader.load(Builder.Exports.class, getClass().getClassLoader())) {
exports.register(builder);
}
// And create a new blank configuration based on the previous DefaultBuilder
Configuration configuration = new Configuration(builder);
configuration.setProperty(Configuration.PROPERTY_LOG_HTTP_REQUESTS, logger.isTraceEnabled());
Configuration managementConfig = ManagementConfiguration.configure(null, configuration,
Management.ENDPOINT_SETTING.get(settings), subscriptionId, keystorePath, keystorePassword, keystoreType);
logger.debug("creating new Azure client for [{}], [{}]", subscriptionId, serviceName);
client = ComputeManagementService.create(managementConfig);
} catch (IOException e) { } catch (IOException e) {
logger.error("can not start azure client: {}", e.getMessage()); throw new ElasticsearchException("Unable to configure Azure compute service", e);
result = null;
} }
this.computeManagementClient = result;
} }
@Override @Override
public HostedServiceGetDetailedResponse getServiceDetails() { public HostedServiceGetDetailedResponse getServiceDetails() {
if (computeManagementClient == null) {
// Azure plugin is disabled
throw new AzureServiceDisableException("azure plugin is disabled.");
}
try { try {
return computeManagementClient.getHostedServicesOperations().getDetailed(serviceName); return client.getHostedServicesOperations().getDetailed(serviceName);
} catch (Exception e) { } catch (Exception e) {
throw new AzureServiceRemoteException("can not get list of azure nodes", e); throw new AzureServiceRemoteException("can not get list of azure nodes", e);
} }
@ -91,9 +101,9 @@ public class AzureComputeServiceImpl extends AbstractLifecycleComponent<AzureCom
@Override @Override
protected void doClose() throws ElasticsearchException { protected void doClose() throws ElasticsearchException {
if (computeManagementClient != null) { if (client != null) {
try { try {
computeManagementClient.close(); client.close();
} catch (IOException e) { } catch (IOException e) {
logger.error("error while closing Azure client", e); logger.error("error while closing Azure client", e);
} }