8000 [KEYCLOAK-12305] [Testsuite] Check LDAP federated user (in)valid login(s) using various authentication methods, bind credential types, and connection encryption mechanisms by iankko · Pull Request #7049 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content

[KEYCLOAK-12305] [Testsuite] Check LDAP federated user (in)valid login(s) using various authentication methods, bind credential types, and connection encryption mechanisms - #7049

Merged
mposolda merged 1 commit into
keycloak:masterfrom
iankko:KEYCLOAK-12305
Jun 2, 2020

Conversation

@iankko
@iankko iankko commented May 7, 2020 •
Copy link
Copy Markdown
Contributor
[KEYCLOAK-12305] [Testsuite] Check LDAP federated user (in)valid
login(s) using various authentication methods, bind credential
types, and connection encryption mechanisms

The tests cover various possible combinations of the following:
* Authentication method: Anonymous or Simple (default),
* Bind credential: Secret (default) or Vault,
* Connection encryption: Plaintext (default), SSL, or startTLS

Also, ignore the StartTLS LDAP tests for now till KEYCLOAK-14343
& KEYCLOAK-14354 are corrected (due these issues they aren't
working with auth server Wildfly). They will be re-enabled later
via KEYCLOAK-14358 once possible

Signed-off-by: Jan Lieskovsky <jlieskov@redhat.com>

@iankko
iankko commented May 7, 2020
Copy link
Copy Markdown
Contributor Author

@tsaarni @mposolda @mhajas @pdrozd @stianst PTAL (once got a chance)

Thanks! Jan

@iankko
iankko commented May 7, 2020
Copy link
Copy Markdown
Contributor Author

Brief testing results on my machine (prior Travis has had chance to complete):

  • With OpenJDK 1.8 (java-1.8.0-openjdk-1.8.0.242.b08-0.fc31.x86_64):
...
[INFO] Tests run: 12, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 80.639 s - in org.keycloak.testsuite.federation.ldap.LDAPUserLoginTest
16:05:27,704 INFO  [org.keycloak.testsuite.arquillian.undertow.KeycloakOnUndertow] Stopping auth server.
16:05:27,713 INFO  [io.undertow] stopping server: Undertow - 2.1.0.Final
16:05:27,739 INFO  [io.undertow] stopping server: Undertow - 2.1.0.Final
[INFO] 
[INFO] Results:
[INFO] 
[INFO] Tests run: 12, Failures: 0, Errors: 0, Skipped: 0
[INFO] 
[INFO] 
...
  • With OpenJDK 11 (java-11-openjdk-11.0.7.10-0.fc31.x86_64):
...
16:13:15,898 DEBUG [org.keycloak.connections.jpa.DefaultJpaConnectionProviderFactory] Database is up-to-date
16:13:16,588 INFO  [org.keycloak.testsuite.arquillian.undertow.KeycloakOnUndertow] Auth server started in 1373ms on http://0.0.0.0:8180/auth
[INFO] Tests run: 12, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 78.815 s - in org.keycloak.testsuite.federation.ldap.LDAPUserLoginTest
16:13:16,652 INFO  [org.keycloak.testsuite.arquillian.undertow.KeycloakOnUndertow] Stopping auth server.
16:13:16,659 INFO  [io.undertow] stopping server: Undertow - 2.1.0.Final
16:13:16,668 INFO  [io.undertow] stopping server: Undertow - 2.1.0.Final
[INFO] 
[INFO] Results:
[INFO] 
[INFO] Tests run: 12, Failures: 0, Errors: 0, Skipped: 0
[INFO] 
[INFO] 
...

@mposolda mposolda self-assigned this May 7, 2020
@iankko
iankko commented May 12, 2020
Copy link
Copy Markdown
Contributor Author

JFTR, verified the reported Travis failure. Seems to be a red-herring, caused by Ubuntu ca-certificates-java package bug. Doesn' 8000 t happen with Ubuntu 16.04 openjdk-8-jdk-8u252-b09-1~16.04 any more, where the aforementioned bug is already fixed (/etc/ssl/certs/java/cacerts is generated properly / is not empty post VM install).

@mposolda mposolda left a comment •
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added few minor inline comments.

However it seems that most major problem would be to fix the startTLS tests to reliably work in travis and on all environments (including OpenJDK11, Ubuntu etc).

Few possibilities:

  • Add some "Assume" checks to the StartTLS tests and ignore them for unstable environments (EG. OpenJDK 11). We need to make sure that tests run at least in the pipeline during run of LDAP tests. If they don't run on travis, it is fine IMO.
  • Another idea would be to add some "sleep" somewhere after start of LDAP server (maybe server just needs some more time for initialization). This just comes to my mind when @tsaarni mentioned, that he doesn't see the issue whe he is debugging. Debugging usually takes some time, so it is possible that LDAP server had some additional time to initialize etc. If it is additional 5 seconds after server startup, it helps to avoid the issue. More like 5 is not ideal as it negatively impacts the testsuite time
  • Something better to make sure that test is stable on all environments and never hangs and doesn't even need to wait. But I don't know if something like this exists :-)

