Kubernetes - PEM is invalid: no begin marker

本文解决了一个在fabric8 Kubernetes客户端中ConfigBuilder使用withClientKeyData()方法时出现的问题,该方法在3.1.8版本中无法正确处理密钥数据,通过将密钥数据Base64编码后传递解决了此问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ConfigBuilder.withClientKeyData() is broken in 3.1.8.

withClientCertData and withCaCertData work just fine.

The following code snippit shows the problem and the workaround.

boolean useWorkaround = true;
if (useWorkaround) {
    File tempWorkaroundFile = null;
    try {
        tempWorkaroundFile = java.nio.file.Files.createTempFile("workaround", "tmp").toFile();
        Files.write(key, tempWorkaroundFile, Charsets.UTF_8);
        configBuilder = configBuilder.withClientCertFile(tempWorkaroundFile.getAbsolutePath());
    } finally {
        tempWorkaroundFile.delete();
    }
 } else {
    configBuilder = configBuilder.withClientKeyData(key);
}

The problem manifests as:

Caused by: java.io.IOException: PEM is invalid: no begin marker
	at io.fabric8.kubernetes.client.internal.CertUtils.decodePem(CertUtils.java:220)
	at io.fabric8.kubernetes.client.internal.CertUtils.createKeyStore(CertUtils.java:105)
	at io.fabric8.kubernetes.client.internal.CertUtils.createKeyStore(CertUtils.java:196)
	at io.fabric8.kubernetes.client.internal.SSLUtils.keyManagers(SSLUtils.java:127)
	at io.fabric8.kubernetes.client.internal.SSLUtils.keyManagers(SSLUtils.java:121)
	at io.fabric8.kubernetes.client.utils.HttpClientUtils.createHttpClient(HttpClientUtils.java:64)
	... 38 more

ConfigBuilder appears to be auto-generated so it is not clear to me how to fix this.

解决方案

It’s not a bug I learned with trial and error. Although it would make sense that the method would accept the contents of the file (which is already armored plaintext), it wants to see the key/cert data base64 encoded again.

Ps:看源码就知道,每次密钥文件会进行 base64 解密,但是这个解密并不是必然事件,随机性的,所以当不必要 base64 解密的时候但是源码进行了解密的话就会出现乱码问题,导致上面的报错信息,所以可以统一给密钥文件先加密 base64。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

陆克和他的代码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值