<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Nissim Museri on Medium]]></title>
        <description><![CDATA[Stories by Nissim Museri on Medium]]></description>
        <link>https://medium.com/@nissim.museri?source=rss-4db4db3dc5c3------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*bOALlH8NJZiXGKwTB87lmw.png</url>
            <title>Stories by Nissim Museri on Medium</title>
            <link>https://medium.com/@nissim.museri?source=rss-4db4db3dc5c3------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 23:04:05 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@nissim.museri/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Uncloak the Key to Orchestration: Streamlining Keycloak in Kubernetes]]></title>
            <link>https://medium.com/israeli-tech-radar/uncloak-the-key-to-orchestration-streamlining-keycloak-in-kubernetes-f6f20a1d82e3?source=rss-4db4db3dc5c3------2</link>
            <guid isPermaLink="false">https://medium.com/p/f6f20a1d82e3</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[keycloak]]></category>
            <category><![CDATA[automation]]></category>
            <category><![CDATA[kubernetes]]></category>
            <category><![CDATA[devops]]></category>
            <dc:creator><![CDATA[Nissim Museri]]></dc:creator>
            <pubDate>Sun, 14 Jul 2024 06:13:00 GMT</pubDate>
            <atom:updated>2024-07-14T10:01:15.937Z</atom:updated>
            <content:encoded><![CDATA[<p>Keycloak is an open-source identity and access management (IAM) solution that provides authentication, authorization, and user management features for modern applications and services.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*g4-Vr28L2xeM1kpN.png" /></figure><p>Like other complex software applications, we have a significant number of configuration settings that need to be manually adjusted. This sometimes makes us hesitant to implement new tools in the system.<br>As a DevOps Engineer at <a href="http://www.tikalk.com/">Tikal</a>, my approach to overcoming this is to first learn the manual operations and then automate them in a simple way.</p><p>In this blog post, I will describe how we achieved a fully automated way of managing Keycloak with its complex configuration using K8s’ native capabilities, Helm, and Python.<br>By automating Keycloak configuration, we can ensure consistency, reduce manual efforts, and enable scalable and repeatable deployments.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Fpk1Cg7e-_xH2Pq6VNxVVQ.jpeg" /><figcaption>Uncloak the Key to Orchestration</figcaption></figure><p>So, after we know what is Keycloak and know it is a complex software that we want to deploy in our Kubernetes environment, but don’t want to spend our time setting its settings, we can deep dive into the details.</p><p>Our application(which uses Keycloak as its IAM) is developed on a Kubernetes cluster as a part of our microservices.<br>We are using Helm to deploy Keycloak and PostgreSQL in our cluster in a customizable way.</p><h3><strong>Prerequisites:</strong></h3><p>As prerequisites for this task, we used:</p><ul><li><a href="https://github.com/codecentric/helm-charts/releases">Codecentric</a> chart for Keycloak(<a href="https://artifacthub.io/packages/helm/codecentric/keycloak/18.4.4">v18.4.4</a>)</li><li><a href="https://bitnami.com/">Bitnami</a> chart for PostgreSQL(<a href="https://artifacthub.io/packages/helm/bitnami/postgresql/13.2.19">v13.2.19</a>)</li></ul><p><em>* We preferred to use a different PostgreSQL chart from the default one that comes with the Keycloak chart to have more control over our database configuration.</em></p><p><em>** To simplify things, I am presenting here a way to pass credentials via environment variables. There are other methods that are more secure for production environments.</em></p><h3>Steps to achieve our target, before and after:</h3><h4><strong>1. Instead of importing the Realm settings json file, save it on your container</strong></h4><p>Till now, when we wanted to deploy Keycloak in a new environment, we based on a json file that contains configurations related to all the environments, and loaded it via the Keycloak UI:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IUqUo-ntYlt4j-RAc-ob6g.png" /></figure><p>With the new approach, we are using Codecentric Keycloak chart capabilities to reference a Kubernetes Secret that contains the json file for our Realm configurations. We are mounting it directly to the container’s volume.<br>The <strong>values.yaml</strong> file should include that part for the Keycloak configurations:</p><pre>extraVolumes: |<br>  - name: realm-secret<br>    secret:<br>      secretName: realm-secret<br><br>extraVolumeMounts: |<br>  - name: realm-secret<br>    mountPath: &quot;/realm/&quot;<br>    readOnly: true<br><br>extraEnv: |<br>  - name: KEYCLOAK_IMPORT<br>    value: /realm/realm.json</pre><p>You can create this secret with the following command:</p><pre>kubectl create secret generic realm-secret --from-file=&lt;path-to-realm-file&gt;</pre><p><em>** Make sure you point to the realm json file path.</em></p><h4>2.<strong> Instead of ClickOps on the UI, work with the API</strong></h4><p>As soon as we load the Realm settings, we can convert all manual operations (ClickOps) to work with the API provided by Keycloak to configure our environment behind the scenes.</p><p>My choice was to use the Python requests package, which provides a simple method for sending HTTP requests.</p><p>Our first step is to obtain the access token of our administrator user and send it as a header when making API calls to the Keycloak server.</p><pre>def get_access_token(keycloak_url, admin_username, admin_password):<br>    try:<br>        token_url = f&quot;{keycloak_url}/realms/master/protocol/openid-connect/token&quot;<br>        token_payload = {<br>            &quot;username&quot;: admin_username,<br>            &quot;password&quot;: admin_password,<br>            &quot;grant_type&quot;: &quot;password&quot;,<br>            &quot;client_id&quot;: &quot;admin-cli&quot;<br>        }<br>        token_response = requests.post(token_url, data=token_payload)<br>        token_response.raise_for_status()<br>        return token_response.json().get(&quot;access_token&quot;)<br>    except requests.exceptions.RequestException as e:<br>        print(f&quot;Error occurred while obtaining access token: {e}&quot;)<br>        return None</pre><p>Once we have it, we are able to perform any “Click” that we performed as administrators of the system previously, in several simple lines of code.</p><p><strong>Examples of Programming Clicks:</strong></p><p>Case 1 — Assign a User to Group</p><p>In my case, I needed to assign a user to a group, so I simply loaded the user’s ID and the group ID, and enforced their combination:</p><pre>user_id = get_user_id(keycloak_url, realm, client_username, headers)<br>group_id = get_group_id(keycloak_url, realm, group_name, headers)<br>if not user_id:<br>  user_id = create_user(keycloak_url, realm, client_username, client_password, client_email, headers)<br><br>assign_user_to_group(keycloak_url, realm, user_id, group_id, headers)</pre><p>The implementation is using PUT calls, for example:</p><pre>def get_user_id(keycloak_url, realm, client_username, headers):<br>    try:<br>        user_url = f&quot;{keycloak_url}/admin/realms/{realm}/users&quot;<br>        response = requests.get(user_url, headers=headers)<br>        response.raise_for_status()<br>        users = response.json()<br>        for user in users:<br>            if user[&quot;username&quot;] == client_username:<br>                return user[&quot;id&quot;]<br>        return None<br>    except requests.exceptions.RequestException as e:<br>        print(f&quot;Error occurred while retrieving user ID: {e}&quot;)<br>        return None</pre><p>And so on for the other functions.</p><p>Case 2 — Password Generation</p><p>In another case, I required Keycloak to generate and pass a one-time secret to my application.</p><p>It was previously generated by clicking on the “generate” button, however, we can now generate it via the API, and it is then sent to my application via Kubernetes Secret, which is exported by one application and read by another.<br>For this purpose, I am using the kubernetes package in Python code.</p><pre>def regenerate_client_secret(keycloak_url, realm, client_id, headers):<br>    try:<br>        client_secret_url = f&quot;{keycloak_url}/admin/realms/{realm}/clients/{client_id}/client-secret&quot;<br>        client_secret_response = requests.post(client_secret_url, headers=headers)<br>        client_secret_response.raise_for_status()<br>        return client_secret_response.json().get(&quot;value&quot;)<br>    except requests.exceptions.RequestException as e:<br>        print(f&quot;Error occurred while regenerating client secret: {e}&quot;)<br>        return None<br><br>def create_kubernetes_secret(api_client, namespace, secret_data):<br>    try:<br>        encrypted_data = {key: base64.b64encode(value.encode()).decode() for key, value in secret_data.items()}<br>        secret = client.V1Secret(<br>            api_version=&quot;v1&quot;,<br>            kind=&quot;Secret&quot;,<br>            metadata=client.V1ObjectMeta(name=&quot;keycloak-data-secret&quot;),<br>            type=&quot;Opaque&quot;,<br>            data=encrypted_data<br>        )<br>        api_client.create_namespaced_secret(namespace=namespace, body=secret)<br>    except client.rest.ApiException as e:<br>        print(f&quot;Error occurred while creating Kubernetes secret: {e}&quot;)</pre><pre># Regenerate the client secret<br>new_client_secret = regenerate_client_secret(keycloak_url, realm, client_id, headers)<br>if new_client_secret is None:<br>    raise Exception(&quot;Failed to regenerate client secret&quot;)<br>print(f&quot;New client secret for {client_id}: {new_client_secret}&quot;)<br><br># Create a K8s Secret to store the variables retrieved from Keycloak API<br>secret_data = {<br>    &quot;CLIENT_SECRET&quot;: new_client_secret,<br>    &quot;USER_ID&quot;: user_id,<br>    &quot;GROUP_ID&quot;: subgroup_id,<br>    &quot;CLIENT_ID&quot;: client_id<br>}<br>create_kubernetes_secret(api_client, current_namespace, secret_data)</pre><p>We can get the data from the Secret as an environment variable in our application when the Secret is created:</p><pre>  - name: CLIENT_SECRET<br>    valueFrom:<br>      secretKeyRef:<br>        name: keycloak-data-secret<br>        key: CLIENT_SECRET<br>        optional: true</pre><p>To conclude, the main program looks as follows:</p><pre># Set the necessary variables<br>keycloak_url = os.environ.get(&quot;KEYCLOAK_URL&quot;)<br>realm = os.environ.get(&quot;REALM&quot;)<br>client_username = os.environ.get(&quot;CLIENT_USERNAME&quot;)<br>client_password = os.environ.get(&quot;CLIENT_PASSWORD&quot;)<br>client_email = os.environ.get(&quot;CLIENT_EMAIL&quot;)<br>admin_username = os.environ.get(&quot;ADMIN_USERNAME&quot;)<br>admin_password = os.environ.get(&quot;ADMIN_PASSWORD&quot;)<br>group_name = os.environ.get(&quot;GROUP_NAME&quot;)<br><br># Connect to the K8s context<br>config.load_incluster_config()<br># Create a K8s API client<br>api_client = client.CoreV1Api()<br># Get the current namespace<br>with open(&quot;/var/run/secrets/kubernetes.io/serviceaccount/namespace&quot;, &quot;r&quot;) as file:<br>    current_namespace = file.read().strip()<br><br># Obtain an access token<br>access_token = get_access_token(keycloak_url, admin_username, admin_password)<br>if access_token is None:<br>    raise Exception(&quot;Failed to obtain access token&quot;)<br><br>headers = {<br>    &quot;Authorization&quot;: f&quot;Bearer {access_token}&quot;,<br>    &quot;Content-Type&quot;: &quot;application/json&quot;<br>}<br><br># Assign user to a group(create if that not exists)<br>user_id = get_user_id(keycloak_url, realm, client_username, headers)<br>group_id = get_group_id(keycloak_url, realm, group_name, headers)<br>if not user_id:<br>  user_id = create_user(keycloak_url, realm, client_username, client_password, client_email, headers)<br><br>assign_user_to_group(keycloak_url, realm, user_id, group_id, headers)<br><br># Regenerate the client secret<br>new_client_secret = regenerate_client_secret(keycloak_url, realm, client_id, headers)<br>if new_client_secret is None:<br>    raise Exception(&quot;Failed to regenerate client secret&quot;)<br>print(f&quot;New client secret for {client_id}: {new_client_secret}&quot;)<br><br># Create a K8s Secret to store the variables retrieved from Keycloak API<br>secret_data = {<br>    &quot;CLIENT_SECRET&quot;: new_client_secret,<br>    &quot;USER_ID&quot;: user_id,<br>    &quot;GROUP_ID&quot;: subgroup_id,<br>    &quot;CLIENT_ID&quot;: client_id<br>}<br>create_kubernetes_secret(api_client, current_namespace, secret_data)</pre><p>You can add or edit the operations as your use case.</p><h4><strong>3. Enable your code running in the cluster(RBAC)</strong></h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hGeH2ZGNnhnK-EWsMljOQA.jpeg" /><figcaption>Put your mind logic into your machines</figcaption></figure><p>However it seems a simple way to load your mind login into your Kubernetes cluster, I had some challenges related to permissions and access control.<br>To overcome these challenges, I created the necessary RBAC (Role-Based Access Control) objects, including a ServiceAccount, Role, and RoleBinding.</p><pre>apiVersion: v1<br>kind: ServiceAccount<br>metadata:<br>  name: {{ .Values.global.serviceAccountName }}<br>---<br>apiVersion: rbac.authorization.k8s.io/v1<br>kind: Role<br>metadata:<br>  name: {{ .Values.global.roleName }}<br>rules:<br>  - verbs:<br>      - create<br>    apiGroups:<br>      - &#39;&#39;<br>    resources:<br>      - secrets<br>      - configmaps<br>---<br>apiVersion: rbac.authorization.k8s.io/v1<br>kind: RoleBinding<br>metadata:<br>  name: {{ .Values.global.roleName }}-binding<br>subjects:<br>  - kind: ServiceAccount<br>    name: {{ .Values.global.serviceAccountName }}<br>roleRef:<br>  apiGroup: rbac.authorization.k8s.io<br>  kind: Role<br>  name: {{ .Values.global.roleName }}</pre><p>A short description of that resources to understand what we are doing:</p><ol><li><strong>ServiceAccount</strong> — represents the identity under which the script runs in the cluster.</li><li><strong>Role</strong> — defines the permissions granted to the ServiceAccount. In this case, the Role allows the creation of Secrets and ConfigMaps in the current namespace.</li><li><strong>RoleBinding</strong> — associates the <strong>ServiceAccount</strong> with the <strong>Role</strong>, effectively granting the ServiceAccount the permissions defined in the Role.</li></ol><p>By creating these RBAC resources, I ensured that the script running in the Kubernetes Job had the necessary permissions to create Secrets and ConfigMaps within the cluster, enabling it to store the retrieved Keycloak variables and configuration data.</p><h4><strong>4. Deploy the automation with Helm(Job + values)</strong></h4><p>After all that preparation, we are ready to start running our logic on the Kubernetes cluster. Our most straightforward method is to create a Kubernetes Job that executes our Python backend code and inserts the environment variables as configurable values.</p><p>To package the script and its dependencies into a container image, I created a Dockerfile with the following layers:<br>The Dockerfile is as follows:</p><pre>FROM python:3.8-alpine<br><br>COPY requirements.txt .<br>RUN pip install -r requirements.txt<br><br>RUN adduser --disabled-password --gecos &#39;&#39; appuser<br><br><br>COPY --chown=appuser:appuser config_realm.py /app/<br>RUN chmod +x /app/config_realm.py<br><br><br>WORKDIR /app<br>USER appuser<br><br>CMD [&quot;python&quot;, &quot;/app/config_realm.py&quot;]</pre><p>By integrating this automation script into our deployment process and running it from a Kubernetes Job after Keycloak has been successfully deployed, I have improved our deployment process. Helm chart defines this job to be triggered when Keycloak is enabled to receive traffic (using an initContainer which listens to the Keycloak service and ensures it is running).</p><pre>{{- if .Values.keycloak.enabled }}<br>apiVersion: batch/v1<br>kind: Job<br>metadata:<br>  name: {{ .Values.global.jobName }}<br>spec:<br>  template:<br>    spec:<br>      serviceAccountName: {{ .Values.global.serviceAccountName }}<br>      initContainers:<br>      - name: test-connectivity<br>        image: curlimages/curl:7.73.0<br>        args:<br>        - /bin/sh<br>        - -c<br>        - &gt;<br>            set -x;<br>            while [ $(curl -sw &#39;%{http_code}&#39; &quot;http://&lt;&lt;keycloak_svc_name&gt;&gt;:80&quot; -o /dev/null) -ne 200 ]; do # Replace with your Keycloak service name<br>            sleep 15;<br>            done<br>      containers:<br>      - name: realm-config<br>        image: &lt;&lt;image_tag_of_our_script&gt;&gt; # Replace with your Keycloak image tag that created from the previous Dockerfile<br>        env:<br>        - name: KEYCLOAK_URL<br>          value: {{ .Values.global.keycloak.url }}<br>        - name: REALM<br>          value: {{ .Values.global.keycloak.realmName }}<br>        - name: CLIENT_USERNAME<br>          value: {{ .Values.global.keycloak.clientUser }}<br>        - name: CLIENT_PASSWORD<br>          value: {{ .Values.global.keycloak.clientPassword }}<br>        - name: CLIENT_EMAIL<br>          value: {{ .Values.global.keycloak.clientEmail }}<br>        - name: ADMIN_USERNAME<br>          value: {{ .Values.global.keycloak.username }}<br>        - name: ADMIN_PASSWORD<br>          value: {{ .Values.global.keycloak.password }}<br>        resources:<br>          limits:<br>            memory: &quot;128Mi&quot;<br>            cpu: &quot;100m&quot;<br>          requests:<br>            memory: &quot;128Mi&quot;<br>            cpu: &quot;100m&quot;<br>      restartPolicy: Never<br>  backoffLimit: 4<br>  {{ end }}</pre><p><em>* Be sure to change the placeholders: </em><em>&lt;&lt;keycloak_svc_name&gt;&gt; </em><em>&lt;&lt;image_tag_of_our_script&gt;&gt;` with the relevant values.</em></p><p>Here’s a simplified version of the Helm chart values.yaml file:</p><pre>global:<br>  serviceAccountName: &#39;keycloak-job-sa&#39;<br>  roleName: &#39;secret-configmap-creator&#39;<br>  jobName: &#39;keycloak-realm-creator&#39;<br>namespace: example<br>cluster: test<br>keycloak:<br>  enabled: true<br>  fullnameOverride: &quot;keycloak&quot;<br>  serviceAccount:<br>    create: false<br>    name: &#39;keycloak-job-sa&#39;<br>  extraVolumes: |<br>    - name: keycloak-realm-json<br>      secret:<br>        secretName: keycloak-realm-json<br>  extraVolumeMounts: |<br>    - name: keycloak-realm-json<br>      mountPath: &quot;/realm/&quot;<br>      readOnly: true<br>  extraEnv: |<br>    - name: KEYCLOAK_IMPORT<br>      value: /realm/realm.json</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FKxyndABCpDHIkMgmvJDQA.jpeg" /><figcaption>Streamlining Keycloak in Kubernetes</figcaption></figure><h4>Conclusion</h4><p>Automating Keycloak configuration using Kubernetes’ native capabilities and Python has significantly enhanced our DevOps workflow, eliminating errors and ensuring consistent setups. This adaptable method can be extended to various scenarios, making it an invaluable DevOps tool.</p><p>Next time you face repetitive manual tasks, remember this approach. By embracing automation, you can transform time-consuming operations into efficient, reliable processes, boosting your productivity.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f6f20a1d82e3" width="1" height="1" alt=""><hr><p><a href="https://medium.com/israeli-tech-radar/uncloak-the-key-to-orchestration-streamlining-keycloak-in-kubernetes-f6f20a1d82e3">Uncloak the Key to Orchestration: Streamlining Keycloak in Kubernetes</a> was originally published in <a href="https://medium.com/israeli-tech-radar">Israeli Tech Radar</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>