gansheer commented on code in PR #8629: URL: https://github.com/apache/camel-quarkus/pull/8629#discussion_r3202131937
########## integration-tests-support/sftp/src/main/java/org/apache/camel/quarkus/test/support/sftp/SftpHostCertTestResource.java: ########## @@ -0,0 +1,170 @@ +/* + * 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.test.support.sftp; + +import java.nio.file.Files; +import java.nio.file.Path; +import java.security.KeyPair; +import java.security.PublicKey; +import java.util.Collections; +import java.util.List; +import java.util.Map; + +import io.quarkus.test.common.QuarkusTestResourceLifecycleManager; +import org.apache.camel.quarkus.test.AvailablePortFinder; +import org.apache.sshd.common.NamedFactory; +import org.apache.sshd.common.config.keys.OpenSshCertificate; +import org.apache.sshd.common.config.keys.PublicKeyEntry; +import org.apache.sshd.common.file.virtualfs.VirtualFileSystemFactory; +import org.apache.sshd.common.keyprovider.FileKeyPairProvider; +import org.apache.sshd.common.keyprovider.KeyPairProvider; +import org.apache.sshd.common.signature.BuiltinSignatures; +import org.apache.sshd.common.signature.Signature; +import org.apache.sshd.scp.server.ScpCommandFactory; +import org.apache.sshd.server.SshServer; +import org.apache.sshd.sftp.server.SftpSubsystemFactory; +import org.jboss.logging.Logger; + +/** + * SFTP test resource that presents host certificates for host certificate verification testing. + * This is separate from SftpTestResource to avoid interfering with other tests. + */ +public class SftpHostCertTestResource implements QuarkusTestResourceLifecycleManager { + private static final Logger LOGGER = Logger.getLogger(SftpHostCertTestResource.class); + + private SshServer sshServer; + private Path sftpHome; + + @Override + public Map<String, String> start() { + try { + final int port = AvailablePortFinder.getNextAvailable(); + + sftpHome = Files.createTempDirectory("sftp-hostcert-"); + Path adminHome = sftpHome.resolve("admin"); + Files.createDirectories(adminHome); + + VirtualFileSystemFactory factory = new VirtualFileSystemFactory(); + factory.setUserHomeDir("admin", adminHome.toAbsolutePath()); + + sshServer = SshServer.setUpDefaultServer(); + sshServer.setPort(port); + + sshServer.setKeyPairProvider(createHostCertKeyPairProvider()); + + sshServer.setSubsystemFactories(Collections.singletonList(new SftpSubsystemFactory())); + sshServer.setCommandFactory(new ScpCommandFactory()); + sshServer.setPasswordAuthenticator((username, password, session) -> true); + sshServer.setPublickeyAuthenticator((username, key, session) -> true); + + // Add certificate signature factories + List<NamedFactory<Signature>> signatureFactories = sshServer.getSignatureFactories(); + signatureFactories.add(BuiltinSignatures.rsa_cert); + signatureFactories.add(BuiltinSignatures.rsaSHA256_cert); + signatureFactories.add(BuiltinSignatures.rsaSHA512_cert); + signatureFactories.add(BuiltinSignatures.ed25519_cert); + sshServer.setSignatureFactories(signatureFactories); + + sshServer.setFileSystemFactory(factory); + sshServer.start(); + + LOGGER.infof("SFTP server with host certificate started on port %d", port); + + return Collections.singletonMap("camel.sftp.hostcert.test-port", Integer.toString(port)); + } catch (Exception e) { + throw new RuntimeException("Failed to start SFTP server with host certificate", e); + } + } + + @Override + public void stop() { + try { + if (sshServer != null) { + sshServer.stop(); + LOGGER.info("SFTP server with host certificate stopped"); + } + } catch (Exception e) { + LOGGER.warn("Failed to stop SFTP server", e); + } + + try { + if (sftpHome != null && Files.exists(sftpHome)) { + Files.walk(sftpHome) + .sorted((a, b) -> -a.compareTo(b)) Review Comment: This is a not that pretty way to ensure that we don't try to delete a non-empty directory first. Files.walk() traverses depth-first but returns paths in an order where parent directories may come before their contents so this reverses the default order avoid this issue. -- 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]
