LDAP
Authenticate requests against membership information that is stored in a Lightweight Directory Access Protocol (LDAP) server.
If you import or export resources across workspaces, your policies might not apply. For more information, see Import and export policies.
About LDAP
The Lightweight Directory Access Protocol (LDAP) is an open protocol that you can use to store and retrieve hierarchically structured data over a network. Many enterprises use LDAP to centrally store and secure organizational information. In particular, LDAP is often used for membership directories. You might set up LDAP to store information such as the following:
- User details like name and email
- Group membership details for each user
- Permissions for each group
You can deploy an LDAP server to your Kubernetes cluster. Then, use a Gloo Mesh Enterprise external auth policy to authenticate users and control access based on their group membership details in the LDAP server.
Want to learn more about LDAP? Try out this LDAP tutorial by Digital Ocean.
LDAP in Gloo Mesh Enterprise
Upon receiving an authentication request that uses LDAP, Gloo Mesh Enterprise performs the following steps.
Gloo Mesh Enterprise looks for a Basic Authentication header on the request to extract the username (
uid
) and credentials (userPassword
).If the header is not present, a
401
response is returned.If the header is present, Gloo Mesh Enterprise tries to perform a BIND operation in one of the following ways:
User binding: Gloo Mesh Enterprise extracts the username from the basic auth header. Then, it builds the distinguished name (DN) for the user entry by substituting the username from the header for the
%s
placeholder in the LDAPuserDnTemplate
setting.To prevent injection attacks, special characters are removed from the username before continuing to the next bind step.Service account binding: Instead of giving each user access to the group membership information, you can use an LDAP service account to look up this information on behalf of the user. To authenticate with the LDAP server, you must store the LDAP service account credentials in a Kubernetes secret in your cluster. Then, you reference that secret in your AuthConfig resource. Note that you can only verify the user’s group membership in the LDAP server with the service account.
If the
BIND
operation fails, a401
response is returned. This response means that the user could not be found or the credentials are incorrect.Gloo Mesh Enterprise issues a search operation for the user entry (with a
base
scope). In the user entry, Gloo Mesh Enterprise looks for an attribute with a name equal tomembershipAttributeName
.Gloo Mesh Enterprise checks if one of the values for the attribute matches one of the
allowedGroups
in the policy. If so, Gloo Mesh Enterprise completes the request. Otherwise, Gloo Mesh Enterprise returns a403
response. This response means that although the user can be authenticated, the user does not have the appropriate permissions to complete the request.
Before you begin
This guide assumes that you use the same names for components like clusters, workspaces, and namespaces as in the getting started. If you have different names, make sure to update the sample configuration files in this guide.
Complete the multicluster getting started guide to set up the following testing environment.
- Three clusters along with environment variables for the clusters and their Kubernetes contexts.
- The Gloo
meshctl
CLI, along with other CLI tools such askubectl
andistioctl
. - The Gloo management server in the management cluster, and the Gloo agents in the workload clusters.
- Istio installed in the workload clusters.
- A simple Gloo workspace setup.
- Install Bookinfo and other sample apps.
Make sure that the external auth service is installed and running. If not, install the external auth service.
Make sure that you have the
ldapsearch
CLI tool on your local machine. This tool is part of the default developer tools in macOS. You can check if you have the tool by running the following command to print the usage details. To install LDAP and Helper utilities, see this Digital Ocean guide.
Deploy an LDAP server
Before you can create an LDAP external auth policy, you must have an LDAP server. The following example configures a simple set of users and groups, and deploys the LDAP server to your cluster.
Download the sample LDAP setup script.
Review what the sample LDAP setup script does. For in-depth information, see About the LDAP setup script. In short, the script sets up LDAP users and groups, as well as creates the following Kubernetes resources:
- A
configmap
with the LDAP server setup configuration. - A
deployment
to run OpenLDAP. - A
service
to provide access to the deployment.
- A
Make the downloaded script executable.
Set your Kubernetes context to the cluster that you want to deploy the LDAP server to and create the external auth policy in.
Run the LDAP setup script. The script accepts an optional string argument to specify the namespace to create the resources in. Otherwise, you can omit the argument to create the resources in the
default
namespace.Example output:
Enable port-forwarding on the deployment so that you can test the LDAP server.
In a new tab in your terminal, search for the distinguished names (DNs) of all entries in the
solo
andio
domain components (DCs). For more information about this command, see the LDAP docs.Example output:
Good job, now you have an LDAP server running! Continue to Configure an external auth policy with LDAP.
About the LDAP setup script
The LDAP setup script sets up a basic LDAP server with a few different users and groups. This setup includes the Kubernetes resources to create in the cluster.
About the LDAP users and groups
The root of the LDAP directory hierarchy is the dc=solo,dc=io
entry, which has two child entries for users and groups.
Summary of LDAP credentials
The user credentials and memberships are summarized in the following table.
Username | Password | Member of developers | Member of sales | Member of managers |
---|---|---|---|---|
marco | marcopwd | ✅ | ❌ | ❌ |
rick | rickpwd | ✅ | ❌ | ✅ |
scott | scottpwd | ✅ | ✅ | ❌ |
Configure an external auth policy with LDAP
Create the external auth policy with LDAP.
You can do the following steps in a different order, depending on when you want the policy to take effect. For example, you might want the policy to always take effect as soon as the route is created. To do so, you can create the policy before you add the route to the route table.
Create an external auth server to use for your policy. The following example refers directly to the default Gloo external auth service, but you can also use a virtual destination instead. For more information, see External auth server setup.
Create an external auth policy that uses the LDAP server.
This policy currently does not support selecting VirtualDestinations as a destination.Review the following table to understand this configuration. For more information, see the API reference.
Setting Description applyToDestinations
Use labels to apply the policy to destinations. Destinations might be a Kubernetes service, VirtualDestination, or ExternalService (if supported by the policy). If you do not specify any destinations or routes, the policy applies to all destinations in the workspace by default. If you do not specify any destinations but you do specify a route, the policy applies to the route but to no destinations. server
The external auth server to use for the policy. ldap
Configure the LDAP server details. address
The address of the LDAP server that Gloo queries when a request matches the policy. This example uses the Kubernetes DNS name and port of the LDAP service that you deployed in the default
namespace.userDnTemplate
The template string for Gloo to build the DNs of the user entry to authenticate and authorize. The string must have a single occurrence of the %s
placeholder. This placeholder is used to substitute the value from the request header that you want to look for, such as the user ID (uid
) in this example. In this example, the template matches the format of the user entry DNs in the config map.membershipAttributeName
A case-insensitive name of the attribute with the names of the groups that a user entry is a member of. If not set, the default value memberOf
is used. In the example, the config map setsmemberOf
automatically for each user entry that is in a group.allowedGroups
The DNs of the user groups that are allowed to access services that are protected by this policy. In this example, only members of "cn=managers,ou=groups,dc=solo,dc=io"
group can get successful responses.
Verify the external auth LDAP policy
To test the LDAP policy, make a series of requests as different users. The following table is based off the users that you created in your LDAP config map. The username and password are encoded to base 64 in the format username:password
so that you can pass them in a basic auth header.
Username | Password | Basic auth header | Notes |
---|---|---|---|
marco | marcopwd | Authorization: Basic bWFyY286bWFyY29wd2Q= | Member of developers group |
rick | rickpwd | Authorization: Basic cmljazpyaWNrcHdk | Member of developers and managers groups |
john | doe | Authorization: Basic am9objpkb2U= | Unknown user, not a member of any group |
Send an unauthenticated request to the app. Now, the request is blocked with a
401 Unauthorized
response. Create a temporary curl pod in thebookinfo
namespace, so that you can test the app setup. You can also use this method in Kubernetes 1.23 or later, but an ephemeral container might be simpler.- Create the curl pod.
- Send a request to the reviews app.
- Create the curl pod.
Repeat the previous step with the credentials of the unknown member,
john
. The request is blocked with the same401 Unauthorized
response.Try a request with the credentials of a known member,
marco
. This user is a member of thedevelopers
group, but the LDAP policy only grants permission to members of themanagers
group. The request is blocked, but this time with a403 Forbidden
response to indicate the lack of permissions.Finally, send a request with the credentials of
rick
, who is a member of themanagers
group and meets the LDAP policy requirements.- From the curl pod, send a request to the ratings app.
- Exit the temporary pod. The pod deletes itself.
The request succeeds!
- From the curl pod, send a request to the ratings app.
Cleanup
You can optionally remove the resources that you set up as part of this guide.
- Remove the LDAP server that you created.
- Remove the Gloo resources that you created.