spring cloud config从https git仓库获取配置时出错

问题

Prong项目的git仓库使用的是gogs,gogs配置自签名的ssl证书。spring cloud config的bootstrap.yml相关配置如下:

spring:
  application:
    name: prong-cloud-config
  cloud:
    config:
      server:
        git:
          uri: https://xxx/prong/prong-config
          username: prong-config
          password: xxx
          clone-on-start: true

在eclipse启动prong-cloud-config服务正常,但在docker中运行时报错,关键的错误信息:

Caused by: org.eclipse.jgit.api.errors.TransportException: https://xxx/prong/prong-config: cannot open git-upload-pack
...
Caused by: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
...
Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

分析问题

通过 java 执行 https 请求时使用特定的数据识别 Certificate Authorities(CA),如果你试图发起的 SSL/TLS 连接使用不属于这些根证书机构所颁发的证书,就会抛出该错误:

SunCertPathBuilderException: unable to find valid certification path to requested target

因此,如果你使用自签名证书,或者根证书机构不在 JRE 默认信任列表中,则需要向 JRE 导入根证书。

解决问题

1、获取gogs的自签名证书cert.pem,放入prong-cloud-config工程目录并重命名为gogs-cert.pem

2、Dockerfile中增加ADD gogs-cert.pem gogs-cert.pem

FROM prong/openjdk:8-jdk-alpine

ADD entrypoint.sh entrypoint.sh
RUN chmod +x entrypoint.sh

ADD ${PWD}/target/dependency/ /dependency

ARG JAR_FILE
ADD ${PWD}/target/${JAR_FILE} app.jar
RUN sh -c 'touch /app.jar'

ADD gogs-cert.pem gogs-cert.pem

ENTRYPOINT ["/bin/bash","-c"]
CMD ["/entrypoint.sh"]

3、entrypoint.sh中增加keytool -importcert ...

#!/bin/sh

keytool -importcert -alias startssl -keystore $JAVA_HOME/jre/lib/security/cacerts -storepass changeit -file gogs-cert.pem -noprompt

echo SPRING_CLOUD_CONFIG_PROFILE=$SPRING_CLOUD_CONFIG_PROFILE

java $JAVA_OPTS -Dapp.alias=${APP_ALIAS} -Djava.security.egd=file:/dev/./urandom  -Dspring.profiles.active=${SPRING_PROFILES_ACTIVE} -Dspring.cloud.config.profile=${SPRING_CLOUD_CONFIG_PROFILE} -Dloader.path="/dependency" -jar /app.jar

注意,-noprompt参数的作用是导入证书时不进行人工交互提示,否则会docker容器中导入证书时报错:

keytool error: java.lang.NullPointerException

4、重新构建prong-cloud-config服务的镜像并运行,问题解决。

你可能感兴趣的:(spring cloud config从https git仓库获取配置时出错)