Skip to content

Commit 95a4295

Browse files
author
Aditya Deshpande
committed
Trim header values and update unit tests
1 parent 9406435 commit 95a4295

File tree

2 files changed

+81
-16
lines changed

2 files changed

+81
-16
lines changed

hadoop-tools/hadoop-aws/src/main/java/org/apache/hadoop/fs/s3a/impl/AWSClientConfig.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.util.List;
2727
import java.util.Map;
2828
import java.util.concurrent.TimeUnit;
29+
import java.util.stream.Collectors;
2930

3031
import org.slf4j.Logger;
3132
import org.slf4j.LoggerFactory;
@@ -442,8 +443,15 @@ private static void initRequestHeaders(Configuration conf,
442443
Map<String, String> awsClientCustomHeadersMap =
443444
S3AUtils.getTrimmedStringCollectionSplitByEquals(conf, configKey);
444445
awsClientCustomHeadersMap.forEach((header, valueString) -> {
445-
List<String> headerValues = Arrays.asList(valueString.split(";"));
446-
clientConfig.putHeader(header, headerValues);
446+
List<String> headerValues = Arrays.stream(valueString.split(";"))
447+
.map(String::trim)
448+
.filter(v -> !v.isEmpty())
449+
.collect(Collectors.toList());
450+
if (!headerValues.isEmpty()) {
451+
clientConfig.putHeader(header, headerValues);
452+
} else {
453+
LOG.warn("Ignoring header '{}' for {} client because no values were provided", header, awsServiceIdentifier);
454+
}
447455
});
448456
LOG.debug("headers for {} client = {}", awsServiceIdentifier, clientConfig.headers());
449457
}

hadoop-tools/hadoop-aws/src/test/java/org/apache/hadoop/fs/s3a/impl/TestAwsClientConfig.java

Lines changed: 71 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,8 @@ private void setOptionsToValue(String value, Configuration conf, String... keys)
216216
@Test
217217
public void testInitRequestHeadersForSTS() throws IOException {
218218
final Configuration conf = new Configuration();
219-
conf.set(CUSTOM_HEADERS_STS, "foo=bar;baz,qux=quux");
219+
conf.set(CUSTOM_HEADERS_STS, "header1=value1;value2,header2=value3");
220+
220221
Assertions.assertThat(conf.get(CUSTOM_HEADERS_S3))
221222
.describedAs("Custom client headers for s3 %s", CUSTOM_HEADERS_S3)
222223
.isNull();
@@ -225,18 +226,21 @@ public void testInitRequestHeadersForSTS() throws IOException {
225226
.headers().size())
226227
.describedAs("Count of S3 client headers")
227228
.isEqualTo(0);
229+
228230
Assertions.assertThat(createClientConfigBuilder(conf, AWS_SERVICE_IDENTIFIER_STS)
229231
.headers().size())
230232
.describedAs("Count of STS client headers")
231233
.isEqualTo(2);
234+
232235
Assertions.assertThat(createClientConfigBuilder(conf, AWS_SERVICE_IDENTIFIER_STS)
233-
.headers().get("foo"))
234-
.describedAs("STS client 'foo' header value")
235-
.isEqualTo(Lists.newArrayList("bar", "baz"));
236+
.headers().get("header1"))
237+
.describedAs("STS client 'header1' header value")
238+
.isEqualTo(Lists.newArrayList("value1", "value2"));
239+
236240
Assertions.assertThat(createClientConfigBuilder(conf, AWS_SERVICE_IDENTIFIER_STS)
237-
.headers().get("qux"))
238-
.describedAs("STS client 'qux' header value")
239-
.isEqualTo(Lists.newArrayList("quux"));
241+
.headers().get("header2"))
242+
.describedAs("STS client 'header2' header value")
243+
.isEqualTo(Lists.newArrayList("value3"));
240244
}
241245

