|
| 1 | +# User Federation with AD/LDAP |
| 2 | + |
| 3 | +Keycloak allows user federation with AD/LDAP. This guide walks you through the user federation with `OpenLDAP` service. For more infomation, see [User Federation](https://www.keycloak.org/docs/latest/server_admin/#_user-storage-federation) from the Keycloak document. |
| 4 | + |
| 5 | + |
| 6 | +# Prerequisities |
| 7 | + |
| 8 | +1. `Keycloak on AWS` - We assume you have already deployed the [keycloak-on-aws](https://github.com/aws-samples/keycloak-on-aws) via cloudformation or AWS CDK and already successfully logged in the keycload dashboard as `keycloak` admin user. |
| 9 | + |
| 10 | +2. `OpenLDAP` - Keycloak supports multiple LDAP services including Microsoft AD and [OpenLDAP](https://www.openldap.org/). In the following guide, we will run an openldap service in the same VPC with the keycloak service for this demo. |
| 11 | + |
| 12 | +# Create a OpenLDAP service |
| 13 | + |
| 14 | +We are going to deploy an openldap service as container from the official [bitnami/openldap](https://gallery.ecr.aws/bitnami/openldap) from Amazon ECR public gallery: |
| 15 | + |
| 16 | +1. launch an Amazon Linux 2 EC2 instance in the same VPC with the your keycloak service |
| 17 | +2. Log in the EC2 instance and run the command to bring up the openldap service: |
| 18 | + |
| 19 | +```sh |
| 20 | +image='public.ecr.aws/bitnami/openldap:latest' |
| 21 | + |
| 22 | +docker run -p 389:1389 $image |
| 23 | +``` |
| 24 | + |
| 25 | +This will bring up the openldap service with docker and keep it running in the foreground. Leave this terminal open. |
| 26 | + |
| 27 | +Under the covers, the `bitnami/openldap` will create `user01` and `user02` as initial users with their default passwords. See the [description](https://gallery.ecr.aws/bitnami/openldap) for more details about the customization. |
| 28 | + |
| 29 | + |
| 30 | +3. configure the security group of this EC2 instance and ensure all traffic from the VPC CIDR can visit its LDAP port(`TCP 389`) |
| 31 | + |
| 32 | + |
| 33 | + |
| 34 | +4. Open another termilal and install the openldap clients on the same EC2. |
| 35 | + |
| 36 | +```sh |
| 37 | +# install ldap client |
| 38 | +yum install openldap-clients |
| 39 | +# list all users |
| 40 | +ldapsearch -x -b "ou=users,dc=example,dc=org" -H ldap://<EC2_PRIVATE_IP> |
| 41 | +``` |
| 42 | + |
| 43 | +For example: |
| 44 | + |
| 45 | +``` |
| 46 | +# ldapsearch -x -b "ou=users,dc=example,dc=org" -H ldap://172.31.47.144 |
| 47 | +# extended LDIF |
| 48 | +# |
| 49 | +# LDAPv3 |
| 50 | +# base <ou=users,dc=example,dc=org> with scope subtree |
| 51 | +# filter: (objectclass=*) |
| 52 | +# requesting: ALL |
| 53 | +# |
| 54 | +
|
| 55 | +# users, example.org |
| 56 | +dn: ou=users,dc=example,dc=org |
| 57 | +objectClass: organizationalUnit |
| 58 | +ou: users |
| 59 | +
|
| 60 | +# user01, users, example.org |
| 61 | +dn: cn=user01,ou=users,dc=example,dc=org |
| 62 | +cn: User1 |
| 63 | +cn: user01 |
| 64 | +sn: Bar1 |
| 65 | +objectClass: inetOrgPerson |
| 66 | +objectClass: posixAccount |
| 67 | +objectClass: shadowAccount |
| 68 | +userPassword:: Yml0bmFtaTE= |
| 69 | +uid: user01 |
| 70 | +uidNumber: 1000 |
| 71 | +gidNumber: 1000 |
| 72 | +homeDirectory: /home/user01 |
| 73 | +
|
| 74 | +# user02, users, example.org |
| 75 | +dn: cn=user02,ou=users,dc=example,dc=org |
| 76 | +cn: User2 |
| 77 | +cn: user02 |
| 78 | +sn: Bar2 |
| 79 | +objectClass: inetOrgPerson |
| 80 | +objectClass: posixAccount |
| 81 | +objectClass: shadowAccount |
| 82 | +userPassword:: Yml0bmFtaTI= |
| 83 | +uid: user02 |
| 84 | +uidNumber: 1001 |
| 85 | +gidNumber: 1001 |
| 86 | +homeDirectory: /home/user02 |
| 87 | +
|
| 88 | +# readers, users, example.org |
| 89 | +dn: cn=readers,ou=users,dc=example,dc=org |
| 90 | +cn: readers |
| 91 | +objectClass: groupOfNames |
| 92 | +member: cn=user01 cn\3Duser02,ou=users,dc=example,dc=org |
| 93 | +
|
| 94 | +# search result |
| 95 | +search: 2 |
| 96 | +result: 0 Success |
| 97 | +
|
| 98 | +# numResponses: 5 |
| 99 | +# numEntries: 4 |
| 100 | +``` |
| 101 | + |
| 102 | +Now your default LDAP service is ready. |
| 103 | + |
| 104 | + |
| 105 | +# Create a User Federation on Keycloak |
| 106 | + |
| 107 | +1. Login the Keycloak dashboard as `keycloak` admin user. |
| 108 | + |
| 109 | +2. Select **Configure** , **User Federation** |
| 110 | + |
| 111 | + |
| 112 | + |
| 113 | +3. Create a LDAP provider as the screenshot below: |
| 114 | + |
| 115 | + |
| 116 | + |
| 117 | +Make sure the **Connection URL** points to your private IP of the EC2 isntance. |
| 118 | + |
| 119 | +Enter `adminpassword` for the **Bind credential** |
| 120 | + |
| 121 | +Click the **Test connection** and **Test Authentication** buttons to ensure everything is working. |
| 122 | + |
| 123 | +Scroll down and click `Synchronize all users`, and 2 users should be imported. |
| 124 | + |
| 125 | + |
| 126 | + |
| 127 | + |
| 128 | +Click **Manage**, **Users** from the left panel. `user1` and `user2` should be imported successfully. |
| 129 | + |
| 130 | + |
| 131 | + |
| 132 | + |
| 133 | +# Validate the user federation |
| 134 | + |
| 135 | +Now let's validate the user federation with the `account-console` login. |
| 136 | + |
| 137 | +Go to **Clients** and click the link of `account-console` |
| 138 | + |
| 139 | + |
| 140 | + |
| 141 | +You should be redirected to the Keycloak account console, click **Sign Out** and **Sign In** on the top right button. Now enter the federated user credentials as: |
| 142 | + |
| 143 | +Username: **user1** |
| 144 | +Password: **bitnami1** |
| 145 | + |
| 146 | + |
| 147 | + |
| 148 | + |
| 149 | +And you should be able to successfully sign in with your LDAP user credentials. |
| 150 | + |
| 151 | + |
| 152 | + |
| 153 | +# FAQ |
| 154 | + |
| 155 | +**Q: Does keycloak support `ldaps` protocol?** |
| 156 | + |
| 157 | +A: Yes. both `ldap://` and `ldaps://` are supported. To enable `ldaps://`, make sure your AD/LDAP is running with `LDAPS` and has properly imported the certificate. |
| 158 | + |
| 159 | + |
| 160 | + |
| 161 | +**Q: What vendor type should I select if I am running Microsoft AD server?** |
| 162 | + |
| 163 | +A: Select **Active Directory** in the **Vendor** parameter. |
| 164 | + |
| 165 | + |
0 commit comments