curl -i http://$PROXY_IP/echo Enable Key Authentication
Apply the key-auth plugin to a route and attach credentials using the KongConsumer and KongCredentialAPIKey CRDs.
Prerequisites
Series 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
-
Create a new
key-authplugin:echo ' kind: KongPlugin apiVersion: configuration.konghq.com/v1 metadata: name: key-auth namespace: kong plugin: key-auth ' | kubectl apply -f - -
Apply the plugin by annotating the
KongServicewith the names of the plugins you have created:kubectl annotate -n kong kongservice service konghq.com/plugins=proxy-cache-all-endpoints,key-auth --overwrite -
Test that the API is secure by sending a request using
curl -i $PROXY_IP/echo:This request returns a
401error with the messageNo 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.
-
Create a new
Secretlabeled to usekey-authcredential 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 - -
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 - -
Wait for the Consumer and its credential to be reconciled:
kubectl wait -n kong kongconsumer/alex --for=condition=Programmed=True --timeout=5m -
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
Delete the Konnect resources
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-authDelete the namespaces
helm uninstall kong-operator -n kong-system
kubectl delete namespace kong kong-system