242246
/**
@@ -246,7 +250,8 @@ public void testInitRequestHeadersForSTS() throws IOException {
246250
@Test
247251
public void testInitRequestHeadersForS3() throws IOException {
248252
final Configuration conf = new Configuration();
249-
conf.set(CUSTOM_HEADERS_S3, "foo=bar;baz,qux=quux");
253+
conf.set(CUSTOM_HEADERS_S3, "header1=value1;value2,header2=value3");
254+
250255
Assertions.assertThat(conf.get(CUSTOM_HEADERS_STS))
251256
.describedAs("Custom client headers for STS %s", CUSTOM_HEADERS_STS)
252257
.isNull();
@@ -255,17 +260,69 @@ public void testInitRequestHeadersForS3() throws IOException {
255260
.headers().size())
256261
.describedAs("Count of STS client headers")
257262
.isEqualTo(0);
263+
258264
Assertions.assertThat(createClientConfigBuilder(conf, AWS_SERVICE_IDENTIFIER_S3)
259265
.headers().size())
260266
.describedAs("Count of S3 client headers")
261267
.isEqualTo(2);
268+
269+
Assertions.assertThat(createClientConfigBuilder(conf, AWS_SERVICE_IDENTIFIER_S3)
270+
.headers().get("header1"))
271+
.describedAs("S3 client 'header1' header value")
272+
.isEqualTo(Lists.newArrayList("value1", "value2"));
273+
274+
Assertions.assertThat(createClientConfigBuilder(conf, AWS_SERVICE_IDENTIFIER_S3)
275+
.headers().get("header2"))
276+
.describedAs("S3 client 'header2' header value")
277+
.isEqualTo(Lists.newArrayList("value3"));
278+
}
279+
280+
/**
281+
* if {@link org.apache.hadoop.fs.s3a.Constants#CUSTOM_HEADERS_S3} is set,
282+
* verify that returned client configuration has desired headers set with whitespaces trimmed for headers and values.
283+
*/
284+
@Test
285+
public void testInitRequestHeadersForS3WithWhitespace() throws IOException {
286+
final Configuration conf = new Configuration();
287+
conf.set(CUSTOM_HEADERS_S3, " header1 = value1 ; value2 , header2= value3 ");
288+
289+
Assertions.assertThat(conf.get(CUSTOM_HEADERS_STS))
290+
.describedAs("Custom client headers for STS %s", CUSTOM_HEADERS_STS)
291+
.isNull();
292+
293+
Assertions.assertThat(createClientConfigBuilder(conf, AWS_SERVICE_IDENTIFIER_STS)
294+
.headers().size())
295+
.describedAs("Count of STS client headers")
296+
.isEqualTo(0);
297+
298+
Assertions.assertThat(createClientConfigBuilder(conf, AWS_SERVICE_IDENTIFIER_S3)
299+
.headers().size())
300+
.describedAs("Count of S3 client headers")
301+
.isEqualTo(2);
302+
262303
Assertions.assertThat(createClientConfigBuilder(conf, AWS_SERVICE_IDENTIFIER_S3)
263-
.headers().get("foo"))
264-
.describedAs("S3 client 'foo' header value")
265-
.isEqualTo(Lists.newArrayList("bar", "baz"));
304+
.headers().get("header1"))
305+
.describedAs("S3 client 'header1' header value")
306+
.isEqualTo(Lists.newArrayList("value1", "value2"));
307+
308+
Assertions.assertThat(createClientConfigBuilder(conf, AWS_SERVICE_IDENTIFIER_S3)
309+
.headers().get("header2"))
310+
.describedAs("S3 client 'header2' header value")
311+
.isEqualTo(Lists.newArrayList("value3"));
312+
}
313+
314+
/**
315+
* if {@link org.apache.hadoop.fs.s3a.Constants#CUSTOM_HEADERS_S3} is set with duplicate values,
316+
* verify that returned client configuration has desired headers with both values.
317+
*/
318+
@Test
319+
public void testInitRequestHeadersForS3WithDuplicateValues() throws IOException {
320+
Configuration conf = new Configuration();
321+
conf.set(CUSTOM_HEADERS_S3, "header1=duplicate;duplicate");
322+
266323
Assertions.assertThat(createClientConfigBuilder(conf, AWS_SERVICE_IDENTIFIER_S3)
267-
.headers().get("qux"))
268-
.describedAs("S3 client 'qux' header value")
269-
.isEqualTo(Lists.newArrayList("quux"));
324+
.headers().get("header1"))
325+
.describedAs("S3 client 'header1' header value")
326+
.isEqualTo(Lists.newArrayList("duplicate", "duplicate"));
270327
}
271328
}

0 commit comments

Comments
 (0)