Skip to content

Commit 5431bf2

Browse files
committed
Added deployer installation script
1 parent 1e27429 commit 5431bf2

File tree

5 files changed

+92
-174
lines changed

5 files changed

+92
-174
lines changed

deploy/freenas.sh

Lines changed: 66 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ freenas_deploy() {
2626
_debug _cca "$_cca"
2727
_debug _cfullchain "$_cfullchain"
2828

29+
_fullchain=$(tr '\n\r' '@#' <"$_cfullchain" | sed 's/@/\\n/g;s/#/\\r/g')
30+
_key=$(tr '\n\r' '@#' <"$_ckey" | sed 's/@/\\n/g;s/#/\\r/g')
31+
32+
_debug _fullchain "$_fullchain"
33+
_debug _key "$_key"
34+
2935
if [ -z "$FREENAS_PASSWORD" ]; then
3036
if [ -z "$Le_Deploy_FreeNAS_password" ]; then
3137
_err "FREENAS_PASSWORD not defined."
@@ -56,10 +62,64 @@ freenas_deploy() {
5662
_savedomainconf Le_Deploy_FreeNAS_verify "$Le_Deploy_FreeNAS_verify"
5763
fi
5864

59-
api_base="${Le_Deploy_FreeNAS_host}/api/v1.0"
60-
cert=$(date +letsencrypt-%Y-%m-%d-%H%M%S)
61-
credentials=$(printf "%s:%s" "root" "$Le_Deploy_FreeNAS_password" | _base64)
65+
_api_base="${Le_Deploy_FreeNAS_host}/api/v1.0"
66+
# _cert=$(date +letsencrypt-%Y-%m-%d-%H%M%S)
67+
_cert=$(date +letsencrypt-%Y-%m-%d)
68+
_realm=$(printf "%s:%s" "root" "$Le_Deploy_FreeNAS_password" | _base64)
69+
70+
_debug _api_base "$_api_base"
71+
_debug _cert "$_cert"
72+
_debug _realm "$_realm"
73+
74+
_info "Update or create SSL certificate"
75+
export _H1="Authorization: Basic $_realm"
76+
export _H2="Content-Type: application/json"
77+
_request="{\"cert_name\":\"$_cert\",\"cert_certificate\":\"$_fullchain\",\"cert_privatekey\":\"$_key\"}"
78+
_debug _request "$_request"
79+
_response="$(_post "$_request" "$_api_base/system/certificate/import/")"
80+
_debug _response "$_response"
81+
82+
if echo "$_response" | grep -q "certificate with this name already exists"; then
83+
_err "SSL certificate with name '$_cert' are already exists. Stop deploying"
84+
return 0
85+
elif [ "$_response" != "Certificate imported." ]; then
86+
_err "Error SSL certificate import"
87+
return 1
88+
fi
89+
90+
_info "Download certificate list and parse it to find the ID that matches our cert name"
91+
_response=$(_get "$_api_base/system/certificate/?limit=0")
92+
_debug _response "$_response"
93+
_regex="^.*\"cert_name\": *\"$_cert\".*$"
94+
_debug _regex "$_regex"
95+
_resource=$(echo "$_response" | sed 's/},{/},\n{/g' | _egrep_o "$_regex")
96+
_debug _resource "$_resource"
97+
_regex="^.*\"cert_name\": \"$_cert\".*$"
98+
_debug _regex "$_regex"
99+
_resource=$(echo "$_response" | sed 's/},{/},\n{/g' | _egrep_o "$_regex")
100+
_debug _resource "$_resource"
101+
_regex=".*\"id\": *\([0-9]*\).*$"
102+
_debug _regex "$_regex"
103+
_cert_id=$(echo "$_resource" | sed -n "s/$_regex/\1/p")
104+
_debug _resourceId "$_cert_id"
105+
106+
_info "Set our cert as active"
107+
_request="{\"stg_guicertificate\":\"$_cert_id\"}"
108+
_response=$(_post "$_request" "$_api_base/system/settings/" '' "PUT")
109+
_debug _response "$_response"
110+
111+
_info "Reload nginx with new cert"
112+
_response="$(_post "" "$_api_base/system/settings/restart-httpd-all/")"
113+
_debug _response "$_response"
114+
115+
# Make time for httpd for reloading
116+
sleep 3
117+
118+
_info "Set our cert as active for FTP plugin"
119+
_request="{\"ftp_ssltls_certfile\":\"$_cert\"}"
120+
_response=$(_post "$_request" "$_api_base/services/ftp/" '' "PUT")
121+
_debug _response "$_response"
62122

63-
_err "Not implemented yet"
64-
return 1
65-
}
123+
_info "Certificate successfully deployed"
124+
return 0
125+
}

deploy_freenas.py

Lines changed: 0 additions & 151 deletions
This file was deleted.

