|
@@ -19,8 +19,14 @@
|
|
package org.apache.hadoop.ozone.s3.header;
|
|
package org.apache.hadoop.ozone.s3.header;
|
|
|
|
|
|
import org.apache.hadoop.ozone.s3.exception.OS3Exception;
|
|
import org.apache.hadoop.ozone.s3.exception.OS3Exception;
|
|
|
|
+import org.apache.hadoop.test.LambdaTestUtils;
|
|
|
|
+import org.junit.Before;
|
|
import org.junit.Test;
|
|
import org.junit.Test;
|
|
|
|
|
|
|
|
+import java.time.LocalDate;
|
|
|
|
+
|
|
|
|
+import static java.time.temporal.ChronoUnit.DAYS;
|
|
|
|
+import static org.apache.hadoop.ozone.s3.header.AWSConstants.DATE_FORMATTER;
|
|
import static org.junit.Assert.assertEquals;
|
|
import static org.junit.Assert.assertEquals;
|
|
import static org.junit.Assert.fail;
|
|
import static org.junit.Assert.fail;
|
|
|
|
|
|
@@ -29,20 +35,27 @@ import static org.junit.Assert.fail;
|
|
*/
|
|
*/
|
|
|
|
|
|
public class TestAuthorizationHeaderV4 {
|
|
public class TestAuthorizationHeaderV4 {
|
|
|
|
+ private String curDate;
|
|
|
|
+
|
|
|
|
+ @Before
|
|
|
|
+ public void setup() {
|
|
|
|
+ LocalDate now = LocalDate.now();
|
|
|
|
+ curDate = DATE_FORMATTER.format(now);
|
|
|
|
+ }
|
|
|
|
|
|
@Test
|
|
@Test
|
|
public void testV4HeaderWellFormed() throws Exception {
|
|
public void testV4HeaderWellFormed() throws Exception {
|
|
String auth = "AWS4-HMAC-SHA256 " +
|
|
String auth = "AWS4-HMAC-SHA256 " +
|
|
- "Credential=ozone/20130524/us-east-1/s3/aws4_request, " +
|
|
|
|
|
|
+ "Credential=ozone/" + curDate + "/us-east-1/s3/aws4_request, " +
|
|
"SignedHeaders=host;range;x-amz-date, " +
|
|
"SignedHeaders=host;range;x-amz-date, " +
|
|
"Signature=fe5f80f77d5fa3beca038a248ff027";
|
|
"Signature=fe5f80f77d5fa3beca038a248ff027";
|
|
AuthorizationHeaderV4 v4 = new AuthorizationHeaderV4(auth);
|
|
AuthorizationHeaderV4 v4 = new AuthorizationHeaderV4(auth);
|
|
assertEquals("AWS4-HMAC-SHA256", v4.getAlgorithm());
|
|
assertEquals("AWS4-HMAC-SHA256", v4.getAlgorithm());
|
|
assertEquals("ozone", v4.getAccessKeyID());
|
|
assertEquals("ozone", v4.getAccessKeyID());
|
|
- assertEquals("20130524", v4.getDate());
|
|
|
|
|
|
+ assertEquals(curDate, v4.getDate());
|
|
assertEquals("us-east-1", v4.getAwsRegion());
|
|
assertEquals("us-east-1", v4.getAwsRegion());
|
|
assertEquals("aws4_request", v4.getAwsRequest());
|
|
assertEquals("aws4_request", v4.getAwsRequest());
|
|
- assertEquals("host;range;x-amz-date", v4.getSignedHeaders());
|
|
|
|
|
|
+ assertEquals("host;range;x-amz-date", v4.getSignedHeaderString());
|
|
assertEquals("fe5f80f77d5fa3beca038a248ff027", v4.getSignature());
|
|
assertEquals("fe5f80f77d5fa3beca038a248ff027", v4.getSignature());
|
|
}
|
|
}
|
|
|
|
|
|
@@ -50,7 +63,7 @@ public class TestAuthorizationHeaderV4 {
|
|
public void testV4HeaderMissingParts() {
|
|
public void testV4HeaderMissingParts() {
|
|
try {
|
|
try {
|
|
String auth = "AWS4-HMAC-SHA256 " +
|
|
String auth = "AWS4-HMAC-SHA256 " +
|
|
- "Credential=ozone/20130524/us-east-1/s3/aws4_request, " +
|
|
|
|
|
|
+ "Credential=ozone/" + curDate + "/us-east-1/s3/aws4_request, " +
|
|
"SignedHeaders=host;range;x-amz-date,";
|
|
"SignedHeaders=host;range;x-amz-date,";
|
|
AuthorizationHeaderV4 v4 = new AuthorizationHeaderV4(auth);
|
|
AuthorizationHeaderV4 v4 = new AuthorizationHeaderV4(auth);
|
|
fail("Exception is expected in case of malformed header");
|
|
fail("Exception is expected in case of malformed header");
|
|
@@ -63,7 +76,7 @@ public class TestAuthorizationHeaderV4 {
|
|
public void testV4HeaderInvalidCredential() {
|
|
public void testV4HeaderInvalidCredential() {
|
|
try {
|
|
try {
|
|
String auth = "AWS4-HMAC-SHA256 " +
|
|
String auth = "AWS4-HMAC-SHA256 " +
|
|
- "Credential=20130524/us-east-1/s3/aws4_request, " +
|
|
|
|
|
|
+ "Credential=" + curDate + "/us-east-1/s3/aws4_request, " +
|
|
"SignedHeaders=host;range;x-amz-date, " +
|
|
"SignedHeaders=host;range;x-amz-date, " +
|
|
"Signature=fe5f80f77d5fa3beca038a248ff027";
|
|
"Signature=fe5f80f77d5fa3beca038a248ff027";
|
|
AuthorizationHeaderV4 v4 = new AuthorizationHeaderV4(auth);
|
|
AuthorizationHeaderV4 v4 = new AuthorizationHeaderV4(auth);
|
|
@@ -77,7 +90,8 @@ public class TestAuthorizationHeaderV4 {
|
|
public void testV4HeaderWithoutSpace() throws OS3Exception {
|
|
public void testV4HeaderWithoutSpace() throws OS3Exception {
|
|
|
|
|
|
String auth =
|
|
String auth =
|
|
- "AWS4-HMAC-SHA256 Credential=ozone/20130524/us-east-1/s3/aws4_request,"
|
|
|
|
|
|
+ "AWS4-HMAC-SHA256 Credential=ozone/" + curDate + "/us-east-1/s3" +
|
|
|
|
+ "/aws4_request,"
|
|
+ "SignedHeaders=host;x-amz-content-sha256;x-amz-date,"
|
|
+ "SignedHeaders=host;x-amz-content-sha256;x-amz-date,"
|
|
+ "Signature"
|
|
+ "Signature"
|
|
+ "=fe5f80f77d5fa3beca038a248ff027";
|
|
+ "=fe5f80f77d5fa3beca038a248ff027";
|
|
@@ -85,13 +99,256 @@ public class TestAuthorizationHeaderV4 {
|
|
|
|
|
|
assertEquals("AWS4-HMAC-SHA256", v4.getAlgorithm());
|
|
assertEquals("AWS4-HMAC-SHA256", v4.getAlgorithm());
|
|
assertEquals("ozone", v4.getAccessKeyID());
|
|
assertEquals("ozone", v4.getAccessKeyID());
|
|
- assertEquals("20130524", v4.getDate());
|
|
|
|
|
|
+ assertEquals(curDate, v4.getDate());
|
|
assertEquals("us-east-1", v4.getAwsRegion());
|
|
assertEquals("us-east-1", v4.getAwsRegion());
|
|
assertEquals("aws4_request", v4.getAwsRequest());
|
|
assertEquals("aws4_request", v4.getAwsRequest());
|
|
assertEquals("host;x-amz-content-sha256;x-amz-date",
|
|
assertEquals("host;x-amz-content-sha256;x-amz-date",
|
|
- v4.getSignedHeaders());
|
|
|
|
|
|
+ v4.getSignedHeaderString());
|
|
assertEquals("fe5f80f77d5fa3beca038a248ff027", v4.getSignature());
|
|
assertEquals("fe5f80f77d5fa3beca038a248ff027", v4.getSignature());
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ @Test
|
|
|
|
+ public void testV4HeaderDateValidationSuccess() throws OS3Exception {
|
|
|
|
+ // Case 1: valid date within range.
|
|
|
|
+ LocalDate now = LocalDate.now();
|
|
|
|
+ String dateStr = DATE_FORMATTER.format(now);
|
|
|
|
+ validateResponse(dateStr);
|
|
|
|
+
|
|
|
|
+ // Case 2: Valid date with in range.
|
|
|
|
+ dateStr = DATE_FORMATTER.format(now.plus(1, DAYS));
|
|
|
|
+ validateResponse(dateStr);
|
|
|
|
+
|
|
|
|
+ // Case 3: Valid date with in range.
|
|
|
|
+ dateStr = DATE_FORMATTER.format(now.minus(1, DAYS));
|
|
|
|
+ validateResponse(dateStr);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Test
|
|
|
|
+ public void testV4HeaderDateValidationFailure() throws Exception {
|
|
|
|
+ // Case 1: Empty date.
|
|
|
|
+ LocalDate now = LocalDate.now();
|
|
|
|
+ String dateStr = "";
|
|
|
|
+ LambdaTestUtils.intercept(OS3Exception.class, "",
|
|
|
|
+ () -> validateResponse(dateStr));
|
|
|
|
+
|
|
|
|
+ // Case 2: Date after yesterday.
|
|
|
|
+ String dateStr2 = DATE_FORMATTER.format(now.plus(2, DAYS));
|
|
|
|
+ LambdaTestUtils.intercept(OS3Exception.class, "",
|
|
|
|
+ () -> validateResponse(dateStr2));
|
|
|
|
+
|
|
|
|
+ // Case 3: Date before yesterday.
|
|
|
|
+ String dateStr3 = DATE_FORMATTER.format(now.minus(2, DAYS));
|
|
|
|
+ LambdaTestUtils.intercept(OS3Exception.class, "",
|
|
|
|
+ () -> validateResponse(dateStr3));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private void validateResponse(String dateStr) throws OS3Exception {
|
|
|
|
+ String auth =
|
|
|
|
+ "AWS4-HMAC-SHA256 Credential=ozone/" + dateStr + "/us-east-1/s3" +
|
|
|
|
+ "/aws4_request,"
|
|
|
|
+ + "SignedHeaders=host;x-amz-content-sha256;x-amz-date,"
|
|
|
|
+ + "Signature"
|
|
|
|
+ + "=fe5f80f77d5fa3beca038a248ff027";
|
|
|
|
+ AuthorizationHeaderV4 v4 = new AuthorizationHeaderV4(auth);
|
|
|
|
+
|
|
|
|
+ assertEquals("AWS4-HMAC-SHA256", v4.getAlgorithm());
|
|
|
|
+ assertEquals("ozone", v4.getAccessKeyID());
|
|
|
|
+ assertEquals(dateStr, v4.getDate());
|
|
|
|
+ assertEquals("us-east-1", v4.getAwsRegion());
|
|
|
|
+ assertEquals("aws4_request", v4.getAwsRequest());
|
|
|
|
+ assertEquals("host;x-amz-content-sha256;x-amz-date",
|
|
|
|
+ v4.getSignedHeaderString());
|
|
|
|
+ assertEquals("fe5f80f77d5fa3beca038a248ff027", v4.getSignature());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Test
|
|
|
|
+ public void testV4HeaderRegionValidationFailure() throws Exception {
|
|
|
|
+ String auth =
|
|
|
|
+ "AWS4-HMAC-SHA256 Credential=ozone/" + curDate + "//s3/aws4_request,"
|
|
|
|
+ + "SignedHeaders=host;x-amz-content-sha256;x-amz-date,"
|
|
|
|
+ + "Signature"
|
|
|
|
+ + "=fe5f80f77d5fa3beca038a248ff027%";
|
|
|
|
+ LambdaTestUtils.intercept(OS3Exception.class, "",
|
|
|
|
+ () -> new AuthorizationHeaderV4(auth));
|
|
|
|
+ String auth2 =
|
|
|
|
+ "AWS4-HMAC-SHA256 Credential=ozone/" + curDate + "s3/aws4_request,"
|
|
|
|
+ + "SignedHeaders=host;x-amz-content-sha256;x-amz-date,"
|
|
|
|
+ + "Signature"
|
|
|
|
+ + "=fe5f80f77d5fa3beca038a248ff027%";
|
|
|
|
+ LambdaTestUtils.intercept(OS3Exception.class, "",
|
|
|
|
+ () -> new AuthorizationHeaderV4(auth2));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Test
|
|
|
|
+ public void testV4HeaderServiceValidationFailure() throws Exception {
|
|
|
|
+ String auth =
|
|
|
|
+ "AWS4-HMAC-SHA256 Credential=ozone/" + curDate + "/us-east-1" +
|
|
|
|
+ "//aws4_request,"
|
|
|
|
+ + "SignedHeaders=host;x-amz-content-sha256;x-amz-date,"
|
|
|
|
+ + "Signature"
|
|
|
|
+ + "=fe5f80f77d5fa3beca038a248ff027";
|
|
|
|
+ LambdaTestUtils.intercept(OS3Exception.class, "",
|
|
|
|
+ () -> new AuthorizationHeaderV4(auth));
|
|
|
|
+
|
|
|
|
+ String auth2 =
|
|
|
|
+ "AWS4-HMAC-SHA256 Credential=ozone/" + curDate + "/us-east-1" +
|
|
|
|
+ "/aws4_request,"
|
|
|
|
+ + "SignedHeaders=host;x-amz-content-sha256;x-amz-date,"
|
|
|
|
+ + "Signature"
|
|
|
|
+ + "=fe5f80f77d5fa3beca038a248ff027";
|
|
|
|
+ LambdaTestUtils.intercept(OS3Exception.class, "",
|
|
|
|
+ () -> new AuthorizationHeaderV4(auth2));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Test
|
|
|
|
+ public void testV4HeaderRequestValidationFailure() throws Exception {
|
|
|
|
+ String auth =
|
|
|
|
+ "AWS4-HMAC-SHA256 Credential=ozone/" + curDate + "/us-east-1/s3" +
|
|
|
|
+ "/ ,"
|
|
|
|
+ + "SignedHeaders=host;x-amz-content-sha256;x-amz-date,"
|
|
|
|
+ + "Signature"
|
|
|
|
+ + "=fe5f80f77d5fa3beca038a248ff027";
|
|
|
|
+ LambdaTestUtils.intercept(OS3Exception.class, "",
|
|
|
|
+ () -> new AuthorizationHeaderV4(auth));
|
|
|
|
+
|
|
|
|
+ String auth2 =
|
|
|
|
+ "AWS4-HMAC-SHA256 Credential=ozone/" + curDate + "/us-east-1/s3" +
|
|
|
|
+ "/,"
|
|
|
|
+ + "SignedHeaders=host;x-amz-content-sha256;x-amz-date,"
|
|
|
|
+ + "Signature"
|
|
|
|
+ + "=fe5f80f77d5fa3beca038a248ff027";
|
|
|
|
+ LambdaTestUtils.intercept(OS3Exception.class, "",
|
|
|
|
+ () -> new AuthorizationHeaderV4(auth2));
|
|
|
|
+
|
|
|
|
+ String auth3 =
|
|
|
|
+ "AWS4-HMAC-SHA256 Credential=ozone/" + curDate + "/us-east-1/s3" +
|
|
|
|
+ ","
|
|
|
|
+ + "SignedHeaders=host;x-amz-content-sha256;x-amz-date,"
|
|
|
|
+ + "Signature"
|
|
|
|
+ + "=fe5f80f77d5fa3beca038a248ff027";
|
|
|
|
+ LambdaTestUtils.intercept(OS3Exception.class, "",
|
|
|
|
+ () -> new AuthorizationHeaderV4(auth3));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Test
|
|
|
|
+ public void testV4HeaderSignedHeaderValidationFailure() throws Exception {
|
|
|
|
+ String auth =
|
|
|
|
+ "AWS4-HMAC-SHA256 Credential=ozone/" + curDate + "/us-east-1/s3" +
|
|
|
|
+ "/aws4_request,"
|
|
|
|
+ + "SignedHeaders=;;,"
|
|
|
|
+ + "Signature"
|
|
|
|
+ + "=fe5f80f77d5fa3beca038a248ff027";
|
|
|
|
+ LambdaTestUtils.intercept(OS3Exception.class, "",
|
|
|
|
+ () -> new AuthorizationHeaderV4(auth));
|
|
|
|
+
|
|
|
|
+ String auth2 =
|
|
|
|
+ "AWS4-HMAC-SHA256 Credential=ozone/" + curDate + "/us-east-1/s3" +
|
|
|
|
+ "/aws4_request,"
|
|
|
|
+ + "SignedHeaders=,"
|
|
|
|
+ + "Signature"
|
|
|
|
+ + "=fe5f80f77d5fa3beca038a248ff027";
|
|
|
|
+ LambdaTestUtils.intercept(OS3Exception.class, "",
|
|
|
|
+ () -> new AuthorizationHeaderV4(auth2));
|
|
|
|
+
|
|
|
|
+ String auth3 =
|
|
|
|
+ "AWS4-HMAC-SHA256 Credential=ozone/" + curDate + "/us-east-1/s3" +
|
|
|
|
+ "/aws4_request,"
|
|
|
|
+ + "=x-amz-content-sha256;x-amz-date,"
|
|
|
|
+ + "Signature"
|
|
|
|
+ + "=fe5f80f77d5fa3beca038a248ff027";
|
|
|
|
+ LambdaTestUtils.intercept(OS3Exception.class, "",
|
|
|
|
+ () -> new AuthorizationHeaderV4(auth3));
|
|
|
|
+
|
|
|
|
+ String auth4 =
|
|
|
|
+ "AWS4-HMAC-SHA256 Credential=ozone/" + curDate + "/us-east-1/s3" +
|
|
|
|
+ "/aws4_request,"
|
|
|
|
+ + "=,"
|
|
|
|
+ + "Signature"
|
|
|
|
+ + "=fe5f80f77d5fa3beca038a248ff027";
|
|
|
|
+ LambdaTestUtils.intercept(OS3Exception.class, "",
|
|
|
|
+ () -> new AuthorizationHeaderV4(auth4));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Test
|
|
|
|
+ public void testV4HeaderSignatureValidationFailure() throws Exception {
|
|
|
|
+ String auth =
|
|
|
|
+ "AWS4-HMAC-SHA256 Credential=ozone/" + curDate + "/us-east-1/s3" +
|
|
|
|
+ "/aws4_request,"
|
|
|
|
+ + "SignedHeaders=host;x-amz-content-sha256;x-amz-date,"
|
|
|
|
+ + "Signature"
|
|
|
|
+ + "=fe5f80f77d5fa3beca038a248ff027%";
|
|
|
|
+ LambdaTestUtils.intercept(OS3Exception.class, "",
|
|
|
|
+ () -> new AuthorizationHeaderV4(auth));
|
|
|
|
+
|
|
|
|
+ String auth2 =
|
|
|
|
+ "AWS4-HMAC-SHA256 Credential=ozone/" + curDate + "/us-east-1/s3" +
|
|
|
|
+ "/aws4_request,"
|
|
|
|
+ + "SignedHeaders=host;x-amz-content-sha256;x-amz-date,"
|
|
|
|
+ + "Signature"
|
|
|
|
+ + "=";
|
|
|
|
+ LambdaTestUtils.intercept(OS3Exception.class, "",
|
|
|
|
+ () -> new AuthorizationHeaderV4(auth2));
|
|
|
|
+
|
|
|
|
+ String auth3 =
|
|
|
|
+ "AWS4-HMAC-SHA256 Credential=ozone/" + curDate + "/us-east-1/s3" +
|
|
|
|
+ "/aws4_request,"
|
|
|
|
+ + "SignedHeaders=host;x-amz-content-sha256;x-amz-date,"
|
|
|
|
+ + ""
|
|
|
|
+ + "=";
|
|
|
|
+ LambdaTestUtils.intercept(OS3Exception.class, "",
|
|
|
|
+ () -> new AuthorizationHeaderV4(auth3));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Test
|
|
|
|
+ public void testV4HeaderHashAlgoValidationFailure() throws Exception {
|
|
|
|
+ String auth =
|
|
|
|
+ "AWS4-HMAC-SHA Credential=ozone/" + curDate + "/us-east-1/s3" +
|
|
|
|
+ "/aws4_request,"
|
|
|
|
+ + "SignedHeaders=host;x-amz-content-sha256;x-amz-date,"
|
|
|
|
+ + "Signature"
|
|
|
|
+ + "=fe5f80f77d5fa3beca038a248ff027";
|
|
|
|
+ LambdaTestUtils.intercept(OS3Exception.class, "",
|
|
|
|
+ () -> new AuthorizationHeaderV4(auth));
|
|
|
|
+
|
|
|
|
+ String auth2 =
|
|
|
|
+ "SHA-256 Credential=ozone/" + curDate + "/us-east-1/s3" +
|
|
|
|
+ "/aws4_request,"
|
|
|
|
+ + "SignedHeaders=host;x-amz-content-sha256;x-amz-date,"
|
|
|
|
+ + "Signature"
|
|
|
|
+ + "=fe5f80f77d5fa3beca038a248ff027";
|
|
|
|
+ LambdaTestUtils.intercept(OS3Exception.class, "",
|
|
|
|
+ () -> new AuthorizationHeaderV4(auth2));
|
|
|
|
+
|
|
|
|
+ String auth3 =
|
|
|
|
+ " Credential=ozone/" + curDate + "/us-east-1/s3" +
|
|
|
|
+ "/aws4_request,"
|
|
|
|
+ + "SignedHeaders=host;x-amz-content-sha256;x-amz-date,"
|
|
|
|
+ + "Signature"
|
|
|
|
+ + "=fe5f80f77d5fa3beca038a248ff027";
|
|
|
|
+ LambdaTestUtils.intercept(OS3Exception.class, "",
|
|
|
|
+ () -> new AuthorizationHeaderV4(auth3));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Test
|
|
|
|
+ public void testV4HeaderCredentialValidationFailure() throws Exception {
|
|
|
|
+ String auth =
|
|
|
|
+ "AWS4-HMAC-SHA Credential=/" + curDate + "//" +
|
|
|
|
+ "/,"
|
|
|
|
+ + "SignedHeaders=host;x-amz-content-sha256;x-amz-date,"
|
|
|
|
+ + "Signature"
|
|
|
|
+ + "=fe5f80f77d5fa3beca038a248ff027";
|
|
|
|
+ LambdaTestUtils.intercept(OS3Exception.class, "",
|
|
|
|
+ () -> new AuthorizationHeaderV4(auth));
|
|
|
|
+
|
|
|
|
+ String auth2 =
|
|
|
|
+ "AWS4-HMAC-SHA =/" + curDate + "//" +
|
|
|
|
+ "/,"
|
|
|
|
+ + "SignedHeaders=host;x-amz-content-sha256;x-amz-date,"
|
|
|
|
+ + "Signature"
|
|
|
|
+ + "=fe5f80f77d5fa3beca038a248ff027";
|
|
|
|
+ LambdaTestUtils.intercept(OS3Exception.class, "",
|
|
|
|
+ () -> new AuthorizationHeaderV4(auth2));
|
|
|
|
+ }
|
|
|
|
+
|
|
}
|
|
}
|