aboutsummaryrefslogtreecommitdiffstats
path: root/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/proxy/ConfigServerRestExecutorImpl.java
blob: 8df28095e5133d3466e8fda784f9677f828ad04c (plain) (blame)
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.controller.proxy;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.inject.Inject;
import com.yahoo.config.provision.Environment;
import com.yahoo.jdisc.http.HttpRequest.Method;
import com.yahoo.log.LogLevel;
import com.yahoo.vespa.athenz.api.AthenzIdentity;
import com.yahoo.vespa.athenz.utils.AthenzIdentities;
import com.yahoo.vespa.athenz.tls.AthenzIdentityVerifier;
import com.yahoo.vespa.hosted.controller.api.integration.athenz.AthenzSslContextProvider;
import com.yahoo.vespa.hosted.controller.api.integration.zone.ZoneId;
import com.yahoo.vespa.hosted.controller.api.integration.zone.ZoneList;
import com.yahoo.vespa.hosted.controller.api.integration.zone.ZoneRegistry;
import org.apache.http.Header;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPatch;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.conn.ssl.X509HostnameVerifier;
import org.apache.http.entity.InputStreamEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

import javax.net.ssl.SSLException;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocket;
import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.net.URI;
import java.security.cert.X509Certificate;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.logging.Logger;

import static java.util.Collections.singleton;

/**
 * @author Haakon Dybdahl
 * @author bjorncs
 */
@SuppressWarnings("unused") // Injected
public class ConfigServerRestExecutorImpl implements ConfigServerRestExecutor {

    private static final Logger log = Logger.getLogger(ConfigServerRestExecutorImpl.class.getName());

    private static final Duration PROXY_REQUEST_TIMEOUT = Duration.ofSeconds(10);
    private static final Set<String> HEADERS_TO_COPY = Collections.singleton("X-HTTP-Method-Override");

    private final ZoneRegistry zoneRegistry;
    private final AthenzSslContextProvider sslContextProvider;

    @Inject
    public ConfigServerRestExecutorImpl(ZoneRegistry zoneRegistry,
                                        AthenzSslContextProvider sslContextProvider) {
        this.zoneRegistry = zoneRegistry;
        this.sslContextProvider = sslContextProvider;
    }

    @Override
    public ProxyResponse handle(ProxyRequest proxyRequest) throws ProxyException {
        if (proxyRequest.isDiscoveryRequest()) {
            return createDiscoveryResponse(proxyRequest);
        }

        ZoneId zoneId = ZoneId.from(proxyRequest.getEnvironment(), proxyRequest.getRegion());

        // Make a local copy of the list as we want to manipulate it in case of ping problems.
        List<URI> allServers = new ArrayList<>(zoneRegistry.getConfigServerSecureUris(zoneId));

        StringBuilder errorBuilder = new StringBuilder();
        if (queueFirstServerIfDown(allServers, proxyRequest)) {
            errorBuilder.append("Change ordering due to failed ping.");
        }
        for (URI uri : allServers) {
            Optional<ProxyResponse> proxyResponse = proxyCall(uri, proxyRequest, errorBuilder);
            if (proxyResponse.isPresent()) {
                return proxyResponse.get();
            }
        }
        // TODO Add logging, for now, experimental and we want to not add more noise.
        throw new ProxyException(ErrorResponse.internalServerError("Failed talking to config servers: "
                + errorBuilder.toString()));
    }

    private static class DiscoveryResponseStructure {
        public List<String> uris = new ArrayList<>();
    }

