How to use SshMessageConverter method of com.consol.citrus.ssh.server.SshServer class

Best Citrus code snippet using com.consol.citrus.ssh.server.SshServer.SshMessageConverter

Source:SshServer.java Github

copy

Full Screen

...18import com.consol.citrus.exceptions.CitrusRuntimeException;19import com.consol.citrus.server.AbstractServer;20import com.consol.citrus.ssh.SshCommand;21import com.consol.citrus.ssh.client.SshEndpointConfiguration;22import com.consol.citrus.ssh.message.SshMessageConverter;23import com.consol.citrus.util.FileUtils;24import org.apache.sshd.common.NamedFactory;25import org.apache.sshd.common.file.virtualfs.VirtualFileSystemFactory;26import org.apache.sshd.common.keyprovider.ClassLoadableResourceKeyPairProvider;27import org.apache.sshd.common.keyprovider.FileKeyPairProvider;28import org.apache.sshd.common.scp.AbstractScpTransferEventListenerAdapter;29import org.apache.sshd.common.scp.ScpTransferEventListener;30import org.apache.sshd.server.command.Command;31import org.apache.sshd.server.scp.ScpCommandFactory;32import org.apache.sshd.server.subsystem.sftp.*;33import org.springframework.core.io.ClassPathResource;34import org.springframework.core.io.Resource;35import org.springframework.util.StringUtils;36import java.io.IOException;37import java.nio.file.*;38import java.util.*;39/**40 * SSH Server implemented with Apache SSHD (http://mina.apache.org/sshd/).41 *42 * It uses the same semantic as the Jetty Servers for HTTP and WS mocks and translates43 * an incoming request into a message, for which a reply message gets translates to44 * the SSH return value.45 *46 * The incoming message generated has the following format:47 *48 * <ssh-request>49 * <command>cat -</command>50 * <stdin>This is the standard input sent</stdin>51 * </ssh-request>52 *53 * The reply message to be generated by a handler should have the following format54 *55 * <ssh-response>56 * <exit>0</exit>57 * <stdout>This is the standard input sent</stdout>58 * <stderr>warning: no tty</stderr>59 * </ssh-response>60 *61 * @author Roland Huss62 * @since 04.09.1263 */64public class SshServer extends AbstractServer {65 /** Port to listen to **/66 private int port = 22;67 /** User allowed to connect **/68 private String user;69 /** User's password or ... **/70 private String password;71 /** ... path to its public key72 Use this to convert to PEM: ssh-keygen -f key.pub -e -m pem **/73 private String allowedKeyPath;74 /* Path to our own host keys. If not provided, a default is used. The format of this75 file should be PEM, a serialized {@link java.security.KeyPair}. **/76 private String hostKeyPath;77 /** User home directory path **/78 private String userHomePath;79 /** Ssh message converter **/80 private SshMessageConverter messageConverter = new SshMessageConverter();81 /** SSH server used **/82 private org.apache.sshd.server.SshServer sshd;83 /** This servers endpoint configuration */84 private final SshEndpointConfiguration endpointConfiguration;85 /**86 * Default constructor using default endpoint configuration.87 */88 public SshServer() {89 this(new SshEndpointConfiguration());90 }91 /**92 * Constructor using endpoint configuration.93 * @param endpointConfiguration94 */95 public SshServer(SshEndpointConfiguration endpointConfiguration) {96 this.endpointConfiguration = endpointConfiguration;97 }98 @Override99 protected void startup() {100 if (!StringUtils.hasText(user)) {101 throw new CitrusRuntimeException("No 'user' provided (mandatory for authentication)");102 }103 sshd = org.apache.sshd.server.SshServer.setUpDefaultServer();104 sshd.setPort(port);105 VirtualFileSystemFactory fileSystemFactory = new VirtualFileSystemFactory();106 Path userHomeDir = Optional.ofNullable(userHomePath).map(Paths::get).map(Path::toAbsolutePath).orElse(Paths.get(String.format("target/%s/home/%s", getName(), user)).toAbsolutePath());107 if (!Files.exists(userHomeDir)) {108 try {109 Files.createDirectories(userHomeDir);110 } catch (IOException e) {111 throw new CitrusRuntimeException("Failed to setup user home dir", e);112 }113 }114 fileSystemFactory.setUserHomeDir(user, userHomeDir);115 sshd.setFileSystemFactory(fileSystemFactory);116 if (hostKeyPath != null) {117 Resource hostKey = FileUtils.getFileResource(hostKeyPath);118 if (hostKey instanceof ClassPathResource) {119 ClassLoadableResourceKeyPairProvider resourceKeyPairProvider = new ClassLoadableResourceKeyPairProvider(Collections.singletonList(((ClassPathResource) hostKey).getPath()));120 sshd.setKeyPairProvider(resourceKeyPairProvider);121 } else {122 try {123 FileKeyPairProvider fileKeyPairProvider = new FileKeyPairProvider(Collections.singletonList(hostKey.getFile().toPath()));124 sshd.setKeyPairProvider(fileKeyPairProvider);125 } catch (IOException e) {126 throw new CitrusRuntimeException("Failed to read host key path", e);127 }128 }129 } else {130 ClassLoadableResourceKeyPairProvider resourceKeyPairProvider = new ClassLoadableResourceKeyPairProvider(Collections.singletonList("com/consol/citrus/ssh/citrus.pem"));131 sshd.setKeyPairProvider(resourceKeyPairProvider);132 }133 // Authentication134 boolean authFound = false;135 if (password != null) {136 sshd.setPasswordAuthenticator(new SimplePasswordAuthenticator(user, password));137 authFound = true;138 }139 if (allowedKeyPath != null) {140 sshd.setPublickeyAuthenticator(new SinglePublicKeyAuthenticator(user, allowedKeyPath));141 authFound = true;142 }143 if (!authFound) {144 throw new CitrusRuntimeException("Neither 'password' nor 'allowed-key-path' is set. Please provide at least one");145 }146 // Setup endpoint adapter147 ScpCommandFactory commandFactory = new ScpCommandFactory.Builder()148 .withDelegate(command -> new SshCommand(command, getEndpointAdapter(), endpointConfiguration))149 .build();150 commandFactory.addEventListener(getScpTransferEventListener());151 sshd.setCommandFactory(commandFactory);152 ArrayList<NamedFactory<Command>> subsystemFactories = new ArrayList<>();153 SftpSubsystemFactory sftpSubsystemFactory = new SftpSubsystemFactory.Builder().build();154 sftpSubsystemFactory.addSftpEventListener(getSftpEventListener());155 subsystemFactories.add(sftpSubsystemFactory);156 sshd.setSubsystemFactories(subsystemFactories);157 try {158 sshd.start();159 } catch (IOException e) {160 throw new CitrusRuntimeException("Failed to start SSH server - " + e.getMessage(), e);161 }162 }163 /**164 * Gets Scp trsanfer event listener. By default uses abstract implementation that use trace level logging of all operations.165 * @return166 */167 protected ScpTransferEventListener getScpTransferEventListener() {168 return new AbstractScpTransferEventListenerAdapter() {};169 }170 /**171 * Gets Sftp event listener. By default uses abstract implementation that use trace level logging of all operations.172 * @return173 */174 protected SftpEventListener getSftpEventListener() {175 return new AbstractSftpEventListenerAdapter(){};176 }177 @Override178 protected void shutdown() {179 try {180 sshd.stop();181 } catch (IOException e) {182 throw new CitrusRuntimeException("Failed to stop SSH server - " + e.getMessage(), e);183 }184 }185 @Override186 public AbstractPollableEndpointConfiguration getEndpointConfiguration() {187 return endpointConfiguration;188 }189 /**190 * Gets the server port.191 * @return192 */193 public int getPort() {194 return port;195 }196 /**197 * Sets the port.198 * @param port the port to set199 */200 public void setPort(int port) {201 this.port = port;202 this.endpointConfiguration.setPort(port);203 }204 /**205 * Gets the username.206 * @return207 */208 public String getUser() {209 return user;210 }211 /**212 * Sets the user.213 * @param user the user to set214 */215 public void setUser(String user) {216 this.user = user;217 this.endpointConfiguration.setUser(user);218 }219 /**220 * Gets the user password.221 * @return222 */223 public String getPassword() {224 return password;225 }226 /**227 * Sets the password.228 * @param password the password to set229 */230 public void setPassword(String password) {231 this.password = password;232 this.endpointConfiguration.setPassword(password);233 }234 /**235 * Gets the allowed key path.236 * @return237 */238 public String getAllowedKeyPath() {239 return allowedKeyPath;240 }241 /**242 * Sets the allowedKeyPath.243 * @param allowedKeyPath the allowedKeyPath to set244 */245 public void setAllowedKeyPath(String allowedKeyPath) {246 this.allowedKeyPath = allowedKeyPath;247 }248 /**249 * Gets the host key path.250 * @return251 */252 public String getHostKeyPath() {253 return hostKeyPath;254 }255 /**256 * Sets the hostKeyPath.257 * @param hostKeyPath the hostKeyPath to set258 */259 public void setHostKeyPath(String hostKeyPath) {260 this.hostKeyPath = hostKeyPath;261 }262 /**263 * Gets the userHomePath.264 *265 * @return266 */267 public String getUserHomePath() {268 return userHomePath;269 }270 /**271 * Sets the userHomePath.272 *273 * @param userHomePath274 */275 public void setUserHomePath(String userHomePath) {276 this.userHomePath = userHomePath;277 }278 /**279 * Gets the message converter.280 * @return281 */282 public SshMessageConverter getMessageConverter() {283 return messageConverter;284 }285 /**286 * Sets the message converter.287 * @param messageConverter288 */289 public void setMessageConverter(SshMessageConverter messageConverter) {290 this.messageConverter = messageConverter;291 this.endpointConfiguration.setMessageConverter(messageConverter);292 }293}...

Full Screen

Full Screen

Source:SshServerConfigParserTest.java Github

copy

Full Screen

...19import com.consol.citrus.annotations.CitrusEndpoint;20import com.consol.citrus.channel.ChannelEndpointAdapter;21import com.consol.citrus.context.SpringBeanReferenceResolver;22import com.consol.citrus.endpoint.EndpointAdapter;23import com.consol.citrus.ssh.message.SshMessageConverter;24import com.consol.citrus.ssh.server.SshServer;25import com.consol.citrus.testng.AbstractTestNGUnitTest;26import org.mockito.*;27import org.springframework.beans.factory.annotation.Autowired;28import org.springframework.context.ApplicationContext;29import org.testng.Assert;30import org.testng.annotations.BeforeClass;31import org.testng.annotations.Test;32import static org.mockito.Mockito.when;33/**34 * @author Christoph Deppisch35 */36public class SshServerConfigParserTest extends AbstractTestNGUnitTest {37 @CitrusEndpoint(name = "sshServer1")38 @SshServerConfig(autoStart = false,39 port = 22)40 private SshServer sshServer1;41 @CitrusEndpoint42 @SshServerConfig(autoStart= false,43 port=10022,44 allowedKeyPath="classpath:com/consol/citrus/ssh/citrus_pub.pem",45 hostKeyPath="classpath:com/consol/citrus/ssh/citrus.pem",46 userHomePath="/home/user",47 user="foo",48 password="bar",49 messageConverter="messageConverter",50 timeout=10000L)51 private SshServer sshServer2;52 @CitrusEndpoint53 @SshServerConfig(autoStart = false,54 endpointAdapter="endpointAdapter",55 actor="testActor")56 private SshServer sshServer3;57 @Autowired58 private SpringBeanReferenceResolver referenceResolver;59 @Mock60 private SshMessageConverter messageConverter = Mockito.mock(SshMessageConverter.class);61 @Mock62 private EndpointAdapter endpointAdapter = Mockito.mock(EndpointAdapter.class);63 @Mock64 private TestActor testActor = Mockito.mock(TestActor.class);65 @Mock66 private ApplicationContext applicationContext = Mockito.mock(ApplicationContext.class);67 @BeforeClass68 public void setup() {69 MockitoAnnotations.initMocks(this);70 referenceResolver.setApplicationContext(applicationContext);71 when(applicationContext.getBean("messageConverter", SshMessageConverter.class)).thenReturn(messageConverter);72 when(applicationContext.getBean("endpointAdapter", EndpointAdapter.class)).thenReturn(endpointAdapter);73 when(applicationContext.getBean("testActor", TestActor.class)).thenReturn(testActor);74 }75 @Test76 public void testSshServerParser() {77 CitrusAnnotations.injectEndpoints(this, context);78 // 1st server79 Assert.assertEquals(sshServer1.getName(), "sshServer1");80 Assert.assertEquals(sshServer1.getPort(), 22);81 Assert.assertFalse(sshServer1.isAutoStart());82 Assert.assertNull(sshServer1.getAllowedKeyPath());83 Assert.assertNull(sshServer1.getHostKeyPath());84 Assert.assertNull(sshServer1.getUserHomePath());85 Assert.assertNull(sshServer1.getUser());...

Full Screen

Full Screen

Source:SshServerConfigParser.java Github

copy

Full Screen

...17import com.consol.citrus.TestActor;18import com.consol.citrus.config.annotation.AbstractAnnotationConfigParser;19import com.consol.citrus.context.ReferenceResolver;20import com.consol.citrus.endpoint.EndpointAdapter;21import com.consol.citrus.ssh.message.SshMessageConverter;22import com.consol.citrus.ssh.server.SshServer;23import com.consol.citrus.ssh.server.SshServerBuilder;24import org.springframework.util.StringUtils;25/**26 * @author Christoph Deppisch27 * @since 2.528 */29public class SshServerConfigParser extends AbstractAnnotationConfigParser<SshServerConfig, SshServer> {30 /**31 * Constructor matching super.32 * @param referenceResolver33 */34 public SshServerConfigParser(ReferenceResolver referenceResolver) {35 super(referenceResolver);36 }37 @Override38 public SshServer parse(SshServerConfig annotation) {39 SshServerBuilder builder = new SshServerBuilder();40 builder.port(annotation.port());41 if (StringUtils.hasText(annotation.user())) {42 builder.user(annotation.user());43 }44 if (StringUtils.hasText(annotation.password())) {45 builder.password(annotation.password());46 }47 if (StringUtils.hasText(annotation.hostKeyPath())) {48 builder.hostKeyPath(annotation.hostKeyPath());49 }50 if (StringUtils.hasText(annotation.userHomePath())) {51 builder.userHomePath(annotation.userHomePath());52 }53 if (StringUtils.hasText(annotation.allowedKeyPath())) {54 builder.allowedKeyPath(annotation.allowedKeyPath());55 }56 if (StringUtils.hasText(annotation.messageConverter())) {57 builder.messageConverter(getReferenceResolver().resolve(annotation.messageConverter(), SshMessageConverter.class));58 }59 builder.pollingInterval(annotation.pollingInterval());60 builder.debugLogging(annotation.debugLogging());61 if (StringUtils.hasText(annotation.endpointAdapter())) {62 builder.endpointAdapter(getReferenceResolver().resolve(annotation.endpointAdapter(), EndpointAdapter.class));63 }64 builder.autoStart(annotation.autoStart());65 builder.timeout(annotation.timeout());66 if (StringUtils.hasText(annotation.actor())) {67 builder.actor(getReferenceResolver().resolve(annotation.actor(), TestActor.class));68 }69 return builder.initialize().build();70 }71}...

Full Screen

Full Screen

SshMessageConverter

Using AI Code Generation

copy

Full Screen

1package com.consol.citrus.dsl.runner;2import com.consol.citrus.dsl.runner.TestRunner;3import com.consol.citrus.dsl.testng.TestNGCitrusTestDesigner;4import org.testng.annotations.Test;5public class SshServerTest extends TestNGCitrusTestDesigner {6 public void test() {7 TestRunner runner = createTestRunner();8 runner.ssh().server()9 .port(2222)10 .autoStart(true)11 .autoStop(true)12 .autoAccept(true)13 .autoAuthenticate(true)14 .user("user")15 .password("password")16 .reply("Hello World")17 .reply("Hello World")

Full Screen

Full Screen

SshMessageConverter

Using AI Code Generation

copy

Full Screen

1public class 3 {2 public void test() {3 SshServer sshServer = new SshServer();4 sshServer.setPort(2222);5 sshServer.setUser("user");6 sshServer.setPassword("password");7 sshServer.setKeyPath("src/test/resources/ssh/ssh-rsa");8 sshServer.setKeyPassword("secret");9 sshServer.start();10 SshMessageConverter converter = new SshMessageConverter();11 Message message = new DefaultMessage("Hello World!");12 sshServer.send(converter.convertOutbound(message, null, null));13 Message response = sshServer.receive(converter);14 assertEquals("Hello World!", response.getPayload(String.class));15 sshServer.stop();16 }17}18public class 4 {19 public void test() {20 SshServer sshServer = new SshServer();21 sshServer.setPort(2222);22 sshServer.setUser("user");23 sshServer.setPassword("password");24 sshServer.setKeyPath("src/test/resources/ssh/ssh-rsa");25 sshServer.setKeyPassword("secret");26 sshServer.start();27 sshServer.send(new DefaultMessage("Hello World!"));28 Message response = sshServer.receive();29 assertEquals("Hello World!", response.getPayload(String.class));30 sshServer.stop();31 }32}

Full Screen

Full Screen

SshMessageConverter

Using AI Code Generation

copy

Full Screen

1public class SshServerTest {2 private SshServer sshServer;3 private SshMessageConverter sshMessageConverter;4 public void testSshServer() {5 sshServer.start();6 SshServer sshServer = new SshServer();7 SshMessageConverter sshMessageConverter = new SshMessageConverter();8 sshServer.setPort(22);9 sshServer.setUser("user");10 sshServer.setPassword("password");11 sshServer.setCommand("ls");12 sshServer.setSshMessageConverter(sshMessageConverter);13 sshServer.start();14 }15}16public class SshServerTest {17 private SshServer sshServer;18 private SshMessageConverter sshMessageConverter;19 public void testSshServer() {20 sshServer.start();21 SshServer sshServer = new SshServer();22 SshMessageConverter sshMessageConverter = new SshMessageConverter();23 sshServer.setPort(22);24 sshServer.setUser("user");25 sshServer.setPassword("password");26 sshServer.setCommand("ls");27 sshServer.setSshMessageConverter(sshMessageConverter);28 sshServer.start();29 }30}31public class SshServerTest {32 private SshServer sshServer;33 private SshMessageConverter sshMessageConverter;34 public void testSshServer() {35 sshServer.start();36 SshServer sshServer = new SshServer();37 SshMessageConverter sshMessageConverter = new SshMessageConverter();38 sshServer.setPort(22);39 sshServer.setUser("user");40 sshServer.setPassword("password");41 sshServer.setCommand("ls");42 sshServer.setSshMessageConverter(sshMessageConverter);43 sshServer.start();44 }45}46public class SshServerTest {47 private SshServer sshServer;

Full Screen

Full Screen

SshMessageConverter

Using AI Code Generation

copy

Full Screen

1package com.consol.citrus.dsl.runner;2import org.testng.annotations.Test;3import com.consol.citrus.dsl.runner.TestNGCitrusTestRunner;4import com.consol.citrus.dsl.testng.TestNGCitrusTest;5import com.consol.citrus.message.MessageType;6import com.consol.citrus.ssh.server.SshServer;7import com.consol.citrus.ssh.message.SshMessage;8import com.consol.citrus.exceptions.CitrusRuntimeException;9import com.consol.citrus.context.TestContext;10import com.consol.citrus.message.Mes

Full Screen

Full Screen

SshMessageConverter

Using AI Code Generation

copy

Full Screen

1package com.consol.citrus.ssh.server;2import com.consol.citrus.ssh.message.SshMessage;3import com.consol.citrus.ssh.message.SshMessageConverter;4import java.io.ByteArrayOutputStream;5import java.io.ObjectOutput;6import java.io.ObjectOutputStream;7import java.io.Serializable;8import java.util.*;9import java.util.concurrent.ConcurrentHashMap;10import java.util.concurrent.ConcurrentMap;11import org.apache.sshd.common.util.Buffer;12import org.apache.sshd.server.Environment;13import org.apache.sshd.server.Signal;14import org.apache.sshd.server.SignalListener;15import org.apache.sshd.server.channel.ChannelSession;16import org.apache.sshd.server.session.ServerSession;17import org.slf4j.Logger;18import org.slf4j.LoggerFactory;19import org.springframework.util.Assert;20import org.springframework.util.StringUtils;21public class SshServer {22private static final Logger LOG = LoggerFactory.getLogger(SshServer.class);23private static final ConcurrentMap<String, SshServer> instances = new ConcurrentHashMap<String, SshServer>();24private final String id;25private final String host;26private final int port;27private final String user;28private final String password;29private final String privateKeyPath;30private final String privateKeyPassphrase;31private final String command;32private final int timeout;33private final String charsetName;34private final SshServerSupport serverSupport;35private final List<SignalListener> signalListeners;36private final List<String> signalNames;37private final List<String> signalNamesToIgnore;38private final SshServerMessageConverter sshServerMessageConverter;39private final SshMessageConverter sshMessageConverter;40private final List<String> commandsToIgnore;41private final List<String> commandsToExecute;42private final List<String> commandsToExecuteAfter;43private final List<String> commandsToExecuteBefore;44private final List<String> commandsToExecuteBeforeAndAfter;45private final List<String> commandsToExecuteOnce;46private final List<String> commandsToExecuteOnceBeforeAndAfter;47private final List<String> commandsToExecuteOnceBefore;48private final List<String> commandsToExecuteOnceAfter;49private final List<String> commandsToExecuteOnceAfterAndBefore;50private final List<String> commandsToExecuteOnceAfterAndBeforeAndOnce;51private final List<String> commandsToExecuteOnceAfterAndOnce;52private final List<String> commandsToExecuteOnceBeforeAndOnce;

Full Screen

Full Screen

SshMessageConverter

Using AI Code Generation

copy

Full Screen

1sshMessageConverter.convertOutbound(2new SshMessage("Hello from Citrus!"),3context);4sshServer.send(response);5SshResponse response = sshServer.receive();6SshMessage message = sshMessageConverter.convertInbound(response, context);7sshMessageConverter.convertOutbound(8new SshMessage("Hello from Citrus!"),9context);10sshServer.send(response);11SshResponse response = sshServer.receive();12SshMessage message = sshMessageConverter.convertInbound(response, context);13sshMessageConverter.convertOutbound(14new SshMessage("Hello from Citrus!"),15context);16sshServer.send(response);17SshResponse response = sshServer.receive();18SshMessage message = sshMessageConverter.convertInbound(response, context);19sshMessageConverter.convertOutbound(20new SshMessage("Hello from Citrus!"),21context);22sshServer.send(response);23SshResponse response = sshServer.receive();24SshMessage message = sshMessageConverter.convertInbound(response

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful