Best Citrus code snippet using com.consol.citrus.ssh.server.SinglePublicKeyAuthenticator.SinglePublicKeyAuthenticator
Source:SinglePublicKeyAuthenticatorTest.java
...30/**31 * @author Roland Huss32 * @since 05.09.1233 */34public class SinglePublicKeyAuthenticatorTest {35 /**36 * Default constructor.37 */38 public SinglePublicKeyAuthenticatorTest() {39 assertTrue(SecurityUtils.isBouncyCastleRegistered());40 }41 @Test42 public void withClassPath() throws IOException {43 SinglePublicKeyAuthenticator auth = new SinglePublicKeyAuthenticator("roland","classpath:com/consol/citrus/ssh/allowed_test_key.pem");44 PublicKey pKey = getPublicKey("/com/consol/citrus/ssh/allowed_test_key.pem");45 assertTrue(auth.authenticate("roland", pKey, null));46 assertFalse(auth.authenticate("guenther", pKey, null));47 pKey = getPublicKey("/com/consol/citrus/ssh/forbidden_test_key.pem");48 assertFalse(auth.authenticate("roland", pKey, null));49 pKey = getPublicKey("/com/consol/citrus/ssh/citrus.pem");50 assertFalse(auth.authenticate("citrus", pKey, null));51 }52 @Test53 public void withFile() throws IOException {54 File temp = copyToTempFile("/com/consol/citrus/ssh/allowed_test_key.pem");55 SinglePublicKeyAuthenticator auth = new SinglePublicKeyAuthenticator("roland",temp.getAbsolutePath());56 PublicKey pKey = getPublicKeyFromStream(new FileInputStream(temp));57 assertTrue(auth.authenticate("roland", pKey, null));58 assertFalse(auth.authenticate("guenther",pKey,null));59 temp = copyToTempFile("/com/consol/citrus/ssh/forbidden_test_key.pem");60 pKey = getPublicKeyFromStream(new FileInputStream(temp));61 assertFalse(auth.authenticate("roland", pKey, null));62 }63 @Test(expectedExceptions = CitrusRuntimeException.class, expectedExceptionsMessageRegExp = ".*com/consol/citrus/ssh/private.key.*")64 public void invalidKeyFormat() {65 new SinglePublicKeyAuthenticator("roland", "classpath:com/consol/citrus/ssh/private.key");66 }67 @Test(expectedExceptions = CitrusRuntimeException.class, expectedExceptionsMessageRegExp = ".*blubber\\.bla.*")68 public void notInClasspath() {69 new SinglePublicKeyAuthenticator("roland", "classpath:com/consol/citrus/ssh/blubber.bla");70 }71 @Test(expectedExceptions = CitrusRuntimeException.class,expectedExceptionsMessageRegExp = ".*/no/valid/path.*")72 public void invalidFilePath() {73 new SinglePublicKeyAuthenticator("roland","/no/valid/path");74 }75 76 /**77 * Gets public key instance from resource.78 * @param pResource79 * @return80 * @throws IOException81 */82 private PublicKey getPublicKey(String pResource) throws IOException {83 return getPublicKeyFromStream(getClass().getResourceAsStream(pResource));84 }85 /**86 * Creates new temporary file from resource.87 * @param pResource...
Source:SinglePublicKeyAuthenticator.java
...33 *34 * @author Roland Huss35 * @since 05.09.1236 */37class SinglePublicKeyAuthenticator implements PublickeyAuthenticator {38 /** Logger */39 private static Logger log = LoggerFactory.getLogger(SinglePublicKeyAuthenticator.class);40 private PublicKey allowedKey;41 private String user;42 private BouncyCastleProvider provider = new BouncyCastleProvider();43 /**44 * Constructor45 *46 * @param username user to verify against47 * @param publicKeyPath path to a single public key PEM, either in the filesystem or, if prefixed48 * with 'classpath:' taken from the classpath.49 */50 public SinglePublicKeyAuthenticator(String username, String publicKeyPath) {51 this.user = username;52 InputStream is = null;53 try {54 is = FileUtils.getFileResource(publicKeyPath).getInputStream();55 allowedKey = readKey(is);56 if (allowedKey == null) {57 throw new CitrusRuntimeException("No public key found at " + publicKeyPath + ", although the file/resource exists. " +58 "It is probably not in a PEM form or contains more than only a public key.");59 }60 } catch (IOException e) {61 throw new CitrusRuntimeException(String.format("Failed to read public key file at %s", publicKeyPath),e);62 } finally {63 IoUtils.closeQuietly(is);64 }...
SinglePublicKeyAuthenticator
Using AI Code Generation
1import com.consol.citrus.ssh.server.SinglePublicKeyAuthenticator;2import com.consol.citrus.ssh.server.SshServer;3import com.consol.citrus.ssh.server.SshServerBuilder;4import com.consol.citrus.ssh.server.SshServerConfiguration;5import com.consol.citrus.ssh.server.SshServerConfigurationBuilder;6import com.consol.citrus.ssh.server.SshServerConfigurationProperties;
SinglePublicKeyAuthenticator
Using AI Code Generation
1import com.consol.citrus.dsl.runner.TestRunner;2import com.consol.citrus.dsl.testng.TestNGCitrusTest;3import com.consol.citrus.ssh.server.SshServer;4import com.consol.citrus.ssh.server.SinglePublicKeyAuthenticator;5import org.apache.sshd.common.NamedFactory;6import org.apache.sshd.common.config.keys.KeyUtils;7import org.apache.sshd.common.session.SessionContext;8import org.apache.sshd.server.Command;9import org.apache.sshd.server.auth.password.PasswordAuthenticator;10import org.apache.sshd.server.auth.pubkey.PublickeyAuthenticator;11import org.apache.sshd.server.keyprovider.PEMGeneratorHostKeyProvider;12import org.apache.sshd.server.session.ServerSession;13import org.apache.sshd.server.sftp.SftpSubsystem;14import org.apache.sshd.server.subsystem.sftp.SftpSubsystemFactory;15import org.testng.annotations.Test;16import java.io.File;17import java.io.IOException;18import java.security.PublicKey;19import java.util.ArrayList;20import java.util.List;21public class SshServerTest extends TestNGCitrusTest {22 public void testSshServer() {23 List<NamedFactory<Command>> subsystemFactories = new ArrayList<>();24 subsystemFactories.add(new SftpSubsystemFactory());25 SshServer sshServer = new SshServer();26 sshServer.setPort(2222);27 sshServer.setSubsystemFactories(subsystemFactories);28 sshServer.setHostKeyProvider(new PEMGeneratorHostKeyProvider(new File("src/test/resources/server.pem"), "RSA", 2048));29 sshServer.setPasswordAuthenticator(new PasswordAuthenticator() {30 public boolean authenticate(String username, String password, ServerSession session) {31 return username.equals("citrus") && password.equals("citrus");32 }33 });34 sshServer.setPublickeyAuthenticator(new PublickeyAuthenticator() {35 public boolean authenticate(String username, PublicKey key, ServerSession session) {36 return username.equals("citrus") && KeyUtils.getFingerPrint(key).equals("SHA256:QgRtRzGy8xvH0q3jKX9OgWJZCk0Z3+4w4a0Kj1s
SinglePublicKeyAuthenticator
Using AI Code Generation
1package com.consol.citrus.ssh.server;2import com.consol.citrus.ssh.SshServerBuilder;3import com.consol.citrus.ssh.SshServerConfiguration;4import com.consol.citrus.ssh.SshServerConfigurationBuilder;5import com.consol.citrus.ssh.SshServerTestRunner;6import org.testng.annotations.Test;7import java.io.File;8import java.io.IOException;9public class SinglePublicKeyAuthenticatorTest {10 public void testSinglePublicKeyAuthenticator() throws IOException {11 SshServerConfiguration sshServerConfiguration = new SshServerConfigurationBuilder()12 .port(2222)13 .username("test")14 .password("test")15 .publicKeyFile(new File("src/test/resources/publickey_rsa.pub"))16 .privateKeyFile(new File("src/test/resources/privatekey_rsa"))17 .build();18 .sshServer()19 .serverConfiguration(sshServerConfiguration)20 .authenticator(new SinglePublicKeyAuthenticator("test", "src/test/resources/publickey_rsa.pub"))21 .build();22 new SshServerTestRunner()23 .autoStart(true)24 .send("test")25 .receive("test")26 .run();27 }28}29package com.consol.citrus.ssh.server;30import com.consol.citrus.ssh.SshServerBuilder;31import com.consol.citrus.ssh.SshServerConfiguration;32import com.consol.citrus.ssh.SshServerConfigurationBuilder;33import com.consol.citrus.ssh.SshServerTestRunner;34import org.testng.annotations.Test;35import java.io.File;36import java.io.IOException;37public class SinglePublicKeyAuthenticatorTest {38 public void testSinglePublicKeyAuthenticator() throws IOException {39 SshServerConfiguration sshServerConfiguration = new SshServerConfigurationBuilder()40 .port(2222)41 .username("test")42 .password("test")43 .publicKeyFile(new File("src/test/resources/publickey_rsa.pub"))44 .privateKeyFile(new File("src/test/resources/privatekey_rsa"))45 .build();46 .sshServer()47 .serverConfiguration(sshServerConfiguration)48 .authenticator(new SinglePublicKeyAuthenticator("
SinglePublicKeyAuthenticator
Using AI Code Generation
1public class SinglePublicKeyAuthenticator implements PublicKeyAuthenticator {2 private String allowedKey;3 public SinglePublicKeyAuthenticator(String allowedKey) {4 this.allowedKey = allowedKey;5 }6 public boolean authenticate(String username, PublicKey key, ServerSession session) {7 return allowedKey.equals(key.getAlgorithm() + " " + Base64.getEncoder().encodeToString(key.getEncoded()));8 }9}10public class SshServer extends AbstractServerBuilder<SshServer> {11 private static final Logger LOG = LoggerFactory.getLogger(SshServer.class);12 private final org.apache.sshd.server.SshServer server;13 private final List<CommandFactory> commandFactories = new ArrayList<>();14 private final List<NamedFactory<Command>> namedCommandFactories = new ArrayList<>();15 private final List<NamedFactory<Command>> namedSubsystemFactories = new ArrayList<>();16 private final List<NamedFactory<UserAuth>> namedUserAuthFactories = new ArrayList<>();17 private final List<NamedFactory<NamedFactory<Command>>> namedChannelFactories = new ArrayList<>();18 private final List<NamedFactory<NamedFactory<Command>>> namedChannelForwardingFactories = new ArrayList<>();19 private final List<NamedFactory<NamedFactory<Command>>> namedChannelDirectTcpipFactories = new ArrayList<>();20 private final List<NamedFactory<NamedFactory<Command>>> namedChannelSessionFactories = new ArrayList<>();21 private final List<NamedFactory<NamedFactory<Command>>> namedChannelAgentForwardingFactories = new ArrayList<>();22 private final List<NamedFactory<NamedFactory<Command>>> namedChannelX11Factories = new ArrayList<>();23 private final List<NamedFactory<NamedFactory<Command>>> namedChannelDirectStreamlocalFactories = new ArrayList<>();24 private final List<NamedFactory<NamedFactory<Command>>> namedChannelForwardedStreamlocalFactories = new ArrayList<>();25 private final List<NamedFactory<NamedFactory<Command>>> namedChannelSftpFactories = new ArrayList<>();26 private final List<NamedFactory<NamedFactory<Command>>> namedChannelExecFactories = new ArrayList<>();27 private final List<NamedFactory<NamedFactory<Command>>> namedChannelShellFactories = new ArrayList<>();
SinglePublicKeyAuthenticator
Using AI Code Generation
1package com.consol.citrus.ssh.server;2import com.consol.citrus.ssh.server.SinglePublicKeyAuthenticator;3import org.apache.sshd.server.auth.pubkey.PublickeyAuthenticator;4import org.testng.Assert;5import org.testng.annotations.Test;6import java.security.PublicKey;7import java.util.ArrayList;8import java.util.List;9public class SinglePublicKeyAuthenticatorTest {10 public void testSinglePublicKeyAuthenticator() {11 List<PublicKey> keys = new ArrayList<PublicKey>();12 SinglePublicKeyAuthenticator singlePublicKeyAuthenticator = new SinglePublicKeyAuthenticator(keys);13 Assert.assertEquals(singlePublicKeyAuthenticator.getPublicKeys(), keys);14 Assert.assertEquals(singlePublicKeyAuthenticator.isPublickeyAuthenticator(), true);15 Assert.assertEquals(singlePublicKeyAuthenticator.authenticate("user", null, null), false);16 }17}18package com.consol.citrus.ssh.server;19import com.consol.citrus.ssh.server.SinglePublicKeyAuthenticator;20import org.apache.sshd.server.auth.pubkey.PublickeyAuthenticator;21import org.testng.Assert;22import org.testng.annotations.Test;23import java.security.PublicKey;24import java.util.ArrayList;25import java.util.List;26public class SinglePublicKeyAuthenticatorTest {27 public void testSinglePublicKeyAuthenticator() {28 List<PublicKey> keys = new ArrayList<PublicKey>();29 SinglePublicKeyAuthenticator singlePublicKeyAuthenticator = new SinglePublicKeyAuthenticator(keys);30 Assert.assertEquals(singlePublicKeyAuthenticator.getPublicKeys(), keys);31 Assert.assertEquals(singlePublicKeyAuthenticator.isPublickeyAuthenticator(), true);32 Assert.assertEquals(singlePublicKeyAuthenticator.authenticate("user", null, null), false);33 }34}
SinglePublicKeyAuthenticator
Using AI Code Generation
1public class 3 {2 public static void main(String[] args) {3 SshServer sshd = SshServer.setUpDefaultServer();4 sshd.setPort(22);5 sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider("src/main/resources/hostkey.ser"));6 sshd.setPublickeyAuthenticator(new SinglePublicKeyAuthenticator("src/main/resources/authorized_keys"));7 sshd.setPasswordAuthenticator(new DefaultPasswordAuthenticator());8 sshd.setShellFactory(new ProcessShellFactory(new String[] { "cmd.exe" }, null));9 try {10 sshd.start();11 } catch (IOException e) {12 e.printStackTrace();13 }14 }15}16public class 4 {17 public static void main(String[] args) {18 SshServer sshd = SshServer.setUpDefaultServer();19 sshd.setPort(22);20 sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider("src/main/resources/hostkey.ser"));21 sshd.setPublickeyAuthenticator(new SinglePublicKeyAuthenticator("src/main/resources/authorized_keys"));22 sshd.setPasswordAuthenticator(new DefaultPasswordAuthenticator());23 sshd.setShellFactory(new ProcessShellFactory(new String[] { "cmd.exe" }, null));24 try {25 sshd.start();26 } catch (IOException e) {27 e.printStackTrace();28 }29 }30}31public class 5 {32 public static void main(String[] args) {33 SshServer sshd = SshServer.setUpDefaultServer();34 sshd.setPort(22);35 sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider("src/main/resources/hostkey.ser"));36 sshd.setPublickeyAuthenticator(new SinglePublicKeyAuthenticator("src/main/resources/authorized_keys"));37 sshd.setPasswordAuthenticator(new DefaultPasswordAuthenticator());38 sshd.setShellFactory(new ProcessShellFactory(new String[] { "cmd.exe" }, null));39 try {40 sshd.start();41 } catch (IOException e) {42 e.printStackTrace();43 }44 }45}
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!