Merge "Replacing the VFC with VFModule"
[clamp.git] / src / main / java / org / onap / clamp / clds / filter / ClampCadiFilter.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights
6  *                             reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END============================================
20  * ===================================================================
21  *
22  */
23
24 package org.onap.clamp.clds.filter;
25
26 import com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
28
29 import java.io.File;
30 import java.io.IOException;
31 import java.io.InputStream;
32 import java.nio.file.StandardCopyOption;
33
34 import javax.servlet.FilterConfig;
35 import javax.servlet.ServletException;
36
37 import org.onap.aaf.cadi.config.Config;
38 import org.onap.aaf.cadi.filter.CadiFilter;
39 import org.springframework.beans.factory.annotation.Autowired;
40 import org.springframework.beans.factory.annotation.Value;
41 import org.springframework.context.ApplicationContext;
42
43 public class ClampCadiFilter extends CadiFilter {
44     private static final EELFLogger logger = EELFManager.getInstance().getLogger(ClampCadiFilter.class);
45
46     @Autowired
47     private ApplicationContext appContext;
48
49     @Value("${server.ssl.key-store:#{null}}")
50     private String keyStore;
51
52     @Value("${clamp.config.cadi.cadiKeystorePassword:#{null}}")
53     private String keyStorePass;
54
55     @Value("${server.ssl.trust-store:#{null}}")
56     private String trustStore;
57
58     @Value("${clamp.config.cadi.cadiTruststorePassword:#{null}}")
59     private String trustStorePass;
60
61     @Value("${server.ssl.key-alias:clamp@clamp.onap.org}")
62     private String alias;
63
64     @Value("${clamp.config.cadi.keyFile:#{null}}")
65     private String keyFile;
66
67     @Value("${clamp.config.cadi.cadiLoglevel:#{null}}")
68     private String cadiLoglevel;
69
70     @Value("${clamp.config.cadi.cadiLatitude:#{null}}")
71     private String cadiLatitude;
72
73     @Value("${clamp.config.cadi.cadiLongitude:#{null}}")
74     private String cadiLongitude;
75
76     @Value("${clamp.config.cadi.aafLocateUrl:#{null}}")
77     private String aafLocateUrl;
78
79     @Value("${clamp.config.cadi.oauthTokenUrl:#{null}}")
80     private String oauthTokenUrl;
81
82     @Value("${clamp.config.cadi.oauthIntrospectUrl:#{null}}")
83     private String oauthIntrospectUrl;
84
85     @Value("${clamp.config.cadi.aafEnv:#{null}}")
86     private String aafEnv;
87
88     @Value("${clamp.config.cadi.aafUrl:#{null}}")
89     private String aafUrl;
90
91     @Value("${clamp.config.cadi.cadiX509Issuers:#{null}}")
92     private String cadiX509Issuers;
93
94     private void checkIfNullProperty(String key, String value) {
95         /* When value is null, so not defined in application.properties
96            set nothing in System properties */
97         if (value != null) {
98             /* Ensure that any properties already defined in System.prop by JVM params
99                 won't be overwritten by Spring application.properties values */
100             System.setProperty(key, System.getProperty(key, value));
101         }
102     }
103
104     @Override
105     public void init(FilterConfig filterConfig) throws ServletException {
106         // set some properties in System so that Cadi filter will find its config
107         // The JVM values set will always overwrite the Spring ones.
108         checkIfNullProperty(Config.CADI_KEYFILE, convertSpringToPath(keyFile));
109         checkIfNullProperty(Config.CADI_LOGLEVEL, cadiLoglevel);
110         checkIfNullProperty(Config.CADI_LATITUDE, cadiLatitude);
111         checkIfNullProperty(Config.CADI_LONGITUDE, cadiLongitude);
112
113         checkIfNullProperty(Config.AAF_LOCATE_URL, aafLocateUrl);
114         checkIfNullProperty(Config.AAF_OAUTH2_TOKEN_URL, oauthTokenUrl);
115         checkIfNullProperty(Config.AAF_OAUTH2_INTROSPECT_URL, oauthIntrospectUrl);
116
117         checkIfNullProperty(Config.AAF_ENV, aafEnv);
118         checkIfNullProperty(Config.AAF_URL, aafUrl);
119         checkIfNullProperty(Config.CADI_X509_ISSUERS, cadiX509Issuers);
120         checkIfNullProperty(Config.CADI_KEYSTORE, convertSpringToPath(keyStore));
121         checkIfNullProperty(Config.CADI_TRUSTSTORE, convertSpringToPath(trustStore));
122         checkIfNullProperty(Config.CADI_ALIAS, alias);
123         checkIfNullProperty(Config.CADI_KEYSTORE_PASSWORD, keyStorePass);
124         checkIfNullProperty(Config.CADI_TRUSTSTORE_PASSWORD, trustStorePass);
125
126         super.init(filterConfig);
127     }
128
129     private String convertSpringToPath(String fileName) {
130         try (InputStream ioFile = appContext.getResource(fileName).getInputStream()) {
131             if (!fileName.contains("file:")) {
132                 File targetFile = new File(appContext.getResource(fileName).getFilename());
133                 java.nio.file.Files.copy(ioFile, targetFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
134                 return targetFile.getPath();
135             } else {
136                 return appContext.getResource(fileName).getFile().getPath();
137             }
138         } catch (IOException e) {
139             logger.error("Unable to open and copy the file: " + fileName, e);
140             return null;
141         }
142
143     }
144 }