9

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

4

3 に答える 3

9

さらに多くのコードを読んだ後、必要な 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/main/resources/keys フォルダーに保存し、クラス ローダーを使用してキーを取得しています。

removeAllIdentities は重要です。JSch は、指定したキーよりも前にデフォルトの ssh キーをロードしていたため、Spring Cloud が暗号化されていたため、クラッシュしていました。

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

于 2016-08-23T19:17:17.223 に答える