|
| 1 | +#!/bin/bash |
| 2 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | +# All rights reserved. |
| 4 | +# |
| 5 | +# This source code is licensed under the terms described in the LICENSE file in |
| 6 | +# the root directory of this source tree. |
| 7 | + |
| 8 | +set -euo pipefail |
| 9 | + |
| 10 | +# Integration auth tests for Llama Stack |
| 11 | +# This script tests authentication and authorization (ABAC) functionality |
| 12 | +# Expects token files to be created before running (e.g., by CI workflow or manual setup) |
| 13 | + |
| 14 | +# Function to test API endpoint with authentication |
| 15 | +# Usage: test_endpoint <curl_args> <user_token_file> <expected_status> [output_file] |
| 16 | +test_endpoint() { |
| 17 | + local curl_args="$1" |
| 18 | + local user_token_file=$2 |
| 19 | + local expected_status=$3 |
| 20 | + local output_file=${4:-/dev/null} |
| 21 | + |
| 22 | + local status |
| 23 | + local extra_curl_args=(-s -L -o "$output_file" -w "%{http_code}") |
| 24 | + |
| 25 | + if [ "$user_token_file" != "none" ]; then |
| 26 | + extra_curl_args+=(-H "Authorization: Bearer $(cat $user_token_file)") |
| 27 | + fi |
| 28 | + |
| 29 | + set -x |
| 30 | + status=$(curl $curl_args "${extra_curl_args[@]}") |
| 31 | + set +x |
| 32 | + |
| 33 | + if [ "$status" = "$expected_status" ]; then |
| 34 | + echo " ✓ Status: $status (expected $expected_status)" |
| 35 | + return 0 |
| 36 | + else |
| 37 | + echo " ✗ Status: $status (expected $expected_status)" |
| 38 | + exit 1 |
| 39 | + fi |
| 40 | +} |
| 41 | + |
| 42 | +# Check if user tokens exist for ABAC testing |
| 43 | +if [ ! -f "llama-stack-auth-token" ] || [ ! -f "llama-stack-user1-token" ] || [ ! -f "llama-stack-user2-token" ]; then |
| 44 | + echo "" |
| 45 | + echo "❌ User tokens not found - expected llama-stack-user1-token and llama-stack-user2-token" |
| 46 | + exit 1 |
| 47 | +fi |
| 48 | + |
| 49 | +echo "Testing /v1/version without token (should succeed)..." |
| 50 | +test_endpoint "http://127.0.0.1:8321/v1/version" "none" "200" || exit 1 |
| 51 | + |
| 52 | +echo "Testing /v1/providers without token (should fail with 401)..." |
| 53 | +test_endpoint "http://127.0.0.1:8321/v1/providers" "none" "401" || exit 1 |
| 54 | + |
| 55 | +echo "Testing /v1/providers with valid token (should succeed)..." |
| 56 | +test_endpoint "http://127.0.0.1:8321/v1/providers" "llama-stack-auth-token" "200" "providers.json" || exit 1 |
| 57 | +cat providers.json | jq . > /dev/null && echo " ✓ Valid JSON response" |
| 58 | + |
| 59 | +echo "" |
| 60 | +echo "Running ABAC tests with user tokens..." |
| 61 | + |
| 62 | +# Create test file |
| 63 | +echo "test content" > test-file.txt |
| 64 | + |
| 65 | +echo "Both user1 and user2 can create files..." |
| 66 | +test_endpoint "http://127.0.0.1:8321/v1/files -F file=@test-file.txt -F purpose=assistants" "llama-stack-user1-token" "200" "user1-files.json" || exit 1 |
| 67 | +test_endpoint "http://127.0.0.1:8321/v1/files -F file=@test-file.txt -F purpose=assistants" "llama-stack-user2-token" "200" "user2-files.json" || exit 1 |
| 68 | + |
| 69 | +echo "user1 can only read their own files..." |
| 70 | +test_endpoint "http://127.0.0.1:8321/v1/files" "llama-stack-user1-token" "200" "user1-files-list.json" || exit 1 |
| 71 | +USER1_FILE_COUNT=$(jq '.data|length' user1-files-list.json) |
| 72 | +echo "User1 has $USER1_FILE_COUNT file(s)" |
| 73 | +[ $USER1_FILE_COUNT -eq 1 ] || ( echo " ✗ User1 should have 1 file, but has $USER1_FILE_COUNT" && exit 1 ) |
| 74 | +echo " ✓ User1 can see exactly 1 file" |
| 75 | + |
| 76 | +echo "user2 can read their own file..." |
| 77 | +test_endpoint "http://127.0.0.1:8321/v1/files" "llama-stack-user2-token" "200" "user2-files-list.json" || exit 1 |
| 78 | +USER2_FILE_COUNT=$(jq '.data|length' user2-files-list.json) |
| 79 | +echo "User2 has $USER2_FILE_COUNT file(s)" |
| 80 | +[ $USER2_FILE_COUNT -eq 1 ] || ( echo " ✗ User2 should have 1 file, but has $USER2_FILE_COUNT" && exit 1 ) |
| 81 | +echo " ✓ User2 can see their own file" |
| 82 | + |
| 83 | +echo "Both file ids should differ" |
| 84 | +FILEID_USER1=$(jq -r '.data[0].id' user1-files-list.json) |
| 85 | +FILEID_USER2=$(jq -r '.data[0].id' user2-files-list.json) |
| 86 | +[ "$FILEID_USER1" != "$FILEID_USER2" ] || ( echo " ✗ File IDs should differ" && exit 1 ) |
| 87 | +echo " ✓ File IDs differ" |
| 88 | + |
| 89 | +echo "user2 can't delete their own file or other users' files..." |
| 90 | +test_endpoint "http://127.0.0.1:8321/v1/files/$FILEID_USER2 -X DELETE" "llama-stack-user2-token" "404" || exit 1 |
| 91 | +test_endpoint "http://127.0.0.1:8321/v1/files/$FILEID_USER1 -X DELETE" "llama-stack-user2-token" "404" || exit 1 |
| 92 | +echo " ✓ Delete correctly blocked" |
| 93 | + |
| 94 | +echo "user1 can delete their own files but not other users' files..." |
| 95 | +test_endpoint "http://127.0.0.1:8321/v1/files/$FILEID_USER1 -X DELETE" "llama-stack-user1-token" "200" || exit 1 |
| 96 | +echo " ✓ Delete successful" |
| 97 | +test_endpoint "http://127.0.0.1:8321/v1/files/$FILEID_USER2 -X DELETE" "llama-stack-user1-token" "404" || exit 1 |
| 98 | +echo " ✓ Delete correctly blocked" |
| 99 | + |
| 100 | +echo "" |
| 101 | +echo "✓ ABAC test completed successfully!" |
0 commit comments