Repository navigation
KeyCloak Class Loading #19189
brettlounsbury
started this conversation in
Ideas
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Hey folks,
I was recently attempting to get KeyCloak to talk to my AWS Aurora Postgres DB using IAM authentication. To do that, I am using AWS' Advanced JDBC Wrapper Driver. KeyCloak has implemented a custom classloader which does not load
java.sql.Driverclasses and this causes the AWS JDBC driver to be unable to load the delegated driver (because the driver isn't registered because it was never added). This issue is documented in Issue 13205.During my testing, I was expecting to be able to get IAM auth working by:
postgresso that KC knows what dialect to usesoftware.amazon.jdbc.Driver)jdbc:aws-wrapper:postgresql)Unfortunately this doesn't work because the AWS driver attempts to dynamically load the postgres driver via a
DriverManager.getDriver(driverUrl)call and because the class isn't loaded it is never registered.I was able to resolve the issue by removing the custom ClassLoader so that all JDBC drivers in the classpath are eagerly loaded. That is obviously not reasonable solution to the problem, but it did prove the issue was the custom class loader.
To resolve this, I propose the following changes:
Add a new property (
System.getProperty()) that flags whether or not to lazy load JDBC drivers. I'm tentatively calling thiskc.config.classloading.lazy-load-db-drivers.Update the
KeyCloakClassLoaderto take a boolean argument in its constructor for that flag, and then update the if statement within thegetResourcescall to something like this:Update the
Environmentclass to have a new methodpublic static boolean isDbDriverLazyLoadingEnabled(boolean defaultValue)that loads the property from the System properties. This method takes a default value so that the default value logic lives where the object is created instead of in the property loading class (similar to how System.getProperty takes a default return value).Update the
KeycloakMainclass to fetch the property and provide a default value oftrueif the property is not set so that the default behavior is the current behavior and provide that to theKeyCloakClassLoaderconstructor.This seems like a fairly straight forward fix (I have the code running on my machine) but I want to make sure this is the best way to do it. Given that the classes being modified are the
KeycloakMainandKeycloakClassLoader, making this a system property and loading it through theEnvironmentclass seems to be the most consistent way (sinceKeycloakMainalready does this)All reactions