mayope / keycloakmigration

Manage your Keycloak configuration with code.
https://mayope.net
MIT License
108 stars 22 forks source link

Output HTTP status code as well when logging KeycloakApiException #55

Closed IngoStrauch2020 closed 1 year ago

IngoStrauch2020 commented 1 year ago

It's maybe just a nice to have, but when e.g. the admin user executing a migration does not have the "manage-authorization" role and tries to use the "addFlow" migration, the error message is just

Error occurred while migrating: 56_add_new_realm_idp_dev
de.klg71.keycloakmigration.keycloakapi.KeycloakApiException: {"error":"unknown_error"}

Calling the REST API with some other tool would show that the HTTP status code is 403 Forbidden, which is the relevant information here.

Maybe the log could be changed like this

Error occurred while migrating: 56_add_new_realm_idp_dev
de.klg71.keycloakmigration.keycloakapi.KeycloakApiException: code=403, response={"error":"unknown_error"}
IngoStrauch2020 commented 1 year ago

I could imagine two easy ways to do it.

a) Change the one relevant code line to pass the final message

fun Response.extractLocationUUID(): UUID {
    if (!isSuccessful()) {
        throw KeycloakApiException("code=${this.status()}, response=${this.body().asReader().readText()})
    }
    ...
}

b) add a second constructor to the KeycloakApiException to centralize the message generation for future use

class KeycloakApiException : RuntimeException {
    constructor(message: String): super(message)
    constructor(code: Int, message: String): super("code=$code, response=$message")
}

plus

fun Response.extractLocationUUID(): UUID {
    if (!isSuccessful()) {
        throw KeycloakApiException(this.status(), this.body().asReader().readText())
    }
    ...
}
klg71 commented 1 year ago

Hey Ingo, sry I forgot about this issue and only implemented it halfway through. I will get to it this week :)

klg71 commented 1 year ago

Released with version 0.2.46 :) Maybe you could verify this works

IngoStrauch2020 commented 1 year ago

Sorry, better late than never. It works, thanks!