How to use BouncyCastleProvider method of com.consol.citrus.ssh.server.SinglePublicKeyAuthenticator class

Best Citrus code snippet using com.consol.citrus.ssh.server.SinglePublicKeyAuthenticator.BouncyCastleProvider

Source:SinglePublicKeyAuthenticatorTest.java Github

copy

Full Screen

...17import com.consol.citrus.exceptions.CitrusRuntimeException;18import org.apache.sshd.common.util.io.IoUtils;19import org.apache.sshd.common.util.security.SecurityUtils;20import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;21import org.bouncycastle.jce.provider.BouncyCastleProvider;22import org.bouncycastle.openssl.PEMKeyPair;23import org.bouncycastle.openssl.PEMParser;24import org.springframework.util.FileCopyUtils;25import org.testng.annotations.Test;26import java.io.*;27import java.security.PublicKey;28import static org.testng.Assert.assertFalse;29import static org.testng.Assert.assertTrue;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 pResource88 * @return89 * @throws IOException90 */91 private File copyToTempFile(String pResource) throws IOException {92 File temp = File.createTempFile("citrus-ssh", "pem");93 FileCopyUtils.copy(getClass().getResourceAsStream(pResource),94 new FileOutputStream(temp));95 return temp;96 }97 98 /**99 * Create public key instance from file input stream.100 * @param is101 * @return102 * @throws IOException103 */104 private PublicKey getPublicKeyFromStream(InputStream is) throws IOException {105 Reader reader = new InputStreamReader(is);106 try {107 Object o = new PEMParser(reader).readObject();108 if (o instanceof PEMKeyPair) {109 return new BouncyCastleProvider().getPublicKey(((PEMKeyPair) o).getPublicKeyInfo());110 } else if (o instanceof SubjectPublicKeyInfo) {111 return new BouncyCastleProvider().getPublicKey((SubjectPublicKeyInfo) o);112 } else {113 throw new CitrusRuntimeException("Unable to read public key");114 }115 } catch (IOException e) {116 throw new CitrusRuntimeException("Failed to read public key", e);117 } finally {118 IoUtils.closeQuietly(is, reader);119 }120 }121}...

Full Screen

Full Screen

Source:SinglePublicKeyAuthenticator.java Github

copy

Full Screen

