aboutsummaryrefslogtreecommitdiffstats
path: root/sidecar/rproxy/src/main/java/org/onap/aaf/rproxy/ReverseProxyApplication.java
blob: ebceceb6bfb78e4ca4b732e55ea578bd38c31897 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/**
 * ============LICENSE_START=======================================================
 * org.onap.aaf
 * ================================================================================
 * Copyright © 2018 European Software Marketing Ltd.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */
package org.onap.aaf.rproxy;

import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.HashMap;
import java.util.Properties;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.net.ssl.SSLContext;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContextBuilder;
import org.eclipse.jetty.util.security.Password;
import org.onap.aaf.cadi.filter.CadiFilter;
import org.onap.aaf.rproxy.config.ForwardProxyProperties;
import org.onap.aaf.rproxy.config.PrimaryServiceProperties;
import org.onap.aaf.rproxy.config.ReverseProxySSLProperties;
import org.onap.aaf.rproxy.mocks.ReverseProxyMockCadiFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.RegistrationBean;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Profile;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.util.ResourceUtils;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@ServletComponentScan
@EnableConfigurationProperties(ReverseProxySSLProperties.class)
@PropertySource("file:${CONFIG_HOME}/reverse-proxy.properties")
public class ReverseProxyApplication extends SpringBootServletInitializer {

    private static final String CADI_TRUSTSTORE_PASS = "cadi_truststore_password";

    @Autowired
    private Environment env;

    /**
     * Spring Boot Initialisation.
     * 
     * @param args main args
     */
    public static void main(String[] args) {
        String keyStorePassword = System.getProperty("KEY_STORE_PASSWORD");
        if (keyStorePassword == null || keyStorePassword.isEmpty()) {
            throw new IllegalArgumentException("Env property KEY_STORE_PASSWORD not set");
        }
        HashMap<String, Object> props = new HashMap<>();
        props.put("server.ssl.key-store-password", Password.deobfuscate(keyStorePassword));
        new ReverseProxyApplication()
                .configure(new SpringApplicationBuilder(ReverseProxyApplication.class).properties(props)).run(args);
    }

    /**
     * Set required trust store system properties using values from application.properties
     */
    @PostConstruct
    public void setSystemProperties() {
        String keyStorePath = env.getProperty("server.ssl.key-store");
        if (keyStorePath != null) {
            String keyStorePassword = env.getProperty("server.ssl.key-store-password");

            if (keyStorePassword != null) {
                System.setProperty("javax.net.ssl.keyStore", keyStorePath);
                System.setProperty("javax.net.ssl.keyStorePassword", keyStorePassword);
                System.setProperty("javax.net.ssl.trustStore", keyStorePath);
                System.setProperty("javax.net.ssl.trustStorePassword", keyStorePassword);
            } else {
                throw new IllegalArgumentException("Env property server.ssl.key-store-password not set");
            }
        }
    }

    @Resource
    private ReverseProxySSLProperties reverseProxySSLProperties;

    @Resource
    Properties cadiProps;

    @Bean(name = "ForwardProxyProperties")
    public ForwardProxyProperties forwardProxyProperties() {
        return new ForwardProxyProperties();
    }

    @Bean(name = "PrimaryServiceProperties")
    public PrimaryServiceProperties primaryServiceProperties() {
        return new PrimaryServiceProperties();
    }

    @Profile("secure")
    @Bean
    public RestTemplate restTemplate(RestTemplateBuilder builder) throws GeneralSecurityException, IOException {
        return new RestTemplate(new HttpComponentsClientHttpRequestFactory(getClientBuilder().build()));
    }

    @Profile("noHostVerification")
    @Bean
    public RestTemplate restTemplateNoHostVerification(RestTemplateBuilder builder)
            throws GeneralSecurityException, IOException {
        return new RestTemplate(new HttpComponentsClientHttpRequestFactory(
                getClientBuilder().setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE).build()));
    }

    private HttpClientBuilder getClientBuilder() throws GeneralSecurityException, IOException {

        SSLContext sslContext = SSLContextBuilder.create()
                .loadKeyMaterial(ResourceUtils.getFile(reverseProxySSLProperties.getClientcert()),
                        reverseProxySSLProperties.getKeystorePassword().toCharArray(),
                        reverseProxySSLProperties.getKeystorePassword().toCharArray())
                .loadTrustMaterial(ResourceUtils.getFile(reverseProxySSLProperties.getKeystore()),
                        reverseProxySSLProperties.getKeystorePassword().toCharArray())
                .build();

        return HttpClients.custom().setSSLContext(sslContext);
    }

    @Profile("cadi")
    @Bean
    public FilterRegistrationBean<CadiFilter> registerCADIFilter() {

        FilterRegistrationBean<CadiFilter> filterRegistrationBean = new FilterRegistrationBean<>();

        filterRegistrationBean.setFilter(new CadiFilter());
        filterRegistrationBean.addUrlPatterns("/*");
        filterRegistrationBean.setName("CADIFilter");
        filterRegistrationBean.setOrder(RegistrationBean.HIGHEST_PRECEDENCE);

        // Deobfuscate truststore password
        String trustStorePassword = cadiProps.getProperty(CADI_TRUSTSTORE_PASS);
        if (trustStorePassword != null) {
            cadiProps.setProperty(CADI_TRUSTSTORE_PASS, Password.deobfuscate(trustStorePassword));
        }

        // Add filter init params
        cadiProps.forEach((k, v) -> filterRegistrationBean.addInitParameter((String) k, (String) v));

        return filterRegistrationBean;
    }

    @Profile("mockCadi")
    @Bean
    public FilterRegistrationBean<ReverseProxyMockCadiFilter> registerMockCADIFilter() {

        FilterRegistrationBean<ReverseProxyMockCadiFilter> filterRegistrationBean = new FilterRegistrationBean<>();

        filterRegistrationBean.setFilter(new ReverseProxyMockCadiFilter());
        filterRegistrationBean.addUrlPatterns("/*");
        filterRegistrationBean.setName("CADIFilter");
        filterRegistrationBean.setOrder(RegistrationBean.HIGHEST_PRECEDENCE);

        return filterRegistrationBean;
    }
}