Search in sources :

Example 1 with ComputeCredential

use of com.google.api.client.googleapis.compute.ComputeCredential in project elasticsearch by elastic.

the class GceInstancesServiceImpl method client.

public synchronized Compute client() {
    if (refreshInterval != null && refreshInterval.millis() != 0) {
        if (client != null && (refreshInterval.millis() < 0 || (System.currentTimeMillis() - lastRefresh) < refreshInterval.millis())) {
            if (logger.isTraceEnabled())
                logger.trace("using cache to retrieve client");
            return client;
        }
        lastRefresh = System.currentTimeMillis();
    }
    try {
        gceJsonFactory = new JacksonFactory();
        logger.info("starting GCE discovery service");
        // Forcing Google Token API URL as set in GCE SDK to
        //      http://metadata/computeMetadata/v1/instance/service-accounts/default/token
        // See https://developers.google.com/compute/docs/metadata#metadataserver
        String tokenServerEncodedUrl = GceMetadataService.GCE_HOST.get(settings) + "/computeMetadata/v1/instance/service-accounts/default/token";
        ComputeCredential credential = new ComputeCredential.Builder(getGceHttpTransport(), gceJsonFactory).setTokenServerEncodedUrl(tokenServerEncodedUrl).build();
        // hack around code messiness in GCE code
        // TODO: get this fixed
        Access.doPrivilegedIOException(credential::refreshToken);
        logger.debug("token [{}] will expire in [{}] s", credential.getAccessToken(), credential.getExpiresInSeconds());
        if (credential.getExpiresInSeconds() != null) {
            refreshInterval = TimeValue.timeValueSeconds(credential.getExpiresInSeconds() - 1);
        }
        Compute.Builder builder = new Compute.Builder(getGceHttpTransport(), gceJsonFactory, null).setApplicationName(VERSION).setRootUrl(GCE_ROOT_URL.get(settings));
        if (RETRY_SETTING.exists(settings)) {
            TimeValue maxWait = MAX_WAIT_SETTING.get(settings);
            RetryHttpInitializerWrapper retryHttpInitializerWrapper;
            if (maxWait.getMillis() > 0) {
                retryHttpInitializerWrapper = new RetryHttpInitializerWrapper(credential, maxWait);
            } else {
                retryHttpInitializerWrapper = new RetryHttpInitializerWrapper(credential);
            }
            builder.setHttpRequestInitializer(retryHttpInitializerWrapper);
        } else {
            builder.setHttpRequestInitializer(credential);
        }
        this.client = builder.build();
    } catch (Exception e) {
        logger.warn("unable to start GCE discovery service", e);
        throw new IllegalArgumentException("unable to start GCE discovery service", e);
    }
    return this.client;
}
Also used : RetryHttpInitializerWrapper(org.elasticsearch.discovery.gce.RetryHttpInitializerWrapper) ComputeCredential(com.google.api.client.googleapis.compute.ComputeCredential) Compute(com.google.api.services.compute.Compute) JacksonFactory(com.google.api.client.json.jackson2.JacksonFactory) TimeValue(org.elasticsearch.common.unit.TimeValue) GeneralSecurityException(java.security.GeneralSecurityException) IOException(java.io.IOException)

Aggregations

ComputeCredential (com.google.api.client.googleapis.compute.ComputeCredential)1 JacksonFactory (com.google.api.client.json.jackson2.JacksonFactory)1 Compute (com.google.api.services.compute.Compute)1 IOException (java.io.IOException)1 GeneralSecurityException (java.security.GeneralSecurityException)1 TimeValue (org.elasticsearch.common.unit.TimeValue)1 RetryHttpInitializerWrapper (org.elasticsearch.discovery.gce.RetryHttpInitializerWrapper)1