jamesnetherton commented on code in PR #7227: URL: https://github.com/apache/camel-quarkus/pull/7227#discussion_r2028750724
########## docs/modules/ROOT/pages/reference/extensions/ldap.adoc: ########## @@ -46,6 +46,38 @@ endif::[] [id="extensions-ldap-usage"] == Usage +[id="extensions-ldap-usage-configuration-via-properties"] +=== Configuration via properties + +The LDAP component supports property-based configuration in addition to plain Camel configuration. +Configuration property should follow the pattern: +``` Review Comment: Nitpick - can we switch all these markdown style code blocks to AsciiDoc. E.g for properties: ``` [source,properties] ---- quarkus.foo=bar ---- ``` ########## extensions/ldap/runtime/src/main/java/org/apache/camel/quarkus/component/ldap/CamelLdapRecorder.java: ########## @@ -0,0 +1,61 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.quarkus.component.ldap; + +import java.util.Hashtable; +import java.util.Optional; +import java.util.stream.StreamSupport; + +import javax.naming.Context; + +import io.quarkus.runtime.RuntimeValue; +import io.quarkus.runtime.annotations.Recorder; +import org.apache.camel.CamelContext; +import org.eclipse.microprofile.config.ConfigProvider; + +@Recorder +public class CamelLdapRecorder { + + public void createDirContext(RuntimeValue<CamelContext> contextRuntimeValue, String contextName) { + + CamelContext context = contextRuntimeValue.getValue(); + + Hashtable<String, Object> env = new Hashtable<>(); + put(env, Context.INITIAL_CONTEXT_FACTORY, contextName, "initial-context-factory"); + put(env, Context.PROVIDER_URL, contextName, "provider-url"); + put(env, Context.SECURITY_AUTHENTICATION, contextName, "security-authentication"); + put(env, Context.SECURITY_PROTOCOL, contextName, "security-protocol"); + put(env, "java.naming.ldap.factory.socket", contextName, "socket-factory"); + + //additional options + StreamSupport.stream(ConfigProvider.getConfig().getPropertyNames().spliterator(), false) + .filter(p -> p + .startsWith("quarkus.camel.ldap.dir-contexts.\"%s\".additional-properties".formatted(contextName))) + .forEach(p -> env.put(p.replaceFirst(".*additional-properties", ""), + ConfigProvider.getConfig().getValue(p, String.class))); + + context.getRegistry().bind(contextName, env); Review Comment: This works, but, it'd be simpler to pass `CamelLdapConfig` to `createDirContext` and just iterate over its `additionalProperties()`. You probably don't need to pass along the `contextName` at all. ########## integration-tests/ldap/src/test/java/org/apache/camel/quarkus/component/ldap/it/LdapTestResource.java: ########## @@ -0,0 +1,103 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.quarkus.component.ldap.it; + +import java.io.InputStream; +import java.net.InetAddress; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Map; + +import com.unboundid.ldap.listener.InMemoryDirectoryServer; +import com.unboundid.ldap.listener.InMemoryDirectoryServerConfig; +import com.unboundid.ldap.listener.InMemoryListenerConfig; +import com.unboundid.ldif.LDIFReader; +import com.unboundid.util.ssl.KeyStoreKeyManager; +import com.unboundid.util.ssl.SSLUtil; +import io.quarkus.test.common.QuarkusTestResourceLifecycleManager; + +public class LdapTestResource implements QuarkusTestResourceLifecycleManager { + + InMemoryDirectoryServer ldapServer; + + @Override + public Map<String, String> start() { + + try { + // Create an LDAP server to handle unencrypted and TLS connections + InMemoryDirectoryServerConfig dsConfig = new InMemoryDirectoryServerConfig("ou=system"); + InMemoryListenerConfig listenerConfig = InMemoryListenerConfig.createLDAPConfig("ldap", + InetAddress.getLoopbackAddress(), 0, null); + + // The keystore is generated by the build process + Path keystoreFile = Paths.get("target/certs/ldap-keystore.p12"); + if (!Files.isRegularFile(keystoreFile)) { + /* The test is run from a test-jar within Quarkus Platform, where the Ant script was not run + * so let's copy the keystore from test-jar to the local folder */ + Files.createDirectories(keystoreFile.getParent()); + try (InputStream in = LdapTest.class.getClassLoader() + .getResourceAsStream(keystoreFile.getFileName().toString())) { + Files.copy(in, keystoreFile); + } + Path truststorePath = Paths.get("target/certs/ldap-truststore.p12"); + try (InputStream in = LdapTest.class.getClassLoader() + .getResourceAsStream(truststorePath.getFileName().toString())) { + Files.copy(in, truststorePath); + } + } + + SSLUtil serverSSLUtil = new SSLUtil(new KeyStoreKeyManager(keystoreFile.toFile(), "changeit".toCharArray()), + null); + InMemoryListenerConfig sslListenerConfig = InMemoryListenerConfig.createLDAPSConfig("ldaps", + InetAddress.getLoopbackAddress(), 0, serverSSLUtil.createSSLServerSocketFactory(), + null); + dsConfig.setListenerConfigs(listenerConfig, sslListenerConfig); + ldapServer = new InMemoryDirectoryServer(dsConfig); + + // Load the LDIF file from the Camel LDAP tests + LDIFReader ldifReader = new LDIFReader( + LdapTest.class.getClassLoader().getResourceAsStream("LdapRouteTest.ldif")); + ldapServer.importFromLDIF(true, ldifReader); + ldapServer.startListening(); + + String host = ldapServer.getListenAddress("ldap").getHostAddress(); + int port = ldapServer.getListenPort("ldap"); + int sslPort = ldapServer.getListenPort("ldaps"); + System.setProperty("port", String.valueOf(port)); + + return Map.of( + "ldap.host", host, + "ldap.port", port + "", + "ldap.sslPort", sslPort + "", + "quarkus.camel.ldap.dir-contexts.\"httpserver\".provider-url", "ldap://%s:%d".formatted(host, port)); Review Comment: Do you need to set `quarkus.camel.ldap.dir-contexts.\"httpserver\"`? I doubt it works because the config phase is `BUILD_AND_RUN_TIME_FIXED` and here we try to override at runtime, which Quarkus does not allow (there's probably a warning on startup logs about it). ########## extensions/ldap/runtime/src/main/java/org/apache/camel/quarkus/component/ldap/CamelLdapConfig.java: ########## @@ -0,0 +1,81 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.quarkus.component.ldap; + +import java.util.Map; +import java.util.Optional; + +import io.quarkus.runtime.annotations.ConfigGroup; +import io.quarkus.runtime.annotations.ConfigPhase; +import io.quarkus.runtime.annotations.ConfigRoot; +import io.smallrye.config.ConfigMapping; +import io.smallrye.config.WithDefault; + +@ConfigRoot(phase = ConfigPhase.BUILD_AND_RUN_TIME_FIXED) +@ConfigMapping(prefix = "quarkus.camel.ldap") +public interface CamelLdapConfig { + + /** + * Ldap dicContext configuration Review Comment: ```suggestion * Ldap dirContext configuration ``` ########## integration-tests/ldap/src/test/java/org/apache/camel/quarkus/component/ldap/it/LdapTestResource.java: ########## @@ -0,0 +1,103 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.quarkus.component.ldap.it; + +import java.io.InputStream; +import java.net.InetAddress; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Map; + +import com.unboundid.ldap.listener.InMemoryDirectoryServer; +import com.unboundid.ldap.listener.InMemoryDirectoryServerConfig; +import com.unboundid.ldap.listener.InMemoryListenerConfig; +import com.unboundid.ldif.LDIFReader; +import com.unboundid.util.ssl.KeyStoreKeyManager; +import com.unboundid.util.ssl.SSLUtil; +import io.quarkus.test.common.QuarkusTestResourceLifecycleManager; + +public class LdapTestResource implements QuarkusTestResourceLifecycleManager { + + InMemoryDirectoryServer ldapServer; + + @Override + public Map<String, String> start() { + + try { + // Create an LDAP server to handle unencrypted and TLS connections + InMemoryDirectoryServerConfig dsConfig = new InMemoryDirectoryServerConfig("ou=system"); + InMemoryListenerConfig listenerConfig = InMemoryListenerConfig.createLDAPConfig("ldap", + InetAddress.getLoopbackAddress(), 0, null); + + // The keystore is generated by the build process + Path keystoreFile = Paths.get("target/certs/ldap-keystore.p12"); + if (!Files.isRegularFile(keystoreFile)) { + /* The test is run from a test-jar within Quarkus Platform, where the Ant script was not run + * so let's copy the keystore from test-jar to the local folder */ + Files.createDirectories(keystoreFile.getParent()); + try (InputStream in = LdapTest.class.getClassLoader() + .getResourceAsStream(keystoreFile.getFileName().toString())) { + Files.copy(in, keystoreFile); + } + Path truststorePath = Paths.get("target/certs/ldap-truststore.p12"); + try (InputStream in = LdapTest.class.getClassLoader() + .getResourceAsStream(truststorePath.getFileName().toString())) { + Files.copy(in, truststorePath); + } + } + + SSLUtil serverSSLUtil = new SSLUtil(new KeyStoreKeyManager(keystoreFile.toFile(), "changeit".toCharArray()), + null); + InMemoryListenerConfig sslListenerConfig = InMemoryListenerConfig.createLDAPSConfig("ldaps", + InetAddress.getLoopbackAddress(), 0, serverSSLUtil.createSSLServerSocketFactory(), + null); + dsConfig.setListenerConfigs(listenerConfig, sslListenerConfig); + ldapServer = new InMemoryDirectoryServer(dsConfig); + + // Load the LDIF file from the Camel LDAP tests + LDIFReader ldifReader = new LDIFReader( + LdapTest.class.getClassLoader().getResourceAsStream("LdapRouteTest.ldif")); + ldapServer.importFromLDIF(true, ldifReader); + ldapServer.startListening(); + + String host = ldapServer.getListenAddress("ldap").getHostAddress(); + int port = ldapServer.getListenPort("ldap"); + int sslPort = ldapServer.getListenPort("ldaps"); + System.setProperty("port", String.valueOf(port)); Review Comment: Do we need to set the port sysprop? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