    private ProxyResponse createDiscoveryResponse(ProxyRequest proxyRequest) {
        ObjectMapper mapper = new ObjectMapper();
        DiscoveryResponseStructure responseStructure = new DiscoveryResponseStructure();
        String environmentName = proxyRequest.getEnvironment();

        ZoneList zones = zoneRegistry.zones().all();
        if ( ! environmentName.isEmpty())
            zones = zones.in(Environment.from(environmentName));

        for (ZoneId zoneId : zones.ids()) {
            responseStructure.uris.add(proxyRequest.getScheme() + "://" + proxyRequest.getControllerPrefix() +
                                       zoneId.environment().name() + "/" + zoneId.region().value());
        }
        JsonNode node = mapper.valueToTree(responseStructure);
        return new ProxyResponse(proxyRequest, node.toString(), 200, Optional.empty(), "application/json");
    }

    private static String removeFirstSlashIfAny(String url) {
        if (url.startsWith("/")) {
            return url.substring(1);
        }
        return url;
    }

    private Optional<ProxyResponse> proxyCall(URI uri, ProxyRequest proxyRequest, StringBuilder errorBuilder)
            throws ProxyException {
        String fullUri = uri.toString() + removeFirstSlashIfAny(proxyRequest.getConfigServerRequest());
        final HttpRequestBase requestBase = createHttpBaseRequest(
                proxyRequest.getMethod(), fullUri, proxyRequest.getData());
        // Empty list of headers to copy for now, add headers when needed, or rewrite logic.
        copyHeaders(proxyRequest.getHeaders(), requestBase);

        RequestConfig config = RequestConfig.custom()
                .setConnectTimeout((int) PROXY_REQUEST_TIMEOUT.toMillis())
                .setConnectionRequestTimeout((int) PROXY_REQUEST_TIMEOUT.toMillis())
                .setSocketTimeout((int) PROXY_REQUEST_TIMEOUT.toMillis()).build();
        try (
                CloseableHttpClient client = createHttpClient(config, sslContextProvider, zoneRegistry, proxyRequest);
                CloseableHttpResponse response = client.execute(requestBase);
        ) {
            String content = getContent(response);
            int status = response.getStatusLine().getStatusCode();
            if (status / 100 == 5) {
                errorBuilder.append("Talking to server ").append(uri.getHost());
                errorBuilder.append(", got ").append(status).append(" ")
                        .append(content).append("\n");
                log.log(LogLevel.DEBUG, () -> String.format("Got response from %s with status code %d and content:\n %s",
                                                            uri.getHost(), status, content));
                return Optional.empty();
            }
            final Header contentHeader = response.getLastHeader("Content-Type");
            final String contentType;
            if (contentHeader != null && contentHeader.getValue() != null && ! contentHeader.getValue().isEmpty()) {
                contentType = contentHeader.getValue().replace("; charset=UTF-8","");
            } else {
                contentType = "application/json";
            }
            // Send response back
            return Optional.of(new ProxyResponse(proxyRequest, content, status, Optional.of(uri), contentType));
        } catch (Exception e) {
            errorBuilder.append("Talking to server ").append(uri.getHost());
            errorBuilder.append(" got exception ").append(e.getMessage());
            log.log(LogLevel.DEBUG, e, () -> "Got exception while sending request to " + uri.getHost());
            return Optional.empty();
        }
    }

    private static String getContent(CloseableHttpResponse response) {
        return Optional.ofNullable(response.getEntity())
                .map(entity ->
                     {
                         try {
                             return EntityUtils.toString(entity);
                         } catch (IOException e) {
                             throw new UncheckedIOException(e);
                         }
                     }
                ).orElse("");
    }

    private static HttpRequestBase createHttpBaseRequest(String method, String uri, InputStream data) throws ProxyException {
        Method enumMethod =  Method.valueOf(method);
        switch (enumMethod) {
            case GET:
                return new HttpGet(uri);
            case POST:
                HttpPost post = new HttpPost(uri);
                if (data != null) {
                    post.setEntity(new InputStreamEntity(data));
                }
                return post;
            case PUT:
                HttpPut put = new HttpPut(uri);
                if (data != null) {
                    put.setEntity(new InputStreamEntity(data));
                }
                return put;
            case DELETE:
                return new HttpDelete(uri);
            case PATCH:
                HttpPatch patch = new HttpPatch(uri);
                if (data != null) {
                    patch.setEntity(new InputStreamEntity(data));
                }
                return patch;
            default:
                throw new ProxyException(ErrorResponse.methodNotAllowed("Will not proxy such calls."));
        }
    }

