2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights
7 * ================================================================================
8 * Modifications Copyright (c) 2019 Samsung
9 * ================================================================================
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
14 * http://www.apache.org/licenses/LICENSE-2.0
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
21 * ============LICENSE_END============================================
22 * ===================================================================
26 package org.onap.clamp.clds;
28 import com.att.eelf.configuration.EELFLogger;
29 import com.att.eelf.configuration.EELFManager;
31 import java.io.IOException;
32 import java.io.InputStream;
33 import java.security.KeyStore;
34 import java.security.KeyStoreException;
35 import java.security.NoSuchAlgorithmException;
36 import java.security.cert.CertificateException;
37 import java.security.cert.X509Certificate;
38 import java.util.Enumeration;
40 import org.apache.catalina.connector.Connector;
41 import org.onap.clamp.clds.util.ClampVersioning;
42 import org.onap.clamp.clds.util.ResourceFileUtil;
43 import org.onap.clamp.util.PassDecoder;
44 import org.springframework.beans.factory.annotation.Autowired;
45 import org.springframework.beans.factory.annotation.Value;
46 import org.springframework.boot.SpringApplication;
47 import org.springframework.boot.autoconfigure.SpringBootApplication;
48 import org.springframework.boot.autoconfigure.domain.EntityScan;
49 import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
50 import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
51 import org.springframework.boot.autoconfigure.security.servlet.UserDetailsServiceAutoConfiguration;
52 import org.springframework.boot.builder.SpringApplicationBuilder;
53 import org.springframework.boot.context.properties.EnableConfigurationProperties;
54 import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
55 import org.springframework.boot.web.servlet.ServletRegistrationBean;
56 import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
57 import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
58 import org.springframework.context.annotation.Bean;
59 import org.springframework.context.annotation.ComponentScan;
60 import org.springframework.core.env.Environment;
61 import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
62 import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
63 import org.springframework.scheduling.annotation.EnableAsync;
64 import org.springframework.scheduling.annotation.EnableScheduling;
65 import org.springframework.transaction.annotation.EnableTransactionManagement;
67 @ComponentScan(basePackages = { "org.onap.clamp" })
68 @SpringBootApplication(exclude = { SecurityAutoConfiguration.class, UserDetailsServiceAutoConfiguration.class })
69 @EnableJpaRepositories(basePackages = { "org.onap.clamp" })
70 @EntityScan(basePackages = { "org.onap.clamp" })
71 @EnableTransactionManagement
72 @EnableConfigurationProperties
76 public class Application extends SpringBootServletInitializer {
78 protected static final EELFLogger eelfLogger = EELFManager.getInstance().getLogger(Application.class);
79 // This settings is an additional one to Spring config,
80 // only if we want to have an additional port automatically redirected to
82 @Value("${server.http-to-https-redirection.port:none}")
83 private String httpRedirectedPort;
85 * This 8080 is the default port used by spring if this parameter is not
86 * specified in application.properties.
88 @Value("${server.port:8080}")
89 private String springServerPort;
90 @Value("${server.ssl.key-store:none}")
91 private String sslKeystoreFile;
94 private Environment env;
97 protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
98 return application.sources(Application.class);
101 public static void main(String[] args) {
102 // Start the Spring application
103 SpringApplication.run(Application.class, args);
107 * This method is used to declare the camel servlet.
109 * @return A servlet bean
110 * @throws IOException IO Exception
113 public ServletRegistrationBean camelServletRegistrationBean() throws IOException {
114 eelfLogger.info(ResourceFileUtil.getResourceAsString("boot-message.txt") + "(v"
115 + ClampVersioning.getCldsVersionFromProps() + ")" + System.getProperty("line.separator")
116 + getSslExpirationDate());
117 ServletRegistrationBean registration = new ServletRegistrationBean(new ClampServlet(), "/restservices/clds/*");
118 registration.setName("CamelServlet");
123 * This method is used by Spring to create the servlet container factory.
125 * @return The TomcatEmbeddedServletContainerFactory just created
128 public ServletWebServerFactory getEmbeddedServletContainerFactory() {
129 TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
130 if (!"none".equals(httpRedirectedPort) && !"none".equals(sslKeystoreFile)) {
131 // Automatically redirect to HTTPS
132 tomcat = new TomcatEmbeddedServletContainerFactoryRedirection();
133 Connector newConnector = createRedirectConnector(Integer.parseInt(springServerPort));
134 if (newConnector != null) {
135 tomcat.addAdditionalTomcatConnectors(newConnector);
141 private Connector createRedirectConnector(int redirectSecuredPort) {
142 if (redirectSecuredPort <= 0) {
143 eelfLogger.warn("HTTP port redirection to HTTPS is disabled because the HTTPS port is 0 (random port) or -1"
144 + " (Connector disabled)");
147 Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
148 connector.setScheme("http");
149 connector.setSecure(false);
150 connector.setPort(Integer.parseInt(httpRedirectedPort));
151 connector.setRedirectPort(redirectSecuredPort);
155 private String getSslExpirationDate() throws IOException {
156 StringBuilder result = new StringBuilder(" :: SSL Certificates :: ");
158 if (env.getProperty("server.ssl.key-store") != null) {
160 KeyStore keystore = KeyStore.getInstance(env.getProperty("server.ssl.key-store-type"));
161 String password = PassDecoder.decode(env.getProperty("server.ssl.key-store-password"),
162 env.getProperty("clamp.config.keyFile"));
163 String keyStore = env.getProperty("server.ssl.key-store");
164 InputStream is = ResourceFileUtil.getResourceAsStream(keyStore.replaceAll("classpath:", ""));
165 keystore.load(is, password.toCharArray());
167 Enumeration<String> aliases = keystore.aliases();
168 while (aliases.hasMoreElements()) {
169 String alias = aliases.nextElement();
170 if ("X.509".equals(keystore.getCertificate(alias).getType())) {
171 result.append("* " + alias + " expires "
172 + ((X509Certificate) keystore.getCertificate(alias)).getNotAfter()
173 + System.getProperty("line.separator"));
177 result.append("* NONE HAS been configured");
179 } catch (CertificateException | NoSuchAlgorithmException | KeyStoreException e) {
180 eelfLogger.warn("SSL certificate access error ", e);
183 return result.toString();