f058a9e6abe49cf2524f7963a5a4d52d41d6434d
[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 package org.onap.clamp.clds.filter;
24
25 import com.att.eelf.configuration.EELFLogger;
26 import com.att.eelf.configuration.EELFManager;
27
28 import java.io.File;
29 import java.io.IOException;
30 import java.io.InputStream;
31 import java.nio.file.StandardCopyOption;
32
33 import javax.servlet.FilterConfig;
34 import javax.servlet.ServletException;
35
36 import org.onap.aaf.cadi.config.Config;
37 import org.onap.aaf.cadi.filter.CadiFilter;
38 import org.springframework.beans.factory.annotation.Autowired;
39 import org.springframework.beans.factory.annotation.Value;
40 import org.springframework.context.ApplicationContext;
41
42 public class ClampCadiFilter extends CadiFilter {
43     private static final EELFLogger logger = EELFManager.getInstance().getLogger(ClampCadiFilter.class);
44
45     @Autowired
46     private ApplicationContext appContext;
47
48     @Value("${server.ssl.key-store:#{null}}")
49     private String keyStore;
50
51     @Value("${clamp.config.cadi.cadiKeystorePassword:#{null}}")
52     private String keyStorePass;
53
54     @Value("${server.ssl.trust-store:#{null}}")
55     private String trustStore;
56
57     @Value("${clamp.config.cadi.cadiTruststorePassword:#{null}}")
58     private String trustStorePass;
59
60     @Value("${server.ssl.key-alias:clamp@clamp.onap.org}")
61     private String alias;
62
63     @Value("${clamp.config.cadi.keyFile:#{null}}")
64     private String keyFile;
65
66     @Value("${clamp.config.cadi.cadiLoglevel:#{null}}")
67     private String cadiLoglevel;
68
69     @Value("${clamp.config.cadi.cadiLatitude:#{null}}")
70     private String cadiLatitude;
71
72     @Value("${clamp.config.cadi.cadiLongitude:#{null}}")
73     private String cadiLongitude;
74
75     @Value("${clamp.config.cadi.aafLocateUrl:#{null}}")
76     private String aafLocateUrl;
77
78     @Value("${clamp.config.cadi.oauthTokenUrl:#{null}}")
79     private String oauthTokenUrl;
80
81     @Value("${clamp.config.cadi.oauthIntrospectUrl:#{null}}")
82     private String oauthIntrospectUrl;
83
84     @Value("${clamp.config.cadi.aafEnv:#{null}}")
85     private String aafEnv;
86
87     @Value("${clamp.config.cadi.aafUrl:#{null}}")
88     private String aafUrl;
89
90     @Value("${clamp.config.cadi.cadiX509Issuers:#{null}}")
91     private String cadiX509Issuers;
92
93     private void checkIfNullProperty(String key, String value) {
94         /* When value is null, so not defined in application.properties
95            set nothing in System properties */
96         if (value != null) {
97             /* Ensure that any properties already defined in System.prop by JVM params
98                 won't be overwritten by Spring application.properties values */
99             System.setProperty(key, System.getProperty(key, value));
100         }
101     }
102
103     @Override
104     public void init(FilterConfig filterConfig) throws ServletException {
105         // set some properties in System so that Cadi filter will find its config
106         // The JVM values set will always overwrite the Spring ones.
107         checkIfNullProperty(Config.CADI_KEYFILE, convertSpringToPath(keyFile));
108         checkIfNullProperty(Config.CADI_LOGLEVEL, cadiLoglevel);
109         checkIfNullProperty(Config.CADI_LATITUDE, cadiLatitude);
110         checkIfNullProperty(Config.CADI_LONGITUDE, cadiLongitude);
111
112         checkIfNullProperty(Config.AAF_LOCATE_URL, aafLocateUrl);
113         checkIfNullProperty(Config.AAF_OAUTH2_TOKEN_URL, oauthTokenUrl);
114         checkIfNullProperty(Config.AAF_OAUTH2_INTROSPECT_URL, oauthIntrospectUrl);
115
116         checkIfNullProperty(Config.AAF_ENV, aafEnv);
117         checkIfNullProperty(Config.AAF_URL, aafUrl);
118         checkIfNullProperty(Config.CADI_X509_ISSUERS, cadiX509Issuers);
119         checkIfNullProperty(Config.CADI_KEYSTORE, convertSpringToPath(keyStore));
120         checkIfNullProperty(Config.CADI_TRUSTSTORE, convertSpringToPath(trustStore));
121         checkIfNullProperty(Config.CADI_ALIAS, alias);
122         checkIfNullProperty(Config.CADI_KEYSTORE_PASSWORD, keyStorePass);
123         checkIfNullProperty(Config.CADI_TRUSTSTORE_PASSWORD, trustStorePass);
124
125         super.init(filterConfig);
126     }
127
128     private String convertSpringToPath(String fileName) {
129         try (InputStream ioFile = appContext.getResource(fileName).getInputStream()) {
130             if (!fileName.contains("file:")) {
131                 File targetFile = new File(appContext.getResource(fileName).getFilename());
132                 java.nio.file.Files.copy(ioFile, targetFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
133                 return targetFile.getPath();
134             } else {
135                 return appContext.getResource(fileName).getFile().getPath();
136             }
137         } catch (IOException e) {
138             logger.error("Unable to open and copy the file: " + fileName, e);
139             return null;
140         }
141
142     }
143 }