Skip to main content
Redhat Developers  Logo
  • Products

    Featured

    • Red Hat Enterprise Linux
      Red Hat Enterprise Linux Icon
    • Red Hat OpenShift AI
      Red Hat OpenShift AI
    • Red Hat Enterprise Linux AI
      Linux icon inside of a brain
    • Image mode for Red Hat Enterprise Linux
      RHEL image mode
    • Red Hat OpenShift
      Openshift icon
    • Red Hat Ansible Automation Platform
      Ansible icon
    • Red Hat Developer Hub
      Developer Hub
    • View All Red Hat Products
    • Linux

      • Red Hat Enterprise Linux
      • Image mode for Red Hat Enterprise Linux
      • Red Hat Universal Base Images (UBI)
    • Java runtimes & frameworks

      • JBoss Enterprise Application Platform
      • Red Hat build of OpenJDK
    • Kubernetes

      • Red Hat OpenShift
      • Microsoft Azure Red Hat OpenShift
      • Red Hat OpenShift Virtualization
      • Red Hat OpenShift Lightspeed
    • Integration & App Connectivity

      • Red Hat Build of Apache Camel
      • Red Hat Service Interconnect
      • Red Hat Connectivity Link
    • AI/ML

      • Red Hat OpenShift AI
      • Red Hat Enterprise Linux AI
    • Automation

      • Red Hat Ansible Automation Platform
      • Red Hat Ansible Lightspeed
    • Developer tools

      • Red Hat Trusted Software Supply Chain
      • Podman Desktop
      • Red Hat OpenShift Dev Spaces
    • Developer Sandbox

      Developer Sandbox
      Try Red Hat products and technologies without setup or configuration fees for 30 days with this shared Openshift and Kubernetes cluster.
    • Try at no cost
  • Technologies

    Featured

    • AI/ML
      AI/ML Icon
    • Linux
      Linux Icon
    • Kubernetes
      Cloud icon
    • Automation
      Automation Icon showing arrows moving in a circle around a gear
    • View All Technologies
    • Programming Languages & Frameworks

      • Java
      • Python
      • JavaScript
    • System Design & Architecture

      • Red Hat architecture and design patterns
      • Microservices
      • Event-Driven Architecture
      • Databases
    • Developer Productivity

      • Developer productivity
      • Developer Tools
      • GitOps
    • Secure Development & Architectures

      • Security
      • Secure coding
    • Platform Engineering

      • DevOps
      • DevSecOps
      • Ansible automation for applications and services
    • Automated Data Processing

      • AI/ML
      • Data Science
      • Apache Kafka on Kubernetes
      • View All Technologies
    • Start exploring in the Developer Sandbox for free

      sandbox graphic
      Try Red Hat's products and technologies without setup or configuration.
    • Try at no cost
  • Learn

    Featured

    • Kubernetes & Cloud Native
      Openshift icon
    • Linux
      Rhel icon
    • Automation
      Ansible cloud icon
    • Java
      Java icon
    • AI/ML
      AI/ML Icon
    • View All Learning Resources

    E-Books

    • GitOps Cookbook
    • Podman in Action
    • Kubernetes Operators
    • The Path to GitOps
    • View All E-books

    Cheat Sheets

    • Linux Commands
    • Bash Commands
    • Git
    • systemd Commands
    • View All Cheat Sheets

    Documentation

    • API Catalog
    • Product Documentation
    • Legacy Documentation
    • Red Hat Learning

      Learning image
      Boost your technical skills to expert-level with the help of interactive lessons offered by various Red Hat Learning programs.
    • Explore Red Hat Learning
  • Developer Sandbox

    Developer Sandbox

    • Access Red Hat’s products and technologies without setup or configuration, and start developing quicker than ever before with our new, no-cost sandbox environments.
    • Explore Developer Sandbox

    Featured Developer Sandbox activities

    • Get started with your Developer Sandbox
    • OpenShift virtualization and application modernization using the Developer Sandbox
    • Explore all Developer Sandbox activities

    Ready to start developing apps?

    • Try at no cost
  • Blog
  • Events
  • Videos

Keycloak Identity Brokering with OpenShift

December 6, 2017
David Martin
Related topics:
Kubernetes
Related products:
Red Hat OpenShift Container Platform

