-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVormetricVTSProxy.java
More file actions
197 lines (153 loc) · 5.58 KB
/
VormetricVTSProxy.java
File metadata and controls
197 lines (153 loc) · 5.58 KB
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
import java.io.*;
import java.net.*;
import org.apache.commons.codec.binary.Base64;
import com.jayway.jsonpath.*;
import javax.net.ssl.HttpsURLConnection;
public class VormetricVTSProxy {
String trustedstoredefaultlocation = "/tmp/mytrustedvtskeystore";
String vtshostip = "192.168.159.141";
String user = "vtsroot";
public static void main(String[] args) {
DataInputStream is;
PrintStream os;
String line = null;
String inputdata = "app:machine:token:value";
VormetricVTSProxy vtsc = new VormetricVTSProxy();
try {
ServerSocket s = new ServerSocket(8181);
Socket in = s.accept();
PrintWriter out = new PrintWriter(in.getOutputStream(), true);
DataInputStream input;
try {
input = new DataInputStream(in.getInputStream());
} catch (IOException e) {
System.out.println(e);
}
is = new DataInputStream(in.getInputStream());
os = new PrintStream(in.getOutputStream());
// As long as we receive data, echo that data back to the client.
String values[] = null;
while (true) {
line = is.readLine();
System.out.println("line is " + line);
System.out.println("line length " + line.length());
values = line.toString().split(":");
if (values.length < 4) {
System.out.println("invalid input for " + line);
System.out.println("proper format is " + inputdata);
break;
}
for (int i = 0; i < values.length; i++) {
System.out.println("first value " + values[i]);
}
String function = values[2];
String datafromclient = values[3];
if (validrequest(values)) {
if (datafromclient.trim().equalsIgnoreCase("stop")) {
os.println("stop");
break;
} else {
String returnval = vtsc.DoIt(datafromclient, function);
os.println(returnval);
}
} else {
System.out.println("invalid request........");
break;
}
}
System.out.println("Hello on a socket from the server " + line);
// out.println("Hello on a socket from the server");
is.close();
os.close();
in.close();
} catch (Exception e) {
}
}
@SuppressWarnings("null")
private String DoIt(String inputfromclient, String function) {
String userpwd = user + ":Vormetric123!";
String credential = Base64.encodeBase64String(userpwd.getBytes());
String token = null;
// Two options to get this example to work:
// Must export a certificate from the VTS server and then import to a
// keystore.
// Put this as VM arguments in the run configuration
// -Djavax.net.debug=ssl
// -Djavax.net.ssl.trustStore="C:\\tmp\\mytrustedvtskeystore"
System.setProperty("javax.net.ssl.trustStore", trustedstoredefaultlocation);
// System.setProperty("javax.net.ssl.trustStorePassword", "changeit");
// System.setProperty("javax.net.debug", "ssl");
try {
// Also needed to add this code as well in order for it to work.
// Note ip must be = to VTS IP.
javax.net.ssl.HttpsURLConnection.setDefaultHostnameVerifier(new javax.net.ssl.HostnameVerifier() {
public boolean verify(String hostname, javax.net.ssl.SSLSession sslSession) {
return hostname.equals(vtshostip);
}
});
// Tokenize request
String https_url = null;
String jStr = null;
HttpsURLConnection con = null;
if (function.equalsIgnoreCase("token")) {
https_url = "https://" + vtshostip + "/vts/rest/v2.0/tokenize/";
URL myurl = new URL(https_url);
con = (HttpsURLConnection) myurl.openConnection();
jStr = "{\"data\":\"" + inputfromclient + "\",\"tokengroup\":\"t1\",\"tokentemplate\":\"Credit Card\"}";
} else {
https_url = "https://" + vtshostip + "/vts/rest/v2.0/detokenize/";
URL myurl = new URL(https_url);
con = (HttpsURLConnection) myurl.openConnection();
jStr = "{\"token\":\"" + inputfromclient
+ "\",\"tokengroup\":\"t1\",\"tokentemplate\":\"Credit Card\"}";
}
con.setRequestProperty("Content-length", String.valueOf(jStr.length()));
con.setRequestProperty("Content-Type", "application/json");
con.setRequestProperty("Authorization", "Basic " + credential);
con.setRequestMethod("POST");
con.setDoOutput(true);
con.setDoInput(true);
DataOutputStream output = new DataOutputStream(con.getOutputStream());
output.writeBytes(jStr);
output.close();
BufferedReader rd = new BufferedReader(new InputStreamReader(con.getInputStream()));
String line = "";
String strResponse = "";
while ((line = rd.readLine()) != null) {
strResponse = strResponse + line;
}
rd.close();
// Also added better error checking...
System.out.println("response is.... " + strResponse);
if (JsonPath.read(strResponse, "$.status").toString().equals("error")) {
System.out.println("Error here is the return: " + strResponse);
} else {
if (function.equalsIgnoreCase("token")) {
token = JsonPath.read(strResponse, "$.token").toString();
} else {
token = JsonPath.read(strResponse, "$.data").toString();
}
con.disconnect();
// System.out.println("Tokenize server: " + https_url);
System.out.println("Tokenize request: " + jStr);
System.out.println("Tokenize response: " + strResponse);
}
// Bulk tokenize
// Detokenize request
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return token;
}
static boolean validrequest(String s[]) {
String applicaiton = s[0];
String machine = s[1];
// String classname = s[3];
System.out.println("in validation for request app ok" + applicaiton + " machine ok" + machine);
// Make calls to verify request is valid. Can check vormetric VTE
// protected file for valid reqeust.
return true;
}
}