How to make impersonate work with kubernetes go-client
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Kubernetes impersonation with the Go client is useful for admin tools, policy simulation, and delegated operations. It lets an authenticated caller request actions as another user or group, but only when RBAC explicitly grants impersonation rights. To make it work reliably, configure impersonation in rest.Config, grant minimal RBAC verbs, and verify behavior with authorization checks.
How Impersonation Works
Impersonation does not replace authentication. The real caller still authenticates first, then sends impersonation headers such as user and groups. The API server allows this only if caller permissions include impersonate on relevant resources.
In practical terms:
- Real identity authenticates.
- Request includes impersonation fields.
- API server validates impersonation permission.
- Authorization evaluates the impersonated identity.
A failure can happen in step three or step four, so debugging should test both.
Configure client-go Impersonation Fields
Set impersonation once on rest.Config before creating clientset.
If this returns forbidden, inspect RBAC for both impersonation and target resource access.
RBAC Needed for Impersonation
The caller must be allowed to impersonate users or groups.
Grant only what is required. Broad impersonation scope is a high-risk permission.
Verify with kubectl auth can-i
Quick checks isolate where failure occurs.
First command tests whether real caller can impersonate. Second tests whether impersonated identity can access the target resource.
Safer Pattern for Multi-Tenant Admin Tools
In internal platforms, do not allow arbitrary impersonation from request parameters. Enforce allow lists and policy checks before setting cfg.Impersonate fields.
Also log who requested impersonation and why. Auditability matters in security reviews.
Common Failure Modes
Typical reasons impersonation appears broken:
- impersonation role missing.
- wrong cluster context in kubeconfig.
- attempting to impersonate a group not covered by policy.
- target user lacks permission even though impersonation itself is allowed.
Treat these as separate checks, not one combined guess.
Impersonate Extra Fields Carefully
Kubernetes also supports extra impersonation attributes through cfg.Impersonate.Extra. Use this only when your auth stack expects those claims, and validate accepted keys with platform security owners.
Extra fields are powerful but can create policy confusion if different services interpret claims differently. Keep usage narrowly documented and audited.
Common Pitfalls
- Setting impersonation fields but forgetting
impersonateRBAC permissions. - Assuming cluster-admin authentication automatically grants impersonation.
- Debugging only target resource permissions and skipping impersonation permission checks.
- Allowing free-form impersonation targets in multi-tenant tooling.
- Running impersonated operations without security audit logging.
Summary
- Configure impersonation in
rest.Configbefore building Kubernetes clients. - Grant explicit
impersonateRBAC verbs to the real caller identity. - Validate both impersonation rights and target resource rights.
- Restrict impersonation scope in admin tooling.
- Keep detailed audit logs for all impersonated operations.
Related reading
- How to make k8s cpu and memory HPA work together?
- How to make microk8s ctr image prune
- How to make nested variables optional in Helm
- How to make use of Kubernetes port names?
- How to make my Java Swing application a Client-Server application?
- How to make nodes wait till the topology is defined
- How to mock JWT authentication in a Spring Boot Unit Test?
- How to mount /dev/kvm in a non-privileged pod?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.