deploy_freenas.sh

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
#!/usr/bin/env sh
22

3-
DEBUG=3
4-
53

64

75
_locate() {
@@ -26,7 +24,7 @@ IS_OPNSENSE=$([ -d "/usr/local/opnsense/" ] && echo 1)
2624
# Locate acme.sh and load it as a library
2725
ACME=$(_locate acme.sh /root/.acme.sh /usr/local/sbin "$WDIR")
2826

29-
if [ -z "$ACME" ] || [ `find "$ACME" -mtime +30` ]; then
27+
if [ -z "$ACME" ] || [ ! -z "$(find "$WDIR/acme.sh" -mtime +30 2>/dev/null)" ]; then
3028
if [ ! -z "$FETCH" ]; then
3129
"$FETCH" https://raw.githubusercontent.com/Neilpang/acme.sh/master/acme.sh
3230
elif [ ! -z "$CURL" ]; then
@@ -39,7 +37,8 @@ fi
3937
if [ -z "$ACME" ]; then echo "ERROR: Can't locate acme.sh"; exit 1; fi
4038

4139
if [ "$IS_OPNSENSE" == "1" ]; then
42-
LE_WORKING_DIR="$WDIR"
40+
LE_WORKING_DIR="/var/etc/acme-client/home"
41+
_SCRIPT_HOME="$WDIR"
4342
else
4443
LE_WORKING_DIR=`dirname $ACME`
4544
fi
@@ -82,13 +81,19 @@ eval $(_parse_ini ${CONFIG})
8281
if [ -z "${ini__deploy__password}" ]; then _err "ERROR: Root password not defined!"; exit 1; fi
8382

8483
DOMAIN_NAME=${ini__deploy__cert_fqdn:-$(hostname)}
84+
CERT_KEY_PATH=${ini__deploy__privkey_path:-"/root/.acme.sh/${DOMAIN_NAME}/${DOMAIN_NAME}.key"}
85+
CERT_FULLCHAIN_PATH=${ini__deploy__fullchain_path:-"/root/.acme.sh/${DOMAIN_NAME}/fullchain.cer"}
8586
export FREENAS_PASSWORD=${ini__deploy__password}
8687
export FREENAS_HOST="${ini__deploy__protocol:-"http://"}${ini__deploy__connect_host:-"localhost"}:${ini__deploy__port:-"80"}"
8788
export FREENAS_VERIFY=${ini__deploy__verify:-"true"}
8889

89-
_debug DOMAIN_NAME ${DOMAIN_NAME}
90-
_debug FREENAS_PASSWORD ${FREENAS_PASSWORD}
91-
_debug FREENAS_HOST ${FREENAS_HOST}
92-
_debug FREENAS_VERIFY ${FREENAS_VERIFY}
90+
_debug DOMAIN_NAME "$DOMAIN_NAME"
91+
_debug CERT_KEY_PATH "$CERT_KEY_PATH"
92+
_debug CERT_FULLCHAIN_PATH "$CERT_FULLCHAIN_PATH"
93+
_debug FREENAS_PASSWORD "$FREENAS_PASSWORD"
94+
_debug FREENAS_HOST "$FREENAS_HOST"
95+
_debug FREENAS_VERIFY "$FREENAS_VERIFY"
96+
97+
. "$WDIR/deploy/freenas.sh"
9398

94-
_deploy ${DOMAIN_NAME} "freenas"
99+
freenas_deploy "$DOMAIN_NAME" "$CERT_KEY_PATH" "" "" "$CERT_FULLCHAIN_PATH"

install.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/usr/bin/env sh
2+
3+
WDIR=$(cd `dirname $0` && pwd)
4+
ACME="/root/.acme.sh"
5+
6+
if [ ! -r "$ACME/acme.sh" ]; then echo "ERROR: Can't locate ACME directory"; exit 1; fi
7+
8+
cp -R "$WDIR/deploy/" "$ACME/deploy/"
9+
10+
echo "The SSL certificate deployer for FreeNAS are successfully installed to ACME."

util/opnsense/install.sh

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
11
#!/usr/bin/env sh
22

3-
DIR=$(cd `dirname $0` && pwd)
4-
5-
# Make service links
6-
ln -Fs /var/etc/acme-client/home/ /root/.acme.sh
7-
8-
# Install required packages
9-
#pkg install -y bash
3+
WDIR=$(cd `dirname $0` && pwd)
104

115
# Install cron action
12-
ln -Fs ${DIR}/actions_deploy_freenas.conf /usr/local/opnsense/service/conf/actions.d/actions_deploy_freenas.conf
6+
ln -Fs ${WDIR}/actions_deploy_freenas.conf /usr/local/opnsense/service/conf/actions.d/actions_deploy_freenas.conf
137

148
# Reload config daemon
159
service configd restart

0 commit comments

Comments
 (0)