Skip to content

Commit 30e9a18

Browse files
committed
Add hash key test
1 parent bea7dc4 commit 30e9a18

File tree

4 files changed

+58
-7
lines changed

4 files changed

+58
-7
lines changed

Diff for: include/aws/s3/private/s3express_credentials_provider_impl.h

+6-2
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,12 @@ struct aws_s3express_credentials_provider *aws_s3express_credentials_provider_ne
107107
const struct aws_s3express_credentials_provider_default_options *options);
108108

109109
/**
110-
* Encode the hash key to be [host_value][hash_of_credentials]
111-
* hash_of_credentials is the sha256 of [access_key][secret_access_key]
110+
* Encodes the hash key in the format: [host_value][hash_of_credentials_and_headers]
111+
*
112+
* The hash_of_credentials_and_headers is calculated as follows:
113+
* 1. Concatenate: [access_key][secret_access_key][headers]
114+
* where headers = ",header_name1:header_value1,header_name2:header_value2..."
115+
* 2. Generates SHA256 hash of the concatenated string
112116
*/
113117
AWS_S3_API
114118
struct aws_string *aws_encode_s3express_hash_key_new(

Diff for: source/s3express_credentials_provider.c

-5
Original file line numberDiff line numberDiff line change
@@ -480,11 +480,6 @@ static struct aws_s3express_session_creator *s_aws_s3express_session_creator_des
480480
return NULL;
481481
}
482482

483-
/**
484-
* Encode the hash key to be [host_value][hash_of_credentials]
485-
* hash_of_credentials is the sha256 of [access_key][secret_access_key]
486-
* TODO: Update docs
487-
**/
488483
struct aws_string *aws_encode_s3express_hash_key_new(
489484
struct aws_allocator *allocator,
490485
const struct aws_credentials *original_credentials,

Diff for: tests/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,7 @@ add_net_test_case(s3express_client_get_object_multiple)
370370
add_net_test_case(s3express_client_get_object_create_session_error)
371371
add_net_test_case(s3express_client_copy_object)
372372
add_net_test_case(s3express_client_copy_object_multipart)
373+
add_net_test_case(s3express_hash_key_test)
373374

374375
add_net_test_case(meta_request_auto_ranged_get_new_error_handling)
375376
add_net_test_case(meta_request_auto_ranged_put_new_error_handling)

Diff for: tests/s3_s3express_client_test.c

+51
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "s3_tester.h"
1111
#include <aws/common/atomics.h>
1212
#include <aws/common/clock.h>
13+
#include <aws/common/encoding.h>
1314
#include <aws/common/lru_cache.h>
1415
#include <aws/io/stream.h>
1516
#include <aws/io/uri.h>
@@ -714,3 +715,53 @@ TEST_CASE(s3express_client_copy_object_multipart) {
714715
aws_s3_tester_clean_up(&tester);
715716
return AWS_OP_SUCCESS;
716717
}
718+
719+
/**
720+
* Test hash of the express cache key
721+
*/
722+
TEST_CASE(s3express_hash_key_test) {
723+
(void)ctx;
724+
725+
struct aws_string *access_key = aws_string_new_from_c_str(allocator, "AccessKey");
726+
struct aws_string *secret_access_key = aws_string_new_from_c_str(allocator, "SecretAccessKey");
727+
struct aws_http_headers *headers = aws_http_headers_new(allocator);
728+
aws_http_headers_add(
729+
headers, aws_byte_cursor_from_c_str("x-amz-server-side-encryption"), aws_byte_cursor_from_c_str("aws:kms"));
730+
aws_http_headers_add(
731+
headers,
732+
aws_byte_cursor_from_c_str("x-amz-server-side-encryption-aws-kms-key-id"),
733+
aws_byte_cursor_from_c_str("kms-key-id"));
734+
aws_http_headers_add(
735+
headers,
736+
aws_byte_cursor_from_c_str("x-amz-server-side-encryption-context"),
737+
aws_byte_cursor_from_c_str("context"));
738+
aws_http_headers_add(
739+
headers,
740+
aws_byte_cursor_from_c_str("x-amz-server-side-encryption-bucket-key-enabled"),
741+
aws_byte_cursor_from_c_str("true"));
742+
aws_http_headers_add(
743+
headers, aws_byte_cursor_from_c_str("header-not-allowed"), aws_byte_cursor_from_c_str("should-be-ignored"));
744+
745+
struct aws_credentials *creds =
746+
aws_credentials_new_from_string(allocator, access_key, secret_access_key, NULL, UINT64_MAX);
747+
748+
struct aws_string *hash_key =
749+
aws_encode_s3express_hash_key_new(allocator, creds, aws_byte_cursor_from_c_str("host"), headers);
750+
struct aws_byte_cursor hash_cursor = aws_byte_cursor_from_string(hash_key);
751+
752+
struct aws_byte_buf encoded_buf;
753+
aws_byte_buf_init(&encoded_buf, allocator, 100);
754+
aws_hex_encode_append_dynamic(&hash_cursor, &encoded_buf);
755+
756+
char *expected_encoded_key = "686f737498ae6a365790707488b3e85402c9eddf422dc39f096e15eaba0d7cdd45f57ad2";
757+
ASSERT_BIN_ARRAYS_EQUALS(expected_encoded_key, strlen(expected_encoded_key), encoded_buf.buffer, encoded_buf.len);
758+
759+
aws_byte_buf_clean_up(&encoded_buf);
760+
aws_string_destroy(access_key);
761+
aws_string_destroy(secret_access_key);
762+
aws_credentials_release(creds);
763+
aws_string_destroy(hash_key);
764+
aws_http_headers_release(headers);
765+
766+
return 0;
767+
}

0 commit comments

Comments
 (0)