CADI and a few small updates.
[music.git] / src / main / java / org / onap / music / MusicApplication.java
1 /*
2  * ============LICENSE_START==========================================
3  * org.onap.music
4  * ===================================================================
5  *  Copyright (c) 2017 AT&T Intellectual Property
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  * 
19  * ============LICENSE_END=============================================
20  * ====================================================================
21  */
22
23 package org.onap.music;
24
25 import javax.servlet.Filter;
26 import javax.servlet.FilterChain;
27 import javax.servlet.ServletException;
28 import javax.servlet.ServletRequest;
29 import javax.servlet.ServletResponse;
30
31 import org.onap.aaf.cadi.PropAccess;
32 import org.onap.music.authentication.CadiAuthFilter;
33 import org.onap.music.authentication.MusicAuthorizationFilter;
34 import org.onap.music.eelf.logging.EELFLoggerDelegate;
35 import org.onap.music.eelf.logging.MusicLoggingServletFilter;
36 import org.onap.music.main.MusicUtil;
37 import org.onap.music.main.PropertiesLoader;
38 import org.springframework.beans.factory.annotation.Autowired;
39 import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
40 import org.springframework.boot.autoconfigure.SpringBootApplication;
41 import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
42 import org.springframework.boot.autoconfigure.data.cassandra.CassandraDataAutoConfiguration;
43 import org.springframework.boot.builder.SpringApplicationBuilder;
44 import org.springframework.boot.web.servlet.FilterRegistrationBean;
45 //import org.springframework.boot.web.support.SpringBootServletInitializer;
46 import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
47 import org.springframework.context.ApplicationContext;
48 import org.springframework.context.annotation.Bean;
49 import org.springframework.context.annotation.ComponentScan;
50 import org.springframework.context.annotation.DependsOn;
51 import org.springframework.scheduling.annotation.EnableScheduling;
52 import org.springframework.web.context.request.RequestContextListener;
53
54 @SpringBootApplication(scanBasePackages = { "org.onap.music.rest" })
55 @EnableAutoConfiguration(exclude = { CassandraDataAutoConfiguration.class })
56 @ComponentScan(value = { "org.onap.music" })
57 @EnableScheduling
58 public class MusicApplication extends SpringBootServletInitializer {
59
60     @Autowired
61     private PropertiesLoader propertyLoader;
62     private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MusicApplication.class);
63
64     public static void main(String[] args) {
65         System.setProperty("AFT_DME2_CLIENT_IGNORE_SSL_CONFIG", "false");
66         System.setProperty("AFT_DME2_CLIENT_KEYSTORE", "/opt/app/music/etc/truststore2018.jks");
67         System.setProperty("AFT_DME2_CLIENT_KEYSTORE_PASSWORD", "changeit");
68         System.setProperty("AFT_DME2_CLIENT_SSL_INCLUDE_PROTOCOLS", "TLSv1.1,TLSv1.2");
69         new MusicApplication().configure(new SpringApplicationBuilder(MusicApplication.class)).run(args);
70     }
71
72     @Override
73     protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
74
75         return application.sources(MusicApplication.class);
76     }
77
78     @Bean("loadProperties")
79     public void loadProperties() {
80         propertyLoader.loadProperties();
81     }
82
83     @Autowired
84     private ApplicationContext appContext;
85
86     @Bean
87     @DependsOn("loadProperties")
88     public PropAccess propAccess() {
89         if (MusicUtil.getIsCadi()) {
90             return new PropAccess(new String[] { 
91                 "cadi_prop_files=/opt/app/music/etc/cadi.properties" });
92         } else {
93             return null;
94         }
95     }
96
97     @Bean(name = "cadiFilter")
98     @DependsOn("loadProperties")
99     public Filter cadiFilter() throws ServletException {
100         propertyLoader.loadProperties();
101         if (MusicUtil.getIsCadi()) {
102             PropAccess propAccess = propAccess();
103             CadiAuthFilter cadiFilter = new CadiAuthFilter(propAccess);
104
105             return cadiFilter;
106         } else {
107             return (ServletRequest request, ServletResponse response, FilterChain chain) -> {
108                 // do nothing for now.
109             };
110         }
111     }
112     
113     /**
114      * Added for capturing custom header values from client.
115      * 
116      * order is set to 1 for this filter
117      * 
118      * sp931a
119      * 
120      * @return
121      * @throws ServletException
122      */
123     @Bean(name="logFilter")
124     @DependsOn("loadProperties")
125     public FilterRegistrationBean<Filter> loggingFilterRegistration() throws ServletException {
126         logger.info("loggingFilterRegistration called for log filter..");
127         propertyLoader.loadProperties();
128         FilterRegistrationBean<Filter> frb = new FilterRegistrationBean<>();
129         frb.setFilter(new MusicLoggingServletFilter());
130         frb.addUrlPatterns(
131             "/v2/keyspaces/*", 
132             "/v2/locks/*", 
133             "/v2/priorityq/*"
134         );
135         frb.setName("logFilter");
136         frb.setOrder(1);
137         return frb;
138     }
139     
140
141     @Bean
142     @DependsOn("loadProperties")
143     public FilterRegistrationBean<Filter> cadiFilterRegistration() throws ServletException {
144         logger.info("cadiFilterRegistration called for cadi filter..");
145         FilterRegistrationBean<Filter> frb = new FilterRegistrationBean<>();
146         frb.setFilter(cadiFilter());
147
148         // The Following Patterns are used to control what APIs will be secure
149         // TODO: Make this a configurable item. Build this from an array?
150         if (MusicUtil.getIsCadi()) {
151             frb.addUrlPatterns(
152                 "/v2/keyspaces/*", 
153                 "/v2/locks/*", 
154                 "/v2/priorityq/*"
155             );
156         } else {
157             frb.addUrlPatterns("/v0/test");
158         }
159         frb.setName("cadiFilter");
160         frb.setOrder(2);
161         return frb;
162     }
163
164     
165     /**
166      * Added for Authorization using CADI
167      * 
168      * sp931a
169      * 
170      * @return
171      * @throws ServletException
172      */
173     @Bean
174     @DependsOn("loadProperties")
175     public FilterRegistrationBean<Filter> cadiFilterRegistrationForAuth() throws ServletException {
176         logger.info("cadiFilterRegistrationForAuth called for cadi auth filter..");
177         FilterRegistrationBean<Filter> frb = new FilterRegistrationBean<>();
178         frb.setFilter(cadiMusicAuthFilter());
179
180         if (MusicUtil.getIsCadi()) {
181             frb.addUrlPatterns(
182                 "/v2/keyspaces/*", 
183                 "/v2/locks/*", 
184                 "/v2/priorityq/*"
185             );
186         } else {
187             frb.addUrlPatterns("/v0/test");
188         }
189         frb.setName("cadiMusicAuthFilter");
190         frb.setOrder(3);
191         return frb;
192     }
193
194     @Bean(name = "cadiMusicAuthFilter")
195     @DependsOn("loadProperties")
196     public Filter cadiMusicAuthFilter() throws ServletException {
197         propertyLoader.loadProperties();
198         if (MusicUtil.getIsCadi()) {
199             MusicAuthorizationFilter authFilter = new MusicAuthorizationFilter();
200             return authFilter;
201         } else {
202             return (ServletRequest request, ServletResponse response, FilterChain chain) -> {
203                 // do nothing for now.
204             };
205         }
206     }
207
208     @Bean
209     @ConditionalOnMissingBean(RequestContextListener.class)
210     public RequestContextListener requestContextListener() {
211         return new RequestContextListener();
212     }
213 }