Sto tentando di configurare un server Spring Cloud Config che utilizza un percorso personalizzato per la chiave privata ssh. La ragione per cui ho bisogno di specificare un percorso personalizzato per la chiave è perché l'utente che esegue l'applicazione non ha una directory home .. quindi non c'è modo per me di usare la directory predefinita ~/.ssh
per la mia chiave. So che esiste la possibilità di creare un account di sola lettura e fornire l'utente/password nella configurazione, ma il modo ssh è più pulito.
C'è un modo per impostarlo?Come utilizzare una posizione chiave ssh personalizzata con Spring Cloud Config
risposta
Dopo aver letto molto più codice ... Ho trovato un lavoro relativamente semplice per consentire di impostare qualsiasi chiave SSH che si desidera.
Primo: Creare una classe come segue:
/**
* @file FixedSshSessionFactory.java
*
* @date Aug 23, 2016 2:16:11 PM
* @author jzampieron
*/
import org.eclipse.jgit.transport.JschConfigSessionFactory;
import org.eclipse.jgit.transport.OpenSshConfig.Host;
import org.eclipse.jgit.util.FS;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
/**
* Short Desc Here.
*
* @author jzampieron
*
*/
public class FixedSshSessionFactory extends JschConfigSessionFactory
{
protected String[] identityKeyPaths;
/**
* @param string
*/
public FixedSshSessionFactory(String... identityKeyPaths)
{
this.identityKeyPaths = identityKeyPaths;
}
/* (non-Javadoc)
* @see org.eclipse.jgit.transport.JschConfigSessionFactory#configure(org.eclipse.jgit.transport.OpenSshConfig.Host, com.jcraft.jsch.Session)
*/
@Override
protected void configure(Host hc, Session session)
{
// nothing special needed here.
}
/* (non-Javadoc)
* @see org.eclipse.jgit.transport.JschConfigSessionFactory#getJSch(org.eclipse.jgit.transport.OpenSshConfig.Host, org.eclipse.jgit.util.FS)
*/
@Override
protected JSch getJSch(Host hc, FS fs) throws JSchException
{
JSch jsch = super.getJSch(hc, fs);
// Clean out anything 'default' - any encrypted keys
// that are loaded by default before this will break.
jsch.removeAllIdentity();
for(final String identKeyPath : identityKeyPaths)
{
jsch.addIdentity(identKeyPath);
}
return jsch;
}
}
quindi registrarlo con jgit:
...
import org.eclipse.jgit.transport.SshSessionFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
@SpringBootApplication
@EnableConfigServer
public class ConfigserverApplication
{
public static void main(String[] args) {
URL res = ConfigserverApplication.class.getClassLoader().getResource("keys/id_rsa");
String path = res.getPath();
SshSessionFactory.setInstance(new FixedSshSessionFactory(path));
SpringApplication.run(ConfigserverApplication.class, args);
}
}
Per questo esempio sto memorizzare le chiavi nella cartella/risorse/chiavi src/main cartella e Sto usando il caricatore di classi per ottenere a loro.
Il removeAllIdentities è importante b/c JSch stava caricando la mia chiave ssh predefinita prima di quella che ho specificato e quindi Spring Cloud si stava bloccando b/c è crittografato.
Ciò mi ha permesso di autenticare con successo con bitbucket.
Ho un problema simile perché la mia chiave SSH predefinita è crittografata con una password e quindi non "funziona", il che ha senso perché questa è un'impostazione head-less.
Sono andato alla subacquea in Spring Cloud Config, org.eclipse.jgit e alla fine sono finito in com.jcraft.jsch. La risposta breve è che né JGit né Spring Cloud espongono un modo ovvio per farlo.
JSch supporta chiaramente questa funzione all'interno di un'istanza JSch(), ma non è possibile ottenerla dal livello Spring Cloud. Almeno non quello che potrei trovare in un'ora o più di guardare.
La soluzione FixedSshSessionFactory
di @Jeffrey Zampieron è buona. Tuttavia non funzionerà se si confeziona l'app di avvio a molla come un barattolo di grasso.
polacco un po 'per lavorare con vaso di grasso,
/**
* @file FixedSshSessionFactory.java
* @date Aug 23, 2016 2:16:11 PM
* @author jzampieron
*/
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.jgit.transport.JschConfigSessionFactory;
import org.eclipse.jgit.transport.OpenSshConfig.Host;
import org.eclipse.jgit.util.FS;
import org.springframework.util.StreamUtils;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
/**
* Short Desc Here.
*
* @author jzampieron
*/
@Slf4j
public class FixedSshSessionFactory extends JschConfigSessionFactory {
protected URL[] identityKeyURLs;
/**
* @param url
*/
public FixedSshSessionFactory(URL... identityKeyURLs) {
this.identityKeyURLs = identityKeyURLs;
}
/* (non-Javadoc)
* @see org.eclipse.jgit.transport.JschConfigSessionFactory#configure(org.eclipse.jgit.transport.OpenSshConfig.Host, com.jcraft.jsch.Session)
*/
@Override
protected void configure(Host hc, Session session) {
// nothing special needed here.
}
/* (non-Javadoc)
* @see org.eclipse.jgit.transport.JschConfigSessionFactory#getJSch(org.eclipse.jgit.transport.OpenSshConfig.Host, org.eclipse.jgit.util.FS)
*/
@Override
protected JSch getJSch(Host hc, FS fs) throws JSchException {
JSch jsch = super.getJSch(hc, fs);
// Clean out anything 'default' - any encrypted keys
// that are loaded by default before this will break.
jsch.removeAllIdentity();
int count = 0;
for (final URL identityKey : identityKeyURLs) {
try (InputStream stream = identityKey.openStream()) {
jsch.addIdentity("key" + ++count, StreamUtils.copyToByteArray(stream), null, null);
} catch (IOException e) {
logger.error("Failed to load identity " + identityKey.getPath());
}
}
return jsch;
}
}
Perché non dovrebbe funzionare se confezionato in un barattolo? –
Grazie. Spero che SB aggiungerà una configurazione in futuro. – mvlupan