|
| 1 | +# OIDC with Front Channel Logout |
| 2 | + |
| 3 | +In this example, we deploy two web applications, configure load balancing for them via VirtualServers, and protect the |
| 4 | +applications using an [OIDC Policies](https://docs.nginx.com/nginx-ingress-controller/configuration/policy-resource/#oidc) and [Keycloak](https://www.keycloak.org/), and ensure behaviour is consistent across multiple replicas by enabling [Zone Synchronization](https://docs.nginx.com/nginx/admin-guide/high-availability/zone_sync/). |
| 5 | + |
| 6 | +**Note**: The KeyCloak container does not support IPv6 environments. |
| 7 | + |
| 8 | +**Note**: This example assumes that your default namespace is set to `default`. You can check this with |
| 9 | + |
| 10 | +```shell |
| 11 | +kubectl config view --minify | grep namespace |
| 12 | +``` |
| 13 | + |
| 14 | +If it's not empty, and anything other than `default`, you can set to `default` with the following command: |
| 15 | + |
| 16 | +```shell |
| 17 | +kubectl config set-context --namespace default --current |
| 18 | +``` |
| 19 | + |
| 20 | +## Prerequisites |
| 21 | + |
| 22 | +1. Follow the [installation](https://docs.nginx.com/nginx-ingress-controller/installation/installation-with-manifests/) |
| 23 | + instructions to deploy NGINX Ingress Controller. This example requires that the HTTPS port of the Ingress |
| 24 | + Controller is `443`. |
| 25 | +2. Save the public IP address of the Ingress Controller into `/etc/hosts` of your machine: |
| 26 | + |
| 27 | + ```text |
| 28 | + ... |
| 29 | +
|
| 30 | + XXX.YYY.ZZZ.III fclo-one.example.com |
| 31 | + XXX.YYY.ZZZ.III fclo-two.example.com |
| 32 | + XXX.YYY.ZZZ.III keycloak.example.com |
| 33 | + ``` |
| 34 | +
|
| 35 | + Here `fclo-one.example.com` and `fclo-two.example.com` are the domains for the two web applications protected by OIDC authentication, and `keycloak.example.com` is the domain for the Keycloak IdP. |
| 36 | +
|
| 37 | +## Step 1 - Deploy a TLS Secret |
| 38 | +
|
| 39 | +Create a secret with the TLS certificate and key that will be used for TLS termination of the web applications and |
| 40 | +Keycloak: |
| 41 | +
|
| 42 | +```shell |
| 43 | +kubectl apply -f tls-secret.yaml |
| 44 | +``` |
| 45 | + |
| 46 | +## Step 2 - Deploy a Web Application |
| 47 | + |
| 48 | +Create the application deployments and services: |
| 49 | + |
| 50 | +```shell |
| 51 | +kubectl apply -f two-webapps.yaml |
| 52 | +``` |
| 53 | + |
| 54 | +## Step 3 - Deploy Keycloak |
| 55 | + |
| 56 | +1. Create the Keycloak deployment and service: |
| 57 | + |
| 58 | + ```shell |
| 59 | + kubectl apply -f keycloak.yaml |
| 60 | + ``` |
| 61 | + |
| 62 | +2. Create a VirtualServer resource for Keycloak: |
| 63 | + |
| 64 | + ```shell |
| 65 | + kubectl apply -f virtual-server-idp.yaml |
| 66 | + ``` |
| 67 | + |
| 68 | +## Step 4 - Configure Keycloak |
| 69 | + |
| 70 | +To set up Keycloak follow the steps in the "Configuring Keycloak" [section of the documentation](keycloak_setup.md). That guide will get you to create a user, two OIDC clients, and save the client secrets in the necessary files. |
| 71 | + |
| 72 | +## Step 5 - Deploy the Client Secrets |
| 73 | + |
| 74 | +By this step, you should have encoded and edited both the `client-secret-one.yaml` and `client-secret-two.yaml` files. If you haven't, go back to the previous step. |
| 75 | +
|
| 76 | +Apply the secrets that will be used by the OIDC policies for the two virtual server: |
| 77 | +
|
| 78 | +```shell |
| 79 | +kubectl apply -f client-secret-one.yaml |
| 80 | +kubectl apply -f client-secret-two.yaml |
| 81 | +``` |
| 82 | +
|
| 83 | +## Step 6 - Configure Zone Synchronization and Resolver |
| 84 | +
|
| 85 | +In this step we configure: |
| 86 | +
|
| 87 | +- [Zone Synchronization](https://docs.nginx.com/nginx/admin-guide/high-availability/zone_sync/). For the OIDC feature to |
| 88 | + work when you have two or more replicas of the Ingress Controller, it is necessary to enable zone synchronization |
| 89 | + among the replicas. This is to ensure that each replica has access to the required session information when authenticating via IDP such as Keycloak. |
| 90 | +- The resolver can resolve the host names. |
| 91 | +
|
| 92 | +Steps: |
| 93 | +
|
| 94 | +1. Apply the ConfigMap `nginx-config.yaml`, which contains `zone-sync` configuration parameter that enable zone synchronization and the resolver using the kube-dns service. |
| 95 | +
|
| 96 | + ```shell |
| 97 | + kubectl apply -f nginx-config.yaml |
| 98 | + ``` |
| 99 | +
|
| 100 | +## Step 7 - Deploy the OIDC Policies |
| 101 | +
|
| 102 | +Create policies with the names `oidc-one-policy` and `oidc-two-policy` that references the secrets from the previous step: |
| 103 | +
|
| 104 | +```shell |
| 105 | +kubectl apply -f oidc-one.yaml |
| 106 | +kubectl apply -f oidc-two.yaml |
| 107 | +``` |
| 108 | +
|
| 109 | +## Step 8 - Configure Load Balancing |
| 110 | +
|
| 111 | +Create VirtualServer resources for the web applications: |
| 112 | +
|
| 113 | +```shell |
| 114 | +kubectl apply -f two-virtual-servers.yaml |
| 115 | +``` |
| 116 | +
|
| 117 | +Note that the VirtualServers reference the policies `oidc-one-policy` and `oidc-two-policy` created in Step 6. |
| 118 | +
|
| 119 | +## Step 9 - Test the Configuration |
| 120 | +
|
| 121 | +1. Open a web browser and navigate to the URL of one of the web applications: `https://fclo-one.example.com`. You will be |
| 122 | + redirected to Keycloak. |
| 123 | +2. Log in with the username and password for the user you created in Keycloak, `nginx-user` and `test`. |
| 124 | + |
| 125 | +3. Once logged in, you will be redirected to the web application and get a response from it. Notice the field `User ID` |
| 126 | +in the response, this will match the ID for your user in Keycloak.  |
| 127 | +4. If you then navigate to the URL of the other web application, `https://fclo-two.example.com`, you will already be authenticated, and you will see the same `User ID` on the page. |
| 128 | +
|
| 129 | +## Step 10 - Log Out |
| 130 | +
|
| 131 | +**Note:** As Front Channel Logout depends on an invisible iframe from keycloak that points to different domains, your browser's Content Security Policy will normally refuse to make the request. In order to make this work, you will need to install a browser extension that disables applying any CSP headers. This will lower your browser's protection, so please make sure to remove or disable the extension as soon as you're done with the test. |
| 132 | + |
| 133 | +1. To log out, navigate to `https://fclo-one.example.com/logout`. Your session will be terminated, and you will be |
| 134 | + redirected to the default post logout URI `https://fclo-one.example.com/_logout`. |
| 135 | +. You can also initiate this logout from the other webapp as well |
| 136 | +2. To confirm that you have been logged out, navigate to `https://fclo-one.example.com` or `https://fclo-two.example.com`. You will be redirected to Keycloak to log in again. |
| 137 | +3. To confirm that front channel logout was responsible for this, you can look at the `nginx-ingress` pod's logs. You should look for the following two lines: |
| 138 | +
|
| 139 | + ```text |
| 140 | + OIDC Front-Channel Logout initiated for sid: <uuid v4> |
| 141 | + GET /front_channel_logout?sid=<uuid v4> |
| 142 | + ``` |
| 143 | +
|
| 144 | + These should show up twice, once for each client. |
0 commit comments