2015-11-19 12 views
9

Spring Cloud Config ssh秘密鍵のカスタムロケーションを使用するサーバをセットアップしようとしています。 私はキーのカスタム位置を指定する必要があるのは、アプリケーションを実行しているユーザーがホームディレクトリを持たないからです。私のためにデフォルトの~/.sshディレクトリを使用する方法はありません。 私は読み取り専用のアカウントを作成し、設定でユーザー/パスワードを提供するオプションがあることを知っていますが、sshの方法ではよりシームレスです。
これをセットアップする方法はありますか?Spring Cloud ConfigでカスタムSSH鍵の場所を使用する方法

答えて

8

もっと多くのコードを読んだ後、私はあなたが望むSSHキーを設定することができる比較的簡単な回避策を見つけました。

まず次のようにクラスを作成します。

/** 
* @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; 
    } 


} 

を次に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); 
    } 

} 

この例では、私はSRC /メイン/リソース/キーのキーを格納していますフォルダと 私はそれらを得るためにクラスローダーを使用しています。

removeAllIdentitiesは重要ですb/c JSchは私の指定したsshキーをロードしてから、Spring Cloudがその暗号化されたb/cをクラッシュさせてしまいました。

これにより、bitbucketで正常に認証することができました。

+0

ありがとうございました。私はSBが将来的に設定を追加することを願っています。 – mvlupan

0

私のデフォルトのSSH鍵はパスワードで暗号化されているため、これは「うまく動作しません」という同じ問題を抱えています。これはヘッドレス設定なので意味があります。

私はSpring Cloud Config、org.eclipse.jgitにソースダイビングし、最終的にcom.jcraft.jschで終了しました。簡単な答えは、JGitもSpring Cloudもこれを行うための明白な方法を公開していないことです。

JSchはJSch()インスタンス内でこの機能をサポートしていますが、Spring CloudレベルからJSchにアクセスすることはできません。少なくとも私は1時間ほどで見ることができたわけではありません。

2

@Jeffrey Zampieronのソリューションが良好です。しかし、それはファットジャーとして春のブートアプリケーションをパッケージ化する場合は動作しません。

ポーランドそれ脂肪jarファイルを操作するためのビット、

/** 
* @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; 
    } 


} 
+1

jarファイルにパッケージ化されているのはなぜですか? –

関連する問題