93fc3b9f0fd59fc627d062f81d26569c6a8d363f
[sdc.git] / common-app-api / src / main / java / org / openecomp / sdc / common / http / config / ClientCertificate.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20 package org.openecomp.sdc.common.http.config;
21
22 import fj.data.Either;
23 import lombok.EqualsAndHashCode;
24 import org.apache.commons.lang3.StringUtils;
25 import org.onap.sdc.security.SecurityUtil;
26
27 @EqualsAndHashCode
28 public class ClientCertificate {
29
30     private String keyStore;
31     private String keyStorePassword;
32
33     public ClientCertificate() {
34     }
35
36     public ClientCertificate(ClientCertificate clientCertificate) {
37         setKeyStore(clientCertificate.getKeyStore());
38         setKeyStorePassword(clientCertificate.getKeyStorePassword(), false);
39     }
40
41     private void setKeyStorePassword(String keyStorePassword, boolean isEncoded) {
42         validate(keyStorePassword);
43         if (isEncoded) {
44             Either<String, String> passkey = SecurityUtil.decrypt(keyStorePassword);
45             if (passkey.isLeft()) {
46                 this.keyStorePassword = passkey.left().value();
47             } else {
48                 throw new IllegalArgumentException(passkey.right().value());
49             }
50         } else {
51             this.keyStorePassword = keyStorePassword;
52         }
53     }
54
55     public String getKeyStore() {
56         return keyStore;
57     }
58
59     public void setKeyStore(String keyStore) {
60         validate(keyStore);
61         this.keyStore = keyStore;
62     }
63
64     public String getKeyStorePassword() {
65         return keyStorePassword;
66     }
67
68     public void setKeyStorePassword(String keyStorePassword) {
69         setKeyStorePassword(keyStorePassword, true);
70     }
71
72     @Override
73     public String toString() {
74         StringBuilder builder = new StringBuilder();
75         builder.append("ClientCertificate [keyStore=");
76         builder.append(keyStore);
77         builder.append("]");
78         return builder.toString();
79     }
80
81     private void validate(String str) {
82         if (StringUtils.isEmpty(str)) {
83             throw new IllegalArgumentException("ClientCertificate keystore and/or kestorePassword cannot be empty");
84         }
85     }
86 }