From 6d6082cf004dae1a44df01b58058addb76cc5a47 Mon Sep 17 00:00:00 2001 From: Adam Wilson Date: Wed, 28 May 2025 05:57:23 -0600 Subject: [PATCH] fix garak config paths --- .github/scripts/test_api.sh | 49 +++++++++++++++---- .github/workflows/llmsecops-cicd.yml | 41 ++-------------- run.sh | 3 +- .../entrypoints/http_api_controller.py | 10 ++-- 4 files changed, 52 insertions(+), 51 deletions(-) diff --git a/.github/scripts/test_api.sh b/.github/scripts/test_api.sh index 84a2ebe76..2a023d7cf 100755 --- a/.github/scripts/test_api.sh +++ b/.github/scripts/test_api.sh @@ -1,18 +1,47 @@ #!/bin/bash +# Local-only usage: ./test_api.sh --local + set -e # Exit on error -cd $GITHUB_WORKSPACE +# Parse command line arguments +LOCAL=false + +while [[ $# -gt 0 ]]; do + case $1 in + --local) + LOCAL=true + shift + ;; + *) + echo "Unknown option: $1" + exit 1 + ;; + esac +done + +if [ "$LOCAL" = false ]; then + cd $GITHUB_WORKSPACE +fi echo "Making API request..." -curl -X POST -i http://localhost:9999/api/conversations \ - -d '{ "prompt": "describe a random planet in our solar system in 10 words or less" }' \ - -H "Content-Type: application/json" > logs/test_request.log 2>&1 -if [ $? -ne 0 ]; then - echo "Test API request failed" - cat logs/test_request.log +# Wait for server to start and verify it's running +max_retries=30 +retry_count=0 +server_ready=false + +while [ $retry_count -lt $max_retries ] && [ "$server_ready" = false ]; do + echo "Waiting for server to start (attempt $retry_count/$max_retries)..." + if curl -s -o /dev/null -w "%{http_code}" localhost:9999 > /dev/null 2>&1; then + server_ready=true + echo "Server is running" + else + sleep 2 + retry_count=$((retry_count + 1)) + fi +done + +if [ "$server_ready" = false ]; then + echo "::error::Server failed to start after $max_retries attempts" exit 1 -else - echo "Test API request succeeded" - cat logs/test_request.log fi \ No newline at end of file diff --git a/.github/workflows/llmsecops-cicd.yml b/.github/workflows/llmsecops-cicd.yml index b0ca6835b..4bc6a0f05 100644 --- a/.github/workflows/llmsecops-cicd.yml +++ b/.github/workflows/llmsecops-cicd.yml @@ -15,49 +15,18 @@ jobs: with: python-version: '3.12' - - name: 'set up Garak' - run: | - pip install garak - continue-on-error: false - - - name: 'start HTTP server' + - name: 'start and test HTTP server' id: start_server run: | nohup ./run.sh > server.log 2>&1 & server_pid=$! - # echo "Server PID: $server_pid" - # echo "server_pid=$server_pid" >> $GITHUB_ENV + echo "Server PID: $server_pid" + echo "server_pid=$server_pid" >> $GITHUB_ENV + ${{ github.workspace }}/.github/scripts/test_api.sh - # # Wait for server to start and verify it's running - # max_retries=30 - # retry_count=0 - # server_ready=false - - # while [ $retry_count -lt $max_retries ] && [ "$server_ready" = false ]; do - # echo "Waiting for server to start (attempt $retry_count/$max_retries)..." - # if curl -s -o /dev/null -w "%{http_code}" localhost:9999 > /dev/null 2>&1; then - # server_ready=true - # echo "Server is running" - # else - # sleep 2 - # retry_count=$((retry_count + 1)) - # fi - # done - - # if [ "$server_ready" = false ]; then - # echo "::error::Server failed to start after $max_retries attempts" - # echo "=== Server Log (last 50 lines) ===" - # tail -n 50 server.log || true - # exit 1 - # fi - - - name: 'Test server with curl and run garak' + - name: 'run garak tests' id: run_tests run: | - # Test curl with detailed error reporting - curl_output=$(curl -X POST -i localhost:9999/api/conversations -d '{ "prompt": "describe a random planet in our solar system in 10 words or less" }' --connect-timeout 10 -v 2>&1) || true - echo "$curl_output" - garak -v \ --config ${{ github.workspace }}/tests/security/garak.config.yml \ --generator_option_file ${{ github.workspace }}/tests/security/garak.rest.llm-rag.json \ diff --git a/run.sh b/run.sh index e4b0d9030..ebbefcb20 100755 --- a/run.sh +++ b/run.sh @@ -1,6 +1,5 @@ #!/usr/bin/bash - -# Local-only usage: ./script.sh --local +# Local-only usage: ./run.sh --local # Parse command line arguments LOCAL=false diff --git a/src/text_generation/entrypoints/http_api_controller.py b/src/text_generation/entrypoints/http_api_controller.py index c6475b6f1..420255a77 100644 --- a/src/text_generation/entrypoints/http_api_controller.py +++ b/src/text_generation/entrypoints/http_api_controller.py @@ -14,6 +14,7 @@ class HttpApiController: def register_routes(self): """Register all API routes""" + self.routes[('GET', '/')] = self.health_check self.routes[('POST', '/api/conversations')] = self.handle_conversations self.routes[('POST', '/api/rag_conversations')] = self.handle_conversations_with_rag @@ -40,6 +41,12 @@ class HttpApiController: response_body = json.dumps({'response': str(data)}).encode('utf-8') return response_body + def health_check(self, env, start_response): + response_body = self.format_response({ "success": True }) + response_headers = [('Content-Type', 'application/json'), ('Content-Length', str(len(response_body)))] + start_response('200 OK', response_headers) + return [response_body] + def handle_conversations(self, env, start_response): """Handle POST requests to /api/conversations""" try: @@ -110,9 +117,6 @@ class HttpApiController: method = env.get('REQUEST_METHOD').upper() path = env.get('PATH_INFO') - if method != 'POST': - return self.__http_415_notsupported(env, start_response) - try: handler = self.routes.get((method, path), self.__http_200_ok) return handler(env, start_response)