Keycloak Terraform Provider

repository·main·Indexed 21 days ago

https://github.com/keycloak/terraform-provider-keycloak

Terraform provider for managing Keycloak resources. Includes documentation on installation for Terraform >=0.13, migration from mrparkers/keycloak, and configuration for legacy Wildfly distributions. The repository also provides examples and implementation guides for custom authorization policies, custom user storage providers, and custom identity providers.

Tokens
138.5K
Snippets
344
Records
498
Agent score
75%

What's inside terraform-provider-keycloak

  1. Create an Audience Resolve OIDC protocol mapper

    main

    The keycloak_openid_audience_resolve_protocol_mapper resource allows you to create an "Audience Resolve" OIDC protocol mapper in Keycloak. This mapper helps automate audience management by using the presence of client roles to determine which audiences should be included in the token, rather than managing them manually.

    You can attach this mapper to either a specific Client or a Client Scope.

    resource "keycloak_openid_audience_resolve_protocol_mapper" "audience_mapper" {
      realm_id  = keycloak_realm.realm.id
      client_id = keycloak_openid_client.openid_client.id
      name      = "my-audience-resolve-mapper"
    }
  2. Manage LDAP group mappers with keycloak_ldap_group_mapper

    main

    The keycloak_ldap_group_mapper resource allows you to create and manage group mappers for Keycloak users federated via LDAP. This mapper maps an LDAP user's groups from a specific Distinguished Name (DN) to Keycloak groups. If the groups do not already exist in Keycloak, the mapper will create them automatically.

    resource "keycloak_realm" "realm" {
      realm   = "my-realm"
      enabled = true
    }
    
    resource "keycloak_ldap_user_federation" "ldap_user_federation" {
      name     = "openldap"
      realm_id = keycloak_realm.realm.id
    
      username_ldap_attribute = "cn"
      rdn_ldap_attribute      = "cn"
      uuid_ldap_attribute     = "entryDN"
      user_object_classes     = [
        "simpleSecurityObject",
        "organizationalRole"
      ]
    
      connection_url  = "ldap://openldap"
      users_dn        = "dc=example,dc=org"
      bind_dn         = "cn=admin,dc=example,dc=org"
      bind_credential = "admin"
    }
    
    resource "keycloak_ldap_group_mapper" "ldap_group_mapper" {
      realm_id                = keycloak_realm.realm.id
      ldap_user_federation_id = keycloak_ldap_user_federation.ldap_user_federation.id
      name                    = "group-mapper"
    
      ldap_groups_dn                 = "dc=example,dc=org"
      group_name_ldap_attribute      = "cn"
      group_object_classes           = [
        "groupOfNames"
      ]
      membership_attribute_type      = "DN"
      membership_ldap_attribute      = "member"
      membership_user_ldap_attribute = "cn"
      memberof_ldap_attribute        = "memberOf"
    }
  3. Manage OpenID Audience Protocol Mappers

    main

    The keycloak_openid_audience_protocol_mapper resource allows you to add audiences to the aud claim within issued tokens. You can specify an audience as either a custom string or by mapping it to the ID of an existing client.

    Audience mappers can be attached to either a specific Client or a Client Scope.

    resource "keycloak_openid_audience_protocol_mapper" "audience_mapper" {
      realm_id                 = keycloak_realm.realm.id
      client_id                = keycloak_openid_client.openid_client.id
      name                     = "audience-mapper"
      included_custom_audience  = "foo"
    }
  4. Manage Keycloak users with keycloak_user

    main

    The keycloak_user resource allows for creating and managing Users within a Keycloak realm.

    Important Note: Creating users directly via this resource is generally not recommended for production environments. Instead, users should ideally be federated from external sources using user federation providers or identity providers. This resource is primarily intended for testing purposes (e.g., acceptance tests for keycloak_group).

    resource "keycloak_user" "user" {
      realm_id = keycloak_realm.realm.id
      username = "bob"
      enabled  = true
    
      email      = "bob@domain.com"
      first_name = "Bob"
      last_name  = "Bobson"
    }
  5. Manage Keycloak OpenID Connect clients with keycloak_openid_client

    main

    The keycloak_openid_client resource allows you to create and manage Keycloak clients that utilize the OpenID Connect (OIDC) protocol. These clients are typically applications that redirect users to Keycloak for authentication to leverage Single Sign-On (SSO) capabilities.

    resource "keycloak_realm" "realm" {
      realm   = "my-realm"
      enabled = true
    }
    
    resource "keycloak_openid_client" "openid_client" {
    	realm_id  = keycloak_realm.realm.id
    	client_id = "test-client"
    
    	name    = "test client"
    	enabled = true
    
    	access_type           = "CONFIDENTIAL"
    	standard_flow_enabled = true
    	valid_redirect_uris = [
    		"http://localhost:8080/openid-callback"
    	]
    
    	login_theme = "keycloak"
    
    	extra_config = {
    		"key1" = "value1"
    		"key2" = "value2"
    	}
    }
  6. Manage user property protocol mappers with keycloak_openid_user_property_protocol_mapper

    main

    The keycloak_openid_user_property_protocol_mapper resource allows you to map built-in Keycloak user properties (like email) to specific claims within an OpenID Connect token.

    Protocol mappers can be attached to a specific Client or to a Client Scope (which can then be shared across multiple clients). You must specify exactly one of client_id or client_scope_id.

    resource "keycloak_openid_user_property_protocol_mapper" "user_property_mapper" {
      realm_id  = keycloak_realm.realm.id
      client_id = keycloak_openid_client.openid_client.id
      name      = "user-property-mapper"
    
      user_property = "email"
      claim_name    = "email"
    }
  7. Manage username template importer identity provider mappers

    main

    The keycloak_user_template_importer_identity_provider_mapper resource allows you to create and manage mappers that use a template to map externally defined OIDC claims or SAML attributes to the username of an imported Keycloak user.

    Username Template Syntax

    Substitutions in the template argument are enclosed in ${}:

    • ${ALIAS}: References the provider alias.
    • ${CLAIM.<NAME>}: References an ID or Access token claim (e.g., ${CLAIM.sub} or ${CLAIM.email}).

    Keycloak 10+ Requirement

    If you are using Keycloak version 10 or higher, you must specify the syncMode within the extra_config argument.

    resource "keycloak_user_template_importer_identity_provider_mapper" "username_importer" {
      realm                   = keycloak_realm.realm.id
      name                    = "username-template-importer"
      identity_provider_alias = keycloak_oidc_identity_provider.oidc.alias
      template                = "$${ALIAS}.$${CLAIM.email}"
    
      # extra_config with syncMode is required in Keycloak 10+
      extra_config = {
        syncMode = "INHERIT"
      }
    }
  8. Manage SAML clients with keycloak_saml_client

    main

    The keycloak_saml_client resource allows you to create and manage Keycloak clients that utilize the SAML protocol. These clients are typically applications that redirect users to Keycloak for authentication to leverage Single Sign-On (SSO) capabilities.

    resource "keycloak_realm" "realm" {
      realm   = "my-realm"
      enabled = true
    }
    
    resource "keycloak_saml_client" "saml_client" {
      realm_id  = keycloak_realm.realm.id
      client_id = "saml-client"
      name      = "saml-client"
    
      sign_documents          = false
      sign_assertions         = true
      include_authn_statement = true
    
      signing_certificate = file("saml-cert.pem")
      signing_private_key = file("saml-key.pem")
    }
  9. Manage Keycloak Groups with keycloak_group

    main

    The keycloak_group resource allows you to create and manage Groups within a Keycloak realm. Groups provide a logical wrapper for users, allowing them to share attributes and roles, and enabling group membership to be mapped to claims.

    Important Note: Do not use this resource to manage groups that are federated from external data sources like LDAP or Active Directory.

    resource "keycloak_group" "parent_group" {
      realm_id = keycloak_realm.realm.id
      name     = "parent-group"
    }
    
    resource "keycloak_group" "child_group" {
      realm_id  = keycloak_realm.realm.id
      parent_id = keycloak_group.parent_group.id
      name      = "child-group"
    }
  10. Manage protocol mappers with keycloak_generic_protocol_mapper

    main

    The keycloak_generic_protocol_mapper resource allows you to create and manage protocol mappers for both openid-connect and saml clients in Keycloak.

    Use this resource in two scenarios:

    1. To configure a custom protocol mapper you have implemented.
    2. When the provider does not yet have a specific resource for a particular protocol mapper.

    Warning: Because this resource is generic, it is less user-friendly and more prone to configuration errors than specific mapper resources. Always prefer a specific mapper resource if one is available.

    resource "keycloak_generic_protocol_mapper" "saml_hardcode_attribute_mapper" {
      realm_id        = keycloak_realm.realm.id
      client_id       = keycloak_saml_client.saml_client.id
      name            = "test-mapper"
      protocol        = "saml"
      protocol_mapper = "saml-hardcode-attribute-mapper"
      config = {
        "attribute.name"       = "name"
        "attribute.nameformat" = "Basic"
        "attribute.value"      = "value"
        "friendly.name"        = "display name"
      }
    }
  11. Manage OpenID Full Name Protocol Mappers

    main

    The keycloak_openid_full_name_protocol_mapper resource allows you to map a user's first and last name to the OpenID Connect name claim in a token. You can attach these mappers to a specific client or to a client scope to share them across multiple clients.

    resource "keycloak_openid_full_name_protocol_mapper" "full_name_mapper" {
      realm_id  = keycloak_realm.realm.id
      client_id = keycloak_openid_client.openid_client.id
      name      = "full-name-mapper"
    }