Skip to content
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

[HADOOP-18562]: S3A: support custom S3 and STS headers. #6550

Open
wants to merge 12 commits into
base: trunk
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -775,6 +775,23 @@ private Constants() {
"fs.s3a." + Constants.AWS_SERVICE_IDENTIFIER_STS.toLowerCase()
+ ".signing-algorithm";

/**
* List of custom headers to be set on the service client.
* Multiple parameters can be used to specify custom headers.
* fs.s3a.s3.custom.headers - headers to add on all the s3 requests.
* fs.s3a.sts.custom.headers - headers to add on all the sts requests.
* Examples
* CustomHeader {@literal ->} 'Header1:Value1'
* CustomHeaders {@literal ->} 'Header1=Value1:Value2,Header2=Value1'
*/
public static final String CUSTOM_HEADERS_STS =
"fs.s3a." + Constants.AWS_SERVICE_IDENTIFIER_STS.toLowerCase()
PradhanPrerak marked this conversation as resolved.
Show resolved Hide resolved
+ ".custom.headers";

public static final String CUSTOM_HEADERS_S3 =
"fs.s3a." + Constants.AWS_SERVICE_IDENTIFIER_S3.toLowerCase()
+ ".custom.headers";

public static final String S3N_FOLDER_SUFFIX = "_$folder$";
public static final String FS_S3A_BLOCK_SIZE = "fs.s3a.block.size";
public static final String FS_S3A = "s3a";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import java.net.URI;
import java.net.URISyntaxException;
import java.time.Duration;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
Expand Down Expand Up @@ -72,6 +74,8 @@
import static org.apache.hadoop.fs.s3a.Constants.SIGNING_ALGORITHM_STS;
import static org.apache.hadoop.fs.s3a.Constants.SOCKET_TIMEOUT;
import static org.apache.hadoop.fs.s3a.Constants.USER_AGENT_PREFIX;
import static org.apache.hadoop.fs.s3a.Constants.CUSTOM_HEADERS_S3;
import static org.apache.hadoop.fs.s3a.Constants.CUSTOM_HEADERS_STS;
import static org.apache.hadoop.fs.s3a.impl.ConfigurationHelper.enforceMinimumDuration;
import static org.apache.hadoop.fs.s3a.impl.ConfigurationHelper.getDuration;
import static org.apache.hadoop.util.Preconditions.checkArgument;
Expand Down Expand Up @@ -116,6 +120,8 @@ public static ClientOverrideConfiguration.Builder createClientConfigBuilder(Conf

initUserAgent(conf, overrideConfigBuilder);

initRequestHeaders(conf, overrideConfigBuilder, awsServiceIdentifier);

String signer = conf.getTrimmed(SIGNING_ALGORITHM, "");
if (!signer.isEmpty()) {
LOG.debug("Signer override = {}", signer);
Expand Down Expand Up @@ -407,6 +413,48 @@ private static void initSigner(Configuration conf,
}
}

/**
*
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need a title here

* @param conf hadoop configuration
* @param clientConfig client configuration to update
* @param awsServiceIdentifier service name
*/
private static void initRequestHeaders(Configuration conf,
ClientOverrideConfiguration.Builder clientConfig, String awsServiceIdentifier) {
String configKey = null;
switch (awsServiceIdentifier) {
case AWS_SERVICE_IDENTIFIER_S3:
configKey = CUSTOM_HEADERS_S3;
break;
case AWS_SERVICE_IDENTIFIER_STS:
configKey = CUSTOM_HEADERS_STS;
break;
default:
// Nothing to do. The original signer override is already setup
}
if (configKey != null) {
String[] customHeaders = conf.getTrimmedStrings(configKey);
if (customHeaders == null || customHeaders.length == 0) {
LOG.debug("No custom headers specified");
return;
}

for (String customHeader : customHeaders) {
PradhanPrerak marked this conversation as resolved.
Show resolved Hide resolved
String[] parts = customHeader.split("=");
if (parts.length != 2) {
String message = "Invalid format (Expected header1=value1:value2,header2=value1) for Header: ["
+ customHeader
+ "]";
LOG.error(message);
throw new IllegalArgumentException(message);
}

List<String> values = Arrays.asList(parts[1].split(":"));
clientConfig.putHeader(parts[0], values);
}
}
}

/**
* Configures request timeout in the client configuration.
* This is independent of the timeouts set in the sync and async HTTP clients;
Expand Down
Loading