test: safety

This commit is contained in:
Luke McCrone
2026-01-28 16:30:27 -03:00
parent a3e3aa330a
commit 4341f6bc3a
3 changed files with 172 additions and 18 deletions

View File

@@ -15,6 +15,7 @@
# under the License.
source ./tests/logger.sh
source ./tests/report.sh
send_command() {
if [ $# -eq 0 ]; then
@@ -27,6 +28,7 @@ send_command() {
fi
# shellcheck disable=SC2154
echo "${masked_args[*]}" >> "$COMMAND_LOG"
record_command_v2 "${masked_args[*]}"
fi
local command_result=0
"$@" || command_result=$?

View File

@@ -125,12 +125,19 @@ get_curl_method() {
echo "$method"
}
get_curl_route() {
local url="$1"
parse_path_and_get_route() {
}
get_route() {
if ! check_param_count_v2 "string" 1 $#; then
return 1
fi
url="$(echo "$1" | grep -oE 'https?://[^" ]+' | head -n 1)"
local path
# Only accept http/https URLs with a path
if [[ ! "$url" =~ ^https?://[^/]+(/.*)?$ ]]; then
if [ -z "$url" ]; then
echo "UNKNOWN"
return 0
fi
@@ -156,43 +163,147 @@ get_curl_route() {
}
get_query() {
if ! check_param_count_v2 "string" 1 $#; then
return 1
fi
url="$(echo "$1" | grep -oE 'https?://[^" ]+' | head -n 1)"
# Must look like a URL
if [ -z "$url" ]; then
echo ""
return 0
fi
# Extract query string (everything after '?')
local query
query="${url#*\?}"
# No query present
if [[ "$query" == "$url" ]]; then
echo ""
return 0
fi
# Remove fragment if present
query="${query%%#*}"
keys=()
while [[ $query ]]; do
key="${query%%=*}" # Extract key
keys+=("$key")
# If no more keys
if [[ "$query" != *"&"* ]]; then
break
fi
query="${query#*&}" # Remove extracted part from query
done
echo "${keys[*]}"
}
parse_curl_rest_command() {
if ! check_param_count "command string" 1 $#; then
if ! check_param_count_v2 "command string" 1 $#; then
return 1
fi
if ! method=$(get_curl_method "$1" 2>&1); then
echo "error retrieving method: $method"
return 1
fi
if ! route=$(get_route "$1" 2>&1); then
echo "error retrieving route: $route"
return 1
fi
if ! query=$(get_query "$1" 2>&1); then
echo "error retrieving query: $query"
return 1
fi
echo "$method $route $query"
return 0
}
parse_rest_command() {
if ! check_param_count "command string" 1 $#; then
get_openssl_method_route_queries() {
if ! check_param_count_v2 "command file" 1 $#; then
return 1
fi
if [[ "$1" == *"curl "* ]]; then
top_line=$(head -n 1 "$1")
method=$(awk 'NR==1{print $1}' "$1")
route=$(get_route "$top_line")
query=$(get_query "$top_line")
echo "$method $route $query"
return 0
}
parse_openssl_command() {
if ! check_param_count_v2 "command file" 1 $#; then
return 1
fi
}
get_client_type() {
if [[ "$1" == *" curl "* ]] || [[ "$1" == "curl "* ]]; then
echo "CURL"
return 0
elif [[ "$1" == *" s3api "* ]] || [[ "$1" == "s3api "* ]]; then
echo "S3API"
return 0
fi
echo "UNKNOWN"
}
parse_command_info() {
if ! check_param_count "command string" 1 $#; then
if ! check_param_count_v2 "command string" 1 $#; then
return 1
fi
if [[ "$1" == *"curl "* ]] || [[ "$1" == *"HTTP"* ]]; then
return 0
if [[ "$1" == *"curl "* ]]; then
if ! command_info=$(parse_curl_rest_command "$1" 2>&1); then
echo "error parsing rest command: $command_info"
return 1
fi
else
command_info="NONE"
fi
}
check_and_create_database_v2() {
# Define SQL commands to create a table
SQL_CREATE_TABLE="CREATE TABLE IF NOT EXISTS entries (
id INTEGER PRIMARY KEY AUTOINCREMENT,
command TEXT UNIQUE NOT NULL,
count INTEGER DEFAULT 1
);"
# Execute the SQL commands to create the database and table
sqlite3 "coverage.db" <<EOF
$SQL_CREATE_TABLE
.exit
EOF
}
record_command_v2() {
if [ -z "$COVERAGE_DB" ]; then
log 5 "no coverage db set, not recording"
#if [ -z "$COVERAGE_DB" ]; then
# log 5 "no coverage db set, not recording"
# return 0
#fi
if ! check_param_count_v2 "command string" 1 $#; then
return 1
fi
if ! db_result=$(check_and_create_database_v2 2>&1); then
log 2 "error creating database: $db_result"
return 1
fi
if ! parse_command_info "$1"; then
return 1
fi
if [ "$command_info" == "NONE" ]; then
return 0
fi
if ! check_param_count "command string" 1 $#; then
return 1
fi
if ! result=$(check_and_create_database 2>&1); then
log 2 "error creating database: $result"
return 1
echo "$command_info" >> "commandInfo.txt"
cat "commandInfo.txt" | sort | uniq > "commandInfo.txt.tmp"
mv "commandInfo.txt.tmp" "commandInfo.txt"
if ! error=$(sqlite3 "coverage.db" "INSERT INTO entries (command, count) VALUES(\"$command_info\", 1) ON CONFLICT(command) DO UPDATE SET count = count + 1" 2>&1); then
log 2 "error in sqlite statement: $error"
fi
}

View File

@@ -45,4 +45,45 @@ source ./tests/report.sh
@test "reporting - get query" {
tests=("https://localhost:7070/?query1=" "https://localhost/bucket?another=" "https://1.2.3.4/" "http://localhost/bucket/file?third")
expected_results=("query1" "another" "" "third")
for ((i=0; i<${#tests[@]}; i++)); do
echo "test: ${tests[$i]}, expected result: ${expected_results[$i]}"
run get_query "${tests[$i]}"
assert_output "${expected_results[$i]}"
done
}
@test "reporting - get client type" {
tests=("curl -iks https://localhost:7070/versity-gwtest-bucket-one-1-20260127113351?location= -H Authorization: AWS4-HMAC-SHA256 Credential=AKIA6****/20260127/us-east-1/s3/aws4_request,SignedHeaders=host;x-amz-content-sha256;x-amz-date,Signature=68c0b96180a5791be8a10335c10d302d31d358c4bc6028aec94faf502f3a185e -H host: localhost:7070 -H x-amz-content-sha256: e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 -H x-amz-date: 20260127T143355Z" \
"aws --no-verify-ssl s3api create-bucket --bucket versity-gwtest-bucket-one-1-20260127113351 --object-lock-enabled-for-bucket" "")
expected_results=("CURL" "S3API" "UNKNOWN")
for ((i=0; i<${#tests[@]}; i++)); do
run get_client_type "${tests[$i]}"
assert_output "${expected_results[$i]}"
done
}
@test "reporting - parse curl rest command" {
tests=("curl -iks https://localhost:7070/versity-gwtest-bucket-one-1-20260127113351?location= -H Authorization: AWS4-HMAC-SHA256 Credential=AKIA6****/20260127/us-east-1/s3/aws4_request,SignedHeaders=host;x-amz-content-sha256;x-amz-date,Signature=68c0b96180a5791be8a10335c10d302d31d358c4bc6028aec94faf502f3a185e -H host: localhost:7070 -H x-amz-content-sha256: e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 -H x-amz-date: 20260127T143355Z")
expected_command=("GET BUCKET location")
for ((i=0; i<${#tests[@]}; i++)); do
run parse_curl_rest_command "${tests[$i]}"
assert_output "${expected_command[$i]}"
done
}
@test "openssl - get method, route, and queries" {
tests=("GET / HTTP/1.1
Authorization: AWS4-HMAC-SHA256 Credential=AKIAQJVWFRZQNI6LF3W7/20250911/us-east-1/s3/aws4_request,SignedHeaders=x-amz-content-sha256;x-amz-date,Signature=86ffbe2317caddcac569b25aa9b8e8db4a613a639b2a402cf4a9dc0e975ba997
x-amz-content-sha256:UNSIGNED-PAYLOAD")
expected_output=("GET MAIN ")
for ((i=0; i<${#tests[@]}; i++)); do
file_name="$TMPDIR/openssl-$(uuidgen)"
echo "${tests[$i]}" > "$file_name"
run get_openssl_method_route_queries "$file_name"
assert_output "${expected_output[$i]}"
done
}