@tsaarni
tsaarni commented May 12, 2020
Copy link
Copy Markdown
Contributor

I tried uplifting Apache DS.

StartTLS hangs seem to disappear, it now works for me with JDK11 as well as JDK8!

Though while uplifting ApacheDS I broke two other LDAP tests. See here for the errors. These seem completely unrelated to StartTLS and are maybe related to a change I needed to do in RangedAttributeInterceptor.java to get the embedded-ldap server to compile. I must confess I don't really know what I'm doing so I'm not sure if I will figure out this 😊.

Here is the patch I have, done on top of this pull request Nordix@6bf7f43

The easiest way I figured out to uplift was by removing the file and in-memory DirectoryServerFactories since it seemed to me that only in-memory was ever used. I followed this example. I think this should be in-memory database, at least ApacheDS comment for AvlPartition says "An XDBM Partition backed by in memory AVL Trees".

@tsaarni
tsaarni commented May 13, 2020 •
Copy link
Copy Markdown
Contributor

Small update on my previous comment:

The failures of test08_ldapOnlyGroupMappingsRanged and test08_ldapOnlyGroupMappingsRanged were because I was not installing the RangedAttributeInterceptor at all, like InMemoryDirectoryServiceFactory did previously.

I did that now in Nordix@637959c but that apparently breaks all searches. I'm trying to figure out what is going on.

@mposolda
Copy link
Copy Markdown
Contributor

@tsaarni We use in-memory when running LDAP tests in the testsuite. However we use file when using LDAPEmbeddedServer and KerberosEmbeddedServer as standalone classes (both of them have main method). This helps to ensure that restart of LDAPEmbeddedServer doesn't mean that you loose your LDAP data, but they are persistent among server restarts. It will be good to still have both available.

@iankko
iankko commented May 13, 2020
Copy link
Copy Markdown
Contributor Author

@tsaarni

Hey Tero,

I tried uplifting Apache DS.

+100 if it would be feasible to overcome the startTLS JDK11 hang with Apache DS LdapServer version bump. With the currently used Apache DS version was yesterday able to reproduce the hang even with JDK 1.8 on Xenial Xerus:

iankko@ubuntu:~$ apt show openjdk-8-jdk
Package: openjdk-8-jdk
Version: 8u252-b09-1~16.04
...

Interestingly, it doesn't hang when not using some debug option at all, or when debugging just SSL (-Djavax.net.debug=ssl), but hangs pretty regularly even with 1.8, when using -Djavax.net.debug=all setting. Yet, when using -Djavax.net.debug=ssl setting (and running all available tests in a sequence, not just the startTLS ones), the log contains message like before the hang:

XNIO-3 I/O-3, fatal error: 80: Inbound closed before receiving peer's close_notify: possible truncation attack?

Per inspection of related OpenJDK bugs, socketRead0 can hang during SSL with misbehaving server, or under higher load.

So AFAICT, there definitely seems to be something wonky with the current Apache DS version, we use, since (maybe depending on the server load?) sometimes the tests run without issues, other time they end up with "possible truncation attack?" and corresponding thread, performing the SSL/TLS (re)negotiation hangs indefinitely.

StartTLS hangs seem to disappear, it now works for me with JDK11 as well as JDK8!

TL;DR: So from the above, I see two ways how to address this: 1) either update the Apache DS version, or 2) update the tests (add some sleeps) so the server doesn't end up with high load while processing the StartTLS requests. But given the latter is just a workaround (might be hard to achieve in real testing CI scenario, when there are multiple tests run in parallel anyway), I would prefer the Apache DS version bump.

Though while uplifting ApacheDS I broke two other LDAP tests. See here for the errors. These seem completely unrelated to StartTLS and are maybe related to a change I needed to do in RangedAttributeInterceptor.java to get the embedded-ldap server to compile. I must confess I don't really know what I'm doing so I'm not sure if I will figure out this blush.

Here is the patch I have, done on top of this pull request Nordix@6bf7f43

Woot, nice. Thx! Will try that. Do you want / need some help to get the Apache DS version bump fully working with the other existing LDAP tests too? (need to adjust the current PR to address Marek's comments yet, but for now I would assume there's no startTLS subcase hang with JDK11 anymore, and could help you later with getting those in-memory / file store based DSFs working with the bumped server, if needed. Those other failing tests are a black-box for me now, but assuming as far as we are able to provide those two DSFs to them, they require, we should be good to go.

The easiest way I figured out to uplift was by removing the file and in-memory DirectoryServerFactories since it seemed to me that only in-memory was ever used. I followed this example. I think this should be in-memory database, at least ApacheDS comment for AvlPartition says "An XDBM Partition backed by in memory AVL Trees".

See Marek's reply below for clarification, why we need that file store based DSF, so that one would need to be preserved, if the server bump should work.

@iankko
iankko commented May 13, 2020
Copy link
Copy Markdown
Contributor Author

I've added few minor inline comments.

However it seems that most major problem would be to fix the startTLS tests to reliably work in travis and on all environments (including OpenJDK11, Ubuntu etc).

Few possibilities:

* Add some "Assume" checks to the StartTLS tests and ignore them for unstable environments (EG. OpenJDK 11). We need to make sure that tests run at least in the pipeline during run of LDAP tests. If they don't run on travis, it is fine IMO.

* Another idea would be to add some "sleep" somewhere after start of LDAP server (maybe server just needs some more time for initialization). This just comes to my mind when @tsaarni mentioned, that he doesn't see the issue whe he is debugging. Debugging usually takes some time, so it is possible that LDAP server had some additional time to initialize etc. If it is additional 5 seconds after server startup, it helps to avoid the issue. More like 5 is not ideal as it negatively impacts the testsuite time

* Something better to make sure that test is stable on all environments and never hangs and doesn't even need to wait. But I don't know if something like this exists :-)

