-
Notifications
You must be signed in to change notification settings - Fork 325
Passing http request headers in the event metadata for http source #6671
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
.../src/main/java/org/opensearch/dataprepper/plugins/source/loghttp/HttpHeaderExtractor.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| /* | ||
| * Copyright OpenSearch Contributors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * | ||
| * The OpenSearch Contributors require contributions made to | ||
| * this file be licensed under the Apache-2.0 license or a | ||
| * compatible open source license. | ||
| * | ||
| */ | ||
|
|
||
| package org.opensearch.dataprepper.plugins.source.loghttp; | ||
|
|
||
| import com.linecorp.armeria.common.AggregatedHttpRequest; | ||
|
|
||
| import javax.annotation.Nonnull; | ||
| import java.util.Collection; | ||
| import java.util.Collections; | ||
| import java.util.HashMap; | ||
| import java.util.LinkedHashSet; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| public class HttpHeaderExtractor { | ||
|
|
||
| static final Set<String> SENSITIVE_HEADERS = Set.of( | ||
| "authorization", | ||
| "proxy-authorization", | ||
| "cookie", | ||
| "set-cookie", | ||
| "www-authenticate", | ||
| "proxy-authenticate", | ||
| "x-api-key", | ||
| "x-csrf-token", | ||
| "x-xsrf-token", | ||
| "x-auth-token", | ||
| "x-amz-security-token", | ||
| "x-amz-credential" | ||
| ); | ||
|
|
||
| private final Collection<String> metadataHeaders; | ||
|
|
||
| public HttpHeaderExtractor(@Nonnull final Collection<String> metadataHeaders) { | ||
| this.metadataHeaders = metadataHeaders; | ||
| } | ||
|
|
||
| public Map<String, Object> extractHeaders(final AggregatedHttpRequest aggregatedHttpRequest) { | ||
| if (metadataHeaders.isEmpty()) { | ||
| return Collections.emptyMap(); | ||
| } | ||
|
|
||
| final Set<String> headerNames = metadataHeaders.stream() | ||
| .map(String::toLowerCase) | ||
| .collect(Collectors.toCollection(LinkedHashSet::new)); | ||
|
|
||
| final Map<String, Object> headers = new HashMap<>(); | ||
| for (String headerName : headerNames) { | ||
| if (isSensitiveHeader(headerName)) { | ||
| continue; | ||
| } | ||
| List<String> values = aggregatedHttpRequest.headers().getAll(headerName); | ||
| if (!values.isEmpty()) { | ||
| headers.put(headerName, values.size() == 1 ? values.get(0) : Collections.unmodifiableList(values)); | ||
| } | ||
| } | ||
|
|
||
| return headers; | ||
| } | ||
|
|
||
| static boolean isSensitiveHeader(final String headerName) { | ||
| return SENSITIVE_HEADERS.contains(headerName.toLowerCase()); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,13 +28,15 @@ | |
| import java.io.IOException; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Collections; | ||
| import java.util.Map; | ||
| import java.util.UUID; | ||
| import java.util.stream.Collectors; | ||
|
|
||
|
|
||
| /* | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did you mean to remove this?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added it back. |
||
| * A HTTP service for log ingestion to be executed by BlockingTaskExecutor. | ||
| */ | ||
|
|
||
| @Blocking | ||
| public class LogHTTPService { | ||
| private static final int SERIALIZATION_OVERHEAD = 1024; | ||
|
|
@@ -60,16 +62,19 @@ public class LogHTTPService { | |
| private final Timer requestProcessDuration; | ||
| private Integer bufferMaxRequestLength; | ||
| private Integer bufferOptimalRequestLength; | ||
| private final HttpHeaderExtractor httpHeaderExtractor; | ||
|
|
||
| public LogHTTPService(final int bufferWriteTimeoutInMillis, | ||
| final Buffer<Record<Log>> buffer, | ||
| final PluginMetrics pluginMetrics, | ||
| final InputCodec codec) { | ||
| final InputCodec codec, | ||
| final HttpHeaderExtractor httpHeaderExtractor) { | ||
| this.buffer = buffer; | ||
| this.bufferWriteTimeoutInMillis = bufferWriteTimeoutInMillis; | ||
| this.bufferMaxRequestLength = buffer.getMaxRequestSize().isPresent() ? buffer.getMaxRequestSize().get(): null; | ||
| this.bufferOptimalRequestLength = buffer.getOptimalRequestSize().isPresent() ? buffer.getOptimalRequestSize().get(): null; | ||
| this.codec = codec; | ||
| this.httpHeaderExtractor = httpHeaderExtractor; | ||
| requestsReceivedCounter = pluginMetrics.counter(REQUESTS_RECEIVED); | ||
| successRequestsCounter = pluginMetrics.counter(SUCCESS_REQUESTS); | ||
| requestsOverOptimalSizeCounter = pluginMetrics.counter(REQUESTS_OVER_OPTIMAL_SIZE); | ||
|
|
@@ -78,6 +83,13 @@ public LogHTTPService(final int bufferWriteTimeoutInMillis, | |
| requestProcessDuration = pluginMetrics.timer(REQUEST_PROCESS_DURATION); | ||
| } | ||
|
|
||
| public LogHTTPService(final int bufferWriteTimeoutInMillis, | ||
| final Buffer<Record<Log>> buffer, | ||
| final PluginMetrics pluginMetrics, | ||
| final InputCodec codec) { | ||
| this(bufferWriteTimeoutInMillis, buffer, pluginMetrics, codec, new HttpHeaderExtractor(Collections.emptySet())); | ||
| } | ||
|
|
||
| @Post | ||
| public HttpResponse doPost(final ServiceRequestContext serviceRequestContext, final AggregatedHttpRequest aggregatedHttpRequest) throws Exception { | ||
| requestsReceivedCounter.increment(); | ||
|
|
@@ -92,6 +104,7 @@ public HttpResponse doPost(final ServiceRequestContext serviceRequestContext, fi | |
|
|
||
| HttpResponse processRequest(final AggregatedHttpRequest aggregatedHttpRequest) throws Exception { | ||
| final HttpData content = aggregatedHttpRequest.content(); | ||
| final Map<String, Object> extractedHeaders = Collections.unmodifiableMap(httpHeaderExtractor.extractHeaders(aggregatedHttpRequest)); | ||
|
|
||
| if (buffer.isByteBuffer()) { | ||
| if (bufferMaxRequestLength != null && bufferOptimalRequestLength != null && content.array().length > bufferOptimalRequestLength) { | ||
|
|
@@ -140,6 +153,12 @@ HttpResponse processRequest(final AggregatedHttpRequest aggregatedHttpRequest) t | |
| ); | ||
| } | ||
|
|
||
| if (!extractedHeaders.isEmpty()) { | ||
| for (Record<Log> record : records) { | ||
| record.getData().getMetadata().setAttribute("headers", extractedHeaders); | ||
| } | ||
| } | ||
|
|
||
| try { | ||
| buffer.writeAll(records, bufferWriteTimeoutInMillis); | ||
| } catch (Exception e) { | ||
|
|
@@ -171,13 +190,10 @@ private void writeChunkedBody(final String chunk) { | |
| } | ||
| } | ||
|
|
||
| private Record<Log> buildRecordLog(String json) { | ||
|
|
||
| final JacksonLog log = JacksonLog.builder() | ||
| private Record<Log> buildRecordLog(final String json) { | ||
| final JacksonLog.Builder builder = JacksonLog.builder() | ||
| .withData(json) | ||
| .getThis() | ||
| .build(); | ||
|
|
||
| return new Record<>(log); | ||
| .getThis(); | ||
| return new Record<>(builder.build()); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Require this to be non-null.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done