aboutsummaryrefslogtreecommitdiffstats
path: root/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/ProxyServlet.java
blob: 72d55a4cf66c5f92bd8f9bcdb9f932871f2e692a (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
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/*******************************************************************************
 * ============LICENSE_START==================================================
 * * org.onap.dmaap
 * * ===========================================================================
 * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
 * * ===========================================================================
 * * Licensed 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.
 * * ============LICENSE_END====================================================
 * *
 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
 * *
 ******************************************************************************/


package org.onap.dmaap.datarouter.provisioning;

import static org.onap.dmaap.datarouter.provisioning.utils.HttpServletUtils.sendResponseError;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.util.Collections;
import java.util.List;
import java.util.Properties;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.IOUtils;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.entity.BasicHttpEntity;
import org.apache.http.impl.client.AbstractHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.onap.dmaap.datarouter.provisioning.utils.AafPropsUtils;
import org.onap.dmaap.datarouter.provisioning.utils.DB;
import org.onap.dmaap.datarouter.provisioning.utils.URLUtilities;

/**
 * This class is the base class for those servlets that need to proxy their requests from the standby to active server.
 * Its methods perform the proxy function to the active server. If the active server is not reachable, a 503
 * (SC_SERVICE_UNAVAILABLE) is returned.  Only DELETE/GET/PUT/POST are supported.
 *
 * @author Robert Eby
 * @version $Id: ProxyServlet.java,v 1.3 2014/03/24 18:47:10 eby Exp $
 */
@SuppressWarnings("serial")

public class ProxyServlet extends BaseServlet {

    private boolean inited = false;
    private Scheme sch;

    /**
     * Initialize this servlet, by setting up SSL.
     */
    @SuppressWarnings("deprecation")
    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        try {
            // Set up keystore
            String type = AafPropsUtils.KEYSTORE_TYPE_PROPERTY;
            String store = Main.aafPropsUtils.getKeystorePathProperty();
            String pass = Main.aafPropsUtils.getKeystorePassProperty();
            KeyStore keyStore = readStore(store, pass, type);
            // Set up truststore
            store = Main.aafPropsUtils.getTruststorePathProperty();
            pass = Main.aafPropsUtils.getTruststorePassProperty();
            if (store == null || store.length() == 0) {
                store = AafPropsUtils.DEFAULT_TRUSTSTORE;
                pass = "changeit";
            }
            KeyStore trustStore = readStore(store, pass, AafPropsUtils.TRUESTSTORE_TYPE_PROPERTY);

            // We are connecting with the node name, but the certificate will have the CNAME
            // So we need to accept a non-matching certificate name
            SSLSocketFactory socketFactory = new SSLSocketFactory(keyStore,
                    Main.aafPropsUtils.getKeystorePassProperty(), trustStore);
            socketFactory.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
            sch = new Scheme("https", 443, socketFactory);
            inited = true;
        } catch (Exception e) {
            intlogger.error("ProxyServlet.init: " + e.getMessage(), e);
        }
        intlogger.info("ProxyServlet: inited = " + inited);
    }

    private KeyStore readStore(String store, String pass, String type) throws KeyStoreException {
        KeyStore ks = KeyStore.getInstance(type);
        try (FileInputStream instream = new FileInputStream(new File(store))) {
            ks.load(instream, pass.toCharArray());
        } catch (FileNotFoundException fileNotFoundException) {
            intlogger.error("ProxyServlet.readStore: " + fileNotFoundException.getMessage(), fileNotFoundException);
        } catch (Exception x) {
            intlogger.error("READING TRUSTSTORE: " + x);
        }
        return ks;
    }

    /**
     * Return <i>true</i> if the requester has NOT set the <i>noproxy</i> CGI variable. If they have, this indicates
     * they want to forcibly turn the proxy off.
     *
     * @param req the HTTP request
     * @return true or false
     */
    boolean isProxyOK(final HttpServletRequest req) {
        String str = req.getQueryString();
        if (str != null) {
            str = str.replaceAll("&amp;", "&");
            for (String s : str.split("&")) {
                if ("noproxy".equals(s) || s.startsWith("noproxy=")) {
                    return false;
                }
            }
        }
        return true;
    }

    /**
     * Is this the standby server?  If it is, the proxy functions can be used. If not, the proxy functions should not be
     * called, and will send a response of 500 (Internal Server Error).
     *
     * @return true if this server is the standby (and hence a proxy server).
     */
    boolean isProxyServer() {
        SynchronizerTask st = SynchronizerTask.getSynchronizer();
        return st.getPodState() == SynchronizerTask.STANDBY_POD;
    }

    /**
     * Issue a proxy DELETE to the active provisioning server.
     */
    @Override
    public void doDelete(HttpServletRequest req, HttpServletResponse resp) {
        doProxy(req, resp, "DELETE");
    }

    /**
     * Issue a proxy GET to the active provisioning server.
     */
    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse resp) {
        doProxy(req, resp, "GET");
    }

    /**
     * Issue a proxy PUT to the active provisioning server.
     */
    @Override
    public void doPut(HttpServletRequest req, HttpServletResponse resp) {
        doProxy(req, resp, "PUT");
    }

    /**
     * Issue a proxy POST to the active provisioning server.
     */
    @Override
    public void doPost(HttpServletRequest req, HttpServletResponse resp) {
        doProxy(req, resp, "POST");
    }

    /**
     * Issue a proxy GET to the active provisioning server.  Unlike doGet() above, this method will allow the caller to
     * fall back to other code if the remote server is unreachable.
     *
     * @return true if the proxy succeeded
     */
    boolean doGetWithFallback(HttpServletRequest req, HttpServletResponse resp) {
        boolean rv = false;
        if (inited) {
            String url = buildUrl(req);
            intlogger.info("ProxyServlet: proxying with fallback GET " + url);
            try (AbstractHttpClient httpclient = new DefaultHttpClient()) {
                HttpRequestBase proxy = new HttpGet(url);
                try {
                    httpclient.getConnectionManager().getSchemeRegistry().register(sch);

                    // Copy request headers and request body
                    copyRequestHeaders(req, proxy);

                    // Execute the request
                    HttpResponse pxyResponse = httpclient.execute(proxy);

                    // Get response headers and body
                    int code = pxyResponse.getStatusLine().getStatusCode();
                    resp.setStatus(code);
                    copyResponseHeaders(pxyResponse, resp);
                    copyEntityContent(pxyResponse, resp);
                    rv = true;

                } catch (IOException e) {
                    intlogger.error("ProxyServlet.doGetWithFallback: " + e.getMessage(), e);
                } finally {
                    proxy.releaseConnection();
                    httpclient.getConnectionManager().shutdown();
                }
            }
        } else {
            intlogger.warn("ProxyServlet: proxy disabled");
        }
        return rv;
    }

    private void doProxy(HttpServletRequest req, HttpServletResponse resp, final String method) {
        if (inited && isProxyServer()) {
            String url = buildUrl(req);
            intlogger.info("ProxyServlet: proxying " + method + " " + url);
            try (AbstractHttpClient httpclient = new DefaultHttpClient()) {
                ProxyHttpRequest proxy = new ProxyHttpRequest(method, url);
                try {
                    httpclient.getConnectionManager().getSchemeRegistry().register(sch);

                    // Copy request headers and request body
                    copyRequestHeaders(req, proxy);

                    handlePutOrPost(req, method, proxy);

                    // Execute the request
                    HttpResponse pxyResponse = httpclient.execute(proxy);

                    // Get response headers and body
                    int code = pxyResponse.getStatusLine().getStatusCode();
                    resp.setStatus(code);
                    copyResponseHeaders(pxyResponse, resp);
                    copyEntityContent(pxyResponse, resp);
                } catch (IOException e) {
                    intlogger.warn("ProxyServlet.doProxy: " + e.getMessage(), e);
                    sendResponseError(resp, HttpServletResponse.SC_SERVICE_UNAVAILABLE, "", intlogger);
                } finally {
                    proxy.releaseConnection();
                    httpclient.getConnectionManager().shutdown();
                }
            }
        } else {
            intlogger.warn("ProxyServlet: proxy disabled");
            sendResponseError(resp, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, DB_PROBLEM_MSG, intlogger);
        }
    }

    private void handlePutOrPost(HttpServletRequest req, String method, ProxyHttpRequest proxy) throws IOException {
        if ("POST".equals(method) || "PUT".equals(method)) {
            BasicHttpEntity body = new BasicHttpEntity();
            body.setContent(req.getInputStream());
            body.setContentLength(-1);    // -1 = unknown
            proxy.setEntity(body);
        }
    }

    private String buildUrl(HttpServletRequest req) {
        StringBuilder sb = new StringBuilder("https://");
        sb.append(URLUtilities.getPeerPodName());
        sb.append(req.getRequestURI());
        String query = req.getQueryString();
        if (query != null) {
            sb.append("?").append(query);
        }
        return sb.toString();
    }

    private void copyRequestHeaders(HttpServletRequest from, HttpRequestBase to) {
        List<String> list = Collections.list(from.getHeaderNames());
        for (String name : list) {
            // Proxy code will add this one
            if (!"Content-Length".equalsIgnoreCase(name)) {
                to.addHeader(name, from.getHeader(name));
            }
        }
    }

    void copyResponseHeaders(HttpResponse from, HttpServletResponse to) {
        for (Header hdr : from.getAllHeaders()) {
            // Don't copy Date: our Jetty will add another Date header
            if (!"Date".equals(hdr.getName())) {
                to.addHeader(hdr.getName(), hdr.getValue());
            }
        }
    }

    void copyEntityContent(HttpResponse pxyResponse, HttpServletResponse resp) {
        HttpEntity entity = pxyResponse.getEntity();
        if (entity != null) {
            try (InputStream in = entity.getContent()) {
                IOUtils.copy(in, resp.getOutputStream());
            } catch (Exception e) {
                intlogger.error("ProxyServlet.copyEntityContent: " + e.getMessage(), e);
            }
        }
    }

    public static class ProxyHttpRequest extends HttpEntityEnclosingRequestBase {

        private final String method;

        ProxyHttpRequest(final String method, final String uri) {
            super();
            this.method = method;
            setURI(URI.create(uri));
        }

        @Override
        public String getMethod() {
            return method;
        }
    }
}