Right, thanks Marek! @ the end didn't actually touch (try to fix) the StartTLS hang issue with JDK11 (solely present on Ubuntu?) issue. since Tero is currently working @ separate PR (JIRA ID to come yet?) to get the Apache DS version bumped to latest upstream version, not prone to this problem any more. @tsaarni TYVM for taking care for this one!

@iankko
iankko requested a review from mposolda May 13, 2020 15:00
@tsaarni
tsaarni commented May 13, 2020
Copy link
Copy Markdown
Contributor

I've now submitted KEYCLOAK-14162 and #7069. Hopefully this fixes the StartTLS problem!

@tsaarni
tsaarni commented May 14, 2020
Copy link
Copy Markdown
Contributor

@iankko I have one proposal regarding the test cases: there is a method ldapServer.setConfidentialityRequired(bool) (doc) which could be set to true in createLdapServer() startTLS branch. I think it could provide an additional check that Keycloak is not accidentally missing to upgrade the LDAP connection to TLS, i.e. missing the StartTLS part and running operations over insecure channel without us realizing that.

@iankko iankko changed the title [KEYCLOAK-12305] [Testsuite] Check LDAP federated user (in)valid login(s) using various authentication methods, bind credential types, and connection encryption mechanisms [WIP DNM] [KEYCLOAK-12305] [Testsuite] Check LDAP federated user (in)valid login(s) using various authentication methods, bind credential types, and connection encryption mechanisms May 14, 2020
@iankko
iankko commented May 14, 2020
Copy link
Copy Markdown
Contributor Author

@iankko I have one proposal regarding the test cases: there is a method ldapServer.setConfidentialityRequired(bool) (doc) which could be set to true in createLdapServer() startTLS branch. I think it could provide an additional check that Keycloak is not accidentally missing to upgrade the LDAP connection to TLS, i.e. missing the StartTLS part and running operations over insecure channel without us realizing that.

@tsaarni Right, good catch! Will add it, thanks

@iankko iankko changed the title [WIP DNM] [KEYCLOAK-12305] [Testsuite] Check LDAP federated user (in)valid login(s) using various authentication methods, bind credential types, and connection encryption mechanisms [KEYCLOAK-12305] [Testsuite] Check LDAP federated user (in)valid login(s) using various authentication methods, bind credential types, and connection encryption mechanisms May 14, 2020
@iankko
iankko commented May 14, 2020 •
Copy link
Copy Markdown
Contributor Author

@tsaarni @mposolda Ready for (re)review. Fixed conflict, rebased to master, added that confidentiality requirement for both startTLS & LDAPs cases.

Verified, it works without issues (the previous hang disappeared) for all possible combinations of the following:

  • Using no debug, -Djavax.net.debug=ssl, or -Djavax.net.debug=all options with the test(s),
  • Running the test(s) on both JDK 1.8 & 11 on both Fedora 31 and Ubuntu 16.04

@mposolda mposolda left a comment
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see there is lots of travis failures in LDAP tests and also UserFederationConnectionTest. I think this is probably caused by the "setConfidentialityRequired(true)" when using StartTLS or SSL support.

IMO it will be good to remove this change and still allow connection over "ldap" even if support for "ldaps" or StartTLS is enabled. This is what I also mentioned in the inline comment.

The alternative could be to add another configuration option for the LDAPEmbeddedServer like "confidentialityRequired" and explicitly enable this just for the StartTLS tests? However by default this "confidentialityRequired" should be false IMO.

@iankko
iankko requested a review from mposolda May 15, 2020 14:38
@mposolda mposolda left a comment
10000 Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still requires some work as LDAPUserLoginTest is still failing.

Travis is using Keycloak on Wildfly. Is it possible that it works for you when testing with Keycloak on Undertow and it is failing on Travis with Keycloak on Wildfly. Just a quick hint, not really sure if this is the reason or not...

@tsaarni
tsaarni commented May 17, 2020 •
Copy link
Copy Markdown
Contributor

I think it fails because truststore is not configured when running Wildfly.

Creating Elytron default SSL context fixed the problem for me (see diff below), though I'm not sure if this is the optimal way to do this? Also, not sure if it would be more appropriate to create new .cli file instead of adding this to keycloak-server-subsystem.cli.

