使用gitUrl作为配置源
gitee 或者github 中有类似于发布密钥的功能,允许通过私钥只读访问指定的仓库,文档中说的是 限制了git的操作为pull 和 clone。生成私钥的方式文档连接在此 https://gitee.com/help/articles/4181#article-header0
spring config只支持PEM格式的私钥,所以在利用ssh-keygen进行生成的时候需要指定格式
ssh-keygen -m PEM -f id_rsa
,执行成功之后,可以在 ~./ssh/ 目录下找到id_rsa.pub id_rsa
两个文件,一个公钥,一个私钥。然后对应平台的公钥管理中,添加公钥,将对应的.pub文件中的内容复制进去,选择好对应的仓库。可以使用已下指令进行测试,这里以gitee平台为例子
文档中使用ssh -T [email protected]
进行测试,但是尝试之后发现,报错ssh: Could not resolve hostname gitee.com: Name or service not known
我使用的另外一个指令 [email protected]:your_username/config-repo.git
这个地址是从对应仓库的ssh地址复制过来的
git ls-remote [email protected]:your_username/config-repo.git
39e47b3xxxxxxxxxxxxxxxxxxxxxxxxxxx2f6101d HEAD
39e47bxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx6101d refs/heads/master
如果成功的话会提示你远程分支的情况。
后面又尝试了一次 ssh -T [email protected]
又可以成功了,成功之后输出如下
ssh -T [email protected]
Hi Anonymous (DeployKey)! You've successfully authenticated, but GITEE.COM does not provide shell access.
Note: Perhaps the current use is DeployKey.
Note: DeployKey only supports pull/fetch operations
以上验证时间为2024年6月24日
以上步骤能通过就说明部署公钥的配置没有问题了,接着在springcloud config 的微服务中将
[email protected]:your_username/config-repo.git
设置为实际仓库地址,spring.cloud.config.server.git.private-key=xxxx
这是为上述步骤生成的 id_rsa
私钥内容地址,这里有一个坑,如果使用的是properties格式的配置文件在配置的时候需要保留输出格式
-----BEGIN RSA PRIVATE KEY-----
...密钥内容
...密钥内容
...密钥内容
-----END RSA PRIVATE KEY-----
比如这是在cat ~/ssh/id_rsa
出来的内容,在复制到配置文件中的时候需要在每一行的末尾追加\n
spring.cloud.config.server.git.private-key=-----BEGIN RSA PRIVATE KEY-----\n\
密钥内容\n\
密钥内容\n\
密钥内容\n\
-----END RSA PRIVATE KEY-----
不然spring config会认为不是一个正确的privatekey报错
Reason: Property 'spring.cloud.config.server.git.privateKey' is not a valid private key
使用Java 生成SSH-RSA格式密钥对
该方法可以用在你没有linux系统的情况下生成密钥对,代码使用4o老师进行生成。
package com.fengxiang.cloudconfig.utils;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.interfaces.RSAPublicKey;
import java.util.Base64;
public class KeyPairGeneratorProcess {
public static void main(String[] args) {
try {
// 1. 创建KeyPairGenerator对象,指定算法为RSA
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
// 2. 初始化KeyPairGenerator,指定密钥长度
keyPairGenerator.initialize(2048);
// 3. 生成密钥对
KeyPair keyPair = keyPairGenerator.generateKeyPair();
// 4. 获取公钥和私钥
PublicKey publicKey = keyPair.getPublic();
PrivateKey privateKey = keyPair.getPrivate();
// 5. 打印密钥(以十六进制编码或Base64编码)
System.out.println("Public Key: " + bytesToHex(publicKey.getEncoded()));
System.out.println("Private Key: " + bytesToHex(privateKey.getEncoded()));
// 5. 以ssh-rsa格式输出公钥
String sshPublicKey = encodeSSHRSAPublicKey(publicKey, "[email protected]");
System.out.println("SSH Public Key: " + sshPublicKey);
// 6. 以PEM格式输出私钥
String pemPrivateKey = encodePEMPrivateKey(privateKey);
System.out.println("PEM Private Key:\n" + pemPrivateKey);
} catch (Exception e) {
e.printStackTrace();
}
}
private static String encodeSSHRSAPublicKey(PublicKey publicKey, String comment) throws Exception {
RSAPublicKey rsaPublicKey = (RSAPublicKey) publicKey;
byte[] sshRsaPrefix = new byte[] {
0x00, 0x00, 0x00, 0x07, 's', 's', 'h', '-', 'r', 's', 'a'};
byte[] exponent = rsaPublicKey.getPublicExponent().toByteArray();
byte[] modulus = rsaPublicKey.getModulus().toByteArray();
byte[] result = new byte[sshRsaPrefix.length + 4 + exponent.length + 4 + modulus.length];
int i = 0;
System.arraycopy(sshRsaPrefix, 0, result, i, sshRsaPrefix.length);
i += sshRsaPrefix.length;
result[i++] = (byte) ((exponent.length >> 24) & 0xff);
result[i++] = (byte) ((exponent.length >> 16) & 0xff);
result[i++] = (byte) ((exponent.length >> 8) & 0xff);
result[i++] = (byte) (exponent.length & 0xff);
System.arraycopy(exponent, 0, result, i, exponent.length);
i += exponent.length;
result[i++] = (byte) ((modulus.length >> 24) & 0xff);
result[i++] = (byte) ((modulus.length >> 16) & 0xff);
result[i++] = (byte) ((modulus.length >> 8) & 0xff);
result[i++] = (byte) (modulus.length & 0xff);
System.arraycopy(modulus, 0, result, i, modulus.length);
String base64Key = Base64.getEncoder().encodeToString(result);
return "ssh-rsa " + base64Key + " " + comment;
}
private static String encodePEMPrivateKey(PrivateKey privateKey) {
byte[] keyBytes = privateKey.getEncoded();
String base64Key = Base64.getMimeEncoder(64, new byte[] {
'\n'}).encodeToString(keyBytes);
return "-----BEGIN PRIVATE KEY-----\n" + base64Key + "\n-----END PRIVATE KEY-----";
}
// 将字节数组转换为十六进制字符串的辅助方法
public static String bytesToHex(byte[] bytes) {
StringBuilder hexString = new StringBuilder();
for (byte b : bytes) {
String hex = Integer.toHexString(0xff & b);
if (hex.length() == 1) {
hexString.append('0');
}
hexString.append(hex);
}
return hexString.toString();
}
}
需要注意的一点是使用这种方式生成的privatekey进行配置的时候
spring.cloud.config.server.git.private-key=-----BEGIN PRIVATE KEY-----
这部分的字符串是没有RSA的
加密
对于配置文件在修改和配置的过程中都需要存放到第三方的或者自建的git服务中,这个过程中可能会导致密钥的泄露,可以使用spring.cloud.config.server.git.private-key={cipher}95376ecf73615297e072fe0819d9cefd85f
这种形式来避免明文泄露,同时在configserver的bootstrap.yml设置好对应的参数,该文件不必暴露到git仓库中
encrypt.key=123456
encrypt.salt=123456
spring.cloud.config.server.encrypt.enabled=true
然后在使用明文请求该服务的/encrypt 接口,即可获取到加密之后的密文。只要妥善保存好encrypt.key salt就可以将任何password privateKey通过第三方或者公开的git 仓库进行共享