diff --git a/tests/commands/create_bucket.sh b/tests/commands/create_bucket.sh new file mode 100644 index 0000000..5fad8fc --- /dev/null +++ b/tests/commands/create_bucket.sh @@ -0,0 +1,169 @@ +#!/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/report.sh + +# create an AWS bucket +# param: bucket name +# return 0 for success, 1 for failure +create_bucket() { + log 6 "create_bucket" + if ! check_param_count "create_bucket" "command type, bucket" 2 $#; then + return 1 + fi + + local exit_code=0 error + if [[ $1 == 's3' ]]; then + error=$(send_command aws --no-verify-ssl s3 mb s3://"$2" 2>&1) || exit_code=$? + elif [[ $1 == 's3api' ]]; then + error=$(send_command aws --no-verify-ssl s3api create-bucket --bucket "$2" 2>&1) || exit_code=$? + elif [[ $1 == "s3cmd" ]]; then + log 5 "s3cmd ${S3CMD_OPTS[*]} --no-check-certificate mb s3://$2" + error=$(send_command s3cmd "${S3CMD_OPTS[@]}" --no-check-certificate mb s3://"$2" 2>&1) || exit_code=$? + elif [[ $1 == "mc" ]]; then + error=$(send_command mc --insecure mb "$MC_ALIAS"/"$2" --region "$AWS_REGION" 2>&1) || exit_code=$? + else + error="invalid command type $1" + exit_code=1 + fi + if [ $exit_code -ne 0 ]; then + printf "error creating bucket: %s\n" "$error" >&2 + return $exit_code + fi + return 0 +} + +create_bucket_invalid_name() { + if [ $# -ne 1 ]; then + log 2 "create bucket w/invalid name missing command type" + return 1 + fi + local exit_code=0 + if [[ $1 == "aws" ]] || [[ $1 == 's3' ]]; then + bucket_create_error=$(aws --no-verify-ssl s3 mb "s3://" 2>&1) || exit_code=$? + elif [[ $1 == 's3api' ]]; then + bucket_create_error=$(aws --no-verify-ssl s3api create-bucket --bucket "s3://" 2>&1) || exit_code=$? + elif [[ $1 == 's3cmd' ]]; then + bucket_create_error=$(s3cmd "${S3CMD_OPTS[@]}" --no-check-certificate mb --region="$AWS_REGION" "s3://" 2>&1) || exit_code=$? + elif [[ $1 == 'mc' ]]; then + bucket_create_error=$(mc --insecure mb "$MC_ALIAS/." 2>&1) || exit_code=$? + else + log 2 "invalid command type $1" + return 1 + fi + if [ $exit_code -eq 0 ]; then + log 2 "error: bucket should have not been created but was" + return 1 + fi + echo "$bucket_create_error" +} + +create_bucket_with_user() { + log 6 "create_bucket_with_user" + if ! check_param_count "create_bucket_with_user" "command type, bucket, access ID, secret key" 4 $#; then + return 1 + fi + local exit_code=0 + if [[ $1 == "aws" ]] || [[ $1 == "s3api" ]]; then + error=$(AWS_ACCESS_KEY_ID="$3" AWS_SECRET_ACCESS_KEY="$4" send_command aws --no-verify-ssl s3 mb s3://"$2" 2>&1) || exit_code=$? + elif [[ $1 == "s3cmd" ]]; then + error=$(send_command s3cmd "${S3CMD_OPTS[@]}" --no-check-certificate mb --access_key="$3" --secret_key="$4" --region="$AWS_REGION" s3://"$2" 2>&1) || exit_code=$? + elif [[ $1 == "mc" ]]; then + error=$(send_command mc --insecure mb "$MC_ALIAS"/"$2" 2>&1) || exit_code=$? + else + log 2 "invalid command type $1" + return 1 + fi + if [ $exit_code -ne 0 ]; then + log 2 "error creating bucket: $error" + return 1 + fi + return 0 +} + +create_bucket_object_lock_enabled() { + log 6 "create_bucket_object_lock_enabled" + if ! check_param_count "create_bucket_object_lock_enabled" "bucket" 1 $#; then + return 1 + fi + + local exit_code=0 + error=$(send_command aws --no-verify-ssl s3api create-bucket --bucket "$1" 2>&1 --object-lock-enabled-for-bucket 2>&1) || local exit_code=$? + if [ $exit_code -ne 0 ]; then + log 2 "error creating bucket: $error" + return 1 + fi + if [ "$DIRECT" == "true" ]; then + sleep 15 + fi + return 0 +} + +create_bucket_rest_with_invalid_acl() { + if ! result=$(COMMAND_LOG="$COMMAND_LOG" BUCKET_NAME="$BUCKET_ONE_NAME" OUTPUT_FILE="$TEST_FILE_FOLDER/result.txt" ACL="public-reads" OBJECT_OWNERSHIP="BucketOwnerPreferred" ./tests/rest_scripts/create_bucket.sh 2>&1); then + log 2 "error creating bucket: $result" + return 1 + fi + if ! check_rest_expected_error "$result" "$TEST_FILE_FOLDER/result.txt" "400" "InvalidArgument" ""; then + log 2 "error checking XML CreateBucket error" + return 1 + fi + return 0 +} + +create_bucket_rest_expect_error() { + if ! check_param_count_v2 "bucket name, params, response code, error code, message" 5 $#; then + return 1 + fi + local response + + env_vars="BUCKET_NAME=$1 $2" + if ! response=$(send_rest_command_expect_error "$env_vars" "./tests/rest_scripts/create_bucket.sh" "$3" "$4" "$5" 2>&1); then + log 2 "error sending REST command and checking error: $response" + return 1 + fi + return 0 +} + +create_bucket_rest_expect_success() { + if ! check_param_count_v2 "bucket name, params" 2 $#; then + return 1 + fi + local response + + env_vars="BUCKET_NAME=$1 $2" + if ! response=$(send_rest_command_expect_success "$env_vars" "./tests/rest_scripts/create_bucket.sh" "200" 2>&1); then + log 2 "REST create bucket command error: $response" + return 1 + fi + return 0 +} + +create_bucket_rest() { + if ! check_param_count "create_bucket_rest" "bucket name" 1 $#; then + return 1 + fi + if ! result=$(COMMAND_LOG="$COMMAND_LOG" BUCKET_NAME="$BUCKET_ONE_NAME" OUTPUT_FILE="$TEST_FILE_FOLDER/result.txt" ./tests/rest_scripts/create_bucket.sh 2>&1); then + log 2 "error creating bucket: $result" + return 1 + fi + if [ "$result" != "200" ]; then + bucket_create_error="$(cat "$TEST_FILE_FOLDER/result.txt")" + log 2 "expected '200', was '$result' ($bucket_create_error)" + return 1 + fi + return 0 +} diff --git a/tests/rest_scripts/create_bucket.sh b/tests/rest_scripts/create_bucket.sh new file mode 100644 index 0000000..e154c62 --- /dev/null +++ b/tests/rest_scripts/create_bucket.sh @@ -0,0 +1,72 @@ +#!/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" +acl="$ACL" +# shellcheck disable=SC2153 +object_ownership="$OBJECT_OWNERSHIP" +# shellcheck disable=SC2153 +grant_full_control="$GRANT_FULL_CONTROL" +# shellcheck disable=SC2153 +grant_read="$GRANT_READ" +# shellcheck disable=SC2153 +grant_read_acp="$GRANT_READ_ACP" +# shellcheck disable=SC2153 +grant_write="$GRANT_WRITE" +# shellcheck disable=SC2153 +grant_write_acp="$GRANT_WRITE_ACP" + +current_date_time=$(date -u +"%Y%m%dT%H%M%SZ") + +cr_data=("PUT" "/$bucket_name" "" "host:$host") +if [ "$acl" != "" ]; then + cr_data+=("x-amz-acl:$acl") +fi +cr_data+=("x-amz-content-sha256:UNSIGNED-PAYLOAD" "x-amz-date:$current_date_time") +if [ "$grant_full_control" != "" ]; then + cr_data+=("x-amz-grant-full-control:$grant_full_control") +fi +if [ "$grant_read" != "" ]; then + cr_data+=("x-amz-grant-read:$grant_read") +fi +if [ "$grant_read_acp" != "" ]; then + cr_data+=("x-amz-grant-read-acp:$grant_read_acp") +fi +if [ "$grant_write" != "" ]; then + cr_data+=("x-amz-grant-write:$grant_write") +fi +if [ "$grant_write_acp" != "" ]; then + cr_data+=("x-amz-grant-write-acp:$grant_write_acp") +fi +if [ "$object_ownership" != "" ]; then + cr_data+=("x-amz-object-ownership:$object_ownership") +fi +build_canonical_request "${cr_data[@]}" + +# shellcheck disable=SC2119 +create_canonical_hash_sts_and_signature + +curl_command+=(curl -ks -w "%{http_code}" -X PUT "$AWS_ENDPOINT_URL/$bucket_name") +curl_command+=(-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