Skip to content

SigV4: Allow specifying Host header as config #134

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* @since 4.3
*/
public class S3Config {
private final String signatureHostname;
private final String hostname;
private final String region;
private final String bucket;
Expand All @@ -30,6 +31,8 @@ public class S3Config {
* Constructor for S3Config.
* </p>
*
* @param signatureHostname
* Hostname to use for the SigV4 signature
* @param hostname
* S3 API host
* @param region
Expand All @@ -50,9 +53,10 @@ public class S3Config {
* verification
* @since 5.8
*/
public S3Config(String hostname, String region, String bucket, String storageClass,
public S3Config(String signatureHostname, String hostname, String region, String bucket, String storageClass,
String accessKey, String secretKey, int expirationSeconds,
boolean disableSslVerify) {
this.signatureHostname = signatureHostname;
this.hostname = hostname;
this.region = region;
this.bucket = bucket;
Expand All @@ -63,6 +67,37 @@ public S3Config(String hostname, String region, String bucket, String storageCla
this.disableSslVerify = disableSslVerify;
}

/**
* <p>
* Constructor for S3Config.
* </p>
*
* @param hostname
* S3 API host
* @param region
* AWS region
* @param bucket
* S3 storage bucket
* @param storageClass
* S3 storage class
* @param accessKey
* access key for authenticating to AWS
* @param secretKey
* secret key for authenticating to AWS
* @param expirationSeconds
* period in seconds after which requests signed for this bucket
* will expire
* @param disableSslVerify
* if {@code true} disable Amazon server certificate and hostname
* verification
* @since 5.8
*/
public S3Config(String hostname, String region, String bucket, String storageClass,
String accessKey, String secretKey, int expirationSeconds,
boolean disableSslVerify) {
this(hostname, hostname, region, bucket, storageClass, accessKey, secretKey, expirationSeconds, disableSslVerify);
}

/**
* <p>Constructor for S3Config.</p>
*
Expand Down Expand Up @@ -91,6 +126,16 @@ public S3Config(String region, String bucket, String storageClass,
disableSslVerify);
}

/**
* Get the <code>hostname</code>.
*
* @return Get the hostname to use for SigV4 signature calculation
* @since 5.8
*/
public String getSignatureHostname() {
return signatureHostname;
}

/**
* Get the <code>hostname</code>.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.eclipse.jgit.util.HttpSupport.HDR_AUTHORIZATION;

import com.google.common.base.Strings;

import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLEncoder;
Expand Down Expand Up @@ -92,7 +94,12 @@ class SignerV4 {
static String createAuthorizationQuery(S3Config bucketConfig, URL url,
String httpMethod, Map<String, String> headers,
Map<String, String> queryParameters, String bodyHash) {
addHostHeader(url, headers);

if (!Strings.isNullOrEmpty(bucketConfig.getSignatureHostname())) {
headers.put("Host", bucketConfig.getSignatureHostname()); //$NON-NLS-1$
} else {
addHostHeader(url, headers);
}

queryParameters.put(X_AMZ_ALGORITHM, SCHEME + "-" + ALGORITHM); //$NON-NLS-1$

Expand Down Expand Up @@ -161,7 +168,12 @@ private static void appendQuery(StringBuilder s, String key,
static Map<String, String> createHeaderAuthorization(
S3Config bucketConfig, URL url, String httpMethod,
Map<String, String> headers, String bodyHash) {
addHostHeader(url, headers);

if (!Strings.isNullOrEmpty(bucketConfig.getSignatureHostname())) {
headers.put("Host", bucketConfig.getSignatureHostname()); //$NON-NLS-1$
} else {
addHostHeader(url, headers);
}

Date now = new Date();
String dateTimeStamp = dateTimeStampISO8601(now);
Expand Down