dasniko / testcontainers-keycloak

A Testcontainer implementation for Keycloak IAM & SSO.
Apache License 2.0
327 stars 50 forks source link

Remote debugging Keycloak #117

Closed dasniko closed 8 months ago

dasniko commented 10 months ago

Discussed in https://github.com/dasniko/testcontainers-keycloak/discussions/116

Originally posted by **martinleim** September 12, 2023 I have recently started to use Keycloak Testcontainer for testing a custom Keycloak extension. Registering the extension and starting Keycloak worked like a charm. When encountering the first bug in my extension, I searched for a way to **remotely debug** within Keycloak and was surprised that I could not find any mention of it within the documentation or the issues and discussion here. After fiddling around with it for some time, I found a solution that involves extending the source code to add a new option `withDebug` that is working for me. **I am wondering if there is a simpler solution for that. If there is not, is there any interest in bringing this into the project?** Please see https://github.com/dasniko/testcontainers-keycloak/compare/main...martinleim:testcontainers-keycloak-debug:main for the change set. It can be used in the following way: 1. Enable debugging (without suspending) on a random port. This is a bit cumbersome since you have to find the mapped port and modify your remote debug settings before each run. On the other hand, this probably won't get in the way on a shared environment like a CI server. ```java @Container private static final KeycloakContainer keycloak = new KeycloakContainer() // ... .withDebug(); ``` 2. Enable debugging on a fixed port. The second parameter controls if the Java process should suspend until the debugger is connected. This allows you to use a static debug port, but is not suitable to run on a CI server. Also, the suspend option requires you to connect the remote debugger two times (as explained in https://github.com/keycloak/keycloak/discussions/12679#discussioncomment-3307999) ```java @Container private static final KeycloakContainer keycloak = new KeycloakContainer() // ... .withDebug(8787, false);
dasniko commented 10 months ago

Hi @martinleim, thanks for coming up with this! 👍 Do you see any chance to provide an automated test for this? I didn't have an idea, yet, but also haven't thought about it that much.

martinleim commented 10 months ago

Hi @dasniko ,

I think the "proper" test would be to somehow open a connection using the Java Debug Wire Protocol (JDWP). I have no experience in this, but this article from Baeldung might give some hints. If this works, it could also be used to test the suspend feature - although I fear that might become complicated.

A poor man's solution would be to check if we can open a plain TCP connection to the exposed port without checking for the procotol in any way. However, this would not allow testing for the suspend mode.

I'll give it a try and see what I can come up with.

martinleim commented 10 months ago

Hi @dasniko ,

I decided to go for the simple TCP connection and created a PR with the changes and the tests.

dasniko commented 9 months ago

Thanks, @martinleim Opening a TCP connection should work for me. I'll have a look at it during next week.

martinleim commented 9 months ago

Hi @dasniko, did you have the chance to look at my PR yet?

dasniko commented 9 months ago

Unfortunately not in depth, I'm pretty busy atm. But generally it seems to look good, from a high perspective.

dasniko commented 8 months ago

Thanks for your work, @martinleim ! 👍 It's really appreciated! 🙏

martinleim commented 8 months ago

Happy to help :-) And thank you for creating this project! Do you plan for a release in the near future?

dasniko commented 8 months ago

Hopefully yes. There's still another issue which needs to be solved in some way. Let's give it one more week, then we'll see.

raoulvdberge commented 8 months ago

@martinleim Thank you for this. I needed this and didn't know it was possible :)