diff --git a/testsuite/integration-arquillian/servers/auth-server/jboss/common/jboss-cli/keycloak-server-subsystem.cli b/testsuite/integration-arquillian/servers/auth-server/jboss/common/jboss-cli/keycloak-server-subsystem.cli
index 3362d7b189..48a5d73457 100644
--- a/testsuite/integration-arquillian/servers/auth-server/jboss/common/jboss-cli/keycloak-server-subsystem.cli
+++ b/testsuite/integration-arquillian/servers/auth-server/jboss/common/jboss-cli/keycloak-server-subsystem.cli
@@ -8,6 +8,12 @@ echo ** Adding truststore spi**
     hostname-verification-policy => "WILDCARD", \
     disabled => "false"})

+echo ** Adding truststore for default SSL context e.g. for LDAP client **
+/subsystem=elytron/key-store=default-trust-store:add(path="${auth.server.truststore:${jboss.home.dir}/standalone/configuration/keycloak.truststore}",credential-reference={clear-text="${auth.server.truststore.password:secret}"})
+/subsystem=elytron/trust-manager=default-trust-manager:add(key-store=default-trust-store,algorithm="SunX509")
+/subsystem=elytron/client-ssl-context=default-ssl-context:add(trust-manager=default-trust-manager)
+/subsystem=elytron:write-attribute(name=default-ssl-context, value=default-ssl-context)
+
 echo ** Adding login-protocol spi **
 /subsystem=keycloak-server/spi=login-protocol/:add
 /subsystem=keycloak-server/spi=login-protocol/provider=saml/:add(enabled=true,properties={knownProtocols => "[\"http=${auth.server.http.port}\",\"https=${auth.server.https.port}\"]"})

@mposolda
Copy link
Copy Markdown
Contributor

@tsaarni Hmm... AFAIK the truststore is set and it is used for example for X509 related Keycloak tests . But as you pointed, I also assume the problem here is somewhere around truststore setup/configuration.

@tsaarni
tsaarni commented May 18, 2020
Copy link
Copy Markdown
Contributor

@mposolda I found the problem by looking at Wireshark traffic capture, Keycloak rejected the TLS server with an alert about unknown CA. I tried to find out where in the code the truststore could be configured but I was not able to find it yet. That's when I ended up trying with the above diff instead.

@tsaarni
tsaarni commented May 18, 2020
Copy link
Copy Markdown
Contributor

@mposolda One question: are the other X509 tests using Keycloak subsystem specific Elytron truststore? LDAP client within Keycloak is not Elytron aware and requires configuration of default-ssl-context.

@mposolda
Copy link
Copy Markdown
Contributor

@tsaarni AFAIR the X509 tests doesn't use the elytron. I recall the truststore is somehow copied to the wildfly distribution during preparation of the auth-server-wildfly server for the testsuite. Then the truststore is set either through the truststore SPI or through the system properties.

I see the properties auth.server.truststore and auth.server.truststore.password used in arquillian.xml during startup of auth-server-wildfly. So possibly the certificate needs to be added to the truststore pointed from here?

@tsaarni
tsaarni commented May 18, 2020 •
Copy link
Copy Markdown
Contributor

@mposolda the file keycloak.truststore is already in the directory pointed by auth.server.truststore and it is configured via truststore SPI here

file => "${auth.server.truststore:${jboss.home.dir}/standalone/configuration/keycloak.truststore}", \

but it still did not work. I wonder how does the truststore SPI interact with something like the LDAP client in javax.naming.ldap? The code in federation/ldap/src/main/java/org/keycloak/storage/ldap/ does not explicitly seem to set up SSLContext or trust manager.

@iankko
iankko commented May 18, 2020
Copy link
Copy Markdown
Contributor Author

@tsaarni

@mposolda the file keycloak.truststore is already in the directory pointed by auth.server.truststore and it is configured via truststore SPI here

file => "${auth.server.truststore:${jboss.home.dir}/standalone/configuration/keycloak.truststore}", \

but it still did not work. I wonder how does the truststore SPI interact with something like the LDAP client in javax.naming.ldap?

But is it the same file (truststore with the same content)? Verbose testing prints for me the tests are set to use the keystore under: ./testsuite/integration-arquillian/tests/base/target/dependency/keystore/keycloak.jks (unsure yet, if it's autogenerated by the embedded ldap server, or where it comes from)

Maybe it would be enough just to unify the paths (make the embedded ldap server use the Keycloak one), IDK, need to try it

@mposolda
Copy link
Copy Markdown
Contributor

@iankko I've just recalled that we already have some existing tests, which allows Keycloak server to connect to ApacheDS with the usage of SSL (ldaps URL). The test is UserFederationConnectionTest.testLdapConnectionsSsl() . So it means that truststore is already correctly set. It is just not used for the "StartTLS" case, but it is only used for the "SSL" case.

I suppose the reason can be the "Use Truststore SPI " switch of the LDAP provider. By default, it has value "Only for ldaps" . So maybe if you switch this to "Always" for the StartTLS tests, it will help?

@iankko
iankko commented May 19, 2020
Copy link
Copy Markdown
Contributor Author

@iankko I've just recalled that we already have some existing tests, which allows Keycloak server to connect to ApacheDS with the usage of SSL (ldaps URL). The test is UserFederationConnectionTest.testLdapConnectionsSsl() . So it means that truststore is already correctly set. It is just not used for the "StartTLS" case, but it is only used for the "SSL" case.

Yes, AFAICT (based on the testing) the truststore seems to be correctly set (it even works for SSL tests, just the StartTLS ones return LDAP Query failed / Caused by: javax.naming.AuthenticationException: Could not negotiate TLS). It works correctly, because when I change the location to some other keycloak.truststore it prints Couldn't find valid certification path to the requested target... (this doesn't happen witth the default auth server Wildfly truststore setting). So it seems it works (but not just for StartTLS yet).

