-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update HttpClient behaviour to prevent client pool leaks
- Loading branch information
Showing
5 changed files
with
77 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,7 +13,7 @@ To be used on a Keycloak instance configured with with the [Keycloak Firebase Sc | |
## Download latest release | ||
|
||
``` | ||
curl -L https://github.com/SmartMoveSystems/firebase-keycloak-importer/releases/download/0.0.4/firebase-keycloak-importer-0.0.4.jar > firebase-keycloak-importer-0.0.4.jar | ||
curl -L https://github.com/SmartMoveSystems/firebase-keycloak-importer/releases/download/0.0.5/firebase-keycloak-importer-0.0.5.jar > firebase-keycloak-importer-0.0.5.jar | ||
``` | ||
|
||
## Usage | ||
|
@@ -36,13 +36,13 @@ Export your Firebase project's hash parameters to a JSON file with the format: | |
|
||
Run the following (the args `"--clientId", "--roles", "--clientSecret", "--default", "--debug"` are optional): | ||
|
||
The `default` argument specifies that the imported hash parameters will be the ones used for future users. | ||
If you are only importing from one Firebase project, you must set this argument to `true`. | ||
|
||
```bash | ||
java -jar build\libs\firebase-keycloak-importer-0.0.4.jar --usersFile example_users.json --hashParamsFile example_hash_config.json --adminUser [email protected] --adminPassword admin --realm smartmove --serverUrl http://localhost:8080/auth --default true --debug | ||
java -jar build\libs\firebase-keycloak-importer-0.0.5.jar --usersFile example_users.json --hashParamsFile example_hash_config.json --adminUser [email protected] --adminPassword admin --realm smartmove --serverUrl http://localhost:8080/auth --default true | ||
``` | ||
|
||
The `default` argument specifies that the imported hash parameters will be the ones used for future users. | ||
If you are only importing from one Firebase project, you must set this argument to `true`. | ||
|
||
The client with the specified `clientId` and all specified `roles` must already exist in your Keycloak configuration. | ||
|
||
## Importing from multiple projects | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
src/main/kotlin/com/smartmovesystems/keycloak/firebasemigrator/keycloak.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package com.smartmovesystems.keycloak.firebasemigrator | ||
|
||
import org.jboss.resteasy.client.jaxrs.ResteasyClient | ||
import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder | ||
import org.jboss.resteasy.client.jaxrs.ResteasyWebTarget | ||
import org.keycloak.OAuth2Constants | ||
import org.keycloak.admin.client.JacksonProvider | ||
import org.keycloak.admin.client.Keycloak | ||
import org.keycloak.admin.client.KeycloakBuilder | ||
import javax.ws.rs.client.ClientRequestContext | ||
import javax.ws.rs.client.ClientRequestFilter | ||
|
||
private val Keycloak.webTarget : ResteasyWebTarget | ||
get() { | ||
val targetField = this::class.java.getDeclaredField("target") | ||
targetField.isAccessible = true | ||
return targetField.get(this) as ResteasyWebTarget | ||
} | ||
|
||
/** | ||
* Making repeated calls to the RestEasyClient seems to eventually exhaust the client pool, regardless of size and | ||
* configuration. Therefore, request the server close connections aggressively to avoid hangs | ||
*/ | ||
private fun Keycloak.setCloseConnections() { | ||
this.webTarget.register(object : ClientRequestFilter { | ||
override fun filter(requestContext: ClientRequestContext?) { | ||
requestContext!!.headers.add("Connection", "close") | ||
} | ||
}) | ||
} | ||
|
||
private val resteasyClientBuilder: ResteasyClientBuilder by lazy { | ||
ResteasyClientBuilder() | ||
.register(JacksonProvider::class.java, 100) | ||
} | ||
|
||
private val restEasyClient: ResteasyClient by lazy { | ||
resteasyClientBuilder.useAsyncHttpEngine().build() | ||
} | ||
|
||
/** | ||
* Returns a logged-in Keycloak instance based on the provided arguments | ||
*/ | ||
fun getKcInstance(arguments: Arguments): Keycloak { | ||
val keycloak = KeycloakBuilder.builder() | ||
.serverUrl(arguments.serverUrl) | ||
.realm(arguments.realm) | ||
.grantType(OAuth2Constants.PASSWORD) | ||
.clientId("admin-cli") | ||
.clientSecret(arguments.clientSecret) | ||
.resteasyClient(restEasyClient) | ||
.username(arguments.adminUser) | ||
.password(arguments.adminPassword) | ||
.build() | ||
keycloak.setCloseConnections() | ||
return keycloak | ||
} |