Spring Cloud Config ssh秘密鍵のカスタムロケーションを使用するサーバをセットアップしようとしています。 私はキーのカスタム位置を指定する必要があるのは、アプリケーションを実行しているユーザーがホームディレクトリを持たないからです。私のためにデフォルトの~/.ssh
ディレクトリを使用する方法はありません。 私は読み取り専用のアカウントを作成し、設定でユーザー/パスワードを提供するオプションがあることを知っていますが、sshの方法ではよりシームレスです。
これをセットアップする方法はありますか?Spring Cloud ConfigでカスタムSSH鍵の場所を使用する方法
答えて
もっと多くのコードを読んだ後、私はあなたが望む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で正常に認証することができました。
私のデフォルトのSSH鍵はパスワードで暗号化されているため、これは「うまく動作しません」という同じ問題を抱えています。これはヘッドレス設定なので意味があります。
私はSpring Cloud Config、org.eclipse.jgitにソースダイビングし、最終的にcom.jcraft.jschで終了しました。簡単な答えは、JGitもSpring Cloudもこれを行うための明白な方法を公開していないことです。
JSchはJSch()インスタンス内でこの機能をサポートしていますが、Spring CloudレベルからJSchにアクセスすることはできません。少なくとも私は1時間ほどで見ることができたわけではありません。
@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;
}
}
jarファイルにパッケージ化されているのはなぜですか? –
- 1. Spring Cloud Config with AWS CodeCommit SSH
- 2. Spring Cloud Configカスタム環境リポジトリ
- 3. クラウドファウンドリでspring cloud-serverでssh github repoを使用する方法
- 4. Spring Cloud Configはssh鍵を使用してプライベートbitbucketリポジトリを複製できません
- 5. Spring Cloud Config(1.5.3.RELEASE)とSpring Cloud Config(1.3.0.RELEASE)を使用してSpring Cloud Config Serverを作成する方法を教えてください。
- 6. spring-cloud-configサーバ用のSSL鍵の設定
- 7. Spring Cloud Configを使用したカスタムプロパティローダー
- 8. 問題点spring-cloud-configとspring-cloud-bus
- 9. Vaultバックエンドを使用したSpring Cloud VaultとSpring Cloud Configの違い
- 10. Gitリポジトリを使用しないSpring Cloud Configの使用
- 11. Spring Cloud Configの暗号化
- 12. Spring Cloud Config Serverの問題
- 13. デフォルトのssh鍵の場所を設定する
- 14. Spring Cloud Config Serverからシステム変数を設定する方法
- 15. Spring Cloud DataFlow - TCPソースでカスタムTCPエンコーダ/デコーダを使用する方法
- 16. Spring-Cloud-Configサーバーのカスタム暗号化/復号化
- 17. Spring Cloud Config Server - プレースホルダ・ラベル
- 18. Spring Cloud MicroserviceでSpring AMQPを使用する方法
- 19. flywayでカスタムSQLスクリプトの場所を使用するには?
- 20. Azure Service FabricとカスタムAzure Cloudを利用する場合の長所と短所
- 21. spring-cloud-config-serverでの暗号化
- 22. Spring Cloud構成対称鍵
- 23. Spring Cloud Config(Vaultバックエンド)が早すぎる
- 24. Spring MockMVCをカスタムSpringセキュリティで使用する方法WebSecurityConfigurerAdapter
- 25. Spring Cloud Configのブランチ名(ラベル)内のスラッシュ文字をエスケープする方法
- 26. Spring Cloud Config Server - セキュリティの詳細
- 27. Java Configを使用する場合のSpringバッチ・テーブル・プレフィックス
- 28. Spring Cloud Config:クライアントの起動後に設定を更新する方法は?
- 29. SSH鍵を使用してSSH接続を検証する
- 30. PWS Config-Serverを使用するためにSpring-Cloud-ConnectorでSpringブートを構成する
ありがとうございました。私はSBが将来的に設定を追加することを願っています。 – mvlupan