Skip to content

Commit 5396baf

Browse files
committed
Adds logic to check script requirements and trust CAs on Linux
1 parent 5cfe125 commit 5396baf

File tree

1 file changed

+71
-16
lines changed

1 file changed

+71
-16
lines changed

self-signed-tls

Lines changed: 71 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,39 @@ _safe_exit() {
5151
exit "$@"
5252
}
5353

54+
#######################################
55+
# Check if packages are installed
56+
# Arguments:
57+
# List of commands (strings) to verify
58+
# Outputs:
59+
# Writes message to stderr and returns error code if package not present
60+
#######################################
61+
_require() {
62+
while [ -n "$1" ]; do
63+
if [ -z "$(command -v "$1")" ]; then
64+
printf "Command '%s' not found\n" "${1}"
65+
printf "Please ensure the program is installed and referenced in PATH variable\n" >&2
66+
return 1
67+
fi
68+
shift
69+
done
70+
}
71+
72+
#######################################
73+
# Validate script requirements
74+
# Arguments:
75+
# None
76+
# Outputs:
77+
# Exits script if required package not found
78+
#######################################
79+
_check_requirements() {
80+
# Require OpenSSL
81+
_require "openssl" || _safe_exit 1
82+
83+
# Require sudo if trusting CA
84+
[ -z "${TRUST}" ] || _require "sudo" || _safe_exit 1
85+
}
86+
5487
#######################################
5588
# Display the help Screen
5689
# Arguments:
@@ -338,6 +371,26 @@ _validate_args() {
338371
fi
339372
}
340373

374+
#######################################
375+
# Trust certificate authority on Linux system
376+
# Arguments:
377+
# Command (string)
378+
# Directory (string)
379+
#######################################
380+
_trust_linux() {
381+
# If command and directory exist
382+
if [ -n "$(command -v "$1")" ] && [ -d "$(dirname "$2")" ]; then
383+
printf "Installing certificate authority (requires sudo privileges)\n"
384+
385+
# Add certificate if it doesn't exist & trust it
386+
[ -f "$2" ] || sudo cp "${CA}" "$2" \
387+
&& sudo "$1" \
388+
&& return
389+
fi
390+
391+
return 1
392+
}
393+
341394
#######################################
342395
# Trust certificate authority
343396
# Globals:
@@ -348,22 +401,23 @@ _validate_args() {
348401
_trust_ca() {
349402
# Check if CA exists and script is instructed to trust
350403
if [ -f "${CA_KEY}" ] && [ -f "${CA}" ] && [ -n "${TRUST}" ]; then
351-
case "${OSTYPE:-undefined}" in
352-
# MacOS
353-
darwin*)
354-
sudo security add-trusted-cert \
355-
-d \
356-
-r trustRoot \
357-
-k "/Library/Keychains/System.keychain" \
358-
"${CA}"
359-
;;
360-
# TODO: Implement certificate trusting for other systems
361-
# linux*) ;;
362-
*)
363-
printf "Error: Unsupported OSTYPE '%s'\n" "${OSTYPE:-undefined}"
364-
EXIT_CODE=1
365-
;;
366-
esac
404+
if [[ "${OSTYPE}" == "darwin"* ]]; then
405+
# MacOS (Darwin)
406+
sudo security add-trusted-cert -d -r trustRoot \
407+
-k "/Library/Keychains/System.keychain" \
408+
"${CA}" \
409+
&& return
410+
elif [[ "${OSTYPE}" == "linux"* ]]; then
411+
# Linux (Fedora/CentOS, Debian/Ubuntu)
412+
_trust_linux "update-ca-trust" "/etc/pki/ca-trust/source/anchors/${FILE}-ca.pem" \
413+
|| _trust_linux "update-ca-certificates" "/usr/local/share/ca-certificates/${FILE}-ca.crt" \
414+
&& return
415+
fi
416+
417+
# Unsupported OS
418+
printf "Error occurred while trusting certificate for OSTYPE '%s'\n" "${OSTYPE:-undefined}" >&2
419+
printf "Please ensure you are on a supported system and have the required packages installed.\n" >&2
420+
EXIT_CODE=1
367421
fi
368422
}
369423

@@ -463,6 +517,7 @@ EOF
463517
# Order of execution
464518
#######################################
465519
_parse_args "$@"
520+
_check_requirements
466521
_validate_args
467522
_build_ca
468523
_trust_ca

0 commit comments

Comments
 (0)