Skip to content

Commit 9003f3a

Browse files
committed
refactor: extract magic numbers to named constants
Move hardcoded values to readonly constants at the top of the script: - CERT_VALIDITY_DAYS: certificate expiry (10 years) - CRL_VALIDITY_DAYS: CRL expiry (10 years) - EASYRSA_VERSION: easy-rsa version - EASYRSA_SHA256: easy-rsa checksum This improves maintainability and makes it easier to update these values in the future.
1 parent 3d9a301 commit 9003f3a

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

openvpn-install.sh

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66
# Secure OpenVPN server installer for Debian, Ubuntu, CentOS, Amazon Linux 2, Fedora, Oracle Linux 8, Arch Linux, Rocky Linux and AlmaLinux.
77
# https://github.com/angristan/openvpn-install
88

9+
# Configuration constants
10+
readonly CERT_VALIDITY_DAYS=3650 # 10 years
11+
readonly CRL_VALIDITY_DAYS=3650 # 10 years
12+
readonly EASYRSA_VERSION="3.1.2"
13+
readonly EASYRSA_SHA256="d63cf129490ffd6d8792ede7344806c506c82c32428b5bb609ad97ca6a6e4499"
14+
915
function isRoot() {
1016
if [ "$EUID" -ne 0 ]; then
1117
return 1
@@ -753,10 +759,8 @@ function installOpenVPN() {
753759
754760
# Install the latest version of easy-rsa from source, if not already installed.
755761
if [[ ! -d /etc/openvpn/easy-rsa/ ]]; then
756-
local version="3.1.2"
757-
local easy_rsa_sha256="d63cf129490ffd6d8792ede7344806c506c82c32428b5bb609ad97ca6a6e4499"
758-
wget -O ~/easy-rsa.tgz https://github.com/OpenVPN/easy-rsa/releases/download/v${version}/EasyRSA-${version}.tgz
759-
if ! echo "${easy_rsa_sha256} ~/easy-rsa.tgz" | sha256sum -c; then
762+
wget -O ~/easy-rsa.tgz "https://github.com/OpenVPN/easy-rsa/releases/download/v${EASYRSA_VERSION}/EasyRSA-${EASYRSA_VERSION}.tgz"
763+
if ! echo "${EASYRSA_SHA256} ~/easy-rsa.tgz" | sha256sum -c; then
760764
echo "SHA256 checksum verification failed for easy-rsa download!"
761765
rm -f ~/easy-rsa.tgz
762766
exit 1
@@ -784,15 +788,15 @@ function installOpenVPN() {
784788
785789
# Create the PKI, set up the CA, the DH params and the server certificate
786790
./easyrsa init-pki
787-
EASYRSA_CA_EXPIRE=3650 ./easyrsa --batch --req-cn="$SERVER_CN" build-ca nopass
791+
EASYRSA_CA_EXPIRE=$CERT_VALIDITY_DAYS ./easyrsa --batch --req-cn="$SERVER_CN" build-ca nopass
788792
789793
if [[ $DH_TYPE == "2" ]]; then
790794
# ECDH keys are generated on-the-fly so we don't need to generate them beforehand
791795
openssl dhparam -out dh.pem "$DH_KEY_SIZE"
792796
fi
793797
794-
EASYRSA_CERT_EXPIRE=3650 ./easyrsa --batch build-server-full "$SERVER_NAME" nopass
795-
EASYRSA_CRL_DAYS=3650 ./easyrsa gen-crl
798+
EASYRSA_CERT_EXPIRE=$CERT_VALIDITY_DAYS ./easyrsa --batch build-server-full "$SERVER_NAME" nopass
799+
EASYRSA_CRL_DAYS=$CRL_VALIDITY_DAYS ./easyrsa gen-crl
796800
797801
case $TLS_SIG in
798802
1)
@@ -1138,11 +1142,11 @@ function newClient() {
11381142
cd /etc/openvpn/easy-rsa/ || return
11391143
case $PASS in
11401144
1)
1141-
EASYRSA_CERT_EXPIRE=3650 ./easyrsa --batch build-client-full "$CLIENT" nopass
1145+
EASYRSA_CERT_EXPIRE=$CERT_VALIDITY_DAYS ./easyrsa --batch build-client-full "$CLIENT" nopass
11421146
;;
11431147
2)
11441148
echo "⚠️ You will be asked for the client password below ⚠️"
1145-
EASYRSA_CERT_EXPIRE=3650 ./easyrsa --batch build-client-full "$CLIENT"
1149+
EASYRSA_CERT_EXPIRE=$CERT_VALIDITY_DAYS ./easyrsa --batch build-client-full "$CLIENT"
11461150
;;
11471151
esac
11481152
echo "Client $CLIENT added."
@@ -1230,7 +1234,7 @@ function revokeClient() {
12301234
CLIENT=$(tail -n +2 /etc/openvpn/easy-rsa/pki/index.txt | grep "^V" | cut -d '=' -f 2 | sed -n "$CLIENTNUMBER"p)
12311235
cd /etc/openvpn/easy-rsa/ || return
12321236
./easyrsa --batch revoke "$CLIENT"
1233-
EASYRSA_CRL_DAYS=3650 ./easyrsa gen-crl
1237+
EASYRSA_CRL_DAYS=$CRL_VALIDITY_DAYS ./easyrsa gen-crl
12341238
rm -f /etc/openvpn/crl.pem
12351239
cp /etc/openvpn/easy-rsa/pki/crl.pem /etc/openvpn/crl.pem
12361240
chmod 644 /etc/openvpn/crl.pem

0 commit comments

Comments
 (0)