...19import org.apache.sshd.common.util.io.IoUtils;20import org.apache.sshd.server.auth.pubkey.PublickeyAuthenticator;21import org.apache.sshd.server.session.ServerSession;22import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;23import org.bouncycastle.jce.provider.BouncyCastleProvider;24import org.bouncycastle.openssl.PEMKeyPair;25import org.bouncycastle.openssl.PEMParser;26import org.slf4j.Logger;27import org.slf4j.LoggerFactory;28import java.io.*;29import java.security.PublicKey;30/**31 * Public key authenticator which verifies a single provided public key. The public key32 * itself must be in PEM format.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) {...

Full Screen

Full Screen

BouncyCastleProvider

Using AI Code Generation

copy

Full Screen

1package com.consol.citrus.ssh.server;2import java.security.PublicKey;3import java.util.Arrays;4import java.util.List;5import org.apache.sshd.server.auth.pubkey.PublickeyAuthenticator;6import org.apache.sshd.server.session.ServerSession;7import org.bouncycastle.jce.provider.BouncyCastleProvider;8public class SinglePublicKeyAuthenticator implements PublickeyAuthenticator {9 private List<PublicKey> authorizedKeys;10 public SinglePublicKeyAuthenticator(PublicKey authorizedKey) {11 this(Arrays.asList(authorizedKey));12 }13 public SinglePublicKeyAuthenticator(List<PublicKey> authorizedKeys) {14 this.authorizedKeys = authorizedKeys;15 }16 public boolean authenticate(String username, PublicKey key, ServerSession session) {17 if (authorizedKeys == null) {18 return false;19 }20 for (PublicKey authorizedKey : authorizedKeys) {21 if (BouncyCastleProvider.getPublicKey(authorizedKey).equals(BouncyCastleProvider.getPublicKey(key))) {22 return true;23 }24 }25 return false;26 }27}28package com.consol.citrus.ssh.server;29import java.io.File;30import java.io.IOException;31import java.security.KeyPair;32import java.security.PublicKey;33import java.util.ArrayList;34import java.util.List;35import org.apache.sshd.common.NamedFactory;36import org.apache.sshd.common.config.keys.KeyUtils;37import org.apache.sshd.common.keyprovider.KeyPairProvider;38import org.apache.sshd.common.session.Session;39import org.apache.sshd.common.util.SecurityUtils;40import org.apache.sshd.server.Command;41import org.apache.sshd.server.CommandFactory;42import org.apache.sshd.server.Environment;43import org.apache.sshd.server.SshServer;44import org.apache.sshd.server.auth.pubkey.PublickeyAuthenticator;45import org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider;46import org.apache.sshd.server.session.ServerSession;47import org.apache.sshd.server.shell.ProcessShellFactory;48import org.bouncycastle.jce.provider.BouncyCastleProvider;49import com.consol.citrus.ssh.SshCommand;50public class SshServerBuilder {51 private final SshServer sshServer;

Full Screen

Full Screen

BouncyCastleProvider

Using AI Code Generation

copy

Full Screen

1package com.consol.citrus.ssh;2import java.security.Security;3import org.bouncycastle.jce.provider.BouncyCastleProvider;4public class BouncyCastleProviderTest {5public static void main(String[] args) {6Security.addProvider(new BouncyCastleProvider());7SinglePublicKeyAuthenticator singlePublicKeyAuthenticator = new SinglePublicKeyAuthenticator();

Full Screen

Full Screen

BouncyCastleProvider

Using AI Code Generation

copy

Full Screen

1public class SinglePublicKeyAuthenticator extends PublickeyAuthenticator {2 private static final Logger LOG = LoggerFactory.getLogger(SinglePublicKeyAuthenticator.class);3 private final PublicKey publicKey;4 public SinglePublicKeyAuthenticator(PublicKey publicKey) {5 this.publicKey = publicKey;6 }7 public boolean authenticate(String username, PublicKey key, ServerSession session) {8 LOG.debug("Authenticating user: " + username);9 return publicKey.equals(key);10 }11}12public class SinglePublicKeyAuthenticator extends PublickeyAuthenticator {13 private static final Logger LOG = LoggerFactory.getLogger(SinglePublicKeyAuthenticator.class);14 private final PublicKey publicKey;15 public SinglePublicKeyAuthenticator(PublicKey publicKey) {16 this.publicKey = publicKey;17 }18 public boolean authenticate(String username, PublicKey key, ServerSession session) {19 LOG.debug("Authenticating user: " + username);20 return publicKey.equals(key);21 }22}23public class SinglePublicKeyAuthenticator extends PublickeyAuthenticator {24 private static final Logger LOG = LoggerFactory.getLogger(SinglePublicKeyAuthenticator.class);25 private final PublicKey publicKey;26 public SinglePublicKeyAuthenticator(PublicKey publicKey) {27 this.publicKey = publicKey;28 }29 public boolean authenticate(String username, PublicKey key, ServerSession session) {30 LOG.debug("Authenticating user: " + username);31 return publicKey.equals(key);32 }33}34public class SinglePublicKeyAuthenticator extends PublickeyAuthenticator {35 private static final Logger LOG = LoggerFactory.getLogger(SinglePublicKeyAuthenticator.class);36 private final PublicKey publicKey;37 public SinglePublicKeyAuthenticator(PublicKey publicKey) {38 this.publicKey = publicKey;39 }40 public boolean authenticate(String username, PublicKey key, ServerSession session) {41 LOG.debug("Authenticating user: " + username);42 return publicKey.equals(key);43 }44}

Full Screen

Full Screen

BouncyCastleProvider

Using AI Code Generation

copy

Full Screen

1package com.consol.citrus.ssh;2import com.consol.citrus.ssh.server.SinglePublicKeyAuthenticator;3import org.apache.sshd.common.NamedFactory;4import org.apache.sshd.common.config.keys.KeyUtils;5import org.apache.sshd.common.keyprovider.FileKeyPairProvider;6import org.apache.sshd.common.session.SessionContext;7import org.apache.sshd.server.auth.UserAuth;8import org.apache.sshd.server.auth.UserAuthFactory;9import org.apache.sshd.server.auth.UserAuthKeyboardInteractive;10import org.apache.sshd.server.auth.UserAuthPassword;11import org.apache.sshd.server.auth.UserAuthPublicKey;12import org.apache.sshd.server.auth.UserAuthNone;13import org.apache.sshd.server.auth.UserAuthKeyboardInteractiveFactory;14import org.apache.sshd.server.auth.UserAuthPasswordFactory;15import org.apache.sshd.server.auth.UserAuthPublicKeyFactory;16import org.apache.sshd.server.auth.UserAuthNoneFactory;17import org.apache.sshd.server.session.ServerSession;18import org.bouncycastle.jce.provider.BouncyCastleProvider;19import org.bouncycastle.openssl.PEMReader;20import java.io.File;21import java.io.FileReader;22import java.io.IOException;23import java.security.KeyPair;24import java.security.PublicKey;25import java.security.Security;26import java.util.ArrayList;27import java.util.List;28public class BouncyCastleSinglePublicKeyAuthenticator extends SinglePublicKeyAuthenticator {29 private static final List<NamedFactory<UserAuth>> USER_AUTH_FACTORIES = new ArrayList<>();30 static {31 Security.addProvider(new BouncyCastleProvider());32 USER_AUTH_FACTORIES.add(new UserAuthNoneFactory());33 USER_AUTH_FACTORIES.add(new UserAuthPasswordFactory());34 USER_AUTH_FACTORIES.add(new UserAuthKeyboardInteractiveFactory());35 USER_AUTH_FACTORIES.add(new UserAuthPublicKeyFactory());36 }37 public BouncyCastleSinglePublicKeyAuthenticator(File publicKeyFile) {38 super(publicKeyFile);39 }40 public boolean authenticate(String username, PublicKey key, ServerSession session) {41 try {42 PublicKey publicKey = getPublicKey();43 return KeyUtils.compareKeys(publicKey, key);44 } catch (IOException e) {45 throw new RuntimeException(e);46 }47 }48 public PublicKey getPublicKey() throws IOException {49 PEMReader pemReader = new PEMReader(new FileReader(getPublicKeyFile()));50 KeyPair keyPair = (KeyPair) pemReader

Full Screen

Full Screen

BouncyCastleProvider

Using AI Code Generation

copy

Full Screen

1public class SinglePublicKeyAuthenticator extends UserAuthPublicKey {2 private static final Logger LOG = LoggerFactory.getLogger(SinglePublicKeyAuthenticator.class);3 private final PublicKey publicKey;4 private final String username;5 public SinglePublicKeyAuthenticator(PublicKey publicKey, String username) {6 this.publicKey = publicKey;7 this.username = username;8 }9 public boolean authenticate(String username, PublicKey key, ServerSession session) throws Exception {10 return this.username.equals(username) && this.publicKey.equals(key);11 }12}13public class SinglePublicKeyAuthenticator extends UserAuthPublicKey {14 private static final Logger LOG = LoggerFactory.getLogger(SinglePublicKeyAuthenticator.class);15 private final PublicKey publicKey;16 private final String username;17 public SinglePublicKeyAuthenticator(PublicKey publicKey, String username) {18 this.publicKey = publicKey;19 this.username = username;20 }21 public boolean authenticate(String username, PublicKey key, ServerSession session) throws Exception {22 return this.username.equals(username) && this.publicKey.equals(key);23 }24}25public class SinglePublicKeyAuthenticator extends UserAuthPublicKey {26 private static final Logger LOG = LoggerFactory.getLogger(SinglePublicKeyAuthenticator.class);27 private final PublicKey publicKey;28 private final String username;29 public SinglePublicKeyAuthenticator(PublicKey publicKey, String username) {30 this.publicKey = publicKey;31 this.username = username;32 }33 public boolean authenticate(String username, PublicKey key, ServerSession session) throws Exception {34 return this.username.equals(username) && this.publicKey.equals(key);35 }36}37public class SinglePublicKeyAuthenticator extends UserAuthPublicKey {38 private static final Logger LOG = LoggerFactory.getLogger(SinglePublicKeyAuthenticator.class);39 private final PublicKey publicKey;40 private final String username;41 public SinglePublicKeyAuthenticator(PublicKey publicKey

Full Screen

Full Screen

BouncyCastleProvider

Using AI Code Generation

copy

Full Screen

1package com.consol.citrus.ssh.server;2import java.io.File;3import java.io.IOException;4import java.nio.file.Files;5import java.nio.file.Paths;6import java.security.PublicKey;7import java.security.Security;8import org.apache.sshd.common.session.Session;9import org.apache.sshd.server.auth.pubkey.PublickeyAuthenticator;10import org.apache.sshd.server.session.ServerSession;11import org.bouncycastle.jce.provider.BouncyCastleProvider;12public class SinglePublicKeyAuthenticator implements PublickeyAuthenticator {13 private final PublicKey publicKey;14 public SinglePublicKeyAuthenticator(String publicKeyPath) throws IOException {15 Security.addProvider(new BouncyCastleProvider());16 publicKey = PublicKeyReader.readPublicKey(new File(publicKeyPath));17 }18 public boolean authenticate(String username, PublicKey key, ServerSession session) {19 return key.equals(publicKey);20 }21}22package com.consol.citrus.ssh.server;23import java.io.BufferedReader;24import java.io.File;25import java.io.FileReader;26import java.io.IOException;27import java.security.PublicKey;28import java.security.spec.InvalidKeySpecException;29import org.apache.sshd.common.util.SecurityUtils;30import org.bouncycastle.openssl.PEMParser;31import org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter;32public class PublicKeyReader {33 public static PublicKey readPublicKey(File file) throws IOException {34 try (BufferedReader reader = new BufferedReader(new FileReader(file))) {35 PEMParser pemParser = new PEMParser(reader);36 JcaPEMKeyConverter converter = new JcaPEMKeyConverter().setProvider(SecurityUtils.BOUNCY_CASTLE_PROVIDER_NAME);37 return converter.getPublicKey((java.security.interfaces.RSAPublicKey) pemParser.readObject());38 } catch (InvalidKeySpecException e) {39 throw new IOException(e);40 }41 }42}43package com.consol.citrus.ssh;44import com.consol.citrus.annotations.CitrusTest;45import com.consol.citrus.dsl.testng.TestNGCitrus

Full Screen

Full Screen

BouncyCastleProvider

Using AI Code Generation

copy

Full Screen

1package com.consol.citrus.ssh.server;2import java.io.IOException;3import java.security.PublicKey;4import java.security.Security;5import java.util.ArrayList;6import java.util.List;7import org.apache.sshd.common.keyprovider.FileKeyPairProvider;8import org.apache.sshd.common.util.SecurityUtils;9import org.apache.sshd.server.SshServer;10import org.apache.sshd.server.auth.pubkey.PublickeyAuthenticator;11import org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider;12import org.apache.sshd.server.session.ServerSession;13import org.bouncycastle.jce.provider.BouncyCastleProvider;14import org.slf4j.Logger;15import org.slf4j.LoggerFactory;16public class SinglePublicKeyAuthenticator implements PublickeyAuthenticator {17 private static final Logger LOG = LoggerFactory.getLogger(SinglePublicKeyAuthenticator.class);18 private List<PublicKey> publicKeys = new ArrayList<PublicKey>();19 public SinglePublicKeyAuthenticator() {20 Security.addProvider(new BouncyCastleProvider());21 SecurityUtils.setRegisterBouncyCastle(true);22 }23 public SinglePublicKeyAuthenticator(List<PublicKey> publicKeys) {24 this();25 this.publicKeys = publicKeys;26 }27 public boolean authenticate(String username, PublicKey key, ServerSession session) {28 for (PublicKey publicKey : publicKeys) {29 if (publicKey.equals(key)) {30 LOG.info("Public key authentication successful for user: '" + username + "'");31 return true;32 }33 }34 LOG.warn("Public key authentication failed for user: '" + username + "'");35 return false;36 }37 public static void main(String[] args) throws IOException {38 SshServer sshd = SshServer.setUpDefaultServer();39 sshd.setPort(2222);40 sshd.setKeyPairProvider(new FileKeyPairProvider(new String[] { "src/test/resources/serverkey.pem" }));41 sshd.setPublickeyAuthenticator(new SinglePublicKeyAuthenticator(new FileKeyPairProvider(new String[] { "src/test/resources/clientkey.pub" }).loadKeys()));42 sshd.setHostKeyProvider(new SimpleGeneratorHostKeyProvider("src/test/resources/hostkey.ser"));43 sshd.start();44 }45}

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.

Run Citrus automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful