9b831e37da7d5ab53fabf33872105b67600fd347
[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.CipherUtil;
37 import org.onap.music.main.MusicUtil;
38 import org.onap.music.main.PropertiesLoader;
39 import org.springframework.beans.factory.annotation.Autowired;
40 import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
41 import org.springframework.boot.autoconfigure.SpringBootApplication;
42 import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
43 import org.springframework.boot.autoconfigure.data.cassandra.CassandraDataAutoConfiguration;
44 import org.springframework.boot.builder.SpringApplicationBuilder;
45 import org.springframework.boot.web.servlet.FilterRegistrationBean;
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         new MusicApplication().configure(new SpringApplicationBuilder(MusicApplication.class)).run(args);
66     }
67
68     @Override
69     protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
70
71         return application.sources(MusicApplication.class);
72     }
73
74     @Bean("loadProperties")
75     public void loadProperties() {
76         propertyLoader.loadProperties();
77     }
78
79     @Autowired
80     private ApplicationContext appContext;
81
82     @Bean
83     @DependsOn("loadProperties")
84     public PropAccess propAccess() {
85         if (MusicUtil.getIsCadi()) {
86             return new PropAccess(new String[] { 
87                 "cadi_prop_files=/opt/app/music/etc/cadi.properties" });
88         } else {
89             return null;
90         }
91     }
92
93     @Bean(name = "cadiFilter")
94     @DependsOn("loadProperties")
95     public Filter cadiFilter() throws ServletException {
96         propertyLoader.loadProperties();
97         if (MusicUtil.getIsCadi()) {
98             PropAccess propAccess = propAccess();
99             CadiAuthFilter cadiFilter = new CadiAuthFilter(propAccess);
100
101             return cadiFilter;
102         } else {
103             return (ServletRequest request, ServletResponse response, FilterChain chain) -> {
104                 // do nothing for now.
105             };
106         }
107     }
108     
109     /**
110      * Added for capturing custom header values from client.
111      * 
112      * order is set to 1 for this filter
113      * 
114      * sp931a
115      * 
116      * @return
117      * @throws ServletException
118      */
119     @Bean(name="logFilter")
120     @DependsOn("loadProperties")
121     public FilterRegistrationBean<Filter> loggingFilterRegistration() throws ServletException {
122         logger.info("loggingFilterRegistration called for log filter..");
123         propertyLoader.loadProperties();
124         FilterRegistrationBean<Filter> frb = new FilterRegistrationBean<>();
125         frb.setFilter(new MusicLoggingServletFilter());
126         frb.addUrlPatterns(
127             "/v2/keyspaces/*", 
128             "/v2/locks/*", 
129             "/v2/priorityq/*"
130         );
131         frb.setName("logFilter");
132         frb.setOrder(1);
133         return frb;
134     }
135     
136
137     @Bean
138     @DependsOn("loadProperties")
139     public FilterRegistrationBean<Filter> cadiFilterRegistration() throws ServletException {
140         logger.info("cadiFilterRegistration called for cadi filter..");
141         FilterRegistrationBean<Filter> frb = new FilterRegistrationBean<>();
142         frb.setFilter(cadiFilter());
143
144         // The Following Patterns are used to control what APIs will be secure
145         // TODO: Make this a configurable item. Build this from an array?
146         if (MusicUtil.getIsCadi()) {
147             frb.addUrlPatterns(
148                 "/v2/keyspaces/*", 
149                 "/v2/locks/*", 
150                 "/v2/priorityq/*"
151             );
152         } else {
153             frb.addUrlPatterns("/v0/test");
154         }
155         frb.setName("cadiFilter");
156         frb.setOrder(2);
157         return frb;
158     }
159
160     
161     /**
162      * Added for Authorization using CADI
163      * 
164      * sp931a
165      * 
166      * @return
167      * @throws ServletException
168      */
169     @Bean
170     @DependsOn("loadProperties")
171     public FilterRegistrationBean<Filter> cadiFilterRegistrationForAuth() throws ServletException {
172         logger.info("cadiFilterRegistrationForAuth called for cadi auth filter..");
173         FilterRegistrationBean<Filter> frb = new FilterRegistrationBean<>();
174         frb.setFilter(cadiMusicAuthFilter());
175
176         if (MusicUtil.getIsCadi()) {
177             frb.addUrlPatterns(
178                 "/v2/keyspaces/*", 
179                 "/v2/locks/*", 
180                 "/v2/priorityq/*"
181             );
182         } else {
183             frb.addUrlPatterns("/v0/test");
184         }
185         frb.setName("cadiMusicAuthFilter");
186         frb.setOrder(3);
187         return frb;
188     }
189
190     @Bean(name = "cadiMusicAuthFilter")
191     @DependsOn("loadProperties")
192     public Filter cadiMusicAuthFilter() throws ServletException {
193         propertyLoader.loadProperties();
194         if (MusicUtil.getIsCadi()) {
195             MusicAuthorizationFilter authFilter = new MusicAuthorizationFilter();
196             return authFilter;
197         } else {
198             return (ServletRequest request, ServletResponse response, FilterChain chain) -> {
199                 // do nothing for now.
200             };
201         }
202     }
203
204     @Bean
205     @ConditionalOnMissingBean(RequestContextListener.class)
206     public RequestContextListener requestContextListener() {
207         return new RequestContextListener();
208     }
209 }