DR AAF CADI integration
[dmaap/datarouter.git] / datarouter-node / src / main / java / org / onap / dmaap / datarouter / node / NodeMain.java
1 /*******************************************************************************
2  * ============LICENSE_START==================================================
3  * * org.onap.dmaap
4  * * ===========================================================================
5  * * Copyright © 2017 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
10  * *
11  *  *      http://www.apache.org/licenses/LICENSE-2.0
12  * *
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====================================================
19  * *
20  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  * *
22  ******************************************************************************/
23
24 package org.onap.dmaap.datarouter.node;
25
26 import org.apache.log4j.Logger;
27 import org.eclipse.jetty.http.HttpVersion;
28 import org.eclipse.jetty.server.*;
29 import org.eclipse.jetty.servlet.FilterHolder;
30 import org.eclipse.jetty.servlet.ServletContextHandler;
31 import org.eclipse.jetty.servlet.ServletHolder;
32 import org.eclipse.jetty.util.ssl.SslContextFactory;
33 import org.onap.aaf.cadi.PropAccess;
34
35 import javax.servlet.DispatcherType;
36 import java.io.IOException;
37 import java.io.InputStream;
38 import java.util.EnumSet;
39 import java.util.Properties;
40
41 /**
42  * The main starting point for the Data Router node
43  */
44 public class NodeMain {
45
46     private NodeMain() {
47     }
48
49     private static Logger nodeMainLogger = Logger.getLogger("org.onap.dmaap.datarouter.node.NodeMain");
50
51     class Inner {
52         InputStream getCadiProps() {
53             InputStream in = null;
54             try {
55                 in = getClass().getClassLoader().getResourceAsStream("drNodeCadi.properties");
56             } catch (Exception e) {
57                 nodeMainLogger.error("Exception in Inner.getCadiProps() method " + e.getMessage());
58             }
59             return in;
60         }
61     }
62
63     private static class WaitForConfig implements Runnable {
64
65         private NodeConfigManager localNodeConfigManager;
66
67         WaitForConfig(NodeConfigManager ncm) {
68             this.localNodeConfigManager = ncm;
69         }
70
71         public synchronized void run() {
72             notify();
73         }
74
75         synchronized void waitForConfig() {
76             localNodeConfigManager.registerConfigTask(this);
77             while (!localNodeConfigManager.isConfigured()) {
78                 nodeMainLogger.info("NODE0003 Waiting for Node Configuration");
79                 try {
80                     wait();
81                 } catch (Exception exception) {
82                     nodeMainLogger
83                             .debug("NodeMain: waitForConfig exception. Exception Message:- " + exception.toString(),
84                                     exception);
85                 }
86             }
87             localNodeConfigManager.deregisterConfigTask(this);
88             nodeMainLogger.info("NODE0004 Node Configuration Data Received");
89         }
90     }
91
92     private static Delivery delivery;
93     private static NodeConfigManager nodeConfigManager;
94
95     /**
96      * Reset the retry timer for a subscription
97      */
98     static void resetQueue(String subid, String ip) {
99         delivery.resetQueue(nodeConfigManager.getSpoolDir(subid, ip));
100     }
101
102     /**
103      * Start the data router.
104      * <p>
105      * The location of the node configuration file can be set using the org.onap.dmaap.datarouter.node.properties system
106      * property.  By default, it is "/opt/app/datartr/etc/node.properties".
107      */
108     public static void main(String[] args) throws Exception {
109         nodeMainLogger.info("NODE0001 Data Router Node Starting");
110         IsFrom.setDNSCache();
111         nodeConfigManager = NodeConfigManager.getInstance();
112         nodeMainLogger.info("NODE0002 I am " + nodeConfigManager.getMyName());
113         (new WaitForConfig(nodeConfigManager)).waitForConfig();
114         delivery = new Delivery(nodeConfigManager);
115         new LogManager(nodeConfigManager);
116
117         Server server = new Server();
118
119         // HTTP configuration
120         HttpConfiguration httpConfiguration = new HttpConfiguration();
121         httpConfiguration.setRequestHeaderSize(2048);
122
123         // HTTP connector
124         try (ServerConnector httpServerConnector = new ServerConnector(server, new HttpConnectionFactory(httpConfiguration))) {
125             httpServerConnector.setPort(nodeConfigManager.getHttpPort());
126             httpServerConnector.setIdleTimeout(2000);
127
128             // HTTPS configuration
129             SslContextFactory sslContextFactory = new SslContextFactory();
130             sslContextFactory.setKeyStoreType(nodeConfigManager.getKSType());
131             sslContextFactory.setKeyStorePath(nodeConfigManager.getKSFile());
132             sslContextFactory.setKeyStorePassword(nodeConfigManager.getKSPass());
133             sslContextFactory.setKeyManagerPassword(nodeConfigManager.getKPass());
134
135             //SP-6 : Fixes for SDV scan to exclude/remove DES/3DES ciphers are taken care by upgrading jdk in descriptor.xml
136             sslContextFactory.setExcludeCipherSuites(
137                     "SSL_RSA_WITH_DES_CBC_SHA",
138                     "SSL_DHE_RSA_WITH_DES_CBC_SHA",
139                     "SSL_DHE_DSS_WITH_DES_CBC_SHA",
140                     "SSL_RSA_EXPORT_WITH_RC4_40_MD5",
141                     "SSL_RSA_EXPORT_WITH_DES40_CBC_SHA",
142                     "SSL_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA",
143                     "SSL_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA"
144             );
145
146             sslContextFactory.addExcludeProtocols("SSLv3");
147             sslContextFactory.setIncludeProtocols(nodeConfigManager.getEnabledprotocols());
148             nodeMainLogger.info("NODE00004 Unsupported protocols node server:-" + String.join(",", sslContextFactory.getExcludeProtocols()));
149             nodeMainLogger.info("NODE00004 Supported protocols node server:-" + String.join(",", sslContextFactory.getIncludeProtocols()));
150             nodeMainLogger.info("NODE00004 Unsupported ciphers node server:-" + String.join(",", sslContextFactory.getExcludeCipherSuites()));
151
152             HttpConfiguration httpsConfiguration = new HttpConfiguration(httpConfiguration);
153             httpsConfiguration.setRequestHeaderSize(8192);
154
155             SecureRequestCustomizer secureRequestCustomizer = new SecureRequestCustomizer();
156             secureRequestCustomizer.setStsMaxAge(2000);
157             secureRequestCustomizer.setStsIncludeSubDomains(true);
158             httpsConfiguration.addCustomizer(secureRequestCustomizer);
159
160             // HTTPS connector
161             try (ServerConnector httpsServerConnector = new ServerConnector(server,
162                     new SslConnectionFactory(sslContextFactory, HttpVersion.HTTP_1_1.asString()),
163                     new HttpConnectionFactory(httpsConfiguration))) {
164
165                 httpsServerConnector.setPort(nodeConfigManager.getHttpsPort());
166                 httpsServerConnector.setIdleTimeout(3600000);
167                 httpsServerConnector.setAcceptQueueSize(2);
168
169                 //Context Handler
170                 ServletContextHandler servletContextHandler = new ServletContextHandler(0);
171                 servletContextHandler.setContextPath("/");
172                 servletContextHandler.addServlet(new ServletHolder(new NodeServlet(delivery)), "/*");
173
174                 //CADI Filter activation check
175                 if (nodeConfigManager.getCadiEnabeld()) {
176                     Properties cadiProperties = new Properties();
177                     try {
178                         Inner obj = new NodeMain().new Inner();
179                         InputStream in = obj.getCadiProps();
180                         cadiProperties.load(in);
181                     } catch (IOException e1) {
182                         nodeMainLogger.error("NODE00005 Exception in NodeMain.Main() loading CADI properties " + e1.getMessage());
183                     }
184                     cadiProperties.setProperty("aaf_locate_url", nodeConfigManager.getAafURL());
185                     nodeMainLogger.info("NODE00005  aaf_url set to - " + cadiProperties.getProperty("aaf_url"));
186
187                     PropAccess access = new PropAccess(cadiProperties);
188                     servletContextHandler.addFilter(new FilterHolder(new DRNodeCadiFilter(true, access)), "/*", EnumSet.of(DispatcherType.REQUEST));
189                 }
190
191                 server.setHandler(servletContextHandler);
192                 server.setConnectors(new Connector[]{httpServerConnector, httpsServerConnector});
193             }
194         }
195
196         try {
197             server.start();
198             nodeMainLogger.info("NODE00006 Node Server started-" + server.getState());
199         } catch (Exception e) {
200             nodeMainLogger.info("NODE00006 Jetty failed to start. Reporting will we unavailable", e);
201         }
202         server.join();
203         nodeMainLogger.info("NODE00007 Node Server joined - " + server.getState());
204     }
205 }