-
Notifications
You must be signed in to change notification settings - Fork 214
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
Support certificate content in Opensearch Source configuration to sup… #4184
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,24 +11,32 @@ | |
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.ArgumentCaptor; | ||
import org.mockito.Mock; | ||
import org.mockito.MockedStatic; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.opensearch.client.opensearch.OpenSearchClient; | ||
import org.opensearch.dataprepper.aws.api.AwsCredentialsOptions; | ||
import org.opensearch.dataprepper.aws.api.AwsCredentialsSupplier; | ||
import org.opensearch.dataprepper.plugins.source.opensearch.OpenSearchSourceConfiguration; | ||
import org.opensearch.dataprepper.plugins.source.opensearch.configuration.AwsAuthenticationConfiguration; | ||
import org.opensearch.dataprepper.plugins.source.opensearch.configuration.ConnectionConfiguration; | ||
import org.opensearch.dataprepper.plugins.truststore.TrustStoreProvider; | ||
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider; | ||
import software.amazon.awssdk.http.async.SdkAsyncHttpClient; | ||
import software.amazon.awssdk.regions.Region; | ||
|
||
import javax.net.ssl.SSLContext; | ||
import java.nio.file.Path; | ||
import java.time.Duration; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.UUID; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.equalTo; | ||
import static org.hamcrest.Matchers.notNullValue; | ||
import static org.mockito.Mockito.lenient; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.mockStatic; | ||
import static org.mockito.Mockito.never; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.verifyNoInteractions; | ||
|
@@ -48,7 +56,7 @@ public class OpenSearchClientFactoryTest { | |
|
||
@BeforeEach | ||
void setup() { | ||
when(openSearchSourceConfiguration.getHosts()).thenReturn(List.of("http://localhost:9200")); | ||
lenient().when(openSearchSourceConfiguration.getHosts()).thenReturn(List.of("http://localhost:9200")); | ||
when(openSearchSourceConfiguration.getConnectionConfiguration()).thenReturn(connectionConfiguration); | ||
} | ||
|
||
|
@@ -209,4 +217,59 @@ void provideOpenSearchClient_with_aws_auth_and_serverless_flag_true() { | |
assertThat(awsCredentialsOptions.getStsHeaderOverrides(), equalTo(Collections.emptyMap())); | ||
assertThat(awsCredentialsOptions.getStsRoleArn(), equalTo(stsRoleArn)); | ||
} | ||
|
||
@Test | ||
void provideOpenSearchClient_with_self_signed_certificate() { | ||
try (MockedStatic<TrustStoreProvider> trustStoreProviderMockedStatic = mockStatic(TrustStoreProvider.class)) { | ||
final Path path = mock(Path.class); | ||
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. I recommend moving as much out of the try block as possible to make the mocking easier to understand. I believe you can move lines 225-230 out of here. 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. Done |
||
final SSLContext sslContext = mock(SSLContext.class); | ||
final String username = UUID.randomUUID().toString(); | ||
final String password = UUID.randomUUID().toString(); | ||
when(openSearchSourceConfiguration.getUsername()).thenReturn(username); | ||
when(openSearchSourceConfiguration.getPassword()).thenReturn(password); | ||
when(connectionConfiguration.getCertPath()).thenReturn(path); | ||
trustStoreProviderMockedStatic.when(() -> TrustStoreProvider.createSSLContext(path)) | ||
.thenReturn(sslContext); | ||
final OpenSearchClient openSearchClient = createObjectUnderTest().provideOpenSearchClient(openSearchSourceConfiguration); | ||
assertThat(openSearchClient, notNullValue()); | ||
trustStoreProviderMockedStatic.verify(() -> TrustStoreProvider.createSSLContext(path)); | ||
} | ||
} | ||
|
||
@Test | ||
void provideElasticSearchClient_with_self_signed_certificate() { | ||
try (MockedStatic<TrustStoreProvider> trustStoreProviderMockedStatic = mockStatic(TrustStoreProvider.class)) { | ||
final Path path = mock(Path.class); | ||
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. As with the above, please move lines 243-248 to sit before the try block. 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. Done |
||
final SSLContext sslContext = mock(SSLContext.class); | ||
final String username = UUID.randomUUID().toString(); | ||
final String password = UUID.randomUUID().toString(); | ||
when(openSearchSourceConfiguration.getUsername()).thenReturn(username); | ||
when(openSearchSourceConfiguration.getPassword()).thenReturn(password); | ||
when(connectionConfiguration.getCertPath()).thenReturn(path); | ||
trustStoreProviderMockedStatic.when(() -> TrustStoreProvider.createSSLContext(path)) | ||
.thenReturn(sslContext); | ||
final ElasticsearchClient elasticsearchClient = createObjectUnderTest().provideElasticSearchClient(openSearchSourceConfiguration); | ||
assertThat(elasticsearchClient, notNullValue()); | ||
trustStoreProviderMockedStatic.verify(() -> TrustStoreProvider.createSSLContext(path)); | ||
} | ||
} | ||
|
||
|
||
@Test | ||
void createSdkAsyncHttpClient_with_self_signed_certificate() { | ||
try (MockedStatic<TrustStoreProvider> trustStoreProviderMockedStatic = mockStatic(TrustStoreProvider.class)) { | ||
final Path path = mock(Path.class); | ||
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. As with the above, please move lines 261-269 to sit before the try block. 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. Done |
||
final Duration duration = mock(Duration.class); | ||
final String username = UUID.randomUUID().toString(); | ||
final String password = UUID.randomUUID().toString(); | ||
lenient().when(openSearchSourceConfiguration.getUsername()).thenReturn(username); | ||
lenient().when(openSearchSourceConfiguration.getPassword()).thenReturn(password); | ||
lenient().when(connectionConfiguration.getConnectTimeout()).thenReturn(duration); | ||
lenient().when(openSearchSourceConfiguration.getConnectionConfiguration()).thenReturn(connectionConfiguration); | ||
lenient().when(connectionConfiguration.getCertPath()).thenReturn(path); | ||
final SdkAsyncHttpClient sdkAsyncHttpClient = createObjectUnderTest().createSdkAsyncHttpClient(openSearchSourceConfiguration); | ||
assertThat(sdkAsyncHttpClient, notNullValue()); | ||
trustStoreProviderMockedStatic.verify(() -> TrustStoreProvider.createTrustManager(path)); | ||
} | ||
} | ||
} |
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.
Should we have a validation on mutual exclusivity?
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