    private static void copyHeaders(Map<String, List<String>> headers, HttpRequestBase toRequest) {
        for (Map.Entry<String, List<String>> headerEntry : headers.entrySet()) {
            if (HEADERS_TO_COPY.contains(headerEntry.getKey())) {
                for (String value : headerEntry.getValue()) {
                    toRequest.addHeader(headerEntry.getKey(), value);
                }
            }
        }
    }

    /**
     * During upgrade, one server can be down, this is normal. Therefor we do a quick ping on the first server,
     * if it is not responding, we try the other servers first. False positive/negatives are not critical,
     * but will increase latency to some extent.
     */
    private boolean queueFirstServerIfDown(List<URI> allServers, ProxyRequest proxyRequest) {
        if (allServers.size() < 2) {
            return false;
        }
        URI uri = allServers.get(0);
        HttpGet httpget = new HttpGet(uri);

        int timeout = 500;
        RequestConfig config = RequestConfig.custom()
                .setConnectTimeout(timeout)
                .setConnectionRequestTimeout(timeout)
                .setSocketTimeout(timeout).build();
        try (
                CloseableHttpClient client = createHttpClient(config, sslContextProvider, zoneRegistry, proxyRequest);
                CloseableHttpResponse response = client.execute(httpget);

        ) {
            if (response.getStatusLine().getStatusCode() == 200) {
                return false;
            }

        } catch (IOException e) {
            // We ignore this, if server is restarting this might happen.
        }
        // Some error happened, move this server to the back. The other servers should be running.
        allServers.remove(0);
        allServers.add(uri);
        return true;
    }

    private static CloseableHttpClient createHttpClient(RequestConfig config,
                                                        AthenzSslContextProvider sslContextProvider,
                                                        ZoneRegistry zoneRegistry,
                                                        ProxyRequest proxyRequest) {
        AthenzIdentityVerifier hostnameVerifier =
                new AthenzIdentityVerifier(
                        singleton(
                                zoneRegistry.getConfigserverAthenzService(
                                        ZoneId.from(proxyRequest.getEnvironment(), proxyRequest.getRegion()))));
        return HttpClientBuilder.create()
                .setUserAgent("config-server-proxy-client")
                .setSslcontext(sslContextProvider.get())
                .setHostnameVerifier(new AthenzIdentityVerifierAdapter(hostnameVerifier))
                .setDefaultRequestConfig(config)
                .build();
    }

    private static class AthenzIdentityVerifierAdapter implements X509HostnameVerifier {

        private final AthenzIdentityVerifier verifier;

        AthenzIdentityVerifierAdapter(AthenzIdentityVerifier verifier) {
            this.verifier = verifier;
        }

        @Override
        public boolean verify(String hostname, SSLSession sslSession) {
            return verifier.verify(hostname, sslSession);
        }

        @Override
        public void verify(String host, SSLSocket ssl) { /* All sockets accepted */}

        @Override
        public void verify(String hostname, X509Certificate certificate) throws SSLException {
            AthenzIdentity identity = AthenzIdentities.from(certificate);
            if (!verifier.isTrusted(identity)) {
                throw new SSLException("Athenz identity is not trusted: " + identity.getFullName());
            }
        }

        @Override
        public void verify(String hostname, String[] cns, String[] subjectAlts) throws SSLException {
            AthenzIdentity identity = AthenzIdentities.from(cns[0]);
            if (!verifier.isTrusted(identity)) {
                throw new SSLException("Athenz identity is not trusted: " + identity.getFullName());
            }
        }
    }

}