Keycloak

keycloak is one of the most widely used, and is perfect for this case, as there is PII to secure.

What's working ?

Integration

This demo uses docker and docker compose to run Keycloak, Postgres, and Encryption Proxy. The plan is to install and add a number of users who will then migrate the data to the encrypted version.

Identification PII data

Table
Column
Searching
Note

user_entity

username

user username

user_entity

email

user email

user_entity

email_constraint

link for user email

user_entity

first_name

user first name

user_entity

last_name

user last name

user_attribute

value

custom user value

With this configuration, if written into config, it will be like this:

rules:
  sql:
    # database name
    keycloak:
      # enable compatibility mode
      # you can turn it off after migration done
      compatibility_mode: true
      scheme: public
      # turn on if you want to see an original query
      # for debugging purposes, do not use in production
      log_original_query: false
      # write rules
      write:
        # user entity table
        user_entity:
          username:
            hash: true
          email:
            hash: true
          email_constraint:
            hash: true
          first_name:
            hash: true
          last_name:
            hash: true
        # user attribute table
        user_attribute:
          value:
            hash: true

In this demo we will add new columns to the user_entity and user_attribute tables, namely the _ciphertext and _digest columns. The script can be found in the sqlarrow-up-right folder, we will do it using flywayarrow-up-right.

Clone demo repository

Change directory to

Download demo licenses

This license is valid until 2025-08-13 08:18:28 GMT

Extracting the Keycloak demo data

We use xz to compress data, so we need to install xz first if it's not available.

For Mac, you can use brew to install xz

For Ubuntu, you can use apt to install xz

extract data.json.tar.xz using xz

Run docker compose

The minimal specifications that will be used for this demo, you can customize, according to your needs.

Container
CPU Limit
RAM Limit

postgres:17.4

2

1024M

encryption-proxy:latest

2

1024M

keycloak:26.1.3

4

4096M

For the first time, we point keycloak to an unencrypted postgres database, and we will add some users to it.

Make sure all services are running properly, If successful, we will get something like this

Run k6 to add users

If successful, we will get something like this

Check in keycloak if the number of users is correct

Run migration

Restart keycloak

keycloak caches data, we need to restart to clear the cache.

After migrating, now try searching, the response should be Unauthorized because the username has been encrypted.

The result will be like this:

Change to encryption proxy

To be able to read the encrypted data, the keyclock must be pointed to the encryption-proxy.

Check if the migration was successful, try to search with match email::

The result will be like this:

Now the keycloak database is encrypted without having to change the source code.

Database ui using dbgate

Open http://localhost:3000arrow-up-right

Delete all containers

To remove all containers in this demo, do it using this command:

Last updated