Enable Key Authentication

TL;DR

Apply the key-auth plugin to a route and attach credentials using the KongConsumer and KongCredentialAPIKey CRDs.

Prerequisites

This page is part of the Get started with Kong Operator and Konnect CRDs series.

Complete the previous page, Enable Proxy Caching before completing this page.

Add authentication to the httpbin service

  1. Create a new key-auth plugin:

    echo '
    kind: KongPlugin
    apiVersion: configuration.konghq.com/v1
    metadata:
      name: key-auth
      namespace: kong
    plugin: key-auth
    ' | kubectl apply -f -
  2. Apply the plugin by annotating the KongService with the names of the plugins you have created:

    kubectl annotate -n kong kongservice service konghq.com/plugins=proxy-cache-all-endpoints,key-auth --overwrite
  3. Test that the API is secure by sending a request using curl -i $PROXY_IP/echo:

    curl -i http://$PROXY_IP/echo 

    This request returns a 401 error with the message No API key found in request.

    You should see the response:

    HTTP/1.1 401 Unauthorized
    Date: Wed, 11 Jan 2044 18:33:46 GMT
    Content-Type: application/json; charset=utf-8
    WWW-Authenticate: Key realm="kong"
    Content-Length: 45
    X-Kong-Response-Latency: 1
    Server: kong/3.16.0.1-enterprise-edition
    
    {
      "message":"No API key found in request",
      "request_id":"89e4883a53a0af51b322204ad1zfa16a"
    }

Set up Consumers and keys

Key authentication in Kong Gateway works by using the Consumer entity. Keys are assigned to Consumers, and client applications present the key within the requests they make.

Keys are stored as Kubernetes Secrets and Consumers are managed with the KongConsumer CRD.

  1. Create a new Secret labeled to use key-auth credential type:

    echo '
    apiVersion: v1
    kind: Secret
    metadata:
       name: alex-key-auth
       namespace: kong
       labels:
          konghq.com/credential: key-auth
          konghq.com/secret: "true"
    stringData:
       key: hello_world
    ' | kubectl apply -f -
  2. Create a new Consumer and attach the credential:

    echo '
    kind: KongConsumer
    apiVersion: configuration.konghq.com/v1
    metadata:
      name: alex
      namespace: kong
    spec:
      controlPlaneRef:
        type: konnectNamespacedRef
        konnectNamespacedRef:
          name: gateway-control-plane
    username: alex
    credentials:
    - alex-key-auth
    ' | kubectl apply -f -
  3. Wait for the Consumer and its credential to be reconciled:

    kubectl wait -n kong kongconsumer/alex --for=condition=Programmed=True --timeout=5m
  4. Make a request to the API and provide your apikey:

    curl "http://$PROXY_IP/echo" \
         --no-progress-meter --fail-with-body  \
         -H "apikey:hello_world"

    The request should be successful.

Cleanup

Deleting the KonnectGatewayControlPlane also deletes the control plane the Kong Operator created in Konnect:

kubectl delete -n kong --ignore-not-found \
  kongconsumer/alex \
  kongplugin/key-auth \
  kongplugin/proxy-cache-all-endpoints \
  kongroute/route \
  kongservice/service \
  dataplane/dataplane \
  konnectextension/my-konnect-config \
  konnectgatewaycontrolplane/gateway-control-plane \
  secret/alex-key-auth \
  secret/konnect-api-auth-secret \
  konnectapiauthconfiguration/konnect-api-auth
helm uninstall kong-operator -n kong-system
kubectl delete namespace kong kong-system

Help us make these docs great!

Kong Developer docs are open source. If you find these useful and want to make them better, contribute today!