forked from opensearch-project/data-prepper
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds sigv4 support to Elasticsearch client (opensearch-project#3305)
Adds sigv4 support to Elasticsearch client. Move AwsRequestSigningApacheInterceptor to aws-plugin-api, use in os source and sink Signed-off-by: Taylor Gray <[email protected]>
- Loading branch information
1 parent
eff31fe
commit 40980c1
Showing
9 changed files
with
226 additions
and
24 deletions.
There are no files selected for viewing
This file contains 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 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
144 changes: 144 additions & 0 deletions
144
...test/java/org/opensearch/dataprepper/aws/api/AwsRequestSigningApache4InterceptorTest.java
This file contains 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,144 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.dataprepper.aws.api; | ||
|
||
import org.apache.http.HttpEntity; | ||
import org.apache.http.HttpEntityEnclosingRequest; | ||
import org.apache.http.HttpHost; | ||
import org.apache.http.RequestLine; | ||
import org.apache.http.message.BasicHeader; | ||
import org.apache.http.protocol.HttpContext; | ||
import org.apache.http.protocol.HttpCoreContext; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider; | ||
import software.amazon.awssdk.core.interceptor.ExecutionAttributes; | ||
import software.amazon.awssdk.core.signer.Signer; | ||
import software.amazon.awssdk.http.ContentStreamProvider; | ||
import software.amazon.awssdk.http.SdkHttpFullRequest; | ||
import software.amazon.awssdk.regions.Region; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
public class AwsRequestSigningApache4InterceptorTest { | ||
|
||
@Mock | ||
private Signer signer; | ||
|
||
@Mock | ||
private AwsCredentialsProvider awsCredentialsProvider; | ||
|
||
@Mock | ||
private HttpEntityEnclosingRequest httpRequest; | ||
|
||
@Mock | ||
private HttpContext httpContext; | ||
|
||
private AwsRequestSigningApache4Interceptor createObjectUnderTest() { | ||
return new AwsRequestSigningApache4Interceptor("es", signer, awsCredentialsProvider, Region.US_EAST_1); | ||
} | ||
|
||
@Test | ||
void invalidURI_throws_IOException() { | ||
|
||
final RequestLine requestLine = mock(RequestLine.class); | ||
when(requestLine.getUri()).thenReturn("http://invalid-uri.com/file[/].html\n"); | ||
|
||
when(httpRequest.getRequestLine()).thenReturn(requestLine); | ||
|
||
final AwsRequestSigningApache4Interceptor objectUnderTest = new AwsRequestSigningApache4Interceptor("es", signer, awsCredentialsProvider, "us-east-1"); | ||
|
||
assertThrows(IOException.class, () -> objectUnderTest.process(httpRequest, httpContext)); | ||
} | ||
|
||
@Test | ||
void IOException_is_thrown_when_buildURI_throws_exception() { | ||
final RequestLine requestLine = mock(RequestLine.class); | ||
when(requestLine.getMethod()).thenReturn("GET"); | ||
when(requestLine.getUri()).thenReturn("http://localhost?param=test"); | ||
when(httpRequest.getRequestLine()).thenReturn(requestLine); | ||
|
||
when(httpContext.getAttribute(HttpCoreContext.HTTP_TARGET_HOST)).thenThrow(RuntimeException.class); | ||
|
||
final AwsRequestSigningApache4Interceptor objectUnderTest = createObjectUnderTest(); | ||
|
||
assertThrows(IOException.class, () -> objectUnderTest.process(httpRequest, httpContext)); | ||
} | ||
|
||
@Test | ||
void empty_contentStreamProvider_throws_IllegalStateException() throws IOException { | ||
final RequestLine requestLine = mock(RequestLine.class); | ||
when(requestLine.getMethod()).thenReturn("GET"); | ||
when(requestLine.getUri()).thenReturn("http://localhost?param=test"); | ||
when(httpRequest.getRequestLine()).thenReturn(requestLine); | ||
when(httpRequest.getAllHeaders()).thenReturn(new BasicHeader[]{ | ||
new BasicHeader("test-name", "test-value"), | ||
new BasicHeader("content-length", "0") | ||
}); | ||
|
||
final HttpEntity httpEntity = mock(HttpEntity.class); | ||
final InputStream inputStream = mock(InputStream.class); | ||
when(httpEntity.getContent()).thenReturn(inputStream); | ||
|
||
when((httpRequest).getEntity()).thenReturn(httpEntity); | ||
|
||
final HttpHost httpHost = HttpHost.create("http://localhost?param=test"); | ||
when(httpContext.getAttribute(HttpCoreContext.HTTP_TARGET_HOST)).thenReturn(httpHost); | ||
|
||
final SdkHttpFullRequest signedRequest = mock(SdkHttpFullRequest.class); | ||
when(signedRequest.headers()).thenReturn(Map.of("test-name", List.of("test-value"))); | ||
when(signedRequest.contentStreamProvider()).thenReturn(Optional.empty()); | ||
when(signer.sign(any(SdkHttpFullRequest.class), any(ExecutionAttributes.class))) | ||
.thenReturn(signedRequest); | ||
|
||
final AwsRequestSigningApache4Interceptor objectUnderTest = createObjectUnderTest(); | ||
|
||
assertThrows(IllegalStateException.class, () -> objectUnderTest.process(httpRequest, httpContext)); | ||
} | ||
|
||
@Test | ||
void testHappyPath() throws IOException { | ||
final RequestLine requestLine = mock(RequestLine.class); | ||
when(requestLine.getMethod()).thenReturn("GET"); | ||
when(requestLine.getUri()).thenReturn("http://localhost?param=test"); | ||
when(httpRequest.getRequestLine()).thenReturn(requestLine); | ||
when(httpRequest.getAllHeaders()).thenReturn(new BasicHeader[]{ | ||
new BasicHeader("test-name", "test-value"), | ||
new BasicHeader("content-length", "0") | ||
}); | ||
|
||
final HttpEntity httpEntity = mock(HttpEntity.class); | ||
final InputStream inputStream = mock(InputStream.class); | ||
when(httpEntity.getContent()).thenReturn(inputStream); | ||
|
||
when((httpRequest).getEntity()).thenReturn(httpEntity); | ||
|
||
final HttpHost httpHost = HttpHost.create("http://localhost?param=test"); | ||
when(httpContext.getAttribute(HttpCoreContext.HTTP_TARGET_HOST)).thenReturn(httpHost); | ||
|
||
final SdkHttpFullRequest signedRequest = mock(SdkHttpFullRequest.class); | ||
when(signedRequest.headers()).thenReturn(Map.of("test-name", List.of("test-value"))); | ||
final ContentStreamProvider contentStreamProvider = mock(ContentStreamProvider.class); | ||
final InputStream contentInputStream = mock(InputStream.class); | ||
when(contentStreamProvider.newStream()).thenReturn(contentInputStream); | ||
when(signedRequest.contentStreamProvider()).thenReturn(Optional.of(contentStreamProvider)); | ||
when(signer.sign(any(SdkHttpFullRequest.class), any(ExecutionAttributes.class))) | ||
.thenReturn(signedRequest); | ||
createObjectUnderTest().process(httpRequest, httpContext); | ||
} | ||
} |
This file contains 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 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 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 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 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.