I suppose the reason can be the "Use Truststore SPI " switch of the LDAP provider. By default, it has value "Only for ldaps" . So maybe if you switch this to "Always" for the StartTLS tests, it will help?

Will check this yet, thanks

@iankko
iankko commented May 20, 2020
Copy link
Copy Markdown
Contributor Author

@iankko I've just recalled that we already have some existing tests, which allows Keycloak server to connect to ApacheDS with the usage of SSL (ldaps URL). The test is UserFederationConnectionTest.testLdapConnectionsSsl() . So it means that truststore is already correctly set. It is just not used for the "StartTLS" case, but it is only used for the "SSL" case.

I suppose the reason can be the "Use Truststore SPI " switch of the LDAP provider. By default, it has value "Only for ldaps" . So maybe if you switch this to "Always" for the StartTLS tests, it will help?

@mposolda JFTR, having Use Truststore SPI changed from Only for ldaps to Always the error changed to:

09:26:04,312 ERROR [org.keycloak.storage.ldap.idm.store.ldap.LDAPContextManager] (default task-1) Could not negotiate TLS: javax.naming.CommunicationException: Unsupported record version Unknown-79.2 [Root exception is javax.net.ssl.SSLException: Unsupported record version Unknown-79.2]
...

Browsing the internet for the explanation / reason of the failure revealed, this might be caused by unsupported client certificate. Digging further (wondering if this might be caused by invalid hostname verification in the certificate or something else yet).

@tsaarni
tsaarni commented May 26, 2020
Copy link
Copy Markdown
Contributor

When I set useTruststoreSpi==always and check Wireshark, I do not see LDAP + StartTLS request but instead I see failed TLS handshake being executed towards port 389.

I'm trying to learn more about this and I think I may have found how Truststore SPI interacts with the LDAP client code in Java:

env.put("java.naming.ldap.factory.socket", "org.keycloak.truststore.SSLSocketFactory");

Does this mean that LDAP client will forcibly get TLS connection, regardless of connection URL being ldap://?

@mposolda
Copy link
Copy Markdown
Contributor

@tsaarni Hi, just small update that @iankko is on PTO for this week. So if you are eventually able to continue with his work and fix the failing test, it will be nice.

If you manage to figure this by the end of this week, then feel free to comment here. It will be fine if you manage to send PR with @iankko's commit for this PR and then possibly another commit of yours, which will fix the test.

Just an idea in case if you have some spare time to look at this :) Thanks for your help!

@tsaarni
tsaarni commented May 28, 2020
Copy link
Copy Markdown
Contributor

I could not think any easy way to make truststore SPI to work with Java LDAP client.

As I wrote previously useTruststoreSpi==always causes java.naming.ldap.factory.socket to be set to org.keycloak.truststore.SSLSocketFactory which to me seems to break StartTLS implementation in Java LDAP client. The LDAP client should start with cleartext TCP connection and then upgrade to TLS by issuing the LDAP StartTLS extension. But to me it seems that after replacing the socket factory, this will not happen. Instead it will directly attempt to establish a TLS connection to port LDAP port 389 which of course fails since the server expects to start with plain LDAP.

I found alternatives though:

As I wrote previously I could use Elytron to define default SSL context with the truststore. Java LDAP client picks up that also in case of StartTLS.

It also works simply by setting default truststore:

diff --git a/testsuite/integration-arquillian/tests/base/src/test/resources/arquillian.xml b/testsuite/integration-arquillian/tests/base/src/test/resources/arquillian.xml
index f56f9bed90..fc07ff659e 100644
--- a/testsuite/integration-arquillian/tests/base/src/test/resources/arquillian.xml
+++ b/testsuite/integration-arquillian/tests/base/src/test/resources/arquillian.xml
@@ -141,6 +141,8 @@
                 -Dauth.server.truststore=${auth.server.truststore}
                 -Dauth.server.truststore.password=${auth.server.truststore.password}
                 -Dauth.server.db.host=${auth.server.db.host}
+                -Djavax.net.ssl.trustStore=${auth.server.truststore}
+                -Djavax.net.ssl.trustStorePassword=${auth.server.truststore.password}
             </property>
             <property name="javaVmArguments">
                 ${auth.server.jboss.jvm.debug.args}

though this does not work exactly like this: for some reason variables are not expanding correctly and I get -Djavax.net.ssl.trustStore=${jboss.home.dir}/standalone/configuration/keycloak.truststore which is not valid path. If I set the full path, manually expanding ${jboss.home.dir}, then it works again.

@tsaarni
tsaarni commented May 29, 2020 •
Copy link
Copy Markdown
Contributor

