From 976e44bb573185a6278e24c07e085d0f906aedcd Mon Sep 17 00:00:00 2001 From: Luke McCrone Date: Tue, 11 Mar 2025 18:23:43 -0300 Subject: [PATCH] test: expires header test --- tests/commands/head_bucket.sh | 23 +++++++++++++++++ tests/rest_scripts/head_bucket.sh | 41 +++++++++++++++++++++++++++++++ tests/rest_scripts/put_object.sh | 8 +++++- tests/test_rest.sh | 29 +++++++++++++++++++--- tests/util/util_bucket.sh | 2 +- tests/util/util_object.sh | 30 ++++++++++++++++++++++ 6 files changed, 127 insertions(+), 6 deletions(-) create mode 100755 tests/rest_scripts/head_bucket.sh diff --git a/tests/commands/head_bucket.sh b/tests/commands/head_bucket.sh index e8240b7..6077091 100644 --- a/tests/commands/head_bucket.sh +++ b/tests/commands/head_bucket.sh @@ -35,6 +35,9 @@ head_bucket() { bucket_info=$(send_command s3cmd --no-check-certificate info "s3://$2" 2>&1) || exit_code=$? elif [[ $1 == 'mc' ]]; then bucket_info=$(send_command mc --insecure stat "$MC_ALIAS"/"$2" 2>&1) || exit_code=$? + elif [[ $1 == 'rest' ]]; then + bucket_info=$(head_bucket_rest "$2") || exit_code=$? + return $exit_code else log 2 "invalid command type $1" fi @@ -49,3 +52,23 @@ head_bucket() { echo "$bucket_info" return 0 } + +head_bucket_rest() { + if [ $# -ne 1 ]; then + log 2 "'head_bucket_rest' requires bucket name" + return 2 + fi + if ! result=$(COMMAND_LOG="$COMMAND_LOG" BUCKET_NAME="$BUCKET_ONE_NAME" OUTPUT_FILE="$TEST_FILE_FOLDER/result.txt" ./tests/rest_scripts/head_bucket.sh 2>&1); then + log 2 "error getting head bucket" + return 2 + fi + if [ "$result" == "200" ]; then + bucket_info="$(cat "$TEST_FILE_FOLDER/result.txt")" + echo "$bucket_info" + return 0 + elif [ "$result" == "404" ]; then + return 1 + fi + log 2 "unexpected response code '$result' ($(cat "$TEST_FILE_FOLDER/result.txt"))" + return 2 +} diff --git a/tests/rest_scripts/head_bucket.sh b/tests/rest_scripts/head_bucket.sh new file mode 100755 index 0000000..38a642c --- /dev/null +++ b/tests/rest_scripts/head_bucket.sh @@ -0,0 +1,41 @@ +#!/usr/bin/env bash + +# Copyright 2024 Versity Software +# This file is licensed under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http:#www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +source ./tests/rest_scripts/rest.sh + +# Fields + +# shellcheck disable=SC2153 +bucket_name="$BUCKET_NAME" + +# shellcheck disable=SC2034 +current_date_time=$(date -u +"%Y%m%dT%H%M%SZ") + +canonical_request_data=("HEAD" "/$bucket_name" "" "host:$host") +canonical_request_data+=("x-amz-content-sha256:UNSIGNED-PAYLOAD" "x-amz-date:$current_date_time") +if ! build_canonical_request "${canonical_request_data[@]}"; then + log_rest 2 "error building request" + exit 1 +fi +# shellcheck disable=SC2119 +create_canonical_hash_sts_and_signature + +curl_command+=(curl -ksI -w "\"%{http_code}\"" "$AWS_ENDPOINT_URL/$bucket_name" +-H "\"Authorization: AWS4-HMAC-SHA256 Credential=$aws_access_key_id/$year_month_day/$aws_region/s3/aws4_request,SignedHeaders=$param_list,Signature=$signature\"") +curl_command+=("${header_fields[@]}") +curl_command+=(-o "$OUTPUT_FILE") +# shellcheck disable=SC2154 +eval "${curl_command[*]}" 2>&1 diff --git a/tests/rest_scripts/put_object.sh b/tests/rest_scripts/put_object.sh index dc56465..9a6473f 100755 --- a/tests/rest_scripts/put_object.sh +++ b/tests/rest_scripts/put_object.sh @@ -28,6 +28,8 @@ key="$OBJECT_KEY" checksum_type="$CHECKSUM_TYPE" # shellcheck disable=SC2153 payload="$PAYLOAD" +# shellcheck disable=SC2153 +expires="$EXPIRES" # use this parameter to check incorrect checksums # shellcheck disable=SC2153,SC2154 @@ -40,7 +42,11 @@ else payload_hash="$payload" fi -cr_data=("PUT" "/$bucket_name/$key" "" "host:$host") +cr_data=("PUT" "/$bucket_name/$key" "") +if [ -n "$expires" ]; then + cr_data+=("expires:$expires") +fi +cr_data+=("host:$host") if [ "$checksum_type" == "sha256" ]; then if [ -z "$checksum_hash" ]; then checksum_hash="$(sha256sum "$data_file" | awk '{print $1}' | xxd -r -p | base64)" diff --git a/tests/test_rest.sh b/tests/test_rest.sh index 8a3906a..38f185f 100755 --- a/tests/test_rest.sh +++ b/tests/test_rest.sh @@ -124,7 +124,7 @@ test_file="test_file" run bucket_cleanup_if_bucket_exists "s3api" "$BUCKET_ONE_NAME" assert_success # in static bucket config, bucket will still exist - if ! bucket_exists "s3api" "$BUCKET_ONE_NAME"; then + if ! bucket_exists "rest" "$BUCKET_ONE_NAME"; then run create_bucket_object_lock_enabled "$BUCKET_ONE_NAME" assert_success fi @@ -155,7 +155,7 @@ test_file="test_file" assert_success # in static bucket config, bucket will still exist - if ! bucket_exists "s3api" "$BUCKET_ONE_NAME"; then + if ! bucket_exists "rest" "$BUCKET_ONE_NAME"; then run create_bucket_object_lock_enabled "$BUCKET_ONE_NAME" assert_success fi @@ -456,7 +456,7 @@ test_file="test_file" assert_success # in static bucket config, bucket will still exist - if ! bucket_exists "s3api" "$BUCKET_ONE_NAME"; then + if ! bucket_exists "rest" "$BUCKET_ONE_NAME"; then run create_bucket_object_lock_enabled "$BUCKET_ONE_NAME" assert_success fi @@ -482,7 +482,7 @@ test_file="test_file" assert_success # in static bucket config, bucket will still exist - if ! bucket_exists "s3api" "$BUCKET_ONE_NAME"; then + if ! bucket_exists "rest" "$BUCKET_ONE_NAME"; then run create_bucket_object_lock_enabled "$BUCKET_ONE_NAME" assert_success fi @@ -499,3 +499,24 @@ test_file="test_file" run get_delete_marker_and_verify_405 "$BUCKET_ONE_NAME" "$test_file" assert_success } + +@test "REST - invalid 'Expires' parameter" { + run setup_bucket_and_file "$BUCKET_ONE_NAME" "$test_file" + assert_success + + run put_object_rest_check_expires_header "$TEST_FILE_FOLDER/$test_file" "$BUCKET_ONE_NAME" "$test_file" + assert_success +} + +@test "REST - HeadBucket" { + run setup_bucket "s3api" "$BUCKET_ONE_NAME" + assert_success + + run head_bucket_rest "$BUCKET_ONE_NAME" + assert_success +} + +@test "REST - HeadBucket - doesn't exist" { + run head_bucket_rest "$BUCKET_ONE_NAME" + assert_failure 1 +} diff --git a/tests/util/util_bucket.sh b/tests/util/util_bucket.sh index 4ecae20..d05a5bf 100644 --- a/tests/util/util_bucket.sh +++ b/tests/util/util_bucket.sh @@ -152,7 +152,7 @@ bucket_exists() { local exists=0 head_bucket "$1" "$2" || exists=$? # shellcheck disable=SC2181 - if [ $exists -ne 0 ] && [ $exists -ne 1 ]; then + if [ $exists -eq 2 ]; then log 2 "unexpected error checking if bucket exists" return 2 fi diff --git a/tests/util/util_object.sh b/tests/util/util_object.sh index 62e6e06..cda67bf 100644 --- a/tests/util/util_object.sh +++ b/tests/util/util_object.sh @@ -457,3 +457,33 @@ check_invalid_checksum_type() { return 1 fi } + +put_object_rest_check_expires_header() { + if [ $# -ne 3 ]; then + log 2 "'put_object-put_object_rest_check_expires_header' requires data file, bucket, key" + return 1 + fi + expiry_date="Tue, 11 Mar 2025 16:00:00 GMT" + if ! result=$(COMMAND_LOG="$COMMAND_LOG" DATA_FILE="$1" BUCKET_NAME="$2" OBJECT_KEY="$3" EXPIRES="$expiry_date" OUTPUT_FILE="$TEST_FILE_FOLDER/result.txt" ./tests/rest_scripts/put_object.sh 2>&1); then + log 2 "error: $result" + return 1 + fi + if [ "$result" != "200" ]; then + log 2 "expected response code of '200', was '$result' ($(cat "$TEST_FILE_FOLDER/result.txt"))" + return 1 + fi + if ! result=$(COMMAND_LOG="$COMMAND_LOG" BUCKET_NAME="$BUCKET_ONE_NAME" OBJECT_KEY="$test_file" OUTPUT_FILE="$TEST_FILE_FOLDER/result.txt" ./tests/rest_scripts/head_object.sh 2>&1); then + log 2 "error: $result" + return 1 + fi + if [ "$result" != "200" ]; then + log 2 "expected response code of '200', was '$result' ($(cat "$TEST_FILE_FOLDER/result.txt"))" + return 1 + fi + returned_expiry_date="$(grep "Expires" "$TEST_FILE_FOLDER/result.txt" | cut -d' ' -f2- | tr -d '\r')" + if [ "$returned_expiry_date" != "$expiry_date" ]; then + log 2 "expected expiry date '$expiry_date', actual '$returned_expiry_date'" + return 1 + fi + return 0 +}