]> git.proxmox.com Git - mirror_acme.sh.git/blob - deploy/truenas.sh
Update truenas.sh
[mirror_acme.sh.git] / deploy / truenas.sh
1 #!/usr/bin/env sh
2
3 # Here is a scipt to deploy the cert to your TrueNAS using the REST API.
4 # https://www.truenas.com/docs/hub/additional-topics/api/rest_api.html
5 #
6 # Written by Frank Plass github@f-plass.de
7 # https://github.com/danb35/deploy-freenas/blob/master/deploy_freenas.py
8 # Thanks to danb35 for your template!
9 #
10 # Following environment variables must be set:
11 #
12 # export DEPLOY_TRUENAS_APIKEY="<API_KEY_GENERATED_IN_THE_WEB_UI"
13 #
14 # The following environmental variables may be set if you don't like their
15 # default values:
16 #
17 # DEPLOY_TRUENAS_HOSTNAME - defaults to localhost
18 # DEPLOY_TRUENAS_SCHEME - defaults to http, set alternatively to https
19 #
20 #returns 0 means success, otherwise error.
21
22 ######## Public functions #####################
23
24 #domain keyfile certfile cafile fullchain
25 truenas_deploy() {
26 _cdomain="$1"
27 _ckey="$2"
28 _ccert="$3"
29 _cca="$4"
30 _cfullchain="$5"
31
32 _debug _cdomain "$_cdomain"
33 _debug _ckey "$_ckey"
34 _debug _ccert "$_ccert"
35 _debug _cca "$_cca"
36 _debug _cfullchain "$_cfullchain"
37
38 _getdeployconf DEPLOY_TRUENAS_APIKEY
39
40 if [ -z "$DEPLOY_TRUENAS_APIKEY" ]; then
41 _err "TrueNAS Api Key is not found, please define DEPLOY_TRUENAS_APIKEY."
42 return 1
43 fi
44 _secure_debug2 DEPLOY_TRUENAS_APIKEY "$DEPLOY_TRUENAS_APIKEY"
45
46 # Optional hostname, scheme for TrueNAS
47 _getdeployconf DEPLOY_TRUENAS_HOSTNAME
48 _getdeployconf DEPLOY_TRUENAS_SCHEME
49
50 # default values for hostname and scheme
51 [ -n "${DEPLOY_TRUENAS_HOSTNAME}" ] || DEPLOY_TRUENAS_HOSTNAME="localhost"
52 [ -n "${DEPLOY_TRUENAS_SCHEME}" ] || DEPLOY_TRUENAS_SCHEME="http"
53
54 _debug2 DEPLOY_TRUENAS_HOSTNAME "$DEPLOY_TRUENAS_HOSTNAME"
55 _debug2 DEPLOY_TRUENAS_SCHEME "$DEPLOY_TRUENAS_SCHEME"
56
57 _api_url="$DEPLOY_TRUENAS_SCHEME://$DEPLOY_TRUENAS_HOSTNAME/api/v2.0"
58 _debug _api_url "$_api_url"
59
60 _H1="Authorization: Bearer $DEPLOY_TRUENAS_APIKEY"
61 _secure_debug3 _H1 "$_H1"
62
63 _info "Testing Connection TrueNAS"
64 _response=$(_get "$_api_url/system/state")
65 _info "TrueNAS System State: $_response."
66
67 if [ -z "$_response" ]; then
68 _err "Unable to authenticate to $_api_url."
69 _err 'Check your Connection and set DEPLOY_TRUENAS_HOSTNAME="192.168.178.x".'
70 _err 'or'
71 _err 'set DEPLOY_TRUENAS_HOSTNAME="<truenas_dnsname>".'
72 _err 'Check your Connection and set DEPLOY_TRUENAS_SCHEME="https".'
73 _err "Check your Api Key."
74 return 1
75 fi
76
77 _saveaccountconf DEPLOY_TRUENAS_APIKEY "$DEPLOY_TRUENAS_APIKEY"
78 _saveaccountconf DEPLOY_TRUENAS_HOSTNAME "$DEPLOY_TRUENAS_HOSTNAME"
79 _saveaccountconf DEPLOY_TRUENAS_SCHEME "$DEPLOY_TRUENAS_SCHEME"
80
81
82 _info "Getting active certificate from TrueNAS"
83 _response=$(_get "$_api_url/system/general")
84 _active_cert_id=$(echo "$_response" | grep -B2 '"name":' | grep 'id' | tr -d -- '"id: ,')
85 _active_cert_name=$(echo "$_response" | grep '"name":' | sed -n 's/.*: "\(.\{1,\}\)",$/\1/p')
86 _param_httpsredirect=$(echo "$_response" | grep '"ui_httpsredirect":' | sed -n 's/.*": \(.\{1,\}\),$/\1/p')
87 _debug Active_UI_Certificate_ID "$_active_cert_id"
88 _debug Active_UI_Certificate_Name "$_active_cert_name"
89 _debug Active_UI_http_redirect "$_param_httpsredirect"
90
91 if [ "$DEPLOY_TRUENAS_SCHEME" = "http" ] && [ "$_param_httpsredirect" = "true" ] ; then
92 _info "http Redirect active"
93 _info "Setting DEPLOY_TRUENAS_SCHEME to 'https'"
94 DEPLOY_TRUENAS_SCHEME="https"
95 _api_url="$DEPLOY_TRUENAS_SCHEME://$DEPLOY_TRUENAS_HOSTNAME/api/v2.0"
96 _saveaccountconf DEPLOY_TRUENAS_SCHEME "$DEPLOY_TRUENAS_SCHEME"
97 fi
98
99
100 _info "Upload new certifikate to TrueNAS"
101 _date_now() {
102 date -u "+%Y-%m-%d_%H%M%S"
103 }
104 _certname="Letsencrypt_$(_date_now)"
105 _debug3 _certname "$_certname"
106
107 _certData="{\"create_type\": \"CERTIFICATE_CREATE_IMPORTED\", \"name\": \"${_certname}\", \"certificate\": \"$(_json_encode <"$_cfullchain")\", \"privatekey\": \"$(_json_encode <"$_ckey")\"}"
108 _add_cert_result="$(_post "$_certData" "$_api_url/certificate" "" "POST" "application/json")"
109
110 _debug3 _add_cert_result "$_add_cert_result"
111
112
113 _info "Getting Certificate list to get new Cert ID"
114 _cert_list=$(_get "$_api_url/system/general/ui_certificate_choices")
115 _cert_id=$(echo "$_cert_list" | grep "$_certname" | sed -n 's/.*"\([0-9]\{1,\}\)".*$/\1/p')
116
117 _debug3 _cert_id "$_cert_id"
118
119
120 _info "Activate Certificate ID: $_cert_id"
121 _activateData="{\"ui_certificate\": \"${_cert_id}\"}"
122 _activate_result="$(_post "$_activateData" "$_api_url/system/general" "" "PUT" "application/json")"
123
124 _debug3 _activate_result "$_activate_result"
125
126
127 _info "Check if WebDAV certificate is the same as the WEB UI"
128 _webdav_list=$(_get "$_api_url/webdav")
129 _webdav_cert_id=$(echo "$_webdav_list" | grep '"certssl":' | tr -d -- '"certsl: ,')
130
131 if [ "$_webdav_cert_id" = "$_active_cert_id" ]; then
132 _info "Update the WebDAV Certificate"
133 _debug _webdav_cert_id "$_webdav_cert_id"
134 _webdav_data="{\"certssl\": \"${_cert_id}\"}"
135 _activate_webdav_cert="$(_post "$_webdav_data" "$_api_url/webdav" "" "PUT" "application/json")"
136 _webdav_new_cert_id=$(echo "$_activate_webdav_cert" | _json_decode | sed -n 's/.*: \([0-9]\{1,\}\) }$/\1/p')
137 if [ "$_webdav_new_cert_id" -eq "$_cert_id" ]; then
138 _info "WebDAV Certificate update successfully"
139 else
140 _err "Unable to set WebDAV certificate"
141 _debug3 _activate_webdav_cert "$_activate_webdav_cert"
142 _debug3 _webdav_new_cert_id "$_webdav_new_cert_id"
143 return 1
144 fi
145 _debug3 _webdav_new_cert_id "$_webdav_new_cert_id"
146 else
147 _info "WebDAV certificate not set or not the same as Web UI"
148 fi
149
150
151 _info "Check if FTP certificate is the same as the WEB UI"
152 _ftp_list=$(_get "$_api_url/ftp")
153 _ftp_cert_id=$(echo "$_ftp_list" | grep '"ssltls_certificate":' | tr -d -- '"certislfa:_ ,')
154
155 if [ "$_ftp_cert_id" = "$_active_cert_id" ]; then
156 _info "Update the FTP Certificate"
157 _debug _ftp_cert_id "$_ftp_cert_id"
158 _ftp_data="{\"ssltls_certificate\": \"${_cert_id}\"}"
159 _activate_ftp_cert="$(_post "$_ftp_data" "$_api_url/ftp" "" "PUT" "application/json")"
160 _ftp_new_cert_id=$(echo "$_activate_ftp_cert" | _json_decode | sed -n 's/.*: \([0-9]\{1,\}\) }$/\1/p')
161 if [ "$_ftp_new_cert_id" -eq "$_cert_id" ]; then
162 _info "FTP Certificate update successfully"
163 else
164 _err "Unable to set FTP certificate"
165 _debug3 _activate_ftp_cert "$_activate_ftp_cert"
166 _debug3 _ftp_new_cert_id "$_ftp_new_cert_id"
167 return 1
168 fi
169 _debug3 _activate_ftp_cert "$_activate_ftp_cert"
170 else
171 _info "FTP certificate not set or not the same as Web UI"
172 fi
173
174
175 _info "Delete old Certificate"
176 _delete_result="$(_post "" "$_api_url/certificate/id/$_active_cert_id" "" "DELETE" "application/json")"
177
178 _debug3 _delete_result "$_delete_result"
179
180
181 _info "Reload WebUI from TrueNAS"
182 # the command
183 # _restart_UI=$(_get "$_api_url/system/general/ui_restart")
184 # throws the Error 52
185 # for this command direct curl command
186 curl --silent -L --no-keepalive --user-agent "$USER_AGENT" -H "$_H1" "$_api_url/system/general/ui_restart"
187 _ret=$?
188 _debug2 CURL_RETURN "$_ret"
189
190 if [ -n "$_add_cert_result" ] && [ -n "$_activate_result" ] && [ "$_ret" = "52" ]; then
191 return 0
192 else
193 _err "Please refer to https://curl.haxx.se/libcurl/c/libcurl-errors.html for error code: $_ret"
194 return 1
195 fi
196 }