@mposolda I might have an idea how to use Truststore SPI for LDAP StartTLS:

  1. do not set java.naming.ldap.factory.socket. This will allow cleartext LDAP connection to proceed
  2. pass the SSL socket factory from truststore SPI when calling StartTlsResponse.negotiate()
  3. now the correctly initialized trustmanager gets used by the LDAP client when it upgrades the cleartext LDAP connection to TLS

I have a work-in-progress implementation here Nordix@6d05d65. However this this does not work yet, and I would appreciate some help:

I need to call org.keycloak.truststore.SSLSocketFactory.getDefault() for step (2) from the LDAP provider code. This causes cannot find symbol unknown symbol but when I add dependency towards keycloak-services module for LDAP provider it seems to cause a cyclic dependency error between keycloak-ldap-federation and keycloak-services modules.

How would I go to solve this?

@mposolda
Copy link
Copy Markdown
Contributor

@tsaarni Thanks for the update!

In high-level view, it will be good to do anything, which will help to:

  • Ensure the StartTLS tests work as expected with ApacheDS :)
  • There won't be a regression in the StartTLS feature itself, which will prevent it to work with some other LDAP servers

Regarding the "Always Use Truststore" switch, it was just an idea from me that failing tests have something to do with this switch. But it is possible that the switch "Always Use Truststore" is not the way to go and it rather doesn't work at all when you use StartTLS. So not sure if it makes sense to spend time to have things working with this switch? I rather leave to you, but hopefully it is a way to have StartTLS working as expected with ApacheDS in the testsuite...

@mposolda
mposolda commented May 29, 2020 •
Copy link
Copy Markdown
Contributor

@tsaarni @iankko Will it be helpful to do this in stages?

  • In stage 1, this @iankko PR will be updated to have @ignore 10BC8 annotation on the StartTLS tests and stating they doesn't work yet. We will merge this PR and create follow-up JIRA for make StartTLS tests to work
  • In stage 2, another PR will be sent to focus just on StartTLS tests.

This will help to have at least some tests working in latest Keycloak master. Then we can follow with StartTLS as another step.

In general, using smaller PRs is usually better and also it will help with the ability to be done by different people (So @iankko can finish his work ASAP and then @tsaarni can follow with the StartTLS tests working).

WDYT?

@tsaarni
tsaarni commented May 29, 2020
Copy link
Copy Markdown
Contributor

At least currently I feel pretty convinced that the test is failing because of interactions between LDAP provider, Truststore SPI and Java LDAP client :-) It think it works with embedded undertow because truststore SPI is disabled in that case.

I interpreted that always flag is supposed to work also with LDAP + StartTLS? So it would make sense to spent some time. Currently I think it can be made to work, but I also wonder, has it been working? Is it really possible that people just used ldapsonly and provided their truststore by other means?

On the other hand: personally I'm more interested in Elytron since I understood it should be able to hot-reload certificates when they are rotated, without needing to do any restarts. That feature alone would make other alternatives quite obsolete in my mind.

Anyways, we can do this in stages like @mposolda proposes.

@mposolda
mposolda commented Jun 1, 2020
Copy link
Copy Markdown
Contributor

@tsaarni @iankko +1 to do that in stages. So if you both agree, we can just keep this @iankko's PR with the StartTLS to have Ignored annotations and create separate JIRA(s) to fix them. WDYT?

Regarding the LDAP Provider, Truststore SPI and Java LDAP client - it is possible that this combination is broken as you pointed. So it is possible that you will need to fix the bug if you want to make this working. However maybe as first step, you can try to have StartTLS tests working with the default setup of the "Use Truststore SPI" flag - which is with the value "Only for ldaps" ? Then to have it working with the "Always" can be another step?

Just to explain a bit more, the "only for ldaps" is the default value of the "Use Truststore SPI" flag. This value means that LDAP provider should try to use Truststore SPI just in case that "ldaps" connection is made. This is the default value for the "Use Truststore SPI" flag and it is the default value for both Keycloak on Wildfly and Keycloak on Undertow. With the StartTLS, the connection to LDAP still starts with "ldap" and hence the Truststore SPI shouldn't be used in this case - this apply for both Wildfly and Undertow. So in this case, the used truststore is not driven by Truststore SPI, but instead it is driven by system properties "javax.net.ssl.truststore" and "javax.net.ssl.truststorePassword" . So if it works on undertow and doesn't work on Wildfly, then probably something doesn't work as expected in the setup of those system properties. Again, just a hint, I am not 100% sure about that... :-)

@iankko
iankko commented Jun 1, 2020
Copy link
Copy Markdown
Contributor Author

@tsaarni @mposolda

At least currently I feel pretty convinced that the test is failing because of interactions between LDAP provider, Truststore SPI and Java LDAP client :-)

AFAICT from the previous testing, this seems to be the case (the currently proposed StartTLS tests are failing already in the afterImportTestRealm() routine call).

It think it works with embedded undertow because truststore SPI is disabled in that case.

I interpreted that always flag is supposed to work also with LDAP + StartTLS?

