19 package com.basistech.df.cybertriage.autopsy.ctapi;
22 import com.fasterxml.jackson.databind.ObjectMapper;
23 import java.io.IOException;
24 import java.net.Authenticator;
25 import java.net.InetAddress;
26 import java.net.PasswordAuthentication;
28 import java.net.URISyntaxException;
29 import java.net.UnknownHostException;
30 import java.security.KeyManagementException;
31 import java.security.KeyStoreException;
32 import java.security.NoSuchAlgorithmException;
33 import java.security.SecureRandom;
34 import java.security.UnrecoverableKeyException;
35 import java.text.MessageFormat;
36 import java.util.ArrayList;
37 import java.util.Arrays;
38 import java.util.Collections;
39 import java.util.List;
41 import java.util.Map.Entry;
42 import java.util.Objects;
43 import java.util.logging.Level;
44 import javax.net.ssl.KeyManager;
45 import javax.net.ssl.KeyManagerFactory;
46 import javax.net.ssl.SSLContext;
47 import javax.net.ssl.TrustManager;
48 import javax.net.ssl.TrustManagerFactory;
49 import javax.net.ssl.X509TrustManager;
50 import org.apache.commons.collections.CollectionUtils;
51 import org.apache.commons.collections4.MapUtils;
52 import org.apache.commons.lang3.StringUtils;
53 import org.apache.http.HttpEntity;
54 import org.apache.http.HttpHost;
55 import org.apache.http.HttpStatus;
56 import org.apache.http.auth.AuthScope;
57 import org.apache.http.auth.NTCredentials;
58 import org.apache.http.client.CredentialsProvider;
59 import org.apache.http.client.config.AuthSchemes;
60 import org.apache.http.client.config.RequestConfig;
61 import org.apache.http.client.methods.CloseableHttpResponse;
62 import org.apache.http.client.methods.HttpPost;
63 import org.apache.http.client.methods.HttpRequestBase;
64 import org.apache.http.impl.client.CloseableHttpClient;
65 import org.apache.http.util.EntityUtils;
66 import org.apache.http.client.utils.URIBuilder;
67 import org.apache.http.entity.StringEntity;
69 import org.apache.http.impl.client.HttpClientBuilder;
70 import org.apache.http.impl.client.HttpClients;
71 import org.apache.http.impl.client.SystemDefaultCredentialsProvider;
72 import org.apache.http.impl.client.WinHttpClients;
85 =
new ArrayList<>(Arrays.asList(
105 this.sslContext = null;
109 if (StringUtils.isBlank(hostName)) {
111 hostName = InetAddress.getLocalHost().getCanonicalHostName();
112 }
catch (UnknownHostException ex) {
113 LOGGER.log(Level.WARNING,
"An error occurred while fetching the hostname", ex);
121 }
catch (NumberFormatException ex) {
122 LOGGER.log(Level.WARNING,
"Unable to convert port to integer");
137 public <O> O doPost(String urlPath, Object jsonBody, Class<O> classType)
throws CTCloudException {
138 return doPost(urlPath, Collections.emptyMap(), jsonBody, classType);
141 public <O> O doPost(String urlPath, Map<String, String> urlReqParams, Object jsonBody, Class<O> classType)
throws CTCloudException {
142 String url = HOST_URL + urlPath;
145 LOGGER.log(Level.INFO,
"initiating http connection to ctcloud server");
147 URIBuilder builder =
new URIBuilder(url);
149 if (!MapUtils.isEmpty(urlReqParams)) {
150 for (Entry<String, String> e : urlReqParams.entrySet()) {
151 String key = e.getKey();
152 String value = e.getValue();
153 if (StringUtils.isNotBlank(key) || StringUtils.isNotBlank(value)) {
154 builder.addParameter(key, value);
159 URI postURI = builder.build();
160 HttpPost postRequest =
new HttpPost(postURI);
164 postRequest.setHeader(
"Content-type",
"application/json");
166 if (jsonBody != null) {
167 String requestBody = mapper.writeValueAsString(jsonBody);
168 if (StringUtils.isNotBlank(requestBody)) {
169 HttpEntity entity =
new StringEntity(requestBody,
"UTF-8");
170 postRequest.setEntity(entity);
174 LOGGER.log(Level.INFO,
"initiating http post request to ctcloud server " + postRequest.getURI());
175 try (CloseableHttpResponse response = httpclient.execute(postRequest)) {
177 if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
178 LOGGER.log(Level.INFO,
"Response Received. - Status OK");
180 HttpEntity entity = response.getEntity();
181 String entityStr = EntityUtils.toString(entity);
182 O respObj = mapper.readValue(entityStr, classType);
185 LOGGER.log(Level.WARNING,
"Response Received. - Status Error {}", response.getStatusLine());
188 }
catch (Exception ex) {
189 LOGGER.log(Level.WARNING,
"Error when parsing response from CyberTriage Cloud", ex);
190 throw new CTCloudException(CTCloudException.parseUnknownException(ex), ex);
193 }
catch (IOException ex) {
194 LOGGER.log(Level.WARNING,
"IO Exception raised when connecting to CT Cloud using " + url, ex);
195 throw new CTCloudException(CTCloudException.ErrorCode.NETWORK_ERROR, ex);
196 }
catch (URISyntaxException ex) {
197 LOGGER.log(Level.WARNING,
"Wrong URL syntax for CT Cloud " + url, ex);
198 throw new CTCloudException(CTCloudException.ErrorCode.UNKNOWN, ex);
214 LOGGER.log(Level.WARNING, MessageFormat.format(
215 "Response code {0}. Message Body {1}",
216 response.getStatusLine().getStatusCode(),
217 EntityUtils.toString(response.getEntity())));
219 switch (response.getStatusLine().getStatusCode()) {
221 case HttpStatus.SC_BAD_REQUEST:
223 throw new CTCloudException(CTCloudException.ErrorCode.BAD_REQUEST);
224 case HttpStatus.SC_UNAUTHORIZED:
226 throw new CTCloudException(CTCloudException.ErrorCode.INVALID_KEY);
227 case HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED:
229 throw new CTCloudException(CTCloudException.ErrorCode.PROXY_UNAUTHORIZED);
230 case HttpStatus.SC_FORBIDDEN:
231 throw new CTCloudException(CTCloudException.ErrorCode.UN_AUTHORIZED);
232 case HttpStatus.SC_INTERNAL_SERVER_ERROR:
234 throw new CTCloudException(CTCloudException.ErrorCode.TEMP_UNAVAILABLE);
235 case HttpStatus.SC_SERVICE_UNAVAILABLE:
238 throw new CTCloudException(CTCloudException.ErrorCode.TEMP_UNAVAILABLE);
239 case HttpStatus.SC_GATEWAY_TIMEOUT:
240 throw new CTCloudException(CTCloudException.ErrorCode.GATEWAY_TIMEOUT);
242 String returnData = EntityUtils.toString(response.getEntity());
243 LOGGER.log(Level.WARNING, MessageFormat.format(
"upload response content for {0}:\n {1}", fileName, returnData));
244 throw new CTCloudException(CTCloudException.ErrorCode.NETWORK_ERROR);
257 RequestConfig config = RequestConfig.custom()
258 .setConnectionRequestTimeout(CONNECTION_TIMEOUT_MS)
259 .setConnectTimeout(CONNECTION_TIMEOUT_MS)
260 .setSocketTimeout(CONNECTION_TIMEOUT_MS)
262 request.setConfig(config);
274 if (sslContext != null) {
275 builder.setSSLContext(sslContext);
277 return builder.build();
282 if (proxySettings.isSystemOrManualProxy()) {
284 Authenticator.setDefault(
new Authenticator() {
286 protected PasswordAuthentication getPasswordAuthentication() {
287 LOGGER.info(
"Requesting Password Authentication...");
288 return super.getPasswordAuthentication();
292 HttpClientBuilder builder = null;
293 HttpHost proxyHost = null;
294 CredentialsProvider proxyCredsProvider = null;
295 RequestConfig config = null;
297 if (Objects.nonNull(proxySettings.getProxyHostname()) && proxySettings.getProxyPort() > 0) {
298 proxyHost =
new HttpHost(proxySettings.getProxyHostname(), proxySettings.getProxyPort());
310 if (Objects.isNull(proxyCredsProvider) && WinHttpClients.isWinAuthAvailable()) {
311 builder = WinHttpClients.custom();
312 builder.useSystemProperties();
313 LOGGER.log(Level.WARNING,
"Using Win HTTP Client");
315 builder = HttpClients.custom();
316 builder.setDefaultRequestConfig(config);
317 if (Objects.nonNull(proxyCredsProvider)) {
318 builder.setDefaultCredentialsProvider(proxyCredsProvider);
320 LOGGER.log(Level.WARNING,
"Using default http client");
322 if (Objects.nonNull(proxyHost)) {
323 builder.setProxy(proxyHost);
324 LOGGER.log(Level.WARNING, MessageFormat.format(
"Using proxy {0}", proxyHost));
329 return HttpClients.custom();
340 CredentialsProvider proxyCredsProvider = null;
341 if (proxySettings.isSystemOrManualProxy()) {
342 if (StringUtils.isNotBlank(proxySettings.getProxyUserId())) {
343 if (null != proxySettings.getProxyPassword() && proxySettings.getProxyPassword().length > 0) {
344 proxyCredsProvider =
new SystemDefaultCredentialsProvider();
345 String userId = proxySettings.getProxyUserId();
346 String domain = null;
347 if (userId.contains(
"\\")) {
348 domain = userId.split(
"\\\\")[0];
349 userId = userId.split(
"\\\\")[1];
351 String workStation = proxySettings.getHostName();
352 proxyCredsProvider.setCredentials(
new AuthScope(proxySettings.getProxyHostname(), proxySettings.getProxyPort()),
353 new NTCredentials(userId,
new String(proxySettings.getProxyPassword()), workStation, domain));
358 return proxyCredsProvider;
371 ProxySettingArgs(
boolean systemOrManualProxy, String hostName, String proxyHostname,
int proxyPort, String proxyUserId,
char[] proxyPassword, String authScheme) {
381 boolean isSystemOrManualProxy() {
385 String getHostName() {
389 String getProxyHostname() {
397 String getProxyUserId() {
401 char[] getProxyPassword() {
static Version.Type getBuildType()
static final String CT_CLOUD_SERVER
static CredentialsProvider getProxyCredentialsProvider(ProxySettingArgs proxySettings)
static char[] getAuthenticationPassword()
static final CTCloudHttpClient instance
static String getHttpPort()
static final int DIRECT_CONNECTION
final SSLContext sslContext
static HttpClientBuilder getHttpClientBuilder(ProxySettingArgs proxySettings)
static final String HOST_URL
static int getProxyType()
static ObjectMapperUtil getInstance()
static final Logger LOGGER
static final int CONNECTION_TIMEOUT_MS
final boolean systemOrManualProxy
static String getAuthenticationUsername()
static final List< String > DEFAULT_SCHEME_PRIORITY
void configureRequestTimeout(HttpRequestBase request)
ProxySettingArgs getProxySettings()
static CTCloudHttpClient getInstance()
static CloseableHttpClient createConnection(ProxySettingArgs proxySettings, SSLContext sslContext)
ObjectMapper getDefaultObjectMapper()
static final String CT_CLOUD_DEV_SERVER
final char[] proxyPassword
final ObjectMapper mapper
void handleNonOKResponse(CloseableHttpResponse response, String fileName)
final String proxyHostname
synchronized static Logger getLogger(String name)
static String getHttpsHost()
static String getHttpsPort()