Create a test script to verify the full authentication flow. The script uses the OpenAI Python SDK, pointing at your Kong Gateway Route with the Consumer’s Key Auth credential as the API key.
cat <<EOF > test_openai.py
from openai import OpenAI
kong_url = "http://localhost:8000"
kong_route = "anything"
client = OpenAI(
api_key="my-consumer-key",
base_url=f"{kong_url}/{kong_route}"
)
response = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": "Say hello."}]
)
print(response.choices[0].message.content)
EOF
cat <<EOF > test_openai.py
from openai import OpenAI
import os
kong_url = os.environ['KONNECT_PROXY_URL']
kong_route = "anything"
client = OpenAI(
api_key="my-consumer-key",
base_url=f"{kong_url}/{kong_route}"
)
response = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": "Say hello."}]
)
print(response.choices[0].message.content)
EOF
Run the script:
If authentication is configured correctly, you’ll see the model’s response printed to the terminal.
To confirm that Key Auth is actually enforcing access, create a second script with an invalid key:
cat <<EOF > test_openai_wrong_key.py
from openai import OpenAI
kong_url = "http://localhost:8000"
kong_route = "anything"
client = OpenAI(
api_key="wrong-key",
base_url=f"{kong_url}/{kong_route}"
)
try:
response = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": "Say hello."}]
)
print(response.choices[0].message.content)
except Exception as e:
print(f"Expected error: {e}")
EOF
cat <<EOF > test_openai_wrong_key.py
from openai import OpenAI
import os
kong_url = os.environ['KONNECT_PROXY_URL']
kong_route = "anything"
client = OpenAI(
api_key="wrong-key",
base_url=f"{kong_url}/{kong_route}"
)
try:
response = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": "Say hello."}]
)
print(response.choices[0].message.content)
except Exception as e:
print(f"Expected error: {e}")
EOF
Run the script:
python test_openai_wrong_key.py
This should return a 401 Unauthorized error, confirming that Kong rejects requests with invalid credentials.