This repository was archived by the owner on Jun 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathElasticLambdaForwarder.java
316 lines (270 loc) · 12.8 KB
/
ElasticLambdaForwarder.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/*
* 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.cloudflare.elastic;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.services.lambda.runtime.LambdaLogger;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.events.S3Event;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.event.S3EventNotification;
import com.amazonaws.services.s3.model.GetObjectRequest;
import com.amazonaws.services.s3.model.S3Object;
import com.amazonaws.services.s3.model.S3ObjectInputStream;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest;
import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse;
import org.elasticsearch.action.bulk.BackoffPolicy;
import org.elasticsearch.action.bulk.BulkProcessor;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.cluster.health.ClusterHealthStatus;
import org.elasticsearch.common.unit.ByteSizeUnit;
import org.elasticsearch.common.unit.ByteSizeValue;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentType;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeUnit;
import java.util.function.BiConsumer;
import java.util.zip.GZIPInputStream;
public class ElasticLambdaForwarder implements RequestHandler<S3Event, Void>
{
private static final String ENV_ELASTIC_HOST = "elastic_hostname";
private static final String ENV_ELASTIC_PORT = "elastic_port";
private static final String ENV_ELASTIC_INDEX = "elastic_index";
private static final String ENV_ELASTIC_USERNAME = "elastic_username";
private static final String ENV_ELASTIC_PASSWORD = "elastic_password";
private static final String ENV_ELASTIC_PIPELINE = "elastic_pipeline";
private static final String ENV_ELASTIC_HTTPS = "elastic_use_https";
private static final String ENV_ELASTIC_BULK_ACTIONS = "elastic_bulk_actions";
private static final String ENV_ELASTIC_CONCURRENCY = "elastic_bulk_concurrency";
private static final String ENV_ELASTIC_DEBUG = "elastic_debug";
private static final String ENV_AWS_ACCESS_KEY = "aws_access_key";
private static final String ENV_AWS_SECRET_KEY = "aws_secret_key";
private LambdaLogger logger = null;
private static int BULK_ACTIONS = 100;
private static int BULK_CONCURRENCY = 2;
private static int ELASTIC_PORT = 9243;
private static String ELASTIC_INDEX = "cloudflare";
private static String ELASTIC_PIPELINE = "cloudflare-pipeline-weekly";
private static String ELASTIC_HOSTNAME = null;
private static String ELASTIC_USERNAME = null;
private static String ELASTIC_PASSWORD = null;
private static boolean ELASTIC_HTTPS = true;
private static boolean ELASTIC_DEBUG = false;
private static boolean USE_AWS_CREDENTIALS = false;
RestHighLevelClient es;
@Override
public Void handleRequest(S3Event event, Context context)
{
logger = context.getLogger();
try {
initialize();
logger.log(String.format(
"Parameters: hostname [%s:%d] index [%s], username [%s], pipeline [%s], ssl/tls [%b]",
ELASTIC_HOSTNAME, ELASTIC_PORT, ELASTIC_INDEX, ELASTIC_USERNAME, ELASTIC_PIPELINE, ELASTIC_HTTPS));
if (es == null) {
es = client(ELASTIC_HOSTNAME, ELASTIC_PORT, ELASTIC_USERNAME, ELASTIC_PASSWORD, ELASTIC_HTTPS);
}
AmazonS3 s3Client = getS3Client();
BulkProcessor processor = processor(es, BULK_ACTIONS, BULK_CONCURRENCY);
try {
process(es, s3Client, event, processor, ELASTIC_INDEX, ELASTIC_PIPELINE);
}
finally {
logger.log("Finished processing; flushing any remaining logs...");
processor.flush();
processor.awaitClose(60L, TimeUnit.SECONDS);
logger.log("Elasticsearch processing done");
}
}
catch (Exception e) {
logger.log(String.format("%s\n%s", e.getMessage(), trace(e)));
try {
if (es != null) {
es.close();
}
} catch (IOException ignored) {
} finally {
es = null;
}
}
return null;
}
private void process(RestHighLevelClient es, AmazonS3 s3Client, S3Event event, BulkProcessor processor, String index, String pipeline)
{
if (ELASTIC_DEBUG) {
logger.log("S3 event record: " + event.toJson());
}
for (S3EventNotification.S3EventNotificationRecord record : event.getRecords())
{
final String key = record.getS3().getObject().getKey();
final String bucket = record.getS3().getBucket().getName();
logger.log("Processing: " + bucket + ":" + key);
S3Object object = s3Client.getObject(new GetObjectRequest(bucket, key));
S3ObjectInputStream s3stream = object.getObjectContent();
try (GZIPInputStream gzip = new GZIPInputStream(s3stream);
BufferedReader br = new BufferedReader(new InputStreamReader(gzip, StandardCharsets.UTF_8)))
{
for (String line; (line = br.readLine()) != null;) {
processor.add(new IndexRequest(index).setPipeline(pipeline).source(line, XContentType.JSON));
}
}
catch (IOException e) {
e.printStackTrace();
}
}
}
private BulkProcessor processor(RestHighLevelClient client, int bulkActions, int bulkConcurrency)
{
BulkProcessor.Listener listener = new BulkProcessor.Listener() {
@Override
public void beforeBulk(long executionId, BulkRequest request) {
logger.log(String.format("Flushing [%s] logs to elasticsearch", request.numberOfActions()));
}
@Override
public void afterBulk(long executionId, BulkRequest request, BulkResponse response) {
if (response.hasFailures()) {
logger.log(response.buildFailureMessage());
}
}
@Override
public void afterBulk(long executionId, BulkRequest request, Throwable failure) {
logger.log(String.format("%s\n%s", failure.getMessage(), trace(failure)));
}
};
BiConsumer<BulkRequest, ActionListener<BulkResponse>> bulkConsumer =
(request, bulkListener) ->
client.bulkAsync(request, RequestOptions.DEFAULT, bulkListener);
BulkProcessor.Builder builder = BulkProcessor.builder(bulkConsumer, listener);
builder.setBulkActions(bulkActions);
builder.setBulkSize(new ByteSizeValue(10L, ByteSizeUnit.MB));
builder.setConcurrentRequests(bulkConcurrency);
builder.setFlushInterval(TimeValue.timeValueSeconds(2L));
builder.setBackoffPolicy(
BackoffPolicy.exponentialBackoff(TimeValue.timeValueSeconds(1), 10));
return builder.build();
}
private RestHighLevelClient client(String endpoint, int port, String username, String password, boolean https) throws IOException
{
CredentialsProvider provider = new BasicCredentialsProvider();
provider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(
new HttpHost(endpoint, port, https ? "https" : "http")).
setHttpClientConfigCallback(
new RestClientBuilder.HttpClientConfigCallback() {
@Override
public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder builder)
{
return builder.setDefaultCredentialsProvider(provider);
}}));
// Verify connection
ClusterHealthResponse health = client.cluster().health(new ClusterHealthRequest(), RequestOptions.DEFAULT);
ClusterHealthStatus status = health.getStatus();
logger.log("Connected to cluster: [" + health.getClusterName() + "] status: [" + status + "]");
if (status == ClusterHealthStatus.RED) {
throw new RuntimeException("Elasticsearch cluster is in RED state; aborting");
}
return client;
}
private AmazonS3 getS3Client()
{
if (USE_AWS_CREDENTIALS) {
return new AmazonS3Client(new AWSCredentials() {
@Override
public String getAWSAccessKeyId() {
return System.getenv(ENV_AWS_ACCESS_KEY);
}
@Override
public String getAWSSecretKey() {
return System.getenv(ENV_AWS_SECRET_KEY);
}
});
}
else {
return AmazonS3ClientBuilder.defaultClient();
}
}
private void initialize()
{
String bulk = System.getenv(ENV_ELASTIC_BULK_ACTIONS);
if (bulk != null) {
BULK_ACTIONS = Integer.parseInt(bulk);
}
String concurrency = System.getenv(ENV_ELASTIC_CONCURRENCY);
if (concurrency != null) {
BULK_CONCURRENCY = Integer.parseInt(concurrency);
}
String port = System.getenv(ENV_ELASTIC_PORT);
if (port != null) {
ELASTIC_PORT = Integer.parseInt(port);
}
String index = System.getenv(ENV_ELASTIC_INDEX);
if (index != null && !index.isEmpty()) {
ELASTIC_INDEX = index;
}
String pipeline = System.getenv(ENV_ELASTIC_PIPELINE);
if (pipeline != null && !pipeline.isEmpty()) {
ELASTIC_PIPELINE = pipeline;
}
String https = System.getenv(ENV_ELASTIC_HTTPS);
if (https != null) {
ELASTIC_HTTPS = Boolean.parseBoolean(https);
}
String debug = System.getenv(ENV_ELASTIC_DEBUG);
if (debug != null) {
ELASTIC_DEBUG = Boolean.parseBoolean(debug);
}
ELASTIC_HOSTNAME = System.getenv(ENV_ELASTIC_HOST);
if (ELASTIC_HOSTNAME == null || ELASTIC_HOSTNAME.isEmpty()) {
throw new RuntimeException("Elastic hostname not set; please set '" + ENV_ELASTIC_HOST + "'");
}
ELASTIC_USERNAME = System.getenv(ENV_ELASTIC_USERNAME);
if (ELASTIC_USERNAME == null || ELASTIC_USERNAME.isEmpty()) {
throw new RuntimeException("Elastic username not set; please set '" + ENV_ELASTIC_USERNAME + "'");
}
ELASTIC_PASSWORD = System.getenv(ENV_ELASTIC_PASSWORD);
if (ELASTIC_PASSWORD == null || ELASTIC_PASSWORD.isEmpty()) {
throw new RuntimeException("Elastic password not set; please set '" + ENV_ELASTIC_PASSWORD + "'");
}
if (System.getenv(ENV_AWS_ACCESS_KEY) != null && System.getenv(ENV_AWS_SECRET_KEY) != null) {
USE_AWS_CREDENTIALS = true;
}
}
private String trace(Throwable t)
{
StringWriter sw = new StringWriter();
t.printStackTrace(new PrintWriter(sw));
return t.toString();
}
}