[UI] Handling users and passwords
[validation.git] / ui / src / main / java / org / akraino / validation / ui / conf / UiInitializer.java
1 /*
2  * Copyright (c) 2019 AT&T Intellectual Property. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may
5  * not use this file except in compliance with the License. You may obtain
6  * a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13  * implied. See the License for the specific language governing
14  * permissions and limitations under the License.
15  */
16 package org.akraino.validation.ui.conf;
17
18 import java.io.IOException;
19 import java.security.KeyManagementException;
20 import java.security.NoSuchAlgorithmException;
21 import java.security.cert.X509Certificate;
22 import java.util.List;
23 import java.util.Map;
24
25 import javax.net.ssl.HostnameVerifier;
26 import javax.net.ssl.HttpsURLConnection;
27 import javax.net.ssl.SSLContext;
28 import javax.net.ssl.SSLSession;
29 import javax.net.ssl.TrustManager;
30 import javax.net.ssl.X509TrustManager;
31
32 import org.onap.portalsdk.core.domain.User;
33 import org.onap.portalsdk.core.onboarding.exception.CipherUtilException;
34 import org.onap.portalsdk.core.onboarding.util.CipherUtil;
35 import org.onap.portalsdk.core.service.UserProfileService;
36 import org.springframework.beans.factory.annotation.Autowired;
37 import org.springframework.context.event.ContextRefreshedEvent;
38 import org.springframework.context.event.EventListener;
39 import org.springframework.stereotype.Component;
40
41 import com.sun.jersey.api.client.config.DefaultClientConfig;
42 import com.sun.jersey.client.urlconnection.HTTPSProperties;
43
44 @Component
45 public class UiInitializer {
46
47     @Autowired
48     UserProfileService userService;
49
50     // Create all-trusting host name verifier
51     private final HostnameVerifier hostnameVerifier = new HostnameVerifier() {
52         @Override
53         public boolean verify(String hostname, SSLSession session) {
54             return true;
55         }
56     };
57     // Create a trust manager that does not validate certificate chains
58     private final TrustManager[] trustAll = new TrustManager[] { new X509TrustManager() {
59         @Override
60         public X509Certificate[] getAcceptedIssuers() {
61             return null; // Not relevant.
62         }
63
64         @Override
65         public void checkClientTrusted(X509Certificate[] certs, String authType) {
66             // Do nothing. Just allow them all.
67         }
68
69         @Override
70         public void checkServerTrusted(X509Certificate[] certs, String authType) {
71             // Do nothing. Just allow them all.
72         }
73     } };
74
75     @EventListener(ContextRefreshedEvent.class)
76     public void setHttpProperties() throws NoSuchAlgorithmException, KeyManagementException {
77         SSLContext sslContext = SSLContext.getInstance("SSL");
78         sslContext.init(null, this.trustAll, new java.security.SecureRandom());
79         HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
80         // Install the all-trusting host verifier
81         HttpsURLConnection.setDefaultHostnameVerifier(this.hostnameVerifier);
82         DefaultClientConfig config = new DefaultClientConfig();
83         Map<String, Object> properties = config.getProperties();
84         HTTPSProperties httpsProperties = new HTTPSProperties((str, sslSession) -> true, sslContext);
85         properties.put(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES, httpsProperties);
86     }
87
88     @EventListener(ContextRefreshedEvent.class)
89     public void updateAdminUser() throws RuntimeException, IOException, CipherUtilException {
90         User admin = null;
91         List<User> users = userService.findAllActive();
92         for (User user : users) {
93             if (user.getLoginId().equals("admin")) {
94                 admin = user;
95             }
96         }
97         if (admin == null) {
98             throw new RuntimeException("Admin user does not exist");
99         }
100         if (admin.getLoginPwd().equals("admin_password")) {
101             admin.setLoginPwd(
102                     CipherUtil.encryptPKC(System.getenv("UI_ADMIN_PASSWORD"), System.getenv("ENCRYPTION_KEY")));
103             userService.saveUser(admin);
104         }
105     }
106
107 }