bilal-fazlani / embedded-keycloak

Embedded keycloak server for jvm integration testing. (developed in scala)

GitHub

Embedded Keycloak

Embedded keycloak server for jvm integration testing. (developed in scala)

Maven Central Build Status Coverage Status Codacy Badge

Installation

libraryDependencies += "tech.bilal" %% "embedded-keycloak" % "<ADD_VERSION_HERE>"

Usage

val keycloak = new EmbeddedKeycloak(
      KeycloakData.fromConfig, // or directly: `KeycloakData(...)`
      Settings.default) // or customize: Settings(...)

val stopHandle = await(keycloak.startServerInBackground())

//do some testing here

stopHandle.stop()

Settings

the following settings options are available.

The default settings looks like this -

Settings(port: Int = 8081,
         host: String = "0.0.0.0",
         keycloakDirectory: String = "/tmp/embedded-keycloak/",
         cleanPreviousData: Boolean = true,
         alwaysDownload: Boolean = false,
         version: String = "4.6.0")

Keycloak Data

The test data can be provided in application.conf of test scope.

For example -

embedded-keycloak{
    adminUser {
      username = admin
      password = admin
    }
    realms = [{
      name = example-realm
      realmRoles = [super-admin]
      clients = [
        {
          name = some-server
          clientType = bearer-only
          clientRoles = [server-admin, server-user]
          authorizationEnabled = true
        },
        {
          name = some-client
        }
      ]
      users = [
        {
          username = user1
          password = abcd,
          realmRoles = [super-admin]
          firstName = john
        },
        {
          username = user2
          password = abcd,
          clientRoles = [{
            clientName = some-server
            roleName = server-user
          }]
      }]
    }]
}

Or the same data can be provided directly as shown below:

    val data = KeycloakData(
      adminUser = AdminUser("admin", "admin"),
      realms = Set(
        Realm(
          name = "example-realm",
          clients = Set(
            Client(
              name = "some-server",
              clientType = "bearer-only",
              clientRoles = Set("server-admin", "server-user"),
              authorizationEnabled = true
            ),
            Client(name = "some-client")
          ),
          users = Set(
            ApplicationUser(
              username = "user1",
              firstName = "john",
              password = "abcd",
              realmRoles = Set("super-admin")
            ),
            ApplicationUser(
              username = "user2",
              password = "abcd",
              clientRoles = Set(ClientRole("some-server", "server-user"))
            )
          ),
          realmRoles = Set("super-admin")
        ))
    )

Limitations

  • Does not support permissions
  • Only supports the configs shown in the config file
  • Does not support importing keycloak exported json file