-
Notifications
You must be signed in to change notification settings - Fork 153
[QA-4212] generate root-CA/sub-CA/keystore/truststore, import to keystore/truststore; #196
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
YipingXiongTG
wants to merge
20
commits into
master
Choose a base branch
from
QA-4212
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
0d4545b
QA-4212 kafka ssl ca generate scripts
YipingXiongTG 60dcd44
QA-4212 new version
YipingXiongTG 201e074
QA-4212 modify file path
YipingXiongTG 8b37efa
QA-4212 modify help message
YipingXiongTG 17b2323
QA-4212 password check
YipingXiongTG ec6ca7a
new version
YipingXiongTG 55b4444
Merge branch 'master' into yiping
YipingXiongTG 00f30e0
QA-4212 add universal script for ssl
YipingXiongTG 7ce1ff3
QA-4212 modify example
YipingXiongTG 835f6bf
QA-4212 update usage
YipingXiongTG d09ad14
QA-4212 delete the firstCA in truststore
YipingXiongTG 3785cad
add warning in gen_truststore
YipingXiongTG ea287f1
QA-4212 update import_to_keystore
YipingXiongTG c021311
QA-4212 update ssl_generate script
YipingXiongTG f4bf23c
QA-4212 support openJDK1.8.0 and later
YipingXiongTG a156793
QA-4212 ssl_generate.sh and ssl_import.sh
YipingXiongTG 14b586d
QA-4212 change private_key to root-CA private_key
YipingXiongTG 8cc0ed8
QA-4212 add ssl_example
YipingXiongTG acf0d5a
QA-4212 modify README
YipingXiongTG f08eb47
QA-4212 add default value
YipingXiongTG File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| # SSL - certificates/keystore/truststore generate | ||
| # SSL - import key/certificate pairs into an existing keystore | ||
| # SSL - import certificate into an existing truststore | ||
|
|
||
| # help commands | ||
| 1. bash ssl_generate.sh -h | ||
| 2. bash ssl_import.sh -h | ||
|
|
||
| # help options | ||
| 1. bash ssl_generate.sh -gen_keystore -h | ||
| 2. bash ssl_import.sh -import_to_keystore -h | ||
|
|
||
| # Example | ||
| 1. Only generate a Certificate Authority (CA) key and certificate | ||
| - `bash ssl_generate.sh -gen_CARoot -c kafka-0.tigergraph.com -p 123456` | ||
| - 'kafka-0.tigergraph.com' is the CN of Certificate Authority (CA) | ||
| - '123456' is the passphrase of CA private_key | ||
|
|
||
|
|
||
| 2. Only generate a keystore | ||
| - `bash ssl_generate.sh -gen_keystore -c kafka-0.tigergraph.com -storepass 123456` | ||
| - 'kafka-0.tigergraph.com' is the Subject CN of keystore | ||
| - '123456' is the storepass of keystore | ||
|
|
||
|
|
||
| 3. Only generate an empty truststore | ||
| - `bash ssl_generate.sh -gen_truststore -storepass 123456` | ||
| - '123456' is the storepass of truststore | ||
|
|
||
|
|
||
| 4. At the same time, generate CARoot/CARoot private_key, keystore, and an empty truststore | ||
| - `bash ssl_generate.sh -c kafka-0.tigergraph.com` | ||
| - 'kafka-0.tigergraph.com' is the CN | ||
| - The default passphrase of CA private_key is 'tiger123' | ||
| - The default storepass of keystore and truststore is 'tiger123' | ||
|
|
||
|
|
||
| 5. Sign sub-certificates with an existing certificate (CARoot or other Superior certificate) | ||
| - `bash ssl_generate.sh -gen_subCA -cer ./SSL_OUTPUT/ca-root.crt -cerKey ./SSL_OUTPUT/ca-root.key -p 123456` -c tigergraph | ||
| - './SSL_OUTPUT/ca-root.crt' is the path of higher-level CA | ||
| - './SSL_OUTPUT/ca-root.key' is the path of higher-level CA private_key | ||
| - '123456' is the passphrase of higher-level CA private_key | ||
| - 'tigergraph' is the CN of your sub-certificate | ||
|
|
||
|
|
||
| 6. Import key/certificate pairs into an existing keystore | ||
| - `bash ssl_import.sh -import_to_keystore -keystore ./SSL_OUTPUT/server.keystore -cer ./SSL_OUTPUT/ca-root.crt -cerKey ./SSL_OUTPUT/ca-root.key -storepass 123456 -p tiger123` | ||
| - './SSL_OUTPUT/server.keystore' is the path of your keystore | ||
| - './SSL_OUTPUT/ca-root.crt' is the certificate path to be imported | ||
| - './SSL_OUTPUT/ca-root.key' is the certificate private_key path to be imported | ||
| - '123456' is the storepass of keystore | ||
| - 'tiger123' is the passphrase of the certificate private_key | ||
|
|
||
|
|
||
| 7. Import certificate into an existing truststore | ||
| - `bash ssl_import.sh -import_to_truststore -truststore ./SSL_OUTPUT/server.truststore -cer ./SSL_OUTPUT/ca-root.crt -storepass 123456` | ||
| - './SSL_OUTPUT/server.truststore' is the path of your truststore | ||
| - './SSL_OUTPUT/ca-root.crt' is the certificate path to be imported | ||
| - '123456' is the storepass of the truststore |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| #! /bin/bash | ||
|
|
||
| cd $(dirname $0) | ||
| BASE_DIR=$(pwd) | ||
|
|
||
| broker_hostname=${1:-kafka-0.tigergraph.com} | ||
| client_hostname=${2:-tigergraph} | ||
| output_path=./SSL_OUTPUT | ||
|
|
||
| cleanup() { | ||
| if [ ! -z "${output_path}" -a -d ${output_path} ]; then | ||
| rm -fr ${output_path} | ||
| fi | ||
| } | ||
|
|
||
| # cleanup | ||
| cleanup | ||
|
|
||
| ## step1: Generate a Certificate Authority (CA) private_key/certificate, keystore and truststore | ||
| bash ssl_generate.sh | ||
|
|
||
| ## step2: generate and sign Kafka broker private_key/certificate | ||
| bash ssl_generate.sh -gen_subCA -cer ${output_path}/ca-root.crt -cerKey ${output_path}/ca-root.key -c ${broker_hostname} | ||
|
|
||
| ## step3: import CA key/certificate pairs to keystore | ||
| bash ssl_import.sh -import_to_keystore -keystore ${output_path}/server.keystore -cer ${output_path}/ca-root.crt -cerKey ${output_path}/ca-root.key | ||
|
|
||
| ## step4: import Kafka broker private_key/certificate in keystore | ||
| bash ssl_import.sh -import_to_keystore -keystore ${output_path}/server.keystore -cer ${output_path}/${broker_hostname}.crt -cerKey ${output_path}/${broker_hostname}.key | ||
|
|
||
| ## step5: generate and sign client private_key/certificate | ||
| bash ssl_generate.sh -gen_subCA -cer ${output_path}/ca-root.crt -cerKey ${output_path}/ca-root.key -c ${client_hostname} | ||
|
|
||
| ## step6: import CA certificate in trustStore | ||
| bash ssl_import.sh -import_to_truststore -truststore ${output_path}/server.truststore -cer ${output_path}/ca-root.crt |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,223 @@ | ||
| #!/bin/bash | ||
|
|
||
| cd $(dirname $0) | ||
| BASE_DIR=$(pwd) | ||
|
|
||
| source_file() { | ||
| file=$1 | ||
| msg="$2" | ||
| src_flag=$3 | ||
| if [ -f "$file" ]; then | ||
| if [ "$src_flag" != "false" ]; then | ||
| source $file | ||
| fi | ||
| else | ||
| echo $(tput setaf 1) "[ERROR ]: $msg" $(tput sgr0) | ||
| exit 1 | ||
| fi | ||
| } | ||
|
|
||
| # source all functions | ||
| source_file utils/pretty_print "File utils/pretty_print NOT found, exit" true | ||
| source_file utils/env_utils "File utils/env_utils NOT found, exit" true | ||
| source_file utils/ssl_utils "File utils/ssl_utils NOT found, exit" true | ||
|
|
||
| OSG=$(get_os) | ||
| OS=$(echo "$OSG" | cut -d' ' -f1) | ||
| version=$(echo "$OSG" | cut -d' ' -f2) | ||
| OSV="$OS$(echo "$version" | cut -d'.' -f1)" | ||
|
|
||
| generate_root=${BASE_DIR}/SSL_OUTPUT | ||
| CN=kafka-0.tigergraph.com | ||
| storetype=jks | ||
| pass=tiger123 | ||
| storepass=tiger123 | ||
| storeName="" | ||
| CA="" | ||
| CAkey="" | ||
|
|
||
| CARoot_flag="" | ||
| subCA_flag="" | ||
| genKeystore_flag="" | ||
| genTruststore_flag="" | ||
| help_flag="" | ||
|
|
||
| opt_string="hip:c:s:o:n:" | ||
| opt_long_string="help,gen_CARoot,gen_subCA,gen_keystore,gen_truststore,passphrase:,output:,storepass:,storetype:,cer:,cerKey:,CN:,name:" | ||
| ARGS=`getopt -a -o $opt_string --long $opt_long_string -- "$@"` | ||
|
|
||
| if [ $? != 0 ] ; then exit 1 ; fi | ||
| eval set -- "${ARGS}" | ||
| while : | ||
| do | ||
| case $1 in | ||
| -h|--help) | ||
| help_flag=true | ||
| ;; | ||
| --gen_CARoot) | ||
| CARoot_flag=true | ||
| ;; | ||
| --gen_subCA) | ||
| subCA_flag=true | ||
| ;; | ||
| --gen_keystore) | ||
| genKeystore_flag=true | ||
| ;; | ||
| --gen_truststore) | ||
| genTruststore_flag=true | ||
| ;; | ||
| --cer) | ||
| CA=`path_conver $2` | ||
| shift | ||
| ;; | ||
| --cerKey) | ||
| CAkey=`path_conver $2` | ||
| shift | ||
| ;; | ||
| -o|--output) | ||
| generate_root=$2 | ||
| if [ ! -d ${generate_root} ]; then | ||
| warn "The path '$generate_root' does not exist" | ||
| prog "start creating output directory..." | ||
| mkdir -p $generate_root | ||
| fi | ||
| generate_root=`path_conver $generate_root` | ||
| shift | ||
| ;; | ||
| -p|--passphrase) | ||
| pass=$2 | ||
| if [ ${#pass} -lt 6 ];then | ||
| error "Password is too short - must be at least 6 characters." | ||
| exit 1 | ||
| fi | ||
| shift | ||
| ;; | ||
| --storepass) | ||
| storepass=$2 | ||
| shift | ||
| ;; | ||
| -c|--CN) | ||
| CN=$2 | ||
| shift | ||
| ;; | ||
| -s|--storetype) | ||
| storetype=$2 | ||
| shift | ||
| ;; | ||
| -n|--name) | ||
| storeName=$2 | ||
| shift | ||
| ;; | ||
| -i|--install) | ||
| SETUP_JDK=true | ||
| SETUP_OPENSSL=true | ||
| ;; | ||
| --) | ||
| shift | ||
| break | ||
| ;; | ||
| *) | ||
| error "${bldred}Invalid option, the correct usage is described below: $txtrst" | ||
| generate_help | ||
| ;; | ||
| esac | ||
| shift | ||
| done | ||
|
|
||
| if [[ ! -z $help_flag ]]; then | ||
| if [[ ! -z $CARoot_flag ]]; then | ||
| general_usage gen_CARoot | ||
| elif [[ ! -z $subCA_flag ]]; then | ||
| general_usage gen_subCA | ||
| elif [[ ! -z $genKeystore_flag ]]; then | ||
| general_usage gen_keystore | ||
| elif [[ ! -z $genTruststore_flag ]]; then | ||
| general_usage gen_truststore | ||
| else | ||
| generate_help | ||
| fi | ||
| exit 0 | ||
| else | ||
| # this script only support rhel/centos | ||
| prog "Checking operation system (OS) version ..." | ||
| check_os $OS $version | ||
|
|
||
| prog "Checking root/sudo ..." | ||
| check_root | ||
|
|
||
| # Using option '-i/--install' will install openjdk-1.8.0 and openssl, | ||
| # otherwise openjdk-1.8.0 and openssl will not be installed | ||
| # install openJDK | ||
| install_openJDK | ||
| # install openssl | ||
| install_openssl | ||
|
|
||
| # If the command is empty, --gen_CARoot, --gen_keystore, and --gen_truststore are executed by default | ||
| total_flag=($CARoot_flag $genKeystore_flag $subCA_flag $genTruststore_flag) | ||
| if [[ -z $(IFS=,; echo "${total_flag[*]}") ]]; then | ||
| CARoot_flag=true | ||
| genKeystore_flag=true | ||
| genTruststore_flag=true | ||
| note "The input command is empty." | ||
| note "'--gen_CARoot', '--gen_keystore', and '--gen_truststore' are executed by default." | ||
| fi | ||
|
|
||
| # generate root CA | ||
| if [[ ! -z $CARoot_flag ]]; then | ||
| prog "root-CA output directory: $generate_root" | ||
| prog "root-CA CN: $CN" | ||
| CA=${generate_root}/ca-root.crt | ||
| CAkey=${generate_root}/ca-root.key | ||
|
|
||
| check_file ${CA} 0 | ||
| check_file ${CAkey} 0 | ||
| generate_CARoot $generate_root $CN $pass | ||
| fi | ||
|
|
||
| # generate keystore | ||
| if [[ ! -z $genKeystore_flag ]]; then | ||
| if [[ -z $storeName ]]; then | ||
| keystoreName=server.keystore | ||
| else | ||
| keystoreName=${storeName}.keystore | ||
| fi | ||
| prog "keystore output directory: $generate_root" | ||
| prog "Keystore -Dname CN: $CN" | ||
| prog "keystore name: $keystoreName" | ||
| generate_keystore ${generate_root} ${storepass} ${CN} ${storetype} ${keystoreName} | ||
| keystore=${generate_root}/${keystoreName} | ||
| prog "Generate keystore: $keystore" | ||
| note "View keystore: keytool -list -v -keystore $keystore -storepass $pass" | ||
| fi | ||
|
|
||
| # generate a sub-certificate using the keytool | ||
| if [[ ! -z $subCA_flag ]]; then | ||
| prog "Subordinate-CA output directory: $generate_root" | ||
| if [[ -z "$CA" || -z "$CAkey" ]]; then | ||
| error "Missing options: '-cer' or '-cerKey', exiting..." | ||
| general_usage gen_subCA | ||
| exit 1 | ||
| fi | ||
|
|
||
| check_cert $CA $CAkey $pass | ||
| generate_sub_cert $generate_root $CA $CAkey $pass $CN | ||
| prog "Generate subordinate-CA: ${CN}.crt successfully" | ||
| fi | ||
|
|
||
| # generate truststore | ||
| if [[ ! -z ${genTruststore_flag:-} ]]; then | ||
| if [[ -z $storeName ]]; then | ||
| truststoreName=server.truststore | ||
| else | ||
| truststoreName=${storeName}.truststore | ||
| fi | ||
| truststore="${generate_root}/${truststoreName}" | ||
| if [ ! -f "${truststore}" ]; then | ||
| prog "Generate truststore: ${truststore}" | ||
| generate_truststore "${generate_root}" "${truststoreName}" "${storepass}" "${storetype}" | ||
| else | ||
| warn "${truststore} already exists, skipping generation!" | ||
| fi | ||
| note "View truststore: keytool -list -v -keystore ${truststore} -storepass ${storepass}" | ||
| fi | ||
| fi | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if user don't provide any options, it is default behavior as below:
(1)
./ssl_generate.sh<==>
./ssl_generate.sh --gen_CARoot --gen_keystore --gen_truststore(2)
./ssl_generate.sh --CN <DN name>./ssl_generate.sh --gen_CARoot --CN <DN name> --gen_keystore --gen_truststore