aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/org/onap/aai/modelloader/restclient/HttpsBabelServiceClient.java
blob: 078999688c7b60b0e78ec54371c78c735c2e6987 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/**
 * ============LICENSE_START=======================================================
 * org.onap.aai
 * ================================================================================
 * Copyright © 2017-2019 AT&T Intellectual Property. All rights reserved.
 * Copyright © 2017-2019 European Software Marketing Ltd.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.aai.modelloader.restclient;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.google.json.JsonSanitizer;
import com.sun.jersey.api.client.Client; // NOSONAR
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Base64;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
import javax.net.ssl.X509TrustManager;
import javax.ws.rs.core.Response;
import org.json.JSONException;
import org.json.JSONObject;
import org.onap.aai.babel.service.data.BabelArtifact;
import org.onap.aai.cl.api.LogFields;
import org.onap.aai.cl.api.LogLine;
import org.onap.aai.cl.api.Logger;
import org.onap.aai.cl.eelf.LoggerFactory;
import org.onap.aai.cl.mdc.MdcContext;
import org.onap.aai.cl.mdc.MdcOverride;
import org.onap.aai.modelloader.config.ModelLoaderConfig;
import org.onap.aai.modelloader.service.ModelLoaderMsgs;

/**
 * HTTPS Client for interfacing with Babel.
 *
 */
public class HttpsBabelServiceClient implements BabelServiceClient {

    private static final Logger logger = LoggerFactory.getInstance().getLogger(HttpsBabelServiceClient.class);
    private static final Logger metricsLogger =
            LoggerFactory.getInstance().getMetricsLogger(HttpsBabelServiceClient.class);
    private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");

    private static final String SSL_PROTOCOL = "TLS";
    private static final String KEYSTORE_ALGORITHM = "SunX509";
    private static final String KEYSTORE_TYPE = "PKCS12";

    private final ModelLoaderConfig config;
    private final Client client;

    /**
     * @param config
     * @throws NoSuchAlgorithmException
     * @throws KeyStoreException
     * @throws CertificateException
     * @throws IOException
     * @throws UnrecoverableKeyException
     * @throws KeyManagementException
     * @throws BabelServiceClientException
     */
    public HttpsBabelServiceClient(ModelLoaderConfig config)
            throws NoSuchAlgorithmException, KeyStoreException, CertificateException, IOException,
            UnrecoverableKeyException, KeyManagementException, BabelServiceClientException {
        this.config = config;

        logger.debug(ModelLoaderMsgs.DISTRIBUTION_EVENT, "Creating Babel Service client");
        //Initialize SSL Context only if SSL is enabled
        if (config.useHttpsWithBabel()) {
            SSLContext ctx = SSLContext.getInstance(SSL_PROTOCOL);
            KeyManagerFactory kmf = KeyManagerFactory.getInstance(KEYSTORE_ALGORITHM);
            KeyStore keyStore = KeyStore.getInstance(KEYSTORE_TYPE);

            String clientCertPassword = config.getBabelKeyStorePassword();

            char[] pwd = null;
            if (clientCertPassword != null) {
                pwd = clientCertPassword.toCharArray();
            }

            TrustManager[] trustManagers = getTrustManagers();

            String clientCertFileName = config.getBabelKeyStorePath();
            if (clientCertFileName == null) {
                ctx.init(null, trustManagers, null);
            } else {
                InputStream fin = Files.newInputStream(Paths.get(clientCertFileName));
                keyStore.load(fin, pwd);
                kmf.init(keyStore, pwd);
                ctx.init(kmf.getKeyManagers(), trustManagers, null);
            }

            logger.debug(ModelLoaderMsgs.DISTRIBUTION_EVENT, "Initialised context");

            HttpsURLConnection.setDefaultSSLSocketFactory(ctx.getSocketFactory());
            HttpsURLConnection.setDefaultHostnameVerifier((host, session) -> true);
        }

        client = Client.create(new DefaultClientConfig());
        client.setConnectTimeout(config.getClientConnectTimeoutMs());
        client.setReadTimeout(config.getClientReadTimeoutMs());

        logger.debug(ModelLoaderMsgs.DISTRIBUTION_EVENT, "Jersey client created");
    }

    private TrustManager[] getTrustManagers() throws NoSuchAlgorithmException, KeyStoreException, CertificateException,
            IOException, BabelServiceClientException {
        TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
        // Using null here initializes the TMF with the default trust store.
        tmf.init((KeyStore) null);

        // Create a new Trust Manager from the local trust store.
        String trustStoreFile = config.getBabelTrustStorePath();
        if (trustStoreFile == null) {
            throw new BabelServiceClientException("No Babel trust store defined");
        }
        try (InputStream myKeys = Files.newInputStream(Paths.get(trustStoreFile))) {
            KeyStore myTrustStore = KeyStore.getInstance(KeyStore.getDefaultType());
            myTrustStore.load(myKeys, config.getBabelTrustStorePassword().toCharArray());
            tmf.init(myTrustStore);
        }
        X509TrustManager localTm = findX509TrustManager(tmf);

        // Create a custom trust manager that wraps both our trust store and the default.
        final X509TrustManager finalLocalTm = localTm;

        // Find the default trust manager.
        final X509TrustManager defaultTrustManager = findX509TrustManager(tmf);

        return new TrustManager[] {new X509TrustManager() {
            @Override
            public X509Certificate[] getAcceptedIssuers() {
                return defaultTrustManager.getAcceptedIssuers();
            }

            @Override
            public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
                try {
                    finalLocalTm.checkServerTrusted(chain, authType);
                } catch (CertificateException e) { // NOSONAR
                    defaultTrustManager.checkServerTrusted(chain, authType);
                }
            }

            @Override
            public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
                defaultTrustManager.checkClientTrusted(chain, authType);
            }
        }};
    }

    private X509TrustManager findX509TrustManager(TrustManagerFactory tmf) {
        X509TrustManager trustManager = null;
        for (TrustManager tm : tmf.getTrustManagers()) {
            if (tm instanceof X509TrustManager) {
                trustManager = (X509TrustManager) tm;
                break;
            }
        }
        return trustManager;
    }

    /**
     * @param artifactPayload
     * @param artifactName
     * @param artifactVersion
     * @param transactionId
     * @return
     * @throws BabelServiceClientException
     * @throws JSONException
     */
    @Override
    public List<BabelArtifact> postArtifact(byte[] artifactPayload, String artifactName, String artifactVersion,
            String transactionId) throws BabelServiceClientException {
        Objects.requireNonNull(artifactPayload);

        String encodedPayload = Base64.getEncoder().encodeToString(artifactPayload);

        JSONObject obj = new JSONObject();
        try {
            obj.put("csar", encodedPayload);
            obj.put("artifactVersion", artifactVersion);
            obj.put("artifactName", artifactName);
        } catch (JSONException ex) {
            throw new BabelServiceClientException(ex);
        }

        if (logger.isInfoEnabled()) {
            logger.info(ModelLoaderMsgs.BABEL_REST_REQUEST_PAYLOAD, " Artifact Name: " + artifactName
                    + " Artifact version: " + artifactVersion + " Artifact payload: " + encodedPayload);
        }

        MdcOverride override = new MdcOverride();
        override.addAttribute(MdcContext.MDC_START_TIME, ZonedDateTime.now().format(formatter));

        String resourceUrl = config.getBabelBaseUrl() + config.getBabelGenerateArtifactsUrl();
        WebResource webResource = client.resource(resourceUrl);
        ClientResponse response = webResource.type("application/json")
                .header(AaiRestClient.HEADER_TRANS_ID, transactionId)
                .header(AaiRestClient.HEADER_FROM_APP_ID, AaiRestClient.ML_APP_NAME)
                .post(ClientResponse.class, obj.toString());
        String sanitizedJson = JsonSanitizer.sanitize(response.getEntity(String.class));

        if (logger.isDebugEnabled()) {
            logger.debug(ModelLoaderMsgs.DISTRIBUTION_EVENT,
                    "Babel response " + response.getStatus() + " " + sanitizedJson);
        }

        metricsLogger.info(ModelLoaderMsgs.BABEL_REST_REQUEST, new LogFields() //
                .setField(LogLine.DefinedFields.TARGET_ENTITY, "Babel")
                .setField(LogLine.DefinedFields.STATUS_CODE,
                        Response.Status.fromStatusCode(response.getStatus()).getFamily()
                                .equals(Response.Status.Family.SUCCESSFUL) ? "COMPLETE" : "ERROR")
                .setField(LogLine.DefinedFields.RESPONSE_CODE, response.getStatus())
                .setField(LogLine.DefinedFields.RESPONSE_DESCRIPTION, response.getStatusInfo().toString()), //
                override, response.toString());

        if (response.getStatus() != Response.Status.OK.getStatusCode()) {
            throw new BabelServiceClientException(sanitizedJson);
        }

        return new Gson().fromJson(sanitizedJson, new TypeToken<List<BabelArtifact>>() {}.getType());
    }
}