简体   繁体   English

Kubernate Pod:如何将证书导入 Java 信任库?

[英]Kubernate Pod : How to import Certificate into Java Truststore?

So in my environment i am using Docker and Kubernete,now i have to import a certificate in Pods Java Keystore.所以在我的环境中,我使用的是 Docker 和 Kubernetes,现在我必须在 Pods Java Keystore 中导入证书。 But my Container running as a NON-ROOT user.When i am trying to import the certificate to Java Keystore its failing as to accessing Java Keystore user have to be root user only.但是我的容器以NON-ROOT root 用户身份运行。当我尝试将证书导入 Java 密钥库时,它无法访问 Java 密钥库用户只能是 root 用户。

keytool -importcert -alias keycloakTest -keystore $JAVA_HOME/lib/security/cacerts -file $WSO2_SERVER_HOME/keycloak.crt -storepass changeit -noprompt

Now How i can import the certificate into Java trust store?现在我如何将证书导入 Java 信任库? Currently i am doing this manually but later i have to do this through DockerFile目前我正在手动执行此操作,但后来我必须通过DockerFile执行此操作

COPY /carFiles/keycloak.crt  $WSO2_SERVER_HOME/
CMD keytool -importcert -alias keycloakTest -keystore $JAVA_HOME/lib/security/cacerts -file $WSO2_SERVER_HOME/keycloak.crt -storepass changeit -noprompt

In Kubernete Pods Java run as a root user only?在 Kubernetes Pods Java 中仅以root用户身份运行?

Edit编辑

I made changes in Docker File something like this我在 Docker 文件中进行了类似这样的更改

COPY /carFiles/keycloak.crt /opt
CMD keytool -importcert -alias keycloakTest -keystore $JAVA_HOME/lib/security/cacerts -file /opt/keycloak.crt -storepass changeit -noprompt

So from Pods Shell i am able to see certificate Copied to /opt directory but next CMD command wont able to add the certificate into the truststore of java所以从 Pods Shell 我可以看到证书复制到/opt目录但下一个CMD命令将无法将证书添加到 Z93F725A07423FE1C889ZF448B33D21F46 的信任库中

在此处输入图像描述

Enter key or you will be logged out any time after 101 min.输入键,否则您将在 101 分钟后随时退出。

spec:
      volumes:
        - name: certs
          emptyDir: {}
      initContainers:
        - name: {{ .Chart.Name }}-create-keystore-truststore
          securityContext:
            runAsNonRoot: true
            runAsUser: 100
          image: >-
            <HOST>/foapi-tools:20.0.1

 command:
            - sh
            - '-c'
            - >
             # Import Keycloak server certificate into truststore

              # Extract host name from ckey URL

              KEYCLOAK_HOST=$(echo
              "https://<HOST>/auth" | sed
              's|[^/]*//\([^/]*\)/.*|\1|')

              echo "Importing server cert of '${KEYCLOAK_HOST}'..."

              openssl s_client -connect ${KEYCLOAK_HOST}:443 2>/dev/null
              </dev/null | openssl x509 > keycloak.crt

              ls -lat /certs/truststore;
          workingDir: /certs/truststore
          resources:
            limits:
              cpu: 200m
              memory: 200Mi
            requests:
              cpu: 200m
              memory: 100Mi
          volumeMounts:
            - name: certs
              mountPath: /certs/truststore
          terminationMessagePath: /dev/termination-log
          terminationMessagePolicy: File
          imagePullPolicy: IfNotPresent

As per the shared information, you are using WSO2 MI and trying to generate a token from the Mediation sequence with Keycloak.根据共享信息,您正在使用 WSO2 MI 并尝试使用 Keycloak 从中介序列生成令牌。 In such a case, it is not required to add the Keycloak's cert to the cacerts of the JDK.在这种情况下,不需要将 Keycloak 的证书添加到 JDK 的cacerts中。 You can make use of the WSO2 MI's client-truststore.jks to import the certs.您可以使用 WSO2 MI 的client-truststore.jks来导入证书。

To access the /opt path, the user needs root permission.要访问/opt路径,用户需要 root 权限。 As WSO2 MI itself contains a truststore and not requires any root permissions to import the certs, you can simply import the Keycloak's TLS public cert in there.由于 WSO2 MI 本身包含一个信任库,并且不需要任何 root 权限来导入证书,您可以简单地在其中导入 Keycloak 的 TLS 公共证书。 Given is a sample Dockerfile to import the certs给出的是用于导入证书的示例Dockerfile

COPY /carFiles/keycloak.crt $WSO2_SERVER_HOME/repository/resources/security/
RUN keytool -importcert -alias keycloakTest -keystore $WSO2_SERVER_HOME/repository/resources/security/client-truststore.jks -file $WSO2_SERVER_HOME/repository/resources/security/keycloak.crt -storepass changeit -noprompt

Note: Replace the CMD with the RUN command, as you are trying to import the certs to the truststore as part of building the images.注意:将CMD替换为RUN命令,因为您正在尝试将证书导入到信任库作为构建映像的一部分。 And not setting that command as an entrypoint to start the containers.并且不将该命令设置为启动容器的入口点。

Further, if you have a certifier chain, you have to include all (root, intermediate, and leaf certs) in the truststore.此外,如果您有一个验证者链,则必须在信任库中包含所有(根证书、中间证书和叶证书)。 Importing only one of them will result again in an SSL Handshake error.仅导入其中一个将再次导致 SSL 握手错误。

This is up to your container, or more specifically the file permissions on the jks file.这取决于您的容器,或者更具体地说,取决于 jks 文件的文件权限。 That file is part of your image and is presumably owned by uid 0 and mode 660 or something, so would need to change something for this to work.该文件是您的图像的一部分,并且可能由 uid 0 和模式 660 或其他内容拥有,因此需要更改某些内容才能使其正常工作。 Either do this at build time or change ownership/mode of the file or use a different jks file.在构建时执行此操作或更改文件的所有权/模式或使用不同的 jks 文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM