2 * ============LICENSE_START====================================================
4 * ===========================================================================
5 * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
6 * ===========================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END====================================================
21 package org.onap.aaf.auth.server;
23 import java.io.IOException;
24 import java.net.InetAddress;
25 import java.util.Properties;
27 import javax.servlet.Filter;
28 import javax.servlet.FilterChain;
29 import javax.servlet.ServletException;
30 import javax.servlet.ServletRequest;
31 import javax.servlet.ServletResponse;
32 import javax.servlet.http.HttpServletRequest;
33 import javax.servlet.http.HttpServletResponse;
35 import org.eclipse.jetty.http.HttpVersion;
36 import org.eclipse.jetty.server.HttpConfiguration;
37 import org.eclipse.jetty.server.HttpConnectionFactory;
38 import org.eclipse.jetty.server.Request;
39 import org.eclipse.jetty.server.SecureRequestCustomizer;
40 import org.eclipse.jetty.server.Server;
41 import org.eclipse.jetty.server.ServerConnector;
42 import org.eclipse.jetty.server.SslConnectionFactory;
43 import org.eclipse.jetty.server.handler.AbstractHandler;
44 import org.eclipse.jetty.util.ssl.SslContextFactory;
45 import org.onap.aaf.auth.org.OrganizationException;
46 import org.onap.aaf.auth.rserv.RServlet;
47 import org.onap.aaf.cadi.CadiException;
48 import org.onap.aaf.cadi.LocatorException;
49 import org.onap.aaf.cadi.Access.Level;
50 import org.onap.aaf.cadi.config.Config;
51 import org.onap.aaf.cadi.config.SecurityInfo;
52 import org.onap.aaf.misc.env.Trans;
53 import org.onap.aaf.misc.env.util.Split;
54 import org.onap.aaf.misc.rosetta.env.RosettaEnv;
57 public class JettyServiceStarter<ENV extends RosettaEnv, TRANS extends Trans> extends AbsServiceStarter<ENV,TRANS> {
59 private boolean secure;
61 public JettyServiceStarter(final AbsService<ENV,TRANS> service) throws OrganizationException {
67 * Specifically set this Service starter to Insecure (HTTP) Mode.
70 public JettyServiceStarter<ENV,TRANS> insecure() {
76 // public void _propertyAdjustment() {
77 // Properties props = access().getProperties();
78 // Object temp = null;
79 // // Critical - if no Security Protocols set, then set it. We'll just get messed up if not
80 // if((temp=props.get(Config.CADI_PROTOCOLS))==null) {
81 // if((temp=props.get(Config.HTTPS_PROTOCOLS))==null) {
82 // props.put(Config.CADI_PROTOCOLS, SecurityInfo.HTTPS_PROTOCOLS_DEFAULT);
84 // props.put(Config.CADI_PROTOCOLS, temp);
88 // if("1.7".equals(System.getProperty("java.specification.version"))) {
89 // System.setProperty(Config.HTTPS_CIPHER_SUITES, Config.HTTPS_CIPHER_SUITES_DEFAULT);
91 // System.setProperty(Config.HTTPS_CIPHER_SUITES, temp.toString());
95 public void _propertyAdjustment() {
96 // System.setProperty("com.sun.management.jmxremote.port", "8081");
97 Properties props = access().getProperties();
98 Object httpproto = null;
99 // Critical - if no Security Protocols set, then set it. We'll just get messed up if not
100 if((httpproto=props.get(Config.CADI_PROTOCOLS))==null) {
101 if((httpproto=props.get(Config.HTTPS_PROTOCOLS))==null) {
102 props.put(Config.CADI_PROTOCOLS, (httpproto=SecurityInfo.HTTPS_PROTOCOLS_DEFAULT));
104 props.put(Config.CADI_PROTOCOLS, httpproto);
108 if("1.7".equals(System.getProperty("java.specification.version")) && (httpproto==null || (httpproto instanceof String && ((String)httpproto).contains("TLSv1.2")))) {
109 System.setProperty(Config.HTTPS_CIPHER_SUITES, Config.HTTPS_CIPHER_SUITES_DEFAULT);
114 public void _start(RServlet<TRANS> rserv) throws Exception {
115 final String hostname = access().getProperty(Config.HOSTNAME, "localhost");
116 final int port = Integer.parseInt(access().getProperty("port","0"));
117 final String keystore = access().getProperty(Config.CADI_KEYSTORE, null);
118 final int IDLE_TIMEOUT = Integer.parseInt(access().getProperty(Config.AAF_CONN_IDLE_TIMEOUT, Config.AAF_CONN_IDLE_TIMEOUT_DEF));
119 Server server = new Server();
121 ServerConnector conn;
123 if(!secure || keystore==null) {
124 conn = new ServerConnector(server);
129 String keystorePassword = access().getProperty(Config.CADI_KEYSTORE_PASSWORD, null);
130 if(keystorePassword==null) {
131 throw new CadiException("No Keystore Password configured for " + keystore);
133 SslContextFactory sslContextFactory = new SslContextFactory();
134 sslContextFactory.setKeyStorePath(keystore);
136 sslContextFactory.setKeyStorePassword(temp=access().decrypt(keystorePassword, true)); // don't allow unencrypted
137 sslContextFactory.setKeyManagerPassword(temp);
138 temp=null; // don't leave lying around
140 String truststore = access().getProperty(Config.CADI_TRUSTSTORE, null);
141 if(truststore!=null) {
142 String truststorePassword = access().getProperty(Config.CADI_TRUSTSTORE_PASSWORD, null);
143 if(truststorePassword==null) {
144 throw new CadiException("No Truststore Password configured for " + truststore);
146 sslContextFactory.setTrustStorePath(truststore);
147 sslContextFactory.setTrustStorePassword(access().decrypt(truststorePassword, true));
149 // Be able to accept only certain protocols, i.e. TLSv1.1+
150 final String[] protocols = Split.splitTrim(',', access().getProperty(Config.CADI_PROTOCOLS, SecurityInfo.HTTPS_PROTOCOLS_DEFAULT));
151 sslContextFactory.setIncludeProtocols(protocols);
153 // Want to use Client Certificates, if they exist.
154 sslContextFactory.setWantClientAuth(true);
156 // Optional future checks.
157 // sslContextFactory.setValidateCerts(true);
158 // sslContextFactory.setValidatePeerCerts(true);
159 // sslContextFactory.setEnableCRLDP(false);
160 // sslContextFactory.setEnableOCSP(false);
161 String certAlias = access().getProperty(Config.CADI_ALIAS, null);
162 if(certAlias!=null) {
163 sslContextFactory.setCertAlias(certAlias);
166 HttpConfiguration httpConfig = new HttpConfiguration();
167 httpConfig.setSecureScheme(protocol);
168 httpConfig.setSecurePort(port);
169 httpConfig.addCustomizer(new SecureRequestCustomizer());
170 // httpConfig.setOutputBufferSize(32768); Not sure why take this setting
172 conn = new ServerConnector(server,
173 new SslConnectionFactory(sslContextFactory,HttpVersion.HTTP_1_1.asString()),
174 new HttpConnectionFactory(httpConfig)
179 // TODO trying to figure out how to set up/log ports
180 // MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer();
181 // MBeanContainer mbContainer=new MBeanContainer(mbeanServer);
182 // server.addEventListener(mbContainer);
183 // server.addBean(mbContainer);
185 // Add loggers MBean to server (will be picked up by MBeanContainer above)
186 // server.addBean(Log.getLog());
188 conn.setHost(hostname);
190 conn.setIdleTimeout(IDLE_TIMEOUT);
191 server.addConnector(conn);
193 server.setHandler(new AbstractHandler() {
194 private FilterChain fc = buildFilterChain(service,new FilterChain() {
196 public void doFilter(ServletRequest req, ServletResponse resp) throws IOException, ServletException {
197 rserv.service(req, resp);
202 public void handle(String target, Request baseRequest, HttpServletRequest hreq, HttpServletResponse hresp) throws IOException, ServletException {
204 fc.doFilter(hreq,hresp);
205 } catch (Exception e) {
206 service.access.log(e, "Error Processing " + target);
207 hresp.setStatus(500 /* Service Error */);
209 baseRequest.setHandled(true);
215 access().printf(Level.INIT, "Starting service on %s:%d (%s)",hostname,port,InetAddress.getLocalHost().getHostAddress());
217 access().log(Level.INIT,server.dump());
218 } catch (Exception e) {
219 access().log(e,"Error starting " + service.app_name);
223 register(service.registrants(port));
224 access().printf(Level.INIT, "Starting Jetty Service for %s, version %s, on %s://%s:%d", service.app_name,service.app_version,protocol,hostname,port);
225 } catch(Exception e) {
226 access().log(e,"Error registering " + service.app_name);
227 // Question: Should Registered Services terminate?
232 private FilterChain buildFilterChain(final AbsService<?,?> as, final FilterChain doLast) throws CadiException, LocatorException {
233 Filter[] filters = as.filters();
234 FilterChain fc = doLast;
235 for(int i=filters.length-1;i>=0;--i) {
236 fc = new FCImpl(filters[i],fc);
241 private class FCImpl implements FilterChain {
243 private FilterChain next;
245 public FCImpl(final Filter f, final FilterChain fc) {
251 public void doFilter(ServletRequest req, ServletResponse resp) throws IOException, ServletException {
252 f.doFilter(req,resp, next);