summaryrefslogtreecommitdiff
path: root/plugins/discovery-gce/src/main/java/org/elasticsearch/plugin/discovery/gce/GceDiscoveryPlugin.java
blob: 5f01a98a5f23b863d5d3f355a42a5775e689e426 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
 * 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.plugin.discovery.gce;

import com.google.api.client.http.HttpHeaders;
import com.google.api.client.util.ClassInfo;

import org.elasticsearch.SpecialPermission;
import org.elasticsearch.cloud.gce.GceComputeService;
import org.elasticsearch.cloud.gce.GceModule;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.component.LifecycleComponent;
import org.elasticsearch.common.inject.Module;
import org.elasticsearch.common.logging.ESLogger;
import org.elasticsearch.common.logging.Loggers;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.discovery.DiscoveryModule;
import org.elasticsearch.discovery.gce.GceDiscovery;
import org.elasticsearch.discovery.gce.GceUnicastHostsProvider;
import org.elasticsearch.plugins.Plugin;

import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class GceDiscoveryPlugin extends Plugin {
    static {
        /*
         * GCE's http client changes access levels because its silly and we
         * can't allow that on any old stack stack so we pull it here, up front,
         * so we can cleanly check the permissions for it. Without this changing
         * the permission can fail if any part of core is on the stack because
         * our plugin permissions don't allow core to "reach through" plugins to
         * change the permission. Because that'd be silly.
         */
        SecurityManager sm = System.getSecurityManager();
        if (sm != null) {
            sm.checkPermission(new SpecialPermission());
        }
        AccessController.doPrivileged(new PrivilegedAction<Void>() {
            @Override
            public Void run() {
                ClassInfo.of(HttpHeaders.class, true);
                return null;
            }
        });
    }

    private final Settings settings;
    protected final ESLogger logger = Loggers.getLogger(GceDiscoveryPlugin.class);

    public GceDiscoveryPlugin(Settings settings) {
        this.settings = settings;
    }

    @Override
    public String name() {
        return "discovery-gce";
    }

    @Override
    public String description() {
        return "Cloud Google Compute Engine Discovery Plugin";
    }

    @Override
    public Collection<Module> nodeModules() {
        List<Module> modules = new ArrayList<>();
        if (isDiscoveryAlive(settings, logger)) {
            modules.add(new GceModule());
        }
        return modules;
    }

    @Override
    public Collection<Class<? extends LifecycleComponent>> nodeServices() {
        Collection<Class<? extends LifecycleComponent>> services = new ArrayList<>();
        if (isDiscoveryAlive(settings, logger)) {
            services.add(GceModule.getComputeServiceImpl());
        }
        return services;
    }

    public void onModule(DiscoveryModule discoveryModule) {
        if (isDiscoveryAlive(settings, logger)) {
            discoveryModule.addDiscoveryType("gce", GceDiscovery.class);
            discoveryModule.addUnicastHostProvider(GceUnicastHostsProvider.class);
        }
    }

    /**
     * Check if discovery is meant to start
     *
     * @return true if we can start gce discovery features
     */
    public static boolean isDiscoveryAlive(Settings settings, ESLogger logger) {
        // User set discovery.type: gce
        if (GceDiscovery.GCE.equalsIgnoreCase(settings.get("discovery.type")) == false) {
            logger.debug("discovery.type not set to {}", GceDiscovery.GCE);
            return false;
        }

        if (checkProperty(GceComputeService.Fields.PROJECT, settings.get(GceComputeService.Fields.PROJECT), logger) == false ||
                checkProperty(GceComputeService.Fields.ZONE, settings.getAsArray(GceComputeService.Fields.ZONE), logger) == false) {
            logger.debug("one or more gce discovery settings are missing. " +
                            "Check elasticsearch.yml file. Should have [{}] and [{}].",
                    GceComputeService.Fields.PROJECT,
                    GceComputeService.Fields.ZONE);
            return false;
        }

        logger.trace("all required properties for gce discovery are set!");

        return true;
    }

    private static boolean checkProperty(String name, String value, ESLogger logger) {
        if (!Strings.hasText(value)) {
            logger.warn("{} is not set.", name);
            return false;
        }
        return true;
    }

    private static boolean checkProperty(String name, String[] values, ESLogger logger) {
        if (values == null || values.length == 0) {
            logger.warn("{} is not set.", name);
            return false;
        }
        return true;
    }

}