Honeynode test tool
[transportpce.git] / tests / honeynode / restconf / src / main / java / io / fd / honeycomb / northbound / restconf / HttpsConnectorProvider.java
1 /*
2  * Copyright (c) 2016 Cisco and/or its affiliates.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package io.fd.honeycomb.northbound.restconf;
18
19 import com.google.inject.Inject;
20 import io.fd.honeycomb.binding.init.ProviderTrait;
21
22 import java.net.URL;
23
24 import org.eclipse.jetty.http.HttpVersion;
25 import org.eclipse.jetty.server.HttpConnectionFactory;
26 import org.eclipse.jetty.server.Server;
27 import org.eclipse.jetty.server.ServerConnector;
28 import org.eclipse.jetty.server.SslConnectionFactory;
29 import org.eclipse.jetty.util.ssl.SslContextFactory;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 final class HttpsConnectorProvider extends ProviderTrait<ServerConnector> {
34
35     private static final Logger LOG = LoggerFactory.getLogger(HttpsConnectorProvider.class);
36
37     @Inject
38     private RestconfConfiguration cfg;
39     @Inject
40     private Server server;
41
42     @Override
43     protected ServerConnector create() {
44         if (!cfg.isRestconfHttpsEnabled()) {
45             LOG.debug("RESTCONF HTTPS disabled, skipping initialization");
46             return null;
47         }
48         LOG.info("Starting RESTCONF HTTPS");
49         // SSL Context Factory
50         // Based on:
51         // https://github.com/eclipse/jetty.project/blob/jetty-9.3.x/examples/embedded/src/main/java/org/eclipse/jetty/embedded/LikeJettyXml.java
52         // https://wiki.eclipse.org/Jetty/Howto/Configure_SSL#Loading_Keys_and_Certificates_via_PKCS12
53         // Keystore created with:
54         // openssl genrsa -des3 -out honeycomb.key
55         // openssl req -new -x509 -key honeycomb.key -out honeycomb.crt
56         // openssl pkcs12 -inkey honeycomb.key -in honeycomb.crt -export -out honeycomb.pkcs12
57         // keytool -importkeystore -srckeystore honeycomb.pkcs12 -srcstoretype PKCS12 -destkeystore honeycomb-keystore
58         SslContextFactory sslContextFactory = new SslContextFactory();
59         URL keystoreURL = getClass().getResource(cfg.restconfKeystore.get());
60         sslContextFactory.setKeyStorePath(keystoreURL.getPath());
61         sslContextFactory.setKeyStorePassword(cfg.keystorePassword.get());
62         sslContextFactory.setKeyManagerPassword((cfg.keystoreManagerPassword.get()));
63         URL truststoreURL = getClass().getResource(cfg.restconfTruststore.get());
64         sslContextFactory.setTrustStorePath(truststoreURL.getPath());
65         sslContextFactory.setTrustStorePassword((cfg.truststorePassword.get()));
66         // TODO HONEYCOMB-167 make this more configurable
67         sslContextFactory.setExcludeCipherSuites("SSL_RSA_WITH_DES_CBC_SHA", "SSL_DHE_RSA_WITH_DES_CBC_SHA",
68                 "SSL_DHE_DSS_WITH_DES_CBC_SHA", "SSL_RSA_EXPORT_WITH_RC4_40_MD5",
69                 "SSL_RSA_EXPORT_WITH_DES40_CBC_SHA",
70                 "SSL_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA", "SSL_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA");
71
72         // SSL Connector
73         ServerConnector sslConnector =
74                 new ServerConnector(server, cfg.httpsAcceptorsSize.get(), cfg.httpsSelectorsSize.get(),
75                         // The ssl connection factory delegates the real processing to http connection factory
76                         new SslConnectionFactory(sslContextFactory, HttpVersion.HTTP_1_1.asString()),
77                         // That's why http connection factory is also required here
78                         // Order is IMPORTANT here
79                         new HttpConnectionFactory()
80                 );
81         sslConnector.setHost(cfg.restconfHttpsBindingAddress.get());
82         sslConnector.setPort(cfg.restconfHttpsPort.get());
83         server.addConnector(sslConnector);
84         return sslConnector;
85     }
86 }