Maybe it was supposed to work with StartTLS too, but TBH I doubt it ever worked. IMHO though setting the "java.naming.ldap.factory.socket" might / would work(see Using Custom Sockets section in that link) for ldaps:// connections, for the StartTLS extension, when using custom socket factory, it's necessary to also specify that socket factory when performing the (re)negotiation, thus use StartTLSResponse method
with the signature:

public abstract SSLSession negotiate(SSLSocketFactory factory)
                              throws IOException
Negotiates a TLS session using an SSL socket factory. ...

instead of always using the default SSL socket factory, like it's done now.

So it would make sense to spent some time. Currently I think it can be made to work,

I was able previously to solve the Symbol not found issue by moving the SSLSocketFactory class implementation from the services/ (services/src/main/java/org/keycloak/truststore/SSLSocketFactory.java) module into the federation-ldap module (federation/ldap/src/main/java/org/keycloak/storage/ldap/truststore/SSLSocketFactory.java) [**] via change in another branch. The Keycloak code now compiles, but running (some) StartTLS now returns Class not found exception yet, which needs to be solved yet.

[**] Simply moved the SSLSocketFactory implementation, because 1) AFAICT no other functionality in services/ module seems to directly depend on it 2) it wasn't possible to add the pom dependency on services module to the ldap-federation module, because the services/ module already depends on ldap-federation module (so Maven was complaining about a cyclic dependency, when tried that approach)

but I also wonder, has it been working?

See above. AFAICT I doubt it was ever working for the StartTLS case due to always using the default socket factory when calling the tls.negotiate() call.

Is it really possible that people just used ldapsonly and provided their truststore by other means?

On the other hand: personally I'm more interested in Elytron since I understood it should be able to hot-reload certificates when they are rotated, without needing to do any restarts. That feature alone would make other alternatives quite obsolete in my mind.

Anyways, we can do this in stages like @mposolda proposes.

+1

@tsaarni
tsaarni commented Jun 1, 2020
Copy link
Copy Markdown
Contributor

So in this case, the used truststore is not driven by Truststore SPI, but instead it is driven by system properties "javax.net.ssl.truststore" and "javax.net.ssl.truststorePassword" . So if it works on undertow and doesn't work on Wildfly, then probably something doesn't work as expected in the setup of those system properties. Again, just a hint, I am not 100% sure about that... :-)

