TLS support in sdc-fe
[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 lombok.Getter;
25 import lombok.Setter;
26
27 import org.apache.commons.lang3.StringUtils;
28 import org.onap.sdc.security.SecurityUtil;
29
30 @EqualsAndHashCode
31 @Getter
32 @Setter
33 public class ClientCertificate {
34
35     private String keyStore;
36     private String keyStorePassword;
37     private String trustStore;
38     private String trustStorePassword;
39     
40     public ClientCertificate() {
41     }
42
43     public ClientCertificate(ClientCertificate clientCertificate) {
44         setKeyStore(clientCertificate.getKeyStore());
45         setKeyStorePassword(clientCertificate.getKeyStorePassword(), false);
46         setTrustStore(clientCertificate.getTrustStore());
47         setTrustStorePassword(clientCertificate.getTrustStorePassword());
48     }
49
50     public void setKeyStorePassword(String keyStorePassword, boolean isEncoded) {
51         validate(keyStorePassword);
52         if (isEncoded) {
53             Either<String, String> passkey = SecurityUtil.decrypt(keyStorePassword);
54             if (passkey.isLeft()) {
55                 this.keyStorePassword = passkey.left().value();
56             } else {
57                 throw new IllegalArgumentException(passkey.right().value());
58             }
59         } else {
60             this.keyStorePassword = keyStorePassword;
61         }
62     }
63
64     public void setKeyStore(String keyStore) {
65         validate(keyStore);
66         this.keyStore = keyStore;
67     }
68
69     public void setKeyStorePassword(String keyStorePassword) {
70         setKeyStorePassword(keyStorePassword, true);
71     }
72
73     private void validate(String str) {
74         if (StringUtils.isEmpty(str)) {
75             throw new IllegalArgumentException("ClientCertificate keystore and/or kestorePassword cannot be empty");
76         }
77     }
78 }