Skip to content

Commit 5e96f81

Browse files
authored
(Temporary) Add region override for aws config (opensearch-project#6926)
Signed-off-by: Kondaka <krishkdk@amazon.com>
1 parent 9e8ee93 commit 5e96f81

4 files changed

Lines changed: 87 additions & 4 deletions

File tree

data-prepper-plugins/aws-plugin-api/src/main/java/org/opensearch/dataprepper/aws/api/AwsConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public Map<String, String> getAwsStsHeaderOverrides() {
6262
"Use either a named configuration reference or inline credentials, not both.")
6363
public boolean isValidConfiguration() {
6464
if (configuration != null) {
65-
return awsRegion == null && awsStsRoleArn == null && awsStsHeaderOverrides == null && awsStsExternalId == null;
65+
return awsStsRoleArn == null && awsStsHeaderOverrides == null && awsStsExternalId == null;
6666
}
6767
return true;
6868
}

data-prepper-plugins/aws-plugin-api/src/test/java/org/opensearch/dataprepper/aws/api/AwsConfigTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,10 @@ void isValidConfiguration_returns_true_when_only_configuration_is_set() throws N
7373
}
7474

7575
@Test
76-
void isValidConfiguration_returns_false_when_configuration_and_region_are_set() throws NoSuchFieldException, IllegalAccessException {
76+
void isValidConfiguration_returns_true_when_configuration_and_region_are_set() throws NoSuchFieldException, IllegalAccessException {
7777
reflectivelySetField(awsConfig, "configuration", "my_config");
7878
reflectivelySetField(awsConfig, "awsRegion", "us-east-1");
79-
assertThat(awsConfig.isValidConfiguration(), equalTo(false));
79+
assertThat(awsConfig.isValidConfiguration(), equalTo(true));
8080
}
8181

8282
@Test

data-prepper-plugins/http-sink/src/main/java/org/opensearch/dataprepper/plugins/sink/http/AwsAuthenticationDecorator.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public AwsAuthenticationDecorator(@Nonnull final AwsCredentialsSupplier awsCrede
4545
@Nonnull final String serviceName) {
4646
this.serviceName = serviceName;
4747
if (awsConfig != null && awsConfig.getConfiguration() != null) {
48-
this.region = awsCredentialsSupplier.getDefaultRegion().orElse(null);
48+
this.region = resolveRegion(awsConfig.getAwsRegion(), awsCredentialsSupplier);
4949
this.credentialsProvider = awsCredentialsSupplier.getProvider(awsConfig.getConfiguration());
5050
} else if (awsConfig != null) {
5151
this.region = awsConfig.getAwsRegion();
@@ -57,6 +57,15 @@ public AwsAuthenticationDecorator(@Nonnull final AwsCredentialsSupplier awsCrede
5757
}
5858
}
5959

60+
/**
61+
* Resolves region using: 1) use the region provided 2) if no region provided, use the default
62+
*/
63+
private static Region resolveRegion(final Region awsRegion, final AwsCredentialsSupplier awsCredentialsSupplier) {
64+
if (awsRegion != null)
65+
return awsRegion;
66+
return awsCredentialsSupplier.getDefaultRegion().orElseGet(null);
67+
}
68+
6069
private static AwsCredentialsOptions convertToCredentialOptions(final AwsConfig awsConfig) {
6170
return AwsCredentialsOptions.builder()
6271
.withRegion(awsConfig.getAwsRegion())

data-prepper-plugins/http-sink/src/test/java/org/opensearch/dataprepper/plugins/sink/http/AwsAuthenticationDecoratorTest.java

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,4 +220,78 @@ void test_constructor_with_named_configuration() {
220220
final AwsAuthenticationDecorator decorator = new AwsAuthenticationDecorator(awsCredentialsSupplier, awsConfig, TEST_SERVICE_NAME);
221221
assertThat(decorator, notNullValue());
222222
}
223+
@Test
224+
void test_constructor_with_named_configuration_resolves_region_from_supplier_default() {
225+
when(awsConfig.getConfiguration()).thenReturn("my_named_config");
226+
when(awsConfig.getAwsRegion()).thenReturn(null);
227+
when(awsCredentialsSupplier.getDefaultRegion()).thenReturn(java.util.Optional.of(Region.EU_WEST_1));
228+
when(awsCredentialsSupplier.getProvider("my_named_config")).thenReturn(awsCredentialsProvider);
229+
230+
final AwsAuthenticationDecorator decorator = new AwsAuthenticationDecorator(awsCredentialsSupplier, awsConfig, TEST_SERVICE_NAME);
231+
232+
final HttpRequest request = decorator.buildRequest(TEST_URL, TEST_PAYLOAD, null);
233+
final String authHeader = request.headers().get("Authorization");
234+
assertTrue(authHeader.contains("eu-west-1"), "Should use region from supplier default when awsConfig.getAwsRegion() is null");
235+
}
236+
237+
@Test
238+
void test_constructor_with_named_configuration_uses_explicit_region_when_set() {
239+
when(awsConfig.getConfiguration()).thenReturn("my_named_config");
240+
when(awsConfig.getAwsRegion()).thenReturn(Region.AP_SOUTHEAST_1);
241+
when(awsCredentialsSupplier.getProvider("my_named_config")).thenReturn(awsCredentialsProvider);
242+
243+
final AwsAuthenticationDecorator decorator = new AwsAuthenticationDecorator(awsCredentialsSupplier, awsConfig, TEST_SERVICE_NAME);
244+
245+
final HttpRequest request = decorator.buildRequest(TEST_URL, TEST_PAYLOAD, null);
246+
final String authHeader = request.headers().get("Authorization");
247+
assertTrue(authHeader.contains("ap-southeast-1"), "Should use explicit region from awsConfig when set");
248+
}
249+
250+
@Test
251+
void test_constructor_with_named_configuration_calls_getProvider_with_config_name() {
252+
final String configName = "ecs_task_role";
253+
when(awsConfig.getConfiguration()).thenReturn(configName);
254+
when(awsConfig.getAwsRegion()).thenReturn(TEST_REGION);
255+
when(awsCredentialsSupplier.getProvider(configName)).thenReturn(awsCredentialsProvider);
256+
257+
new AwsAuthenticationDecorator(awsCredentialsSupplier, awsConfig, TEST_SERVICE_NAME);
258+
259+
verify(awsCredentialsSupplier).getProvider(configName);
260+
}
261+
262+
@Test
263+
void test_constructor_with_null_config_uses_default_credentials_provider() {
264+
when(awsCredentialsSupplier.getDefaultRegion()).thenReturn(java.util.Optional.of(TEST_REGION));
265+
when(awsCredentialsSupplier.getProvider(any(AwsCredentialsOptions.class))).thenReturn(awsCredentialsProvider);
266+
267+
final AwsAuthenticationDecorator decorator = new AwsAuthenticationDecorator(awsCredentialsSupplier, null, TEST_SERVICE_NAME);
268+
269+
final ArgumentCaptor<AwsCredentialsOptions> captor = ArgumentCaptor.forClass(AwsCredentialsOptions.class);
270+
verify(awsCredentialsSupplier).getProvider(captor.capture());
271+
assertTrue(captor.getValue().isUseDefaultCredentialsProvider(),
272+
"Should use default credentials provider when awsConfig is null");
273+
}
274+
275+
@Test
276+
void test_constructor_with_null_config_resolves_region_from_supplier_default() {
277+
when(awsCredentialsSupplier.getDefaultRegion()).thenReturn(java.util.Optional.of(Region.US_WEST_2));
278+
when(awsCredentialsSupplier.getProvider(any(AwsCredentialsOptions.class))).thenReturn(awsCredentialsProvider);
279+
280+
final AwsAuthenticationDecorator decorator = new AwsAuthenticationDecorator(awsCredentialsSupplier, null, TEST_SERVICE_NAME);
281+
282+
final HttpRequest request = decorator.buildRequest(TEST_URL, TEST_PAYLOAD, null);
283+
final String authHeader = request.headers().get("Authorization");
284+
assertTrue(authHeader.contains("us-west-2"), "Should use region from supplier default when config is null");
285+
}
286+
287+
@Test
288+
void test_constructor_with_inline_config_does_not_call_getProvider_with_string() {
289+
// awsConfig has no 'configuration' set (returns null) — should use convertToCredentialOptions path
290+
when(awsConfig.getConfiguration()).thenReturn(null);
291+
292+
createObjectUnderTest();
293+
294+
verify(awsCredentialsSupplier).getProvider(any(AwsCredentialsOptions.class));
295+
verify(awsCredentialsSupplier, org.mockito.Mockito.never()).getProvider(org.mockito.ArgumentMatchers.anyString());
296+
}
223297
}

0 commit comments

Comments
 (0)