Share:

    This article discusses how to set up and configure a Keycloak instance to use OpenShift for authentication via Identity Brokering. This allows for Single Sign On between the OpenShift cluster and the Keycloak instance. The Keycloak instance will be running on the OpenShift cluster and leverage a ServiceAccount OAuth Client.

    Provisioning Keycloak to your OpenShift namespace

    Use the below command to create the Keycloak resources in your OpenShift project.

     oc process -f https://raw.githubusercontent.com/david-martin/keycloak-with-openshift-auth-provider/0.0.2/keycloak-with-openshift-auth-provider.yaml | oc create -f -
    
    

    IMPORTANT: This template is intended for demonstration purposes only.

    This will create the following resources:

    • DeploymentConfig - Defines the Keycloak image to use and other Pod & container settings.
    • Service - Defines a Service in front of the Keycloak Pod.
    • Route - Exposes the Keycloak Service at an externally available hostname.
    • ServiceAccount - Defines a constrained form of OAuth Client in our namespace.

    For more info on the ServiceAccount OAuth Client, see https://docs.openshift.com/container-platform/3.7/architecture/additional_concepts/authentication.html#service-accounts-as-oauth-clients.

    After provisioning, the Keycloak service will be available at the exposed Route. Use the below command to get the route and sign in to the Administration Console.

    oc get route keycloak --template "http://{{.spec.host}} "

    You can sign in initially using the 'admin' user and the generated password stored in the 'KEYCLOAK_PASSWORD' environment variable.

    oc env dc/keycloak --list | grep KEYCLOAK_PASSWORD

    Creating a new Realm

    The 'admin' user will be signed in to the 'master' realm. This user has full control over the Keycloak instance. We can create a dedicated realm for our OpenShift project and allow OpenShift users to administer the realm. Only users who can access our OpenShift cluster will be able to sign in to Keycloak.

    Create a realm, and name it after our OpenShift Project.

    Configuring Keycloak to use OpenShift for Identity Brokering

    After creating the realm, the context should switch to the new realm. From the 'Identity Providers' menu, choose to 'Add provider...' and select 'OpenShift v3'. Fill in the below fields.

    openshift v3 provider

    Client ID

    This field is the OAuth Client identifier in OpenShift. As we're using a ServiceAccount OAuth Client, the id will be in the below format:

    system:serviceaccount::keycloak

    For example, if our project had an id of 'myproject', the Client ID would be:

    system:serviceaccount:myproject:keycloak

    Client Secret

    The secret is stored as a token for the ServiceAccount in OpenShift. To retrieve the secret, execute the following:

    oc sa get-token keycloak

    Base URL

    The Base URL is the OpenShift Master URL e.g. https://openshift.example.com.

    IMPORTANT: The OpenShift Master URL will need to have a trusted CA-signed certificate for Keycloak to successfully call the OAuth callback endpoint.

    Default Scopes

    These are the scopes to send to OpenShift when authorizing the user. As we're only interested in authentication, and not making modifications to OpenShift on behalf of users, we can just use the 'user:info' scope.

    After filling in all the above fields, the Provider can be created.

    Giving your OpenShift user a Role in Keycloak

    If you attempt to sign in to the realm now, any user who successfully signs in will *only* be able to manage their own account in Keycloak. To allow users to manage the realm, they'll need additional permission. There are two approaches to giving Users extra permissions:

    • Setting Default Roles/Groups for every user.
    • Explicitly setting Roles per user.

    Explicit Roles can be managed from the 'Users' menu after Users have signed in at least once.
    However, we're going to set up Default Roles so Users have roles on first sign in.

    To set a Default Role, choose the 'Default Roles' tab from the 'Roles' menu. Then choose the 'realm-management' Client Role and add the 'manage-realm' role to the 'Client Default Roles'. You may want to choose different or more restrictive roles depending on your requirements.

    realm roles

    Trying it out

    Try it out by navigating to the Realm Admin Console page in a new browser session or incognito window. The URL for the Realm Admin Console page can found in the 'Clients' menu as the Base URL for the 'security-admin-console' Client ID.

    realm client

    This will show a login screen with an 'Openshift v3' option. Choosing the 'Openshift v3' option should open an OpenShift login page.

    openshift v3 provider

    Login to OpenShift and you should eventually be redirected back to Keycloak and be able to manage the Realm. You may need to fill in some account details on the first login.

    Where to go from here?

    If you'd like to have more control over the permissions OpenShift users have in your Keycloak instance, you may want to remove the Default Roles. This would be particularly important if you don't know or trust all OpenShift users. In that case, you could remove the Default Roles, and only add specific Roles to users you trust. You could also create a Group with specific Roles to make it more manageable.

    The OpenShift template used in this article is available at https://github.com/david-martin/keycloak-with-openshift-auth-provider.


    The Red Hat OpenShift Container Platform is available for download.

    Last updated: December 4, 2017

    Recent Posts

    • Create and enrich ServiceNow ITSM tickets with Ansible Automation Platform

    • Expand Model-as-a-Service for secure enterprise AI

    • OpenShift LACP bonding performance expectations

    • Build container images in CI/CD with Tekton and Buildpacks

    • How to deploy OpenShift AI & Service Mesh 3 on one cluster

    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Products

    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform

    Build

    • Developer Sandbox
    • Developer Tools
    • Interactive Tutorials
    • API Catalog

    Quicklinks

    • Learning Resources
    • E-books
    • Cheat Sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site Status Dashboard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit
    © 2025 Red Hat

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility

    Report a website issue