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;
30 import java.io.IOException;
31 import java.io.InputStream;
32 import java.security.KeyStore;
33 import java.security.KeyStoreException;
34 import java.security.NoSuchAlgorithmException;
35 import java.security.cert.CertificateException;
36 import java.security.cert.X509Certificate;
37 import java.util.Enumeration;
38 import org.apache.catalina.connector.Connector;
39 import org.onap.clamp.clds.util.ClampVersioning;
40 import org.onap.clamp.clds.util.ResourceFileUtils;
41 import org.onap.clamp.util.PassDecoder;
42 import org.springframework.beans.factory.annotation.Autowired;
43 import org.springframework.beans.factory.annotation.Value;
44 import org.springframework.boot.SpringApplication;
45 import org.springframework.boot.autoconfigure.SpringBootApplication;
46 import org.springframework.boot.autoconfigure.domain.EntityScan;
47 import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
48 import org.springframework.boot.autoconfigure.security.servlet.UserDetailsServiceAutoConfiguration;
49 import org.springframework.boot.builder.SpringApplicationBuilder;
50 import org.springframework.boot.context.properties.EnableConfigurationProperties;
51 import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
52 import org.springframework.boot.web.servlet.ServletRegistrationBean;
53 import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
54 import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
55 import org.springframework.context.annotation.Bean;
56 import org.springframework.context.annotation.ComponentScan;
57 import org.springframework.core.env.Environment;
58 import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
59 import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
60 import org.springframework.scheduling.annotation.EnableAsync;
61 import org.springframework.scheduling.annotation.EnableScheduling;
62 import org.springframework.transaction.annotation.EnableTransactionManagement;
64 @ComponentScan(basePackages = { "org.onap.clamp" })
65 @SpringBootApplication(exclude = { SecurityAutoConfiguration.class, UserDetailsServiceAutoConfiguration.class })
66 @EnableJpaRepositories(basePackages = { "org.onap.clamp" })
67 @EntityScan(basePackages = { "org.onap.clamp" })
68 @EnableTransactionManagement
69 @EnableConfigurationProperties
73 public class Application extends SpringBootServletInitializer {
75 protected static final EELFLogger eelfLogger = EELFManager.getInstance().getLogger(Application.class);
76 // This settings is an additional one to Spring config,
77 // only if we want to have an additional port automatically redirected to
79 @Value("${server.http-to-https-redirection.port:none}")
80 private String httpRedirectedPort;
82 * This 8080 is the default port used by spring if this parameter is not
83 * specified in application.properties.
85 @Value("${server.port:8080}")
86 private String springServerPort;
87 @Value("${server.ssl.key-store:none}")
88 private String sslKeystoreFile;
91 private Environment env;
94 protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
95 return application.sources(Application.class);
98 public static void main(String[] args) {
99 // Start the Spring application
100 SpringApplication.run(Application.class, args);
104 * This method is used to declare the camel servlet.
106 * @return A servlet bean
107 * @throws IOException IO Exception
110 public ServletRegistrationBean camelServletRegistrationBean() throws IOException {
111 eelfLogger.info(ResourceFileUtils.getResourceAsString("boot-message.txt") + "(v"
112 + ClampVersioning.getCldsVersionFromProps() + ")" + System.getProperty("line.separator")
113 + getSslExpirationDate());
114 ServletRegistrationBean registration = new ServletRegistrationBean(new ClampServlet(), "/restservices/clds/*");
115 registration.setName("CamelServlet");
120 * This method is used by Spring to create the servlet container factory.
122 * @return The TomcatEmbeddedServletContainerFactory just created
125 public ServletWebServerFactory getEmbeddedServletContainerFactory() {
126 TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
127 if (!"none".equals(httpRedirectedPort) && !"none".equals(sslKeystoreFile)) {
128 // Automatically redirect to HTTPS
129 tomcat = new TomcatEmbeddedServletContainerFactoryRedirection();
130 Connector newConnector = createRedirectConnector(Integer.parseInt(springServerPort));
131 if (newConnector != null) {
132 tomcat.addAdditionalTomcatConnectors(newConnector);
138 private Connector createRedirectConnector(int redirectSecuredPort) {
139 if (redirectSecuredPort <= 0) {
140 eelfLogger.warn("HTTP port redirection to HTTPS is disabled because the HTTPS port is 0 (random port) or -1"
141 + " (Connector disabled)");
144 Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
145 connector.setScheme("http");
146 connector.setSecure(false);
147 connector.setPort(Integer.parseInt(httpRedirectedPort));
148 connector.setRedirectPort(redirectSecuredPort);
152 private String getSslExpirationDate() throws IOException {
153 StringBuilder result = new StringBuilder(" :: SSL Certificates :: ");
155 if (env.getProperty("server.ssl.key-store") != null) {
157 KeyStore keystore = KeyStore.getInstance(env.getProperty("server.ssl.key-store-type"));
158 String password = PassDecoder.decode(env.getProperty("server.ssl.key-store-password"),
159 env.getProperty("clamp.config.keyFile"));
160 String keyStore = env.getProperty("server.ssl.key-store");
161 InputStream is = ResourceFileUtils.getResourceAsStream(keyStore.replaceAll("classpath:", ""));
162 keystore.load(is, password.toCharArray());
164 Enumeration<String> aliases = keystore.aliases();
165 while (aliases.hasMoreElements()) {
166 String alias = aliases.nextElement();
167 if ("X.509".equals(keystore.getCertificate(alias).getType())) {
168 result.append("* " + alias + " expires "
169 + ((X509Certificate) keystore.getCertificate(alias)).getNotAfter()
170 + System.getProperty("line.separator"));
174 result.append("* NONE HAS been configured");
176 } catch (CertificateException | NoSuchAlgorithmException | KeyStoreException e) {
177 eelfLogger.warn("SSL certificate access error ", e);
180 return result.toString();