How to bind ldap user with RBAC roles in Kong
Binding LDAP users with RBAC roles in Kong requires integration with an LDAP directory for user authentication, followed by mapping those users to Kong’s RBAC roles. Here’s a step-by-step guide to achieving this setup in Kong:
Step 1: Configure Kong for LDAP Authentication
First, enable Kong’s LDAP authentication plugin to allow LDAP users to access Kong services.
- Install the LDAP Authentication Plugin:
- The LDAP plugin is available as part of Kong’s plugin suite. Verify that it’s installed by running:
curl -i -X GET http://<kong-admin-url>:8001/plugins
- Configure the LDAP Plugin:
- You can set up the LDAP authentication plugin on a specific route, service, or globally. Here’s an example of enabling it globally:
curl -i -X POST http://<kong-admin-url>:8001/plugins \
–data “name=ldap-auth” \
–data “config.ldap_host=<ldap-server-ip-or-hostname>” \
–data “config.ldap_port=389” \
–data “config.start_tls=true” \
–data “config.base_dn=dc=example,dc=com” \
–data “config.attribute=username” \
–data “config.cache_ttl=60” \
–data “config.header_type=ldap”
- Replace values such as ldap_host, ldap_port, and base_dn with those specific to your LDAP setup.
- Test LDAP Authentication:
- Ensure that LDAP authentication works by making a request with an LDAP user’s credentials:
curl -i -X GET http://<kong-proxy-url>:8000/your-service \
–header “Authorization: ldap <base64-encoded-credentials>”
Step 2: Create Kong RBAC Roles and Permissions
- Enable RBAC in Kong:
- RBAC is enabled by setting the KONG_ENFORCE_RBAC=on environment variable and restarting Kong.
- Create RBAC Roles:
- Use the Kong Admin API to create roles. For example:
curl -i -X POST http://<kong-admin-url>:8001/rbac/roles \
–data “name=admin”
- Create other roles as needed (e.g., developer, read-only, etc.).
- Assign Permissions to Roles:
- Define permissions for each role to control access to various Kong resources. For example:
curl -i -X POST http://<kong-admin-url>:8001/rbac/roles/admin/endpoints \
–data “endpoint=/services” \
–data “actions=create,read,update,delete”
- Assign permissions according to your access control needs.
Step 3: Bind LDAP Users to RBAC Roles
LDAP users need Kong RBAC tokens to access the Admin API according to their roles. This step involves creating RBAC users and mapping them to LDAP users.
- Create RBAC Users in Kong:
- For each LDAP user, create a corresponding RBAC user in Kong:
curl -i -X POST http://<kong-admin-url>:8001/rbac/users \
–data “name=<ldap-username>” \
–data “user_token=<custom-generated-token>”
- Store the user_token securely, as it serves as the RBAC access token for the user.
- Map RBAC Users to Roles:
- Assign the RBAC user to a role:
curl -i -X POST http://<kong-admin-url>:8001/rbac/users/<ldap-username>/roles \
–data “roles[]=admin”
- Assign roles according to each user’s LDAP role or group to control access.
Step 4: Authenticate LDAP Users with Kong RBAC
Once LDAP users have been mapped to Kong RBAC roles, they can access Kong based on the permissions defined for their roles.
- Access Kong Admin API:
- LDAP users can authenticate to Kong using their RBAC token by including it in the Authorization header:
curl -i -X GET http://<kong-admin-url>:8001/<protected-endpoint> \
–header “Authorization: <user_token>”
- The RBAC token grants access according to the user’s assigned role and permissions.
Additional Considerations
- LDAP Group Mapping: If using groups in LDAP, you could create Kong roles that correspond to LDAP groups. This allows easier role assignment by assigning a Kong RBAC user to a role based on their LDAP group.
- Token Expiration and Rotation: Define an expiration policy for RBAC tokens and ensure tokens are securely managed and rotated if necessary.
- Monitoring and Auditing: Use Kong’s logging features and plugins to monitor access and audit role usage.
By following these steps, you’ll establish a secure, role-based access control system in Kong, integrating LDAP authentication with Kong RBAC.