d2e109eef2ade9c4158bcfc4e48316063a933275
[dcaegen2/services/sdk.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * DCAEGEN2-SERVICES-SDK
4  * ================================================================================
5  * Copyright (C) 2018-2019 NOKIA 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
21 package org.onap.dcaegen2.services.sdk.rest.services.aai.client.service;
22
23 import io.netty.handler.ssl.SslContext;
24 import io.vavr.control.Try;
25 import org.onap.dcaegen2.services.sdk.rest.services.aai.client.config.AaiClientConfiguration;
26 import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.CloudHttpClient;
27 import org.onap.dcaegen2.services.sdk.rest.services.model.logging.ImmutableRequestDiagnosticContext;
28 import org.onap.dcaegen2.services.sdk.rest.services.model.logging.RequestDiagnosticContext;
29 import org.onap.dcaegen2.services.sdk.security.ssl.ImmutableSecurityKeys;
30 import org.onap.dcaegen2.services.sdk.security.ssl.ImmutableSecurityKeysStore;
31 import org.onap.dcaegen2.services.sdk.security.ssl.Passwords;
32 import org.onap.dcaegen2.services.sdk.security.ssl.SecurityKeys;
33 import org.onap.dcaegen2.services.sdk.security.ssl.SslFactory;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 import java.nio.file.Path;
38 import java.nio.file.Paths;
39 import java.util.Base64;
40 import java.util.UUID;
41
42 public class AaiHttpClientFactory {
43
44     private static final Logger LOGGER = LoggerFactory.getLogger(AaiHttpClientFactory.class);
45
46     private final AaiClientConfiguration configuration;
47     private final SslFactory sslFactory = new SslFactory();
48
49
50     public AaiHttpClientFactory(AaiClientConfiguration configuration) {
51         this.configuration = configuration;
52     }
53
54     public CloudHttpClient build() {
55         LOGGER.debug("Setting ssl context");
56         return new CloudHttpClient(createSslContext());
57     }
58
59     private SslContext createSslContext() {
60         if (configuration.enableAaiCertAuth()) {
61             final SecurityKeys collectorSecurityKeys = ImmutableSecurityKeys.builder()
62                     .keyStore(ImmutableSecurityKeysStore.of(resource(configuration.keyStorePath()).get()))
63                     .keyStorePassword(Passwords.fromResource(configuration.keyStorePasswordPath()))
64                     .trustStore(ImmutableSecurityKeysStore.of(resource(configuration.trustStorePath()).get()))
65                     .trustStorePassword(Passwords.fromResource(configuration.trustStorePasswordPath()))
66                     .build();
67             return sslFactory.createSecureClientContext(collectorSecurityKeys);
68         }
69         return sslFactory.createInsecureClientContext();
70     }
71
72     private Try<Path> resource(String resource) {
73         return Try.of(() -> Paths.get(Passwords.class.getResource(resource).toURI()));
74     }
75
76     public static RequestDiagnosticContext createRequestDiagnosticContext() {
77         return ImmutableRequestDiagnosticContext.builder()
78                 .invocationId(UUID.randomUUID()).requestId(UUID.randomUUID()).build();
79     }
80
81 }