If using javax.net.ssl.truststore and javax.net.ssl.truststorePassword is acceptable in default "Only for ldaps" scenario, could StartTLS test then still be left enabled by adding the properties to testsuite/integration-arquillian/tests/base/src/test/resources/arquillian.xml (like I tested here (#7049 (comment))?

I could then try adding Truststore SPI support for "always" scenario separately.

@mposolda
mposolda commented Jun 1, 2020
Copy link
Copy Markdown
Contributor

So in this case, the used truststore is not driven by Truststore SPI, but instead it is driven by system properties "javax.net.ssl.truststore" and "javax.net.ssl.truststorePassword" . So if it works on undertow and doesn't work on Wildfly, then probably something doesn't work as expected in the setup of those system properties. Again, just a hint, I am not 100% sure about that... :-)

If using javax.net.ssl.truststore and javax.net.ssl.truststorePassword is acceptable in default "Only for ldaps" scenario, could StartTLS test then still be left enabled by adding the properties to testsuite/integration-arquillian/tests/base/src/test/resources/arquillian.xml (like I tested here (#7049 (comment))?

I could then try adding Truststore SPI support for "always" scenario separately.

@tsaarni +1

@iankko
iankko commented Jun 1, 2020
Copy link
Copy Markdown
Contributor Author

So in this case, the used truststore is not driven by Truststore SPI, but instead it is driven by system properties "javax.net.ssl.truststore" and "javax.net.ssl.truststorePassword" . So if it works on undertow and doesn't work on Wildfly, then probably something doesn't work as expected in the setup of those system properties. Again, just a hint, I am not 100% sure about that... :-)

If using javax.net.ssl.truststore and javax.net.ssl.truststorePassword is acceptable in default "Only for ldaps" scenario, could StartTLS test then still be left enabled by adding the properties to testsuite/integration-arquillian/tests/base/src/test/resources/arquillian.xml (like I tested here (#7049 (comment))?
I could then try adding Truststore SPI support for "always" scenario separately.

@tsaarni +1

Filed KEYCLOAK-14343 for the ldap.use-truststore-spi==always StartTLS issue.

Will update this PR with the suggested pom changes yet (fix the conflict, rebase etc.)

login(s) using various authentication methods, bind credential
types, and connection encryption mechanisms

The tests cover various possible combinations of the following:
* Authentication method: Anonymous or Simple (default),
* Bind credential: Secret (default) or Vault,
* Connection encryption: Plaintext (default), SSL, or startTLS

Also, ignore the StartTLS LDAP tests for now till KEYCLOAK-14343
& KEYCLOAK-14354 are corrected (due these issues they aren't
working with auth server Wildfly). They will be re-enabled later
via KEYCLOAK-14358 once possible

Signed-off-by: Jan Lieskovsky <jlieskov@redhat.com>
@iankko
iankko requested a review from mposolda June 2, 2020 10:11
@iankko
iankko commented Jun 2, 2020
Copy link
Copy Markdown
Contributor Author

@tsaarni

I could not think any easy way to make truststore SPI to work with Java LDAP client.

As I wrote previously useTruststoreSpi==always causes java.naming.ldap.factory.socket to be set to org.keycloak.truststore.SSLSocketFactory which to me seems to break StartTLS implementation in Java LDAP client. The LDAP client should start with cleartext TCP connection and then upgrade to TLS by issuing the LDAP StartTLS extension. But to me it seems that after replacing the socket factory, this will not happen. Instead it will directly attempt to establish a TLS connection to port LDAP port 389 which of course fails since the server expects to start with plain LDAP.

I found alternatives though:

As I wrote previously I could use Elytron to define default SSL context with the truststore. Java LDAP client picks up that also in case of StartTLS.

It also works simply by setting default truststore:

diff --git a/testsuite/integration-arquillian/tests/base/src/test/resources/arquillian.xml b/testsuite/integration-arquillian/tests/base/src/test/resources/arquillian.xml
index f56f9bed90..fc07ff659e 100644
--- a/testsuite/integration-arquillian/tests/base/src/test/resources/arquillian.xml
+++ b/testsuite/integration-arquillian/tests/base/src/test/resources/arquillian.xml
@@ -141,6 +141,8 @@
                 -Dauth.server.truststore=${auth.server.truststore}
                 -Dauth.server.truststore.password=${auth.server.truststore.password}
                 -Dauth.server.db.host=${auth.server.db.host}
+                -Djavax.net.ssl.trustStore=${auth.server.truststore}
+                -Djavax.net.ssl.trustStorePassword=${auth.server.truststore.password}
             </property>
             <property name="javaVmArguments">
                 ${auth.server.jboss.jvm.debug.args}

Thanks for the hint, Tero!

though this does not work exactly like this: for some reason variables are not expanding correctly and I get -Djavax.net.ssl.trustStore=${jboss.home.dir}/standalone/configuration/keycloak.truststore which is not valid path.

Right, great catch! Reproduced this behaviour yesterday too & filed KEYCLOAK-14354 to track the problem. Also, seems to have got a fix for it (official PR to come soon), but since it will take some time that PR to get reviewed (verified if it doesn't cause regression in other LDAP tests), have disabled the StartTLS tests for now (till both of KEYCLOAK-14343 & KEYCLOAK-14354 are fixed). Will re-enable them back within KEYCLOAK-14358 once they are fixed

If I set the full path, manually expanding ${jboss.home.dir}, then it works again.

@iankko
iankko commented Jun 2, 2020
Copy link
Copy Markdown
Contributor Author

@mposolda @tsaarni All done (rebased to current master & fixed the conflict), should be ready for re-review once Travis job finished

As mentioned already, marked the StartTLS tests to be ignored for now (due to the KEYCLOAK-14343 & KEYCLOAK-14354 issues), since they aren't working with the auth server Wildfly. They will be re-enabled back once these two issues are corrected

@mposolda mposolda left a comment
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@iankko @tsaarni Thanks for your work and help with this.

@mposolda
mposolda merged commit a121f77 into keycloak:master Jun 2, 2020
@mposolda
mposolda commented Jun 2, 2020
Copy link
Copy Markdown
Contributor

@iankko @tsaarni The PR is merged and KEYCLOAK-12305 JIRA resolved. So if I understand correctly, the next step might be:

@tsaarni
tsaarni commented Jun 2, 2020
Copy link
Copy Markdown
Contributor

I'm working with https://issues.redhat.com/browse/KEYCLOAK-14343 though I'm fighting with Wildfly modules and since I'm a total newbie, I'm now quite lost. I could submit WIP PR and ask help from you :)

@iankko
iankko commented Jun 2, 2020
Copy link
Copy Markdown
Contributor Author

@iankko @tsaarni The PR is merged and KEYCLOAK-12305 JIRA resolved.

@mposolda Woot, thx!

So if I understand correctly, the next step might be:

* @iankko looking at https://issues.redhat.com/browse/KEYCLOAK-14354

Yes, looking into this one (since it seems it's another testing feature that (n)ever worked & not used just by this LDAP login user test). Will report the progress back

@iankko
iankko deleted the KEYCLOAK-12305 branch June 2, 2020 13:58
@iankko
iankko commented Jun 2, 2020
Copy link
Copy Markdown
Contributor Author

@tsaarni

I'm working with https://issues.redhat.com/browse/KEYCLOAK-14343 though I'm fighting with Wildfly modules and since I'm a total newbie, I'm now quite lost. I could submit WIP PR and ask help from you :)

No problem, no worries. Thx for looking into this!

@mposolda
mposolda commented Jun 3, 2020 •
Copy link
Copy Markdown
Contributor

@tsaarni Hmm... Strange that you need to find with the Wildfly modules. I hope that no change in them will be needed, but maybe I am wrong... +1 for the WIP PR. We can possibly discuss the details here?

I think you can explicitly mark the PR as "draft" or something like that, so that it is more explicit that it is work in progress.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

0