Search in sources :

Example 6 with HostKey

use of com.jcraft.jsch.HostKey in project spring-cloud-config by spring-cloud.

the class PropertyBasedSshSessionFactory method createSession.

@Override
protected Session createSession(Host hc, String user, String host, int port, FS fs) throws JSchException {
    if (sshKeysByHostname.containsKey(host)) {
        SshUri sshUriProperties = sshKeysByHostname.get(host);
        jSch.addIdentity(host, sshUriProperties.getPrivateKey().getBytes(), null, null);
        if (sshUriProperties.getKnownHostsFile() != null) {
            jSch.setKnownHosts(sshUriProperties.getKnownHostsFile());
        }
        if (sshUriProperties.getHostKey() != null) {
            HostKey hostkey = new HostKey(host, Base64.decode(sshUriProperties.getHostKey()));
            jSch.getHostKeyRepository().add(hostkey, null);
        }
        return jSch.getSession(user, host, port);
    }
    throw new JSchException("no keys configured for hostname " + host);
}
Also used : JSchException(com.jcraft.jsch.JSchException) HostKey(com.jcraft.jsch.HostKey)

Example 7 with HostKey

use of com.jcraft.jsch.HostKey in project spring-cloud-config by spring-cloud.

the class PropertyBasedSshSessionFactoryTest method hostKeyIsUsed.

@Test
public void hostKeyIsUsed() throws Exception {
    SshUri sshKey = new SshUriProperties.SshUriPropertiesBuilder().uri("git@gitlab.example.local:someorg/somerepo.git").hostKey(HOST_KEY).privateKey(PRIVATE_KEY).build();
    setupSessionFactory(sshKey);
    factory.createSession(hc, null, SshUriPropertyProcessor.getHostname(sshKey.getUri()), 22, null);
    ArgumentCaptor<HostKey> captor = ArgumentCaptor.forClass(HostKey.class);
    verify(hostKeyRepository).add(captor.capture(), isNull());
    HostKey hostKey = captor.getValue();
    Assert.assertEquals("gitlab.example.local", hostKey.getHost());
    Assert.assertEquals(HOST_KEY, hostKey.getKey());
}
Also used : HostKey(com.jcraft.jsch.HostKey) Test(org.junit.Test)

Example 8 with HostKey

use of com.jcraft.jsch.HostKey in project sw360portal by sw360.

the class FossologyHostKeyRepositoryTest method testCheckAValidNewKeyIsSavedAsUntrusted.

@Test
public void testCheckAValidNewKeyIsSavedAsUntrusted() throws Exception {
    when(connector.getAll()).thenReturn(Collections.<FossologyHostFingerPrint>emptyList());
    String host = "host";
    byte[] key = GOOD_KEY.getBytes();
    final String expectedFingerPrint = new HostKey(host, key).getFingerPrint(jSch);
    assertThat(expectedFingerPrint, not(isEmptyOrNullString()));
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            FossologyHostFingerPrint fingerPrint = (FossologyHostFingerPrint) invocation.getArguments()[0];
            assertThat(fingerPrint.getFingerPrint(), is(expectedFingerPrint));
            assertFalse(fingerPrint.isTrusted());
            return null;
        }
    }).when(connector).add(any(FossologyHostFingerPrint.class));
    assertThat(repo.check(host, key), is(HostKeyRepository.NOT_INCLUDED));
    verify(connector).getAll();
    verify(connector).add(any(FossologyHostFingerPrint.class));
}
Also used : Answer(org.mockito.stubbing.Answer) HostKey(com.jcraft.jsch.HostKey) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Matchers.isEmptyOrNullString(org.hamcrest.Matchers.isEmptyOrNullString) FossologyHostFingerPrint(org.eclipse.sw360.datahandler.thrift.fossology.FossologyHostFingerPrint) Test(org.junit.Test)

Example 9 with HostKey

use of com.jcraft.jsch.HostKey in project sw360portal by sw360.

the class FossologyHostKeyRepositoryTest method testCheckAValidKnownUntrustedKeyIsNotTrusted.

@Test
public void testCheckAValidKnownUntrustedKeyIsNotTrusted() throws Exception {
    String host = "host";
    byte[] key = GOOD_KEY.getBytes();
    final String expectedFingerPrint = new HostKey(host, key).getFingerPrint(jSch);
    assertThat(expectedFingerPrint, not(isEmptyOrNullString()));
    when(connector.getAll()).thenReturn(ImmutableList.of(new FossologyHostFingerPrint().setFingerPrint(expectedFingerPrint).setTrusted(false)));
    assertThat(repo.check(host, key), is(HostKeyRepository.NOT_INCLUDED));
    verify(connector).getAll();
    verify(connector, never()).add(any(FossologyHostFingerPrint.class));
}
Also used : HostKey(com.jcraft.jsch.HostKey) Matchers.isEmptyOrNullString(org.hamcrest.Matchers.isEmptyOrNullString) FossologyHostFingerPrint(org.eclipse.sw360.datahandler.thrift.fossology.FossologyHostFingerPrint) Test(org.junit.Test)

Example 10 with HostKey

use of com.jcraft.jsch.HostKey in project sw360portal by sw360.

the class FossologyHostKeyRepositoryTest method testAdd.

@Test
public void testAdd() {
    UserInfo ui = mock(UserInfo.class);
    HostKey hk = mock(HostKey.class);
    expectUnsupported();
    repo.add(hk, ui);
}
Also used : HostKey(com.jcraft.jsch.HostKey) UserInfo(com.jcraft.jsch.UserInfo) Test(org.junit.Test)

Aggregations

HostKey (com.jcraft.jsch.HostKey)10 Test (org.junit.Test)5 FossologyHostFingerPrint (org.eclipse.sw360.datahandler.thrift.fossology.FossologyHostFingerPrint)4 Matchers.isEmptyOrNullString (org.hamcrest.Matchers.isEmptyOrNullString)3 JSchException (com.jcraft.jsch.JSchException)2 FileTransferException (com.axway.ats.common.filetransfer.FileTransferException)1 SshHostKey (com.google.gerrit.common.data.SshHostKey)1 SftpException (com.jcraft.jsch.SftpException)1 UserInfo (com.jcraft.jsch.UserInfo)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 StringWriter (java.io.StringWriter)1 KeyPair (java.security.KeyPair)1 ArrayList (java.util.ArrayList)1 Buffer (org.apache.sshd.common.util.Buffer)1 SimpleGeneratorHostKeyProvider (org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider)1 JcaPEMWriter (org.bouncycastle.openssl.jcajce.JcaPEMWriter)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1 Answer (org.mockito.stubbing.Answer)1