Python和JDK SSL证书

时间:2014-01-07 09:39:41

标签: python keytool

我想知道是否有人曾经使用python处理过将SSL证书导入JAVA,就像我们使用keytool一样。

bash脚本可以做,但我只是想知道是否有一个模块可以做得更优雅。

谢谢

2 个答案:

答案 0 :(得分:1)

您可以尝试pyjks,但我不知道它是否可以写入JKS密钥库或只是阅读它们。

答案 1 :(得分:0)

我有这个小的脚本可以导入证书:

#!/bin/bash

#set -xeuo pipefail
set -xe

##cert file location
cert_location=/path/to/Certificates
TIMESTAMP=$(date +%Y-%m-%d_%H-%M-%S)

##check if directory exist
if [ -d "$cert_location" ]; then
    echo -e "\033[31;7m Directory $cert_location exists. \e[0m"
else
    echo -e "\033[31;7m Directory $cert_location does NOT exists. \e[0m"
    return 1 2>/dev/null
    exit 1
fi

# define file array
cd $cert_location
files=(*)


# Use for loop iterate through an array
# $f stores current value
for f in "${files[@]}"; do
    ##check fi alias already in use
    echo -e "\033[31;7m Directory Certificate name $f. \e[0m"
    alias_name=$(keytool -list -keystore /u01/jdk/jre/lib/security/cacerts -storepass JAVA_PASSWORD | grep $f | awk -F, '{print $1}')
    if [ -z "$alias_name" ]; then
        echo -e "\033[31;7m $f will be installed \e[0m"
        scp -r -i private_key -o 'StrictHostKeyChecking no' $cert_location/"$f" "${SSH_SOA}":./deploy/"$f"
        echo -e "\033[31;7mLoading the $f file  \e[0m"
        keytool -import -v -trustcacerts -alias "$f_$TIMESTAMP" -file ./deploy/"$f" -keystore /u01/app/oracle/middleware/wlserver/server/lib/DemoTrust.jks -storepass KEYSTORE_PASSWORD -noprompt | true
        keytool -import -v -trustcacerts -alias "$f_$TIMESTAMP" -file ./deploy/"$f" -keystore /u01/jdk/jre/lib/security/cacerts -storepass JAVA_PASSWORD -noprompt | true
        keytool -import -v -trustcacerts -alias "$f_$TIMESTAMP" -file ./deploy/"$f" -keystore /u01/app/oracle/middleware/wlserver/server/lib/cacerts -storepass JAVA_PASSWORD -noprompt | true
        keytool -import -v -trustcacerts -alias "$f_$TIMESTAMP" -file ./deploy/"$f" -keystore /u01/data/domains/intsoaol_domain/security/DemoIdentity.jks -storepass JAVA_PASSWORD -noprompt | true
    else
        echo -e "\033[31;7m $alias_name Alias already in use \e[0m"
    fi
done