-
Notifications
You must be signed in to change notification settings - Fork 1
/
BTPAzureProxyServletIgnoreSSL.java
132 lines (115 loc) · 5.23 KB
/
BTPAzureProxyServletIgnoreSSL.java
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
package com.sap.cap.productsservice;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManagerFactory;
import javax.servlet.annotation.HttpConstraint;
import javax.servlet.annotation.ServletSecurity;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.sap.cloud.sdk.cloudplatform.connectivity.DestinationAccessor;
import com.sap.cloud.sdk.cloudplatform.connectivity.HttpDestination;
import com.sap.cloud.sdk.cloudplatform.security.BasicCredentials;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.time.Duration;
import java.time.Instant;
import java.util.Base64;
@WebServlet("/ignoressl/*")
/* uncomment once you start using the approuter */
//@ServletSecurity(@HttpConstraint(rolesAllowed = { "Display" }))
public class BTPAzureProxyServletIgnoreSSL extends HttpServlet
{
private static final long serialVersionUID = 1L;
private static final Logger logger = LoggerFactory.getLogger(BTPAzureProxyServletIgnoreSSL.class);
private static final String DESTINATION_NAME = "s4BasicAuth";
@Override
protected void doGet( final HttpServletRequest request, final HttpServletResponse response )
throws IOException
{
HttpDestination httpDestination = DestinationAccessor.getDestination(DESTINATION_NAME).asHttp();
KeyStore keyStore = httpDestination.getTrustStore().getOrNull();
SSLContext sslContext = null;
TrustManagerFactory trustManagerFactory;
try {
trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init(keyStore);
sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, trustManagerFactory.getTrustManagers(), null);
//Certificate certificate = keyStore.getCertificate("fake.cer");
} catch (NoSuchAlgorithmException | KeyStoreException | KeyManagementException e) {
e.printStackTrace();
}
// Create trusting host name verifier for private link IP
String privateIP = httpDestination.getUri().getHost();
HostnameVerifier allHostsValid = new HostnameVerifier() {
public boolean verify(String hostname, SSLSession session) {
boolean result = false;
if( hostname.equals(privateIP)){
result = true;
}
return result;
}
};
// Install the all-trusting host verifier
HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
String uriBase = httpDestination.getUri().toString();
String uriReplaced = "/" + request.getRequestURI().trim().split("/ignoressl/")[1];
String queryString = request.getQueryString();
logger.info("Destination target: " + uriBase + uriReplaced + "?" + queryString);
URL url = null;
if(queryString != null){
url = new URL(uriBase + uriReplaced + "?" + queryString);
}else{
url = new URL(uriBase + uriReplaced);
}
BasicCredentials props = httpDestination.getBasicCredentials().get();
String basic = "Basic " + Base64.getEncoder().encodeToString((props.getUsername() + ":" + props.getPassword()).getBytes());
//measure execution time
Instant start = Instant.now();
HttpsURLConnection con = (HttpsURLConnection)url.openConnection();
con.setSSLSocketFactory(sslContext.getSocketFactory());
con.setRequestMethod("GET");
con.setRequestProperty("Authorization", basic);
Instant end = Instant.now();
long timeElapsed = Duration.between(start, end).toMillis();
logger.info("Message round trip time: " + timeElapsed + "ms");
int status = con.getResponseCode();
BufferedReader in = null;
if (status > 299) {
in = new BufferedReader(new InputStreamReader(con.getErrorStream()));
} else {
in = new BufferedReader(new InputStreamReader(con.getInputStream()));
}
String inputLine;
StringBuffer content = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
content.append(inputLine);
}
in.close();
con.disconnect();
//post back response
String result = content.toString();
response.setCharacterEncoding("UTF-8");
if (result != "") {
response.setContentType("application/xml");
response.setStatus(HttpServletResponse.SC_OK);
response.getWriter().write(result);
} else {
response.setStatus(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
response.setContentType("text/plain");
response.getWriter().write("No data available.");
}
}
}