-
-
Save ormaaj/b1cfebf10767b38c188039579ba38747 to your computer and use it in GitHub Desktop.
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
# Define a new type "SslCmd" with three fields: "outfile", "args", and "cmd". "cmd" has a default value of "openssl" that can be overridden by an object initializer list. | |
# The type defines a public method named "run". | |
# The result of this declaration is a new declaration command "SslCmd" that can be used in place of "typeset". | |
typeset -T SslCmd=( | |
typeset -h 'output file' outfile | |
typeset -h 'arguments passed to the command name passed to _.run' -a args | |
typeset -h 'The command to run' cmd=openssl | |
# A method associated with each instance that runs the command and associated args. | |
# _ (in this context) is a pointer to the instance, similar to "this" or "self". | |
function run { | |
[[ -f ${_.outfile} ]] && return 1 | |
"${1:-${_.cmd}}" "${_.args[@]}" | |
} | |
) | |
function main { | |
# Define a "struct" that contains configuration information. | |
compound config=( | |
serverKey=server-key.pem | |
caSubj='/C=US/O=ormaaj.org/CN=ormaaj' | |
hostSubj='/C=US/O=ormaaj.org/CN=ormaaj' | |
) | |
# Declare and initialize an indexed array of SslCmd objects. | |
SslCmd -a cmds=( | |
(outfile=ca-key.pem; args=(genrsa -des3 -out ca-key.pem 1024)) | |
(outfile=ca-cert.pem; args=(req -new -x509 -days 1095 -key ca-key.pem -out ca-cert.pem -utf8 -subj "${config.caSubj}")) | |
(outfile=${config.serverKey}; args=(genrsa -out "${config.serverKey}" 1024)) | |
(outfile=server-key.csr; args=(req -new -key "${config.serverKey}" -out server-key.csr -utf8 -subj "${config.hostSubj}")) | |
(outfile=server-cert.pem; args=(x509 -req -days 1095 -in server-key.csr -CA ca-cert.pem -CAkey ca-key.pem -set_serial 01 -out server-cert.pem)) | |
) | |
# Iterate over the keys of the "cmds" array and call each run method. | |
typeset x | |
for x in "${!cmds[@]}"; do | |
if ! 'cmds[x].run'; then | |
printf 'failed running command:\n %s\n' "${cmds[x]}" >&2 | |
return 1 | |
fi | |
done | |
# Create a key | |
openssl rsa -in "${config.serverKey}" -out "${config.serverKey}.insecure" | |
mv -- "${config.serverKey}" "${config.serverKey}.secure" | |
mv -- "${config.serverKey}.insecure" "${config.serverKey}" | |
# showResults is a 2-dimensional array of argument lists to display the results. | |
typeset -a showResults=( | |
(rsa -noout -text -in "${config.serverKey}") | |
(rsa -noout -text -in ca-key.pem) | |
(req -noout -text -in server-key.csr) | |
(x509 -noout -text -in server-cert.pem) | |
(x509 -noout -text -in ca-cert.pem) | |
) | |
for x in "${!showResults[@]}"; do | |
openssl "${showResults[x][@]}" | |
done | |
} | |
main "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment