forked from splunk/kafka-connect-splunk
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathIndexer.java
287 lines (254 loc) · 9.83 KB
/
Indexer.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
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*
* Copyright 2017 Splunk, Inc..
*
* 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.
*/
package com.splunk.hecclient;
import com.splunk.kafka.connect.SplunkSinkConnectorConfig;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;
import org.apache.kafka.connect.errors.ConnectException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.security.auth.Subject;
import javax.security.auth.kerberos.KerberosPrincipal;
import javax.security.auth.login.AppConfigurationEntry;
import javax.security.auth.login.Configuration;
import javax.security.auth.login.LoginContext;
import java.io.IOException;
import java.security.Principal;
import java.security.PrivilegedAction;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
final class Indexer implements IndexerInf {
private static final Logger log = LoggerFactory.getLogger(Indexer.class);
private HecConfig hecConfig;
private Configuration config;
private CloseableHttpClient httpClient;
private HttpContext context;
private String baseUrl;
private String hecToken;
private boolean keepAlive;
private HecChannel channel;
private Header[] headers;
private Poller poller;
private long backPressure;
private long lastBackPressure;
private long backPressureThreshhold = 60 * 1000; // 1 min
// Indexer doesn't own client, ack poller
public Indexer(
String baseUrl,
CloseableHttpClient client,
Poller poller,
HecConfig config) {
this.httpClient = client;
this.baseUrl = baseUrl;
this.hecToken = config.getToken();
this.poller = poller;
this.context = HttpClientContext.create();
backPressure = 0;
this.hecConfig = config;
channel = new HecChannel(this);
// Init headers
headers = new Header[3];
headers[0] = new BasicHeader("Authorization", String.format("Splunk %s", hecToken));
headers[1] = new BasicHeader("X-Splunk-Request-Channel", channel.getId());
keepAlive = false;
setKeepAlive(true);
}
public Indexer setBackPressureThreshhold(long threshhold /* milli-seconds */) {
backPressureThreshhold = threshhold;
return this;
}
public Indexer setKeepAlive(boolean keepAlive) {
if (this.keepAlive == keepAlive) {
return this;
}
if (keepAlive) {
headers[2] = new BasicHeader("Connection", "Keep-Alive");
} else {
headers[2] = new BasicHeader("Connection", "close");
}
this.keepAlive = keepAlive;
return this;
}
public boolean getKeepAlive() {
return keepAlive;
}
@Override
public Header[] getHeaders() {
return headers;
}
public String getToken() {
return hecToken;
}
@Override
public String getBaseUrl() {
return baseUrl;
}
public HecChannel getChannel() {
return channel;
}
// this method is multi-thread safe
@Override
public boolean send(final EventBatch batch) {
String endpoint = batch.getRestEndpoint();
String url = baseUrl + endpoint;
final HttpPost httpPost = new HttpPost(url);
httpPost.setHeaders(headers);
httpPost.setEntity(batch.getHttpEntity());
String resp;
try {
resp = executeHttpRequest(httpPost);
} catch (HecException ex) {
poller.fail(channel, batch, ex);
return false;
}
poller.stickySessionHandler(channel);
// we are all good
poller.add(this.channel, batch, resp);
log.debug("sent {} events to splunk through channel={} indexer={}", batch.size(), channel.getId(), getBaseUrl());
return true;
}
// executeHttpRequest is synchronized since there are multi-threads to access the context
@Override
public synchronized String executeHttpRequest(final HttpUriRequest req) {
CloseableHttpResponse resp;
if (hecConfig.kerberosAuthEnabled()) {
if (config == null) {
defineKerberosConfigs();
}
Set<Principal> princ = new HashSet<Principal>(1);
princ.add(new KerberosPrincipal(hecConfig.kerberosUser()));
Subject sub = new Subject(false, princ, new HashSet<Object>(), new HashSet<Object>());
try {
LoginContext lc = new LoginContext("", sub, null, config);
lc.login();
Subject serviceSubject = lc.getSubject();
resp = Subject.doAs(serviceSubject, new PrivilegedAction<CloseableHttpResponse>() {
@Override
public CloseableHttpResponse run() {
try {
return httpClient.execute(req, context);
} catch (IOException ex) {
logBackPressure();
throw new HecException("Encountered exception while posting data.", ex);
}
}
});
} catch (Exception le) {
throw new HecException(
"Encountered exception while authenticating via Kerberos.", le);
}
} else {
try {
resp = httpClient.execute(req, context);
} catch (Exception ex) {
logBackPressure();
throw new HecException("encountered exception when post data", ex);
}
}
return readAndCloseResponse(resp);
}
private void defineKerberosConfigs() {
config = new Configuration() {
@SuppressWarnings("serial")
@Override
public AppConfigurationEntry[] getAppConfigurationEntry(String name) {
return new AppConfigurationEntry[]{new AppConfigurationEntry("com.sun.security.auth.module.Krb5LoginModule",
AppConfigurationEntry.LoginModuleControlFlag.REQUIRED, new HashMap<String, Object>() {
{
put("useTicketCache", "false");
put("useKeyTab", "true");
put("keyTab", hecConfig.kerberosKeytabLocation());
//Krb5 in GSS API needs to be refreshed so it does not throw the error
//Specified version of key is not available
put("refreshKrb5Config", "true");
put("principal", hecConfig.kerberosPrincipal());
put("storeKey", "false");
put("doNotPrompt", "true");
put("isInitiator", "true");
put("debug", "true");
}
})};
}
};
}
private String readAndCloseResponse(CloseableHttpResponse resp) {
String respPayload;
HttpEntity entity = resp.getEntity();
try {
respPayload = EntityUtils.toString(entity, "utf-8");
} catch (Exception ex) {
log.error("failed to process http response", ex);
throw new HecException("failed to process http response", ex);
} finally {
try {
resp.close();
} catch (IOException ex) {
throw new HecException("failed to close http response", ex);
}
}
// log.info("event posting, channel={}, cookies={}, cookies.length={}", channel, resp.getHeaders("Set-Cookie"), resp.getHeaders("Set-Cookie").length);
if((resp.getHeaders("Set-Cookie") != null) && (resp.getHeaders("Set-Cookie").length > 0)) {
log.info("Sticky session expiry detected, will cleanup old channel and its associated batches");
poller.setStickySessionToTrue();
}
int status = resp.getStatusLine().getStatusCode();
// FIXME 503 server is busy backpressure
if (status != 200 && status != 201) {
if (status == 503) {
logBackPressure();
}
log.error("failed to post events resp={}, status={}", respPayload, status);
throw new HecException(String.format("failed to post events resp=%s, status=%d", respPayload, status));
}
clearBackPressure();
return respPayload;
}
private void logBackPressure() {
backPressure += 1;
lastBackPressure = System.currentTimeMillis();
}
private void clearBackPressure() {
backPressure = 0;
lastBackPressure = 0;
}
@Override
public String toString() {
return baseUrl;
}
@Override
public boolean hasBackPressure() {
if (backPressure > 0) {
if (System.currentTimeMillis() - lastBackPressure < backPressureThreshhold) {
// still in the backpressure window
return true;
} else {
clearBackPressure();
return false;
}
}
return false;
}
}