How to Build a Custom Kubernetes Authentication Webhook with GitHub and LDAP
This article explains how to create a custom Kubernetes authentication webhook in Go that supports GitHub token and LDAP authentication, details the required API Server configuration, provides full code examples, and demonstrates testing both methods, illustrating a practical way to integrate external account systems with Kubernetes.
Authentication Overview
In Kubernetes the API server is the central component. Every request passes through three stages: Authentication, Authorization, and AdmissionControl. This article focuses on the Authentication stage.
Authentication Plugins
Kubernetes supports many authentication plugins such as X509 certificates, static tokens, ServiceAccount, OpenID, and Webhook. The article demonstrates the use of a Webhook to delegate authentication to external services.
Developing a Webhook Service
The example is written in Go (1.17.3) and runs against Kubernetes v1.22.3 on CentOS 7.6.
Webhook Specification
The webhook must expose an HTTPS POST endpoint that receives a
TokenReviewobject and returns a
TokenReviewwith the authentication result.
<code>{
"apiVersion": "authentication.k8s.io/v1beta1",
"kind": "TokenReview",
"spec": { "token": "<持有者令牌>" }
}</code>If authentication succeeds, the API server expects a response like:
<code>{
"apiVersion": "authentication.k8s.io/v1beta1",
"kind": "TokenReview",
"status": {
"authenticated": true,
"user": {
"username": "[email protected]",
"uid": "42",
"groups": ["developers", "qa"],
"extra": { "extrafield1": ["extravalue1", "extravalue2"] }
}
}
}</code>If authentication fails:
<code>{
"apiVersion": "authentication.k8s.io/v1beta1",
"kind": "TokenReview",
"status": { "authenticated": false }
}</code>Project Structure
Create the project directory and initialize
go.mod.
webhook.goimplements the HTTP handler that parses the
TokenReview, extracts the token type, and calls the appropriate authentication function.
github.govalidates a GitHub token by calling the GitHub API.
ldap.goauthenticates against an OpenLDAP server and returns the groups of the user.
main.gostarts the HTTP server on a configurable port.
Token Formats
GitHub:
github:<token>LDAP:
ldap:<username>:<password>Deploying the Webhook
Create a kubeconfig‑style JSON file (webhook-config.json) that points to the webhook service and add the flag
--authentication-token-webhook-config-fileto the kube‑apiserver manifest.
<code># mkdir /etc/kubernetes/webhook
# cat >> webhook-config.json <<EOF
{
"kind": "Config",
"apiVersion": "v1",
"clusters": [
{
"name": "github-authn",
"cluster": { "server": "http://10.0.4.9:9999/auth" }
}
],
"users": [
{
"name": "authn-apiserver",
"user": { "token": "secret" }
}
],
"contexts": [
{
"name": "webhook",
"context": { "cluster": "github-authn", "user": "authn-apiserver" }
}
],
"current-context": "webhook"
}
EOF</code>Mount the file into the apiserver pod (e.g., via a hostPath volume) and restart the component.
Testing GitHub Authentication
Generate a personal access token on GitHub (see image below), add it to
~/.kube/configas
token: github:…, and run
kubectl get po --user=joker. The webhook logs show
auth by github success.
Testing LDAP Authentication
Install OpenLDAP, create a base DN, add a user and a group, then configure the token
ldap:jack:123456in the kubeconfig. After running
kubectl get po --user=jackthe webhook logs show
auth by ldap success.
Conclusion
Using a webhook makes it easy to integrate Kubernetes authentication with existing enterprise account systems, but the example is simplistic; for production use a more robust solution such as Dex.
Ops Development Stories
Maintained by a like‑minded team, covering both operations and development. Topics span Linux ops, DevOps toolchain, Kubernetes containerization, monitoring, log collection, network security, and Python or Go development. Team members: Qiao Ke, wanger, Dong Ge, Su Xin, Hua Zai, Zheng Ge, Teacher Xia.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.