|
| 1 | +--- |
| 2 | +title: Introduction to Keycloak |
| 3 | +authorName: Amrita Shrestha |
| 4 | +authorAvatar: https://avatars.githubusercontent.com/u/54478846?s=100&v=4 |
| 5 | +authorLink: https://github.com/amrita-shrestha |
| 6 | +createdAt: May 31, 2024 |
| 7 | +tags: Keycloak, IAM |
| 8 | +banner: https://blog.jankaritech.com/src/assets/Keycloak/images/Keycloak/Keycloak.png |
| 9 | +--- |
| 10 | + |
| 11 | +[Keycloak](https://github.com/keycloak/keycloak) is an open-source project created by RedHat for Single Sign-On. It provides an `Identity and Access Management` (IAM) solution designed to secure application services. |
| 12 | +Additionally, it enables users to authenticate through various identity providers and use fine-grained permissions for regulating access to Software as a Service (SaaS) applications. |
| 13 | + |
| 14 | +It facilitates the creation of a user database with customizable roles and groups, offering functionalities such as user management, registration, and password policy enforcement. |
| 15 | +This makes it a comprehensive Identity and Access Management solution for contemporary applications. The Keycloak API enables the integration of all these features into your application |
| 16 | +without the need for additional coding. |
| 17 | + |
| 18 | +## Keycloak Features |
| 19 | + |
| 20 | + |
| 21 | +1. Identity Broker: |
| 22 | + Identity Broker acts as a middleman, linking various service providers with different identity providers. It establishes trust with external identity providers to utilize their identities for accessing internal services offered by service providers. |
| 23 | + Imagine a company, which has two Keycloak setups: one for customers and another for internal employees. If you want your employees to access external services without creating new accounts on the customer Keycloak, you can use the customer Keycloak |
| 24 | + as `Broker` and the internal Keycloak as `Provider` in this scenario. |
| 25 | + |
| 26 | +2. User Federation: |
| 27 | + Keycloak provides the ability to integrate with existing LDAP or Active Directory servers. It also supports implementation of your own provider such as a relational database. |
| 28 | + |
| 29 | +3. Standard Protocols: |
| 30 | + Keycloak is based on three standard protocols i.e., OpenID Connect, OAuth2.0, and SAML. |
| 31 | + |
| 32 | +4. Password Policies: |
| 33 | + Keycloak contains different password policies i.e., HashAlgorithm, Hashing Iterations, Digits, Lowercase Characters, Regular Expression and so on. |
| 34 | + |
| 35 | +5. Single Sign-On: |
| 36 | + Keycloak facilitates a seamless login experience where users only need to sign in once to access multiple applications, eliminating the need for repeated logins. |
| 37 | + This not only streamlines the user experience but also simplifies identity management for administrators. |
| 38 | + |
| 39 | +6. Social Login: |
| 40 | + Keycloak has built-in support to login via Google, GitHub, and Facebook, which helps us to use social identity providers. |
| 41 | + |
| 42 | +## Keycloak Installation |
| 43 | +### By Docker |
| 44 | +Make sure [Docker Engine](https://docs.docker.com/engine/install/) and [Docker Compose](https://docs.docker.com/compose/install/) has been installed in your system. |
| 45 | +Two different ways are available to install Keycloak using docker as mentioned below. |
| 46 | + |
| 47 | +1. Keycloak using docker image |
| 48 | +Run the following docker command to serve the Keycloak image. Keycloak will be available on `http://localhost:8080. |
| 49 | +```bash |
| 50 | +docker run -p 8080:8080 -e KEYCLOAK_ADMIN=admin -e KEYCLOAK_ADMIN_PASSWORD=admin quay.io/keycloak/keycloak:24.0.2 start-dev |
| 51 | + ``` |
| 52 | + |
| 53 | +2. Keycloak with postgres database using docker-compose |
| 54 | +``` |
| 55 | +services: |
| 56 | + postgres: |
| 57 | + image: postgres:alpine |
| 58 | + volumes: |
| 59 | + - postgres_data:/var/lib/postgresql/data |
| 60 | + environment: |
| 61 | + POSTGRES_DB: keycloak |
| 62 | + POSTGRES_USER: keycloak |
| 63 | + POSTGRES_PASSWORD: keycloak |
| 64 | +
|
| 65 | + keycloak: |
| 66 | + image: quay.io/keycloak/keycloak:24.0.1 |
| 67 | + command: ["start-dev"] |
| 68 | + environment: |
| 69 | + KC_DB: postgres |
| 70 | + KC_DB_URL: "jdbc:postgresql://postgres:5432/keycloak" |
| 71 | + KC_DB_USERNAME: keycloak |
| 72 | + KC_DB_PASSWORD: keycloak |
| 73 | + KC_FEATURES: impersonation |
| 74 | + KEYCLOAK_ADMIN: admin |
| 75 | + KEYCLOAK_ADMIN_PASSWORD: admin |
| 76 | + ports: |
| 77 | + - 8001:8080 # map keycloak port 8080 to 8001 |
| 78 | + depends_on: |
| 79 | + - postgres |
| 80 | +
|
| 81 | +volumes: |
| 82 | + postgres_data: |
| 83 | +``` |
| 84 | + |
| 85 | +Save the above code in `compose.yaml` file and run `docker compose up` in a terminal. |
| 86 | + |
| 87 | +Docker compose serves Keycloak on `http://localhost:8001`. |
| 88 | + |
| 89 | +### By Distribution File |
| 90 | +There is another way to set up Keycloak using the Keycloak distribution file. |
| 91 | +1. Download the Keycloak distribution |
| 92 | + ``` |
| 93 | + export KC_VERSION=24.0.4 |
| 94 | + curl -LO https://github.com/keycloak/keycloak/releases/download/"${KC_VERSION}"/keycloak-"${KC_VERSION}".zip |
| 95 | + ``` |
| 96 | +
|
| 97 | +2. Unzip the Keycloak distribution package |
| 98 | + ``` |
| 99 | + unzip keycloak-${KC_VERSION}.zip |
| 100 | + ``` |
| 101 | +
|
| 102 | +3. Install openjdk |
| 103 | + ``` |
| 104 | + sudo apt install openjdk-17-jdk |
| 105 | + ``` |
| 106 | +
|
| 107 | +4. Navigate to the Keycloak directory |
| 108 | + ``` |
| 109 | + cd keycloak-${KC_VERSION} |
| 110 | + ``` |
| 111 | + |
| 112 | +5. When we start the server for the first time, we have to set the admin user and the admin password: |
| 113 | + ``` |
| 114 | + KEYCLOAK_ADMIN=admin KEYCLOAK_ADMIN_PASSWORD=admin ./bin/kc.sh start-dev |
| 115 | + ``` |
| 116 | + > Note: if port 8080 is already used, then you can map the Keycloak port using the Keycloak environment variable 'KC_HTTP_PORT=8001' |
| 117 | +
|
| 118 | +6. When we start again, it is not necessary to set these variables again. You can start the server with: |
| 119 | + ``` |
| 120 | + ./bin/kc.sh start-dev |
| 121 | + ``` |
| 122 | + > Note: start-dev runs the Keycloak application in DEV-mode. Do not use this for production. |
| 123 | +
|
| 124 | +By default, the Keycloak server is on port 8080 for http and 8084 for https. They are only served from the localhost loopback address 127.0.0.1: |
| 125 | +Keycloak serves on `http://localhost:<port>`. |
| 126 | + |
| 127 | +## Why Keycloak Matters |
| 128 | +1. Keycloak is open source. |
| 129 | +2. Keycloak supports three different authentication protocols which gives you the possibility to cover many applications with different security demands with a single tool. |
| 130 | +3. Keycloak provides a web-based GUI which makes any configuration changes easy. |
| 131 | +4. Keycloak has huge [community support](https://www.keycloak.org/community). |
| 132 | + |
| 133 | +## Keycloak Drawbacks |
| 134 | +Despite its extensive features, Keycloak does have certain limitations. One of these is the need for a more varied implementation approach. |
| 135 | +Additionally, the following are some of the drawbacks associated with Keycloak: |
| 136 | +1. Complex Server Deployment: |
| 137 | + The manual process involved in deploying Keycloak on a server can be complicated, potentially affecting overall productivity. |
| 138 | + |
| 139 | +2. Lack of Comprehensive Support Documentation: |
| 140 | + Despite its robust functionalities, Keycloak could benefit from more efficient and extensive support documentation. This would make it easier for users to find suitable solutions to their problems. |
| 141 | + |
| 142 | +## When It May Not Be the Best Choice |
| 143 | +1. Enterprises seeking robust guarantees or support may find Keycloak lacking in this regard. As an open-source project, there are no assurances provided by its producer regarding its functionality or roadmap. |
| 144 | + Support is community-driven, typically through platforms like Stack Overflow, with no guaranteed response times. |
| 145 | +2. If your application consists of a single application with just one client in the Keycloak realm, you won't benefit from Single Sign-On (SSO) capabilities. |
| 146 | +3. For applications solely reliant on a pure user database, Keycloak could be overkill. A database with specific tables may offer a simpler alternative, especially if you already have one set up. |
| 147 | + |
| 148 | +In conclusion, when considering Identity Access Management solutions, Keycloak stands out as a robust option. However, it's essential to explore alternatives such as Auth0, Zluri, Microsoft Azure Active Directory, Okta, or WSO2 Identity Server to ensure you find the best fit for your specific needs and preferences. |
0 commit comments