Skip to content

Commit

Permalink
remove refernces to invalid authentication configuration
Browse files Browse the repository at this point in the history
Signed-off-by: Maxwell Brown <[email protected]>
  • Loading branch information
Galactus22625 committed Jan 23, 2025
1 parent c7ac019 commit 122aaba
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 127 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@
import org.opensearch.dataprepper.plugins.sink.opensearch.configuration.AwsAuthenticationConfiguration;
import org.opensearch.dataprepper.plugins.sink.opensearch.configuration.OpenSearchSinkConfig;
import org.opensearch.dataprepper.plugins.sink.opensearch.configuration.ServerlessOptions;

import org.opensearch.dataprepper.plugins.source.opensearch.AuthConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import software.amazon.awssdk.arns.Arn;
Expand Down Expand Up @@ -100,7 +98,6 @@ public class ConnectionConfiguration {
private final String serverlessCollectionName;
private final String serverlessVpceId;
private final boolean requestCompressionEnabled;
private final AuthConfig authConfig;

List<String> getHosts() {
return hosts;
Expand Down Expand Up @@ -162,10 +159,6 @@ boolean isRequestCompressionEnabled() {
return requestCompressionEnabled;
}

public AuthConfig getAuthConfig() {
return authConfig;
}

private ConnectionConfiguration(final Builder builder) {
this.hosts = builder.hosts;
this.username = builder.username;
Expand All @@ -185,7 +178,6 @@ private ConnectionConfiguration(final Builder builder) {
this.serverlessCollectionName = builder.serverlessCollectionName;
this.serverlessVpceId = builder.serverlessVpceId;
this.requestCompressionEnabled = builder.requestCompressionEnabled;
this.authConfig = builder.authConfig;
}

public static ConnectionConfiguration readConnectionConfiguration(final OpenSearchSinkConfig openSearchSinkConfig){
Expand Down Expand Up @@ -322,18 +314,10 @@ public AwsCredentialsOptions createAwsCredentialsOptions() {

private void attachUserCredentials(final RestClientBuilder restClientBuilder) {
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
if (authConfig != null) {
if (authConfig.getUsername() != null) {
LOG.info("Using the authentication provided in the config.");
credentialsProvider.setCredentials(
AuthScope.ANY, new UsernamePasswordCredentials(authConfig.getUsername(), authConfig.getPassword()));
}
} else {
if (username != null) {
LOG.info("Using the username provided in the config.");
credentialsProvider.setCredentials(
AuthScope.ANY, new UsernamePasswordCredentials(username, password));
}
if (username != null) {
LOG.info("Using the username provided in the config.");
credentialsProvider.setCredentials(
AuthScope.ANY, new UsernamePasswordCredentials(username, password));
}
restClientBuilder.setHttpClientConfigCallback(
httpClientBuilder -> {
Expand Down Expand Up @@ -497,7 +481,6 @@ public static class Builder {
private String serverlessCollectionName;
private String serverlessVpceId;
private boolean requestCompressionEnabled;
private AuthConfig authConfig;

private void validateStsRoleArn(final String awsStsRoleArn) {
final Arn arn = getArn(awsStsRoleArn);
Expand Down Expand Up @@ -622,11 +605,6 @@ public Builder withRequestCompressionEnabled(final boolean requestCompressionEna
return this;
}

public Builder withAuthConfig(final AuthConfig authConfig) {
this.authConfig = authConfig;
return this;
}

public ConnectionConfiguration build() {
return new ConnectionConfiguration(this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,23 +74,13 @@ public void update(OpenSearchSinkConfig openSearchSinkConfig) {
private boolean basicAuthChanged(final ConnectionConfiguration newConfig) {
final String existingUsername;
final String existingPassword;
if (currentConfig.getAuthConfig() != null) {
existingUsername = currentConfig.getAuthConfig().getUsername();
existingPassword = currentConfig.getAuthConfig().getPassword();
} else {
existingUsername = currentConfig.getUsername();
existingPassword = currentConfig.getPassword();
}
existingUsername = currentConfig.getUsername();
existingPassword = currentConfig.getPassword();

final String newUsername;
final String newPassword;
if (newConfig.getAuthConfig() != null) {
newUsername = newConfig.getAuthConfig().getUsername();
newPassword = newConfig.getAuthConfig().getPassword();
} else {
newUsername = newConfig.getUsername();
newPassword = newConfig.getPassword();
}
newUsername = newConfig.getUsername();
newPassword = newConfig.getPassword();

return !Objects.equals(existingUsername, newUsername) ||
!Objects.equals(existingPassword, newPassword);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import org.opensearch.client.opensearch.OpenSearchClient;
import org.opensearch.dataprepper.metrics.PluginMetrics;
import org.opensearch.dataprepper.plugins.sink.opensearch.configuration.OpenSearchSinkConfig;
import org.opensearch.dataprepper.plugins.source.opensearch.AuthConfig;

import java.util.function.Function;

Expand Down Expand Up @@ -41,9 +40,6 @@ class OpenSearchClientRefresherTest {
@Mock
private OpenSearchClient openSearchClient;

@Mock
private AuthConfig authConfig;

@Mock
private PluginMetrics pluginMetrics;

Expand All @@ -69,7 +65,7 @@ void testGet() {
}

@Test
void testGetAfterUpdateWithDeprecatedBasicAuthUnchanged() {
void testGetAfterUpdateWithBasicAuthUnchanged() {
final OpenSearchClientRefresher objectUnderTest = createObjectUnderTest();
assertThat(objectUnderTest.get(), equalTo(openSearchClient));
verify(clientFunction, times(1)).apply(any());
Expand All @@ -90,31 +86,7 @@ void testGetAfterUpdateWithDeprecatedBasicAuthUnchanged() {
}

@Test
void testGetAfterUpdateWithBasicAuthUnchanged() {
final OpenSearchClientRefresher objectUnderTest = createObjectUnderTest();
assertThat(objectUnderTest.get(), equalTo(openSearchClient));
verify(clientFunction, times(1)).apply(any());
when(connectionConfiguration.getAuthConfig()).thenReturn(authConfig);
when(authConfig.getUsername()).thenReturn(TEST_USERNAME);
when(authConfig.getPassword()).thenReturn(TEST_PASSWORD);
final OpenSearchSinkConfig newConfig = mock(OpenSearchSinkConfig.class);
final ConnectionConfiguration newConnectionConfiguration = mock(ConnectionConfiguration.class);
final AuthConfig newAuthConfig = mock(AuthConfig.class);
when(newConnectionConfiguration.getAuthConfig()).thenReturn(newAuthConfig);
when(newAuthConfig.getUsername()).thenReturn(TEST_USERNAME);
when(newAuthConfig.getPassword()).thenReturn(TEST_PASSWORD);
try (MockedStatic<ConnectionConfiguration> configurationMockedStatic = mockStatic(
ConnectionConfiguration.class)) {
configurationMockedStatic.when(() -> ConnectionConfiguration.readConnectionConfiguration(eq(newConfig)))
.thenReturn(newConnectionConfiguration);
objectUnderTest.update(newConfig);
}
assertThat(objectUnderTest.get(), equalTo(openSearchClient));
verifyNoMoreInteractions(clientFunction);
}

@Test
void testGetAfterUpdateWithDeprecatedUsernameChanged() {
void testGetAfterUpdateWithUsernameChanged() {
when(pluginMetrics.counter(CREDENTIALS_CHANGED)).thenReturn(credentialsChangeCounter);
final OpenSearchClientRefresher objectUnderTest = createObjectUnderTest();
assertThat(objectUnderTest.get(), equalTo(openSearchClient));
Expand All @@ -137,36 +109,9 @@ void testGetAfterUpdateWithDeprecatedUsernameChanged() {
verify(clientFunction, times(2)).apply(any());
}

@Test
void testGetAfterUpdateWithUsernameChanged() {
when(pluginMetrics.counter(CREDENTIALS_CHANGED)).thenReturn(credentialsChangeCounter);
final OpenSearchClientRefresher objectUnderTest = createObjectUnderTest();
assertThat(objectUnderTest.get(), equalTo(openSearchClient));
verify(clientFunction, times(1)).apply(any());
assertThat(objectUnderTest.get(), equalTo(openSearchClient));
when(connectionConfiguration.getAuthConfig()).thenReturn(authConfig);
when(authConfig.getUsername()).thenReturn(TEST_USERNAME);
when(authConfig.getPassword()).thenReturn(TEST_PASSWORD);
final OpenSearchSinkConfig newConfig = mock(OpenSearchSinkConfig.class);
final ConnectionConfiguration newConnectionConfiguration = mock(ConnectionConfiguration.class);
final AuthConfig newAuthConfig = mock(AuthConfig.class);
when(newConnectionConfiguration.getAuthConfig()).thenReturn(newAuthConfig);
when(newAuthConfig.getUsername()).thenReturn(TEST_USERNAME + "_changed");
final OpenSearchClient newClient = mock(OpenSearchClient.class);
when(clientFunction.apply(eq(newConnectionConfiguration))).thenReturn(newClient);
try (MockedStatic<ConnectionConfiguration> configurationMockedStatic = mockStatic(
ConnectionConfiguration.class)) {
configurationMockedStatic.when(() -> ConnectionConfiguration.readConnectionConfiguration(eq(newConfig)))
.thenReturn(newConnectionConfiguration);
objectUnderTest.update(newConfig);
}
assertThat(objectUnderTest.get(), equalTo(newClient));
verify(credentialsChangeCounter).increment();
verify(clientFunction, times(2)).apply(any());
}

@Test
void testGetAfterUpdateWithDeprecatedPasswordChanged() {
void testGetAfterUpdateWithPasswordChanged() {
when(pluginMetrics.counter(CREDENTIALS_CHANGED)).thenReturn(credentialsChangeCounter);
final OpenSearchClientRefresher objectUnderTest = createObjectUnderTest();
assertThat(objectUnderTest.get(), equalTo(openSearchClient));
Expand All @@ -191,35 +136,6 @@ void testGetAfterUpdateWithDeprecatedPasswordChanged() {
verify(clientFunction, times(2)).apply(any());
}

@Test
void testGetAfterUpdateWithPasswordChanged() {
when(pluginMetrics.counter(CREDENTIALS_CHANGED)).thenReturn(credentialsChangeCounter);
final OpenSearchClientRefresher objectUnderTest = createObjectUnderTest();
assertThat(objectUnderTest.get(), equalTo(openSearchClient));
verify(clientFunction, times(1)).apply(any());
assertThat(objectUnderTest.get(), equalTo(openSearchClient));
when(connectionConfiguration.getAuthConfig()).thenReturn(authConfig);
when(authConfig.getUsername()).thenReturn(TEST_USERNAME);
when(authConfig.getPassword()).thenReturn(TEST_PASSWORD);
final OpenSearchSinkConfig newConfig = mock(OpenSearchSinkConfig.class);
final ConnectionConfiguration newConnectionConfiguration = mock(ConnectionConfiguration.class);
final AuthConfig newAuthConfig = mock(AuthConfig.class);
when(newConnectionConfiguration.getAuthConfig()).thenReturn(newAuthConfig);
when(newAuthConfig.getUsername()).thenReturn(TEST_USERNAME);
when(newAuthConfig.getPassword()).thenReturn(TEST_PASSWORD + "_changed");
final OpenSearchClient newClient = mock(OpenSearchClient.class);
when(clientFunction.apply(eq(newConnectionConfiguration))).thenReturn(newClient);
try (MockedStatic<ConnectionConfiguration> configurationMockedStatic = mockStatic(
ConnectionConfiguration.class)) {
configurationMockedStatic.when(() -> ConnectionConfiguration.readConnectionConfiguration(eq(newConfig)))
.thenReturn(newConnectionConfiguration);
objectUnderTest.update(newConfig);
}
assertThat(objectUnderTest.get(), equalTo(newClient));
verify(credentialsChangeCounter).increment();
verify(clientFunction, times(2)).apply(any());
}

@Test
void testGetAfterUpdateClientFailure() {
when(pluginMetrics.counter(CREDENTIALS_CHANGED)).thenReturn(credentialsChangeCounter);
Expand Down

0 comments on commit 122aaba

Please sign in to comment.