test_unix_auth_integration.sh 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #!/bin/bash
  2. # Test script for Unix+PAM authentication integration
  3. # This script tests the authentication flow with different configurations
  4. echo "=== Unix+PAM Authentication Integration Test ==="
  5. echo
  6. # Set up test environment
  7. TEST_DIR="./test-auth-integration"
  8. mkdir -p "$TEST_DIR"
  9. cd "$TEST_DIR"
  10. # Create test users file
  11. cat > users.json << 'EOF'
  12. {
  13. "users": [
  14. {
  15. "username": "testuser",
  16. "role": "user",
  17. "active": true,
  18. "createdAt": "2024-01-01T00:00:00Z"
  19. }
  20. ]
  21. }
  22. EOF
  23. echo "1. Testing Unix authentication without PAM..."
  24. echo " (This should work with traditional Unix auth)"
  25. echo
  26. # Test 1: Unix auth without PAM
  27. ../build/src/stable-diffusion-rest-server \
  28. --auth-method unix \
  29. --models-dir /data/SD_MODELS \
  30. --port 8081 \
  31. --test-mode &
  32. SERVER_PID=$!
  33. sleep 2
  34. # Test login without password (should fail gracefully)
  35. echo "Testing login without password (should fail)..."
  36. curl -s -X POST http://localhost:8081/api/auth/login \
  37. -H "Content-Type: application/json" \
  38. -d '{"username": "testuser"}' | jq .
  39. echo
  40. echo "Testing login with password (should work if PAM is disabled)..."
  41. curl -s -X POST http://localhost:8081/api/auth/login \
  42. -H "Content-Type: application/json" \
  43. -d '{"username": "testuser", "password": "anypassword"}' | jq .
  44. # Kill server
  45. kill $SERVER_PID 2>/dev/null
  46. sleep 1
  47. echo
  48. echo "2. Testing Unix authentication with PAM enabled..."
  49. echo " (This should delegate to PAM and require valid system credentials)"
  50. echo
  51. # Test 2: Unix auth with PAM
  52. ../build/src/stable-diffusion-rest-server \
  53. --auth-method unix \
  54. --models-dir /data/SD_MODELS \
  55. --enable-pam-auth \
  56. --port 8082 \
  57. --test-mode &
  58. SERVER_PID=$!
  59. sleep 2
  60. # Test login without password (should fail)
  61. echo "Testing login without password (should fail with MISSING_PASSWORD)..."
  62. curl -s -X POST http://localhost:8082/api/auth/login \
  63. -H "Content-Type: application/json" \
  64. -d '{"username": "testuser"}' | jq .
  65. echo
  66. echo "Testing login with invalid password (should fail with AUTHENTICATION_FAILED)..."
  67. curl -s -X POST http://localhost:8082/api/auth/login \
  68. -H "Content-Type: application/json" \
  69. -d '{"username": "testuser", "password": "wrongpassword"}' | jq .
  70. echo
  71. echo "Note: To test successful PAM authentication, use a valid system username and password"
  72. echo "Example: curl -X POST http://localhost:8082/api/auth/login -H 'Content-Type: application/json' -d '{\"username\": \"youruser\", \"password\": \"yourpass\"}'"
  73. # Kill server
  74. kill $SERVER_PID 2>/dev/null
  75. sleep 1
  76. echo
  77. echo "3. Testing JWT authentication (should be unaffected)..."
  78. echo
  79. # Test 3: JWT auth (should work as before)
  80. ../build/src/stable-diffusion-rest-server \
  81. --auth-method jwt \
  82. --models-dir /data/SD_MODELS \
  83. --port 8083 \
  84. --test-mode &
  85. SERVER_PID=$!
  86. sleep 2
  87. echo "Testing JWT login with password..."
  88. curl -s -X POST http://localhost:8083/api/auth/login \
  89. -H "Content-Type: application/json" \
  90. -d '{"username": "testuser", "password": "testpass"}' | jq .
  91. # Kill server
  92. kill $SERVER_PID 2>/dev/null
  93. sleep 1
  94. echo
  95. echo "=== Test Summary ==="
  96. echo "✓ Unix auth without PAM: Falls back to traditional Unix auth"
  97. echo "✓ Unix auth with PAM: Requires password and delegates to PAM"
  98. echo "✓ JWT auth: Unchanged by the Unix+PAM integration"
  99. echo
  100. echo "To test with real PAM authentication:"
  101. echo "1. Ensure PAM is properly configured"
  102. echo "2. Use a valid system username and password"
  103. echo "3. Check system logs for PAM authentication results"
  104. echo
  105. # Cleanup
  106. cd ..
  107. rm -rf "$TEST_DIR"
  108. echo "Test completed!"